Java Code Examples for org.apache.solr.common.cloud.DocCollection#getReplica()

The following examples show how to use org.apache.solr.common.cloud.DocCollection#getReplica() . 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: ReplicaPropertiesBase.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public static void verifyPropertyNotPresent(CloudSolrClient client, String collectionName, String replicaName,
                              String property)
    throws KeeperException, InterruptedException {
  ClusterState clusterState = null;
  Replica replica = null;
  for (int idx = 0; idx < 300; ++idx) {
    clusterState = client.getZkStateReader().getClusterState();
    final DocCollection docCollection = clusterState.getCollectionOrNull(collectionName);
    replica = (docCollection == null) ? null : docCollection.getReplica(replicaName);
    if (replica == null) {
      fail("Could not find collection/replica pair! " + collectionName + "/" + replicaName);
    }
    if (StringUtils.isBlank(replica.getProperty(property))) return;
    Thread.sleep(100);
  }
  fail("Property " + property + " not set correctly for collection/replica pair: " +
      collectionName + "/" + replicaName + ". Replica props: " + replica.getProperties().toString() +
      ". Cluster state is " + clusterState.toString());

}
 
Example 2
Source File: ReplicaPropertiesBase.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public static void verifyPropertyVal(CloudSolrClient client, String collectionName,
                       String replicaName, String property, String val)
    throws InterruptedException, KeeperException {
  Replica replica = null;
  ClusterState clusterState = null;

  for (int idx = 0; idx < 300; ++idx) { // Keep trying while Overseer writes the ZK state for up to 30 seconds.
    clusterState = client.getZkStateReader().getClusterState();
    final DocCollection docCollection = clusterState.getCollectionOrNull(collectionName);
    replica = (docCollection == null) ? null : docCollection.getReplica(replicaName);
    if (replica == null) {
      fail("Could not find collection/replica pair! " + collectionName + "/" + replicaName);
    }
    if (StringUtils.equals(val, replica.getProperty(property))) return;
    Thread.sleep(100);
  }

  fail("Property '" + property + "' with value " + replica.getProperty(property) +
      " not set correctly for collection/replica pair: " + collectionName + "/" + replicaName + " property map is " +
      replica.getProperties().toString() + ".");

}
 
Example 3
Source File: TestCollectionAPI.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private Map<String, String> getProps(CloudSolrClient client, String collectionName, String replicaName, String... props)
    throws KeeperException, InterruptedException {

  client.getZkStateReader().forceUpdateCollection(collectionName);
  ClusterState clusterState = client.getZkStateReader().getClusterState();
  final DocCollection docCollection = clusterState.getCollectionOrNull(collectionName);
  if (docCollection == null || docCollection.getReplica(replicaName) == null) {
    fail("Could not find collection/replica pair! " + collectionName + "/" + replicaName);
  }
  Replica replica = docCollection.getReplica(replicaName);
  Map<String, String> propMap = new HashMap<>();
  for (String prop : props) {
    propMap.put(prop, replica.getProperty(prop));
  }
  return propMap;
}
 
Example 4
Source File: TestTlogReplica.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private List<SolrCore> getSolrCore(boolean isLeader) {
  List<SolrCore> rs = new ArrayList<>();

  CloudSolrClient cloudClient = cluster.getSolrClient();
  DocCollection docCollection = cloudClient.getZkStateReader().getClusterState().getCollection(collectionName);

  for (JettySolrRunner solrRunner : cluster.getJettySolrRunners()) {
    if (solrRunner.getCoreContainer() == null) continue;
    for (SolrCore solrCore : solrRunner.getCoreContainer().getCores()) {
      CloudDescriptor cloudDescriptor = solrCore.getCoreDescriptor().getCloudDescriptor();
      Slice slice = docCollection.getSlice(cloudDescriptor.getShardId());
      Replica replica = docCollection.getReplica(cloudDescriptor.getCoreNodeName());
      if (slice.getLeader().equals(replica) && isLeader) {
        rs.add(solrCore);
      } else if (!slice.getLeader().equals(replica) && !isLeader) {
        rs.add(solrCore);
      }
    }
  }
  return rs;
}
 
Example 5
Source File: TestTlogReplica.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private List<JettySolrRunner> getSolrRunner(boolean isLeader) {
  List<JettySolrRunner> rs = new ArrayList<>();
  CloudSolrClient cloudClient = cluster.getSolrClient();
  DocCollection docCollection = cloudClient.getZkStateReader().getClusterState().getCollection(collectionName);
  for (JettySolrRunner solrRunner : cluster.getJettySolrRunners()) {
    if (solrRunner.getCoreContainer() == null) continue;
    for (SolrCore solrCore : solrRunner.getCoreContainer().getCores()) {
      CloudDescriptor cloudDescriptor = solrCore.getCoreDescriptor().getCloudDescriptor();
      Slice slice = docCollection.getSlice(cloudDescriptor.getShardId());
      Replica replica = docCollection.getReplica(cloudDescriptor.getCoreNodeName());
      if (slice.getLeader() == replica && isLeader) {
        rs.add(solrRunner);
      } else if (slice.getLeader() != replica && !isLeader) {
        rs.add(solrRunner);
      }
    }
  }
  return rs;
}
 
Example 6
Source File: Assign.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static String assignCoreNodeName(DistribStateManager stateManager, DocCollection collection) {
  // for backward compatibility;
  int defaultValue = defaultCounterValue(collection, false);
  String coreNodeName = "core_node" + incAndGetId(stateManager, collection.getName(), defaultValue);
  while (collection.getReplica(coreNodeName) != null) {
    // there is wee chance that, the new coreNodeName id not totally unique,
    // but this will be guaranteed unique for new collections
    coreNodeName = "core_node" + incAndGetId(stateManager, collection.getName(), defaultValue);
  }
  return coreNodeName;
}
 
Example 7
Source File: ReplicaMutator.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public ZkWriteCommand deleteReplicaProperty(ClusterState clusterState, ZkNodeProps message) {
  if (checkKeyExistence(message, ZkStateReader.COLLECTION_PROP) == false ||
      checkKeyExistence(message, ZkStateReader.SHARD_ID_PROP) == false ||
      checkKeyExistence(message, ZkStateReader.REPLICA_PROP) == false ||
      checkKeyExistence(message, ZkStateReader.PROPERTY_PROP) == false) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
        "Overseer DELETEREPLICAPROP requires " +
            ZkStateReader.COLLECTION_PROP + " and " + ZkStateReader.SHARD_ID_PROP + " and " +
            ZkStateReader.REPLICA_PROP + " and " + ZkStateReader.PROPERTY_PROP + " no action taken.");
  }
  String collectionName = message.getStr(ZkStateReader.COLLECTION_PROP);
  String sliceName = message.getStr(ZkStateReader.SHARD_ID_PROP);
  String replicaName = message.getStr(ZkStateReader.REPLICA_PROP);
  String property = message.getStr(ZkStateReader.PROPERTY_PROP).toLowerCase(Locale.ROOT);
  if (StringUtils.startsWith(property, OverseerCollectionMessageHandler.COLL_PROP_PREFIX) == false) {
    property = OverseerCollectionMessageHandler.COLL_PROP_PREFIX + property;
  }

  DocCollection collection = clusterState.getCollection(collectionName);
  Replica replica = collection.getReplica(replicaName);

  if (replica == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Could not find collection/slice/replica " +
        collectionName + "/" + sliceName + "/" + replicaName + " no action taken.");
  }

  log.info("Deleting property {} for collection: {} slice: {} replica: {}", property, collectionName, sliceName, replicaName);
  log.debug("Full message: {}", message);
  String curProp = replica.getStr(property);
  if (curProp == null) return ZkStateWriter.NO_OP; // not there anyway, nothing to do.

  Slice slice = collection.getSlice(sliceName);
  DocCollection newCollection = SliceMutator.updateReplica(collection,
      slice, replicaName, unsetProperty(replica, property));
  return new ZkWriteCommand(collectionName, newCollection);
}
 
Example 8
Source File: SolrLogLayout.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked"})
private Map<String, Object> getReplicaProps(ZkController zkController, SolrCore core) {
  final String collectionName = core.getCoreDescriptor().getCloudDescriptor().getCollectionName();
  DocCollection collection = zkController.getClusterState().getCollectionOrNull(collectionName);
  Replica replica = collection.getReplica(zkController.getCoreNodeName(core.getCoreDescriptor()));
  if (replica != null) {
    return replica.getProperties();
  }
  return Collections.EMPTY_MAP;
}
 
Example 9
Source File: ReplicaMutator.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public ZkWriteCommand addReplicaProperty(ClusterState clusterState, ZkNodeProps message) {
  if (!checkKeyExistence(message, ZkStateReader.COLLECTION_PROP) ||
      !checkKeyExistence(message, ZkStateReader.SHARD_ID_PROP) ||
      !checkKeyExistence(message, ZkStateReader.REPLICA_PROP) ||
      !checkKeyExistence(message, ZkStateReader.PROPERTY_PROP) ||
      !checkKeyExistence(message, ZkStateReader.PROPERTY_VALUE_PROP)) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
        "Overseer ADDREPLICAPROP requires " +
            ZkStateReader.COLLECTION_PROP + " and " + ZkStateReader.SHARD_ID_PROP + " and " +
            ZkStateReader.REPLICA_PROP + " and " + ZkStateReader.PROPERTY_PROP + " and " +
            ZkStateReader.PROPERTY_VALUE_PROP + " no action taken.");
  }

  String collectionName = message.getStr(ZkStateReader.COLLECTION_PROP);
  String sliceName = message.getStr(ZkStateReader.SHARD_ID_PROP);
  String replicaName = message.getStr(ZkStateReader.REPLICA_PROP);
  String property = message.getStr(ZkStateReader.PROPERTY_PROP).toLowerCase(Locale.ROOT);
  if (StringUtils.startsWith(property, OverseerCollectionMessageHandler.COLL_PROP_PREFIX) == false) {
    property = OverseerCollectionMessageHandler.COLL_PROP_PREFIX + property;
  }
  property = property.toLowerCase(Locale.ROOT);
  String propVal = message.getStr(ZkStateReader.PROPERTY_VALUE_PROP);
  String shardUnique = message.getStr(OverseerCollectionMessageHandler.SHARD_UNIQUE);

  boolean isUnique = false;

  if (SliceMutator.SLICE_UNIQUE_BOOLEAN_PROPERTIES.contains(property)) {
    if (StringUtils.isNotBlank(shardUnique) && Boolean.parseBoolean(shardUnique) == false) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Overseer ADDREPLICAPROP for " +
          property + " cannot have " + OverseerCollectionMessageHandler.SHARD_UNIQUE + " set to anything other than" +
          "'true'. No action taken");
    }
    isUnique = true;
  } else {
    isUnique = Boolean.parseBoolean(shardUnique);
  }

  DocCollection collection = clusterState.getCollection(collectionName);
  Replica replica = collection.getReplica(replicaName);

  if (replica == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Could not find collection/slice/replica " +
        collectionName + "/" + sliceName + "/" + replicaName + " no action taken.");
  }
  log.info("Setting property {} with value {} for collection {}", property, propVal, collectionName);
  log.debug("Full message: {}", message);
  if (StringUtils.equalsIgnoreCase(replica.getStr(property), propVal))
    return ZkStateWriter.NO_OP; // already the value we're going to set

  // OK, there's no way we won't change the cluster state now
  Map<String, Replica> replicas = collection.getSlice(sliceName).getReplicasCopy();
  if (isUnique == false) {
    replicas.get(replicaName).getProperties().put(property, propVal);
  } else { // Set prop for this replica, but remove it for all others.
    for (Replica rep : replicas.values()) {
      if (rep.getName().equalsIgnoreCase(replicaName)) {
        rep.getProperties().put(property, propVal);
      } else {
        rep.getProperties().remove(property);
      }
    }
  }
  Slice newSlice = new Slice(sliceName, replicas, collection.getSlice(sliceName).shallowCopy(),collectionName);
  DocCollection newCollection = CollectionMutator.updateSlice(collectionName, collection,
      newSlice);
  return new ZkWriteCommand(collectionName, newCollection);
}
 
Example 10
Source File: ShardLeaderElectionContext.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private Replica getReplica(ClusterState clusterState, String collectionName, String replicaName) {
  if (clusterState == null) return null;
  final DocCollection docCollection = clusterState.getCollectionOrNull(collectionName);
  if (docCollection == null) return null;
  return docCollection.getReplica(replicaName);
}