Skip to content

Commit b3d2cfc

Browse files
original-brownbearkcm
authored andcommitted
TESTS: Use File Based Discovery in REST Tests (#34560)
* For `6.5+` use file based discovery in REST tests * Relates #33675
1 parent d44126d commit b3d2cfc

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

buildSrc/src/main/groovy/org/elasticsearch/gradle/test/ClusterConfiguration.groovy

+3-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ class ClusterConfiguration {
6868
* In case of more than one node, this defaults to the number of nodes
6969
*/
7070
@Input
71-
Closure<Integer> minimumMasterNodes = { getNumNodes() > 1 ? getNumNodes() : -1 }
71+
Closure<Integer> minimumMasterNodes = {
72+
return getNumNodes() > 1 ? getNumNodes() : -1
73+
}
7274

7375
@Input
7476
String jvmArgs = "-Xms" + System.getProperty('tests.heap.size', '512m') +

buildSrc/src/main/groovy/org/elasticsearch/gradle/test/ClusterFormationTasks.groovy

+42-9
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,31 @@ class ClusterFormationTasks {
122122
}
123123
NodeInfo node = new NodeInfo(config, i, project, prefix, elasticsearchVersion, sharedDir)
124124
nodes.add(node)
125-
Object dependsOn = startTasks.empty ? startDependencies : startTasks.get(0)
126-
startTasks.add(configureNode(project, prefix, runner, dependsOn, node, config, distro, nodes.get(0)))
125+
Closure<Map> writeConfigSetup
126+
Object dependsOn
127+
if (node.nodeVersion.onOrAfter("6.5.0-SNAPSHOT")) {
128+
writeConfigSetup = { Map esConfig ->
129+
// Don't force discovery provider if one is set by the test cluster specs already
130+
if (esConfig.containsKey('discovery.zen.hosts_provider') == false) {
131+
esConfig['discovery.zen.hosts_provider'] = 'file'
132+
}
133+
esConfig['discovery.zen.ping.unicast.hosts'] = []
134+
esConfig
135+
}
136+
dependsOn = startDependencies
137+
} else {
138+
dependsOn = startTasks.empty ? startDependencies : startTasks.get(0)
139+
writeConfigSetup = { Map esConfig ->
140+
String unicastTransportUri = node.config.unicastTransportUri(nodes.get(0), node, project.ant)
141+
if (unicastTransportUri == null) {
142+
esConfig['discovery.zen.ping.unicast.hosts'] = []
143+
} else {
144+
esConfig['discovery.zen.ping.unicast.hosts'] = "\"${unicastTransportUri}\""
145+
}
146+
esConfig
147+
}
148+
}
149+
startTasks.add(configureNode(project, prefix, runner, dependsOn, node, config, distro, writeConfigSetup))
127150
}
128151

129152
Task wait = configureWaitTask("${prefix}#wait", project, nodes, startTasks, config.nodeStartupWaitSeconds)
@@ -182,7 +205,7 @@ class ClusterFormationTasks {
182205
* @return a task which starts the node.
183206
*/
184207
static Task configureNode(Project project, String prefix, Task runner, Object dependsOn, NodeInfo node, ClusterConfiguration config,
185-
Configuration distribution, NodeInfo seedNode) {
208+
Configuration distribution, Closure<Map> writeConfig) {
186209

187210
// tasks are chained so their execution order is maintained
188211
Task setup = project.tasks.create(name: taskName(prefix, node, 'clean'), type: Delete, dependsOn: dependsOn) {
@@ -198,7 +221,7 @@ class ClusterFormationTasks {
198221
setup = configureCheckPreviousTask(taskName(prefix, node, 'checkPrevious'), project, setup, node)
199222
setup = configureStopTask(taskName(prefix, node, 'stopPrevious'), project, setup, node)
200223
setup = configureExtractTask(taskName(prefix, node, 'extract'), project, setup, node, distribution)
201-
setup = configureWriteConfigTask(taskName(prefix, node, 'configure'), project, setup, node, seedNode)
224+
setup = configureWriteConfigTask(taskName(prefix, node, 'configure'), project, setup, node, writeConfig)
202225
setup = configureCreateKeystoreTask(taskName(prefix, node, 'createKeystore'), project, setup, node)
203226
setup = configureAddKeystoreSettingTasks(prefix, project, setup, node)
204227
setup = configureAddKeystoreFileTasks(prefix, project, setup, node)
@@ -301,7 +324,7 @@ class ClusterFormationTasks {
301324
}
302325

303326
/** Adds a task to write elasticsearch.yml for the given node configuration */
304-
static Task configureWriteConfigTask(String name, Project project, Task setup, NodeInfo node, NodeInfo seedNode) {
327+
static Task configureWriteConfigTask(String name, Project project, Task setup, NodeInfo node, Closure<Map> configFilter) {
305328
Map esConfig = [
306329
'cluster.name' : node.clusterName,
307330
'node.name' : "node-" + node.nodeNum,
@@ -347,10 +370,7 @@ class ClusterFormationTasks {
347370

348371
Task writeConfig = project.tasks.create(name: name, type: DefaultTask, dependsOn: setup)
349372
writeConfig.doFirst {
350-
String unicastTransportUri = node.config.unicastTransportUri(seedNode, node, project.ant)
351-
if (unicastTransportUri != null) {
352-
esConfig['discovery.zen.ping.unicast.hosts'] = "\"${unicastTransportUri}\""
353-
}
373+
esConfig = configFilter.call(esConfig)
354374
File configFile = new File(node.pathConf, 'elasticsearch.yml')
355375
logger.info("Configuring ${configFile}")
356376
configFile.setText(esConfig.collect { key, value -> "${key}: ${value}" }.join('\n'), 'UTF-8')
@@ -681,6 +701,19 @@ class ClusterFormationTasks {
681701
static Task configureWaitTask(String name, Project project, List<NodeInfo> nodes, List<Task> startTasks, int waitSeconds) {
682702
Task wait = project.tasks.create(name: name, dependsOn: startTasks)
683703
wait.doLast {
704+
705+
Collection<String> unicastHosts = new HashSet<>()
706+
nodes.forEach { otherNode ->
707+
String unicastHost = otherNode.config.unicastTransportUri(otherNode, null, project.ant)
708+
if (unicastHost != null) {
709+
unicastHosts.addAll(Arrays.asList(unicastHost.split(",")))
710+
}
711+
}
712+
String unicastHostsTxt = String.join("\n", unicastHosts)
713+
nodes.forEach { node ->
714+
node.pathConf.toPath().resolve("unicast_hosts.txt").setText(unicastHostsTxt)
715+
}
716+
684717
ant.waitfor(maxwait: "${waitSeconds}", maxwaitunit: 'second', checkevery: '500', checkeveryunit: 'millisecond', timeoutproperty: "failed${name}") {
685718
or {
686719
for (NodeInfo node : nodes) {

0 commit comments

Comments
 (0)