Java Code Examples for org.apache.solr.common.cloud.ZkStateReader#getLeaderRetry()

The following examples show how to use org.apache.solr.common.cloud.ZkStateReader#getLeaderRetry() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CollectionsAPIDistributedZkTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void waitForStable(int cnt, CollectionAdminRequest.Create[] createRequests) throws InterruptedException {
  for (int i = 0; i < cnt; i++) {
    String collectionName = "awhollynewcollection_" + i;
    final int j = i;
    waitForState("Expected to see collection " + collectionName, collectionName,
        (n, c) -> {
          CollectionAdminRequest.Create req = createRequests[j];
          return DocCollection.isFullyActive(n, c, req.getNumShards(), req.getReplicationFactor());
        });
    
    ZkStateReader zkStateReader = cluster.getSolrClient().getZkStateReader();
    // make sure we have leaders for each shard
    for (int z = 1; z < createRequests[j].getNumShards(); z++) {
      zkStateReader.getLeaderRetry(collectionName, "shard" + z, 10000);
    }      // make sure we again have leaders for each shard
  }
}
 
Example 2
Source File: AbstractFullDistribZkTestBase.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
protected void waitForReplicationFromReplicas(String collectionName, ZkStateReader zkStateReader, TimeOut timeout) throws KeeperException, InterruptedException, IOException {
  log.info("waitForReplicationFromReplicas: {}", collectionName);
  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 = zkStateReader.getLeaderRetry(collectionName, s.getName(), (int)timeout.timeLeft(TimeUnit.MILLISECONDS));
    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) {
          if (log.isInfoEnabled()) {
            log.info("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 {
                if (log.isInfoEnabled()) {
                  log.info("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) {
            if (log.isInfoEnabled()) {
              log.info("{} version is {} and leader's is {}, will wait for replication"
                  , pullReplica.getName(), replicaIndexVersion, leaderIndexVersion);
            }
          } else {
            if (log.isInfoEnabled()) {
              log.info("Leader replica's version ({}) is lower than pull replica({}): {} < {}"
                  , leader.getName(), pullReplica.getName(), leaderIndexVersion, replicaIndexVersion);
            }
          }
        }
        Thread.sleep(1000);
      }
    }
  }
}
 
Example 3
Source File: BasicDistributedZk2Test.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
@ShardsFixed(num = 4)
public void test() throws Exception {
  boolean testFinished = false;
  try {
    handle.clear();
    handle.put("timestamp", SKIPVAL);
    
    testNodeWithoutCollectionForwarding();
   
    indexr(id, 1, i1, 100, tlong, 100, t1,
        "now is the time for all good men", "foo_f", 1.414f, "foo_b", "true",
        "foo_d", 1.414d);
    
    commit();
    
    // make sure we are in a steady state...
    waitForRecoveriesToFinish(false);

    assertDocCounts(false);
    
    indexAbunchOfDocs();
    
    // check again 
    waitForRecoveriesToFinish(false);
    
    commit();
    
    assertDocCounts(VERBOSE);
    checkQueries();
    
    assertDocCounts(VERBOSE);
    
    query("q", "*:*", "sort", "n_tl1 desc");
    
    brindDownShardIndexSomeDocsAndRecover();
    
    query("q", "*:*", "sort", "n_tl1 desc");
    
    // test adding another replica to a shard - it should do a
    // recovery/replication to pick up the index from the leader
    addNewReplica();
    
    long docId = testUpdateAndDelete();
    
    // index a bad doc...
    expectThrows(SolrException.class, () -> indexr(t1, "a doc with no id"));
    
    // TODO: bring this to its own method?
    // try indexing to a leader that has no replicas up
    ZkStateReader zkStateReader = cloudClient.getZkStateReader();
    ZkNodeProps leaderProps = zkStateReader.getLeaderRetry(
        DEFAULT_COLLECTION, SHARD2);
    
    String nodeName = leaderProps.getStr(ZkStateReader.NODE_NAME_PROP);
    chaosMonkey.stopShardExcept(SHARD2, nodeName);
    
    SolrClient client = getClient(nodeName);
    
    index_specific(client, "id", docId + 1, t1, "what happens here?");
    
    // expire a session...
    CloudJettyRunner cloudJetty = shardToJetty.get(SHARD1).get(0);
    chaosMonkey.expireSession(cloudJetty.jetty);
    
    indexr("id", docId + 1, t1, "slip this doc in");
    
    waitForRecoveriesToFinish(false);
    
    checkShardConsistency(SHARD1);
    checkShardConsistency(SHARD2);
    
    testFinished = true;
  } finally {
    if (!testFinished) {
      printLayoutOnTearDown = true;
    }
  }
  
}
 
Example 4
Source File: BaseCdcrDistributedZkTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Creates and starts a given number of servers.
 */
protected List<String> startServers(int nServer) throws Exception {
  String temporaryCollection = "tmp_collection";

  for (int i = 1; i <= nServer; i++) {
    // give everyone there own solrhome
    File jettyDir = createTempDir("jetty").toFile();
    jettyDir.mkdirs();
    setupJettySolrHome(jettyDir);
    JettySolrRunner jetty = createJetty(jettyDir, null, "shard" + i);
    jetty.start();
    jettys.add(jetty);
  }

  try (SolrClient client = createCloudClient(temporaryCollection)) {
    assertEquals(0, CollectionAdminRequest
        .createCollection(temporaryCollection, "conf1", shardCount, 1)
        .setCreateNodeSet("")
        .process(client).getStatus());
    for (int i = 0; i < jettys.size(); i++) {
      assertTrue(CollectionAdminRequest
          .addReplicaToShard(temporaryCollection, "shard"+((i % shardCount) + 1))
          .setNode(jettys.get(i).getNodeName())
          .process(client).isSuccess());
    }
  }

  ZkStateReader zkStateReader = jettys.get(0).getCoreContainer().getZkController().getZkStateReader();

  // now wait till we see the leader for each shard
  for (int i = 1; i <= shardCount; i++) {
    zkStateReader.getLeaderRetry(temporaryCollection, "shard" + i, 15000);
  }

  // store the node names
  List<String> nodeNames = new ArrayList<>();
  for (Slice shard : zkStateReader.getClusterState().getCollection(temporaryCollection).getSlices()) {
    for (Replica replica : shard.getReplicas()) {
      nodeNames.add(replica.getNodeName());
    }
  }

  this.waitForRecoveriesToFinish(temporaryCollection,zkStateReader, true);
  // delete the temporary collection - we will create our own collections later
  this.deleteCollection(temporaryCollection);
  this.waitForCollectionToDisappear(temporaryCollection);
  System.clearProperty("collection");

  return nodeNames;
}