Search in sources :

Example 36 with ZkStateReader

use of org.apache.solr.common.cloud.ZkStateReader 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 37 with ZkStateReader

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

the class AbstractFullDistribZkTestBase method initCloud.

protected void initCloud() throws Exception {
    assert (cloudInit == false);
    cloudInit = true;
    cloudClient = createCloudClient(DEFAULT_COLLECTION);
    cloudClient.connect();
    ZkStateReader zkStateReader = cloudClient.getZkStateReader();
    chaosMonkey = new ChaosMonkey(zkServer, zkStateReader, DEFAULT_COLLECTION, shardToJetty, shardToLeaderJetty);
}
Also used : ZkStateReader(org.apache.solr.common.cloud.ZkStateReader)

Example 38 with ZkStateReader

use of org.apache.solr.common.cloud.ZkStateReader 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 39 with ZkStateReader

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

the class AbstractDistribZkTestBase method waitForNewLeader.

static void waitForNewLeader(CloudSolrClient cloudClient, String shardName, Replica oldLeader, TimeOut timeOut) throws Exception {
    log.info("Will wait for a node to become leader for {} secs", timeOut.timeLeft(SECONDS));
    ZkStateReader zkStateReader = cloudClient.getZkStateReader();
    zkStateReader.forceUpdateCollection(DEFAULT_COLLECTION);
    for (; ; ) {
        ClusterState clusterState = zkStateReader.getClusterState();
        DocCollection coll = clusterState.getCollection("collection1");
        Slice slice = coll.getSlice(shardName);
        if (slice.getLeader() != null && !slice.getLeader().equals(oldLeader) && slice.getLeader().getState() == Replica.State.ACTIVE) {
            log.info("Old leader {}, new leader {}. New leader got elected in {} ms", oldLeader, slice.getLeader(), timeOut.timeElapsed(MILLISECONDS));
            break;
        }
        if (timeOut.hasTimedOut()) {
            Diagnostics.logThreadDumps("Could not find new leader in specified timeout");
            zkStateReader.getZkClient().printLayoutToStdOut();
            fail("Could not find new leader even after waiting for " + timeOut.timeElapsed(MILLISECONDS) + "ms");
        }
        Thread.sleep(100);
    }
}
Also used : ZkStateReader(org.apache.solr.common.cloud.ZkStateReader) ClusterState(org.apache.solr.common.cloud.ClusterState) Slice(org.apache.solr.common.cloud.Slice) DocCollection(org.apache.solr.common.cloud.DocCollection)

Example 40 with ZkStateReader

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

the class AbstractFullDistribZkTestBase method waitForRecoveriesToFinish.

protected void waitForRecoveriesToFinish(boolean verbose, int timeoutSeconds) throws Exception {
    ZkStateReader zkStateReader = cloudClient.getZkStateReader();
    super.waitForRecoveriesToFinish(DEFAULT_COLLECTION, zkStateReader, verbose, true, timeoutSeconds);
}
Also used : ZkStateReader(org.apache.solr.common.cloud.ZkStateReader)

Aggregations

ZkStateReader (org.apache.solr.common.cloud.ZkStateReader)133 ClusterState (org.apache.solr.common.cloud.ClusterState)58 Replica (org.apache.solr.common.cloud.Replica)48 Slice (org.apache.solr.common.cloud.Slice)48 HashMap (java.util.HashMap)34 SolrZkClient (org.apache.solr.common.cloud.SolrZkClient)33 ArrayList (java.util.ArrayList)32 DocCollection (org.apache.solr.common.cloud.DocCollection)31 Test (org.junit.Test)26 SolrException (org.apache.solr.common.SolrException)25 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)22 ZkNodeProps (org.apache.solr.common.cloud.ZkNodeProps)20 IOException (java.io.IOException)19 Map (java.util.Map)19 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)18 KeeperException (org.apache.zookeeper.KeeperException)16 SolrQuery (org.apache.solr.client.solrj.SolrQuery)15 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)15 SolrServerException (org.apache.solr.client.solrj.SolrServerException)13 JettySolrRunner (org.apache.solr.client.solrj.embedded.JettySolrRunner)12