Java Code Examples for org.apache.solr.common.cloud.Replica#getBool()

The following examples show how to use org.apache.solr.common.cloud.Replica#getBool() . 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: RebalanceLeaders.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void checkLeaderStatus() throws InterruptedException, KeeperException {
  for (int idx = 0; pendingOps.size() > 0 && idx < 600; ++idx) {
    ClusterState clusterState = coreContainer.getZkController().getClusterState();
    Set<String> liveNodes = clusterState.getLiveNodes();
    DocCollection dc = clusterState.getCollection(collectionName);
    for (Slice slice : dc.getSlices()) {
      for (Replica replica : slice.getReplicas()) {
        if (replica.isActive(liveNodes) && replica.getBool(SliceMutator.PREFERRED_LEADER_PROP, false)) {
          if (replica.getBool(LEADER_PROP, false)) {
            if (pendingOps.containsKey(slice.getName())) {
              // Record for return that the leader changed successfully
              pendingOps.remove(slice.getName());
              addToSuccesses(slice, replica);
              break;
            }
          }
        }
      }
    }
    TimeUnit.MILLISECONDS.sleep(100);
    coreContainer.getZkController().getZkStateReader().forciblyRefreshAllClusterStateSlow();
  }
  addAnyFailures();
}
 
Example 2
Source File: TestRebalanceLeaders.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private boolean checkPreferredsAreLeaders(boolean doAsserts) throws KeeperException, InterruptedException {
  forceUpdateCollectionStatus();
  DocCollection docCollection = cluster.getSolrClient().getZkStateReader().getClusterState().getCollection(COLLECTION_NAME);
  for (Slice slice : docCollection.getSlices()) {
    for (Replica rep : slice.getReplicas()) {
      if (rep.getBool("property.preferredleader", false)) {
        boolean isLeader = rep.getBool("leader", false);
        if (doAsserts) {
          assertTrue("PreferredLeader should be the leader: ", isLeader);
        } else if (isLeader == false) {
          return false;
        }
      }
    }
  }
  return true;
}
 
Example 3
Source File: TestRebalanceLeaders.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void verifyPropCorrectlyDistributed(String prop) throws KeeperException, InterruptedException {

    TimeOut timeout = new TimeOut(timeoutMs, TimeUnit.MILLISECONDS, TimeSource.NANO_TIME);

    String propLC = prop.toLowerCase(Locale.ROOT);
    DocCollection docCollection = null;
    while (timeout.hasTimedOut() == false) {
      forceUpdateCollectionStatus();
      docCollection = cluster.getSolrClient().getZkStateReader().getClusterState().getCollection(COLLECTION_NAME);
      int maxPropCount = Integer.MAX_VALUE;
      int minPropCount = Integer.MIN_VALUE;
      for (Slice slice : docCollection.getSlices()) {
        int repCount = 0;
        for (Replica rep : slice.getReplicas()) {
          if (rep.getBool("property." + propLC, false)) {
            repCount++;
          }
        }
        maxPropCount = Math.max(maxPropCount, repCount);
        minPropCount = Math.min(minPropCount, repCount);
      }
      if (Math.abs(maxPropCount - minPropCount) < 2) return;
    }
    log.error("Property {} is not distributed evenly. {}", prop, docCollection);
    fail("Property is not distributed evenly " + prop);
  }
 
Example 4
Source File: TestRebalanceLeaders.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void verifyPropDistributedAsExpected(Map<String, String> expectedShardReplicaMap, String prop) throws InterruptedException, KeeperException {
  // Make sure that the shard unique are where you expect.
  TimeOut timeout = new TimeOut(timeoutMs, TimeUnit.MILLISECONDS, TimeSource.NANO_TIME);

  String propLC = prop.toLowerCase(Locale.ROOT);
  boolean failure = false;
  DocCollection docCollection = null;
  while (timeout.hasTimedOut() == false) {
    forceUpdateCollectionStatus();
    docCollection = cluster.getSolrClient().getZkStateReader().getClusterState().getCollection(COLLECTION_NAME);
    failure = false;
    for (Map.Entry<String, String> ent : expectedShardReplicaMap.entrySet()) {
      Replica rep = docCollection.getSlice(ent.getKey()).getReplica(ent.getValue());
      if (rep.getBool("property." + propLC, false) == false) {
        failure = true;
      }
    }
    if (failure == false) {
      return;
    }
    TimeUnit.MILLISECONDS.sleep(100);
  }

  fail(prop + " properties are not on the expected replicas: " + docCollection.toString()
      + System.lineSeparator() + "Expected " + expectedShardReplicaMap.toString());
}
 
Example 5
Source File: TestRebalanceLeaders.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private boolean checkdUniquePropPerShard(Map<String, String> uniques, String prop) throws KeeperException, InterruptedException {
  forceUpdateCollectionStatus();
  DocCollection docCollection = cluster.getSolrClient().getZkStateReader().getClusterState().getCollection(COLLECTION_NAME);

  for (Slice slice : docCollection.getSlices()) {
    int propfCount = 0;
    for (Replica rep : slice.getReplicas()) {
      if (rep.getBool("property." + prop.toLowerCase(Locale.ROOT), false)) {
        propfCount++;
        uniques.put(slice.getName(), rep.getName());
      }
    }
    if (1 != propfCount) {
      return false;
    }
  }
  return true;
}
 
Example 6
Source File: SystemLogListenerTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** 
 * Helper method for picking a node that can safely be stoped
 * @see <a href="https://issues.apache.org/jira/browse/SOLR-13050">SOLR-13050</a>
 */
private JettySolrRunner pickNodeToStop() throws Exception {
  // first get the nodeName of the overser.
  // stopping the overseer is not something we want to hassle with in this test
  final String overseerNodeName = (String) cluster.getSolrClient().request
    (CollectionAdminRequest.getOverseerStatus()).get("leader");

  // now find a node that is *NOT* the overseer or the leader of a .system collection shard
  for (Replica r :  getCollectionState(CollectionAdminParams.SYSTEM_COLL).getReplicas()) {
    if ( ! (r.getBool("leader", false) || r.getNodeName().equals(overseerNodeName) ) ) {
      return cluster.getReplicaJetty(r);
    }
  }
  fail("Couldn't find non-leader, non-overseer, replica of .system collection to kill");
  return null;
}
 
Example 7
Source File: ReplicaInfo.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public ReplicaInfo(String coll, String shard, Replica r, Map<String, Object> vals) {
  this.name = r.getName();
  this.core = r.getCoreName();
  this.collection = coll;
  this.shard = shard;
  this.type = r.getType();
  this.node = r.getNodeName();
  boolean maybeLeader = r.getBool(LEADER_PROP, false);
  if (vals != null) {
    this.variables.putAll(vals);
    maybeLeader = "true".equals(String.valueOf(vals.getOrDefault(LEADER_PROP, maybeLeader)));
  }
  this.isLeader = maybeLeader;
  validate();
}
 
Example 8
Source File: TestReplicaProperties.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void verifyLeaderAssignment(CloudSolrClient client, String collectionName)
    throws InterruptedException, KeeperException {
  String lastFailMsg = "";
  for (int idx = 0; idx < 300; ++idx) { // Keep trying while Overseer writes the ZK state for up to 30 seconds.
    lastFailMsg = "";
    ClusterState clusterState = client.getZkStateReader().getClusterState();
    for (Slice slice : clusterState.getCollection(collectionName).getSlices()) {
      Boolean foundLeader = false;
      Boolean foundPreferred = false;
      for (Replica replica : slice.getReplicas()) {
        Boolean isLeader = replica.getBool("leader", false);
        Boolean isPreferred = replica.getBool("property.preferredleader", false);
        if (isLeader != isPreferred) {
          lastFailMsg = "Replica should NOT have preferredLeader != leader. Preferred: " + isPreferred.toString() +
              " leader is " + isLeader.toString();
        }
        if (foundLeader && isLeader) {
          lastFailMsg = "There should only be a single leader in _any_ shard! Replica " + replica.getName() +
              " is the second leader in slice " + slice.getName();
        }
        if (foundPreferred && isPreferred) {
          lastFailMsg = "There should only be a single preferredLeader in _any_ shard! Replica " + replica.getName() +
              " is the second preferredLeader in slice " + slice.getName();
        }
        foundLeader = foundLeader ? foundLeader : isLeader;
        foundPreferred = foundPreferred ? foundPreferred : isPreferred;
      }
    }
    if (lastFailMsg.length() == 0) return;
    Thread.sleep(100);
  }
  fail(lastFailMsg);
}
 
Example 9
Source File: RebalanceLeaders.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private void ensurePreferredIsLeader(Slice slice) throws KeeperException, InterruptedException {
  for (Replica replica : slice.getReplicas()) {
    // Tell the replica to become the leader if we're the preferred leader AND active AND not the leader already
    if (replica.getBool(SliceMutator.PREFERRED_LEADER_PROP, false) == false) {
      continue;
    }
    // OK, we are the preferred leader, are we the actual leader?
    if (replica.getBool(LEADER_PROP, false)) {
      //We're a preferred leader, but we're _also_ the leader, don't need to do anything.
      addAlreadyLeaderToResults(slice, replica);
      return; // already the leader, do nothing.
    }
    ZkStateReader zkStateReader = coreContainer.getZkController().getZkStateReader();
    // We're the preferred leader, but someone else is leader. Only become leader if we're active.
    if (replica.isActive(zkStateReader.getClusterState().getLiveNodes()) == false) {
      addInactiveToResults(slice, replica);
      return; // Don't try to become the leader if we're not active!
    }

    List<String> electionNodes = OverseerTaskProcessor.getSortedElectionNodes(zkStateReader.getZkClient(),
        ZkStateReader.getShardLeadersElectPath(collectionName, slice.getName()));

    if (electionQueueInBadState(electionNodes, slice, replica)) {
      return;
    }

    // Replica is the preferred leader but not the actual leader, do something about that.
    // "Something" is
    // 1> if the preferred leader isn't first in line, tell it to re-queue itself.
    // 2> tell the actual leader to re-queue itself.

    // Ok, the sorting for election nodes is a bit strange. If the sequence numbers are the same, then the whole
    // string is used, but that sorts nodes with the same sequence number by their session IDs from ZK.
    // While this is determinate, it's not quite what we need, so re-queue nodes that aren't us and are
    // watching the leader node..


    String firstWatcher = electionNodes.get(1);

    if (LeaderElector.getNodeName(firstWatcher).equals(replica.getName()) == false) {
      makeReplicaFirstWatcher(slice, replica);
    }

    // This replica should be the leader at the end of the day, so let's record that information to check at the end
    pendingOps.put(slice.getName(), replica.getName());
    String leaderElectionNode = electionNodes.get(0);
    String coreName = slice.getReplica(LeaderElector.getNodeName(leaderElectionNode)).getStr(CORE_NAME_PROP);
    rejoinElectionQueue(slice, leaderElectionNode, coreName, false);
    waitForNodeChange(slice, leaderElectionNode);

    return; // Done with this slice, skip the rest of the replicas.
  }
}