Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.util.concurrent.EsExecutors;
import org.elasticsearch.inference.InferenceServiceRegistry;
import org.elasticsearch.inference.UnparsedModel;
Expand Down Expand Up @@ -86,17 +87,6 @@ private void doExecuteForked(
ClusterState state,
ActionListener<DeleteInferenceEndpointAction.Response> masterListener
) {
if (modelRegistry.containsDefaultConfigId(request.getInferenceEndpointId())) {
masterListener.onFailure(
new ElasticsearchStatusException(
"[{}] is a reserved inference endpoint. Cannot delete a reserved inference endpoint.",
RestStatus.BAD_REQUEST,
request.getInferenceEndpointId()
)
);
return;
}

SubscribableListener.<UnparsedModel>newForked(modelConfigListener -> {
// Get the model from the registry

Expand All @@ -118,6 +108,18 @@ private void doExecuteForked(
if (errorString != null) {
listener.onFailure(new ElasticsearchStatusException(errorString, RestStatus.CONFLICT));
return;
} else if (isInferenceIdReserved(request.getInferenceEndpointId())) {
listener.onFailure(
new ElasticsearchStatusException(
Strings.format(
"[%s] is a reserved inference endpoint. Use the force=true query parameter "
+ "to delete the inference endpoint.",
request.getInferenceEndpointId()
),
RestStatus.BAD_REQUEST
)
);
return;
}
}

Expand Down Expand Up @@ -186,6 +188,10 @@ private static String endpointIsReferencedInPipelinesOrIndexes(final ClusterStat
return null;
}

private boolean isInferenceIdReserved(String inferenceEndpointId) {
return modelRegistry.containsDefaultConfigId(inferenceEndpointId);
}

private static String buildErrorString(String inferenceEndpointId, Set<String> pipelines, Set<String> indexes) {
StringBuilder errorString = new StringBuilder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
package org.elasticsearch.xpack.inference.action;

import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.PlainActionFuture;
import org.elasticsearch.client.internal.Client;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.inference.InferenceService;
import org.elasticsearch.inference.InferenceServiceRegistry;
import org.elasticsearch.inference.MinimalServiceSettings;
import org.elasticsearch.inference.TaskType;
import org.elasticsearch.inference.UnparsedModel;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.threadpool.ThreadPool;
Expand All @@ -27,30 +27,39 @@
import org.junit.After;
import org.junit.Before;

import java.util.Map;
import java.util.Optional;

import static org.elasticsearch.xpack.inference.Utils.inferenceUtilityPool;
import static org.hamcrest.Matchers.is;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class TransportDeleteInferenceEndpointActionTests extends ESTestCase {

private static final TimeValue TIMEOUT = TimeValue.timeValueSeconds(30);

private TransportDeleteInferenceEndpointAction action;
private ThreadPool threadPool;
private ModelRegistry modelRegistry;
private ModelRegistry mockModelRegistry;
private InferenceServiceRegistry mockInferenceServiceRegistry;

@Before
public void setUp() throws Exception {
super.setUp();
modelRegistry = new ModelRegistry(mock(Client.class));
threadPool = createThreadPool(inferenceUtilityPool());
mockModelRegistry = mock(ModelRegistry.class);
mockInferenceServiceRegistry = mock(InferenceServiceRegistry.class);
action = new TransportDeleteInferenceEndpointAction(
mock(TransportService.class),
mock(ClusterService.class),
threadPool,
mock(ActionFilters.class),
modelRegistry,
mock(InferenceServiceRegistry.class)
mockModelRegistry,
mockInferenceServiceRegistry
);
}

Expand All @@ -60,24 +69,63 @@ public void tearDown() throws Exception {
terminate(threadPool);
}

public void testFailsToDelete_ADefaultEndpoint() {
modelRegistry.addDefaultIds(
new InferenceService.DefaultConfigId("model-id", MinimalServiceSettings.chatCompletion(), mock(InferenceService.class))
);
public void testFailsToDelete_ADefaultEndpoint_WithoutPassingForceQueryParameter() {
doAnswer(invocationOnMock -> {
ActionListener<UnparsedModel> listener = invocationOnMock.getArgument(1);
listener.onResponse(new UnparsedModel("model_id", TaskType.COMPLETION, "service", Map.of(), Map.of()));
return Void.TYPE;
}).when(mockModelRegistry).getModel(anyString(), any());
when(mockModelRegistry.containsDefaultConfigId(anyString())).thenReturn(true);

var listener = new PlainActionFuture<DeleteInferenceEndpointAction.Response>();

action.masterOperation(
mock(Task.class),
new DeleteInferenceEndpointAction.Request("model-id", TaskType.CHAT_COMPLETION, true, false),
mock(ClusterState.class),
new DeleteInferenceEndpointAction.Request("model-id", TaskType.COMPLETION, false, false),
ClusterState.EMPTY_STATE,
listener
);

var exception = expectThrows(ElasticsearchStatusException.class, () -> listener.actionGet(TIMEOUT));
assertThat(
exception.getMessage(),
is("[model-id] is a reserved inference endpoint. " + "Cannot delete a reserved inference endpoint.")
is("[model-id] is a reserved inference endpoint. Use the force=true query parameter to delete the inference endpoint.")
);
}

public void testDeletesDefaultEndpoint_WhenForceIsTrue() {
doAnswer(invocationOnMock -> {
ActionListener<UnparsedModel> listener = invocationOnMock.getArgument(1);
listener.onResponse(new UnparsedModel("model_id", TaskType.COMPLETION, "service", Map.of(), Map.of()));
return Void.TYPE;
}).when(mockModelRegistry).getModel(anyString(), any());
when(mockModelRegistry.containsDefaultConfigId(anyString())).thenReturn(true);
doAnswer(invocationOnMock -> {
ActionListener<Boolean> listener = invocationOnMock.getArgument(1);
listener.onResponse(true);
return Void.TYPE;
}).when(mockModelRegistry).deleteModel(anyString(), any());

var mockService = mock(InferenceService.class);
doAnswer(invocationOnMock -> {
ActionListener<Boolean> listener = invocationOnMock.getArgument(1);
listener.onResponse(true);
return Void.TYPE;
}).when(mockService).stop(any(), any());

when(mockInferenceServiceRegistry.getService(anyString())).thenReturn(Optional.of(mockService));

var listener = new PlainActionFuture<DeleteInferenceEndpointAction.Response>();

action.masterOperation(
mock(Task.class),
new DeleteInferenceEndpointAction.Request("model-id", TaskType.COMPLETION, true, false),
ClusterState.EMPTY_STATE,
listener
);

var response = listener.actionGet(TIMEOUT);

assertTrue(response.isAcknowledged());
}
}