Skip to content

Commit 2916b8d

Browse files
Add feature flag to delete mem container (opensearch-project#4072)
Signed-off-by: Nathalie Jonathan <[email protected]>
1 parent de3df61 commit 2916b8d

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

plugin/src/main/java/org/opensearch/ml/action/memorycontainer/TransportDeleteMemoryContainerAction.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package org.opensearch.ml.action.memorycontainer;
77

88
import static org.opensearch.ml.common.CommonValue.ML_MEMORY_CONTAINER_INDEX;
9+
import static org.opensearch.ml.common.settings.MLCommonsSettings.ML_COMMONS_AGENTIC_MEMORY_DISABLED_MESSAGE;
910

1011
import org.opensearch.OpenSearchStatusException;
1112
import org.opensearch.action.ActionRequest;
@@ -73,6 +74,11 @@ public TransportDeleteMemoryContainerAction(
7374

7475
@Override
7576
protected void doExecute(Task task, ActionRequest request, ActionListener<DeleteResponse> actionListener) {
77+
if (!mlFeatureEnabledSetting.isAgenticMemoryEnabled()) {
78+
actionListener.onFailure(new OpenSearchStatusException(ML_COMMONS_AGENTIC_MEMORY_DISABLED_MESSAGE, RestStatus.FORBIDDEN));
79+
return;
80+
}
81+
7682
MLMemoryContainerDeleteRequest deleteRequest = MLMemoryContainerDeleteRequest.fromActionRequest(request);
7783
String memoryContainerId = deleteRequest.getMemoryContainerId();
7884
String tenantId = deleteRequest.getTenantId();

plugin/src/main/java/org/opensearch/ml/rest/RestMLDeleteMemoryContainerAction.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77

88
import static org.opensearch.ml.common.memorycontainer.MemoryContainerConstants.BASE_MEMORY_CONTAINERS_PATH;
99
import static org.opensearch.ml.common.memorycontainer.MemoryContainerConstants.PARAMETER_MEMORY_CONTAINER_ID;
10+
import static org.opensearch.ml.common.settings.MLCommonsSettings.ML_COMMONS_AGENTIC_MEMORY_DISABLED_MESSAGE;
1011
import static org.opensearch.ml.utils.TenantAwareHelper.getTenantID;
1112

1213
import java.io.IOException;
1314
import java.util.List;
1415
import java.util.Locale;
1516

17+
import org.opensearch.OpenSearchStatusException;
18+
import org.opensearch.core.rest.RestStatus;
1619
import org.opensearch.ml.common.settings.MLFeatureEnabledSetting;
1720
import org.opensearch.ml.common.transport.memorycontainer.MLMemoryContainerDeleteAction;
1821
import org.opensearch.ml.common.transport.memorycontainer.MLMemoryContainerDeleteRequest;
@@ -53,6 +56,10 @@ public List<Route> routes() {
5356

5457
@Override
5558
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
59+
if (!mlFeatureEnabledSetting.isAgenticMemoryEnabled()) {
60+
throw new OpenSearchStatusException(ML_COMMONS_AGENTIC_MEMORY_DISABLED_MESSAGE, RestStatus.FORBIDDEN);
61+
}
62+
5663
String memoryContainerId = request.param(PARAMETER_MEMORY_CONTAINER_ID);
5764
String tenantId = getTenantID(mlFeatureEnabledSetting.isMultiTenancyEnabled(), request);
5865
MLMemoryContainerDeleteRequest mlMemoryContainerDeleteRequest = new MLMemoryContainerDeleteRequest(memoryContainerId, tenantId);

plugin/src/test/java/org/opensearch/ml/action/memorycontainer/TransportDeleteMemoryContainerActionTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.junit.Before;
2323
import org.junit.Test;
2424
import org.mockito.ArgumentCaptor;
25+
import org.mockito.Captor;
2526
import org.mockito.Mock;
2627
import org.mockito.MockitoAnnotations;
2728
import org.opensearch.OpenSearchStatusException;
@@ -97,6 +98,9 @@ public class TransportDeleteMemoryContainerActionTests extends OpenSearchTestCas
9798
@Mock
9899
private MemoryContainerHelper memoryContainerHelper;
99100

101+
@Captor
102+
private ArgumentCaptor<Exception> exceptionCaptor;
103+
100104
TransportDeleteMemoryContainerAction transportDeleteMemoryContainerAction;
101105
MLMemoryContainerDeleteRequest mlMemoryContainerDeleteRequest;
102106
ThreadContext threadContext;
@@ -145,6 +149,7 @@ public void setup() throws IOException {
145149
listener.onResponse(mockContainer);
146150
return null;
147151
}).when(memoryContainerHelper).getMemoryContainer(any(), any());
152+
when(mlFeatureEnabledSetting.isAgenticMemoryEnabled()).thenReturn(true);
148153

149154
threadContext = new ThreadContext(settings);
150155
when(client.threadPool()).thenReturn(threadPool);
@@ -297,4 +302,21 @@ public void testDeleteMemoryContainer_UnauthorizedUser() {
297302
)
298303
);
299304
}
305+
306+
public void testDoExecuteWithAgenticMemoryDisabled() throws InterruptedException {
307+
// Disable agentic memory feature
308+
when(mlFeatureEnabledSetting.isAgenticMemoryEnabled()).thenReturn(false);
309+
310+
// Execute
311+
transportDeleteMemoryContainerAction.doExecute(null, mlMemoryContainerDeleteRequest, actionListener);
312+
313+
// Verify failure response due to feature being disabled
314+
verify(actionListener).onFailure(exceptionCaptor.capture());
315+
Exception exception = exceptionCaptor.getValue();
316+
assertNotNull(exception);
317+
assertTrue(exception instanceof OpenSearchStatusException);
318+
OpenSearchStatusException statusException = (OpenSearchStatusException) exception;
319+
assertEquals(RestStatus.FORBIDDEN, statusException.status());
320+
assertEquals("The Agentic Memory APIs are not enabled. To enable, please update the setting plugins.ml_commons.agentic_memory_enabled", exception.getMessage());
321+
}
300322
}

plugin/src/test/java/org/opensearch/ml/rest/RestMLDeleteMemoryContainerActionTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.mockito.ArgumentCaptor;
2222
import org.mockito.Mock;
2323
import org.mockito.MockitoAnnotations;
24+
import org.opensearch.OpenSearchStatusException;
2425
import org.opensearch.action.delete.DeleteResponse;
2526
import org.opensearch.common.settings.Settings;
2627
import org.opensearch.core.action.ActionListener;
@@ -58,6 +59,7 @@ public class RestMLDeleteMemoryContainerActionTests extends OpenSearchTestCase {
5859
public void setup() {
5960
MockitoAnnotations.openMocks(this);
6061
when(mlFeatureEnabledSetting.isRemoteInferenceEnabled()).thenReturn(false);
62+
when(mlFeatureEnabledSetting.isAgenticMemoryEnabled()).thenReturn(true);
6163
restMLDeleteMemoryContainerAction = new RestMLDeleteMemoryContainerAction(mlFeatureEnabledSetting);
6264

6365
threadPool = new TestThreadPool(this.getClass().getSimpleName() + "ThreadPool");
@@ -145,6 +147,19 @@ public void testPrepareRequest_MultiTenancyDisabled() throws Exception {
145147
assertNull(mlMemoryContainerDeleteRequest.getTenantId());
146148
}
147149

150+
public void testPrepareRequestWithAgenticMemoryDisabled() throws Exception {
151+
// Disable agentic memory feature
152+
when(mlFeatureEnabledSetting.isAgenticMemoryEnabled()).thenReturn(false);
153+
154+
RestRequest request = getRestRequest("memory_container_id", "tenant_id");
155+
156+
// Expect OpenSearchStatusException when feature is disabled
157+
thrown.expect(OpenSearchStatusException.class);
158+
thrown.expectMessage("The Agentic Memory APIs are not enabled. To enable, please update the setting plugins.ml_commons.agentic_memory_enabled");
159+
160+
restMLDeleteMemoryContainerAction.handleRequest(request, channel, client);
161+
}
162+
148163
public void testActionNameConstant() {
149164
// Test that the action name constant is correctly defined
150165
assertEquals("ml_delete_memory_container_action", restMLDeleteMemoryContainerAction.getName());

0 commit comments

Comments
 (0)