Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Add temporal routing processors for time-based document routing ([#18920](https://github.com/opensearch-project/OpenSearch/issues/18920))
- The dynamic mapping parameter supports false_allow_templates ([#19065](https://github.com/opensearch-project/OpenSearch/pull/19065))
- Add a toBuilder method in EngineConfig to support easy modification of configs([#19054](https://github.com/opensearch-project/OpenSearch/pull/19054))
- Publish transport-grpc-spi exposing QueryBuilderProtoConverter and QueryBuilderProtoConverterRegistry ([#18949](https://github.com/opensearch-project/OpenSearch/pull/18949))

### Changed
- Add CompletionStage variants to methods in the Client Interface and default to ActionListener impl ([#18998](https://github.com/opensearch-project/OpenSearch/pull/18998))
Expand Down
1 change: 1 addition & 0 deletions modules/transport-grpc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ testClusters {
}

dependencies {
api project('spi')
compileOnly "com.google.code.findbugs:jsr305:3.0.2"
runtimeOnly "com.google.guava:guava:${versions.guava}"
implementation "com.google.errorprone:error_prone_annotations:2.24.1"
Expand Down
118 changes: 118 additions & 0 deletions modules/transport-grpc/spi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# transport-grpc-spi

Service Provider Interface (SPI) for the OpenSearch gRPC transport module. This module provides interfaces and utilities that allow external plugins to extend the gRPC transport functionality.

## Overview

The `transport-grpc-spi` module enables plugin developers to:
- Implement custom query converters for gRPC transport
- Extend gRPC protocol buffer handling
- Register custom query types that can be processed via gRPC

## Key Components

### QueryBuilderProtoConverter

Interface for converting protobuf query messages to OpenSearch QueryBuilder objects.

```java
public interface QueryBuilderProtoConverter {
QueryContainer.QueryContainerCase getHandledQueryCase();
QueryBuilder fromProto(QueryContainer queryContainer);
}
```

### QueryBuilderProtoConverterSpiRegistry

Registry that manages and discovers all available query converters. External plugins can register their custom converters through this registry.

## Usage for Plugin Developers

### 1. Add Dependency

Add the SPI dependency to your plugin's `build.gradle`:

```gradle
dependencies {
compileOnly 'org.opensearch.plugin:transport-grpc-spi:${opensearch.version}'
compileOnly 'org.opensearch:protobufs:${protobufs.version}'
}
```

### 2. Implement Custom Query Converter

```java
public class MyCustomQueryConverter implements QueryBuilderProtoConverter {

@Override
public QueryContainer.QueryContainerCase getHandledQueryCase() {
return QueryContainer.QueryContainerCase.MY_CUSTOM_QUERY;
}

@Override
public QueryBuilder fromProto(QueryContainer queryContainer) {
// Convert your custom protobuf query to QueryBuilder
MyCustomQuery customQuery = queryContainer.getMyCustomQuery();
return new MyCustomQueryBuilder(customQuery.getField(), customQuery.getValue());
}
}
```

### 3. Register Your Converter

In your plugin's main class, register the converter:

```java
public class MyPlugin extends Plugin implements ExtensiblePlugin {

@Override
public Collection<Object> createComponents(Client client, ClusterService clusterService,
ThreadPool threadPool, ResourceWatcherService resourceWatcherService,
ScriptService scriptService, NamedXContentRegistry xContentRegistry,
Environment environment, NodeEnvironment nodeEnvironment,
NamedWriteableRegistry namedWriteableRegistry,
IndexNameExpressionResolver indexNameExpressionResolver,
Supplier<RepositoriesService> repositoriesServiceSupplier) {

// Get the registry and register your converter
QueryBuilderProtoConverterSpiRegistry registry =
// Obtain registry from OpenSearch's dependency injection
registry.registerConverter(new MyCustomQueryConverter());

return Collections.emptyList();
}
}
```

## Testing

### Unit Tests

```bash
./gradlew :modules:transport-grpc:spi:test
```

### Testing Your Custom Converter

```java
@Test
public void testCustomQueryConverter() {
MyCustomQueryConverter converter = new MyCustomQueryConverter();

// Create test protobuf query
QueryContainer queryContainer = QueryContainer.newBuilder()
.setMyCustomQuery(MyCustomQuery.newBuilder()
.setField("test_field")
.setValue("test_value")
.build())
.build();

// Convert and verify
QueryBuilder result = converter.fromProto(queryContainer);
assertThat(result, instanceOf(MyCustomQueryBuilder.class));

MyCustomQueryBuilder customQuery = (MyCustomQueryBuilder) result;
assertEquals("test_field", customQuery.fieldName());
assertEquals("test_value", customQuery.value());
}
```
22 changes: 22 additions & 0 deletions modules/transport-grpc/spi/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

apply plugin: 'opensearch.build'
apply plugin: 'opensearch.publish'

base {
group = 'org.opensearch.plugin'
archivesName = 'transport-grpc-spi'
}

dependencies {
api project(":server")
api "org.opensearch:protobufs:0.6.0"

testImplementation project(":test:framework")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1675c5085e1376fd1a107b87f7e325944ab5b4dc
Loading
Loading