protected void createAliasIfNotExist(CloudSolrClient client, String collectionName, String aliasName) { Aliases aliases = client.getZkStateReader().getAliases(); Map<String, String> aliasCollectionMap = aliases.getCollectionAliasMap(); if (!aliasCollectionMap.containsKey(aliasName)) { try { new CollectionAdminRequest.CreateAlias().setAliasName(aliasName) .setAliasedCollections(collectionName).process(client); } catch (SolrServerException e) { throw ExceptionHelper.refineException(e); } catch (IOException e) { throw ExceptionHelper.refineException(e); } } }
private String checkForAlias(SolrZkClient zkClient, String collection) throws KeeperException, InterruptedException { byte[] aliasData = zkClient.getData(ZkStateReader.ALIASES, null, null, true); Aliases aliases = ClusterState.load(aliasData); String alias = aliases.getCollectionAlias(collection); if (alias != null) { List<String> aliasList = StrUtils.splitSmart(alias, ",", true); if (aliasList.size() > 1) { throw new IllegalArgumentException("collection cannot be an alias that maps to multiple collections"); } collection = aliasList.get(0); } return collection; }
/** * Creates a new Aliases instance with the same data as the current one but with a modification based on the * parameters. * <p> * Note that the state in zookeeper is unaffected by this method and the change must still be persisted via * {@link ZkStateReader.AliasesManager#applyModificationAndExportToZk(UnaryOperator)} * * @param alias the alias to update, must not be null * @param collections the comma separated list of collections for the alias, null to remove the alias */ public Aliases cloneWithCollectionAlias(String alias, String collections) { if (alias == null) { throw new NullPointerException("Alias name cannot be null"); } Map<String, Map<String, String>> newColProperties; Map<String, List<String>> newColAliases = new LinkedHashMap<>(this.collectionAliases);//clone to modify if (collections == null) { // REMOVE: newColProperties = new LinkedHashMap<>(this.collectionAliasProperties);//clone to modify newColProperties.remove(alias); newColAliases.remove(alias); } else { newColProperties = this.collectionAliasProperties;// no changes // java representation is a list, so split before adding to maintain consistency newColAliases.put(alias, splitCollections(collections)); // note: unmodifiableList } return new Aliases(newColAliases, newColProperties, zNodeVersion); }
/** * Create an instance from the JSON bytes read from zookeeper. Generally this should * only be done by a ZkStateReader. * * @param bytes The bytes read via a getData request to zookeeper (possibly null) * @param zNodeVersion the version of the data in zookeeper that this instance corresponds to * @return A new immutable Aliases object */ @SuppressWarnings("unchecked") public static Aliases fromJSON(byte[] bytes, int zNodeVersion) { Map<String, Map> aliasMap; if (bytes == null || bytes.length == 0) { aliasMap = Collections.emptyMap(); } else { aliasMap = (Map<String, Map>) Utils.fromJSON(bytes); } Map colAliases = aliasMap.getOrDefault(COLLECTION, Collections.emptyMap()); colAliases = convertMapOfCommaDelimitedToMapOfList(colAliases); // also unmodifiable Map<String, Map<String, String>> colMeta = aliasMap.getOrDefault(COLLECTION_METADATA, Collections.emptyMap()); colMeta.replaceAll((k, metaMap) -> Collections.unmodifiableMap(metaMap)); return new Aliases(colAliases, colMeta, zNodeVersion); }
/** * Get a map similar to the JSON data as stored in zookeeper. Callers may prefer use of * {@link #getCollectionAliasListMap()} instead, if collection names will be iterated. * * @return an unmodifiable Map of collection aliases mapped to a comma delimited string of the collection(s) the * alias maps to. Does not return null. */ @SuppressWarnings("unchecked") public Map<String, String> getCollectionAliasMap() { return Collections.unmodifiableMap(convertMapOfListToMapOfCommaDelimited(collectionAliases)); }
/** * Set the value for some properties on a collection alias. This is done by creating a new Aliases instance * with the same data as the current one but with a modification based on the parameters. * <p> * Note that the state in zookeeper is unaffected by this method and the change must still be persisted via * {@link ZkStateReader.AliasesManager#applyModificationAndExportToZk(UnaryOperator)} * * @param alias the alias to update * @param propertiesKey the key for the properties * @param propertiesValue the properties to add/replace, null to remove the key. * @return An immutable copy of the aliases with the new properties. */ public Aliases cloneWithCollectionAliasProperties(String alias, String propertiesKey, String propertiesValue) { return cloneWithCollectionAliasProperties(alias, Collections.singletonMap(propertiesKey,propertiesValue)); }
public Map<String, List<String>> getAliasesAsLists() { // TODO we compute on each call... should this be done once & cached? return Aliases.convertMapOfCommaDelimitedToMapOfList(getAliases()); }
private String checkForAlias(SolrZkClient zkClient, String collection) throws KeeperException, InterruptedException { byte[] aliasData = zkClient.getData(ZkStateReader.ALIASES, null, null, true); Aliases aliases = ClusterState.load(aliasData); String alias = aliases.getCollectionAlias(collection); if (alias != null) { List<String> aliasList = StrUtils.splitSmart(alias, ",", true); if (aliasList.size() > 1) { throw new IllegalArgumentException("collection cannot be an alias that maps to multiple collections"); } collection = aliasList.get(0); } return collection; }
/** * Set the values for some properties keys on a collection alias. This is done by creating a new Aliases instance * with the same data as the current one but with a modification based on the parameters. * <p> * Note that the state in zookeeper is unaffected by this method and the change must still be persisted via * {@link ZkStateReader.AliasesManager#applyModificationAndExportToZk(UnaryOperator)} * * @param alias the alias to update * @param properties the properties to add/replace, null values in the map will remove the key. * @return An immutable copy of the aliases with the new properties. */ public Aliases cloneWithCollectionAliasProperties(String alias, Map<String,String> properties) { if (!collectionAliases.containsKey(alias)) { throw new IllegalArgumentException(alias + " is not a valid alias"); } if (properties == null) { throw new IllegalArgumentException("Null is not a valid properties map"); } Map<String,Map<String,String>> newColProperties = new LinkedHashMap<>(this.collectionAliasProperties);//clone to modify Map<String, String> newMetaMap = new LinkedHashMap<>(newColProperties.getOrDefault(alias, Collections.emptyMap())); for (Map.Entry<String, String> metaEntry : properties.entrySet()) { if (metaEntry.getValue() != null) { newMetaMap.put(metaEntry.getKey(), metaEntry.getValue()); } else { newMetaMap.remove(metaEntry.getKey()); } } newColProperties.put(alias, Collections.unmodifiableMap(newMetaMap)); return new Aliases(collectionAliases, newColProperties, zNodeVersion); }
/** * Serialize our state. */ public byte[] toJSON() { if (collectionAliases.isEmpty()) { assert collectionAliasProperties.isEmpty(); return null; } else { Map<String,Map> tmp = new LinkedHashMap<>(); tmp.put(COLLECTION, convertMapOfListToMapOfCommaDelimited(collectionAliases)); if (!collectionAliasProperties.isEmpty()) { tmp.put(COLLECTION_METADATA, collectionAliasProperties); } return Utils.toJSON(tmp); } }
private Set<String> getCollectionNames(ClusterState clusterState, String collection) { // Extract each comma separated collection name and store in a List. List<String> rawCollectionsList = StrUtils.splitSmart(collection, ",", true); Set<String> collectionNames = new HashSet<>(); // validate collections for (String collectionName : rawCollectionsList) { if (!clusterState.getCollections().contains(collectionName)) { Aliases aliases = zkStateReader.getAliases(); String alias = aliases.getCollectionAlias(collectionName); if (alias != null) { List<String> aliasList = StrUtils.splitSmart(alias, ",", true); collectionNames.addAll(aliasList); continue; } throw new SolrException(ErrorCode.BAD_REQUEST, "Collection not found: " + collectionName); } collectionNames.add(collectionName); } return collectionNames; }
primaryCloudClient.connect(); Aliases aliases = primaryCloudClient.getZkStateReader().getAliases(); Map<String, String> aliasCollectionMap = aliases.getCollectionAliasMap(); if (aliasCollectionMap == null || !aliasCollectionMap.containsKey(primaryCloudClient.getDefaultCollection()) || !aliasCollectionMap.containsKey(reindexCloudClient.getDefaultCollection())) {
String alias = aliases.getCollectionAlias(collection); if (alias != null) { List<String> aliasList = StrUtils.splitSmart(alias, ",", true);
Map<String, String> aliasCollectionMap = aliases.getCollectionAliasMap(); aliasCollectionMap = aliases.getCollectionAliasMap();
String alias = aliases.getCollectionAlias(collection); if (alias != null) { List<String> aliasList = StrUtils.splitSmart(alias, ",", true);
Map<String, String> collectionAliases = aliases.getCollectionAliasMap(); if(collectionAliases != null && collectionAliases.containsKey(collection)) { collection = collectionAliases.get(collection);
String alias = aliases.getCollectionAlias(collection); if (alias != null) { List<String> aliasList = StrUtils.splitSmart(alias, ",", true);
String alias = aliases.getCollectionAlias(collection); if (alias != null) { List<String> aliasList = StrUtils.splitSmart(alias, ",", true);