Search in sources :

Example 1 with StdNode

use of org.apache.solr.update.SolrCmdDistributor.StdNode in project lucene-solr by apache.

the class DistributedUpdateProcessor method getCollectionUrls.

private List<Node> getCollectionUrls(SolrQueryRequest req, String collection, EnumSet<Replica.Type> types) {
    ClusterState clusterState = req.getCore().getCoreContainer().getZkController().getClusterState();
    Map<String, Slice> slices = clusterState.getSlicesMap(collection);
    if (slices == null) {
        throw new ZooKeeperException(ErrorCode.BAD_REQUEST, "Could not find collection in zk: " + clusterState);
    }
    final List<Node> urls = new ArrayList<>(slices.size());
    for (Map.Entry<String, Slice> sliceEntry : slices.entrySet()) {
        Slice replicas = slices.get(sliceEntry.getKey());
        Map<String, Replica> shardMap = replicas.getReplicasMap();
        for (Entry<String, Replica> entry : shardMap.entrySet()) {
            if (!types.contains(entry.getValue().getType())) {
                continue;
            }
            ZkCoreNodeProps nodeProps = new ZkCoreNodeProps(entry.getValue());
            if (clusterState.liveNodesContain(nodeProps.getNodeName())) {
                urls.add(new StdNode(nodeProps, collection, replicas.getName()));
            }
        }
    }
    if (urls.isEmpty()) {
        return null;
    }
    return urls;
}
Also used : ClusterState(org.apache.solr.common.cloud.ClusterState) 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) Replica(org.apache.solr.common.cloud.Replica) ZooKeeperException(org.apache.solr.common.cloud.ZooKeeperException) Slice(org.apache.solr.common.cloud.Slice) StdNode(org.apache.solr.update.SolrCmdDistributor.StdNode) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with StdNode

use of org.apache.solr.update.SolrCmdDistributor.StdNode in project lucene-solr by apache.

the class DistributedUpdateProcessor method getSubShardLeaders.

private List<Node> getSubShardLeaders(DocCollection coll, String shardId, String docId, SolrInputDocument doc) {
    Collection<Slice> allSlices = coll.getSlices();
    List<Node> nodes = null;
    for (Slice aslice : allSlices) {
        final Slice.State state = aslice.getState();
        if (state == Slice.State.CONSTRUCTION || state == Slice.State.RECOVERY) {
            DocRouter.Range myRange = coll.getSlice(shardId).getRange();
            if (myRange == null)
                myRange = new DocRouter.Range(Integer.MIN_VALUE, Integer.MAX_VALUE);
            boolean isSubset = aslice.getRange() != null && aslice.getRange().isSubsetOf(myRange);
            if (isSubset && (// in case of deletes
            docId == null || (docId != null && coll.getRouter().isTargetSlice(docId, doc, req.getParams(), aslice.getName(), coll)))) {
                Replica sliceLeader = aslice.getLeader();
                // slice leader can be null because node/shard is created zk before leader election
                if (sliceLeader != null && zkController.getClusterState().liveNodesContain(sliceLeader.getNodeName())) {
                    if (nodes == null)
                        nodes = new ArrayList<>();
                    ZkCoreNodeProps nodeProps = new ZkCoreNodeProps(sliceLeader);
                    nodes.add(new StdNode(nodeProps, coll.getName(), shardId));
                }
            }
        }
    }
    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) Replica(org.apache.solr.common.cloud.Replica) Slice(org.apache.solr.common.cloud.Slice) State(org.apache.solr.common.cloud.Slice.State) DocRouter(org.apache.solr.common.cloud.DocRouter) StdNode(org.apache.solr.update.SolrCmdDistributor.StdNode)

Example 3 with StdNode

use of org.apache.solr.update.SolrCmdDistributor.StdNode in project lucene-solr by apache.

the class DistributedUpdateProcessor method setupRequestForDBQ.

// used for deleteByQuery to get the list of nodes this leader should forward to
private List<Node> setupRequestForDBQ() {
    List<Node> nodes = null;
    String shardId = cloudDesc.getShardId();
    try {
        Replica leaderReplica = zkController.getZkStateReader().getLeaderRetry(collection, shardId);
        isLeader = leaderReplica.getName().equals(req.getCore().getCoreDescriptor().getCloudDescriptor().getCoreNodeName());
        // TODO: what if we are no longer the leader?
        forwardToLeader = false;
        List<ZkCoreNodeProps> replicaProps = zkController.getZkStateReader().getReplicaProps(collection, shardId, leaderReplica.getName(), null, Replica.State.DOWN, EnumSet.of(Replica.Type.NRT, Replica.Type.TLOG));
        if (replicaProps != null) {
            nodes = new ArrayList<>(replicaProps.size());
            for (ZkCoreNodeProps props : replicaProps) {
                nodes.add(new StdNode(props, collection, shardId));
            }
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR, "", e);
    }
    return nodes;
}
Also used : ZooKeeperException(org.apache.solr.common.cloud.ZooKeeperException) 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) StdNode(org.apache.solr.update.SolrCmdDistributor.StdNode) Replica(org.apache.solr.common.cloud.Replica)

Example 4 with StdNode

use of org.apache.solr.update.SolrCmdDistributor.StdNode in project lucene-solr by apache.

the class DistributedUpdateProcessor method setupRequest.

private List<Node> setupRequest(String id, SolrInputDocument doc, String route) {
    List<Node> nodes = null;
    // if we are in zk mode...
    if (zkEnabled) {
        assert TestInjection.injectUpdateRandomPause();
        if ((updateCommand.getFlags() & (UpdateCommand.REPLAY | UpdateCommand.PEER_SYNC)) != 0) {
            // we actually might be the leader, but we don't want leader-logic for these types of updates anyway.
            isLeader = false;
            forwardToLeader = false;
            return nodes;
        }
        ClusterState cstate = zkController.getClusterState();
        DocCollection coll = cstate.getCollection(collection);
        Slice slice = coll.getRouter().getTargetSlice(id, doc, route, req.getParams(), coll);
        if (slice == null) {
            // No slice found.  Most strict routers will have already thrown an exception, so a null return is
            // a signal to use the slice of this core.
            // TODO: what if this core is not in the targeted collection?
            String shardId = req.getCore().getCoreDescriptor().getCloudDescriptor().getShardId();
            slice = coll.getSlice(shardId);
            if (slice == null) {
                throw new SolrException(ErrorCode.BAD_REQUEST, "No shard " + shardId + " in " + coll);
            }
        }
        DistribPhase phase = DistribPhase.parseParam(req.getParams().get(DISTRIB_UPDATE_PARAM));
        if (DistribPhase.FROMLEADER == phase && !couldIbeSubShardLeader(coll)) {
            if (req.getCore().getCoreDescriptor().getCloudDescriptor().isLeader()) {
            // locally we think we are leader but the request says it came FROMLEADER
            // that could indicate a problem, let the full logic below figure it out
            } else {
                assert TestInjection.injectFailReplicaRequests();
                // we actually might be the leader, but we don't want leader-logic for these types of updates anyway.
                isLeader = false;
                forwardToLeader = false;
                return nodes;
            }
        }
        String shardId = slice.getName();
        try {
            // Not equivalent to getLeaderProps, which does retries to find a leader.
            // Replica leader = slice.getLeader();
            Replica leaderReplica = zkController.getZkStateReader().getLeaderRetry(collection, shardId);
            isLeader = leaderReplica.getName().equals(req.getCore().getCoreDescriptor().getCloudDescriptor().getCoreNodeName());
            if (!isLeader) {
                isSubShardLeader = amISubShardLeader(coll, slice, id, doc);
                if (isSubShardLeader) {
                    String myShardId = req.getCore().getCoreDescriptor().getCloudDescriptor().getShardId();
                    slice = coll.getSlice(myShardId);
                    shardId = myShardId;
                    leaderReplica = zkController.getZkStateReader().getLeaderRetry(collection, myShardId);
                    List<ZkCoreNodeProps> myReplicas = zkController.getZkStateReader().getReplicaProps(collection, shardId, leaderReplica.getName(), null, Replica.State.DOWN);
                }
            }
            doDefensiveChecks(phase);
            // if request is coming from another collection then we want it to be sent to all replicas
            // even if its phase is FROMLEADER
            String fromCollection = updateCommand.getReq().getParams().get(DISTRIB_FROM_COLLECTION);
            if (DistribPhase.FROMLEADER == phase && !isSubShardLeader && fromCollection == null) {
                // we are coming from the leader, just go local - add no urls
                forwardToLeader = false;
            } else if (isLeader || isSubShardLeader) {
                // that means I want to forward onto my replicas...
                // so get the replicas...
                forwardToLeader = false;
                List<ZkCoreNodeProps> replicaProps = zkController.getZkStateReader().getReplicaProps(collection, shardId, leaderReplica.getName(), null, Replica.State.DOWN);
                if (replicaProps != null) {
                    if (nodes == null) {
                        nodes = new ArrayList<>(replicaProps.size());
                    }
                    // check for test param that lets us miss replicas
                    String[] skipList = req.getParams().getParams(TEST_DISTRIB_SKIP_SERVERS);
                    Set<String> skipListSet = null;
                    if (skipList != null) {
                        skipListSet = new HashSet<>(skipList.length);
                        skipListSet.addAll(Arrays.asList(skipList));
                        log.info("test.distrib.skip.servers was found and contains:" + skipListSet);
                    }
                    for (ZkCoreNodeProps props : replicaProps) {
                        if (skipList != null) {
                            boolean skip = skipListSet.contains(props.getCoreUrl());
                            log.info("check url:" + props.getCoreUrl() + " against:" + skipListSet + " result:" + skip);
                            if (!skip) {
                                nodes.add(new StdNode(props, collection, shardId));
                            }
                        } else {
                            nodes.add(new StdNode(props, collection, shardId));
                        }
                    }
                }
            } else {
                // I need to forward onto the leader...
                nodes = new ArrayList<>(1);
                nodes.add(new RetryNode(new ZkCoreNodeProps(leaderReplica), zkController.getZkStateReader(), collection, shardId));
                forwardToLeader = true;
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR, "", e);
        }
    }
    return nodes;
}
Also used : ClusterState(org.apache.solr.common.cloud.ClusterState) RetryNode(org.apache.solr.update.SolrCmdDistributor.RetryNode) EnumSet(java.util.EnumSet) Set(java.util.Set) HashSet(java.util.HashSet) 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) Replica(org.apache.solr.common.cloud.Replica) ZooKeeperException(org.apache.solr.common.cloud.ZooKeeperException) Slice(org.apache.solr.common.cloud.Slice) StdNode(org.apache.solr.update.SolrCmdDistributor.StdNode) List(java.util.List) ArrayList(java.util.ArrayList) NamedList(org.apache.solr.common.util.NamedList) DocCollection(org.apache.solr.common.cloud.DocCollection) SolrException(org.apache.solr.common.SolrException) HashSet(java.util.HashSet)

Example 5 with StdNode

use of org.apache.solr.update.SolrCmdDistributor.StdNode in project lucene-solr by apache.

the class SolrCmdDistributorTest method test.

@Test
@ShardsFixed(num = 4)
public void test() throws Exception {
    del("*:*");
    SolrCmdDistributor cmdDistrib = new SolrCmdDistributor(updateShardHandler);
    ModifiableSolrParams params = new ModifiableSolrParams();
    List<Node> nodes = new ArrayList<>();
    ZkNodeProps nodeProps = new ZkNodeProps(ZkStateReader.BASE_URL_PROP, ((HttpSolrClient) controlClient).getBaseURL(), ZkStateReader.CORE_NAME_PROP, "");
    nodes.add(new StdNode(new ZkCoreNodeProps(nodeProps)));
    // add one doc to controlClient
    AddUpdateCommand cmd = new AddUpdateCommand(null);
    cmd.solrDoc = sdoc("id", id.incrementAndGet());
    params = new ModifiableSolrParams();
    cmdDistrib.distribAdd(cmd, nodes, params);
    CommitUpdateCommand ccmd = new CommitUpdateCommand(null, false);
    params = new ModifiableSolrParams();
    params.set(DistributedUpdateProcessor.COMMIT_END_POINT, true);
    cmdDistrib.distribCommit(ccmd, nodes, params);
    cmdDistrib.finish();
    List<Error> errors = cmdDistrib.getErrors();
    assertEquals(errors.toString(), 0, errors.size());
    long numFound = controlClient.query(new SolrQuery("*:*")).getResults().getNumFound();
    assertEquals(1, numFound);
    HttpSolrClient client = (HttpSolrClient) clients.get(0);
    nodeProps = new ZkNodeProps(ZkStateReader.BASE_URL_PROP, client.getBaseURL(), ZkStateReader.CORE_NAME_PROP, "");
    nodes.add(new StdNode(new ZkCoreNodeProps(nodeProps)));
    // add another 2 docs to control and 3 to client
    cmdDistrib = new SolrCmdDistributor(updateShardHandler);
    cmd.solrDoc = sdoc("id", id.incrementAndGet());
    params = new ModifiableSolrParams();
    params.set(DistributedUpdateProcessor.COMMIT_END_POINT, true);
    cmdDistrib.distribAdd(cmd, nodes, params);
    int id2 = id.incrementAndGet();
    AddUpdateCommand cmd2 = new AddUpdateCommand(null);
    cmd2.solrDoc = sdoc("id", id2);
    params = new ModifiableSolrParams();
    params.set(DistributedUpdateProcessor.COMMIT_END_POINT, true);
    cmdDistrib.distribAdd(cmd2, nodes, params);
    AddUpdateCommand cmd3 = new AddUpdateCommand(null);
    cmd3.solrDoc = sdoc("id", id.incrementAndGet());
    params = new ModifiableSolrParams();
    params.set(DistributedUpdateProcessor.COMMIT_END_POINT, true);
    cmdDistrib.distribAdd(cmd3, Collections.singletonList(nodes.get(1)), params);
    params = new ModifiableSolrParams();
    params.set(DistributedUpdateProcessor.COMMIT_END_POINT, true);
    cmdDistrib.distribCommit(ccmd, nodes, params);
    cmdDistrib.finish();
    errors = cmdDistrib.getErrors();
    assertEquals(errors.toString(), 0, errors.size());
    SolrDocumentList results = controlClient.query(new SolrQuery("*:*")).getResults();
    numFound = results.getNumFound();
    assertEquals(results.toString(), 3, numFound);
    numFound = client.query(new SolrQuery("*:*")).getResults().getNumFound();
    assertEquals(3, numFound);
    // now delete doc 2 which is on both control and client1
    DeleteUpdateCommand dcmd = new DeleteUpdateCommand(null);
    dcmd.id = Integer.toString(id2);
    cmdDistrib = new SolrCmdDistributor(updateShardHandler);
    params = new ModifiableSolrParams();
    params.set(DistributedUpdateProcessor.COMMIT_END_POINT, true);
    cmdDistrib.distribDelete(dcmd, nodes, params);
    params = new ModifiableSolrParams();
    params.set(DistributedUpdateProcessor.COMMIT_END_POINT, true);
    cmdDistrib.distribCommit(ccmd, nodes, params);
    cmdDistrib.finish();
    errors = cmdDistrib.getErrors();
    assertEquals(errors.toString(), 0, errors.size());
    results = controlClient.query(new SolrQuery("*:*")).getResults();
    numFound = results.getNumFound();
    assertEquals(results.toString(), 2, numFound);
    numFound = client.query(new SolrQuery("*:*")).getResults().getNumFound();
    assertEquals(results.toString(), 2, numFound);
    for (SolrClient c : clients) {
        c.optimize();
    //System.out.println(clients.get(0).request(new LukeRequest()));
    }
    cmdDistrib = new SolrCmdDistributor(updateShardHandler);
    int cnt = atLeast(303);
    for (int i = 0; i < cnt; i++) {
        nodes.clear();
        for (SolrClient c : clients) {
            if (random().nextBoolean()) {
                continue;
            }
            HttpSolrClient httpClient = (HttpSolrClient) c;
            nodeProps = new ZkNodeProps(ZkStateReader.BASE_URL_PROP, httpClient.getBaseURL(), ZkStateReader.CORE_NAME_PROP, "");
            nodes.add(new StdNode(new ZkCoreNodeProps(nodeProps)));
        }
        AddUpdateCommand c = new AddUpdateCommand(null);
        c.solrDoc = sdoc("id", id.incrementAndGet());
        if (nodes.size() > 0) {
            params = new ModifiableSolrParams();
            cmdDistrib.distribAdd(c, nodes, params);
        }
    }
    nodes.clear();
    for (SolrClient c : clients) {
        HttpSolrClient httpClient = (HttpSolrClient) c;
        nodeProps = new ZkNodeProps(ZkStateReader.BASE_URL_PROP, httpClient.getBaseURL(), ZkStateReader.CORE_NAME_PROP, "");
        nodes.add(new StdNode(new ZkCoreNodeProps(nodeProps)));
    }
    final AtomicInteger commits = new AtomicInteger();
    for (JettySolrRunner jetty : jettys) {
        CoreContainer cores = jetty.getCoreContainer();
        try (SolrCore core = cores.getCore("collection1")) {
            core.getUpdateHandler().registerCommitCallback(new SolrEventListener() {

                @Override
                public void init(NamedList args) {
                }

                @Override
                public void postSoftCommit() {
                }

                @Override
                public void postCommit() {
                    commits.incrementAndGet();
                }

                @Override
                public void newSearcher(SolrIndexSearcher newSearcher, SolrIndexSearcher currentSearcher) {
                }
            });
        }
    }
    params = new ModifiableSolrParams();
    params.set(DistributedUpdateProcessor.COMMIT_END_POINT, true);
    cmdDistrib.distribCommit(ccmd, nodes, params);
    cmdDistrib.finish();
    assertEquals(getShardCount(), commits.get());
    for (SolrClient c : clients) {
        NamedList<Object> resp = c.request(new LukeRequest());
        assertEquals("SOLR-3428: We only did adds - there should be no deletes", ((NamedList<Object>) resp.get("index")).get("numDocs"), ((NamedList<Object>) resp.get("index")).get("maxDoc"));
    }
    testMaxRetries();
    testOneRetry();
    testRetryNodeAgainstBadAddress();
    testRetryNodeWontRetrySocketError();
    testDistribOpenSearcher();
}
Also used : ZkCoreNodeProps(org.apache.solr.common.cloud.ZkCoreNodeProps) SolrCore(org.apache.solr.core.SolrCore) RetryNode(org.apache.solr.update.SolrCmdDistributor.RetryNode) StdNode(org.apache.solr.update.SolrCmdDistributor.StdNode) Node(org.apache.solr.update.SolrCmdDistributor.Node) ZkNodeProps(org.apache.solr.common.cloud.ZkNodeProps) ArrayList(java.util.ArrayList) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrQuery(org.apache.solr.client.solrj.SolrQuery) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) CoreContainer(org.apache.solr.core.CoreContainer) SolrClient(org.apache.solr.client.solrj.SolrClient) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) StdNode(org.apache.solr.update.SolrCmdDistributor.StdNode) LukeRequest(org.apache.solr.client.solrj.request.LukeRequest) JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) NamedList(org.apache.solr.common.util.NamedList) Error(org.apache.solr.update.SolrCmdDistributor.Error) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) SolrEventListener(org.apache.solr.core.SolrEventListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

ZkCoreNodeProps (org.apache.solr.common.cloud.ZkCoreNodeProps)8 RetryNode (org.apache.solr.update.SolrCmdDistributor.RetryNode)8 StdNode (org.apache.solr.update.SolrCmdDistributor.StdNode)8 ArrayList (java.util.ArrayList)7 Replica (org.apache.solr.common.cloud.Replica)7 Node (org.apache.solr.update.SolrCmdDistributor.Node)7 ZooKeeperException (org.apache.solr.common.cloud.ZooKeeperException)6 Slice (org.apache.solr.common.cloud.Slice)5 SolrException (org.apache.solr.common.SolrException)4 DocCollection (org.apache.solr.common.cloud.DocCollection)3 IOException (java.io.IOException)2 SolrServerException (org.apache.solr.client.solrj.SolrServerException)2 ClusterState (org.apache.solr.common.cloud.ClusterState)2 DocRouter (org.apache.solr.common.cloud.DocRouter)2 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)2 NamedList (org.apache.solr.common.util.NamedList)2 Error (org.apache.solr.update.SolrCmdDistributor.Error)2 KeeperException (org.apache.zookeeper.KeeperException)2 EnumSet (java.util.EnumSet)1 HashMap (java.util.HashMap)1