Skip to content

Commit 473d3c0

Browse files
committed
stable node names based on node id by default
1 parent 4156a4b commit 473d3c0

File tree

14 files changed

+188
-2984
lines changed

14 files changed

+188
-2984
lines changed

core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ static void initializeProbes() {
142142
JvmInfo.jvmInfo();
143143
}
144144

145-
private void setup(boolean addShutdownHook, Settings settings, Environment environment) throws Exception {
145+
private void setup(boolean addShutdownHook, Environment environment) throws Exception {
146+
Settings settings = environment.settings();
146147
initializeNatives(
147148
environment.tmpFile(),
148149
BootstrapSettings.MEMORY_LOCK_SETTING.get(settings),
@@ -171,15 +172,15 @@ public void run() {
171172
// install SM after natives, shutdown hooks, etc.
172173
Security.configure(environment, BootstrapSettings.SECURITY_FILTER_BAD_DEFAULTS_SETTING.get(settings));
173174

174-
node = new Node(settings) {
175+
node = new Node(environment) {
175176
@Override
176177
protected void validateNodeBeforeAcceptingRequests(Settings settings, BoundTransportAddress boundTransportAddress) {
177178
BootstrapCheck.check(settings, boundTransportAddress);
178179
}
179180
};
180181
}
181182

182-
private static Environment initialSettings(boolean foreground, Path pidFile, Map<String, String> esSettings) {
183+
private static Environment initialEnvironment(boolean foreground, Path pidFile, Map<String, String> esSettings) {
183184
Terminal terminal = foreground ? Terminal.DEFAULT : null;
184185
Settings.Builder builder = Settings.builder();
185186
if (pidFile != null) {
@@ -225,9 +226,8 @@ static void init(
225226

226227
INSTANCE = new Bootstrap();
227228

228-
Environment environment = initialSettings(foreground, pidFile, esSettings);
229-
Settings settings = environment.settings();
230-
LogConfigurator.configure(settings, true);
229+
Environment environment = initialEnvironment(foreground, pidFile, esSettings);
230+
LogConfigurator.configure(environment.settings(), true);
231231
checkForCustomConfFile();
232232

233233
if (environment.pidFile() != null) {
@@ -250,9 +250,9 @@ static void init(
250250
// initialized as we do not want to grant the runtime permission
251251
// setDefaultUncaughtExceptionHandler
252252
Thread.setDefaultUncaughtExceptionHandler(
253-
new ElasticsearchUncaughtExceptionHandler(() -> Node.NODE_NAME_SETTING.get(settings)));
253+
new ElasticsearchUncaughtExceptionHandler(() -> Node.NODE_NAME_SETTING.get(environment.settings())));
254254

255-
INSTANCE.setup(true, settings, environment);
255+
INSTANCE.setup(true, environment);
256256

257257
INSTANCE.start();
258258

@@ -266,7 +266,7 @@ static void init(
266266
}
267267
ESLogger logger = Loggers.getLogger(Bootstrap.class);
268268
if (INSTANCE.node != null) {
269-
logger = Loggers.getLogger(Bootstrap.class, INSTANCE.node.settings().get("node.name"));
269+
logger = Loggers.getLogger(Bootstrap.class, Node.NODE_NAME_SETTING.get(INSTANCE.node.settings()));
270270
}
271271
// HACK, it sucks to do this, but we will run users out of disk space otherwise
272272
if (e instanceof CreationException) {

core/src/main/java/org/elasticsearch/cluster/node/DiscoveryNode.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import java.util.Map;
4040
import java.util.Set;
4141
import java.util.function.Predicate;
42-
import java.util.function.Supplier;
4342

4443
import static org.elasticsearch.common.transport.TransportAddressSerializers.addressToStream;
4544

@@ -208,7 +207,7 @@ public DiscoveryNode(String nodeName, String nodeId, String ephemeralId, String
208207
}
209208

210209
/** Creates a DiscoveryNode representing the local node. */
211-
public static DiscoveryNode createLocal(Settings settings, TransportAddress publishAddress, String nodeIdSupplier) {
210+
public static DiscoveryNode createLocal(Settings settings, TransportAddress publishAddress, String nodeId) {
212211
Map<String, String> attributes = new HashMap<>(Node.NODE_ATTRIBUTES.get(settings).getAsMap());
213212
Set<DiscoveryNode.Role> roles = new HashSet<>();
214213
if (Node.NODE_INGEST_SETTING.get(settings)) {
@@ -221,7 +220,11 @@ public static DiscoveryNode createLocal(Settings settings, TransportAddress publ
221220
roles.add(DiscoveryNode.Role.DATA);
222221
}
223222

224-
return new DiscoveryNode(Node.NODE_NAME_SETTING.get(settings), nodeIdSupplier, publishAddress,
223+
String nodeName = Node.NODE_NAME_SETTING.get(settings);
224+
if (nodeName.isEmpty()) {
225+
nodeName = nodeId.substring(0, 7);
226+
}
227+
return new DiscoveryNode(nodeName, nodeId, publishAddress,
225228
attributes, roles, Version.CURRENT);
226229
}
227230

core/src/main/java/org/elasticsearch/common/component/AbstractComponent.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.elasticsearch.common.logging.ESLogger;
2525
import org.elasticsearch.common.logging.Loggers;
2626
import org.elasticsearch.common.settings.Settings;
27+
import org.elasticsearch.node.Node;
2728

2829
/**
2930
*
@@ -50,7 +51,7 @@ public AbstractComponent(Settings settings, Class customClass) {
5051
* Returns the nodes name from the settings or the empty string if not set.
5152
*/
5253
public final String nodeName() {
53-
return settings.get("node.name", "");
54+
return Node.NODE_NAME_SETTING.get(settings);
5455
}
5556

5657
/**

core/src/main/java/org/elasticsearch/common/logging/Loggers.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.elasticsearch.common.settings.Settings;
2525
import org.elasticsearch.index.Index;
2626
import org.elasticsearch.index.shard.ShardId;
27+
import org.elasticsearch.node.Node;
2728

2829
import java.net.InetAddress;
2930
import java.net.UnknownHostException;
@@ -101,9 +102,8 @@ public static ESLogger getLogger(String loggerName, Settings settings, String...
101102
prefixesList.add(addr.getHostName());
102103
}
103104
}
104-
String name = settings.get("node.name");
105-
if (name != null) {
106-
prefixesList.add(name);
105+
if (Node.NODE_NAME_SETTING.exists(settings)) {
106+
prefixesList.add(Node.NODE_NAME_SETTING.get(settings));
107107
}
108108
if (prefixes != null && prefixes.length > 0) {
109109
prefixesList.addAll(asList(prefixes));

core/src/main/java/org/elasticsearch/common/settings/Setting.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,11 @@ public static Setting<Long> longSetting(String key, long defaultValue, long minV
503503
}
504504

505505
public static Setting<String> simpleString(String key, Property... properties) {
506-
return new Setting<>(key, s -> "", Function.identity(), properties);
506+
return simpleString(key, "", properties);
507+
}
508+
509+
public static Setting<String> simpleString(String key, String defaultValue, Property... properties) {
510+
return new Setting<>(key, s -> defaultValue, Function.identity(), properties);
507511
}
508512

509513
public static int parseInt(String s, int minValue, String key) {

core/src/main/java/org/elasticsearch/common/util/concurrent/EsExecutors.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.elasticsearch.common.settings.Setting;
2323
import org.elasticsearch.common.settings.Setting.Property;
2424
import org.elasticsearch.common.settings.Settings;
25+
import org.elasticsearch.node.Node;
2526

2627
import java.util.Arrays;
2728
import java.util.concurrent.BlockingQueue;
@@ -83,11 +84,10 @@ public static String threadName(Settings settings, String ... names) {
8384
}
8485

8586
public static String threadName(Settings settings, String namePrefix) {
86-
String nodeName = settings.get("node.name");
87-
if (nodeName == null) {
88-
return threadName("", namePrefix);
87+
if (Node.NODE_NAME_SETTING.exists(settings)) {
88+
return threadName(Node.NODE_NAME_SETTING.get(settings), namePrefix);
8989
} else {
90-
return threadName(nodeName, namePrefix);
90+
return threadName("", namePrefix);
9191
}
9292
}
9393

core/src/main/java/org/elasticsearch/env/Environment.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.ArrayList;
3737
import java.util.Collections;
3838
import java.util.List;
39+
import java.util.Objects;
3940
import java.util.function.Function;
4041

4142
import static org.elasticsearch.common.Strings.cleanPath;
@@ -107,7 +108,6 @@ public class Environment {
107108
}
108109

109110
public Environment(Settings settings) {
110-
this.settings = settings;
111111
final Path homeFile;
112112
if (PATH_HOME_SETTING.exists(settings)) {
113113
homeFile = PathUtils.get(cleanPath(PATH_HOME_SETTING.get(settings)));
@@ -171,6 +171,13 @@ public Environment(Settings settings) {
171171
binFile = homeFile.resolve("bin");
172172
libFile = homeFile.resolve("lib");
173173
modulesFile = homeFile.resolve("modules");
174+
175+
Settings.Builder finalSettings = Settings.builder().put(settings);
176+
finalSettings.put(PATH_HOME_SETTING.getKey(), homeFile);
177+
finalSettings.putArray(PATH_DATA_SETTING.getKey(), dataPaths);
178+
finalSettings.put(PATH_LOGS_SETTING.getKey(), logsFile);
179+
this.settings = finalSettings.build();
180+
174181
}
175182

176183
/**
@@ -332,4 +339,26 @@ public Path tmpFile() {
332339
public static FileStore getFileStore(Path path) throws IOException {
333340
return ESFileStore.getMatchingFileStore(path, fileStores);
334341
}
342+
343+
/**
344+
* assserts that the two environments are equivalent for all things the environment cares about (i.e., all but the setting
345+
* object which may contain different setting)
346+
*/
347+
public static void assertEquivalent(Environment actual, Environment expected) {
348+
assertEquals(actual.dataWithClusterFiles(), expected.dataWithClusterFiles(), "dataWithClusterFiles");
349+
assertEquals(actual.repoFiles(), expected.repoFiles(), "repoFiles");
350+
assertEquals(actual.configFile(), expected.configFile(), "configFile");
351+
assertEquals(actual.scriptsFile(), expected.scriptsFile(), "scriptsFile");
352+
assertEquals(actual.pluginsFile(), expected.pluginsFile(), "pluginsFile");
353+
assertEquals(actual.binFile(), expected.binFile(), "binFile");
354+
assertEquals(actual.libFile(), expected.libFile(), "libFile");
355+
assertEquals(actual.modulesFile(), expected.modulesFile(), "modulesFile");
356+
assertEquals(actual.logsFile(), expected.logsFile(), "logsFile");
357+
assertEquals(actual.pidFile(), expected.pidFile(), "pidFile");
358+
assertEquals(actual.tmpFile(), expected.pidFile(), "tmpFile");
359+
}
360+
361+
private static void assertEquals(Object actual, Object expected, String name) {
362+
assert Objects.equals(actual, expected) : "actual " + name + " [" + actual + "] is different than [ " + expected + "]";
363+
}
335364
}

core/src/main/java/org/elasticsearch/env/NodeEnvironment.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434
import org.elasticsearch.common.Randomness;
3535
import org.elasticsearch.common.SuppressForbidden;
3636
import org.elasticsearch.common.UUIDs;
37-
import org.elasticsearch.common.component.AbstractComponent;
3837
import org.elasticsearch.common.io.FileSystemUtils;
3938
import org.elasticsearch.common.logging.DeprecationLogger;
4039
import org.elasticsearch.common.logging.ESLogger;
40+
import org.elasticsearch.common.logging.Loggers;
4141
import org.elasticsearch.common.settings.Setting;
4242
import org.elasticsearch.common.settings.Setting.Property;
4343
import org.elasticsearch.common.settings.Settings;
@@ -52,6 +52,7 @@
5252
import org.elasticsearch.monitor.fs.FsInfo;
5353
import org.elasticsearch.monitor.fs.FsProbe;
5454
import org.elasticsearch.monitor.jvm.JvmInfo;
55+
import org.elasticsearch.node.Node;
5556

5657
import java.io.Closeable;
5758
import java.io.IOException;
@@ -79,7 +80,9 @@
7980
/**
8081
* A component that holds all data paths for a single node.
8182
*/
82-
public final class NodeEnvironment extends AbstractComponent implements Closeable {
83+
public final class NodeEnvironment implements Closeable {
84+
85+
private final ESLogger logger;
8386

8487
public static class NodePath {
8588
/* ${data.paths}/nodes/{node.id} */
@@ -139,8 +142,6 @@ public String toString() {
139142
private final Path sharedDataPath;
140143
private final Lock[] locks;
141144

142-
private final boolean addLockIdToCustomPath;
143-
144145
private final int nodeLockId;
145146
private final AtomicBoolean closed = new AtomicBoolean(false);
146147
private final Map<ShardId, InternalShardLock> shardLocks = new HashMap<>();
@@ -177,25 +178,25 @@ public String toString() {
177178
public static final String NODES_FOLDER = "nodes";
178179
public static final String INDICES_FOLDER = "indices";
179180
public static final String NODE_LOCK_FILENAME = "node.lock";
180-
public static final String UPGRADE_LOCK_FILENAME = "upgrade.lock";
181181

182182
public NodeEnvironment(Settings settings, Environment environment) throws IOException {
183-
super(settings);
184-
185-
this.addLockIdToCustomPath = ADD_NODE_LOCK_ID_TO_CUSTOM_PATH.get(settings);
186183

187184
if (!DiscoveryNode.nodeRequiresLocalStorage(settings)) {
188185
nodePaths = null;
189186
sharedDataPath = null;
190187
locks = null;
191188
nodeLockId = -1;
192189
nodeMetaData = new NodeMetaData(generateNodeId(settings));
190+
logger = Loggers.getLogger(getClass(), Node.addNodeNameIfNeeded(settings, this.nodeMetaData.nodeId()));
193191
return;
194192
}
195193
final NodePath[] nodePaths = new NodePath[environment.dataWithClusterFiles().length];
196194
final Lock[] locks = new Lock[nodePaths.length];
197195
boolean success = false;
198196

197+
// trace logger to debug issues before the default node name is derived from the node id
198+
ESLogger startupTraceLogger = Loggers.getLogger(getClass(), settings);
199+
199200
try {
200201
sharedDataPath = environment.sharedDataFile();
201202
int nodeLockId = -1;
@@ -207,7 +208,7 @@ public NodeEnvironment(Settings settings, Environment environment) throws IOExce
207208
Path dataDir = environment.dataFiles()[dirIndex];
208209
// TODO: Remove this in 6.0, we are no longer going to read from the cluster name directory
209210
if (readFromDataPathWithClusterName(dataDirWithClusterName)) {
210-
DeprecationLogger deprecationLogger = new DeprecationLogger(logger);
211+
DeprecationLogger deprecationLogger = new DeprecationLogger(startupTraceLogger);
211212
deprecationLogger.deprecated("ES has detected the [path.data] folder using the cluster name as a folder [{}], " +
212213
"Elasticsearch 6.0 will not allow the cluster name as a folder within the data path", dataDir);
213214
dataDir = dataDirWithClusterName;
@@ -216,20 +217,20 @@ public NodeEnvironment(Settings settings, Environment environment) throws IOExce
216217
Files.createDirectories(dir);
217218

218219
try (Directory luceneDir = FSDirectory.open(dir, NativeFSLockFactory.INSTANCE)) {
219-
logger.trace("obtaining node lock on {} ...", dir.toAbsolutePath());
220+
startupTraceLogger.trace("obtaining node lock on {} ...", dir.toAbsolutePath());
220221
try {
221222
locks[dirIndex] = luceneDir.obtainLock(NODE_LOCK_FILENAME);
222223
nodePaths[dirIndex] = new NodePath(dir);
223224
nodeLockId = possibleLockId;
224225
} catch (LockObtainFailedException ex) {
225-
logger.trace("failed to obtain node lock on {}", dir.toAbsolutePath());
226+
startupTraceLogger.trace("failed to obtain node lock on {}", dir.toAbsolutePath());
226227
// release all the ones that were obtained up until now
227228
releaseAndNullLocks(locks);
228229
break;
229230
}
230231

231232
} catch (IOException e) {
232-
logger.trace("failed to obtain node lock on {}", e, dir.toAbsolutePath());
233+
startupTraceLogger.trace("failed to obtain node lock on {}", e, dir.toAbsolutePath());
233234
lastException = new IOException("failed to obtain lock on " + dir.toAbsolutePath(), e);
234235
// release all the ones that were obtained up until now
235236
releaseAndNullLocks(locks);
@@ -246,6 +247,8 @@ public NodeEnvironment(Settings settings, Environment environment) throws IOExce
246247
throw new IllegalStateException("Failed to obtain node lock, is the following location writable?: "
247248
+ Arrays.toString(environment.dataWithClusterFiles()), lastException);
248249
}
250+
this.nodeMetaData = loadOrCreateNodeMetaData(settings, startupTraceLogger, nodePaths);
251+
this.logger = Loggers.getLogger(getClass(), Node.addNodeNameIfNeeded(settings, this.nodeMetaData.nodeId()));
249252

250253
this.nodeLockId = nodeLockId;
251254
this.locks = locks;
@@ -258,8 +261,6 @@ public NodeEnvironment(Settings settings, Environment environment) throws IOExce
258261
maybeLogPathDetails();
259262
maybeLogHeapDetails();
260263

261-
this.nodeMetaData = loadOrCreateNodeMetaData(settings, logger, nodePaths);
262-
263264
applySegmentInfosTrace(settings);
264265
assertCanWrite();
265266
success = true;
@@ -915,10 +916,6 @@ public void ensureAtomicMoveSupported() throws IOException {
915916
}
916917
}
917918

918-
Settings getSettings() { // for testing
919-
return settings;
920-
}
921-
922919
/**
923920
* Resolve the custom path for a index's shard.
924921
* Uses the {@code IndexMetaData.SETTING_DATA_PATH} setting to determine
@@ -931,7 +928,7 @@ public Path resolveBaseCustomLocation(IndexSettings indexSettings) {
931928
if (customDataDir != null) {
932929
// This assert is because this should be caught by MetaDataCreateIndexService
933930
assert sharedDataPath != null;
934-
if (addLockIdToCustomPath) {
931+
if (ADD_NODE_LOCK_ID_TO_CUSTOM_PATH.get(indexSettings.getNodeSettings())) {
935932
return sharedDataPath.resolve(customDataDir).resolve(Integer.toString(this.nodeLockId));
936933
} else {
937934
return sharedDataPath.resolve(customDataDir);

core/src/main/java/org/elasticsearch/index/IndexSettings.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.elasticsearch.common.unit.TimeValue;
3535
import org.elasticsearch.index.mapper.internal.AllFieldMapper;
3636
import org.elasticsearch.index.translog.Translog;
37+
import org.elasticsearch.node.Node;
3738

3839
import java.util.Locale;
3940
import java.util.concurrent.TimeUnit;
@@ -227,7 +228,7 @@ public IndexSettings(final IndexMetaData indexMetaData, final Settings nodeSetti
227228
this.index = indexMetaData.getIndex();
228229
version = Version.indexCreated(settings);
229230
logger = Loggers.getLogger(getClass(), settings, index);
230-
nodeName = settings.get("node.name", "");
231+
nodeName = Node.NODE_NAME_SETTING.get(settings);
231232
this.indexMetaData = indexMetaData;
232233
numberOfShards = settings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_SHARDS, null);
233234
isShadowReplicaIndex = IndexMetaData.isIndexUsingShadowReplicas(settings);

0 commit comments

Comments
 (0)