Skip to content

Commit 4351ec5

Browse files
JeremyDahlgrengmjehovich
authored andcommitted
Move RCS settings-based test method to unit test (elastic#134668)
Moves the test-only settings-based updateRemoteCluster() method to the RemoteClusterService unit test class. Part of work to eliminate remaining settings-based methods in RemoteClusterService. Relates: ES-12864
1 parent 4fe10f3 commit 4351ec5

File tree

2 files changed

+55
-64
lines changed

2 files changed

+55
-64
lines changed

server/src/main/java/org/elasticsearch/transport/RemoteClusterService.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -405,23 +405,15 @@ public void onFailure(Exception e) {
405405
}
406406
}
407407

408-
// Package-access for testing.
409-
@FixForMultiProject(description = "Refactor to supply the project ID associated with the alias and settings, or eliminate this method.")
410-
void updateRemoteCluster(String clusterAlias, Settings newSettings, ActionListener<RemoteClusterConnectionStatus> listener) {
411-
final var mergedSettings = Settings.builder().put(settings, false).put(newSettings, false).build();
412-
@FixForMultiProject(description = "Refactor to add the linked project ID associated with the alias.")
413-
final var config = RemoteClusterSettings.toConfig(projectResolver.getProjectId(), ProjectId.DEFAULT, clusterAlias, mergedSettings);
414-
updateRemoteCluster(config, false, listener);
415-
}
416-
417408
/**
418409
* Adds, rebuilds, or closes and removes the connection for the specified remote cluster.
419410
*
420411
* @param config The linked project configuration.
421412
* @param forceRebuild Forces an existing connection to be closed and reconnected even if the connection strategy does not require it.
422413
* @param listener The listener invoked once the configured cluster has been connected.
423414
*/
424-
private synchronized void updateRemoteCluster(
415+
// Package-access for testing.
416+
synchronized void updateRemoteCluster(
425417
LinkedProjectConfig config,
426418
boolean forceRebuild,
427419
ActionListener<RemoteClusterConnectionStatus> listener

server/src/test/java/org/elasticsearch/transport/RemoteClusterServiceTests.java

Lines changed: 53 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@
1717
import org.elasticsearch.action.support.ActionTestUtils;
1818
import org.elasticsearch.action.support.IndicesOptions;
1919
import org.elasticsearch.action.support.PlainActionFuture;
20+
import org.elasticsearch.cluster.metadata.ProjectId;
2021
import org.elasticsearch.cluster.node.DiscoveryNode;
2122
import org.elasticsearch.cluster.node.DiscoveryNodeRole;
2223
import org.elasticsearch.cluster.node.VersionInformation;
2324
import org.elasticsearch.cluster.project.DefaultProjectResolver;
25+
import org.elasticsearch.cluster.project.ProjectResolver;
2426
import org.elasticsearch.common.Strings;
2527
import org.elasticsearch.common.settings.AbstractScopedSettings;
2628
import org.elasticsearch.common.settings.ClusterSettings;
2729
import org.elasticsearch.common.settings.MockSecureSettings;
2830
import org.elasticsearch.common.settings.Settings;
2931
import org.elasticsearch.common.util.set.Sets;
32+
import org.elasticsearch.core.FixForMultiProject;
3033
import org.elasticsearch.core.IOUtils;
3134
import org.elasticsearch.core.TimeValue;
3235
import org.elasticsearch.node.Node;
@@ -67,6 +70,7 @@
6770
public class RemoteClusterServiceTests extends ESTestCase {
6871

6972
private final ThreadPool threadPool = new TestThreadPool(getClass().getName());
73+
private final ProjectResolver projectResolver = DefaultProjectResolver.INSTANCE;
7074
private LinkedProjectConfigService linkedProjectConfigService = null;
7175

7276
@Override
@@ -84,12 +88,8 @@ private RemoteClusterService createRemoteClusterService(
8488
ClusterSettings clusterSettings,
8589
MockTransportService transportService
8690
) {
87-
linkedProjectConfigService = new ClusterSettingsLinkedProjectConfigService(
88-
settings,
89-
clusterSettings,
90-
DefaultProjectResolver.INSTANCE
91-
);
92-
return new RemoteClusterService(settings, transportService, DefaultProjectResolver.INSTANCE);
91+
linkedProjectConfigService = new ClusterSettingsLinkedProjectConfigService(settings, clusterSettings, projectResolver);
92+
return new RemoteClusterService(settings, transportService, projectResolver);
9393
}
9494

9595
private MockTransportService startTransport(
@@ -776,19 +776,11 @@ public void testRemoteNodeAttribute() throws IOException, InterruptedException {
776776
assertFalse(hasRegisteredClusters(service));
777777

778778
final CountDownLatch firstLatch = new CountDownLatch(1);
779-
service.updateRemoteCluster(
780-
"cluster_1",
781-
createSettings("cluster_1", Arrays.asList(c1N1Node.getAddress().toString(), c1N2Node.getAddress().toString())),
782-
connectionListener(firstLatch)
783-
);
779+
updateRemoteCluster(service, "cluster_1", settings, List.of(c1N1Node, c1N2Node), connectionListener(firstLatch));
784780
firstLatch.await();
785781

786782
final CountDownLatch secondLatch = new CountDownLatch(1);
787-
service.updateRemoteCluster(
788-
"cluster_2",
789-
createSettings("cluster_2", Arrays.asList(c2N1Node.getAddress().toString(), c2N2Node.getAddress().toString())),
790-
connectionListener(secondLatch)
791-
);
783+
updateRemoteCluster(service, "cluster_2", settings, List.of(c2N1Node, c2N2Node), connectionListener(secondLatch));
792784
secondLatch.await();
793785

794786
assertTrue(hasRegisteredClusters(service));
@@ -866,19 +858,11 @@ public void testRemoteNodeRoles() throws IOException, InterruptedException {
866858
assertFalse(hasRegisteredClusters(service));
867859

868860
final CountDownLatch firstLatch = new CountDownLatch(1);
869-
service.updateRemoteCluster(
870-
"cluster_1",
871-
createSettings("cluster_1", Arrays.asList(c1N1Node.getAddress().toString(), c1N2Node.getAddress().toString())),
872-
connectionListener(firstLatch)
873-
);
861+
updateRemoteCluster(service, "cluster_1", settings, List.of(c1N1Node, c1N2Node), connectionListener(firstLatch));
874862
firstLatch.await();
875863

876864
final CountDownLatch secondLatch = new CountDownLatch(1);
877-
service.updateRemoteCluster(
878-
"cluster_2",
879-
createSettings("cluster_2", Arrays.asList(c2N1Node.getAddress().toString(), c2N2Node.getAddress().toString())),
880-
connectionListener(secondLatch)
881-
);
865+
updateRemoteCluster(service, "cluster_2", settings, List.of(c2N1Node, c2N2Node), connectionListener(secondLatch));
882866
secondLatch.await();
883867

884868
assertTrue(hasRegisteredClusters(service));
@@ -961,20 +945,11 @@ public void testCollectNodes() throws InterruptedException, IOException {
961945
assertFalse(hasRegisteredClusters(service));
962946

963947
final CountDownLatch firstLatch = new CountDownLatch(1);
964-
965-
service.updateRemoteCluster(
966-
"cluster_1",
967-
createSettings("cluster_1", Arrays.asList(c1N1Node.getAddress().toString(), c1N2Node.getAddress().toString())),
968-
connectionListener(firstLatch)
969-
);
948+
updateRemoteCluster(service, "cluster_1", settings, List.of(c1N1Node, c1N2Node), connectionListener(firstLatch));
970949
firstLatch.await();
971950

972951
final CountDownLatch secondLatch = new CountDownLatch(1);
973-
service.updateRemoteCluster(
974-
"cluster_2",
975-
createSettings("cluster_2", Arrays.asList(c2N1Node.getAddress().toString(), c2N2Node.getAddress().toString())),
976-
connectionListener(secondLatch)
977-
);
952+
updateRemoteCluster(service, "cluster_2", settings, List.of(c2N1Node, c2N2Node), connectionListener(secondLatch));
978953
secondLatch.await();
979954
CountDownLatch latch = new CountDownLatch(1);
980955
service.collectNodes(
@@ -1108,8 +1083,9 @@ public void testCollectNodesConcurrentWithSettingsChanges() throws IOException {
11081083
final var seedList = List.of(c1N1Node.getAddress().toString());
11091084
transportService.start();
11101085
transportService.acceptIncomingRequests();
1086+
final var initialSettings = createSettings("cluster_1", seedList);
11111087

1112-
try (RemoteClusterService service = createRemoteClusterService(createSettings("cluster_1", seedList), transportService)) {
1088+
try (RemoteClusterService service = createRemoteClusterService(initialSettings, transportService)) {
11131089
initializeRemoteClusters(service);
11141090
assertTrue(hasRegisteredClusters(service));
11151091
final var numTasks = between(3, 5);
@@ -1122,7 +1098,7 @@ public void testCollectNodesConcurrentWithSettingsChanges() throws IOException {
11221098
while (taskLatch.getCount() != 0) {
11231099
final var future = new PlainActionFuture<RemoteClusterService.RemoteClusterConnectionStatus>();
11241100
final var settings = createSettings("cluster_1", isLinked ? Collections.emptyList() : seedList);
1125-
service.updateRemoteCluster("cluster_1", settings, future);
1101+
updateRemoteCluster(service, "cluster_1", initialSettings, settings, future);
11261102
safeGet(future);
11271103
isLinked = isLinked == false;
11281104
}
@@ -1296,7 +1272,8 @@ public void testReconnectWhenStrategySettingsUpdated() throws Exception {
12961272

12971273
final Settings.Builder builder = Settings.builder();
12981274
builder.putList("cluster.remote.cluster_test.seeds", Collections.singletonList(node0.getAddress().toString()));
1299-
try (RemoteClusterService service = createRemoteClusterService(builder.build(), transportService)) {
1275+
final var initialSettings = builder.build();
1276+
try (RemoteClusterService service = createRemoteClusterService(initialSettings, transportService)) {
13001277
assertFalse(hasRegisteredClusters(service));
13011278
initializeRemoteClusters(service);
13021279
assertTrue(hasRegisteredClusters(service));
@@ -1308,11 +1285,7 @@ public void testReconnectWhenStrategySettingsUpdated() throws Exception {
13081285
assertFalse(firstRemoteClusterConnection.isClosed());
13091286

13101287
final CountDownLatch firstLatch = new CountDownLatch(1);
1311-
service.updateRemoteCluster(
1312-
"cluster_test",
1313-
createSettings("cluster_test", Collections.singletonList(node0.getAddress().toString())),
1314-
connectionListener(firstLatch)
1315-
);
1288+
updateRemoteCluster(service, "cluster_test", initialSettings, List.of(node0), connectionListener(firstLatch));
13161289
firstLatch.await();
13171290

13181291
assertTrue(hasRegisteredClusters(service));
@@ -1322,15 +1295,15 @@ public void testReconnectWhenStrategySettingsUpdated() throws Exception {
13221295
assertFalse(firstRemoteClusterConnection.isClosed());
13231296
assertSame(firstRemoteClusterConnection, service.getRemoteClusterConnection("cluster_test"));
13241297

1325-
final List<String> newSeeds = new ArrayList<>();
1326-
newSeeds.add(node1.getAddress().toString());
1298+
final List<DiscoveryNode> newSeeds = new ArrayList<>();
1299+
newSeeds.add(node1);
13271300
if (randomBoolean()) {
1328-
newSeeds.add(node0.getAddress().toString());
1301+
newSeeds.add(node0);
13291302
Collections.shuffle(newSeeds, random());
13301303
}
13311304

13321305
final CountDownLatch secondLatch = new CountDownLatch(1);
1333-
service.updateRemoteCluster("cluster_test", createSettings("cluster_test", newSeeds), connectionListener(secondLatch));
1306+
updateRemoteCluster(service, "cluster_test", initialSettings, newSeeds, connectionListener(secondLatch));
13341307
secondLatch.await();
13351308

13361309
assertTrue(hasRegisteredClusters(service));
@@ -1575,7 +1548,8 @@ public void testUseDifferentTransportProfileForCredentialsProtectedRemoteCluster
15751548
} else {
15761549
firstRemoteClusterSettingsBuilder.put("cluster.remote.cluster_1.seeds", c1Node.getAddress().toString());
15771550
}
1578-
service.updateRemoteCluster("cluster_1", firstRemoteClusterSettingsBuilder.build(), connectionListener(firstLatch));
1551+
final var updatedSettings1 = firstRemoteClusterSettingsBuilder.build();
1552+
updateRemoteCluster(service, "cluster_1", settings, updatedSettings1, connectionListener(firstLatch));
15791553
firstLatch.await();
15801554

15811555
final CountDownLatch secondLatch = new CountDownLatch(1);
@@ -1587,7 +1561,8 @@ public void testUseDifferentTransportProfileForCredentialsProtectedRemoteCluster
15871561
} else {
15881562
secondRemoteClusterSettingsBuilder.put("cluster.remote.cluster_2.seeds", c2Node.getAddress().toString());
15891563
}
1590-
service.updateRemoteCluster("cluster_2", secondRemoteClusterSettingsBuilder.build(), connectionListener(secondLatch));
1564+
final var updatedSettings2 = secondRemoteClusterSettingsBuilder.build();
1565+
updateRemoteCluster(service, "cluster_2", settings, updatedSettings2, connectionListener(secondLatch));
15911566
secondLatch.await();
15921567

15931568
assertTrue(hasRegisteredClusters(service));
@@ -1642,7 +1617,7 @@ public void testUpdateRemoteClusterCredentialsRebuildsConnectionWithCorrectProfi
16421617

16431618
final Settings clusterSettings = buildRemoteClusterSettings("cluster_1", discoNode.getAddress().toString());
16441619
final CountDownLatch latch = new CountDownLatch(1);
1645-
service.updateRemoteCluster("cluster_1", clusterSettings, connectionListener(latch));
1620+
updateRemoteCluster(service, "cluster_1", Settings.EMPTY, clusterSettings, connectionListener(latch));
16461621
latch.await();
16471622

16481623
assertConnectionHasProfile(service.getRemoteClusterConnection("cluster_1"), "default");
@@ -1731,12 +1706,12 @@ public void testUpdateRemoteClusterCredentialsRebuildsMultipleConnectionsDespite
17311706

17321707
final Settings cluster1Settings = buildRemoteClusterSettings(goodCluster, c1DiscoNode.getAddress().toString());
17331708
final var latch = new CountDownLatch(1);
1734-
service.updateRemoteCluster(goodCluster, cluster1Settings, connectionListener(latch));
1709+
updateRemoteCluster(service, goodCluster, Settings.EMPTY, cluster1Settings, connectionListener(latch));
17351710
latch.await();
17361711

17371712
final Settings cluster2Settings = buildRemoteClusterSettings(badCluster, c2DiscoNode.getAddress().toString());
17381713
final PlainActionFuture<RemoteClusterService.RemoteClusterConnectionStatus> future = new PlainActionFuture<>();
1739-
service.updateRemoteCluster(badCluster, cluster2Settings, future);
1714+
updateRemoteCluster(service, badCluster, Settings.EMPTY, cluster2Settings, future);
17401715
final var ex = expectThrows(Exception.class, () -> future.actionGet(10, TimeUnit.SECONDS));
17411716
assertThat(ex.getMessage(), containsString("bad cluster"));
17421717

@@ -1853,6 +1828,30 @@ public void testLogsConnectionResult() throws IOException {
18531828
}
18541829
}
18551830

1831+
private void updateRemoteCluster(
1832+
RemoteClusterService service,
1833+
String alias,
1834+
Settings settings,
1835+
List<DiscoveryNode> seedNodes,
1836+
ActionListener<RemoteClusterService.RemoteClusterConnectionStatus> listener
1837+
) {
1838+
final var newSettings = createSettings(alias, seedNodes.stream().map(n -> n.getAddress().toString()).toList());
1839+
updateRemoteCluster(service, alias, settings, newSettings, listener);
1840+
}
1841+
1842+
private void updateRemoteCluster(
1843+
RemoteClusterService service,
1844+
String alias,
1845+
Settings settings,
1846+
Settings newSettings,
1847+
ActionListener<RemoteClusterService.RemoteClusterConnectionStatus> listener
1848+
) {
1849+
final var mergedSettings = Settings.builder().put(settings, false).put(newSettings, false).build();
1850+
@FixForMultiProject(description = "Refactor to add the linked project ID associated with the alias.")
1851+
final var config = RemoteClusterSettings.toConfig(projectResolver.getProjectId(), ProjectId.DEFAULT, alias, mergedSettings);
1852+
service.updateRemoteCluster(config, false, listener);
1853+
}
1854+
18561855
private void initializeRemoteClusters(RemoteClusterService remoteClusterService) {
18571856
remoteClusterService.initializeRemoteClusters(linkedProjectConfigService.getInitialLinkedProjectConfigs());
18581857
}

0 commit comments

Comments
 (0)