From 111f496f977d06e356a95d5f09ab64b6aacabe37 Mon Sep 17 00:00:00 2001 From: Navneet Verma Date: Tue, 1 Aug 2023 00:23:47 -0700 Subject: [PATCH] Fixing the core breaking changes in various classes. Signed-off-by: Navneet Verma --- .../java/org/opensearch/knn/bwc/ModelIT.java | 6 ++--- .../org/opensearch/knn/index/KNNSettings.java | 6 ++--- .../knn/index/KNNVectorIndexFieldData.java | 2 +- .../KNN80Codec/KNN80DocValuesConsumer.java | 14 +++++++---- .../knn/index/KNNSettingsTests.java | 2 +- .../knn/index/query/KNNWeightTests.java | 2 +- .../action/RestDeleteModelHandlerIT.java | 8 +++---- .../plugin/action/RestGetModelHandlerIT.java | 6 ++--- .../plugin/action/RestKNNStatsHandlerIT.java | 8 +++---- .../action/RestSearchModelHandlerIT.java | 12 +++++----- .../action/RestTrainModelHandlerIT.java | 19 +++++++-------- .../knn/plugin/script/KNNScriptScoringIT.java | 12 +++++----- .../transport/DeleteModelResponseTests.java | 3 +-- .../transport/GetModelResponseTests.java | 5 ++-- .../org/opensearch/knn/KNNRestTestCase.java | 24 +++++++++---------- .../org/opensearch/knn/ODFERestTestCase.java | 4 ++-- .../java/org/opensearch/knn/TestUtils.java | 14 +++++++---- 17 files changed, 75 insertions(+), 72 deletions(-) diff --git a/qa/restart-upgrade/src/test/java/org/opensearch/knn/bwc/ModelIT.java b/qa/restart-upgrade/src/test/java/org/opensearch/knn/bwc/ModelIT.java index cef1b9b61..473802ca8 100644 --- a/qa/restart-upgrade/src/test/java/org/opensearch/knn/bwc/ModelIT.java +++ b/qa/restart-upgrade/src/test/java/org/opensearch/knn/bwc/ModelIT.java @@ -15,7 +15,7 @@ import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentFactory; import org.opensearch.core.xcontent.XContentParser; -import org.opensearch.core.xcontent.MediaTypeParserRegistry; +import org.opensearch.core.xcontent.MediaTypeRegistry; import org.opensearch.knn.index.SpaceType; import org.opensearch.knn.index.util.KNNEngine; import org.opensearch.knn.indices.ModelMetadata; @@ -186,7 +186,7 @@ public void searchKNNModel(String testModelID) throws Exception { String responseBody = EntityUtils.toString(response.getEntity()); assertNotNull(responseBody); - XContentParser parser = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody); + XContentParser parser = createParser(MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody); SearchResponse searchResponse = SearchResponse.fromXContent(parser); assertNotNull(searchResponse); assertEquals(EXP_NUM_OF_MODELS, searchResponse.getHits().getHits().length); @@ -203,7 +203,7 @@ public void validateModelCreated(String modelId) throws Exception { String responseBody = EntityUtils.toString(getResponse.getEntity()); assertNotNull(responseBody); - Map responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); + Map responseMap = createParser(MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody).map(); assertEquals(modelId, responseMap.get(MODEL_ID)); assertTrainingSucceeds(modelId, NUM_OF_ATTEMPTS, DELAY_MILLI_SEC); } diff --git a/src/main/java/org/opensearch/knn/index/KNNSettings.java b/src/main/java/org/opensearch/knn/index/KNNSettings.java index 526a529f1..05c107ca7 100644 --- a/src/main/java/org/opensearch/knn/index/KNNSettings.java +++ b/src/main/java/org/opensearch/knn/index/KNNSettings.java @@ -15,8 +15,8 @@ import org.opensearch.cluster.service.ClusterService; import org.opensearch.common.settings.Setting; import org.opensearch.common.settings.Settings; -import org.opensearch.common.unit.ByteSizeUnit; -import org.opensearch.common.unit.ByteSizeValue; +import org.opensearch.core.common.unit.ByteSizeUnit; +import org.opensearch.core.common.unit.ByteSizeValue; import org.opensearch.common.unit.TimeValue; import org.opensearch.index.IndexModule; import org.opensearch.knn.index.memory.NativeMemoryCacheManager; @@ -37,7 +37,7 @@ import static org.opensearch.common.settings.Setting.Property.Dynamic; import static org.opensearch.common.settings.Setting.Property.IndexScope; import static org.opensearch.common.settings.Setting.Property.NodeScope; -import static org.opensearch.common.unit.ByteSizeValue.parseBytesSizeValue; +import static org.opensearch.core.common.unit.ByteSizeValue.parseBytesSizeValue; import static org.opensearch.common.unit.MemorySizeValue.parseBytesSizeValueOrHeapRatio; /** diff --git a/src/main/java/org/opensearch/knn/index/KNNVectorIndexFieldData.java b/src/main/java/org/opensearch/knn/index/KNNVectorIndexFieldData.java index deef8bae1..d8a50e6b6 100644 --- a/src/main/java/org/opensearch/knn/index/KNNVectorIndexFieldData.java +++ b/src/main/java/org/opensearch/knn/index/KNNVectorIndexFieldData.java @@ -10,7 +10,7 @@ import org.opensearch.common.util.BigArrays; import org.opensearch.index.fielddata.IndexFieldData; import org.opensearch.index.fielddata.IndexFieldDataCache; -import org.opensearch.indices.breaker.CircuitBreakerService; +import org.opensearch.core.indices.breaker.CircuitBreakerService; import org.opensearch.search.DocValueFormat; import org.opensearch.search.MultiValueMode; import org.opensearch.search.aggregations.support.ValuesSourceType; diff --git a/src/main/java/org/opensearch/knn/index/codec/KNN80Codec/KNN80DocValuesConsumer.java b/src/main/java/org/opensearch/knn/index/codec/KNN80Codec/KNN80DocValuesConsumer.java index a3ea798f6..d9a30f75b 100644 --- a/src/main/java/org/opensearch/knn/index/codec/KNN80Codec/KNN80DocValuesConsumer.java +++ b/src/main/java/org/opensearch/knn/index/codec/KNN80Codec/KNN80DocValuesConsumer.java @@ -9,8 +9,9 @@ import lombok.NonNull; import lombok.extern.log4j.Log4j2; import org.apache.lucene.store.ChecksumIndexInput; -import org.opensearch.common.xcontent.XContentFactory; -import org.opensearch.core.xcontent.MediaTypeParserRegistry; +import org.opensearch.common.xcontent.XContentHelper; +import org.opensearch.core.common.bytes.BytesArray; +import org.opensearch.core.xcontent.MediaTypeRegistry; import org.opensearch.core.xcontent.DeprecationHandler; import org.opensearch.core.xcontent.NamedXContentRegistry; import org.opensearch.knn.index.KNNSettings; @@ -176,9 +177,12 @@ private void createKNNIndexFromScratch(FieldInfo fieldInfo, KNNCodecUtil.Pair pa parameters.put(PARAMETERS, algoParams); } else { parameters.putAll( - XContentFactory.xContent(MediaTypeParserRegistry.getDefaultMediaType()) - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, parametersString) - .map() + XContentHelper.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + new BytesArray(parametersString), + MediaTypeRegistry.getDefaultMediaType() + ).map() ); } diff --git a/src/test/java/org/opensearch/knn/index/KNNSettingsTests.java b/src/test/java/org/opensearch/knn/index/KNNSettingsTests.java index 724c9ad6a..f6a10616b 100644 --- a/src/test/java/org/opensearch/knn/index/KNNSettingsTests.java +++ b/src/test/java/org/opensearch/knn/index/KNNSettingsTests.java @@ -10,7 +10,7 @@ import org.opensearch.cluster.service.ClusterService; import org.opensearch.common.network.NetworkModule; import org.opensearch.common.settings.Settings; -import org.opensearch.common.unit.ByteSizeValue; +import org.opensearch.core.common.unit.ByteSizeValue; import org.opensearch.env.Environment; import org.opensearch.knn.KNNTestCase; import org.opensearch.knn.plugin.KNNPlugin; diff --git a/src/test/java/org/opensearch/knn/index/query/KNNWeightTests.java b/src/test/java/org/opensearch/knn/index/query/KNNWeightTests.java index 53d0330f0..cc2dbd3f0 100644 --- a/src/test/java/org/opensearch/knn/index/query/KNNWeightTests.java +++ b/src/test/java/org/opensearch/knn/index/query/KNNWeightTests.java @@ -29,7 +29,7 @@ import org.junit.BeforeClass; import org.mockito.MockedStatic; import org.opensearch.common.io.PathUtils; -import org.opensearch.common.unit.ByteSizeValue; +import org.opensearch.core.common.unit.ByteSizeValue; import org.opensearch.common.unit.TimeValue; import org.opensearch.knn.KNNTestCase; import org.opensearch.knn.index.KNNSettings; diff --git a/src/test/java/org/opensearch/knn/plugin/action/RestDeleteModelHandlerIT.java b/src/test/java/org/opensearch/knn/plugin/action/RestDeleteModelHandlerIT.java index b64d9d9a3..b9f1b0a79 100644 --- a/src/test/java/org/opensearch/knn/plugin/action/RestDeleteModelHandlerIT.java +++ b/src/test/java/org/opensearch/knn/plugin/action/RestDeleteModelHandlerIT.java @@ -17,7 +17,7 @@ import org.opensearch.client.ResponseException; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentFactory; -import org.opensearch.core.xcontent.MediaTypeParserRegistry; +import org.opensearch.core.xcontent.MediaTypeRegistry; import org.opensearch.knn.KNNRestTestCase; import org.opensearch.knn.plugin.KNNPlugin; import org.opensearch.core.rest.RestStatus; @@ -62,7 +62,7 @@ public void testDeleteModelExists() throws Exception { String responseBody = EntityUtils.toString(getModelResponse.getEntity()); assertNotNull(responseBody); - Map responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); + Map responseMap = createParser(MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody).map(); assertEquals(modelId, responseMap.get(MODEL_ID)); @@ -99,7 +99,7 @@ public void testDeleteTrainingModel() throws Exception { String responseBody = EntityUtils.toString(getModelResponse.getEntity()); assertNotNull(responseBody); - Map responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); + Map responseMap = createParser(MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody).map(); assertEquals(modelId, responseMap.get(MODEL_ID)); @@ -205,7 +205,7 @@ private void trainModel(String modelId, String trainingIndexName, String trainin String responseBody = EntityUtils.toString(getResponse.getEntity()); assertNotNull(responseBody); - Map responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); + Map responseMap = createParser(MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody).map(); assertEquals(modelId, responseMap.get(MODEL_ID)); diff --git a/src/test/java/org/opensearch/knn/plugin/action/RestGetModelHandlerIT.java b/src/test/java/org/opensearch/knn/plugin/action/RestGetModelHandlerIT.java index 81f6a36e8..20e0d58c3 100644 --- a/src/test/java/org/opensearch/knn/plugin/action/RestGetModelHandlerIT.java +++ b/src/test/java/org/opensearch/knn/plugin/action/RestGetModelHandlerIT.java @@ -16,7 +16,7 @@ import org.opensearch.client.Request; import org.opensearch.client.Response; import org.opensearch.client.ResponseException; -import org.opensearch.core.xcontent.MediaTypeParserRegistry; +import org.opensearch.core.xcontent.MediaTypeRegistry; import org.opensearch.knn.KNNRestTestCase; import org.opensearch.knn.plugin.KNNPlugin; import org.opensearch.core.rest.RestStatus; @@ -74,7 +74,7 @@ public void testGetModelExists() throws Exception { String responseBody = EntityUtils.toString(response.getEntity()); assertNotNull(responseBody); - Map responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); + Map responseMap = createParser(MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody).map(); assertEquals(modelId, responseMap.get(MODEL_ID)); assertEquals(modelDescription, responseMap.get(MODEL_DESCRIPTION)); assertEquals(FAISS.getName(), responseMap.get(KNN_ENGINE)); @@ -106,7 +106,7 @@ public void testGetModelExistsWithFilter() throws Exception { String responseBody = EntityUtils.toString(response.getEntity()); assertNotNull(responseBody); - Map responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); + Map responseMap = createParser(MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody).map(); assertTrue(responseMap.size() == filteredPath.size()); assertEquals(modelId, responseMap.get(MODEL_ID)); diff --git a/src/test/java/org/opensearch/knn/plugin/action/RestKNNStatsHandlerIT.java b/src/test/java/org/opensearch/knn/plugin/action/RestKNNStatsHandlerIT.java index c7030647b..e3b832e8c 100644 --- a/src/test/java/org/opensearch/knn/plugin/action/RestKNNStatsHandlerIT.java +++ b/src/test/java/org/opensearch/knn/plugin/action/RestKNNStatsHandlerIT.java @@ -19,7 +19,7 @@ import org.opensearch.common.unit.TimeValue; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentFactory; -import org.opensearch.core.xcontent.MediaTypeParserRegistry; +import org.opensearch.core.xcontent.MediaTypeRegistry; import org.opensearch.index.query.MatchAllQueryBuilder; import org.opensearch.index.query.QueryBuilder; import org.opensearch.index.query.QueryBuilders; @@ -347,7 +347,7 @@ public void testModelIndexHealthMetricsStats() throws Exception { final Response response = getKnnStats(Collections.emptyList(), Arrays.asList(modelIndexStatusName)); final String responseBody = EntityUtils.toString(response.getEntity()); - final Map statsMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); + final Map statsMap = createParser(MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody).map(); // Check that model health status is null since model index is not created to system yet assertNull(statsMap.get(StatNames.MODEL_INDEX_STATUS.getName())); @@ -358,7 +358,7 @@ public void testModelIndexHealthMetricsStats() throws Exception { Response response = getKnnStats(Collections.emptyList(), Arrays.asList(modelIndexStatusName)); final String responseBody = EntityUtils.toString(response.getEntity()); - final Map statsMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); + final Map statsMap = createParser(MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody).map(); // Check that model health status is not null assertNotNull(statsMap.get(modelIndexStatusName)); @@ -452,7 +452,7 @@ public void validateModelCreated(String modelId) throws Exception { String responseBody = EntityUtils.toString(getResponse.getEntity()); assertNotNull(responseBody); - Map responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); + Map responseMap = createParser(MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody).map(); assertEquals(modelId, responseMap.get(MODEL_ID)); assertTrainingSucceeds(modelId, NUM_OF_ATTEMPTS, DELAY_MILLI_SEC); } diff --git a/src/test/java/org/opensearch/knn/plugin/action/RestSearchModelHandlerIT.java b/src/test/java/org/opensearch/knn/plugin/action/RestSearchModelHandlerIT.java index 17a61931d..7dbe8d950 100644 --- a/src/test/java/org/opensearch/knn/plugin/action/RestSearchModelHandlerIT.java +++ b/src/test/java/org/opensearch/knn/plugin/action/RestSearchModelHandlerIT.java @@ -17,7 +17,7 @@ import org.opensearch.client.Response; import org.opensearch.client.ResponseException; import org.opensearch.core.xcontent.XContentParser; -import org.opensearch.core.xcontent.MediaTypeParserRegistry; +import org.opensearch.core.xcontent.MediaTypeRegistry; import org.opensearch.knn.KNNRestTestCase; import org.opensearch.knn.index.SpaceType; import org.opensearch.knn.index.util.KNNEngine; @@ -73,7 +73,7 @@ public void testNoModelExists() throws Exception { String responseBody = EntityUtils.toString(response.getEntity()); assertNotNull(responseBody); - XContentParser parser = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody); + XContentParser parser = createParser(MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody); SearchResponse searchResponse = SearchResponse.fromXContent(parser); assertNotNull(searchResponse); assertEquals(searchResponse.getHits().getHits().length, 0); @@ -133,7 +133,7 @@ public void testSearchModelExists() throws Exception { String responseBody = EntityUtils.toString(response.getEntity()); assertNotNull(responseBody); - XContentParser parser = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody); + XContentParser parser = createParser(MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody); SearchResponse searchResponse = SearchResponse.fromXContent(parser); assertNotNull(searchResponse); @@ -177,7 +177,7 @@ public void testSearchModelWithoutSource() throws Exception { String responseBody = EntityUtils.toString(response.getEntity()); assertNotNull(responseBody); - XContentParser parser = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody); + XContentParser parser = createParser(MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody); SearchResponse searchResponse = SearchResponse.fromXContent(parser); assertNotNull(searchResponse); @@ -225,7 +225,7 @@ public void testSearchModelWithSourceFilteringIncludes() throws Exception { String responseBody = EntityUtils.toString(response.getEntity()); assertNotNull(responseBody); - XContentParser parser = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody); + XContentParser parser = createParser(MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody); SearchResponse searchResponse = SearchResponse.fromXContent(parser); assertNotNull(searchResponse); @@ -277,7 +277,7 @@ public void testSearchModelWithSourceFilteringExcludes() throws Exception { String responseBody = EntityUtils.toString(response.getEntity()); assertNotNull(responseBody); - XContentParser parser = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody); + XContentParser parser = createParser(MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody); SearchResponse searchResponse = SearchResponse.fromXContent(parser); assertNotNull(searchResponse); diff --git a/src/test/java/org/opensearch/knn/plugin/action/RestTrainModelHandlerIT.java b/src/test/java/org/opensearch/knn/plugin/action/RestTrainModelHandlerIT.java index 1f2742344..545fa7cc7 100644 --- a/src/test/java/org/opensearch/knn/plugin/action/RestTrainModelHandlerIT.java +++ b/src/test/java/org/opensearch/knn/plugin/action/RestTrainModelHandlerIT.java @@ -15,7 +15,7 @@ import org.opensearch.client.Response; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentFactory; -import org.opensearch.core.xcontent.MediaTypeParserRegistry; +import org.opensearch.core.xcontent.MediaTypeRegistry; import org.opensearch.knn.KNNRestTestCase; import org.opensearch.core.rest.RestStatus; @@ -96,8 +96,7 @@ public void testTrainModel_fail_notEnoughData() throws Exception { String trainResponseBody = EntityUtils.toString(trainResponse.getEntity()); assertNotNull(trainResponseBody); - Map trainResponseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), trainResponseBody) - .map(); + Map trainResponseMap = createParser(MediaTypeRegistry.getDefaultMediaType().xContent(), trainResponseBody).map(); String modelId = (String) trainResponseMap.get(MODEL_ID); assertNotNull(modelId); @@ -106,7 +105,7 @@ public void testTrainModel_fail_notEnoughData() throws Exception { String responseBody = EntityUtils.toString(getResponse.getEntity()); assertNotNull(responseBody); - Map responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); + Map responseMap = createParser(MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody).map(); assertEquals(modelId, responseMap.get(MODEL_ID)); @@ -177,8 +176,7 @@ public void testTrainModel_fail_tooMuchData() throws Exception { String trainResponseBody = EntityUtils.toString(trainResponse.getEntity()); assertNotNull(trainResponseBody); - Map trainResponseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), trainResponseBody) - .map(); + Map trainResponseMap = createParser(MediaTypeRegistry.getDefaultMediaType().xContent(), trainResponseBody).map(); String modelId = (String) trainResponseMap.get(MODEL_ID); assertNotNull(modelId); @@ -187,7 +185,7 @@ public void testTrainModel_fail_tooMuchData() throws Exception { String responseBody = EntityUtils.toString(getResponse.getEntity()); assertNotNull(responseBody); - Map responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); + Map responseMap = createParser(MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody).map(); assertEquals(modelId, responseMap.get(MODEL_ID)); @@ -258,7 +256,7 @@ public void testTrainModel_success_withId() throws Exception { String responseBody = EntityUtils.toString(getResponse.getEntity()); assertNotNull(responseBody); - Map responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); + Map responseMap = createParser(MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody).map(); assertEquals(modelId, responseMap.get(MODEL_ID)); @@ -328,8 +326,7 @@ public void testTrainModel_success_noId() throws Exception { String trainResponseBody = EntityUtils.toString(trainResponse.getEntity()); assertNotNull(trainResponseBody); - Map trainResponseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), trainResponseBody) - .map(); + Map trainResponseMap = createParser(MediaTypeRegistry.getDefaultMediaType().xContent(), trainResponseBody).map(); String modelId = (String) trainResponseMap.get(MODEL_ID); assertNotNull(modelId); @@ -338,7 +335,7 @@ public void testTrainModel_success_noId() throws Exception { String responseBody = EntityUtils.toString(getResponse.getEntity()); assertNotNull(responseBody); - Map responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); + Map responseMap = createParser(MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody).map(); assertEquals(modelId, responseMap.get(MODEL_ID)); diff --git a/src/test/java/org/opensearch/knn/plugin/script/KNNScriptScoringIT.java b/src/test/java/org/opensearch/knn/plugin/script/KNNScriptScoringIT.java index 2a06376a2..2b398245d 100644 --- a/src/test/java/org/opensearch/knn/plugin/script/KNNScriptScoringIT.java +++ b/src/test/java/org/opensearch/knn/plugin/script/KNNScriptScoringIT.java @@ -17,7 +17,7 @@ import org.opensearch.core.xcontent.ToXContent; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentFactory; -import org.opensearch.core.xcontent.MediaTypeParserRegistry; +import org.opensearch.core.xcontent.MediaTypeRegistry; import org.opensearch.index.query.MatchAllQueryBuilder; import org.opensearch.index.query.QueryBuilder; import org.opensearch.index.query.functionscore.ScriptScoreQueryBuilder; @@ -391,7 +391,7 @@ public void testKNNScoreforNonVectorDocument() throws Exception { String responseBody = EntityUtils.toString(response.getEntity()); List hits = (List) ((Map) createParser( - MediaTypeParserRegistry.getDefaultMediaType().xContent(), + MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody ).map().get("hits")).get("hits"); @@ -459,7 +459,7 @@ public void testHammingScriptScore_Long() throws Exception { String responseBody1 = EntityUtils.toString(response1.getEntity()); List hits1 = (List) ((Map) createParser( - MediaTypeParserRegistry.getDefaultMediaType().xContent(), + MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody1 ).map().get("hits")).get("hits"); @@ -499,7 +499,7 @@ public void testHammingScriptScore_Long() throws Exception { String responseBody2 = EntityUtils.toString(response2.getEntity()); List hits2 = (List) ((Map) createParser( - MediaTypeParserRegistry.getDefaultMediaType().xContent(), + MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody2 ).map().get("hits")).get("hits"); @@ -570,7 +570,7 @@ public void testHammingScriptScore_Base64() throws Exception { String responseBody1 = EntityUtils.toString(response1.getEntity()); List hits1 = (List) ((Map) createParser( - MediaTypeParserRegistry.getDefaultMediaType().xContent(), + MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody1 ).map().get("hits")).get("hits"); @@ -610,7 +610,7 @@ public void testHammingScriptScore_Base64() throws Exception { String responseBody2 = EntityUtils.toString(response2.getEntity()); List hits2 = (List) ((Map) createParser( - MediaTypeParserRegistry.getDefaultMediaType().xContent(), + MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody2 ).map().get("hits")).get("hits"); diff --git a/src/test/java/org/opensearch/knn/plugin/transport/DeleteModelResponseTests.java b/src/test/java/org/opensearch/knn/plugin/transport/DeleteModelResponseTests.java index 98e030b22..626068d13 100644 --- a/src/test/java/org/opensearch/knn/plugin/transport/DeleteModelResponseTests.java +++ b/src/test/java/org/opensearch/knn/plugin/transport/DeleteModelResponseTests.java @@ -15,7 +15,6 @@ import org.opensearch.common.io.stream.BytesStreamOutput; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentFactory; -import org.opensearch.core.xcontent.MediaTypeParserRegistry; import org.opensearch.knn.KNNTestCase; import java.io.IOException; @@ -39,7 +38,7 @@ public void testXContentWithoutError() throws IOException { BytesStreamOutput streamOutput = new BytesStreamOutput(); deleteModelResponse.writeTo(streamOutput); String expectedResponseString = "{\"model_id\":\"test-model\",\"result\":\"deleted\"}"; - XContentBuilder xContentBuilder = XContentFactory.contentBuilder(MediaTypeParserRegistry.getDefaultMediaType()); + XContentBuilder xContentBuilder = XContentFactory.jsonBuilder(); deleteModelResponse.toXContent(xContentBuilder, null); assertEquals(expectedResponseString, Strings.toString(xContentBuilder)); } diff --git a/src/test/java/org/opensearch/knn/plugin/transport/GetModelResponseTests.java b/src/test/java/org/opensearch/knn/plugin/transport/GetModelResponseTests.java index 22ad4c78e..1d69e02cb 100644 --- a/src/test/java/org/opensearch/knn/plugin/transport/GetModelResponseTests.java +++ b/src/test/java/org/opensearch/knn/plugin/transport/GetModelResponseTests.java @@ -15,7 +15,6 @@ import org.opensearch.common.io.stream.BytesStreamOutput; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentFactory; -import org.opensearch.core.xcontent.MediaTypeParserRegistry; import org.opensearch.knn.KNNTestCase; import org.opensearch.knn.index.SpaceType; import org.opensearch.knn.index.util.KNNEngine; @@ -49,7 +48,7 @@ public void testXContent() throws IOException { GetModelResponse getModelResponse = new GetModelResponse(model); String expectedResponseString = "{\"model_id\":\"test-model\",\"model_blob\":\"aGVsbG8=\",\"state\":\"created\",\"timestamp\":\"2021-03-27 10:15:30 AM +05:30\",\"description\":\"test model\",\"error\":\"\",\"space_type\":\"l2\",\"dimension\":4,\"engine\":\"nmslib\"}"; - XContentBuilder xContentBuilder = XContentFactory.contentBuilder(MediaTypeParserRegistry.getDefaultMediaType()); + XContentBuilder xContentBuilder = XContentFactory.jsonBuilder(); getModelResponse.toXContent(xContentBuilder, null); assertEquals(expectedResponseString, Strings.toString(xContentBuilder)); } @@ -60,7 +59,7 @@ public void testXContentWithNoModelBlob() throws IOException { GetModelResponse getModelResponse = new GetModelResponse(model); String expectedResponseString = "{\"model_id\":\"test-model\",\"model_blob\":\"\",\"state\":\"failed\",\"timestamp\":\"2021-03-27 10:15:30 AM +05:30\",\"description\":\"test model\",\"error\":\"\",\"space_type\":\"l2\",\"dimension\":4,\"engine\":\"nmslib\"}"; - XContentBuilder xContentBuilder = XContentFactory.contentBuilder(MediaTypeParserRegistry.getDefaultMediaType()); + XContentBuilder xContentBuilder = XContentFactory.jsonBuilder(); getModelResponse.toXContent(xContentBuilder, null); assertEquals(expectedResponseString, Strings.toString(xContentBuilder)); } diff --git a/src/testFixtures/java/org/opensearch/knn/KNNRestTestCase.java b/src/testFixtures/java/org/opensearch/knn/KNNRestTestCase.java index a2ca398f6..4c9f3ae5f 100644 --- a/src/testFixtures/java/org/opensearch/knn/KNNRestTestCase.java +++ b/src/testFixtures/java/org/opensearch/knn/KNNRestTestCase.java @@ -14,6 +14,7 @@ import org.opensearch.core.common.bytes.BytesReference; import org.opensearch.common.xcontent.XContentHelper; import org.opensearch.core.xcontent.DeprecationHandler; +import org.opensearch.core.xcontent.MediaTypeRegistry; import org.opensearch.core.xcontent.NamedXContentRegistry; import org.opensearch.core.xcontent.XContentParser; import org.opensearch.index.query.MatchAllQueryBuilder; @@ -33,7 +34,6 @@ import org.opensearch.core.xcontent.ToXContent; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentFactory; -import org.opensearch.core.xcontent.MediaTypeParserRegistry; import org.opensearch.core.xcontent.MediaType; import org.opensearch.index.query.ExistsQueryBuilder; import org.opensearch.index.query.QueryBuilder; @@ -229,7 +229,7 @@ protected Response searchExists(String index, ExistsQueryBuilder existsQueryBuil protected List parseSearchResponse(String responseBody, String fieldName) throws IOException { @SuppressWarnings("unchecked") List hits = (List) ((Map) createParser( - MediaTypeParserRegistry.getDefaultMediaType().xContent(), + MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody ).map().get("hits")).get("hits"); @@ -248,7 +248,7 @@ protected List parseSearchResponse(String responseBody, String fieldN protected List parseSearchResponseScore(String responseBody, String fieldName) throws IOException { @SuppressWarnings("unchecked") List hits = (List) ((Map) createParser( - MediaTypeParserRegistry.getDefaultMediaType().xContent(), + MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody ).map().get("hits")).get("hits"); @@ -266,7 +266,7 @@ protected List parseSearchResponseScore(String responseBody, String field protected Double parseAggregationResponse(String responseBody, String aggregationName) throws IOException { @SuppressWarnings("unchecked") Map aggregations = ((Map) createParser( - MediaTypeParserRegistry.getDefaultMediaType().xContent(), + MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody ).map().get("aggregations")); @@ -373,7 +373,7 @@ public Map getIndexMappingAsMap(String index) throws Exception { String responseBody = EntityUtils.toString(response.getEntity()); - Map responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); + Map responseMap = createParser(MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody).map(); return (Map) ((Map) responseMap.get(index)).get("mappings"); } @@ -387,7 +387,7 @@ public int getDocCount(String indexName) throws Exception { String responseBody = EntityUtils.toString(response.getEntity()); - Map responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); + Map responseMap = createParser(MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody).map(); return (Integer) responseMap.get("count"); } @@ -504,7 +504,7 @@ protected Map getKnnDoc(final String index, final String docId) final Response response = client().performRequest(request); final Map responseMap = createParser( - MediaTypeParserRegistry.getDefaultMediaType().xContent(), + MediaTypeRegistry.getDefaultMediaType().xContent(), EntityUtils.toString(response.getEntity()) ).map(); @@ -582,7 +582,7 @@ protected Response executeWarmupRequest(List indices, final String baseU * Parse KNN Cluster stats from response */ protected Map parseClusterStatsResponse(String responseBody) throws IOException { - Map responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); + Map responseMap = createParser(MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody).map(); responseMap.remove("cluster_name"); responseMap.remove("_nodes"); responseMap.remove("nodes"); @@ -595,7 +595,7 @@ protected Map parseClusterStatsResponse(String responseBody) thr protected List> parseNodeStatsResponse(String responseBody) throws IOException { @SuppressWarnings("unchecked") Map responseMap = (Map) createParser( - MediaTypeParserRegistry.getDefaultMediaType().xContent(), + MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody ).map().get("nodes"); @@ -614,7 +614,7 @@ protected List> parseNodeStatsResponse(String responseBody) @SuppressWarnings("unchecked") protected int parseTotalSearchHits(String searchResponseBody) throws IOException { Map responseMap = (Map) createParser( - MediaTypeParserRegistry.getDefaultMediaType().xContent(), + MediaTypeRegistry.getDefaultMediaType().xContent(), searchResponseBody ).map().get("hits"); @@ -1156,7 +1156,7 @@ public void assertTrainingSucceeds(String modelId, int attempts, int delayInMill response = getModel(modelId, null); - responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), EntityUtils.toString(response.getEntity())) + responseMap = createParser(MediaTypeRegistry.getDefaultMediaType().xContent(), EntityUtils.toString(response.getEntity())) .map(); modelState = ModelState.getModelState((String) responseMap.get(MODEL_STATE)); @@ -1181,7 +1181,7 @@ public void assertTrainingFails(String modelId, int attempts, int delayInMillis) response = getModel(modelId, null); - responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), EntityUtils.toString(response.getEntity())) + responseMap = createParser(MediaTypeRegistry.getDefaultMediaType().xContent(), EntityUtils.toString(response.getEntity())) .map(); modelState = ModelState.getModelState((String) responseMap.get(MODEL_STATE)); diff --git a/src/testFixtures/java/org/opensearch/knn/ODFERestTestCase.java b/src/testFixtures/java/org/opensearch/knn/ODFERestTestCase.java index 8a68d124c..986a45e5e 100644 --- a/src/testFixtures/java/org/opensearch/knn/ODFERestTestCase.java +++ b/src/testFixtures/java/org/opensearch/knn/ODFERestTestCase.java @@ -49,7 +49,7 @@ import org.opensearch.core.xcontent.NamedXContentRegistry; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.core.xcontent.XContentParser; -import org.opensearch.core.xcontent.MediaTypeParserRegistry; +import org.opensearch.core.xcontent.MediaTypeRegistry; import org.opensearch.core.xcontent.MediaType; import org.opensearch.knn.plugin.KNNPlugin; import org.opensearch.core.rest.RestStatus; @@ -234,7 +234,7 @@ private List getModelIds() throws IOException, ParseException { final String responseBody = EntityUtils.toString(response.getEntity()); assertNotNull(responseBody); - final XContentParser parser = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody); + final XContentParser parser = createParser(MediaTypeRegistry.getDefaultMediaType().xContent(), responseBody); final SearchResponse searchResponse = SearchResponse.fromXContent(parser); return Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId).collect(Collectors.toList()); diff --git a/src/testFixtures/java/org/opensearch/knn/TestUtils.java b/src/testFixtures/java/org/opensearch/knn/TestUtils.java index 99ab607a7..12751dff8 100644 --- a/src/testFixtures/java/org/opensearch/knn/TestUtils.java +++ b/src/testFixtures/java/org/opensearch/knn/TestUtils.java @@ -6,10 +6,11 @@ package org.opensearch.knn; import com.google.common.collect.ImmutableMap; +import org.opensearch.common.xcontent.XContentHelper; +import org.opensearch.core.common.bytes.BytesArray; import org.opensearch.core.xcontent.DeprecationHandler; import org.opensearch.core.xcontent.NamedXContentRegistry; -import org.opensearch.common.xcontent.XContentFactory; -import org.opensearch.core.xcontent.MediaTypeParserRegistry; +import org.opensearch.core.xcontent.MediaTypeRegistry; import org.opensearch.knn.index.codec.util.KNNCodecUtil; import java.io.BufferedReader; import java.io.FileReader; @@ -252,9 +253,12 @@ private KNNCodecUtil.Pair readIndexData(String path) throws IOException { BufferedReader reader = new BufferedReader(new FileReader(path)); String line = reader.readLine(); while (line != null) { - Map doc = XContentFactory.xContent(MediaTypeParserRegistry.getDefaultMediaType()) - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, line) - .map(); + Map doc = XContentHelper.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + new BytesArray(line), + MediaTypeRegistry.getDefaultMediaType() + ).map(); idsList.add((Integer) doc.get("id")); @SuppressWarnings("unchecked")