Search in sources :

Example 6 with ImplicitDocRouter

use of org.apache.solr.common.cloud.ImplicitDocRouter 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);
}
Also used : ImplicitDocRouter(org.apache.solr.common.cloud.ImplicitDocRouter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Slice(org.apache.solr.common.cloud.Slice) ImplicitDocRouter(org.apache.solr.common.cloud.ImplicitDocRouter) DocRouter(org.apache.solr.common.cloud.DocRouter) DocCollection(org.apache.solr.common.cloud.DocCollection) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) SolrException(org.apache.solr.common.SolrException)

Aggregations

HashMap (java.util.HashMap)6 DocCollection (org.apache.solr.common.cloud.DocCollection)6 ImplicitDocRouter (org.apache.solr.common.cloud.ImplicitDocRouter)6 ArrayList (java.util.ArrayList)4 Slice (org.apache.solr.common.cloud.Slice)4 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 SolrException (org.apache.solr.common.SolrException)3 DocRouter (org.apache.solr.common.cloud.DocRouter)3 HashSet (java.util.HashSet)2 List (java.util.List)2 Properties (java.util.Properties)2 ClusterState (org.apache.solr.common.cloud.ClusterState)2 Replica (org.apache.solr.common.cloud.Replica)2 ZkNodeProps (org.apache.solr.common.cloud.ZkNodeProps)2 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)2 NamedList (org.apache.solr.common.util.NamedList)2 IOException (java.io.IOException)1 ConnectException (java.net.ConnectException)1 SocketException (java.net.SocketException)1