Skip to content

Commit

Permalink
Restore support for Java 8 (opensearch-project#767)
Browse files Browse the repository at this point in the history
* Restore support for Java 8

Signed-off-by: Andriy Redko <[email protected]>

* Address code review comments (update compatibility)

Signed-off-by: Andriy Redko <[email protected]>

---------

Signed-off-by: Andriy Redko <[email protected]>
(cherry picked from commit ff5e0a2)
  • Loading branch information
reta committed Dec 20, 2023
1 parent e7c3f66 commit 3047d50
Show file tree
Hide file tree
Showing 60 changed files with 163 additions and 95 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test-integration-unreleased.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
- { opensearch_ref: '1.x', java: 11 }
- { opensearch_ref: '2.x', java: 11 }
- { opensearch_ref: '2.x', java: 17 }
- { opensearch_ref: '2.x', java: 21 }
- { opensearch_ref: 'main', java: 11 }
- { opensearch_ref: 'main', java: 17 }
- { opensearch_ref: 'main', java: 21 }
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Bumps `com.diffplug.spotless` from 6.22.0 to 6.23.3

### Changed
- Restore support for Java 8 ([#767](https://github.com/opensearch-project/opensearch-java/pull/767))

### Deprecated

Expand Down
2 changes: 1 addition & 1 deletion COMPATIBILITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ The below matrix shows the compatibility of the [`opensearch-java-client`](https
| Client Version | JDK |
|----------------|--------------------|
| 1.0.0 | 8 |
| 2.x.0 | 11 / 17 / 21 |
| 2.x.0 | 8 / 11 / 17 / 21 |

## Upgrading

Expand Down
46 changes: 43 additions & 3 deletions java-client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ configurations {
}

java {
targetCompatibility = JavaVersion.VERSION_11
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_1_8

withJavadocJar()
withSourcesJar()
Expand Down Expand Up @@ -146,15 +146,20 @@ val integrationTest = task<Test>("integrationTest") {
System.getProperty("tests.awsSdk2support.domainRegion", "us-east-1"))
}

val opensearchVersion = "2.11.1"

dependencies {

val opensearchVersion = "2.7.0"
val jacksonVersion = "2.15.2"
val jacksonDatabindVersion = "2.15.2"

// Apache 2.0
implementation("org.opensearch.client", "opensearch-rest-client", opensearchVersion)
testImplementation("org.opensearch.test", "framework", opensearchVersion)
testImplementation("org.hamcrest:hamcrest:2.1")
testImplementation("com.carrotsearch.randomizedtesting:randomizedtesting-runner:2.7.1") {
exclude(group = "junit")
}

// Apache 2.0
// https://search.maven.org/artifact/com.google.code.findbugs/jsr305
Expand Down Expand Up @@ -327,3 +332,38 @@ publishing {
}
}
}

if (JavaVersion.current() >= JavaVersion.VERSION_11) {
val java11: SourceSet = sourceSets.create("java11") {
java {
compileClasspath += sourceSets.main.get().output + sourceSets.test.get().output
runtimeClasspath += sourceSets.main.get().output + sourceSets.test.get().output
srcDir("src/test/java11")
}
}

configurations[java11.implementationConfigurationName].extendsFrom(configurations.testImplementation.get())
configurations[java11.runtimeOnlyConfigurationName].extendsFrom(configurations.testRuntimeOnly.get())

dependencies {
testImplementation("org.opensearch.test", "framework", opensearchVersion) {
exclude(group = "org.hamcrest")
}
}

tasks.named<JavaCompile>("compileJava11Java") {
targetCompatibility = JavaVersion.VERSION_11.toString()
sourceCompatibility = JavaVersion.VERSION_11.toString()
}

tasks.named<JavaCompile>("compileTestJava") {
targetCompatibility = JavaVersion.VERSION_11.toString()
sourceCompatibility = JavaVersion.VERSION_11.toString()
}

tasks.test {
testClassesDirs += java11.output.classesDirs
classpath = sourceSets["java11"].runtimeClasspath
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ public CatRequestBase(CatRequestBaseBuilder<?> builder) {

protected final Map<String, String> queryParameters() {
Map<String, String> params = new HashMap<>();
if (headers != null && !headers.isBlank()) {
if (headers != null && !headers.trim().isEmpty()) {
params.put("h", headers);
}
if (sort != null && !sort.isBlank()) {
if (sort != null && !sort.trim().isEmpty()) {
params.put("s", sort);
}
params.put("format", "json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

package org.opensearch.client.transport;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -98,7 +99,7 @@ public Builder addHeader(String name, String value) {
if (headers.isEmpty()) {
headers = new ArrayList<>();
}
headers.add(Map.entry(name, value));
headers.add(new AbstractMap.SimpleEntry<>(name, value));
return this;
}

Expand Down Expand Up @@ -135,8 +136,8 @@ class DefaultImpl implements TransportOptions {
private final Function<List<String>, Boolean> onWarnings;

protected DefaultImpl(BuilderImpl builder) {
this.headers = builder.headers.isEmpty() ? Collections.emptyList() : List.copyOf(builder.headers);
this.params = builder.queryParameters.isEmpty() ? Collections.emptyMap() : Map.copyOf(builder.queryParameters);
this.headers = builder.headers.isEmpty() ? Collections.emptyList() : new ArrayList<>(builder.headers);
this.params = builder.queryParameters.isEmpty() ? Collections.emptyMap() : new HashMap<>(builder.queryParameters);
this.onWarnings = builder.onWarnings;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Supplier;
import java.util.zip.GZIPInputStream;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -261,11 +262,12 @@ private <RequestT> OpenSearchRequestBodyBuffer prepareRequestBody(
.map(o -> o instanceof AwsSdk2TransportOptions ? ((AwsSdk2TransportOptions) o) : null)
.map(AwsSdk2TransportOptions::mapper)
.orElse(defaultMapper);
final int maxUncompressedSize = Optional.ofNullable(options)
.map(o -> o instanceof AwsSdk2TransportOptions ? ((AwsSdk2TransportOptions) o) : null)
.map(AwsSdk2TransportOptions::requestCompressionSize)
.or(() -> Optional.ofNullable(transportOptions.requestCompressionSize()))
.orElse(DEFAULT_REQUEST_COMPRESSION_SIZE);
final int maxUncompressedSize = or(
Optional.ofNullable(options)
.map(o -> o instanceof AwsSdk2TransportOptions ? ((AwsSdk2TransportOptions) o) : null)
.map(AwsSdk2TransportOptions::requestCompressionSize),
() -> Optional.ofNullable(transportOptions.requestCompressionSize())
).orElse(DEFAULT_REQUEST_COMPRESSION_SIZE);

OpenSearchRequestBodyBuffer buffer = new OpenSearchRequestBodyBuffer(mapper, maxUncompressedSize);
buffer.addContent(request);
Expand All @@ -280,7 +282,7 @@ private <RequestT> SdkHttpFullRequest prepareRequest(
Endpoint<RequestT, ?, ?> endpoint,
@CheckForNull TransportOptions options,
@CheckForNull OpenSearchRequestBodyBuffer body
) {
) throws UnsupportedEncodingException {
SdkHttpFullRequest.Builder req = SdkHttpFullRequest.builder().method(SdkHttpMethod.fromValue(endpoint.method(request)));

StringBuilder url = new StringBuilder();
Expand All @@ -293,9 +295,9 @@ private <RequestT> SdkHttpFullRequest prepareRequest(
Map<String, String> params = endpoint.queryParameters(request);
if (params != null && !params.isEmpty()) {
char sep = '?';
for (var ent : params.entrySet()) {
for (Map.Entry<String, String> ent : params.entrySet()) {
url.append(sep).append(ent.getKey()).append('=');
url.append(URLEncoder.encode(ent.getValue(), StandardCharsets.UTF_8));
url.append(URLEncoder.encode(ent.getValue(), "UTF-8"));
sep = '&';
}
}
Expand All @@ -321,22 +323,24 @@ private <RequestT> SdkHttpFullRequest prepareRequest(
req.putHeader("x-amz-content-sha256", "required");
}

boolean responseCompression = Optional.ofNullable(options)
.map(o -> o instanceof AwsSdk2TransportOptions ? ((AwsSdk2TransportOptions) o) : null)
.map(AwsSdk2TransportOptions::responseCompression)
.or(() -> Optional.ofNullable(transportOptions.responseCompression()))
.orElse(Boolean.TRUE);
boolean responseCompression = or(
Optional.ofNullable(options)
.map(o -> o instanceof AwsSdk2TransportOptions ? ((AwsSdk2TransportOptions) o) : null)
.map(AwsSdk2TransportOptions::responseCompression),
() -> Optional.ofNullable(transportOptions.responseCompression())
).orElse(Boolean.TRUE);
if (responseCompression) {
req.putHeader("Accept-Encoding", "gzip");
} else {
req.removeHeader("Accept-Encoding");
}

final AwsCredentialsProvider credentials = Optional.ofNullable(options)
.map(o -> o instanceof AwsSdk2TransportOptions ? ((AwsSdk2TransportOptions) o) : null)
.map(AwsSdk2TransportOptions::credentials)
.or(() -> Optional.ofNullable(transportOptions.credentials()))
.orElse(DefaultCredentialsProvider.create());
final AwsCredentialsProvider credentials = or(
Optional.ofNullable(options)
.map(o -> o instanceof AwsSdk2TransportOptions ? ((AwsSdk2TransportOptions) o) : null)
.map(AwsSdk2TransportOptions::credentials),
() -> Optional.ofNullable(transportOptions.credentials())
).orElse(DefaultCredentialsProvider.create());

Aws4SignerParams signerParams = Aws4SignerParams.builder()
.awsCredentials(credentials.resolveCredentials())
Expand All @@ -346,7 +350,7 @@ private <RequestT> SdkHttpFullRequest prepareRequest(
return Aws4Signer.create().sign(req.build(), signerParams);
}

private void applyOptionsParams(StringBuilder url, TransportOptions options) {
private void applyOptionsParams(StringBuilder url, TransportOptions options) throws UnsupportedEncodingException {
if (options == null) {
return;
}
Expand All @@ -355,7 +359,7 @@ private void applyOptionsParams(StringBuilder url, TransportOptions options) {
char sep = url.indexOf("?") < 0 ? '?' : '&';
for (Map.Entry<String, String> param : params.entrySet()) {
url.append(sep).append(param.getKey()).append('=');
url.append(URLEncoder.encode(param.getValue(), StandardCharsets.UTF_8));
url.append(URLEncoder.encode(param.getValue(), "UTF-8"));
sep = '?';
}
}
Expand Down Expand Up @@ -524,4 +528,16 @@ private <ResponseT, ErrorT> ResponseT parseResponse(
}
}
}

private static <T> Optional<T> or(Optional<T> opt, Supplier<? extends Optional<? extends T>> supplier) {
Objects.requireNonNull(opt);
Objects.requireNonNull(supplier);
if (opt.isPresent()) {
return opt;
} else {
@SuppressWarnings("unchecked")
Optional<T> r = (Optional<T>) supplier.get();
return Objects.requireNonNull(r);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -932,9 +932,15 @@ public long getContentLength() {
if (chunkedEnabled.get()) {
return -1L;
} else {
long size;
long size = 0;
final byte[] buf = new byte[8192];
int nread = 0;

try (InputStream is = getContent()) {
size = is.readAllBytes().length;
// read to EOF which may read more or less than buffer size
while ((nread = is.read(buf)) > 0) {
size += nread;
}
} catch (IOException ex) {
size = -1L;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import jakarta.json.stream.JsonParser;
import java.io.StringReader;
import java.util.Map;
import java.util.Collections;
import junit.framework.TestCase;
import org.junit.Test;
import org.opensearch.client.json.JsonData;
Expand All @@ -30,7 +30,7 @@ public void testCreateKnnVectorMethodWithAll() {
KnnVectorMethod knnVectorMethod = new KnnVectorMethod.Builder().name("hnsw")
.spaceType("l2")
.engine("nmslib")
.parameters(Map.of("ef_construction", JsonData.of(128)))
.parameters(Collections.singletonMap("ef_construction", JsonData.of(128)))
.build();
assertEquals("hnsw", knnVectorMethod.name());
assertEquals("l2", knnVectorMethod.spaceType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.json.stream.JsonParser;
import java.io.StringReader;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -12,7 +13,9 @@
import org.opensearch.client.json.jsonb.JsonbJsonpMapper;
import org.opensearch.client.opensearch._types.mapping.IcuCollationKeywordProperty;
import org.opensearch.client.opensearch._types.mapping.Property;
import org.opensearch.client.opensearch._types.mapping.TypeMapping;
import org.opensearch.client.opensearch.indices.GetTemplateResponse;
import org.opensearch.client.opensearch.indices.TemplateMapping;

public class GetMappingsResponseTest extends Assert {

Expand All @@ -36,32 +39,27 @@ public void deserialize_IcuCollationKeywordExists_propertyDeserializes() throws
icuCollationConfig.put("strength", "quaternary");
icuCollationConfig.put("variable_top", "$");

mappingTemplate.put(
"test-index",
Map.of(
"aliases",
Collections.emptyMap(),
"index_patterns",
Collections.singletonList("test-pattern*"),
"mappings",
Map.of("properties", Map.of("icu_test_field", icuCollationConfig)),
"order",
0,
"settings",
Collections.emptyMap(),
"version",
1
)
final Map<String, Object> mappingTemplateMap = new HashMap<>();
mappingTemplateMap.put("aliases", Collections.emptyMap());
mappingTemplateMap.put("index_patterns", Collections.singletonList("test-pattern*"));
mappingTemplateMap.put(
"mappings",
Collections.singletonMap("properties", Collections.singletonMap("icu_test_field", icuCollationConfig))
);
mappingTemplateMap.put("order", 0);
mappingTemplateMap.put("settings", Collections.emptyMap());
mappingTemplateMap.put("version", 1);
mappingTemplate.put("test-index", mappingTemplateMap);

final JsonpMapper mapper = new JsonbJsonpMapper();
final String indexTemplate = new ObjectMapper().writeValueAsString(mappingTemplate);
final var parser = mapper.jsonProvider().createParser(new StringReader(indexTemplate));
final JsonParser parser = mapper.jsonProvider().createParser(new StringReader(indexTemplate));

final GetTemplateResponse response = GetTemplateResponse._DESERIALIZER.deserialize(parser, mapper);
final var template = response.get("test-index");
final var mappings = template.mappings();
final var properties = mappings.properties();
final var property = properties.get("icu_test_field");
final TemplateMapping template = response.get("test-index");
final TypeMapping mappings = template.mappings();
final Map<String, Property> properties = mappings.properties();
final Property property = properties.get("icu_test_field");
final IcuCollationKeywordProperty icu = property.icuCollationKeyword();

assertEquals(property._kind(), Property.Kind.IcuCollationKeyword);
Expand Down
Loading

0 comments on commit 3047d50

Please sign in to comment.