Search in sources :

Example 61 with Replica

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

the class AbstractFullDistribZkTestBase method assertDocCounts.

protected void assertDocCounts(boolean verbose) throws Exception {
    // and node/client to shard?
    if (verbose)
        System.err.println("control docs:" + controlClient.query(new SolrQuery("*:*")).getResults().getNumFound() + "\n\n");
    long controlCount = controlClient.query(new SolrQuery("*:*")).getResults().getNumFound();
    // do some really inefficient mapping...
    Map<String, Slice> slices = null;
    ClusterState clusterState;
    try (ZkStateReader zk = new ZkStateReader(zkServer.getZkAddress(), AbstractZkTestCase.TIMEOUT, AbstractZkTestCase.TIMEOUT)) {
        zk.createClusterStateWatchersAndUpdate();
        clusterState = zk.getClusterState();
        slices = clusterState.getSlicesMap(DEFAULT_COLLECTION);
    }
    if (slices == null) {
        throw new RuntimeException("Could not find collection " + DEFAULT_COLLECTION + " in " + clusterState.getCollectionsMap().keySet());
    }
    for (CloudJettyRunner cjetty : cloudJettys) {
        CloudSolrServerClient client = cjetty.client;
        for (Map.Entry<String, Slice> slice : slices.entrySet()) {
            Map<String, Replica> theShards = slice.getValue().getReplicasMap();
            for (Map.Entry<String, Replica> shard : theShards.entrySet()) {
                String shardName = new URI(((HttpSolrClient) client.solrClient).getBaseURL()).getPort() + "_solr_";
                if (verbose && shard.getKey().endsWith(shardName)) {
                    System.err.println("shard:" + slice.getKey());
                    System.err.println(shard.getValue());
                }
            }
        }
        ZkStateReader zkStateReader = cloudClient.getZkStateReader();
        long count = 0;
        final Replica.State currentState = Replica.State.getState(cjetty.info.getStr(ZkStateReader.STATE_PROP));
        if (currentState == Replica.State.ACTIVE && zkStateReader.getClusterState().liveNodesContain(cjetty.info.getStr(ZkStateReader.NODE_NAME_PROP))) {
            SolrQuery query = new SolrQuery("*:*");
            query.set("distrib", false);
            count = client.solrClient.query(query).getResults().getNumFound();
        }
        if (verbose)
            System.err.println("client docs:" + count + "\n\n");
    }
    if (verbose)
        System.err.println("control docs:" + controlClient.query(new SolrQuery("*:*")).getResults().getNumFound() + "\n\n");
    SolrQuery query = new SolrQuery("*:*");
    assertEquals("Doc Counts do not add up", controlCount, cloudClient.query(query).getResults().getNumFound());
}
Also used : ClusterState(org.apache.solr.common.cloud.ClusterState) Replica(org.apache.solr.common.cloud.Replica) URI(java.net.URI) SolrQuery(org.apache.solr.client.solrj.SolrQuery) ZkStateReader(org.apache.solr.common.cloud.ZkStateReader) Slice(org.apache.solr.common.cloud.Slice) Map(java.util.Map) Utils.makeMap(org.apache.solr.common.util.Utils.makeMap) HashMap(java.util.HashMap)

Example 62 with Replica

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

the class AbstractFullDistribZkTestBase method waitForReplicationFromReplicas.

protected void waitForReplicationFromReplicas(String collectionName, ZkStateReader zkStateReader, TimeOut timeout) throws KeeperException, InterruptedException, IOException {
    zkStateReader.forceUpdateCollection(collectionName);
    DocCollection collection = zkStateReader.getClusterState().getCollection(collectionName);
    Map<String, CoreContainer> containers = new HashMap<>();
    for (JettySolrRunner runner : jettys) {
        if (!runner.isRunning()) {
            continue;
        }
        containers.put(runner.getNodeName(), runner.getCoreContainer());
    }
    for (Slice s : collection.getSlices()) {
        Replica leader = s.getLeader();
        long leaderIndexVersion = -1;
        while (!timeout.hasTimedOut()) {
            leaderIndexVersion = getIndexVersion(leader);
            if (leaderIndexVersion >= 0) {
                break;
            }
            Thread.sleep(1000);
        }
        if (timeout.hasTimedOut()) {
            fail("Unable to get leader indexVersion");
        }
        for (Replica pullReplica : s.getReplicas(EnumSet.of(Replica.Type.PULL, Replica.Type.TLOG))) {
            if (!zkStateReader.getClusterState().liveNodesContain(pullReplica.getNodeName())) {
                continue;
            }
            while (true) {
                long replicaIndexVersion = getIndexVersion(pullReplica);
                if (leaderIndexVersion == replicaIndexVersion) {
                    log.debug("Leader replica's version ({}) in sync with replica({}): {} == {}", leader.getName(), pullReplica.getName(), leaderIndexVersion, replicaIndexVersion);
                    // Make sure the host is serving the correct version
                    try (SolrCore core = containers.get(pullReplica.getNodeName()).getCore(pullReplica.getCoreName())) {
                        RefCounted<SolrIndexSearcher> ref = core.getRegisteredSearcher();
                        try {
                            SolrIndexSearcher searcher = ref.get();
                            String servingVersion = searcher.getIndexReader().getIndexCommit().getUserData().get(SolrIndexWriter.COMMIT_TIME_MSEC_KEY);
                            if (Long.parseLong(servingVersion) == replicaIndexVersion) {
                                break;
                            } else {
                                log.debug("Replica {} has the correct version replicated, but the searcher is not ready yet. Replicated version: {}, Serving version: {}", pullReplica.getName(), replicaIndexVersion, servingVersion);
                            }
                        } finally {
                            if (ref != null)
                                ref.decref();
                        }
                    }
                } else {
                    if (timeout.hasTimedOut()) {
                        logReplicaTypesReplicationInfo(collectionName, zkStateReader);
                        fail(String.format(Locale.ROOT, "Timed out waiting for replica %s (%d) to replicate from leader %s (%d)", pullReplica.getName(), replicaIndexVersion, leader.getName(), leaderIndexVersion));
                    }
                    if (leaderIndexVersion > replicaIndexVersion) {
                        log.debug("{} version is {} and leader's is {}, will wait for replication", pullReplica.getName(), replicaIndexVersion, leaderIndexVersion);
                    } else {
                        log.debug("Leader replica's version ({}) is lower than pull replica({}): {} < {}", leader.getName(), pullReplica.getName(), leaderIndexVersion, replicaIndexVersion);
                    }
                }
                Thread.sleep(1000);
            }
        }
    }
}
Also used : CoreContainer(org.apache.solr.core.CoreContainer) HashMap(java.util.HashMap) JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) Slice(org.apache.solr.common.cloud.Slice) SolrCore(org.apache.solr.core.SolrCore) DocCollection(org.apache.solr.common.cloud.DocCollection) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) Replica(org.apache.solr.common.cloud.Replica)

Example 63 with Replica

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

the class AbstractFullDistribZkTestBase method updateMappingsFromZk.

protected void updateMappingsFromZk(List<JettySolrRunner> jettys, List<SolrClient> clients, boolean allowOverSharding) throws Exception {
    ZkStateReader zkStateReader = cloudClient.getZkStateReader();
    zkStateReader.forceUpdateCollection(DEFAULT_COLLECTION);
    cloudJettys.clear();
    shardToJetty.clear();
    ClusterState clusterState = zkStateReader.getClusterState();
    DocCollection coll = clusterState.getCollection(DEFAULT_COLLECTION);
    List<CloudSolrServerClient> theClients = new ArrayList<>();
    for (SolrClient client : clients) {
        // find info for this client in zk 
        nextClient: // we find out state by simply matching ports...
        for (Slice slice : coll.getSlices()) {
            for (Replica replica : slice.getReplicas()) {
                int port = new URI(((HttpSolrClient) client).getBaseURL()).getPort();
                if (replica.getStr(ZkStateReader.BASE_URL_PROP).contains(":" + port)) {
                    CloudSolrServerClient csc = new CloudSolrServerClient();
                    csc.solrClient = client;
                    csc.port = port;
                    csc.shardName = replica.getStr(ZkStateReader.NODE_NAME_PROP);
                    csc.info = replica;
                    theClients.add(csc);
                    break nextClient;
                }
            }
        }
    }
    for (JettySolrRunner jetty : jettys) {
        int port = jetty.getLocalPort();
        if (port == -1) {
            throw new RuntimeException("Cannot find the port for jetty");
        }
        nextJetty: for (Slice slice : coll.getSlices()) {
            Set<Entry<String, Replica>> entries = slice.getReplicasMap().entrySet();
            for (Entry<String, Replica> entry : entries) {
                Replica replica = entry.getValue();
                if (replica.getStr(ZkStateReader.BASE_URL_PROP).contains(":" + port)) {
                    List<CloudJettyRunner> list = shardToJetty.get(slice.getName());
                    if (list == null) {
                        list = new ArrayList<>();
                        shardToJetty.put(slice.getName(), list);
                    }
                    boolean isLeader = slice.getLeader() == replica;
                    CloudJettyRunner cjr = new CloudJettyRunner();
                    cjr.jetty = jetty;
                    cjr.info = replica;
                    cjr.nodeName = replica.getStr(ZkStateReader.NODE_NAME_PROP);
                    cjr.coreNodeName = entry.getKey();
                    cjr.url = replica.getStr(ZkStateReader.BASE_URL_PROP) + "/" + replica.getStr(ZkStateReader.CORE_NAME_PROP);
                    cjr.client = findClientByPort(port, theClients);
                    list.add(cjr);
                    if (isLeader) {
                        shardToLeaderJetty.put(slice.getName(), cjr);
                    }
                    cloudJettys.add(cjr);
                    break nextJetty;
                }
            }
        }
    }
    // running jetty though
    for (Slice slice : coll.getSlices()) {
        // check that things look right
        List<CloudJettyRunner> jetties = shardToJetty.get(slice.getName());
        if (!allowOverSharding) {
            assertNotNull("Test setup problem: We found no jetties for shard: " + slice.getName() + " just:" + shardToJetty.keySet(), jetties);
            assertEquals("slice:" + slice.getName(), slice.getReplicas().size(), jetties.size());
        }
    }
}
Also used : ClusterState(org.apache.solr.common.cloud.ClusterState) EnumSet(java.util.EnumSet) Set(java.util.Set) HashSet(java.util.HashSet) JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) ArrayList(java.util.ArrayList) Replica(org.apache.solr.common.cloud.Replica) URI(java.net.URI) ZkStateReader(org.apache.solr.common.cloud.ZkStateReader) Entry(java.util.Map.Entry) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient) SolrClient(org.apache.solr.client.solrj.SolrClient) Slice(org.apache.solr.common.cloud.Slice) SolrDocumentList(org.apache.solr.common.SolrDocumentList) List(java.util.List) ArrayList(java.util.ArrayList) NamedList(org.apache.solr.common.util.NamedList) DocCollection(org.apache.solr.common.cloud.DocCollection)

Example 64 with Replica

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

the class AbstractDistribZkTestBase method waitForRecoveriesToFinish.

public static void waitForRecoveriesToFinish(String collection, ZkStateReader zkStateReader, boolean verbose, boolean failOnTimeout, int timeoutSeconds) throws Exception {
    log.info("Wait for recoveries to finish - collection: " + collection + " failOnTimeout:" + failOnTimeout + " timeout (sec):" + timeoutSeconds);
    boolean cont = true;
    int cnt = 0;
    while (cont) {
        if (verbose)
            System.out.println("-");
        boolean sawLiveRecovering = false;
        ClusterState clusterState = zkStateReader.getClusterState();
        Map<String, Slice> slices = clusterState.getSlicesMap(collection);
        assertNotNull("Could not find collection:" + collection, slices);
        for (Map.Entry<String, Slice> entry : slices.entrySet()) {
            Slice slice = entry.getValue();
            if (slice.getState() == Slice.State.CONSTRUCTION) {
                // similar to replica recovering; pretend its the same thing
                if (verbose)
                    System.out.println("Found a slice in construction state; will wait.");
                sawLiveRecovering = true;
            }
            Map<String, Replica> shards = slice.getReplicasMap();
            for (Map.Entry<String, Replica> shard : shards.entrySet()) {
                if (verbose)
                    System.out.println("replica:" + shard.getValue().getName() + " rstate:" + shard.getValue().getStr(ZkStateReader.STATE_PROP) + " live:" + clusterState.liveNodesContain(shard.getValue().getNodeName()));
                final Replica.State state = shard.getValue().getState();
                if ((state == Replica.State.RECOVERING || state == Replica.State.DOWN || state == Replica.State.RECOVERY_FAILED) && clusterState.liveNodesContain(shard.getValue().getStr(ZkStateReader.NODE_NAME_PROP))) {
                    sawLiveRecovering = true;
                }
            }
        }
        if (!sawLiveRecovering || cnt == timeoutSeconds) {
            if (!sawLiveRecovering) {
                if (verbose)
                    System.out.println("no one is recoverying");
            } else {
                if (verbose)
                    System.out.println("Gave up waiting for recovery to finish..");
                if (failOnTimeout) {
                    Diagnostics.logThreadDumps("Gave up waiting for recovery to finish.  THREAD DUMP:");
                    zkStateReader.getZkClient().printLayoutToStdOut();
                    fail("There are still nodes recoverying - waited for " + timeoutSeconds + " seconds");
                    // won't get here
                    return;
                }
            }
            cont = false;
        } else {
            Thread.sleep(1000);
        }
        cnt++;
    }
    log.info("Recoveries finished - collection: " + collection);
}
Also used : ClusterState(org.apache.solr.common.cloud.ClusterState) Slice(org.apache.solr.common.cloud.Slice) Map(java.util.Map) Replica(org.apache.solr.common.cloud.Replica)

Example 65 with Replica

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

the class AbstractFullDistribZkTestBase method logReplicaTypesReplicationInfo.

protected void logReplicaTypesReplicationInfo(String collectionName, ZkStateReader zkStateReader) throws KeeperException, InterruptedException, IOException {
    log.info("## Collecting extra Replica.Type information of the cluster");
    zkStateReader.updateLiveNodes();
    StringBuilder builder = new StringBuilder();
    zkStateReader.forceUpdateCollection(collectionName);
    DocCollection collection = zkStateReader.getClusterState().getCollection(collectionName);
    for (Slice s : collection.getSlices()) {
        Replica leader = s.getLeader();
        for (Replica r : s.getReplicas()) {
            if (!r.isActive(zkStateReader.getClusterState().getLiveNodes())) {
                builder.append(String.format(Locale.ROOT, "Replica %s not in liveNodes or is not active%s", r.getName(), System.lineSeparator()));
                continue;
            }
            if (r.equals(leader)) {
                builder.append(String.format(Locale.ROOT, "Replica %s is leader%s", r.getName(), System.lineSeparator()));
            }
            logReplicationDetails(r, builder);
        }
    }
    log.info("Summary of the cluster: " + builder.toString());
}
Also used : Slice(org.apache.solr.common.cloud.Slice) DocCollection(org.apache.solr.common.cloud.DocCollection) Replica(org.apache.solr.common.cloud.Replica)

Aggregations

Replica (org.apache.solr.common.cloud.Replica)232 Slice (org.apache.solr.common.cloud.Slice)140 DocCollection (org.apache.solr.common.cloud.DocCollection)86 ArrayList (java.util.ArrayList)81 ClusterState (org.apache.solr.common.cloud.ClusterState)67 HashMap (java.util.HashMap)60 SolrException (org.apache.solr.common.SolrException)53 ZkStateReader (org.apache.solr.common.cloud.ZkStateReader)50 Test (org.junit.Test)50 Map (java.util.Map)45 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)37 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)35 JettySolrRunner (org.apache.solr.client.solrj.embedded.JettySolrRunner)29 NamedList (org.apache.solr.common.util.NamedList)28 SolrQuery (org.apache.solr.client.solrj.SolrQuery)26 IOException (java.io.IOException)25 SolrInputDocument (org.apache.solr.common.SolrInputDocument)25 ZkCoreNodeProps (org.apache.solr.common.cloud.ZkCoreNodeProps)25 HashSet (java.util.HashSet)24 List (java.util.List)20