use of org.apache.solr.common.cloud.Slice in project lucene-solr by apache.
the class SliceMutator method setShardLeader.
public ZkWriteCommand setShardLeader(ClusterState clusterState, ZkNodeProps message) {
StringBuilder sb = new StringBuilder();
String baseUrl = message.getStr(ZkStateReader.BASE_URL_PROP);
String coreName = message.getStr(ZkStateReader.CORE_NAME_PROP);
sb.append(baseUrl);
if (baseUrl != null && !baseUrl.endsWith("/"))
sb.append("/");
sb.append(coreName == null ? "" : coreName);
if (!(sb.substring(sb.length() - 1).equals("/")))
sb.append("/");
String leaderUrl = sb.length() > 0 ? sb.toString() : null;
String collectionName = message.getStr(ZkStateReader.COLLECTION_PROP);
String sliceName = message.getStr(ZkStateReader.SHARD_ID_PROP);
DocCollection coll = clusterState.getCollectionOrNull(collectionName);
if (coll == null) {
log.error("Could not mark shard leader for non existing collection:" + collectionName);
return ZkStateWriter.NO_OP;
}
Map<String, Slice> slices = coll.getSlicesMap();
Slice slice = slices.get(sliceName);
Replica oldLeader = slice.getLeader();
final Map<String, Replica> newReplicas = new LinkedHashMap<>();
for (Replica replica : slice.getReplicas()) {
// TODO: this should only be calculated once and cached somewhere?
String coreURL = ZkCoreNodeProps.getCoreUrl(replica.getStr(ZkStateReader.BASE_URL_PROP), replica.getStr(ZkStateReader.CORE_NAME_PROP));
if (replica == oldLeader && !coreURL.equals(leaderUrl)) {
replica = new ReplicaMutator(zkStateReader).unsetLeader(replica);
} else if (coreURL.equals(leaderUrl)) {
replica = new ReplicaMutator(zkStateReader).setLeader(replica);
}
newReplicas.put(replica.getName(), replica);
}
Map<String, Object> newSliceProps = slice.shallowCopy();
newSliceProps.put(Slice.REPLICAS, newReplicas);
slice = new Slice(slice.getName(), newReplicas, slice.getProperties());
return new ZkWriteCommand(collectionName, CollectionMutator.updateSlice(collectionName, coll, slice));
}
use of org.apache.solr.common.cloud.Slice in project lucene-solr by apache.
the class SliceMutator method removeReplica.
public ZkWriteCommand removeReplica(ClusterState clusterState, ZkNodeProps message) {
final String cnn = message.getStr(ZkStateReader.CORE_NODE_NAME_PROP);
final String collection = message.getStr(ZkStateReader.COLLECTION_PROP);
final String baseUrl = message.getStr(ZkStateReader.BASE_URL_PROP);
if (!checkCollectionKeyExistence(message))
return ZkStateWriter.NO_OP;
DocCollection coll = clusterState.getCollectionOrNull(collection);
if (coll == null) {
// make sure we delete the zk nodes for this collection just to be safe
return new ZkWriteCommand(collection, null);
}
Map<String, Slice> newSlices = new LinkedHashMap<>();
for (Slice slice : coll.getSlices()) {
Replica replica = slice.getReplica(cnn);
if (replica != null && (baseUrl == null || baseUrl.equals(replica.getBaseUrl()))) {
Map<String, Replica> newReplicas = slice.getReplicasCopy();
newReplicas.remove(cnn);
slice = new Slice(slice.getName(), newReplicas, slice.getProperties());
}
newSlices.put(slice.getName(), slice);
}
return new ZkWriteCommand(collection, coll.copyWithSlices(newSlices));
}
use of org.apache.solr.common.cloud.Slice in project lucene-solr by apache.
the class ClusterStateMutator method getAssignedCoreNodeName.
public static String getAssignedCoreNodeName(DocCollection collection, String forNodeName, String forCoreName) {
Collection<Slice> slices = collection != null ? collection.getSlices() : null;
if (slices != null) {
for (Slice slice : slices) {
for (Replica replica : slice.getReplicas()) {
String nodeName = replica.getStr(ZkStateReader.NODE_NAME_PROP);
String core = replica.getStr(ZkStateReader.CORE_NAME_PROP);
if (nodeName.equals(forNodeName) && core.equals(forCoreName)) {
return replica.getName();
}
}
}
}
return null;
}
use of org.apache.solr.common.cloud.Slice in project lucene-solr by apache.
the class ReplaceNodeCmd method getReplicasOfNode.
static List<ZkNodeProps> getReplicasOfNode(String source, ClusterState state) {
List<ZkNodeProps> sourceReplicas = new ArrayList<>();
for (Map.Entry<String, DocCollection> e : state.getCollectionsMap().entrySet()) {
for (Slice slice : e.getValue().getSlices()) {
for (Replica replica : slice.getReplicas()) {
if (source.equals(replica.getNodeName())) {
ZkNodeProps props = new ZkNodeProps(COLLECTION_PROP, e.getKey(), SHARD_ID_PROP, slice.getName(), ZkStateReader.CORE_NAME_PROP, replica.getCoreName(), ZkStateReader.REPLICA_PROP, replica.getName(), ZkStateReader.REPLICA_TYPE, replica.getType().name(), CoreAdminParams.NODE, source);
sourceReplicas.add(props);
}
}
}
}
return sourceReplicas;
}
use of org.apache.solr.common.cloud.Slice in project lucene-solr by apache.
the class OverseerCollectionMessageHandler method waitForNewShard.
void waitForNewShard(String collectionName, String sliceName) throws KeeperException, InterruptedException {
log.debug("Waiting for slice {} of collection {} to be available", sliceName, collectionName);
RTimer timer = new RTimer();
int retryCount = 320;
while (retryCount-- > 0) {
DocCollection collection = zkStateReader.getClusterState().getCollection(collectionName);
if (collection == null) {
throw new SolrException(ErrorCode.SERVER_ERROR, "Unable to find collection: " + collectionName + " in clusterstate");
}
Slice slice = collection.getSlice(sliceName);
if (slice != null) {
log.debug("Waited for {}ms for slice {} of collection {} to be available", timer.getTime(), sliceName, collectionName);
return;
}
Thread.sleep(1000);
}
throw new SolrException(ErrorCode.SERVER_ERROR, "Could not find new slice " + sliceName + " in collection " + collectionName + " even after waiting for " + timer.getTime() + "ms");
}
Aggregations