org.apache.solr.cloud.api.collections.MaintainRoutedAliasCmd Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of solr-core Show documentation
Show all versions of solr-core Show documentation
Apache Solr (module: core)
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.cloud.api.collections;
import static org.apache.solr.common.params.CommonParams.NAME;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.solr.client.solrj.SolrResponse;
import org.apache.solr.cloud.Overseer;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.cloud.Aliases;
import org.apache.solr.common.cloud.ClusterState;
import org.apache.solr.common.cloud.CollectionProperties;
import org.apache.solr.common.cloud.ZkNodeProps;
import org.apache.solr.common.cloud.ZkStateReader;
import org.apache.solr.common.params.CollectionParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.StrUtils;
import org.apache.solr.handler.admin.CollectionsHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MaintainRoutedAliasCmd extends AliasCmd {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
static final String INVOKED_BY_ROUTED_ALIAS = "invokedByRoutedAlias";
static final String ROUTED_ALIAS_TARGET_COL = "routedAliasTargetCol";
MaintainRoutedAliasCmd(CollectionCommandContext ccc) {
super(ccc);
}
/**
* Invokes this command from the client. If there's a problem it will throw an exception. Please
* note that is important to never add async to this invocation. This method must block (up to the
* standard OCP timeout) to prevent large batches of add's from sending a message to the overseer
* for every document added in RoutedAliasUpdateProcessor.
*/
static void remoteInvoke(CollectionsHandler collHandler, String aliasName, String targetCol)
throws Exception {
final CollectionParams.CollectionAction maintainroutedalias =
CollectionParams.CollectionAction.MAINTAINROUTEDALIAS;
Map msg = new HashMap<>();
msg.put(Overseer.QUEUE_OPERATION, maintainroutedalias.toLower());
msg.put(CollectionParams.NAME, aliasName);
msg.put(MaintainRoutedAliasCmd.ROUTED_ALIAS_TARGET_COL, targetCol);
final SolrResponse rsp =
collHandler.submitCollectionApiCommand(new ZkNodeProps(msg), maintainroutedalias);
if (rsp.getException() != null) {
throw rsp.getException();
}
}
void addCollectionToAlias(
String aliasName, ZkStateReader.AliasesManager aliasesManager, String createCollName) {
aliasesManager.applyModificationAndExportToZk(
curAliases -> {
final List curTargetCollections =
curAliases.getCollectionAliasListMap().get(aliasName);
if (curTargetCollections.contains(createCollName)) {
return curAliases;
} else {
List 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, ','));
}
});
}
private void removeCollectionFromAlias(
String aliasName, ZkStateReader.AliasesManager aliasesManager, String createCollName) {
aliasesManager.applyModificationAndExportToZk(
curAliases -> {
final List curTargetCollections =
curAliases.getCollectionAliasListMap().get(aliasName);
if (curTargetCollections.contains(createCollName)) {
List newTargetCollections = new ArrayList<>(curTargetCollections.size());
newTargetCollections.addAll(curTargetCollections);
newTargetCollections.remove(createCollName);
return curAliases.cloneWithCollectionAlias(
aliasName, StrUtils.join(newTargetCollections, ','));
} else {
return curAliases;
}
});
}
@Override
public void call(ClusterState clusterState, ZkNodeProps message, NamedList