Skip to content

Commit 8f9fcef

Browse files
committed
Move unicast_hosts.txt to the top-level config dir
Support both paths for BWC reasons, but emit a deprecation warning if the old one is used. Also, replace the unnecessary full stack trace emitted if the file isn't there with a single-line warning. A stack trace is still emitted if the file seems to be there but reading it fails.
1 parent e2ef488 commit 8f9fcef

File tree

2 files changed

+89
-50
lines changed

2 files changed

+89
-50
lines changed

server/src/main/java/org/elasticsearch/discovery/zen/FileBasedUnicastHostsProvider.java

+31-20
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,12 @@
2020
package org.elasticsearch.discovery.zen;
2121

2222
import org.apache.logging.log4j.message.ParameterizedMessage;
23-
import org.apache.logging.log4j.util.Supplier;
2423
import org.elasticsearch.common.component.AbstractComponent;
2524
import org.elasticsearch.common.settings.Settings;
2625
import org.elasticsearch.common.transport.TransportAddress;
2726

28-
import java.io.FileNotFoundException;
2927
import java.io.IOException;
3028
import java.nio.file.Files;
31-
import java.nio.file.NoSuchFileException;
3229
import java.nio.file.Path;
3330
import java.util.Collections;
3431
import java.util.List;
@@ -52,30 +49,44 @@ public class FileBasedUnicastHostsProvider extends AbstractComponent implements
5249
public static final String UNICAST_HOSTS_FILE = "unicast_hosts.txt";
5350

5451
private final Path unicastHostsFilePath;
52+
private final Path legacyUnicastHostsFilePath;
5553

5654
public FileBasedUnicastHostsProvider(Settings settings, Path configFile) {
5755
super(settings);
58-
this.unicastHostsFilePath = configFile.resolve("discovery-file").resolve(UNICAST_HOSTS_FILE);
56+
this.unicastHostsFilePath = configFile.resolve(UNICAST_HOSTS_FILE);
57+
this.legacyUnicastHostsFilePath = configFile.resolve("discovery-file").resolve(UNICAST_HOSTS_FILE);
5958
}
6059

61-
@Override
62-
public List<TransportAddress> buildDynamicHosts(HostsResolver hostsResolver) {
63-
List<String> hostsList;
64-
try (Stream<String> lines = Files.lines(unicastHostsFilePath)) {
65-
hostsList = lines.filter(line -> line.startsWith("#") == false) // lines starting with `#` are comments
66-
.collect(Collectors.toList());
67-
} catch (FileNotFoundException | NoSuchFileException e) {
68-
logger.warn((Supplier<?>) () -> new ParameterizedMessage("[discovery-file] Failed to find unicast hosts file [{}]",
69-
unicastHostsFilePath), e);
70-
hostsList = Collections.emptyList();
60+
private List<String> getHostsList() {
61+
if (Files.exists(unicastHostsFilePath)) {
62+
return readFileContents(unicastHostsFilePath);
63+
}
64+
65+
if (Files.exists(legacyUnicastHostsFilePath)) {
66+
deprecationLogger.deprecated("Found dynamic hosts list at [{}] but this path is deprecated. This list should be at [{}] " +
67+
"instead. Support for the deprecated path will be removed in future.", legacyUnicastHostsFilePath, unicastHostsFilePath);
68+
return readFileContents(legacyUnicastHostsFilePath);
69+
}
70+
71+
logger.warn("expected, but did not find, a dynamic hosts list at [{}]", unicastHostsFilePath);
72+
73+
return Collections.emptyList();
74+
}
75+
76+
private List<String> readFileContents(Path path) {
77+
try (Stream<String> lines = Files.lines(path)) {
78+
return lines.filter(line -> line.startsWith("#") == false) // lines starting with `#` are comments
79+
.collect(Collectors.toList());
7180
} catch (IOException e) {
72-
logger.warn((Supplier<?>) () -> new ParameterizedMessage("[discovery-file] Error reading unicast hosts file [{}]",
73-
unicastHostsFilePath), e);
74-
hostsList = Collections.emptyList();
81+
logger.warn(() -> new ParameterizedMessage("failed to read file [{}]", unicastHostsFilePath), e);
82+
return Collections.emptyList();
7583
}
84+
}
7685

77-
final List<TransportAddress> dynamicHosts = hostsResolver.resolveHosts(hostsList, 1);
78-
logger.debug("[discovery-file] Using dynamic discovery nodes {}", dynamicHosts);
79-
return dynamicHosts;
86+
@Override
87+
public List<TransportAddress> buildDynamicHosts(HostsResolver hostsResolver) {
88+
final List<TransportAddress> transportAddresses = hostsResolver.resolveHosts(getHostsList(), 1);
89+
logger.debug("seed addresses: {}", transportAddresses);
90+
return transportAddresses;
8091
}
8192
}

server/src/test/java/org/elasticsearch/discovery/zen/FileBasedUnicastHostsProviderTests.java

+58-30
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@
5252

5353
public class FileBasedUnicastHostsProviderTests extends ESTestCase {
5454

55+
private boolean legacyLocation;
5556
private ThreadPool threadPool;
5657
private ExecutorService executorService;
5758
private MockTransportService transportService;
59+
private Path configPath;
5860

5961
@Before
6062
public void setUp() throws Exception {
@@ -78,23 +80,20 @@ public void tearDown() throws Exception {
7880

7981
@Before
8082
public void createTransportSvc() {
81-
MockTcpTransport transport =
82-
new MockTcpTransport(Settings.EMPTY,
83-
threadPool,
84-
BigArrays.NON_RECYCLING_INSTANCE,
85-
new NoneCircuitBreakerService(),
86-
new NamedWriteableRegistry(Collections.emptyList()),
87-
new NetworkService(Collections.emptyList())) {
88-
@Override
89-
public BoundTransportAddress boundAddress() {
90-
return new BoundTransportAddress(
91-
new TransportAddress[]{new TransportAddress(InetAddress.getLoopbackAddress(), 9300)},
92-
new TransportAddress(InetAddress.getLoopbackAddress(), 9300)
93-
);
94-
}
95-
};
83+
final MockTcpTransport transport = new MockTcpTransport(Settings.EMPTY, threadPool, BigArrays.NON_RECYCLING_INSTANCE,
84+
new NoneCircuitBreakerService(),
85+
new NamedWriteableRegistry(Collections.emptyList()),
86+
new NetworkService(Collections.emptyList())) {
87+
@Override
88+
public BoundTransportAddress boundAddress() {
89+
return new BoundTransportAddress(
90+
new TransportAddress[]{new TransportAddress(InetAddress.getLoopbackAddress(), 9300)},
91+
new TransportAddress(InetAddress.getLoopbackAddress(), 9300)
92+
);
93+
}
94+
};
9695
transportService = new MockTransportService(Settings.EMPTY, transport, threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR,
97-
null);
96+
null);
9897
}
9998

10099
public void testBuildDynamicNodes() throws Exception {
@@ -109,16 +108,26 @@ public void testBuildDynamicNodes() throws Exception {
109108
assertEquals(9300, nodes.get(2).getPort());
110109
}
111110

111+
public void testBuildDynamicNodesLegacyLocation() throws Exception {
112+
legacyLocation = true;
113+
testBuildDynamicNodes();
114+
assertDeprecatedLocationWarning();
115+
}
116+
112117
public void testEmptyUnicastHostsFile() throws Exception {
113118
final List<String> hostEntries = Collections.emptyList();
114119
final List<TransportAddress> addresses = setupAndRunHostProvider(hostEntries);
115120
assertEquals(0, addresses.size());
116121
}
117122

118-
public void testUnicastHostsDoesNotExist() throws Exception {
119-
final Settings settings = Settings.builder()
120-
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir())
121-
.build();
123+
public void testEmptyUnicastHostsFileLegacyLocation() throws Exception {
124+
legacyLocation = true;
125+
testEmptyUnicastHostsFile();
126+
assertDeprecatedLocationWarning();
127+
}
128+
129+
public void testUnicastHostsDoesNotExist() {
130+
final Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()).build();
122131
final FileBasedUnicastHostsProvider provider = new FileBasedUnicastHostsProvider(settings, createTempDir().toAbsolutePath());
123132
final List<TransportAddress> addresses = provider.buildDynamicHosts((hosts, limitPortCounts) ->
124133
UnicastZenPing.resolveHostsLists(executorService, logger, hosts, limitPortCounts, transportService,
@@ -127,41 +136,60 @@ public void testUnicastHostsDoesNotExist() throws Exception {
127136
}
128137

129138
public void testInvalidHostEntries() throws Exception {
130-
List<String> hostEntries = Arrays.asList("192.168.0.1:9300:9300");
131-
List<TransportAddress> addresses = setupAndRunHostProvider(hostEntries);
139+
final List<String> hostEntries = Arrays.asList("192.168.0.1:9300:9300");
140+
final List<TransportAddress> addresses = setupAndRunHostProvider(hostEntries);
132141
assertEquals(0, addresses.size());
133142
}
134143

144+
public void testInvalidHostEntriesLegacyLocation() throws Exception {
145+
legacyLocation = true;
146+
testInvalidHostEntries();
147+
assertDeprecatedLocationWarning();
148+
}
149+
135150
public void testSomeInvalidHostEntries() throws Exception {
136-
List<String> hostEntries = Arrays.asList("192.168.0.1:9300:9300", "192.168.0.1:9301");
137-
List<TransportAddress> addresses = setupAndRunHostProvider(hostEntries);
151+
final List<String> hostEntries = Arrays.asList("192.168.0.1:9300:9300", "192.168.0.1:9301");
152+
final List<TransportAddress> addresses = setupAndRunHostProvider(hostEntries);
138153
assertEquals(1, addresses.size()); // only one of the two is valid and will be used
139154
assertEquals("192.168.0.1", addresses.get(0).getAddress());
140155
assertEquals(9301, addresses.get(0).getPort());
141156
}
142157

158+
public void testSomeInvalidHostEntriesLegacyLocation() throws Exception {
159+
legacyLocation = true;
160+
testSomeInvalidHostEntries();
161+
assertDeprecatedLocationWarning();
162+
}
163+
143164
// sets up the config dir, writes to the unicast hosts file in the config dir,
144165
// and then runs the file-based unicast host provider to get the list of discovery nodes
145166
private List<TransportAddress> setupAndRunHostProvider(final List<String> hostEntries) throws IOException {
146167
final Path homeDir = createTempDir();
147168
final Settings settings = Settings.builder()
148-
.put(Environment.PATH_HOME_SETTING.getKey(), homeDir)
149-
.build();
150-
final Path configPath;
169+
.put(Environment.PATH_HOME_SETTING.getKey(), homeDir)
170+
.build();
151171
if (randomBoolean()) {
152172
configPath = homeDir.resolve("config");
153173
} else {
154174
configPath = createTempDir();
155175
}
156-
final Path discoveryFilePath = configPath.resolve("discovery-file");
176+
final Path discoveryFilePath = legacyLocation ? configPath.resolve("discovery-file") : configPath;
157177
Files.createDirectories(discoveryFilePath);
158178
final Path unicastHostsPath = discoveryFilePath.resolve(UNICAST_HOSTS_FILE);
159179
try (BufferedWriter writer = Files.newBufferedWriter(unicastHostsPath)) {
160180
writer.write(String.join("\n", hostEntries));
161181
}
162182

163183
return new FileBasedUnicastHostsProvider(settings, configPath).buildDynamicHosts((hosts, limitPortCounts) ->
164-
UnicastZenPing.resolveHostsLists(executorService, logger, hosts, limitPortCounts, transportService,
165-
TimeValue.timeValueSeconds(10)));
184+
UnicastZenPing.resolveHostsLists(executorService, logger, hosts, limitPortCounts, transportService,
185+
TimeValue.timeValueSeconds(10)));
186+
}
187+
188+
private void assertDeprecatedLocationWarning() {
189+
assertWarnings("Found dynamic hosts list at [" +
190+
configPath.resolve("discovery-file").resolve(UNICAST_HOSTS_FILE) +
191+
"] but this path is deprecated. This list should be at [" +
192+
configPath.resolve(UNICAST_HOSTS_FILE) +
193+
"] instead. Support for the deprecated path will be removed in future.");
166194
}
167195
}

0 commit comments

Comments
 (0)