Skip to content

Commit ad99a70

Browse files
committed
distinguish local cluster when name is same as remote
Signed-off-by: Amit Galitzky <amgalitz@amazon.com>
1 parent 5e3577e commit ad99a70

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
2121
### Enhancements
2222
- Github workflow for changelog verification ([#1413](https://github.com/opensearch-project/anomaly-detection/pull/1413))
2323
### Bug Fixes
24+
- Distinguish local cluster when local name is same as remote ([#1446](https://github.com/opensearch-project/anomaly-detection/pull/1446))
2425
### Infrastructure
2526
### Documentation
2627
### Maintenance

src/main/java/org/opensearch/timeseries/util/CrossClusterConfigUtils.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public class CrossClusterConfigUtils {
2828
* @return The local {@link NodeClient} for the local cluster, or a remote client for a remote cluster.
2929
*/
3030
public static Client getClientForCluster(String clusterName, Client client, String localClusterName) {
31-
return clusterName.equals(localClusterName) ? client : client.getRemoteClusterClient(clusterName);
31+
return clusterName.contains("#local") && clusterName.split("#")[0].equals(localClusterName)
32+
? client
33+
: client.getRemoteClusterClient(clusterName);
3234
}
3335

3436
/**
@@ -68,9 +70,9 @@ public static HashMap<String, List<String>> separateClusterIndexes(List<String>
6870
String clusterName = clusterAndIndex.getKey();
6971
String indexName = clusterAndIndex.getValue();
7072

71-
// If the index entry does not have a cluster_name, it indicates the index is on the local cluster.
7273
if (clusterName.isEmpty()) {
73-
clusterName = localClusterName;
74+
// Use #local marker to indicate local cluster to avoid clashing if local cluster has the same name as remote cluster
75+
clusterName = localClusterName + "#local";
7476
}
7577
output.computeIfAbsent(clusterName, k -> new ArrayList<>()).add(indexName);
7678
}

src/test/java/org/opensearch/timeseries/util/CrossClusterConfigUtilsTests.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,22 @@ public void testSeparateClusterIndexesRemoteCluster() {
6262

6363
assertEquals(3, result.size());
6464
assertEquals(Arrays.asList("index1"), result.get("remoteCluster"));
65-
assertEquals(Arrays.asList("index2"), result.get("localCluster"));
65+
assertEquals(Arrays.asList("index2"), result.get("localCluster#local"));
6666
assertEquals(Arrays.asList("index2"), result.get("remoteCluster2"));
6767
}
6868

69+
public void testSeparateClusterIndexesWithSameRemoteClusterName() {
70+
List<String> indexes = Arrays.asList("opensearch:index1", "index2");
71+
ClusterService mockClusterService = mock(ClusterService.class);
72+
when(mockClusterService.getClusterName()).thenReturn(new ClusterName("opensearch"));
73+
74+
HashMap<String, List<String>> result = CrossClusterConfigUtils.separateClusterIndexes(indexes, mockClusterService);
75+
76+
assertEquals(2, result.size());
77+
assertEquals(Arrays.asList("index1"), result.get("opensearch"));
78+
assertEquals(Arrays.asList("index2"), result.get("opensearch#local"));
79+
}
80+
6981
public void testParseClusterAndIndexName_WithClusterAndIndex() {
7082
String input = "clusterA:index1";
7183
Pair<String, String> result = CrossClusterConfigUtils.parseClusterAndIndexName(input);

0 commit comments

Comments
 (0)