Search in sources :

Example 1 with DistributedQueue

use of org.apache.solr.cloud.DistributedQueue in project lucene-solr by apache.

the class DistributedUpdateProcessor method getNodesByRoutingRules.

private List<Node> getNodesByRoutingRules(ClusterState cstate, DocCollection coll, String id, SolrInputDocument doc) {
    DocRouter router = coll.getRouter();
    List<Node> nodes = null;
    if (router instanceof CompositeIdRouter) {
        CompositeIdRouter compositeIdRouter = (CompositeIdRouter) router;
        String myShardId = req.getCore().getCoreDescriptor().getCloudDescriptor().getShardId();
        Slice slice = coll.getSlice(myShardId);
        Map<String, RoutingRule> routingRules = slice.getRoutingRules();
        if (routingRules != null) {
            // delete by query case
            if (id == null) {
                for (Entry<String, RoutingRule> entry : routingRules.entrySet()) {
                    String targetCollectionName = entry.getValue().getTargetCollectionName();
                    Collection<Slice> activeSlices = cstate.getActiveSlices(targetCollectionName);
                    if (activeSlices != null && !activeSlices.isEmpty()) {
                        Slice any = activeSlices.iterator().next();
                        if (nodes == null)
                            nodes = new ArrayList<>();
                        nodes.add(new StdNode(new ZkCoreNodeProps(any.getLeader())));
                    }
                }
                return nodes;
            }
            String routeKey = SolrIndexSplitter.getRouteKey(id);
            if (routeKey != null) {
                RoutingRule rule = routingRules.get(routeKey + "!");
                if (rule != null) {
                    if (!rule.isExpired()) {
                        List<DocRouter.Range> ranges = rule.getRouteRanges();
                        if (ranges != null && !ranges.isEmpty()) {
                            int hash = compositeIdRouter.sliceHash(id, doc, null, coll);
                            for (DocRouter.Range range : ranges) {
                                if (range.includes(hash)) {
                                    DocCollection targetColl = cstate.getCollection(rule.getTargetCollectionName());
                                    Collection<Slice> activeSlices = targetColl.getRouter().getSearchSlicesSingle(id, null, targetColl);
                                    if (activeSlices == null || activeSlices.isEmpty()) {
                                        throw new SolrException(ErrorCode.SERVER_ERROR, "No active slices serving " + id + " found for target collection: " + rule.getTargetCollectionName());
                                    }
                                    Replica targetLeader = targetColl.getLeader(activeSlices.iterator().next().getName());
                                    nodes = new ArrayList<>(1);
                                    nodes.add(new StdNode(new ZkCoreNodeProps(targetLeader)));
                                    break;
                                }
                            }
                        }
                    } else {
                        ReentrantLock ruleExpiryLock = req.getCore().getRuleExpiryLock();
                        if (!ruleExpiryLock.isLocked()) {
                            try {
                                if (ruleExpiryLock.tryLock(10, TimeUnit.MILLISECONDS)) {
                                    log.info("Going to expire routing rule");
                                    try {
                                        Map<String, Object> map = Utils.makeMap(Overseer.QUEUE_OPERATION, OverseerAction.REMOVEROUTINGRULE.toLower(), ZkStateReader.COLLECTION_PROP, collection, ZkStateReader.SHARD_ID_PROP, myShardId, "routeKey", routeKey + "!");
                                        SolrZkClient zkClient = req.getCore().getCoreContainer().getZkController().getZkClient();
                                        DistributedQueue queue = Overseer.getStateUpdateQueue(zkClient);
                                        queue.offer(Utils.toJSON(map));
                                    } catch (KeeperException e) {
                                        log.warn("Exception while removing routing rule for route key: " + routeKey, e);
                                    } catch (Exception e) {
                                        log.error("Exception while removing routing rule for route key: " + routeKey, e);
                                    } finally {
                                        ruleExpiryLock.unlock();
                                    }
                                }
                            } catch (InterruptedException e) {
                                Thread.currentThread().interrupt();
                            }
                        }
                    }
                }
            }
        }
    }
    return nodes;
}
Also used : ZkCoreNodeProps(org.apache.solr.common.cloud.ZkCoreNodeProps) RetryNode(org.apache.solr.update.SolrCmdDistributor.RetryNode) Node(org.apache.solr.update.SolrCmdDistributor.Node) StdNode(org.apache.solr.update.SolrCmdDistributor.StdNode) ArrayList(java.util.ArrayList) DocRouter(org.apache.solr.common.cloud.DocRouter) StdNode(org.apache.solr.update.SolrCmdDistributor.StdNode) DocCollection(org.apache.solr.common.cloud.DocCollection) RoutingRule(org.apache.solr.common.cloud.RoutingRule) SolrException(org.apache.solr.common.SolrException) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Replica(org.apache.solr.common.cloud.Replica) SolrZkClient(org.apache.solr.common.cloud.SolrZkClient) SolrServerException(org.apache.solr.client.solrj.SolrServerException) SolrException(org.apache.solr.common.SolrException) ZooKeeperException(org.apache.solr.common.cloud.ZooKeeperException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) CompositeIdRouter(org.apache.solr.common.cloud.CompositeIdRouter) Slice(org.apache.solr.common.cloud.Slice) DistributedQueue(org.apache.solr.cloud.DistributedQueue) ZooKeeperException(org.apache.solr.common.cloud.ZooKeeperException) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 SolrServerException (org.apache.solr.client.solrj.SolrServerException)1 DistributedQueue (org.apache.solr.cloud.DistributedQueue)1 SolrException (org.apache.solr.common.SolrException)1 CompositeIdRouter (org.apache.solr.common.cloud.CompositeIdRouter)1 DocCollection (org.apache.solr.common.cloud.DocCollection)1 DocRouter (org.apache.solr.common.cloud.DocRouter)1 Replica (org.apache.solr.common.cloud.Replica)1 RoutingRule (org.apache.solr.common.cloud.RoutingRule)1 Slice (org.apache.solr.common.cloud.Slice)1 SolrZkClient (org.apache.solr.common.cloud.SolrZkClient)1 ZkCoreNodeProps (org.apache.solr.common.cloud.ZkCoreNodeProps)1 ZooKeeperException (org.apache.solr.common.cloud.ZooKeeperException)1 Node (org.apache.solr.update.SolrCmdDistributor.Node)1 RetryNode (org.apache.solr.update.SolrCmdDistributor.RetryNode)1 StdNode (org.apache.solr.update.SolrCmdDistributor.StdNode)1 KeeperException (org.apache.zookeeper.KeeperException)1