use of org.apache.solr.common.cloud.ZkStateReader in project lucene-solr by apache.
the class RebalanceLeaders method makeReplicaFirstWatcher.
// Put the replica in at the head of the queue and send all nodes with the same sequence number to the back of the list
void makeReplicaFirstWatcher(String collectionName, Slice slice, Replica replica) throws KeeperException, InterruptedException {
ZkStateReader zkStateReader = coreContainer.getZkController().getZkStateReader();
List<String> electionNodes = OverseerTaskProcessor.getSortedElectionNodes(zkStateReader.getZkClient(), ZkStateReader.getShardLeadersElectPath(collectionName, slice.getName()));
// First, queue up the preferred leader at the head of the queue.
int newSeq = -1;
for (String electionNode : electionNodes) {
if (LeaderElector.getNodeName(electionNode).equals(replica.getName())) {
String coreName = slice.getReplica(LeaderElector.getNodeName(electionNode)).getStr(CORE_NAME_PROP);
rejoinElection(collectionName, slice, electionNode, coreName, true);
newSeq = waitForNodeChange(collectionName, slice, electionNode);
break;
}
}
if (newSeq == -1) {
// let's not continue if we didn't get what we expect. Possibly we're offline etc..
return;
}
// Now find other nodes that have the same sequence number as this node and re-queue them at the end of the queue.
electionNodes = OverseerTaskProcessor.getSortedElectionNodes(zkStateReader.getZkClient(), ZkStateReader.getShardLeadersElectPath(collectionName, slice.getName()));
for (String thisNode : electionNodes) {
if (LeaderElector.getSeq(thisNode) > newSeq) {
break;
}
if (LeaderElector.getNodeName(thisNode).equals(replica.getName())) {
continue;
}
if (LeaderElector.getSeq(thisNode) == newSeq) {
String coreName = slice.getReplica(LeaderElector.getNodeName(thisNode)).getStr(CORE_NAME_PROP);
rejoinElection(collectionName, slice, thisNode, coreName, false);
waitForNodeChange(collectionName, slice, thisNode);
}
}
}
use of org.apache.solr.common.cloud.ZkStateReader in project lucene-solr by apache.
the class RebalanceLeaders method waitForNodeChange.
int waitForNodeChange(String collectionName, Slice slice, String electionNode) throws InterruptedException, KeeperException {
String nodeName = LeaderElector.getNodeName(electionNode);
int oldSeq = LeaderElector.getSeq(electionNode);
for (int idx = 0; idx < 600; ++idx) {
ZkStateReader zkStateReader = coreContainer.getZkController().getZkStateReader();
List<String> electionNodes = OverseerTaskProcessor.getSortedElectionNodes(zkStateReader.getZkClient(), ZkStateReader.getShardLeadersElectPath(collectionName, slice.getName()));
for (String testNode : electionNodes) {
if (LeaderElector.getNodeName(testNode).equals(nodeName) && oldSeq != LeaderElector.getSeq(testNode)) {
return LeaderElector.getSeq(testNode);
}
}
Thread.sleep(100);
}
return -1;
}
use of org.apache.solr.common.cloud.ZkStateReader in project lucene-solr by apache.
the class BaseCdcrDistributedZkTest method updateMappingsFromZk.
/**
* Updates the mappings between the jetty's instances and the zookeeper cluster state.
*/
protected void updateMappingsFromZk(String collection) throws Exception {
List<CloudJettyRunner> cloudJettys = new ArrayList<>();
Map<String, List<CloudJettyRunner>> shardToJetty = new HashMap<>();
Map<String, CloudJettyRunner> shardToLeaderJetty = new HashMap<>();
CloudSolrClient cloudClient = this.createCloudClient(null);
try {
cloudClient.connect();
ZkStateReader zkStateReader = cloudClient.getZkStateReader();
ClusterState clusterState = zkStateReader.getClusterState();
DocCollection coll = clusterState.getCollection(collection);
for (JettySolrRunner jetty : jettys) {
int port = jetty.getLocalPort();
if (port == -1) {
throw new RuntimeException("Cannot find the port for jetty");
}
nextJetty: for (Slice shard : coll.getSlices()) {
Set<Map.Entry<String, Replica>> entries = shard.getReplicasMap().entrySet();
for (Map.Entry<String, Replica> entry : entries) {
Replica replica = entry.getValue();
if (replica.getStr(ZkStateReader.BASE_URL_PROP).contains(":" + port)) {
if (!shardToJetty.containsKey(shard.getName())) {
shardToJetty.put(shard.getName(), new ArrayList<CloudJettyRunner>());
}
boolean isLeader = shard.getLeader() == replica;
CloudJettyRunner cjr = new CloudJettyRunner(jetty, replica, collection, shard.getName(), entry.getKey());
shardToJetty.get(shard.getName()).add(cjr);
if (isLeader) {
shardToLeaderJetty.put(shard.getName(), cjr);
}
cloudJettys.add(cjr);
break nextJetty;
}
}
}
}
List<CloudJettyRunner> oldRunners = this.cloudJettys.putIfAbsent(collection, cloudJettys);
if (oldRunners != null) {
// must close resources for the old entries
for (CloudJettyRunner oldRunner : oldRunners) {
IOUtils.closeQuietly(oldRunner.client);
}
}
this.cloudJettys.put(collection, cloudJettys);
this.shardToJetty.put(collection, shardToJetty);
this.shardToLeaderJetty.put(collection, shardToLeaderJetty);
} finally {
cloudClient.close();
}
}
use of org.apache.solr.common.cloud.ZkStateReader in project lucene-solr by apache.
the class BaseCdcrDistributedZkTest method distribSetUp.
@Override
public void distribSetUp() throws Exception {
super.distribSetUp();
if (shardCount > 0) {
System.setProperty("numShards", Integer.toString(shardCount));
} else {
System.clearProperty("numShards");
}
if (isSSLMode()) {
System.clearProperty("urlScheme");
ZkStateReader zkStateReader = new ZkStateReader(zkServer.getZkAddress(), AbstractZkTestCase.TIMEOUT, AbstractZkTestCase.TIMEOUT);
try {
zkStateReader.getZkClient().create(ZkStateReader.CLUSTER_PROPS, Utils.toJSON(Collections.singletonMap("urlScheme", "https")), CreateMode.PERSISTENT, true);
} finally {
zkStateReader.close();
}
}
}
use of org.apache.solr.common.cloud.ZkStateReader in project lucene-solr by apache.
the class CreateAliasCmd method call.
@Override
public void call(ClusterState state, ZkNodeProps message, NamedList results) throws Exception {
String aliasName = message.getStr(NAME);
String collections = message.getStr("collections");
ZkStateReader zkStateReader = ocmh.zkStateReader;
Map<String, String> prevColAliases = zkStateReader.getAliases().getCollectionAliasMap();
validateAllCollectionsExist(collections, prevColAliases, zkStateReader.getClusterState());
Map<String, Map<String, String>> newAliasesMap = new HashMap<>();
Map<String, String> newCollectionAliasesMap = new HashMap<>();
if (prevColAliases != null) {
newCollectionAliasesMap.putAll(prevColAliases);
}
newCollectionAliasesMap.put(aliasName, collections);
newAliasesMap.put("collection", newCollectionAliasesMap);
Aliases newAliases = new Aliases(newAliasesMap);
byte[] jsonBytes = null;
if (newAliases.collectionAliasSize() > 0) {
// only sub map right now
jsonBytes = Utils.toJSON(newAliases.getAliasMap());
}
try {
zkStateReader.getZkClient().setData(ZkStateReader.ALIASES, jsonBytes, true);
checkForAlias(aliasName, collections);
// some fudge for other nodes
Thread.sleep(100);
} catch (KeeperException e) {
log.error("", e);
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);
} catch (InterruptedException e) {
log.warn("", e);
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);
}
}
Aggregations