Skip to content

Commit

Permalink
[Extensions] Adding cross version support for registering Transport A…
Browse files Browse the repository at this point in the history
…ctions (#7403)

* Adding cross version support for RegisterTransportActions for extensions

Signed-off-by: Sarat Vemulapalli <vemulapallisarat@gmail.com>
  • Loading branch information
saratvemulapalli committed May 3, 2023
1 parent 84d66e0 commit eacfb70
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 23 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased 2.x]
### Added
- [Extensions] Moving Extensions APIs to protobuf serialization. ([#6960](https://github.com/opensearch-project/OpenSearch/pull/6960))
- [Extensions] Moving Extensions APIs to support cross versions via protobuf. ([#7402](https://github.com/opensearch-project/OpenSearch/issues/7402))
- [Extensions] Add IdentityPlugin into core to support Extension identities ([#7246](https://github.com/opensearch-project/OpenSearch/pull/7246))
- Add descending order search optimization through reverse segment read. ([#7244](https://github.com/opensearch-project/OpenSearch/pull/7244))
- [Extensions] Moving RestActions APIs to protobuf serialization. ([#7302](https://github.com/opensearch-project/OpenSearch/pull/7302))

### Dependencies
- Bump `jackson` from 2.14.2 to 2.15.0 ([#7286](https://github.com/opensearch-project/OpenSearch/pull/7286)
Expand Down
12 changes: 12 additions & 0 deletions DEVELOPER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- [Windows](#windows)
- [Docker](#docker)
- [Build](#build)
- [Generated Code](#generated-code)
- [Run Tests](#run-tests)
- [Run OpenSearch](#run-opensearch)
- [Use an Editor](#use-an-editor)
Expand Down Expand Up @@ -134,6 +135,17 @@ To build a distribution to run on your local platform, run:

All distributions built will be under `distributions/archives`.

#### Generated Code

OpenSearch uses code generators like [Protobuf](https://protobuf.dev/).
OpenSearch build system already takes a dependency of generating code from protobuf, incase you run into compilation errors, run:

```
./gradlew generateProto
```

Generated code in OpenSearch is used to establish cross version compatibility communication for API contracts within OpenSearch.

### Run Tests

OpenSearch uses a Gradle wrapper for its build. Run `gradlew` on Unix systems, or `gradlew.bat` on Windows in the root of the repository.
Expand Down
2 changes: 2 additions & 0 deletions server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ tasks.named("missingJavadoc").configure {
"org.opensearch.extensions.proto.RegisterRestActionsProto.RegisterRestActionsOrBuilder",
"org.opensearch.extensions.proto.ExtensionRequestProto",
"org.opensearch.extensions.proto.ExtensionRequestProto.ExtensionRequestOrBuilder",
"org.opensearch.extensions.proto.RegisterTransportActionsProto",
"org.opensearch.extensions.proto.RegisterTransportActionsProto.RegisterTransportActionsOrBuilder",
"org.opensearch.extensions.proto"
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@

import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.extensions.proto.RegisterTransportActionsProto.RegisterTransportActions;
import org.opensearch.extensions.proto.ExtensionIdentityProto.ExtensionIdentity;
import org.opensearch.transport.TransportRequest;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

Expand All @@ -23,52 +25,48 @@
* @opensearch.internal
*/
public class RegisterTransportActionsRequest extends TransportRequest {
// The uniqueId defining the extension which runs this action
private String uniqueId;
// The action names to register
private Set<String> transportActions;
private final RegisterTransportActions request;

public RegisterTransportActionsRequest(String uniqueId, Set<String> transportActions) {
this.uniqueId = uniqueId;
this.transportActions = transportActions;
ExtensionIdentity identity = ExtensionIdentity.newBuilder().setUniqueId(uniqueId).build();
this.request = RegisterTransportActions.newBuilder().setIdentity(identity).addAllTransportActions(transportActions).build();
}

public RegisterTransportActionsRequest(StreamInput in) throws IOException {
super(in);
this.uniqueId = in.readString();
this.transportActions = new HashSet<>(in.readStringList());
this.request = RegisterTransportActions.parseFrom(in.readByteArray());
}

public String getUniqueId() {
return uniqueId;
return request.getIdentity().getUniqueId();
}

public Set<String> getTransportActions() {
return transportActions;
public List<String> getTransportActions() {
return request.getTransportActionsList();
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeString(uniqueId);
out.writeStringCollection(transportActions);
out.writeByteArray(request.toByteArray());
}

@Override
public String toString() {
return "TransportActionsRequest{uniqueId=" + uniqueId + ", actions=" + transportActions + "}";
return "TransportActionsRequest{Identity=" + request.getIdentity() + ", actions=" + request.getTransportActionsList() + "}";
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
RegisterTransportActionsRequest that = (RegisterTransportActionsRequest) obj;
return Objects.equals(uniqueId, that.uniqueId) && Objects.equals(transportActions, that.transportActions);
return Objects.equals(request.getIdentity().getUniqueId(), that.request.getIdentity().getUniqueId())
&& Objects.equals(request.getTransportActionsList(), that.request.getTransportActionsList());
}

@Override
public int hashCode() {
return Objects.hash(uniqueId, transportActions);
return Objects.hash(request.getIdentity(), request.getTransportActionsList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

syntax = "proto3";
package org.opensearch.extensions.proto;

import "extensions/ExtensionIdentityProto.proto";
option java_outer_classname = "RegisterTransportActionsProto";

message RegisterTransportActions {
ExtensionIdentity identity = 1;
repeated string transportActions = 2;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
* compatible open source license.
*/

package org.opensearch.extensions;
package org.opensearch.extensions.action;

import org.junit.Before;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.extensions.action.RegisterTransportActionsRequest;
import org.opensearch.test.OpenSearchTestCase;

import java.io.IOException;
import java.util.List;
import java.util.Set;

public class RegisterTransportActionsRequestTests extends OpenSearchTestCase {
Expand All @@ -37,6 +37,32 @@ public void testRegisterTransportActionsRequest() throws IOException {
}

public void testToString() {
assertEquals(originalRequest.toString(), "TransportActionsRequest{uniqueId=extension-uniqueId, actions=[testAction]}");
logger.info(originalRequest.toString());
assertEquals(
originalRequest.toString(),
"TransportActionsRequest{Identity=uniqueId: \"extension-uniqueId\"\n, actions=[testAction]}"
);
}

public void testNoIdentityRegisterTransportActionsRequest() {
String uniqueId = null;
// Expect exception as Extension Identity(uniqueId) is null
expectThrows(NullPointerException.class, () -> new RegisterTransportActionsRequest(uniqueId, Set.of()));
}

public void testNoTransportActionsRequest() {
String uniqueId = "extension-1234";
Set<String> expectedActions = null;
// Expect exception as paths are null
expectThrows(NullPointerException.class, () -> new RegisterTransportActionsRequest(uniqueId, expectedActions));
}

public void testEmptyTransportActionsRequest() {
String uniqueId = "extension-1234";
Set<String> expectedActions = Set.of();
RegisterTransportActionsRequest request = new RegisterTransportActionsRequest(uniqueId, expectedActions);

assertEquals(uniqueId, request.getUniqueId());
assertEquals(List.of(), request.getTransportActions());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void testNoRestActionsRequest() {
String uniqueId = "extension-1234";
List<String> expected = null;
List<String> expectedDeprecated = null;
// Expect exception as Extension Identity(uniqueId) is null
// Expect exception as paths are null
expectThrows(NullPointerException.class, () -> new RegisterRestActionsRequest(uniqueId, expected, expectedDeprecated));
}

Expand Down

0 comments on commit eacfb70

Please sign in to comment.