generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 108
Adding replication (CCR) plugin interface and classes to common-utils #667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
da68444
Adding replication (CCR) plugin interface and classes
aggarwalShivani 54c8310
Merge branch 'opensearch-project:main' into ccr-libs
aggarwalShivani a3900cd
Adding new actiontype for unfollow replication through ism plugin
aggarwalShivani e46f3b6
Merge branch 'opensearch-project:main' into ccr-libs
aggarwalShivani 72a4fdf
Fix ktlint issues for replication libs
aggarwalShivani 4d65546
Merge branch 'opensearch-project:main' into ccr-libs
aggarwalShivani 8d673ba
Changes for stop-replication action
aggarwalShivani d042fc1
Merge branch 'opensearch-project:main' into ccr-libs
aggarwalShivani 833d9af
Fixed imports for AcknowledgedResponse and org.opensearch.transport c…
aggarwalShivani 0b3c563
Changing ReplicationPluginInterface to static object
aggarwalShivani 3d85d6e
Merge branch 'opensearch-project:main' into ccr-libs
aggarwalShivani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
src/main/kotlin/org/opensearch/commons/replication/ReplicationPluginInterface.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package org.opensearch.commons.replication | ||
|
|
||
| import org.opensearch.action.support.master.AcknowledgedResponse | ||
| import org.opensearch.client.node.NodeClient | ||
| import org.opensearch.commons.replication.action.StopIndexReplicationRequest | ||
| import org.opensearch.commons.replication.action.ReplicationActions.STOP_REPLICATION_ACTION_TYPE | ||
| import org.opensearch.commons.utils.recreateObject | ||
| import org.opensearch.core.action.ActionListener | ||
| import org.opensearch.core.action.ActionResponse | ||
| import org.opensearch.core.common.io.stream.Writeable | ||
|
|
||
|
|
||
| /** | ||
| * All the transport action plugin interfaces for the cross-cluster-replication plugin. | ||
| */ | ||
| object ReplicationPluginInterface { | ||
|
|
||
| /** | ||
| * Stop replication. | ||
| * @param client Node client for making transport action | ||
| * @param request The request object | ||
| * @param listener The listener for getting response | ||
| */ | ||
|
|
||
| fun stopReplication( | ||
| client: NodeClient, | ||
| request: StopIndexReplicationRequest, | ||
| listener: ActionListener<AcknowledgedResponse> | ||
| ) { | ||
| return client.execute( | ||
| STOP_REPLICATION_ACTION_TYPE, | ||
| request, | ||
| wrapActionListener(listener) { response -> | ||
| recreateObject(response) { | ||
| AcknowledgedResponse(it) | ||
| } | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Wrap action listener on concrete response class by a new created one on ActionResponse. | ||
| * This is required because the response may be loaded by different classloader across plugins. | ||
| * The onResponse(ActionResponse) avoids type cast exception and give a chance to recreate | ||
| * the response object. | ||
| */ | ||
| @Suppress("UNCHECKED_CAST") | ||
| private fun <Response : AcknowledgedResponse> wrapActionListener( | ||
| listener: ActionListener<Response>, | ||
| recreate: (Writeable) -> Response | ||
| ): ActionListener<Response> { | ||
| return object : ActionListener<ActionResponse> { | ||
| override fun onResponse(response: ActionResponse) { | ||
| val recreated = response as? Response ?: recreate(response) | ||
| listener.onResponse(recreated) | ||
| } | ||
|
|
||
| override fun onFailure(exception: java.lang.Exception) { | ||
| listener.onFailure(exception) | ||
| } | ||
| } as ActionListener<Response> | ||
| } | ||
| } | ||
27 changes: 27 additions & 0 deletions
27
src/main/kotlin/org/opensearch/commons/replication/action/ReplicationActions.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package org.opensearch.commons.replication.action | ||
|
|
||
| import org.opensearch.action.ActionType | ||
| import org.opensearch.action.support.master.AcknowledgedResponse | ||
|
|
||
| /** | ||
| * All the transport action information for the Replication plugin | ||
| */ | ||
| object ReplicationActions { | ||
|
|
||
| /** | ||
| * Stop replication. Internal only - Inter plugin communication. | ||
| */ | ||
| const val STOP_REPLICATION_NAME = "indices:admin/plugins/replication/index/stop" | ||
| const val STOP_REPLICATION_BASE_ACTION_NAME = "indices:admin/plugins/replication/index/unfollow" | ||
|
|
||
| /** | ||
| * Stop replication transport action type. Internal only - Inter plugin communication. | ||
| */ | ||
| val STOP_REPLICATION_ACTION_TYPE = | ||
bowenlan-amzn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ActionType(STOP_REPLICATION_NAME, ::AcknowledgedResponse) | ||
|
|
||
| } | ||
62 changes: 62 additions & 0 deletions
62
src/main/kotlin/org/opensearch/commons/replication/action/StopIndexReplicationRequest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package org.opensearch.commons.replication.action | ||
|
|
||
| import org.opensearch.action.ActionRequestValidationException | ||
| import org.apache.logging.log4j.LogManager | ||
| import org.opensearch.action.IndicesRequest | ||
| import org.opensearch.action.support.IndicesOptions | ||
| import org.opensearch.action.support.master.AcknowledgedRequest | ||
| import org.opensearch.core.common.io.stream.StreamInput | ||
| import org.opensearch.core.common.io.stream.StreamOutput | ||
| import org.opensearch.core.xcontent.* | ||
| class StopIndexReplicationRequest : AcknowledgedRequest<StopIndexReplicationRequest>, IndicesRequest.Replaceable, ToXContentObject { | ||
| lateinit var indexName: String | ||
| constructor(indexName: String) { | ||
| this.indexName = indexName | ||
| } | ||
|
|
||
| private constructor() { | ||
| } | ||
|
|
||
| constructor(inp: StreamInput): super(inp) { | ||
| indexName = inp.readString() | ||
| } | ||
| companion object { | ||
| private val PARSER = ObjectParser<StopIndexReplicationRequest, Void>("StopReplicationRequestParser") { | ||
| StopIndexReplicationRequest() | ||
| } | ||
|
|
||
| fun fromXContent(parser: XContentParser, followerIndex: String): StopIndexReplicationRequest { | ||
| val stopIndexReplicationRequest = PARSER.parse(parser, null) | ||
| stopIndexReplicationRequest.indexName = followerIndex | ||
| return stopIndexReplicationRequest | ||
| } | ||
| private val log = LogManager.getLogger(StopIndexReplicationRequest::class.java) | ||
| } | ||
|
|
||
| override fun validate(): ActionRequestValidationException? { | ||
| return null | ||
| } | ||
|
|
||
| override fun indices(vararg indices: String?): IndicesRequest { | ||
| return this | ||
| } | ||
| override fun indices(): Array<String> { | ||
| return arrayOf(indexName) | ||
| } | ||
|
|
||
| override fun indicesOptions(): IndicesOptions { | ||
| return IndicesOptions.strictSingleIndexNoExpandForbidClosed() | ||
| } | ||
|
|
||
| override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder { | ||
| builder.startObject() | ||
| builder.field("indexName", indexName) | ||
| builder.endObject() | ||
| return builder | ||
| } | ||
|
|
||
| override fun writeTo(out: StreamOutput) { | ||
| super.writeTo(out) | ||
| out.writeString(indexName) | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
src/test/kotlin/org/opensearch/commons/replication/ReplicationPluginInterfaceTests.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.commons.replication | ||
|
|
||
| import com.nhaarman.mockitokotlin2.whenever | ||
| import org.junit.jupiter.api.Test | ||
| import org.junit.jupiter.api.extension.ExtendWith | ||
| import org.mockito.Answers | ||
| import org.mockito.ArgumentMatchers | ||
| import org.mockito.Mock | ||
| import org.mockito.Mockito | ||
| import org.mockito.junit.jupiter.MockitoExtension | ||
| import org.opensearch.action.ActionType | ||
| import org.opensearch.action.support.master.AcknowledgedResponse | ||
| import org.opensearch.client.node.NodeClient | ||
| import org.opensearch.commons.replication.action.StopIndexReplicationRequest | ||
| import org.opensearch.core.action.ActionListener | ||
|
|
||
| @Suppress("UNCHECKED_CAST") | ||
| @ExtendWith(MockitoExtension::class) | ||
| internal class ReplicationPluginInterfaceTests { | ||
|
|
||
| @Mock(answer = Answers.RETURNS_DEEP_STUBS) | ||
| private lateinit var client: NodeClient | ||
| @Test | ||
| fun stopReplication() { | ||
| val request = Mockito.mock(StopIndexReplicationRequest::class.java) | ||
| val response = AcknowledgedResponse(true) | ||
| val listener: ActionListener<AcknowledgedResponse> = | ||
| Mockito.mock(ActionListener::class.java) as ActionListener<AcknowledgedResponse> | ||
|
|
||
| Mockito.doAnswer { | ||
| (it.getArgument(2) as ActionListener<AcknowledgedResponse>) | ||
| .onResponse(response) | ||
| }.whenever(client).execute(Mockito.any(ActionType::class.java), Mockito.any(), Mockito.any()) | ||
|
|
||
| ReplicationPluginInterface.stopReplication(client, request, listener) | ||
| Mockito.verify(listener, Mockito.times(1)).onResponse(ArgumentMatchers.eq(response)) | ||
bowenlan-amzn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
23 changes: 23 additions & 0 deletions
23
...test/kotlin/org/opensearch/commons/replication/action/StopIndexReplicationRequestTests.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package org.opensearch.commons.replication.action | ||
|
|
||
| import org.junit.jupiter.api.Assertions.assertEquals | ||
| import org.junit.jupiter.api.Assertions.assertNotNull | ||
| import org.junit.jupiter.api.Assertions.assertNull | ||
| import org.junit.jupiter.api.Test | ||
| import org.opensearch.commons.utils.recreateObject | ||
|
|
||
| internal class StopIndexReplicationRequestTests { | ||
| @Test | ||
| fun `Stop Replication request serialize and deserialize transport object should be equal`() { | ||
| val index = "test-idx" | ||
| val request = StopIndexReplicationRequest(index) | ||
| val recreatedRequest = recreateObject(request) { StopIndexReplicationRequest(it) } | ||
| assertNotNull(recreatedRequest) | ||
| assertEquals(request.indexName, recreatedRequest.indexName) | ||
| assertNull(recreatedRequest.validate()) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.