List of usage examples for org.apache.solr.common.cloud Aliases getCollectionAliasMap
@SuppressWarnings("unchecked") public Map<String, String> getCollectionAliasMap()
From source file:org.broadleafcommerce.core.search.service.solr.SolrConfiguration.java
License:Open Source License
@Override public void afterPropertiesSet() throws SolrServerException, IOException, IllegalStateException { if (CloudSolrClient.class.isAssignableFrom(getServer().getClass())) { //We want to use the Solr APIs to make sure the correct collections are set up. CloudSolrClient primary = (CloudSolrClient) primaryServer; CloudSolrClient reindex = (CloudSolrClient) reindexServer; if (primary == null || reindex == null) { throw new IllegalStateException("The primary and reindex CloudSolrServers must not be null. Check " + "your configuration and ensure that you are passing a different instance for each to the " + "constructor of " + this.getClass().getName() + " and ensure that each has a null (empty)" + " defaultCollection property, or ensure that defaultCollection is unique between" + " the two instances. All other things, like Zookeeper addresses should be the same."); }//from www. j a va 2 s . co m if (primary == reindex) { //These are the same object instances. They should be separate instances, with generally //the same configuration, except for the defaultCollection name. throw new IllegalStateException( "The primary and reindex CloudSolrServers must be different instances " + "and their defaultCollection property must be unique or null. All other things like the " + "Zookeeper addresses should be the same."); } //check if the default collection is null if (StringUtils.isEmpty(primary.getDefaultCollection())) { throw new IllegalStateException( "The primary CloudSolrServer must have a defaultCollection property set."); } else { this.setPrimaryName(primary.getDefaultCollection()); } //check if the default collection is null if (StringUtils.isEmpty(reindex.getDefaultCollection())) { throw new IllegalStateException( "The reindex CloudSolrServer must have a defaultCollection property set."); } else { this.setReindexName(reindex.getDefaultCollection()); } if (Objects.equals(primary.getDefaultCollection(), reindex.getDefaultCollection())) { throw new IllegalStateException("The primary and reindex CloudSolrServers must have " + "unique defaultCollection properties. All other things like the " + "Zookeeper addresses should be the same."); } primary.connect(); //This is required to ensure no NPE! //Get a list of existing collections so we don't overwrite one NamedList<Object> listResponse = new CollectionAdminRequest.List().process(primary).getResponse(); List<String> collectionNames = listResponse.get("collections") == null ? collectionNames = new ArrayList<String>() : (List<String>) listResponse.get("collections"); Aliases aliases = primary.getZkStateReader().getAliases(); Map<String, String> aliasCollectionMap = aliases.getCollectionAliasMap(); if (aliasCollectionMap == null || !aliasCollectionMap.containsKey(primary.getDefaultCollection())) { //Create a completely new collection String collectionName = null; for (int i = 0; i < 1000; i++) { collectionName = "blcCollection" + i; if (collectionNames.contains(collectionName)) { collectionName = null; } else { break; } } new CollectionAdminRequest.Create().setCollectionName(collectionName) .setNumShards(solrCloudNumShards).setConfigName(solrCloudConfigName).process(primary); new CollectionAdminRequest.CreateAlias().setAliasName(primary.getDefaultCollection()) .setAliasedCollections(collectionName).process(primary); } else { //Aliases can be mapped to collections that don't exist.... Make sure the collection exists String collectionName = aliasCollectionMap.get(primary.getDefaultCollection()); collectionName = collectionName.split(",")[0]; if (!collectionNames.contains(collectionName)) { new CollectionAdminRequest.Create().setCollectionName(collectionName) .setNumShards(solrCloudNumShards).setConfigName(solrCloudConfigName).process(primary); } } //Reload the collection names listResponse = new CollectionAdminRequest.List().process(primary).getResponse(); collectionNames = listResponse.get("collections") == null ? collectionNames = new ArrayList<String>() : (List<String>) listResponse.get("collections"); //Reload these maps for the next collection. aliases = primary.getZkStateReader().getAliases(); aliasCollectionMap = aliases.getCollectionAliasMap(); if (aliasCollectionMap == null || !aliasCollectionMap.containsKey(reindex.getDefaultCollection())) { //Create a completely new collection String collectionName = null; for (int i = 0; i < 1000; i++) { collectionName = "blcCollection" + i; if (collectionNames.contains(collectionName)) { collectionName = null; } else { break; } } new CollectionAdminRequest.Create().setCollectionName(collectionName) .setNumShards(solrCloudNumShards).setConfigName(solrCloudConfigName).process(primary); new CollectionAdminRequest.CreateAlias().setAliasName(reindex.getDefaultCollection()) .setAliasedCollections(collectionName).process(primary); } else { //Aliases can be mapped to collections that don't exist.... Make sure the collection exists String collectionName = aliasCollectionMap.get(reindex.getDefaultCollection()); collectionName = collectionName.split(",")[0]; if (!collectionNames.contains(collectionName)) { new CollectionAdminRequest.Create().setCollectionName(collectionName) .setNumShards(solrCloudNumShards).setConfigName(solrCloudConfigName).process(primary); } } } }
From source file:org.broadleafcommerce.core.search.service.solr.SolrHelperServiceImpl.java
License:Apache License
/** * This should only ever be called when using the Solr reindex service to do a full reindex. *///from w w w .j av a 2s .co m @Override public synchronized void swapActiveCores() throws ServiceException { if (SolrContext.isSolrCloudMode()) { CloudSolrServer primary = (CloudSolrServer) SolrContext.getServer(); CloudSolrServer reindex = (CloudSolrServer) SolrContext.getReindexServer(); try { primary.connect(); Aliases aliases = primary.getZkStateReader().getAliases(); Map<String, String> aliasCollectionMap = aliases.getCollectionAliasMap(); if (aliasCollectionMap == null || !aliasCollectionMap.containsKey(primary.getDefaultCollection()) || !aliasCollectionMap.containsKey(reindex.getDefaultCollection())) { throw new IllegalStateException("Could not determine the PRIMARY or REINDEX " + "collection or collections from the Solr aliases."); } String primaryCollectionName = aliasCollectionMap.get(primary.getDefaultCollection()); //Do this just in case primary is aliased to more than one collection primaryCollectionName = primaryCollectionName.split(",")[0]; String reindexCollectionName = aliasCollectionMap.get(reindex.getDefaultCollection()); //Do this just in case primary is aliased to more than one collection reindexCollectionName = reindexCollectionName.split(",")[0]; //Essentially "swap cores" here by reassigning the aliases CollectionAdminRequest.createAlias(primary.getDefaultCollection(), reindexCollectionName, primary); CollectionAdminRequest.createAlias(reindex.getDefaultCollection(), primaryCollectionName, primary); } catch (Exception e) { LOG.error("An exception occured swapping cores.", e); throw new ServiceException("Unable to swap SolrCloud collections after a full reindex.", e); } } else { if (SolrContext.isSingleCoreMode()) { LOG.debug("In single core mode. There are no cores to swap."); } else { LOG.debug("Swapping active cores"); CoreAdminRequest car = new CoreAdminRequest(); car.setCoreName(SolrContext.PRIMARY); car.setOtherCoreName(SolrContext.REINDEX); car.setAction(CoreAdminAction.SWAP); try { SolrContext.getAdminServer().request(car); } catch (Exception e) { LOG.error(e); throw new ServiceException("Unable to swap cores", e); } } } }
From source file:org.broadleafcommerce.core.search.service.solr.SolrSearchServiceImpl.java
License:Apache License
@Override public void afterPropertiesSet() throws Exception { if (SolrContext.isSolrCloudMode()) { //We want to use the Solr APIs to make sure the correct collections are set up. CloudSolrServer primary = (CloudSolrServer) SolrContext.getServer(); CloudSolrServer reindex = (CloudSolrServer) SolrContext.getReindexServer(); if (primary == null || reindex == null) { throw new IllegalStateException("The primary and reindex CloudSolrServers must not be null. Check " + "your configuration and ensure that you are passing a different instance for each to the " + "constructor of " + this.getClass().getName() + " and ensure that each has a null (empty)" + " defaultCollection property, or ensure that defaultCollection is unique between" + " the two instances. All other things, like Zookeeper addresses should be the same."); }/* w w w .j av a2s.c o m*/ if (primary == reindex) { //These are the same object instances. They should be separate instances, with generally //the same configuration, except for the defaultCollection name. throw new IllegalStateException( "The primary and reindex CloudSolrServers must be different instances " + "and their defaultCollection property must be unique or null. All other things like the " + "Zookeeper addresses should be the same."); } //Set the default collection if it's null if (StringUtils.isEmpty(primary.getDefaultCollection())) { primary.setDefaultCollection(SolrContext.PRIMARY); } //Set the default collection if it's null if (StringUtils.isEmpty(reindex.getDefaultCollection())) { reindex.setDefaultCollection(SolrContext.REINDEX); } if (primary.getDefaultCollection().equals(reindex.getDefaultCollection())) { throw new IllegalStateException( "The primary and reindex CloudSolrServers must have a null (empty) or " + "unique defaultCollection property. All other things like the " + "Zookeeper addresses should be the same."); } primary.connect(); //This is required to ensure no NPE! //Get a list of existing collections so we don't overwrite one Set<String> collectionNames = primary.getZkStateReader().getClusterState().getCollections(); if (collectionNames == null) { collectionNames = new HashSet<String>(); } Aliases aliases = primary.getZkStateReader().getAliases(); Map<String, String> aliasCollectionMap = aliases.getCollectionAliasMap(); if (aliasCollectionMap == null || !aliasCollectionMap.containsKey(primary.getDefaultCollection())) { //Create a completely new collection String collectionName = null; for (int i = 0; i < 1000; i++) { collectionName = "blcCollection" + i; if (collectionNames.contains(collectionName)) { collectionName = null; } else { break; } } CollectionAdminRequest.createCollection(collectionName, solrCloudNumShards, solrCloudConfigName, primary); CollectionAdminRequest.createAlias(primary.getDefaultCollection(), collectionName, primary); } else { //Aliases can be mapped to collections that don't exist.... Make sure the collection exists String collectionName = aliasCollectionMap.get(primary.getDefaultCollection()); collectionName = collectionName.split(",")[0]; if (!collectionNames.contains(collectionName)) { CollectionAdminRequest.createCollection(collectionName, solrCloudNumShards, solrCloudConfigName, primary); } } //Reload the collection names collectionNames = primary.getZkStateReader().getClusterState().getCollections(); if (collectionNames == null) { collectionNames = new HashSet<String>(); } //Reload these maps for the next collection. aliases = primary.getZkStateReader().getAliases(); aliasCollectionMap = aliases.getCollectionAliasMap(); if (aliasCollectionMap == null || !aliasCollectionMap.containsKey(reindex.getDefaultCollection())) { //Create a completely new collection String collectionName = null; for (int i = 0; i < 1000; i++) { collectionName = "blcCollection" + i; if (collectionNames.contains(collectionName)) { collectionName = null; } else { break; } } CollectionAdminRequest.createCollection(collectionName, solrCloudNumShards, solrCloudConfigName, primary); CollectionAdminRequest.createAlias(reindex.getDefaultCollection(), collectionName, primary); } else { //Aliases can be mapped to collections that don't exist.... Make sure the collection exists String collectionName = aliasCollectionMap.get(reindex.getDefaultCollection()); collectionName = collectionName.split(",")[0]; if (!collectionNames.contains(collectionName)) { CollectionAdminRequest.createCollection(collectionName, solrCloudNumShards, solrCloudConfigName, primary); } } } }