Skip to content

Commit

Permalink
Porting different Backward incompatible changes happened in Core to 2…
Browse files Browse the repository at this point in the history
….x branch (#253)

* Fixed compilation errors after refactoring in core (#249)

Signed-off-by: Martin Gaievski <gaievski@amazon.com>

* Fixing test classes after core refactoring (#237)

Signed-off-by: Martin Gaievski <gaievski@amazon.com>

* Fixed compilation errors after refactoring in core Strings class (#243)

Signed-off-by: Martin Gaievski <gaievski@amazon.com>

* Switch from XContentType to MediaType to fix compilation errors (#232)

Signed-off-by: Martin Gaievski <gaievski@amazon.com>

* Fixing the Media type being not able to be read from Headers.

Signed-off-by: Navneet Verma <navneev@amazon.com>

---------

Signed-off-by: Martin Gaievski <gaievski@amazon.com>
Signed-off-by: Navneet Verma <navneev@amazon.com>
Co-authored-by: Martin Gaievski <gaievski@amazon.com>
  • Loading branch information
navneet1v and martin-gaievski committed Aug 17, 2023
1 parent 8a7ab9b commit adf3925
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;

import org.opensearch.action.ActionListener;
import org.opensearch.core.action.ActionListener;
import org.opensearch.ml.client.MachineLearningNodeClient;
import org.opensearch.ml.common.FunctionName;
import org.opensearch.ml.common.dataset.MLInputDataset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import lombok.extern.log4j.Log4j2;

import org.apache.commons.lang3.StringUtils;
import org.opensearch.action.ActionListener;
import org.opensearch.core.action.ActionListener;
import org.opensearch.env.Environment;
import org.opensearch.index.mapper.MapperService;
import org.opensearch.ingest.AbstractProcessor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.lucene.search.Query;
import org.opensearch.action.ActionListener;
import org.opensearch.common.SetOnce;
import org.opensearch.core.ParseField;
import org.opensearch.core.action.ActionListener;
import org.opensearch.core.common.ParsingException;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.opensearch.common.settings.Settings;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.core.xcontent.DeprecationHandler;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.XContentParser;
Expand Down Expand Up @@ -131,14 +131,12 @@ protected boolean preserveIndicesUponCompletion() {
@After
public void deleteExternalIndices() throws IOException {
final Response response = client().performRequest(new Request("GET", "/_cat/indices?format=json" + "&expand_wildcards=all"));
final XContentType xContentType = XContentType.fromMediaType(response.getEntity().getContentType().getValue());
try (
final XContentParser parser = xContentType.xContent()
.createParser(
NamedXContentRegistry.EMPTY,
DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
response.getEntity().getContent()
)
final XContentParser parser = JsonXContent.jsonXContent.createParser(
NamedXContentRegistry.EMPTY,
DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
response.getEntity().getContent()
)
) {
final XContentParser.Token token = parser.nextToken();
final List<Map<String, Object>> parserList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.junit.Assert;
import org.opensearch.client.Request;
import org.opensearch.client.Response;
import org.opensearch.common.Strings;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.rest.RestRequest;
Expand Down Expand Up @@ -62,21 +61,20 @@ public void testValidateMLPluginSetup() throws IOException {
}

private void createBasicKnnIndex() throws IOException {
String mapping = Strings.toString(
XContentFactory.jsonBuilder()
.startObject()
.startObject("properties")
.startObject(KNN_VECTOR_FIELD_NAME)
.field("type", "knn_vector")
.field("dimension", Integer.toString(3))
.startObject("method")
.field("engine", "lucene")
.field("name", "hnsw")
.endObject()
.endObject()
.endObject()
.endObject()
);
String mapping = XContentFactory.jsonBuilder()
.startObject()
.startObject("properties")
.startObject(KNN_VECTOR_FIELD_NAME)
.field("type", "knn_vector")
.field("dimension", Integer.toString(3))
.startObject("method")
.field("engine", "lucene")
.field("name", "hnsw")
.endObject()
.endObject()
.endObject()
.endObject()
.toString();
mapping = mapping.substring(1, mapping.length() - 1);
createIndex(KNN_INDEX_NAME, Settings.EMPTY, mapping);
}
Expand All @@ -99,16 +97,15 @@ private Set<String> getAllInstalledPlugins() throws IOException {
}

private void indexDocument() throws IOException {
final String indexRequestBody = Strings.toString(
XContentFactory.jsonBuilder()
.startObject()
.startArray(KNN_VECTOR_FIELD_NAME)
.value(1.0)
.value(2.0)
.value(4.0)
.endArray()
.endObject()
);
final String indexRequestBody = XContentFactory.jsonBuilder()
.startObject()
.startArray(KNN_VECTOR_FIELD_NAME)
.value(1.0)
.value(2.0)
.value(4.0)
.endArray()
.endObject()
.toString();
final Request indexRequest = new Request(RestRequest.Method.POST.name(), KNN_DOCUMENT_URL);
indexRequest.setJsonEntity(indexRequestBody);
assertOK(client().performRequest(indexRequest));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.opensearch.client.Response;
import org.opensearch.client.RestClient;
import org.opensearch.client.WarningsHandler;
import org.opensearch.common.Strings;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.common.xcontent.XContentType;
Expand Down Expand Up @@ -82,7 +81,7 @@ protected void updateClusterSettings(String settingKey, Object value) {
"PUT",
"_cluster/settings",
null,
toHttpEntity(Strings.toString(builder)),
toHttpEntity(builder.toString()),
ImmutableList.of(new BasicHeader(HttpHeaders.USER_AGENT, ""))
);

Expand All @@ -102,7 +101,7 @@ protected String uploadModel(String requestBody) throws Exception {
ImmutableList.of(new BasicHeader(HttpHeaders.USER_AGENT, "Kibana"))
);
Map<String, Object> uploadResJson = XContentHelper.convertToMap(
XContentFactory.xContent(XContentType.JSON),
XContentType.JSON.xContent(),
EntityUtils.toString(uploadResponse.getEntity()),
false
);
Expand Down Expand Up @@ -131,7 +130,7 @@ protected void loadModel(String modelId) throws IOException, InterruptedExceptio
ImmutableList.of(new BasicHeader(HttpHeaders.USER_AGENT, "Kibana"))
);
Map<String, Object> uploadResJson = XContentHelper.convertToMap(
XContentFactory.xContent(XContentType.JSON),
XContentType.JSON.xContent(),
EntityUtils.toString(uploadResponse.getEntity()),
false
);
Expand Down Expand Up @@ -180,7 +179,7 @@ protected float[] runInference(String modelId, String queryText) {
);

Map<String, Object> inferenceResJson = XContentHelper.convertToMap(
XContentFactory.xContent(XContentType.JSON),
XContentType.JSON.xContent(),
EntityUtils.toString(inferenceResponse.getEntity()),
false
);
Expand Down Expand Up @@ -210,7 +209,7 @@ protected void createIndexWithConfiguration(String indexName, String indexConfig
ImmutableList.of(new BasicHeader(HttpHeaders.USER_AGENT, "Kibana"))
);
Map<String, Object> node = XContentHelper.convertToMap(
XContentFactory.xContent(XContentType.JSON),
XContentType.JSON.xContent(),
EntityUtils.toString(response.getEntity()),
false
);
Expand All @@ -234,7 +233,7 @@ protected void createPipelineProcessor(String modelId, String pipelineName) thro
ImmutableList.of(new BasicHeader(HttpHeaders.USER_AGENT, "Kibana"))
);
Map<String, Object> node = XContentHelper.convertToMap(
XContentFactory.xContent(XContentType.JSON),
XContentType.JSON.xContent(),
EntityUtils.toString(pipelineCreateResponse.getEntity()),
false
);
Expand Down Expand Up @@ -293,14 +292,14 @@ protected Map<String, Object> search(String index, QueryBuilder queryBuilder, Qu

Request request = new Request("POST", "/" + index + "/_search");
request.addParameter("size", Integer.toString(resultSize));
request.setJsonEntity(Strings.toString(builder));
request.setJsonEntity(builder.toString());

Response response = client().performRequest(request);
assertEquals(request.getEndpoint() + ": failed", RestStatus.OK, RestStatus.fromCode(response.getStatusLine().getStatusCode()));

String responseBody = EntityUtils.toString(response.getEntity());

return XContentHelper.convertToMap(XContentFactory.xContent(XContentType.JSON), responseBody, false);
return XContentHelper.convertToMap(XContentType.JSON.xContent(), responseBody, false);
}

/**
Expand Down Expand Up @@ -345,7 +344,7 @@ protected void addKnnDoc(
}
builder.endObject();

request.setJsonEntity(Strings.toString(builder));
request.setJsonEntity(builder.toString());
Response response = client().performRequest(request);
assertEquals(request.getEndpoint() + ": failed", RestStatus.CREATED, RestStatus.fromCode(response.getStatusLine().getStatusCode()));
}
Expand Down Expand Up @@ -411,11 +410,7 @@ protected Map<String, Object> getTaskQueryResponse(String taskId) throws IOExcep
toHttpEntity(""),
ImmutableList.of(new BasicHeader(HttpHeaders.USER_AGENT, "Kibana"))
);
return XContentHelper.convertToMap(
XContentFactory.xContent(XContentType.JSON),
EntityUtils.toString(taskQueryResponse.getEntity()),
false
);
return XContentHelper.convertToMap(XContentType.JSON.xContent(), EntityUtils.toString(taskQueryResponse.getEntity()), false);
}

protected boolean checkComplete(Map<String, Object> node) {
Expand Down Expand Up @@ -445,7 +440,8 @@ private String buildIndexConfiguration(List<KNNFieldConfig> knnFieldConfigs) {
.endObject()
.endObject();
}
return Strings.toString(xContentBuilder.endObject().endObject().endObject());
xContentBuilder.endObject().endObject().endObject();
return xContentBuilder.toString();
}

protected static Response makeRequest(
Expand Down Expand Up @@ -512,7 +508,7 @@ private String registerModelGroup() throws IOException, URISyntaxException {
ImmutableList.of(new BasicHeader(HttpHeaders.USER_AGENT, "Kibana"))
);
Map<String, Object> modelGroupResJson = XContentHelper.convertToMap(
XContentFactory.xContent(XContentType.JSON),
XContentType.JSON.xContent(),
EntityUtils.toString(modelGroupResponse.getEntity()),
false
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.opensearch.action.ActionListener;
import org.opensearch.cluster.node.DiscoveryNode;
import org.opensearch.core.action.ActionListener;
import org.opensearch.ml.client.MachineLearningNodeClient;
import org.opensearch.ml.common.input.MLInput;
import org.opensearch.ml.common.output.MLOutput;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.opensearch.client.Response;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.neuralsearch.common.BaseNeuralSearchIT;
Expand Down Expand Up @@ -71,7 +70,7 @@ private void ingestDocument() throws Exception {
ImmutableList.of(new BasicHeader(HttpHeaders.USER_AGENT, "Kibana"))
);
Map<String, Object> map = XContentHelper.convertToMap(
XContentFactory.xContent(XContentType.JSON),
XContentType.JSON.xContent(),
EntityUtils.toString(response.getEntity()),
false
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.opensearch.OpenSearchParseException;
import org.opensearch.action.ActionListener;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.action.ActionListener;
import org.opensearch.env.Environment;
import org.opensearch.ingest.IngestDocument;
import org.opensearch.ingest.Processor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@

import lombok.SneakyThrows;

import org.opensearch.action.ActionListener;
import org.opensearch.client.Client;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.ParseField;
import org.opensearch.core.action.ActionListener;
import org.opensearch.core.common.ParsingException;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.common.io.stream.FilterStreamInput;
Expand Down

0 comments on commit adf3925

Please sign in to comment.