SOLR-8995: Use lambda to simplify CollectionsHandler

This commit is contained in:
Noble Paul 2016-07-20 22:00:55 +05:30
parent 9aa639d45e
commit fb4de6adb1
1 changed files with 380 additions and 479 deletions

View File

@ -37,7 +37,6 @@ import org.apache.commons.lang.StringUtils;
import org.apache.solr.client.solrj.SolrResponse; import org.apache.solr.client.solrj.SolrResponse;
import org.apache.solr.client.solrj.impl.HttpSolrClient; import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.impl.HttpSolrClient.Builder; import org.apache.solr.client.solrj.impl.HttpSolrClient.Builder;
import org.apache.solr.client.solrj.request.CoreAdminRequest;
import org.apache.solr.client.solrj.request.CoreAdminRequest.RequestSyncShard; import org.apache.solr.client.solrj.request.CoreAdminRequest.RequestSyncShard;
import org.apache.solr.client.solrj.response.RequestStatusState; import org.apache.solr.client.solrj.response.RequestStatusState;
import org.apache.solr.client.solrj.util.SolrIdentifierValidator; import org.apache.solr.client.solrj.util.SolrIdentifierValidator;
@ -204,7 +203,7 @@ public class CollectionsHandler extends RequestHandlerBase implements Permission
log.info("Invoked Collection Action :{} with params {} and sendToOCPQueue={}", action.toLower(), req.getParamString(), operation.sendToOCPQueue); log.info("Invoked Collection Action :{} with params {} and sendToOCPQueue={}", action.toLower(), req.getParamString(), operation.sendToOCPQueue);
SolrResponse response = null; SolrResponse response = null;
Map<String, Object> props = operation.call(req, rsp, this); Map<String, Object> props = operation.execute(req, rsp, this);
String asyncId = req.getParams().get(ASYNC); String asyncId = req.getParams().get(ASYNC);
if (props != null) { if (props != null) {
if (asyncId != null) { if (asyncId != null) {
@ -335,7 +334,34 @@ public class CollectionsHandler extends RequestHandlerBase implements Permission
public static final String SYSTEM_COLL = ".system"; public static final String SYSTEM_COLL = ".system";
enum CollectionOperation { private static void createSysConfigSet(CoreContainer coreContainer) throws KeeperException, InterruptedException {
SolrZkClient zk = coreContainer.getZkController().getZkStateReader().getZkClient();
ZkCmdExecutor cmdExecutor = new ZkCmdExecutor(zk.getZkClientTimeout());
cmdExecutor.ensureExists(ZkStateReader.CONFIGS_ZKNODE, zk);
cmdExecutor.ensureExists(ZkStateReader.CONFIGS_ZKNODE + "/" + SYSTEM_COLL, zk);
try {
String path = ZkStateReader.CONFIGS_ZKNODE + "/" + SYSTEM_COLL + "/schema.xml";
byte[] data = IOUtils.toByteArray(Thread.currentThread().getContextClassLoader().getResourceAsStream("SystemCollectionSchema.xml"));
cmdExecutor.ensureExists(path, data, CreateMode.PERSISTENT, zk);
path = ZkStateReader.CONFIGS_ZKNODE + "/" + SYSTEM_COLL + "/solrconfig.xml";
data = IOUtils.toByteArray(Thread.currentThread().getContextClassLoader().getResourceAsStream("SystemCollectionSolrConfig.xml"));
cmdExecutor.ensureExists(path, data, CreateMode.PERSISTENT, zk);
} catch (IOException e) {
throw new SolrException(ErrorCode.SERVER_ERROR, e);
}
}
private static void addStatusToResponse(NamedList<Object> results, RequestStatusState state, String msg) {
SimpleOrderedMap<String> status = new SimpleOrderedMap<>();
status.add("state", state.getKey());
status.add("msg", msg);
results.add("status", status);
}
enum CollectionOperation implements CollectionOp {
/** /**
* very simple currently, you can pass a template collection, and the new collection is created on * very simple currently, you can pass a template collection, and the new collection is created on
* every node the template collection is on * every node the template collection is on
@ -343,10 +369,7 @@ public class CollectionsHandler extends RequestHandlerBase implements Permission
* we might also want to think about error handling (add the request to a zk queue and involve overseer?) * we might also want to think about error handling (add the request to a zk queue and involve overseer?)
* as well as specific replicas= options * as well as specific replicas= options
*/ */
CREATE_OP(CREATE) { CREATE_OP(CREATE, (req, rsp, h) -> {
@Override
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler h)
throws KeeperException, InterruptedException {
Map<String, Object> props = req.getParams().required().getAll(null, NAME); Map<String, Object> props = req.getParams().required().getAll(null, NAME);
props.put("fromApi", "true"); props.put("fromApi", "true");
req.getParams().getAll(props, req.getParams().getAll(props,
@ -367,7 +390,7 @@ public class CollectionsHandler extends RequestHandlerBase implements Permission
addMapObject(props, RULE); addMapObject(props, RULE);
addMapObject(props, SNITCH); addMapObject(props, SNITCH);
verifyRuleParams(h.coreContainer, props); verifyRuleParams(h.coreContainer, props);
final String collectionName = SolrIdentifierValidator.validateCollectionName((String)props.get(NAME)); final String collectionName = SolrIdentifierValidator.validateCollectionName((String) props.get(NAME));
final String shardsParam = (String) props.get(SHARDS_PROP); final String shardsParam = (String) props.get(SHARDS_PROP);
if (StringUtils.isNotEmpty(shardsParam)) { if (StringUtils.isNotEmpty(shardsParam)) {
verifyShardsParam(shardsParam); verifyShardsParam(shardsParam);
@ -382,47 +405,12 @@ public class CollectionsHandler extends RequestHandlerBase implements Permission
copyPropertiesWithPrefix(req.getParams(), props, COLL_PROP_PREFIX); copyPropertiesWithPrefix(req.getParams(), props, COLL_PROP_PREFIX);
return copyPropertiesWithPrefix(req.getParams(), props, "router."); return copyPropertiesWithPrefix(req.getParams(), props, "router.");
} }),
DELETE_OP(DELETE, (req, rsp, h) -> req.getParams().required().getAll(null, NAME)),
private void createSysConfigSet(CoreContainer coreContainer) throws KeeperException, InterruptedException { RELOAD_OP(RELOAD, (req, rsp, h) -> req.getParams().required().getAll(null, NAME)),
SolrZkClient zk = coreContainer.getZkController().getZkStateReader().getZkClient();
ZkCmdExecutor cmdExecutor = new ZkCmdExecutor(zk.getZkClientTimeout());
cmdExecutor.ensureExists(ZkStateReader.CONFIGS_ZKNODE, zk);
cmdExecutor.ensureExists(ZkStateReader.CONFIGS_ZKNODE + "/" + SYSTEM_COLL, zk);
try { SYNCSHARD_OP(SYNCSHARD, (req, rsp, h) -> {
String path = ZkStateReader.CONFIGS_ZKNODE + "/" + SYSTEM_COLL + "/schema.xml";
byte[] data = IOUtils.toByteArray(Thread.currentThread().getContextClassLoader().getResourceAsStream("SystemCollectionSchema.xml"));
cmdExecutor.ensureExists(path, data, CreateMode.PERSISTENT, zk);
path = ZkStateReader.CONFIGS_ZKNODE + "/" + SYSTEM_COLL + "/solrconfig.xml";
data = IOUtils.toByteArray(Thread.currentThread().getContextClassLoader().getResourceAsStream("SystemCollectionSolrConfig.xml"));
cmdExecutor.ensureExists(path, data, CreateMode.PERSISTENT, zk);
} catch (IOException e) {
throw new SolrException(ErrorCode.SERVER_ERROR, e);
}
}
},
DELETE_OP(DELETE) {
@Override
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler handler)
throws Exception {
return req.getParams().required().getAll(null, NAME);
}
},
RELOAD_OP(RELOAD) {
@Override
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler handler)
throws Exception {
return req.getParams().required().getAll(null, NAME);
}
},
SYNCSHARD_OP(SYNCSHARD) {
@Override
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler h)
throws Exception {
String collection = req.getParams().required().get("collection"); String collection = req.getParams().required().get("collection");
String shard = req.getParams().required().get("shard"); String shard = req.getParams().required().get("shard");
@ -435,36 +423,20 @@ public class CollectionsHandler extends RequestHandlerBase implements Permission
try (HttpSolrClient client = new Builder(nodeProps.getBaseUrl()).build()) { try (HttpSolrClient client = new Builder(nodeProps.getBaseUrl()).build()) {
client.setConnectionTimeout(15000); client.setConnectionTimeout(15000);
client.setSoTimeout(60000); client.setSoTimeout(60000);
RequestSyncShard reqSyncShard = new CoreAdminRequest.RequestSyncShard(); RequestSyncShard reqSyncShard = new RequestSyncShard();
reqSyncShard.setCollection(collection); reqSyncShard.setCollection(collection);
reqSyncShard.setShard(shard); reqSyncShard.setShard(shard);
reqSyncShard.setCoreName(nodeProps.getCoreName()); reqSyncShard.setCoreName(nodeProps.getCoreName());
client.request(reqSyncShard); client.request(reqSyncShard);
} }
return null; return null;
} }),
CREATEALIAS_OP(CREATEALIAS, (req, rsp, h) -> {
},
CREATEALIAS_OP(CREATEALIAS) {
@Override
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler handler)
throws Exception {
final String aliasName = SolrIdentifierValidator.validateAliasName(req.getParams().get(NAME)); final String aliasName = SolrIdentifierValidator.validateAliasName(req.getParams().get(NAME));
return req.getParams().required().getAll(null, NAME, "collections"); return req.getParams().required().getAll(null, NAME, "collections");
} }),
}, DELETEALIAS_OP(DELETEALIAS, (req, rsp, h) -> req.getParams().required().getAll(null, NAME)),
DELETEALIAS_OP(DELETEALIAS) { SPLITSHARD_OP(SPLITSHARD, DEFAULT_COLLECTION_OP_TIMEOUT * 5, true, (req, rsp, h) -> {
@Override
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler handler)
throws Exception {
return req.getParams().required().getAll(null, NAME);
}
},
SPLITSHARD_OP(SPLITSHARD, DEFAULT_COLLECTION_OP_TIMEOUT * 5, true) {
@Override
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler h)
throws Exception {
String name = req.getParams().required().get(COLLECTION_PROP); String name = req.getParams().required().get(COLLECTION_PROP);
// TODO : add support for multiple shards // TODO : add support for multiple shards
String shard = req.getParams().get(SHARD_ID_PROP); String shard = req.getParams().get(SHARD_ID_PROP);
@ -489,11 +461,8 @@ public class CollectionsHandler extends RequestHandlerBase implements Permission
"split.key", "split.key",
CoreAdminParams.RANGES); CoreAdminParams.RANGES);
return copyPropertiesWithPrefix(req.getParams(), map, COLL_PROP_PREFIX); return copyPropertiesWithPrefix(req.getParams(), map, COLL_PROP_PREFIX);
} }),
}, DELETESHARD_OP(DELETESHARD, (req, rsp, h) -> {
DELETESHARD_OP(DELETESHARD) {
@Override
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler handler) throws Exception {
Map<String, Object> map = req.getParams().required().getAll(null, Map<String, Object> map = req.getParams().required().getAll(null,
COLLECTION_PROP, COLLECTION_PROP,
SHARD_ID_PROP); SHARD_ID_PROP);
@ -502,22 +471,16 @@ public class CollectionsHandler extends RequestHandlerBase implements Permission
DELETE_DATA_DIR, DELETE_DATA_DIR,
DELETE_INSTANCE_DIR); DELETE_INSTANCE_DIR);
return map; return map;
} }),
}, FORCELEADER_OP(FORCELEADER, (req, rsp, h) -> {
FORCELEADER_OP(FORCELEADER) { forceLeaderElection(req, h);
@Override
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler handler) throws Exception {
forceLeaderElection(req, handler);
return null; return null;
} }),
}, CREATESHARD_OP(CREATESHARD, (req, rsp, h) -> {
CREATESHARD_OP(CREATESHARD) {
@Override
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler handler) throws Exception {
Map<String, Object> map = req.getParams().required().getAll(null, Map<String, Object> map = req.getParams().required().getAll(null,
COLLECTION_PROP, COLLECTION_PROP,
SHARD_ID_PROP); SHARD_ID_PROP);
ClusterState clusterState = handler.coreContainer.getZkController().getClusterState(); ClusterState clusterState = h.coreContainer.getZkController().getClusterState();
final String newShardName = SolrIdentifierValidator.validateShardName(req.getParams().get(SHARD_ID_PROP)); final String newShardName = SolrIdentifierValidator.validateShardName(req.getParams().get(SHARD_ID_PROP));
if (!ImplicitDocRouter.NAME.equals(((Map) clusterState.getCollection(req.getParams().get(COLLECTION_PROP)).get(DOC_ROUTER)).get(NAME))) if (!ImplicitDocRouter.NAME.equals(((Map) clusterState.getCollection(req.getParams().get(COLLECTION_PROP)).get(DOC_ROUTER)).get(NAME)))
throw new SolrException(ErrorCode.BAD_REQUEST, "shards can be added only to 'implicit' collections"); throw new SolrException(ErrorCode.BAD_REQUEST, "shards can be added only to 'implicit' collections");
@ -525,11 +488,8 @@ public class CollectionsHandler extends RequestHandlerBase implements Permission
REPLICATION_FACTOR, REPLICATION_FACTOR,
CREATE_NODE_SET); CREATE_NODE_SET);
return copyPropertiesWithPrefix(req.getParams(), map, COLL_PROP_PREFIX); return copyPropertiesWithPrefix(req.getParams(), map, COLL_PROP_PREFIX);
} }),
}, DELETEREPLICA_OP(DELETEREPLICA, (req, rsp, h) -> {
DELETEREPLICA_OP(DELETEREPLICA) {
@Override
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler handler) throws Exception {
Map<String, Object> map = req.getParams().required().getAll(null, Map<String, Object> map = req.getParams().required().getAll(null,
COLLECTION_PROP, COLLECTION_PROP,
SHARD_ID_PROP, SHARD_ID_PROP,
@ -541,52 +501,36 @@ public class CollectionsHandler extends RequestHandlerBase implements Permission
DELETE_INSTANCE_DIR); DELETE_INSTANCE_DIR);
return req.getParams().getAll(map, ONLY_IF_DOWN); return req.getParams().getAll(map, ONLY_IF_DOWN);
} }),
}, MIGRATE_OP(MIGRATE, (req, rsp, h) -> {
MIGRATE_OP(MIGRATE) {
@Override
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler h) throws Exception {
Map<String, Object> map = req.getParams().required().getAll(null, COLLECTION_PROP, "split.key", "target.collection"); Map<String, Object> map = req.getParams().required().getAll(null, COLLECTION_PROP, "split.key", "target.collection");
return req.getParams().getAll(map, "forward.timeout"); return req.getParams().getAll(map, "forward.timeout");
} }),
}, ADDROLE_OP(ADDROLE, (req, rsp, h) -> {
ADDROLE_OP(ADDROLE) {
@Override
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler handler) throws Exception {
Map<String, Object> map = req.getParams().required().getAll(null, "role", "node"); Map<String, Object> map = req.getParams().required().getAll(null, "role", "node");
if (!KNOWN_ROLES.contains(map.get("role"))) if (!KNOWN_ROLES.contains(map.get("role")))
throw new SolrException(ErrorCode.BAD_REQUEST, "Unknown role. Supported roles are ," + KNOWN_ROLES); throw new SolrException(ErrorCode.BAD_REQUEST, "Unknown role. Supported roles are ," + KNOWN_ROLES);
return map; return map;
} }),
}, REMOVEROLE_OP(REMOVEROLE, (req, rsp, h) -> {
REMOVEROLE_OP(REMOVEROLE) {
@Override
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler h) throws Exception {
Map<String, Object> map = req.getParams().required().getAll(null, "role", "node"); Map<String, Object> map = req.getParams().required().getAll(null, "role", "node");
if (!KNOWN_ROLES.contains(map.get("role"))) if (!KNOWN_ROLES.contains(map.get("role")))
throw new SolrException(ErrorCode.BAD_REQUEST, "Unknown role. Supported roles are ," + KNOWN_ROLES); throw new SolrException(ErrorCode.BAD_REQUEST, "Unknown role. Supported roles are ," + KNOWN_ROLES);
return map; return map;
} }),
}, CLUSTERPROP_OP(CLUSTERPROP, (req, rsp, h) -> {
CLUSTERPROP_OP(CLUSTERPROP) {
@Override
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler h) throws Exception {
String name = req.getParams().required().get(NAME); String name = req.getParams().required().get(NAME);
String val = req.getParams().get(VALUE_LONG); String val = req.getParams().get(VALUE_LONG);
ClusterProperties cp = new ClusterProperties(h.coreContainer.getZkController().getZkClient()); ClusterProperties cp = new ClusterProperties(h.coreContainer.getZkController().getZkClient());
cp.setClusterProperty(name, val); cp.setClusterProperty(name, val);
return null; return null;
} }),
}, REQUESTSTATUS_OP(REQUESTSTATUS, (req, rsp, h) -> {
REQUESTSTATUS_OP(REQUESTSTATUS) {
@SuppressWarnings("unchecked")
@Override
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler h) throws Exception {
req.getParams().required().check(REQUESTID); req.getParams().required().check(REQUESTID);
final CoreContainer coreContainer = h.coreContainer; final CoreContainer coreContainer1 = h.coreContainer;
final String requestId = req.getParams().get(REQUESTID); final String requestId = req.getParams().get(REQUESTID);
final ZkController zkController = coreContainer.getZkController(); final ZkController zkController = coreContainer1.getZkController();
final NamedList<Object> results = new NamedList<>(); final NamedList<Object> results = new NamedList<>();
if (zkController.getOverseerCompletedMap().contains(requestId)) { if (zkController.getOverseerCompletedMap().contains(requestId)) {
@ -608,19 +552,11 @@ public class CollectionsHandler extends RequestHandlerBase implements Permission
final SolrResponse response = new OverseerSolrResponse(results); final SolrResponse response = new OverseerSolrResponse(results);
rsp.getValues().addAll(response.getResponse()); rsp.getValues().addAll(response.getResponse());
return null; return null;
} }),
DELETESTATUS_OP(DELETESTATUS, new CollectionOp() {
private void addStatusToResponse(NamedList<Object> results, RequestStatusState state, String msg) {
SimpleOrderedMap<String> status = new SimpleOrderedMap<>();
status.add("state", state.getKey());
status.add("msg", msg);
results.add("status", status);
}
},
DELETESTATUS_OP(DELETESTATUS) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler h) throws Exception { public Map<String, Object> execute(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler h) throws Exception {
final CoreContainer coreContainer = h.coreContainer; final CoreContainer coreContainer = h.coreContainer;
final String requestId = req.getParams().get(REQUESTID); final String requestId = req.getParams().get(REQUESTID);
final ZkController zkController = coreContainer.getZkController(); final ZkController zkController = coreContainer.getZkController();
@ -652,11 +588,8 @@ public class CollectionsHandler extends RequestHandlerBase implements Permission
} }
return null; return null;
} }
}, }),
ADDREPLICA_OP(ADDREPLICA) { ADDREPLICA_OP(ADDREPLICA, (req, rsp, h) -> {
@Override
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler h)
throws Exception {
Map<String, Object> props = req.getParams().getAll(null, Map<String, Object> props = req.getParams().getAll(null,
COLLECTION_PROP, COLLECTION_PROP,
"node", "node",
@ -666,51 +599,36 @@ public class CollectionsHandler extends RequestHandlerBase implements Permission
INSTANCE_DIR, INSTANCE_DIR,
DATA_DIR); DATA_DIR);
return copyPropertiesWithPrefix(req.getParams(), props, COLL_PROP_PREFIX); return copyPropertiesWithPrefix(req.getParams(), props, COLL_PROP_PREFIX);
} }),
}, OVERSEERSTATUS_OP(OVERSEERSTATUS, (req, rsp, h) -> (Map) new LinkedHashMap<>()),
OVERSEERSTATUS_OP(OVERSEERSTATUS) {
@Override
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler h) throws Exception {
return new LinkedHashMap<>();
}
},
/** /**
* Handle list collection request. * Handle list collection request.
* Do list collection request to zk host * Do list collection request to zk host
*/ */
LIST_OP(LIST) { LIST_OP(LIST, (req, rsp, h) -> {
@Override
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler handler) throws Exception {
NamedList<Object> results = new NamedList<>(); NamedList<Object> results = new NamedList<>();
Map<String, DocCollection> collections = handler.coreContainer.getZkController().getZkStateReader().getClusterState().getCollectionsMap(); Map<String, DocCollection> collections = h.coreContainer.getZkController().getZkStateReader().getClusterState().getCollectionsMap();
List<String> collectionList = new ArrayList<>(collections.keySet()); List<String> collectionList = new ArrayList<>(collections.keySet());
results.add("collections", collectionList); results.add("collections", collectionList);
SolrResponse response = new OverseerSolrResponse(results); SolrResponse response = new OverseerSolrResponse(results);
rsp.getValues().addAll(response.getResponse()); rsp.getValues().addAll(response.getResponse());
return null; return null;
} }),
},
/** /**
* Handle cluster status request. * Handle cluster status request.
* Can return status per specific collection/shard or per all collections. * Can return status per specific collection/shard or per all collections.
*/ */
CLUSTERSTATUS_OP(CLUSTERSTATUS) { CLUSTERSTATUS_OP(CLUSTERSTATUS, (req, rsp, h) -> {
@Override
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler handler)
throws KeeperException, InterruptedException {
Map<String, Object> all = req.getParams().getAll(null, Map<String, Object> all = req.getParams().getAll(null,
COLLECTION_PROP, COLLECTION_PROP,
SHARD_ID_PROP, SHARD_ID_PROP,
_ROUTE_); _ROUTE_);
new ClusterStatus(handler.coreContainer.getZkController().getZkStateReader(), new ClusterStatus(h.coreContainer.getZkController().getZkStateReader(),
new ZkNodeProps(all)).getClusterStatus(rsp.getValues()); new ZkNodeProps(all)).getClusterStatus(rsp.getValues());
return null; return null;
} }),
}, ADDREPLICAPROP_OP(ADDREPLICAPROP, (req, rsp, h) -> {
ADDREPLICAPROP_OP(ADDREPLICAPROP) {
@Override
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler h) throws Exception {
Map<String, Object> map = req.getParams().required().getAll(null, Map<String, Object> map = req.getParams().required().getAll(null,
COLLECTION_PROP, COLLECTION_PROP,
PROPERTY_PROP, PROPERTY_PROP,
@ -730,28 +648,22 @@ public class CollectionsHandler extends RequestHandlerBase implements Permission
if (StringUtils.isNotBlank((String) map.get(SHARD_UNIQUE)) && if (StringUtils.isNotBlank((String) map.get(SHARD_UNIQUE)) &&
SliceMutator.SLICE_UNIQUE_BOOLEAN_PROPERTIES.contains(property.toLowerCase(Locale.ROOT)) && SliceMutator.SLICE_UNIQUE_BOOLEAN_PROPERTIES.contains(property.toLowerCase(Locale.ROOT)) &&
uniquePerSlice == false) { uniquePerSlice == false) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, throw new SolrException(ErrorCode.BAD_REQUEST,
"Overseer replica property command received for property " + property + "Overseer replica property command received for property " + property +
" with the " + SHARD_UNIQUE + " with the " + SHARD_UNIQUE +
" parameter set to something other than 'true'. No action taken."); " parameter set to something other than 'true'. No action taken.");
} }
return map; return map;
} }),
}, DELETEREPLICAPROP_OP(DELETEREPLICAPROP, (req, rsp, h) -> {
DELETEREPLICAPROP_OP(DELETEREPLICAPROP) {
@Override
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler h) throws Exception {
Map<String, Object> map = req.getParams().required().getAll(null, Map<String, Object> map = req.getParams().required().getAll(null,
COLLECTION_PROP, COLLECTION_PROP,
PROPERTY_PROP, PROPERTY_PROP,
SHARD_ID_PROP, SHARD_ID_PROP,
REPLICA_PROP); REPLICA_PROP);
return req.getParams().getAll(map, PROPERTY_PROP); return req.getParams().getAll(map, PROPERTY_PROP);
} }),
}, BALANCESHARDUNIQUE_OP(BALANCESHARDUNIQUE, (req, rsp, h) -> {
BALANCESHARDUNIQUE_OP(BALANCESHARDUNIQUE) {
@Override
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler h) throws Exception {
Map<String, Object> map = req.getParams().required().getAll(null, Map<String, Object> map = req.getParams().required().getAll(null,
COLLECTION_PROP, COLLECTION_PROP,
PROPERTY_PROP); PROPERTY_PROP);
@ -768,19 +680,12 @@ public class CollectionsHandler extends RequestHandlerBase implements Permission
} }
return req.getParams().getAll(map, ONLY_ACTIVE_NODES, SHARD_UNIQUE); return req.getParams().getAll(map, ONLY_ACTIVE_NODES, SHARD_UNIQUE);
} }),
}, REBALANCELEADERS_OP(REBALANCELEADERS, (req, rsp, h) -> {
REBALANCELEADERS_OP(REBALANCELEADERS) { new RebalanceLeaders(req, rsp, h).execute();
@Override
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler h) throws Exception {
new RebalanceLeaders(req,rsp,h).execute();
return null; return null;
} }),
}, MODIFYCOLLECTION_OP(MODIFYCOLLECTION, (req, rsp, h) -> {
MODIFYCOLLECTION_OP(MODIFYCOLLECTION) {
@Override
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler h) throws Exception {
Map<String, Object> m = req.getParams().getAll(null, MODIFIABLE_COLL_PROPS); Map<String, Object> m = req.getParams().getAll(null, MODIFIABLE_COLL_PROPS);
if (m.isEmpty()) throw new SolrException(ErrorCode.BAD_REQUEST, if (m.isEmpty()) throw new SolrException(ErrorCode.BAD_REQUEST,
formatString("no supported values provided rule, snitch, maxShardsPerNode, replicationFactor, collection.configName")); formatString("no supported values provided rule, snitch, maxShardsPerNode, replicationFactor, collection.configName"));
@ -790,18 +695,10 @@ public class CollectionsHandler extends RequestHandlerBase implements Permission
for (String prop : MODIFIABLE_COLL_PROPS) DocCollection.verifyProp(m, prop); for (String prop : MODIFIABLE_COLL_PROPS) DocCollection.verifyProp(m, prop);
verifyRuleParams(h.coreContainer, m); verifyRuleParams(h.coreContainer, m);
return m; return m;
} }),
}, MIGRATESTATEFORMAT_OP(MIGRATESTATEFORMAT, (req, rsp, h) -> req.getParams().required().getAll(null, COLLECTION_PROP)),
MIGRATESTATEFORMAT_OP(MIGRATESTATEFORMAT) {
@Override BACKUP_OP(BACKUP, (req, rsp, h) -> {
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler handler)
throws Exception {
return req.getParams().required().getAll(null, COLLECTION_PROP);
}
},
BACKUP_OP(BACKUP) {
@Override
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler h) throws Exception {
req.getParams().required().check(NAME, COLLECTION_PROP); req.getParams().required().check(NAME, COLLECTION_PROP);
String collectionName = req.getParams().get(COLLECTION_PROP); String collectionName = req.getParams().get(COLLECTION_PROP);
@ -840,11 +737,8 @@ public class CollectionsHandler extends RequestHandlerBase implements Permission
Map<String, Object> params = req.getParams().getAll(null, NAME, COLLECTION_PROP); Map<String, Object> params = req.getParams().getAll(null, NAME, COLLECTION_PROP);
params.put(CoreAdminParams.BACKUP_LOCATION, location); params.put(CoreAdminParams.BACKUP_LOCATION, location);
return params; return params;
} }),
}, RESTORE_OP(RESTORE, (req, rsp, h) -> {
RESTORE_OP(RESTORE) {
@Override
Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler h) throws Exception {
req.getParams().required().check(NAME, COLLECTION_PROP); req.getParams().required().check(NAME, COLLECTION_PROP);
String collectionName = SolrIdentifierValidator.validateCollectionName(req.getParams().get(COLLECTION_PROP)); String collectionName = SolrIdentifierValidator.validateCollectionName(req.getParams().get(COLLECTION_PROP));
@ -887,28 +781,24 @@ public class CollectionsHandler extends RequestHandlerBase implements Permission
req.getParams().getAll(params, COLL_CONF, REPLICATION_FACTOR, MAX_SHARDS_PER_NODE, STATE_FORMAT, AUTO_ADD_REPLICAS); req.getParams().getAll(params, COLL_CONF, REPLICATION_FACTOR, MAX_SHARDS_PER_NODE, STATE_FORMAT, AUTO_ADD_REPLICAS);
copyPropertiesWithPrefix(req.getParams(), params, COLL_PROP_PREFIX); copyPropertiesWithPrefix(req.getParams(), params, COLL_PROP_PREFIX);
return params; return params;
} });
}; public final CollectionOp fun;
CollectionAction action; CollectionAction action;
long timeOut; long timeOut;
boolean sendToOCPQueue; boolean sendToOCPQueue;
CollectionOperation(CollectionAction action) { CollectionOperation(CollectionAction action, CollectionOp fun) {
this(action, DEFAULT_COLLECTION_OP_TIMEOUT, true); this(action, DEFAULT_COLLECTION_OP_TIMEOUT, true, fun);
} }
CollectionOperation(CollectionAction action, long timeOut, boolean sendToOCPQueue) { CollectionOperation(CollectionAction action, long timeOut, boolean sendToOCPQueue, CollectionOp fun) {
this.action = action; this.action = action;
this.timeOut = timeOut; this.timeOut = timeOut;
this.sendToOCPQueue = sendToOCPQueue; this.sendToOCPQueue = sendToOCPQueue;
this.fun = fun;
} }
/**
* All actions must implement this method. If a non null map is returned , the action name is added to
* the map and sent to overseer for processing. If it returns a null, the call returns immediately
*/
abstract Map<String, Object> call(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler h) throws Exception;
public static CollectionOperation get(CollectionAction action) { public static CollectionOperation get(CollectionAction action) {
for (CollectionOperation op : values()) { for (CollectionOperation op : values()) {
@ -916,6 +806,12 @@ public class CollectionsHandler extends RequestHandlerBase implements Permission
} }
throw new SolrException(ErrorCode.SERVER_ERROR, "No such action" + action); throw new SolrException(ErrorCode.SERVER_ERROR, "No such action" + action);
} }
@Override
public Map<String, Object> execute(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler h)
throws Exception {
return fun.execute(req, rsp, h);
}
} }
private static void forceLeaderElection(SolrQueryRequest req, CollectionsHandler handler) { private static void forceLeaderElection(SolrQueryRequest req, CollectionsHandler handler) {
@ -1096,6 +992,11 @@ public class CollectionsHandler extends RequestHandlerBase implements Permission
} }
} }
interface CollectionOp {
Map<String, Object> execute(SolrQueryRequest req, SolrQueryResponse rsp, CollectionsHandler h) throws Exception;
}
public static final List<String> MODIFIABLE_COLL_PROPS = Arrays.asList( public static final List<String> MODIFIABLE_COLL_PROPS = Arrays.asList(
RULE, RULE,
SNITCH, SNITCH,