Esempio n. 1
0
 public void inform(SolrCore core) {
   if (initParams != null) {
     log.info("Initializing Clustering Engines");
     boolean searchHasDefault = false;
     boolean documentHasDefault = false;
     for (int i = 0; i < initParams.size(); i++) {
       if (initParams.getName(i).equals("engine")) {
         NamedList engineNL = (NamedList) initParams.getVal(i);
         String className = (String) engineNL.get("classname");
         if (className == null) {
           className = CarrotClusteringEngine.class.getName();
         }
         SolrResourceLoader loader = core.getResourceLoader();
         ClusteringEngine clusterer = (ClusteringEngine) loader.newInstance(className);
         if (clusterer != null) {
           String name = clusterer.init(engineNL, core);
           if (name != null) {
             boolean isDefault = name.equals(ClusteringEngine.DEFAULT_ENGINE_NAME);
             if (clusterer instanceof SearchClusteringEngine) {
               if (isDefault == true && searchHasDefault == false) {
                 searchHasDefault = true;
               } else if (isDefault == true && searchHasDefault == true) {
                 throw new RuntimeException("More than one engine is missing name: " + engineNL);
               }
               searchClusteringEngines.put(name, (SearchClusteringEngine) clusterer);
             } else if (clusterer instanceof DocumentClusteringEngine) {
               if (isDefault == true && documentHasDefault == false) {
                 searchHasDefault = true;
               } else if (isDefault == true && documentHasDefault == true) {
                 throw new RuntimeException("More than one engine is missing name: " + engineNL);
               }
               documentClusteringEngines.put(name, (DocumentClusteringEngine) clusterer);
             }
           } else {
             if (clusterer instanceof SearchClusteringEngine && searchHasDefault == false) {
               searchClusteringEngines.put(
                   ClusteringEngine.DEFAULT_ENGINE_NAME, (SearchClusteringEngine) clusterer);
               searchHasDefault = true;
             } else if (clusterer instanceof DocumentClusteringEngine
                 && documentHasDefault == false) {
               documentClusteringEngines.put(
                   ClusteringEngine.DEFAULT_ENGINE_NAME, (DocumentClusteringEngine) clusterer);
               documentHasDefault = true;
             } else {
               throw new RuntimeException("More than one engine is missing name: " + engineNL);
             }
           }
         }
       }
     }
     log.info("Finished Initializing Clustering Engines");
   }
 }
  /**
   * Creates a new StorageIO instance for a Solr core, taking into account whether the core is
   * running in cloud mode as well as initArgs.
   */
  public static StorageIO newStorageIO(
      String collection, SolrResourceLoader resourceLoader, NamedList<String> initArgs) {
    StorageIO storageIO = null;

    SolrZkClient zkClient = null;
    String zkConfigName = null;
    if (resourceLoader instanceof ZkSolrResourceLoader) {
      zkClient = ((ZkSolrResourceLoader) resourceLoader).getZkController().getZkClient();
      try {
        zkConfigName =
            ((ZkSolrResourceLoader) resourceLoader)
                .getZkController()
                .getZkStateReader()
                .readConfigName(collection);
      } catch (Exception e) {
        log.error(
            "Failed to get config name for collection {} due to: {}", collection, e.toString());
      }
      if (zkConfigName == null) {
        throw new SolrException(
            ErrorCode.SERVER_ERROR, "Could not find config name for collection:" + collection);
      }
    }

    if (initArgs.get(STORAGE_IO_CLASS_INIT_ARG) != null) {
      storageIO =
          resourceLoader.newInstance(initArgs.get(STORAGE_IO_CLASS_INIT_ARG), StorageIO.class);
    } else {
      if (zkClient != null) {
        String znodeBase = "/configs/" + zkConfigName;
        log.info(
            "Setting up ZooKeeper-based storage for the RestManager with znodeBase: " + znodeBase);
        storageIO = new ManagedResourceStorage.ZooKeeperStorageIO(zkClient, znodeBase);
      } else {
        storageIO = new FileStorageIO();
      }
    }

    if (storageIO instanceof FileStorageIO) {
      // using local fs, if storageDir is not set in the solrconfig.xml, assume the configDir for
      // the core
      if (initArgs.get(STORAGE_DIR_INIT_ARG) == null) {
        initArgs.add(STORAGE_DIR_INIT_ARG, resourceLoader.getConfigDir());
      }
    }

    storageIO.configure(resourceLoader, initArgs);

    return storageIO;
  }
  @SuppressWarnings("unchecked")
  @Override
  public void inform(SolrCore core) {
    if (initParams != null) {
      log.info("Initializing Clustering Engines");

      // Our target list of engines, split into search-results and document clustering.
      SolrResourceLoader loader = core.getResourceLoader();

      for (Map.Entry<String, Object> entry : initParams) {
        if ("engine".equals(entry.getKey())) {
          NamedList<Object> engineInitParams = (NamedList<Object>) entry.getValue();

          String engineClassName =
              StringUtils.defaultIfBlank(
                  (String) engineInitParams.get("classname"),
                  CarrotClusteringEngine.class.getName());

          // Instantiate the clustering engine and split to appropriate map.
          final ClusteringEngine engine =
              loader.newInstance(engineClassName, ClusteringEngine.class);
          final String name = StringUtils.defaultIfBlank(engine.init(engineInitParams, core), "");
          final ClusteringEngine previousEntry;
          if (engine instanceof SearchClusteringEngine) {
            previousEntry = searchClusteringEngines.put(name, (SearchClusteringEngine) engine);
          } else if (engine instanceof DocumentClusteringEngine) {
            previousEntry = documentClusteringEngines.put(name, (DocumentClusteringEngine) engine);
          } else {
            log.warn("Unknown type of a clustering engine for class: " + engineClassName);
            continue;
          }
          if (previousEntry != null) {
            log.warn("Duplicate clustering engine component named '" + name + "'.");
          }
        }
      }

      // Set up the default engine key for both types of engines.
      setupDefaultEngine("search results clustering", searchClusteringEngines);
      setupDefaultEngine("document clustering", documentClusteringEngines);

      log.info("Finished Initializing Clustering Engines");
    }
  }