Search in sources :

Example 21 with DocRouter

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);
}
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

DocRouter (org.apache.solr.common.cloud.DocRouter)21 Slice (org.apache.solr.common.cloud.Slice)16 DocCollection (org.apache.solr.common.cloud.DocCollection)12 ArrayList (java.util.ArrayList)9 ClusterState (org.apache.solr.common.cloud.ClusterState)9 HashMap (java.util.HashMap)8 Map (java.util.Map)6 SolrException (org.apache.solr.common.SolrException)6 HashSet (java.util.HashSet)5 IOException (java.io.IOException)4 List (java.util.List)4 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)4 CompositeIdRouter (org.apache.solr.common.cloud.CompositeIdRouter)4 Replica (org.apache.solr.common.cloud.Replica)4 SolrQuery (org.apache.solr.client.solrj.SolrQuery)3 SolrServerException (org.apache.solr.client.solrj.SolrServerException)3 ImplicitDocRouter (org.apache.solr.common.cloud.ImplicitDocRouter)3 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)3 NamedList (org.apache.solr.common.util.NamedList)3 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)3