From f99d49293e04df5c289fc2fdab414a0aa2fa3095 Mon Sep 17 00:00:00 2001 From: Navneet Verma Date: Wed, 4 Jan 2023 23:24:23 -0800 Subject: [PATCH] Remove unused MLPredict Transport action from src. Signed-off-by: Navneet Verma --- .../neuralsearch/plugin/NeuralSearch.java | 14 ---- .../transport/MLPredictAction.java | 25 ------ .../transport/MLPredictActionRequest.java | 55 ------------- .../transport/MLPredictActionResponse.java | 67 ---------------- .../transport/MLPredictTransportAction.java | 48 ------------ .../constants/TestCommonConstants.java | 2 - .../MLPredictActionRequestTests.java | 45 ----------- .../MLPredictActionResponseTests.java | 49 ------------ .../MLPredictTransportActionTests.java | 77 ------------------- 9 files changed, 382 deletions(-) delete mode 100644 src/main/java/org/opensearch/neuralsearch/transport/MLPredictAction.java delete mode 100644 src/main/java/org/opensearch/neuralsearch/transport/MLPredictActionRequest.java delete mode 100644 src/main/java/org/opensearch/neuralsearch/transport/MLPredictActionResponse.java delete mode 100644 src/main/java/org/opensearch/neuralsearch/transport/MLPredictTransportAction.java delete mode 100644 src/test/java/org/opensearch/neuralsearch/transport/MLPredictActionRequestTests.java delete mode 100644 src/test/java/org/opensearch/neuralsearch/transport/MLPredictActionResponseTests.java delete mode 100644 src/test/java/org/opensearch/neuralsearch/transport/MLPredictTransportActionTests.java diff --git a/src/main/java/org/opensearch/neuralsearch/plugin/NeuralSearch.java b/src/main/java/org/opensearch/neuralsearch/plugin/NeuralSearch.java index c3c49634f..b1ca8f932 100644 --- a/src/main/java/org/opensearch/neuralsearch/plugin/NeuralSearch.java +++ b/src/main/java/org/opensearch/neuralsearch/plugin/NeuralSearch.java @@ -11,8 +11,6 @@ import java.util.Map; import java.util.function.Supplier; -import org.opensearch.action.ActionRequest; -import org.opensearch.action.ActionResponse; import org.opensearch.client.Client; import org.opensearch.cluster.metadata.IndexNameExpressionResolver; import org.opensearch.cluster.service.ClusterService; @@ -26,8 +24,6 @@ import org.opensearch.neuralsearch.processor.TextEmbeddingProcessor; import org.opensearch.neuralsearch.processor.factory.TextEmbeddingProcessorFactory; import org.opensearch.neuralsearch.query.NeuralQueryBuilder; -import org.opensearch.neuralsearch.transport.MLPredictAction; -import org.opensearch.neuralsearch.transport.MLPredictTransportAction; import org.opensearch.plugins.ActionPlugin; import org.opensearch.plugins.ExtensiblePlugin; import org.opensearch.plugins.IngestPlugin; @@ -63,16 +59,6 @@ public Collection createComponents( return List.of(clientAccessor); } - /** - * Registering the Action Handlers - * - * @return A {@link List} of {@link ActionHandler} - */ - @Override - public List> getActions() { - return List.of(new ActionHandler<>(MLPredictAction.INSTANCE, MLPredictTransportAction.class)); - } - public List> getQueries() { return Collections.singletonList( new QuerySpec<>(NeuralQueryBuilder.NAME, NeuralQueryBuilder::new, NeuralQueryBuilder::fromXContent) diff --git a/src/main/java/org/opensearch/neuralsearch/transport/MLPredictAction.java b/src/main/java/org/opensearch/neuralsearch/transport/MLPredictAction.java deleted file mode 100644 index faef26b50..000000000 --- a/src/main/java/org/opensearch/neuralsearch/transport/MLPredictAction.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.opensearch.neuralsearch.transport; - -import org.opensearch.action.ActionType; -import org.opensearch.common.io.stream.Writeable; - -public class MLPredictAction extends ActionType { - - public static final MLPredictAction INSTANCE = new MLPredictAction(); - public static final String NAME = "cluster:admin/opensearch/neural-search/ml_predict_action"; - - private MLPredictAction() { - super(NAME, MLPredictActionResponse::new); - } - - @Override - public Writeable.Reader getResponseReader() { - return MLPredictActionResponse::new; - } - -} diff --git a/src/main/java/org/opensearch/neuralsearch/transport/MLPredictActionRequest.java b/src/main/java/org/opensearch/neuralsearch/transport/MLPredictActionRequest.java deleted file mode 100644 index a43a8fbd6..000000000 --- a/src/main/java/org/opensearch/neuralsearch/transport/MLPredictActionRequest.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.opensearch.neuralsearch.transport; - -import java.io.IOException; -import java.util.List; - -import lombok.AllArgsConstructor; -import lombok.EqualsAndHashCode; -import lombok.Getter; -import lombok.ToString; - -import org.opensearch.action.ActionRequest; -import org.opensearch.action.ActionRequestValidationException; -import org.opensearch.action.ValidateActions; -import org.opensearch.common.Strings; -import org.opensearch.common.io.stream.StreamInput; -import org.opensearch.common.io.stream.StreamOutput; - -@AllArgsConstructor -@EqualsAndHashCode(callSuper = false) -@ToString -public class MLPredictActionRequest extends ActionRequest { - @Getter - private final String modelId; - @Getter - private final List inputSentencesList; - - public MLPredictActionRequest(final StreamInput in) throws IOException { - super(in); - this.modelId = in.readString(); - this.inputSentencesList = in.readStringList(); - } - - @Override - public void writeTo(final StreamOutput out) throws IOException { - super.writeTo(out); - out.writeString(modelId); - out.writeStringCollection(inputSentencesList); - } - - @Override - public ActionRequestValidationException validate() { - if (!Strings.hasText(modelId)) { - return ValidateActions.addValidationError("Model id cannot be empty ", null); - } - if (inputSentencesList.size() == 0) { - return ValidateActions.addValidationError("Input Sentences List cannot be empty ", null); - } - return null; - } -} diff --git a/src/main/java/org/opensearch/neuralsearch/transport/MLPredictActionResponse.java b/src/main/java/org/opensearch/neuralsearch/transport/MLPredictActionResponse.java deleted file mode 100644 index ca5f898e8..000000000 --- a/src/main/java/org/opensearch/neuralsearch/transport/MLPredictActionResponse.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.opensearch.neuralsearch.transport; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.extern.log4j.Log4j2; - -import org.opensearch.action.ActionResponse; -import org.opensearch.common.io.stream.StreamInput; -import org.opensearch.common.io.stream.StreamOutput; -import org.opensearch.common.xcontent.ToXContentObject; -import org.opensearch.common.xcontent.XContentBuilder; - -@Data -@EqualsAndHashCode(callSuper = false) -@AllArgsConstructor -@Log4j2 -public class MLPredictActionResponse extends ActionResponse implements ToXContentObject { - - private static final String INFERENCE_VECTORS = "inference_vectors"; - - private List> inferenceVectorsList; - - public MLPredictActionResponse(StreamInput streamInput) throws IOException { - super(streamInput); - final int inferenceVectorsListSize = streamInput.readVInt(); - inferenceVectorsList = new ArrayList<>(inferenceVectorsListSize); - for (int i = 0; i < inferenceVectorsListSize; i++) { - final int vectorSize = streamInput.readVInt(); - final List vector = new ArrayList<>(); - for (int j = 0; j < vectorSize; j++) { - vector.add(streamInput.readFloat()); - } - inferenceVectorsList.add(vector); - } - } - - @Override - public void writeTo(StreamOutput streamOutput) throws IOException { - streamOutput.writeVInt(inferenceVectorsList.size()); - for (final List vector : inferenceVectorsList) { - streamOutput.writeCollection(vector, StreamOutput::writeFloat); - } - } - - @Override - public XContentBuilder toXContent(XContentBuilder xContentBuilder, Params params) throws IOException { - xContentBuilder.startObject().startArray(INFERENCE_VECTORS); - for (final List floats : inferenceVectorsList) { - xContentBuilder.startArray(); - for (final Float value : floats) { - xContentBuilder.value(value); - } - xContentBuilder.endArray(); - } - return xContentBuilder.endArray().endObject(); - } -} diff --git a/src/main/java/org/opensearch/neuralsearch/transport/MLPredictTransportAction.java b/src/main/java/org/opensearch/neuralsearch/transport/MLPredictTransportAction.java deleted file mode 100644 index f718d4d6f..000000000 --- a/src/main/java/org/opensearch/neuralsearch/transport/MLPredictTransportAction.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.opensearch.neuralsearch.transport; - -import org.opensearch.action.ActionListener; -import org.opensearch.action.support.ActionFilters; -import org.opensearch.action.support.HandledTransportAction; -import org.opensearch.common.inject.Inject; -import org.opensearch.neuralsearch.ml.MLCommonsClientAccessor; -import org.opensearch.tasks.Task; -import org.opensearch.transport.TransportService; - -/** - * Transport action to do call inference/predict api of ML Client. - */ -public class MLPredictTransportAction extends HandledTransportAction { - - private final MLCommonsClientAccessor clientAccessor; - - @Inject - public MLPredictTransportAction( - final TransportService transportService, - final ActionFilters filters, - final MLCommonsClientAccessor clientAccessor - ) { - super(MLPredictAction.NAME, transportService, filters, MLPredictActionRequest::new); - this.clientAccessor = clientAccessor; - } - - @Override - protected void doExecute( - final Task task, - final MLPredictActionRequest request, - final ActionListener actionListener - ) { - clientAccessor.inferenceSentences( - request.getModelId(), - request.getInputSentencesList(), - ActionListener.wrap( - inferenceResponse -> actionListener.onResponse(new MLPredictActionResponse(inferenceResponse)), - actionListener::onFailure - ) - ); - } -} diff --git a/src/test/java/org/opensearch/neuralsearch/constants/TestCommonConstants.java b/src/test/java/org/opensearch/neuralsearch/constants/TestCommonConstants.java index 4b7b98547..2776a53e6 100644 --- a/src/test/java/org/opensearch/neuralsearch/constants/TestCommonConstants.java +++ b/src/test/java/org/opensearch/neuralsearch/constants/TestCommonConstants.java @@ -5,7 +5,6 @@ package org.opensearch.neuralsearch.constants; -import java.util.Arrays; import java.util.List; import lombok.AccessLevel; @@ -16,6 +15,5 @@ public class TestCommonConstants { public static final String MODEL_ID = "modeId"; public static final List TARGET_RESPONSE_FILTERS = List.of("sentence_embedding"); public static final Float[] PREDICT_VECTOR_ARRAY = new Float[] { 2.0f, 3.0f }; - public static final List> PREDICTIONS_LIST = List.of(Arrays.asList(PREDICT_VECTOR_ARRAY)); public static final List SENTENCES_LIST = List.of("TEXT"); } diff --git a/src/test/java/org/opensearch/neuralsearch/transport/MLPredictActionRequestTests.java b/src/test/java/org/opensearch/neuralsearch/transport/MLPredictActionRequestTests.java deleted file mode 100644 index 87dc8b29f..000000000 --- a/src/test/java/org/opensearch/neuralsearch/transport/MLPredictActionRequestTests.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.opensearch.neuralsearch.transport; - -import java.util.Collections; - -import lombok.SneakyThrows; - -import org.junit.Assert; -import org.opensearch.common.io.stream.BytesStreamOutput; -import org.opensearch.neuralsearch.constants.TestCommonConstants; -import org.opensearch.test.OpenSearchTestCase; - -public class MLPredictActionRequestTests extends OpenSearchTestCase { - - @SneakyThrows - public void testStreams_whenValidInput_thenSuccess() { - final MLPredictActionRequest request = new MLPredictActionRequest(TestCommonConstants.MODEL_ID, TestCommonConstants.SENTENCES_LIST); - final MLPredictActionRequest differentObject = new MLPredictActionRequest(TestCommonConstants.MODEL_ID, Collections.emptyList()); - final BytesStreamOutput streamOutput = new BytesStreamOutput(); - request.writeTo(streamOutput); - final MLPredictActionRequest mlPredictActionRequestDuplicate = new MLPredictActionRequest(streamOutput.bytes().streamInput()); - Assert.assertEquals(request, mlPredictActionRequestDuplicate); - Assert.assertNotEquals(differentObject, mlPredictActionRequestDuplicate); - } - - public void testValidateForAllCases() { - final MLPredictActionRequest validRequest = new MLPredictActionRequest( - TestCommonConstants.MODEL_ID, - TestCommonConstants.SENTENCES_LIST - ); - Assert.assertNull(validRequest.validate()); - final MLPredictActionRequest inValidRequest = new MLPredictActionRequest(null, TestCommonConstants.SENTENCES_LIST); - Assert.assertNotNull(inValidRequest.validate()); - - final MLPredictActionRequest inValidRequestWithEmptySentenceList = new MLPredictActionRequest( - TestCommonConstants.MODEL_ID, - Collections.emptyList() - ); - Assert.assertNotNull(inValidRequestWithEmptySentenceList.validate()); - } -} diff --git a/src/test/java/org/opensearch/neuralsearch/transport/MLPredictActionResponseTests.java b/src/test/java/org/opensearch/neuralsearch/transport/MLPredictActionResponseTests.java deleted file mode 100644 index 618b4eefe..000000000 --- a/src/test/java/org/opensearch/neuralsearch/transport/MLPredictActionResponseTests.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.opensearch.neuralsearch.transport; - -import java.util.Collections; - -import lombok.SneakyThrows; - -import org.junit.Assert; -import org.opensearch.common.Strings; -import org.opensearch.common.io.stream.BytesStreamOutput; -import org.opensearch.common.xcontent.XContentBuilder; -import org.opensearch.common.xcontent.XContentFactory; -import org.opensearch.common.xcontent.XContentType; -import org.opensearch.neuralsearch.constants.TestCommonConstants; -import org.opensearch.test.OpenSearchTestCase; - -public class MLPredictActionResponseTests extends OpenSearchTestCase { - - @SneakyThrows - public void testStreams_whenValidInput_thenSuccess() { - final MLPredictActionResponse response = new MLPredictActionResponse(TestCommonConstants.PREDICTIONS_LIST); - final BytesStreamOutput streamOutput = new BytesStreamOutput(); - response.writeTo(streamOutput); - final MLPredictActionResponse duplicateResponse = new MLPredictActionResponse(streamOutput.bytes().streamInput()); - Assert.assertEquals(response, duplicateResponse); - } - - @SneakyThrows - public void testStreams_whenPredictionListEmpty_thenSuccess() { - final MLPredictActionResponse response = new MLPredictActionResponse(Collections.emptyList()); - final BytesStreamOutput streamOutput = new BytesStreamOutput(); - response.writeTo(streamOutput); - final MLPredictActionResponse duplicateResponse = new MLPredictActionResponse(streamOutput.bytes().streamInput()); - Assert.assertEquals(response, duplicateResponse); - } - - @SneakyThrows - public void testToXContent_whenValidInput_thenSuccess() { - final MLPredictActionResponse response = new MLPredictActionResponse(TestCommonConstants.PREDICTIONS_LIST); - final String xContentString = "{\"inference_vectors\":[[2.0,3.0]]}"; - final XContentBuilder xContentBuilder = XContentFactory.contentBuilder(XContentType.JSON); - Assert.assertEquals(xContentString, Strings.toString(response.toXContent(xContentBuilder, null))); - } - -} diff --git a/src/test/java/org/opensearch/neuralsearch/transport/MLPredictTransportActionTests.java b/src/test/java/org/opensearch/neuralsearch/transport/MLPredictTransportActionTests.java deleted file mode 100644 index 9509fcf05..000000000 --- a/src/test/java/org/opensearch/neuralsearch/transport/MLPredictTransportActionTests.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.opensearch.neuralsearch.transport; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import org.junit.Before; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; -import org.opensearch.action.ActionListener; -import org.opensearch.action.support.ActionFilters; -import org.opensearch.neuralsearch.constants.TestCommonConstants; -import org.opensearch.neuralsearch.ml.MLCommonsClientAccessor; -import org.opensearch.tasks.Task; -import org.opensearch.test.OpenSearchTestCase; -import org.opensearch.transport.TransportService; - -public class MLPredictTransportActionTests extends OpenSearchTestCase { - - @Mock - private MLCommonsClientAccessor mlCommonsClientAccessor; - @Mock - private ActionListener mlPredictActionResponseActionListener; - @Mock - private Task task; - // This variable is required for construction of TransportAction - @Mock - private TransportService transportService; - // This variable is required for construction of TransportAction - @Mock - private ActionFilters actionFilters; - @InjectMocks - private MLPredictTransportAction transportAction; - - @Before - public void setup() { - MockitoAnnotations.openMocks(this); - } - - public void testDoExecute_whenValidInput_thenSuccess() { - final MLPredictActionRequest request = new MLPredictActionRequest(TestCommonConstants.MODEL_ID, TestCommonConstants.SENTENCES_LIST); - - final List> vectorList = new ArrayList<>(); - vectorList.add(Arrays.asList(TestCommonConstants.PREDICT_VECTOR_ARRAY)); - - final MLPredictActionResponse response = new MLPredictActionResponse(vectorList); - Mockito.doAnswer(invocation -> { - final ActionListener>> actionListener = invocation.getArgument(2); - actionListener.onResponse(vectorList); - return null; - }) - .when(mlCommonsClientAccessor) - .inferenceSentences( - Mockito.eq(TestCommonConstants.MODEL_ID), - Mockito.eq(TestCommonConstants.SENTENCES_LIST), - Mockito.isA(ActionListener.class) - ); - - transportAction.doExecute(task, request, mlPredictActionResponseActionListener); - - Mockito.verify(mlCommonsClientAccessor) - .inferenceSentences( - Mockito.eq(TestCommonConstants.MODEL_ID), - Mockito.eq(TestCommonConstants.SENTENCES_LIST), - Mockito.isA(ActionListener.class) - ); - - Mockito.verify(mlPredictActionResponseActionListener).onResponse(response); - } -}