Skip to content

Commit 53df199

Browse files
committed
updates naming & adds UTs to doesMultiTenantIndexExist
Signed-off-by: Brian Flores <[email protected]>
1 parent 9134666 commit 53df199

File tree

7 files changed

+39
-7
lines changed

7 files changed

+39
-7
lines changed

ml-algorithms/src/main/java/org/opensearch/ml/engine/algorithms/agent/MLAgentExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public void execute(Input input, ActionListener<Output> listener) {
174174
.fetchSourceContext(fetchSourceContext)
175175
.build();
176176

177-
if (MLIndicesHandler.doesMultiTenantIndexExists(clusterService, mlFeatureEnabledSetting.isMultiTenancyEnabled(), ML_AGENT_INDEX)) {
177+
if (MLIndicesHandler.doesMultiTenantIndexExist(clusterService, mlFeatureEnabledSetting.isMultiTenancyEnabled(), ML_AGENT_INDEX)) {
178178
try (ThreadContext.StoredContext context = client.threadPool().getThreadContext().stashContext()) {
179179
sdkClient
180180
.getDataObjectAsync(getDataObjectRequest, client.threadPool().executor("opensearch_ml_general"))

ml-algorithms/src/main/java/org/opensearch/ml/engine/indices/MLIndicesHandler.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import org.opensearch.ml.common.settings.MLFeatureEnabledSetting;
3434
import org.opensearch.transport.client.Client;
3535

36+
import com.google.common.annotations.VisibleForTesting;
37+
3638
import lombok.AccessLevel;
3739
import lombok.RequiredArgsConstructor;
3840
import lombok.experimental.FieldDefaults;
@@ -65,7 +67,7 @@ public class MLIndicesHandler {
6567
* @implNote It is recommended that if your environment enables multi tenancy, your plugin indices are
6668
* pre-populated otherwise this method will result in unwanted early returns without checking the clusterService
6769
*/
68-
public static boolean doesMultiTenantIndexExists(ClusterService clusterService, boolean isMultiTenancyEnabled, String indexName) {
70+
public static boolean doesMultiTenantIndexExist(ClusterService clusterService, boolean isMultiTenancyEnabled, String indexName) {
6971
return isMultiTenancyEnabled || clusterService.state().metadata().hasIndex(indexName);
7072
}
7173

@@ -122,7 +124,7 @@ public void initMLIndexIfAbsent(MLIndex index, ActionListener<Boolean> listener)
122124
String mapping = index.getMapping();
123125
try (ThreadContext.StoredContext threadContext = client.threadPool().getThreadContext().stashContext()) {
124126
ActionListener<Boolean> internalListener = ActionListener.runBefore(listener, () -> threadContext.restore());
125-
if (!MLIndicesHandler.doesMultiTenantIndexExists(clusterService, mlFeatureEnabledSetting.isMultiTenancyEnabled(), indexName)) {
127+
if (!MLIndicesHandler.doesMultiTenantIndexExist(clusterService, mlFeatureEnabledSetting.isMultiTenancyEnabled(), indexName)) {
126128
ActionListener<CreateIndexResponse> actionListener = ActionListener.wrap(r -> {
127129
if (r.isAcknowledged()) {
128130
log.info("create index:{}", indexName);
@@ -237,4 +239,9 @@ public void shouldUpdateIndex(String indexName, Integer newVersion, ActionListen
237239
listener.onResponse(newVersion > oldVersion);
238240
}
239241

242+
@VisibleForTesting
243+
public boolean doesIndexExists(String indexName) {
244+
return MLIndicesHandler.doesMultiTenantIndexExist(clusterService, mlFeatureEnabledSetting.isMultiTenancyEnabled(), indexName);
245+
}
246+
240247
}

ml-algorithms/src/test/java/org/opensearch/ml/engine/indices/MLIndicesHandlerTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.opensearch.ml.engine.indices;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertTrue;
46
import static org.mockito.ArgumentMatchers.any;
57
import static org.mockito.ArgumentMatchers.anyString;
68
import static org.mockito.ArgumentMatchers.isA;
@@ -11,6 +13,7 @@
1113
import static org.mockito.Mockito.when;
1214
import static org.opensearch.ml.common.CommonValue.META;
1315
import static org.opensearch.ml.common.CommonValue.ML_AGENT_INDEX;
16+
import static org.opensearch.ml.common.CommonValue.ML_CONFIG_INDEX;
1417
import static org.opensearch.ml.common.CommonValue.ML_JOBS_INDEX;
1518
import static org.opensearch.ml.common.CommonValue.ML_MEMORY_MESSAGE_INDEX;
1619
import static org.opensearch.ml.common.CommonValue.ML_MEMORY_META_INDEX;
@@ -109,6 +112,28 @@ public void setUp() {
109112
indicesHandler = new MLIndicesHandler(clusterService, client, mlFeatureEnabledSetting);
110113
}
111114

115+
@Test
116+
public void doesMultiTenantIndexExist_multiTenancyEnabled_returnsTrue() {
117+
assertTrue(MLIndicesHandler.doesMultiTenantIndexExist(null, true, null));
118+
MLIndicesHandler mlIndicesHandler = new MLIndicesHandler(clusterService, client, mlFeatureEnabledSetting);
119+
assertTrue(mlIndicesHandler.doesIndexExists(ML_CONFIG_INDEX));
120+
}
121+
122+
@Test
123+
public void doesMultiTenantIndexExist_multiTenancyDisabledSearchesClusterService_returnsValidSearchResult() {
124+
assertFalse(MLIndicesHandler.doesMultiTenantIndexExist(clusterService, false, null));
125+
126+
String sampleIndexName = "test-index";
127+
when(mlFeatureEnabledSetting.isMultiTenancyEnabled()).thenReturn(false);
128+
MLIndicesHandler mlIndicesHandler = new MLIndicesHandler(clusterService, client, mlFeatureEnabledSetting);
129+
130+
when(clusterService.state().metadata().hasIndex(sampleIndexName)).thenReturn(true);
131+
assertTrue(mlIndicesHandler.doesIndexExists(sampleIndexName));
132+
133+
when(clusterService.state().metadata().hasIndex(sampleIndexName)).thenReturn(false);
134+
assertFalse(mlIndicesHandler.doesIndexExists(sampleIndexName));
135+
}
136+
112137
@Test
113138
public void initMemoryMetaIndex() {
114139
ActionListener<Boolean> listener = mock(ActionListener.class);

plugin/src/main/java/org/opensearch/ml/action/connector/ExecuteConnectorTransportAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected void doExecute(Task task, ActionRequest request, ActionListener<MLTask
7676
String connectorAction = ConnectorAction.ActionType.EXECUTE.name();
7777

7878
if (MLIndicesHandler
79-
.doesMultiTenantIndexExists(clusterService, mlFeatureEnabledSetting.isMultiTenancyEnabled(), ML_CONNECTOR_INDEX)) {
79+
.doesMultiTenantIndexExist(clusterService, mlFeatureEnabledSetting.isMultiTenancyEnabled(), ML_CONNECTOR_INDEX)) {
8080
ActionListener<Connector> listener = ActionListener.wrap(connector -> {
8181
if (connectorAccessControlHelper.validateConnectorAccess(client, connector)) {
8282
// adding tenantID as null, because we are not implement multi-tenancy for this feature yet.

plugin/src/main/java/org/opensearch/ml/action/handler/MLSearchHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public void search(SdkClient sdkClient, SearchRequest request, String tenantId,
138138
.wrap(wrappedListener::onResponse, e -> wrapListenerToHandleSearchIndexNotFound(e, wrappedListener));
139139
if (modelAccessControlHelper.skipModelAccessControl(user)
140140
|| !MLIndicesHandler
141-
.doesMultiTenantIndexExists(
141+
.doesMultiTenantIndexExist(
142142
clusterService,
143143
mlFeatureEnabledSetting.isMultiTenancyEnabled(),
144144
CommonValue.ML_MODEL_GROUP_INDEX

plugin/src/main/java/org/opensearch/ml/action/tasks/CancelBatchJobTransportAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ private void processRemoteBatchPrediction(MLTask mlTask, ActionListener<MLCancel
201201
Connector connector = model.getConnector();
202202
executeConnector(connector, mlInput, actionListener);
203203
} else if (MLIndicesHandler
204-
.doesMultiTenantIndexExists(
204+
.doesMultiTenantIndexExist(
205205
clusterService,
206206
mlFeatureEnabledSetting.isMultiTenancyEnabled(),
207207
ML_CONNECTOR_INDEX

plugin/src/main/java/org/opensearch/ml/action/tasks/GetTaskTransportAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ private void processRemoteBatchPrediction(
392392
actionListener
393393
);
394394
} else if (MLIndicesHandler
395-
.doesMultiTenantIndexExists(
395+
.doesMultiTenantIndexExist(
396396
clusterService,
397397
mlFeatureEnabledSetting.isMultiTenancyEnabled(),
398398
ML_CONNECTOR_INDEX

0 commit comments

Comments
 (0)