use of org.apache.solr.common.cloud.DocRouter in project lucene-solr by apache.
the class ClusterStateMutator method createCollection.
public ZkWriteCommand createCollection(ClusterState clusterState, ZkNodeProps message) {
String cName = message.getStr(NAME);
log.debug("building a new cName: " + cName);
if (clusterState.hasCollection(cName)) {
log.warn("Collection {} already exists. exit", cName);
return ZkStateWriter.NO_OP;
}
Map<String, Object> routerSpec = DocRouter.getRouterSpec(message);
String routerName = routerSpec.get(NAME) == null ? DocRouter.DEFAULT_NAME : (String) routerSpec.get(NAME);
DocRouter router = DocRouter.getDocRouter(routerName);
Object messageShardsObj = message.get("shards");
Map<String, Slice> slices;
if (messageShardsObj instanceof Map) {
// we are being explicitly told the slice data (e.g. coll restore)
slices = Slice.loadAllFromMap((Map<String, Object>) messageShardsObj);
} else {
List<String> shardNames = new ArrayList<>();
if (router instanceof ImplicitDocRouter) {
getShardNames(shardNames, message.getStr("shards", DocRouter.DEFAULT_NAME));
} else {
int numShards = message.getInt(ZkStateReader.NUM_SHARDS_PROP, -1);
if (numShards < 1)
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "numShards is a required parameter for 'compositeId' router");
getShardNames(numShards, shardNames);
}
//maybe null
List<DocRouter.Range> ranges = router.partitionRange(shardNames.size(), router.fullRange());
slices = new LinkedHashMap<>();
for (int i = 0; i < shardNames.size(); i++) {
String sliceName = shardNames.get(i);
Map<String, Object> sliceProps = new LinkedHashMap<>(1);
sliceProps.put(Slice.RANGE, ranges == null ? null : ranges.get(i));
slices.put(sliceName, new Slice(sliceName, null, sliceProps));
}
}
Map<String, Object> collectionProps = new HashMap<>();
for (Map.Entry<String, Object> e : OverseerCollectionMessageHandler.COLL_PROPS.entrySet()) {
Object val = message.get(e.getKey());
if (val == null) {
val = OverseerCollectionMessageHandler.COLL_PROPS.get(e.getKey());
}
if (val != null)
collectionProps.put(e.getKey(), val);
}
collectionProps.put(DocCollection.DOC_ROUTER, routerSpec);
if (message.getStr("fromApi") == null) {
collectionProps.put("autoCreated", "true");
}
//TODO default to 2; but need to debug why BasicDistributedZk2Test fails early on
String znode = message.getInt(DocCollection.STATE_FORMAT, 1) == 1 ? null : ZkStateReader.getCollectionPath(cName);
DocCollection newCollection = new DocCollection(cName, slices, collectionProps, router, -1, znode);
return new ZkWriteCommand(cName, newCollection);
}
Aggregations