-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding remote index and multi-index checks in validation #1290
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you make one call and return a Pair instead of calling similar logic twice? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed |
||
|
||
// 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(':')) : ""; | ||
} | ||
} |
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")); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these logs still needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed