Skip to content

Commit

Permalink
SOLR-9280 - make nodeName a configurable parameter in solr.xml
Browse files Browse the repository at this point in the history
  • Loading branch information
kelaban committed Oct 17, 2016
1 parent 46aeb52 commit b9bfb85
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 57 deletions.
25 changes: 17 additions & 8 deletions solr/core/src/java/org/apache/solr/cloud/ZkController.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ public boolean equals(Object obj) {

private final int localHostPort; // example: 54065
private final String hostName; // example: 127.0.0.1
private final String nodeName; // example: 127.0.0.1:54065_solr
private final String genericNodeName; // example: 127.0.0.1:54065_solr
private final String nodeName; //set in solr.xml or genericNodeName as default
private String baseURL; // example: http://127.0.0.1:54065/solr

private final CloudConfig cloudConfig;
Expand Down Expand Up @@ -271,8 +272,11 @@ public ZkController(final CoreContainer cc, String zkServerAddress, int zkClient
this.zkServerAddress = zkServerAddress;
this.localHostPort = cloudConfig.getSolrHostPort();
this.hostName = normalizeHostName(cloudConfig.getHost());
this.nodeName = generateNodeName(this.hostName, Integer.toString(this.localHostPort), localHostContext);
MDCLoggingContext.setNode(nodeName);
this.genericNodeName = generateNodeName(this.hostName, Integer.toString(this.localHostPort), localHostContext);
String nodeName = cloudConfig.getNodeName();
this.nodeName = nodeName == null || nodeName.isEmpty() ? this.genericNodeName : nodeName;
String loggingContext = this.nodeName == this.genericNodeName ? this.genericNodeName : String.format("%s[%s]", this.nodeName, this.genericNodeName);
MDCLoggingContext.setNode(loggingContext);
this.leaderVoteWait = cloudConfig.getLeaderVoteWait();
this.leaderConflictResolveWait = cloudConfig.getLeaderConflictResolveWait();

Expand Down Expand Up @@ -671,7 +675,7 @@ private void init(CurrentCoreDescriptorProvider registerOnReconnect) {
try {
createClusterZkNodes(zkClient);
zkStateReader.createClusterStateWatchersAndUpdate();
this.baseURL = zkStateReader.getBaseUrlForNodeName(this.nodeName);
this.baseURL = zkStateReader.getBaseUrlFromGenericNodeName(this.genericNodeName);

checkForExistingEphemeralNode();

Expand Down Expand Up @@ -738,9 +742,10 @@ private void checkForExistingEphemeralNode() throws KeeperException, Interrupted
}

boolean deleted = deletedLatch.await(zkClient.getSolrZooKeeper().getSessionTimeout() * 2, TimeUnit.MILLISECONDS);

if (!deleted) {
throw new SolrException(ErrorCode.SERVER_ERROR, "A previous ephemeral live node still exists. " +
"Solr cannot continue. Please ensure that no other Solr process using the same port is running already.");
throw new SolrException(ErrorCode.SERVER_ERROR, "An ephemeral live node still exists named '" + this.nodeName +
"'. Solr cannot continue. Please ensure that no other Solr process using the same port is running already or configured with the same nodeName in solr.xml");
}
}

Expand Down Expand Up @@ -819,7 +824,11 @@ private void createEphemeralLiveNode() throws KeeperException,
String nodeName = getNodeName();
String nodePath = ZkStateReader.LIVE_NODES_ZKNODE + "/" + nodeName;
log.info("Register node as live in ZooKeeper:" + nodePath);
zkClient.makePath(nodePath, CreateMode.EPHEMERAL, true);
zkClient.makePath(nodePath, getGenericNodeName().getBytes(), CreateMode.EPHEMERAL, true);
}

private String getGenericNodeName() {
return genericNodeName;
}

public String getNodeName() {
Expand Down Expand Up @@ -2104,7 +2113,7 @@ public void updateLeaderInitiatedRecoveryState(String collection, String shardId
stateObj.put(ZkStateReader.STATE_PROP, state.toString());
// only update the createdBy value if it's not set
if (stateObj.get("createdByNodeName") == null) {
stateObj.put("createdByNodeName", this.nodeName);
stateObj.put("createdByNodeName", this.genericNodeName);
}
if (stateObj.get("createdByCoreNodeName") == null && leaderCoreNodeName != null) {
stateObj.put("createdByCoreNodeName", leaderCoreNodeName);
Expand Down
17 changes: 15 additions & 2 deletions solr/core/src/java/org/apache/solr/core/CloudConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ public class CloudConfig {
private final int createCollectionWaitTimeTillActive;

private final boolean createCollectionCheckLeaderActive;

private final String nodeName;

CloudConfig(String zkHost, int zkClientTimeout, int hostPort, String hostName, String hostContext, boolean useGenericCoreNames,
int leaderVoteWait, int leaderConflictResolveWait, int autoReplicaFailoverWaitAfterExpiration,
int autoReplicaFailoverWorkLoopDelay, int autoReplicaFailoverBadNodeExpiration, String zkCredentialsProviderClass,
String zkACLProviderClass, int createCollectionWaitTimeTillActive, boolean createCollectionCheckLeaderActive) {
String zkACLProviderClass, int createCollectionWaitTimeTillActive, boolean createCollectionCheckLeaderActive, String nodeName) {
this.zkHost = zkHost;
this.zkClientTimeout = zkClientTimeout;
this.hostPort = hostPort;
Expand All @@ -69,6 +71,7 @@ public class CloudConfig {
this.zkACLProviderClass = zkACLProviderClass;
this.createCollectionWaitTimeTillActive = createCollectionWaitTimeTillActive;
this.createCollectionCheckLeaderActive = createCollectionCheckLeaderActive;
this.nodeName = nodeName;

if (this.hostPort == -1)
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "'hostPort' must be configured to run SolrCloud");
Expand Down Expand Up @@ -135,6 +138,10 @@ public int getCreateCollectionWaitTimeTillActive() {
public boolean isCreateCollectionCheckLeaderActive() {
return createCollectionCheckLeaderActive;
}

public String getNodeName() {
return nodeName;
}

public static class CloudConfigBuilder {

Expand Down Expand Up @@ -164,6 +171,7 @@ public static class CloudConfigBuilder {
private String zkACLProviderClass;
private int createCollectionWaitTimeTillActive = DEFAULT_CREATE_COLLECTION_ACTIVE_WAIT;
private boolean createCollectionCheckLeaderActive = DEFAULT_CREATE_COLLECTION_CHECK_LEADER_ACTIVE;
private String nodeName = null;

public CloudConfigBuilder(String hostName, int hostPort) {
this(hostName, hostPort, null);
Expand Down Expand Up @@ -235,11 +243,16 @@ public CloudConfigBuilder setCreateCollectionCheckLeaderActive(boolean createCol
return this;
}

public CloudConfigBuilder setNodeName(String nodeName) {
this.nodeName = nodeName;
return this;
}

public CloudConfig build() {
return new CloudConfig(zkHost, zkClientTimeout, hostPort, hostName, hostContext, useGenericCoreNames, leaderVoteWait,
leaderConflictResolveWait, autoReplicaFailoverWaitAfterExpiration, autoReplicaFailoverWorkLoopDelay,
autoReplicaFailoverBadNodeExpiration, zkCredentialsProviderClass, zkACLProviderClass, createCollectionWaitTimeTillActive,
createCollectionCheckLeaderActive);
createCollectionCheckLeaderActive, nodeName);
}
}
}
4 changes: 4 additions & 0 deletions solr/core/src/java/org/apache/solr/core/CoreContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,10 @@ public ZkController getZkController() {
return zkSys.getZkController();
}

public ZkStateReader getZkStateReader() {
return isZooKeeperAware() ? zkSys.getZkController().getZkStateReader() : null;
}

public NodeConfig getConfig() {
return cfg;
}
Expand Down
3 changes: 3 additions & 0 deletions solr/core/src/java/org/apache/solr/core/SolrXmlConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@ private static CloudConfig fillSolrCloudSection(NamedList<Object> nl) {
case "genericCoreNodeNames":
builder.setUseGenericCoreNames(Boolean.parseBoolean(value));
break;
case "nodeName":
builder.setNodeName(value);
break;
case "zkACLProvider":
builder.setZkACLProviderClass(value);
break;
Expand Down
1 change: 1 addition & 0 deletions solr/core/src/test-files/solr/solr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<str name="hostContext">${hostContext:solr}</str>
<int name="zkClientTimeout">${solr.zkclienttimeout:30000}</int>
<bool name="genericCoreNodeNames">${genericCoreNodeNames:true}</bool>
<str name="nodeName">${solr.nodeName:}</str>
<int name="leaderVoteWait">${leaderVoteWait:10000}</int>
<int name="distribUpdateConnTimeout">${distribUpdateConnTimeout:45000}</int>
<int name="distribUpdateSoTimeout">${distribUpdateSoTimeout:340000}</int>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,14 +635,6 @@ private NamedList realTimeGetDocId(HttpSolrClient solr, String docId) throws Sol
return solr.request(qr);
}

protected int getReplicaPort(Replica replica) {
String replicaNode = replica.getNodeName();
String tmp = replicaNode.substring(replicaNode.indexOf(':')+1);
if (tmp.indexOf('_') != -1)
tmp = tmp.substring(0,tmp.indexOf('_'));
return Integer.parseInt(tmp);
}

protected void waitToSeeReplicasActive(String testCollectionName, String shardId, Set<String> replicasToCheck, int maxWaitSecs) throws Exception {
final RTimer timer = new RTimer();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public void restartWithRolesTest() throws Exception {
numDesignateOverseers = Math.max(getShardCount(), numDesignateOverseers);
List<String> designates = new ArrayList<>();
List<CloudJettyRunner> designateJettys = new ArrayList<>();

for (int i = 0; i < numDesignateOverseers; i++) {
int n = random().nextInt(getShardCount());
String nodeName = cloudJettys.get(n).nodeName;
Expand Down
30 changes: 15 additions & 15 deletions solr/core/src/test/org/apache/solr/cloud/ZkControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,41 +97,41 @@ public void testNodeNameUrlConversion() throws Exception {

// getBaseUrlForNodeName
assertEquals("http://zzz.xxx:1234/solr",
zkStateReader.getBaseUrlForNodeName("zzz.xxx:1234_solr"));
zkStateReader.getBaseUrlFromGenericNodeName("zzz.xxx:1234_solr"));
assertEquals("http://xxx:99",
zkStateReader.getBaseUrlForNodeName("xxx:99_"));
zkStateReader.getBaseUrlFromGenericNodeName("xxx:99_"));
assertEquals("http://foo-bar.baz.org:9999/some_dir",
zkStateReader.getBaseUrlForNodeName("foo-bar.baz.org:9999_some_dir"));
zkStateReader.getBaseUrlFromGenericNodeName("foo-bar.baz.org:9999_some_dir"));
assertEquals("http://foo-bar.baz.org:9999/solr/sub_dir",
zkStateReader.getBaseUrlForNodeName("foo-bar.baz.org:9999_solr%2Fsub_dir"));
zkStateReader.getBaseUrlFromGenericNodeName("foo-bar.baz.org:9999_solr%2Fsub_dir"));

// generateNodeName + getBaseUrlForNodeName
assertEquals("http://foo:9876/solr",
zkStateReader.getBaseUrlForNodeName
zkStateReader.getBaseUrlFromGenericNodeName
(ZkController.generateNodeName("foo", "9876", "solr")));
assertEquals("http://foo:9876/solr",
zkStateReader.getBaseUrlForNodeName
zkStateReader.getBaseUrlFromGenericNodeName
(ZkController.generateNodeName("foo", "9876", "/solr")));
assertEquals("http://foo:9876/solr",
zkStateReader.getBaseUrlForNodeName
zkStateReader.getBaseUrlFromGenericNodeName
(ZkController.generateNodeName("foo", "9876", "/solr/")));
assertEquals("http://foo.bar.com:9876/solr/sub_dir",
zkStateReader.getBaseUrlForNodeName
zkStateReader.getBaseUrlFromGenericNodeName
(ZkController.generateNodeName("foo.bar.com", "9876", "solr/sub_dir")));
assertEquals("http://foo.bar.com:9876/solr/sub_dir",
zkStateReader.getBaseUrlForNodeName
zkStateReader.getBaseUrlFromGenericNodeName
(ZkController.generateNodeName("foo.bar.com", "9876", "/solr/sub_dir/")));
assertEquals("http://foo-bar:9876",
zkStateReader.getBaseUrlForNodeName
zkStateReader.getBaseUrlFromGenericNodeName
(ZkController.generateNodeName("foo-bar", "9876", "")));
assertEquals("http://foo-bar:9876",
zkStateReader.getBaseUrlForNodeName
zkStateReader.getBaseUrlFromGenericNodeName
(ZkController.generateNodeName("foo-bar", "9876", "/")));
assertEquals("http://foo-bar.com:80/some_dir",
zkStateReader.getBaseUrlForNodeName
zkStateReader.getBaseUrlFromGenericNodeName
(ZkController.generateNodeName("foo-bar.com", "80", "some_dir")));
assertEquals("http://foo-bar.com:80/some_dir",
zkStateReader.getBaseUrlForNodeName
zkStateReader.getBaseUrlFromGenericNodeName
(ZkController.generateNodeName("foo-bar.com", "80", "/some_dir")));

}
Expand All @@ -146,10 +146,10 @@ public void testNodeNameUrlConversion() throws Exception {
zkStateReader.createClusterStateWatchersAndUpdate();

assertEquals("https://zzz.xxx:1234/solr",
zkStateReader.getBaseUrlForNodeName("zzz.xxx:1234_solr"));
zkStateReader.getBaseUrlFromGenericNodeName("zzz.xxx:1234_solr"));

assertEquals("https://foo-bar.com:80/some_dir",
zkStateReader.getBaseUrlForNodeName
zkStateReader.getBaseUrlFromGenericNodeName
(ZkController.generateNodeName("foo-bar.com", "80", "/some_dir")));

}
Expand Down
Loading

0 comments on commit b9bfb85

Please sign in to comment.