Skip to content

gRPC support for opensearch-java#2062

Open
dayana-j wants to merge 18 commits into
opensearch-project:mainfrom
dayana-j:grpc-java
Open

gRPC support for opensearch-java#2062
dayana-j wants to merge 18 commits into
opensearch-project:mainfrom
dayana-j:grpc-java

Conversation

@dayana-j

@dayana-j dayana-j commented Jul 15, 2026

Copy link
Copy Markdown

Description

This PR adds a transparent gRPC support to the opensearch-java as a separate java-client-grpc module with a translation layer, transport layers, connection management, and security implementation for gRPC such as TLS/basic auth, SigV4, and JWT. Bulk operations are automatically routed over gRPC for improved performance; lower latency, higher throughput, and smaller payloads via HTTP/2 and Protocol Buffers, while all other operations fall back to REST seamlessly. Users configure the transport once and use the standard OpenSearchClient API without any protobuf imports, channel management, or code changes.

OpenSearch 3.5+ exposes a gRPC endpoint (default port 9400) alongside REST for Bulk and k-NN operations. This implementation bridges the gap so that existing client code benefits from gRPC performance without migration effort.
The gRPC transport lives in its own module to isolate gRPC dependencies (grpc-netty-shaded, protobuf-java) from the core client, preventing classpath conflicts with the OpenSearch test framework.

Users add the gRPC module as an optional dependency:

dependencies {
    implementation 'org.opensearch.client:opensearch-java:3.x'
    implementation 'org.opensearch.client:opensearch-java-grpc:3.x'  // optional
}

How It Works

The HybridTransport wraps both a GrpcTransport (for bulk) and a standard REST transport (for everything else). When client.bulk() is called, the request is converted from the client's Java types to protobuf, sent over gRPC, and the response is converted back — all transparently. If gRPC is unavailable or the conversion fails, the request automatically retries via REST.

var restTransport = ApacheHttpClient5TransportBuilder.builder(new HttpHost("http", "localhost", 9200)).build();

var grpcTransport = GrpcTransport.builder("localhost", 9400)
    .jsonpMapper(new JacksonJsonpMapper())
    .basicAuth("admin", "admin")
    .build();

var client = new OpenSearchClient(new HybridTransport(grpcTransport, restTransport));

client.bulk(bulkRequest);   // → gRPC (fast path)
client.search(searchReq);   // → REST (automatic fallback)

Authentication

All three authentication methods supported by OpenSearch are implemented as gRPC ClientInterceptors that attach credentials to every outgoing call's metadata.

TLS + Basic Auth:

var grpcTransport = GrpcTransport.builder("localhost", 9400)
    .jsonpMapper(new JacksonJsonpMapper())
    .tls(GrpcTlsConfig.builder()
        .trustCertificatePath("/path/to/ca.pem")
        .build())
    .basicAuth("admin", "admin")
    .build();

AWS SigV4 signs each gRPC call using AWS SDK v2 credential providers. The interceptor constructs a synthetic URL from the gRPC method path, signs it with AwsV4HttpSigner, and attaches the Authorization headers as gRPC metadata. Body-aware payload signing is supported for full SigV4 compliance.

var grpcTransport = GrpcTransport.builder("domain.us-east-1.es.amazonaws.com", 9400)
    .jsonpMapper(new JacksonJsonpMapper())
    .tls(GrpcTlsConfig.builder().build())
    .sigV4(GrpcSigV4Config.builder()
        .region(Region.US_EAST_1)
        .service("es")
        .credentialsProvider(DefaultCredentialsProvider.create())
        .build())
    .build();

JWT uses a token supplier called on every request for automatic token rotation:
.jwtAuth(() -> myOidcProvider.getAccessToken())

Connection Management

The gRPC ManagedChannel provides built-in connection health management that's more capable than REST's passive dead-node tracking. The channel automatically reconnects on failure with exponential backoff, and the GrpcChannelHealthMonitor exposes the connectivity state machine (IDLE → CONNECTING → READY → TRANSIENT_FAILURE) so callers can check readiness or register state change callbacks.

// Warm up the connection at startup
transport.healthMonitor().waitForReady(5, TimeUnit.SECONDS);

// Check before sending
if (transport.healthMonitor().isReady()) { /* connected */ }

Retry logic is configurable via GrpcTransportOptions — max retries, backoff interval, keepalive pings, idle timeout, and max inbound message size all have sensible defaults that match the OpenSearch server's gRPC settings.

Translation Layer

BulkRequestConverter walks the typed BulkOperation list, serializes document bodies to JSON bytes via JsonpMapper, and builds the protobuf BulkRequest. BulkResponseConverter converts each protobuf ResponseItem back to the client's BulkResponseItem with all fields. Status code mapping follows the OpenSearch server's RestToGrpcStatusConverter.

Tests

The implementation includes 112 unit tests across 6 test files covering all conversion logic, transport routing, authentication interceptors, channel management, and TLS configuration. Three condensed integration tests validate end-to-end behavior against a real OpenSearch 3.5+ cluster using the project's existing test framework (OpenSearchJavaClientTestCase + Testcontainers). Tests are version-gated via assumeTrue and skip automatically on older clusters.

CI Configuration

The Docker configuration in .ci/opensearch/ enables gRPC transport (port 9400) in the test container. A standalone validate-grpc-integration.sh script is included for local validation. gRPC integration tests run via ./gradlew :java-client-grpc:test.

Dependencies

gRPC support is configured as an optional feature (grpcSupport) with zero impact on users who don't enable it. Dependencies are io.grpc (1.68.0), com.google.protobuf (3.25.5), and org.opensearch:protobufs (1.2.0).

Related

This is the Java counterpart to the opensearch-py gRPC implementation, following the same design pattern:

OpenSearch gRPC API documentation: https://docs.opensearch.org/latest/api-reference/grpc-apis/index/
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@dayana-j
dayana-j force-pushed the grpc-java branch 2 times, most recently from 5e44f37 to cc3185c Compare July 15, 2026 17:51
@dayana-j
dayana-j marked this pull request as draft July 15, 2026 17:52
@dayana-j
dayana-j marked this pull request as ready for review July 15, 2026 18:44
@dayana-j dayana-j changed the title Transparent gRPC Transport for opensearch-java gRPC Transport for opensearch-java Jul 15, 2026
@dayana-j dayana-j changed the title gRPC Transport for opensearch-java gRPC support for opensearch-java Jul 15, 2026
Adds a transparent gRPC transport layer that routes bulk operations
over gRPC for improved performance while falling back to REST for
all other operations. Isolated in a separate java-client-grpc module
to prevent classpath conflicts.

Includes:
- GrpcTransport + HybridTransport (automatic routing and fallback)
- Translation layer (BulkRequest/Response <-> protobuf conversion)
- TLS support (trust cert, trust store, mTLS, insecure, hostname override)
- Basic auth, AWS SigV4, and JWT authentication interceptors
- Channel health monitoring via gRPC connectivity state machine
- Integration tests (framework-compliant, version-gated to 3.5.0+)
- Sample code and CI configuration

Signed-off-by: Dayana Jean <jeadayao@amazon.com>
Signed-off-by: Dayana Jean <jeadayao@amazon.com>
@finnegancarroll

Copy link
Copy Markdown

If gRPC is unavailable or the conversion fails, the request automatically retries via REST.

I think if we encounter some gRPC error we should explicitly propagate this back to the user instead of silently falling back to REST here.

Comment thread java-client-grpc/build.gradle.kts Outdated
Comment thread java-client-grpc/build.gradle.kts Outdated
Comment thread java-client-grpc/build.gradle.kts
@reta

reta commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator

Thanks @dayana-j , good one, did a first pass, look very solid in general! Could you please also add OpenSearch 3.5.0 (or 3.6.0) to the test matrix here .github/workflows/test-integration.yml ? Thank you.

dayana-j and others added 5 commits July 20, 2026 14:12
Co-authored-by: Andriy Redko <drreta@gmail.com>
Signed-off-by: Dayana <jeandayana28@gmail.com>
Apply suggestion from @reta

Co-authored-by: Andriy Redko <drreta@gmail.com>
Signed-off-by: Dayana <jeandayana28@gmail.com>
Apply suggestion from @reta

Co-authored-by: Andriy Redko <drreta@gmail.com>
Signed-off-by: Dayana <jeandayana28@gmail.com>
…ivate classes

- Removed GrpcStatusConverter.toHttpStatus() which was unused anywhere
  in the codebase (per maintainer feedback)
- Moved TranslationTest to the translation package so it can access
  package-private FieldMappingUtil and GrpcStatusConverter.convert()

Signed-off-by: Dayana Jean <jeadayao@amazon.com>
Follows the same pattern as ApacheHttpClient5Transport (general) vs
AwsSdk2Transport (AWS-specific) in the existing codebase.

Changes:
- Created AwsGrpcTransport: extends GrpcTransport, adds SigV4 signing
  - AwsGrpcTransport.awsBuilder(host, port).sigV4(...).tls(...).build()
  - Requires sigV4Config and TLS
  - Overrides preProcessBulk() for payload hash computation
- Removed SigV4 from GrpcTransport:
  - Removed .sigV4() builder method
  - Removed sigV4Interceptor field
  - Added protected preProcessBulk() hook for subclasses
- GrpcTransport is now purely general-purpose (basic auth, JWT, TLS)
- Updated tests to use AwsGrpcTransport for SigV4 tests

Signed-off-by: Dayana Jean <jeadayao@amazon.com>
dayana-j added 7 commits July 20, 2026 14:57
Per maintainer feedback: if the intent is to use gRPC for a supported
endpoint, errors should propagate to the user rather than silently
falling back to REST.

Changes:
- HybridTransport no longer catches gRPC errors and retries via REST
- Removed fallbackOnError constructor parameter and field
- Routing behavior preserved: unsupported endpoints → REST directly
- gRPC-supported endpoints: errors propagate to caller
- Simplified performRequest/performRequestAsync (no try/catch)
- Updated tests: testGrpcErrorPropagatesForSupportedEndpoint

Behavior:
  client.bulk(req)   → gRPC (errors propagate if gRPC fails)
  client.search(req) → REST (not supported by gRPC, routed directly)

Signed-off-by: Dayana Jean <jeadayao@amazon.com>
Stale references to 'automatic REST fallback' replaced with accurate
'REST routing for unsupported endpoints' language throughout.

- GrpcTransport: updated javadoc and error message
- GrpcDemo: rewrote Demo 3 to show REST routing (not fallback on error)
- GrpcAwsSigV4: updated to use AwsGrpcTransport.awsBuilder()
- GrpcBulkIT: renamed testRestFallback → testRestRouting

Signed-off-by: Dayana Jean <jeadayao@amazon.com>
Per maintainer feedback: OpenSearchJavaClientTestCase and
TestcontainersThreadFilter are internal test classes not exposed for
external module use.

Changes:
- AbstractGrpcIT: now extends nothing, uses standard JUnit 4 + Assume
  - Removed dependency on OpenSearchJavaClientTestCase
  - Removed @ThreadLeakFilters(TestcontainersThreadFilter)
  - Uses Assume.assumeTrue() with manual version parsing
  - Reads cluster config from system properties directly
- GrpcTransportSupport: converted from interface (extending
  OpenSearchTransportSupport) to utility class
- GrpcBulkIT: no longer implements GrpcTransportSupport interface

Signed-off-by: Dayana Jean <jeadayao@amazon.com>
Per maintainer feedback: opensearch-java baseline is JDK 8 and gRPC-Java
supports Java 8. No technical reason to require JDK 11.

Changes:
- build.gradle.kts: targetCompatibility/sourceCompatibility → 1.8
- GrpcSigV4Test: replaced 'var' (Java 10+) with explicit types

Signed-off-by: Dayana Jean <jeadayao@amazon.com>
Per maintainer feedback: opensearch-java has a hard dependency on
Jackson 3.x, so it comes transitively. No need to declare it again.

Signed-off-by: Dayana Jean <jeadayao@amazon.com>
Per maintainer feedback: integration tests were not being compiled or
run. Added the standard java21 source set configuration used by
java-client to java-client-grpc.

Changes:
- Added unitTest/integrationTest task definitions
- Added java21 source set (src/test/java11) gated on JDK 21+
- Added test framework, testcontainers, opensearch-testcontainers deps
- Added static import for JUnit Assert in GrpcBulkIT
- Integration tests now compile and will run with:
  ./gradlew :java-client-grpc:integrationTest -Dtests.opensearch.version=3.5.0

Signed-off-by: Dayana Jean <jeadayao@amazon.com>
Adds the first gRPC-capable version to the test matrix so the gRPC
integration tests run in CI. Includes both Java 21 and Java 25.

Signed-off-by: Dayana Jean <jeadayao@amazon.com>
dayana-j added 4 commits July 20, 2026 15:29
Spotless rejects wildcard imports. Replaced 'import static org.junit.Assert.*'
with explicit imports for assertEquals, assertFalse, assertNotNull, assertTrue.

Signed-off-by: Dayana Jean <jeadayao@amazon.com>
The java21 source set only contains integration tests (integTest package).
Adding it to unitTest causes 'No tests found' failure since the filter
excludes integTest classes. Only integrationTest needs the java21 classes.

Signed-off-by: Dayana Jean <jeadayao@amazon.com>
- GrpcDemo.java: replaced custom header with standard Apache-2.0 license
- GrpcAwsSigV4.java: fixed import ordering (AwsGrpcTransport alphabetical)
- Removed unused imports

Signed-off-by: Dayana Jean <jeadayao@amazon.com>
assumeGrpcSupported() now verifies both:
1. Server version is 3.5.0+ (via REST info endpoint)
2. gRPC port is actually reachable (TCP socket check)

This prevents test failures when running integrationTest against
OpenSearch versions that don't have gRPC enabled or when the gRPC
port isn't exposed by testcontainers.

Signed-off-by: Dayana Jean <jeadayao@amazon.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants