Java Code Examples for org.apache.solr.common.cloud.ZkStateReader#AliasesManager

The following examples show how to use org.apache.solr.common.cloud.ZkStateReader#AliasesManager . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AliasIntegrationTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private ZkStateReader createColectionsAndAlias(String aliasName) throws SolrServerException, IOException, KeeperException, InterruptedException {
  CollectionAdminRequest.createCollection("collection1meta", "conf", 2, 1).process(cluster.getSolrClient());
  CollectionAdminRequest.createCollection("collection2meta", "conf", 1, 1).process(cluster.getSolrClient());

  cluster.waitForActiveCollection("collection1meta", 2, 2);
  cluster.waitForActiveCollection("collection2meta", 1, 1);

  waitForState("Expected collection1 to be created with 2 shards and 1 replica", "collection1meta", clusterShape(2, 2));
  waitForState("Expected collection2 to be created with 1 shard and 1 replica", "collection2meta", clusterShape(1, 1));
  ZkStateReader zkStateReader = cluster.getSolrClient().getZkStateReader();
  zkStateReader.createClusterStateWatchersAndUpdate();
  List<String> aliases = zkStateReader.getAliases().resolveAliases(aliasName);
  assertEquals(1, aliases.size());
  assertEquals(aliasName, aliases.get(0));
  UnaryOperator<Aliases> op6 = a -> a.cloneWithCollectionAlias(aliasName, "collection1meta,collection2meta");
  final ZkStateReader.AliasesManager aliasesManager = zkStateReader.aliasesManager;

  aliasesManager.applyModificationAndExportToZk(op6);
  aliases = zkStateReader.getAliases().resolveAliases(aliasName);
  assertEquals(2, aliases.size());
  assertEquals("collection1meta", aliases.get(0));
  assertEquals("collection2meta", aliases.get(1));
  return zkStateReader;
}
 
Example 2
Source File: MaintainRoutedAliasCmd.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
void addCollectionToAlias(String aliasName, ZkStateReader.AliasesManager aliasesManager, String createCollName) {
  aliasesManager.applyModificationAndExportToZk(curAliases -> {
    final List<String> curTargetCollections = curAliases.getCollectionAliasListMap().get(aliasName);
    if (curTargetCollections.contains(createCollName)) {
      return curAliases;
    } else {
      List<String> newTargetCollections = new ArrayList<>(curTargetCollections.size() + 1);
      // prepend it on purpose (thus reverse sorted). Solr alias resolution defaults to the first collection in a list
      newTargetCollections.add(createCollName);
      newTargetCollections.addAll(curTargetCollections);
      return curAliases.cloneWithCollectionAlias(aliasName, StrUtils.join(newTargetCollections, ','));
    }
  });
}
 
Example 3
Source File: MaintainRoutedAliasCmd.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void removeCollectionFromAlias(String aliasName, ZkStateReader.AliasesManager aliasesManager, String createCollName) {
  aliasesManager.applyModificationAndExportToZk(curAliases -> {
    final List<String> curTargetCollections = curAliases.getCollectionAliasListMap().get(aliasName);
    if (curTargetCollections.contains(createCollName)) {
      List<String> newTargetCollections = new ArrayList<>(curTargetCollections.size());
      newTargetCollections.addAll(curTargetCollections);
      newTargetCollections.remove(createCollName);
      return curAliases.cloneWithCollectionAlias(aliasName, StrUtils.join(newTargetCollections, ','));
    } else {
      return curAliases;
    }
  });
}
 
Example 4
Source File: MaintainRoutedAliasCmd.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked"})
public void addTargetCollection(ClusterState clusterState, @SuppressWarnings({"rawtypes"})NamedList results, String aliasName, ZkStateReader.AliasesManager aliasesManager, Map<String, String> aliasMetadata, RoutedAlias.Action action) throws Exception {
  @SuppressWarnings({"rawtypes"})
  NamedList createResults = createCollectionAndWait(clusterState, aliasName, aliasMetadata,
      action.targetCollection, ocmh);
  if (createResults != null) {
    results.add("create", createResults);
  }
  addCollectionToAlias(aliasName, aliasesManager, action.targetCollection);
}
 
Example 5
Source File: MaintainRoutedAliasCmd.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void deleteTargetCollection(ClusterState clusterState, @SuppressWarnings({"rawtypes"})NamedList results, String aliasName, ZkStateReader.AliasesManager aliasesManager, RoutedAlias.Action action) throws Exception {
  Map<String, Object> delProps = new HashMap<>();
  delProps.put(INVOKED_BY_ROUTED_ALIAS,
      (Runnable) () -> removeCollectionFromAlias(aliasName, aliasesManager, action.targetCollection));
  delProps.put(NAME, action.targetCollection);
  ZkNodeProps messageDelete = new ZkNodeProps(delProps);
  new DeleteCollectionCmd(ocmh).call(clusterState, messageDelete, results);
}
 
Example 6
Source File: SetAliasPropCmd.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void call(ClusterState state, ZkNodeProps message, @SuppressWarnings({"rawtypes"})NamedList results) throws Exception {
  String aliasName = message.getStr(NAME);

  final ZkStateReader.AliasesManager aliasesManager = messageHandler.zkStateReader.aliasesManager;

  // Ensure we see the alias.  This may be redundant but SetAliasPropCmd isn't expected to be called very frequently
  aliasesManager.update();

  if (aliasesManager.getAliases().getCollectionAliasMap().get(aliasName) == null) {
    // nicer than letting aliases object throw later on...
    throw new SolrException(BAD_REQUEST,
        String.format(Locale.ROOT,  "Can't modify non-existent alias %s", aliasName));
  }

  @SuppressWarnings("unchecked")
  Map<String, String> properties = new LinkedHashMap<>((Map<String, String>) message.get(PROPERTIES));

  // check & cleanup properties.  It's a mutable copy.
  for (Map.Entry<String, String> entry : properties.entrySet()) {
    String key = entry.getKey();
    if ("".equals(key.trim())) {
      throw new SolrException(BAD_REQUEST, "property keys must not be pure whitespace");
    }
    if (!key.equals(key.trim())) {
      throw new SolrException(BAD_REQUEST, "property keys should not begin or end with whitespace");
    }
    String value = entry.getValue();
    if ("".equals(value)) {
      entry.setValue(null);
    }
  }

  aliasesManager.applyModificationAndExportToZk(aliases1 -> aliases1.cloneWithCollectionAliasProperties(aliasName, properties));
}
 
Example 7
Source File: MaintainRoutedAliasCmd.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public void call(ClusterState clusterState, ZkNodeProps message, @SuppressWarnings({"rawtypes"})NamedList results) throws Exception {
  //---- PARSE PRIMARY MESSAGE PARAMS
  // important that we use NAME for the alias as that is what the Overseer will get a lock on before calling us
  final String aliasName = message.getStr(NAME);
  final String routeValue = message.getStr(ROUTED_ALIAS_TARGET_COL);

  final ZkStateReader.AliasesManager aliasesManager = ocmh.zkStateReader.aliasesManager;
  final Aliases aliases = aliasesManager.getAliases();
  final Map<String, String> aliasMetadata = aliases.getCollectionAliasProperties(aliasName);
  if (aliasMetadata.isEmpty()) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
        "Alias " + aliasName + " does not exist or is not a routed alias."); // if it did exist, we'd have a non-null map
  }
  final RoutedAlias ra = RoutedAlias.fromProps(aliasName, aliasMetadata);
  if (ra == null) {
    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "MaintainRoutedAlias called on non-routed alias");
  }

  ra.updateParsedCollectionAliases(ocmh.zkStateReader, true);
  List<RoutedAlias.Action> actions = ra.calculateActions(routeValue);
  for (RoutedAlias.Action action : actions) {
    boolean exists = ocmh.zkStateReader.getClusterState().getCollectionOrNull(action.targetCollection) != null;
    switch (action.actionType) {
      case ENSURE_REMOVED:
        if (exists) {
          ocmh.tpe.submit(() -> {
            try {
              deleteTargetCollection(clusterState, results, aliasName, aliasesManager, action);
            } catch (Exception e) {
              log.warn("Deletion of {} by {} {} failed (this might be ok if two clients were"
                  , action.targetCollection, ra.getAliasName()
                  , " writing to a routed alias at the same time and both caused a deletion)");
              log.debug("Exception for last message:", e);
            }
          });
        }
        break;
      case ENSURE_EXISTS:
        if (!exists) {
          addTargetCollection(clusterState, results, aliasName, aliasesManager, aliasMetadata, action);
        } else {
          // check that the collection is properly integrated into the alias (see
          // TimeRoutedAliasUpdateProcessorTest.java:141). Presently we need to ensure inclusion in the alias
          // and the presence of the appropriate collection property. Note that this only works if the collection
          // happens to fall where we would have created one already. Support for un-even collection sizes will
          // take additional work (though presently they might work if the below book keeping is done by hand)
          if (!ra.getCollectionList(aliases).contains(action.targetCollection)) {
            addCollectionToAlias(aliasName, aliasesManager, action.targetCollection);
            Map<String, String> collectionProperties = ocmh.zkStateReader
                .getCollectionProperties(action.targetCollection, 1000);
            if (!collectionProperties.containsKey(RoutedAlias.ROUTED_ALIAS_NAME_CORE_PROP)) {
              CollectionProperties props = new CollectionProperties(ocmh.zkStateReader.getZkClient());
              props.setCollectionProperty(action.targetCollection, RoutedAlias.ROUTED_ALIAS_NAME_CORE_PROP, aliasName);
            }
          }
        }
        break;
      default:
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown action type!");
    }
  }
}