forked from opensearch-project/anomaly-detection
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding remote index and multi index checks in validation
Signed-off-by: Amit Galitzky <[email protected]>
- Loading branch information
1 parent
2922bbd
commit 73ca471
Showing
12 changed files
with
348 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
218 changes: 142 additions & 76 deletions
218
src/main/java/org/opensearch/timeseries/rest/handler/AbstractTimeSeriesActionHandler.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/main/java/org/opensearch/timeseries/util/CrossClusterConfigUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.timeseries.util; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.client.Client; | ||
import org.opensearch.client.node.NodeClient; | ||
import org.opensearch.cluster.service.ClusterService; | ||
|
||
public class CrossClusterConfigUtils { | ||
private static final Logger logger = LogManager.getLogger(ParseUtils.class); | ||
|
||
/** | ||
* Uses the clusterName to determine whether the target client is the local or a remote client, | ||
* and returns the appropriate client. | ||
* @param clusterName The name of the cluster to evaluate. | ||
* @param client The local {@link NodeClient}. | ||
* @param localClusterName The name of the local cluster. | ||
* @return The local {@link NodeClient} for the local cluster, or a remote client for a remote cluster. | ||
*/ | ||
public static Client getClientForCluster(String clusterName, Client client, String localClusterName) { | ||
return clusterName.equals(localClusterName) ? client : client.getRemoteClusterClient(clusterName); | ||
} | ||
|
||
/** | ||
* Uses the clusterName to determine whether the target client is the local or a remote client, | ||
* and returns the appropriate client. | ||
* @param clusterName The name of the cluster to evaluate. | ||
* @param client The local {@link NodeClient}. | ||
* @param clusterService Used to retrieve the name of the local cluster. | ||
* @return The local {@link NodeClient} for the local cluster, or a remote client for a remote cluster. | ||
*/ | ||
public static Client getClientForCluster(String clusterName, Client client, ClusterService clusterService) { | ||
logger.info("clusterName1: " + clusterName); | ||
logger.info("clusterService.getClusterName().value(): " + clusterService.getClusterName().value()); | ||
|
||
return getClientForCluster(clusterName, client, clusterService.getClusterName().value()); | ||
} | ||
|
||
/** | ||
* Parses the list of indexes into a map of cluster_name to List of index names | ||
* @param indexes A list of index names in cluster_name:index_name format. | ||
* Local indexes can also be in index_name format. | ||
* @param clusterService Used to retrieve the name of the local cluster. | ||
* @return A map of cluster_name:index names | ||
*/ | ||
public static HashMap<String, List<String>> separateClusterIndexes(List<String> indexes, ClusterService clusterService) { | ||
return separateClusterIndexes(indexes, clusterService.getClusterName().value()); | ||
} | ||
|
||
/** | ||
* Parses the list of indexes into a map of cluster_name to list of index_name | ||
* @param indexes A list of index names in cluster_name:index_name format. | ||
* @param localClusterName The name of the local cluster. | ||
* @return A map of cluster_name to List index_name | ||
*/ | ||
public static HashMap<String, List<String>> separateClusterIndexes(List<String> indexes, String localClusterName) { | ||
HashMap<String, List<String>> output = new HashMap<>(); | ||
for (String index : indexes) { | ||
String clusterName = parseClusterName(index); | ||
String indexName = parseIndexName(index); | ||
|
||
// If the index entry does not have a cluster_name, it indicates the index is on the local cluster. | ||
if (clusterName.isEmpty()) { | ||
clusterName = localClusterName; | ||
} | ||
output.computeIfAbsent(clusterName, k -> new ArrayList<>()).add(indexName); | ||
} | ||
return output; | ||
} | ||
|
||
/** | ||
* @param index The name of the index to evaluate. | ||
* Can be in either cluster_name:index_name or index_name format. | ||
* @return The index name. | ||
*/ | ||
public static String parseIndexName(String index) { | ||
if (index.contains(":")) { | ||
String[] parts = index.split(":"); | ||
return parts.length > 1 ? parts[1] : index; | ||
} else { | ||
return index; | ||
} | ||
} | ||
|
||
/** | ||
* @param index The name of the index to evaluate. | ||
* Can be in either cluster_name:index_name or index_name format. | ||
* @return The index name. | ||
*/ | ||
public static String parseClusterName(String index) { | ||
return index.contains(":") ? index.substring(0, index.indexOf(':')) : ""; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/test/java/org/opensearch/timeseries/util/CrossClusterConfigUtilsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.opensearch.timeseries.util; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
import org.mockito.Mock; | ||
import org.opensearch.client.Client; | ||
import org.opensearch.client.node.NodeClient; | ||
import org.opensearch.cluster.ClusterName; | ||
import org.opensearch.cluster.service.ClusterService; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
public class CrossClusterConfigUtilsTests extends OpenSearchTestCase { | ||
|
||
@Mock | ||
private Client clientMock; | ||
|
||
public void testGetClientForClusterLocalCluster() { | ||
String clusterName = "localCluster"; | ||
Client mockClient = mock(NodeClient.class); | ||
String localClusterName = "localCluster"; | ||
|
||
Client result = CrossClusterConfigUtils.getClientForCluster(clusterName, mockClient, localClusterName); | ||
|
||
assertEquals(mockClient, result); | ||
} | ||
|
||
public void testGetClientForClusterRemoteCluster() { | ||
String clusterName = "remoteCluster"; | ||
Client mockClient = mock(NodeClient.class); | ||
// Client mockRemoteClient = mock(Client.class); | ||
|
||
when(mockClient.getRemoteClusterClient(clusterName)).thenReturn(mockClient); | ||
|
||
Client result = CrossClusterConfigUtils.getClientForCluster(clusterName, mockClient, "localCluster"); | ||
|
||
assertEquals(mockClient, result); | ||
} | ||
|
||
public void testSeparateClusterIndexesRemoteCluster() { | ||
List<String> indexes = Arrays.asList("remoteCluster:index1", "index2"); | ||
ClusterService mockClusterService = mock(ClusterService.class); | ||
when(mockClusterService.getClusterName()).thenReturn(new ClusterName("localCluster")); | ||
|
||
HashMap<String, List<String>> result = CrossClusterConfigUtils.separateClusterIndexes(indexes, mockClusterService); | ||
|
||
assertEquals(2, result.size()); | ||
assertEquals(Arrays.asList("index1"), result.get("remoteCluster")); | ||
assertEquals(Arrays.asList("index2"), result.get("localCluster")); | ||
} | ||
|
||
public void testParseIndexName() { | ||
assertEquals("index1", CrossClusterConfigUtils.parseIndexName("remoteCluster:index1")); | ||
assertEquals("index2", CrossClusterConfigUtils.parseIndexName("index2")); | ||
} | ||
|
||
public void testParseClusterName() { | ||
assertEquals("remoteCluster", CrossClusterConfigUtils.parseClusterName("remoteCluster:index1")); | ||
assertEquals("", CrossClusterConfigUtils.parseClusterName("index2")); | ||
} | ||
} |