-
Notifications
You must be signed in to change notification settings - Fork 61
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
Adding UTs for IndexReplicationTask #109
Merged
Merged
Changes from all commits
Commits
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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
204 changes: 204 additions & 0 deletions
204
src/test/kotlin/org/opensearch/replication/task/index/IndexReplicationTaskTests.kt
This file contains 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,204 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package org.opensearch.replication.task.index | ||
|
||
import org.opensearch.replication.ReplicationPlugin | ||
import org.opensearch.replication.ReplicationSettings | ||
import org.opensearch.replication.action.index.block.UpdateIndexBlockAction | ||
import org.opensearch.replication.metadata.ReplicationMetadataManager | ||
import org.opensearch.replication.metadata.store.ReplicationContext | ||
import org.opensearch.replication.metadata.store.ReplicationMetadata | ||
import org.opensearch.replication.repository.REMOTE_REPOSITORY_PREFIX | ||
import org.opensearch.replication.task.shard.ShardReplicationExecutor | ||
import org.opensearch.replication.task.shard.ShardReplicationParams | ||
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope | ||
import com.nhaarman.mockitokotlin2.any | ||
import com.nhaarman.mockitokotlin2.doAnswer | ||
import com.nhaarman.mockitokotlin2.doReturn | ||
import com.nhaarman.mockitokotlin2.spy | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.runBlocking | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.opensearch.Version | ||
import org.opensearch.action.ActionListener | ||
import org.opensearch.action.ActionRequest | ||
import org.opensearch.action.ActionResponse | ||
import org.opensearch.action.ActionType | ||
import org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotAction | ||
import org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse | ||
import org.opensearch.action.admin.indices.recovery.RecoveryAction | ||
import org.opensearch.action.admin.indices.recovery.RecoveryResponse | ||
import org.opensearch.action.admin.indices.settings.get.GetSettingsAction | ||
import org.opensearch.action.admin.indices.settings.get.GetSettingsResponse | ||
import org.opensearch.action.admin.indices.settings.put.UpdateSettingsAction | ||
import org.opensearch.action.support.master.AcknowledgedResponse | ||
import org.opensearch.cluster.ClusterState | ||
import org.opensearch.cluster.ClusterStateObserver | ||
import org.opensearch.cluster.RestoreInProgress | ||
import org.opensearch.cluster.metadata.IndexMetadata | ||
import org.opensearch.cluster.metadata.Metadata | ||
import org.opensearch.cluster.routing.RoutingTable | ||
import org.opensearch.common.settings.Settings | ||
import org.opensearch.common.settings.SettingsModule | ||
import org.opensearch.common.unit.TimeValue | ||
import org.opensearch.index.Index | ||
import org.opensearch.index.shard.ShardId | ||
import org.opensearch.persistent.PersistentTaskParams | ||
import org.opensearch.persistent.PersistentTasksCustomMetadata | ||
import org.opensearch.persistent.PersistentTasksService | ||
import org.opensearch.snapshots.Snapshot | ||
import org.opensearch.snapshots.SnapshotId | ||
import org.opensearch.tasks.TaskId.EMPTY_TASK_ID | ||
import org.opensearch.tasks.TaskManager | ||
import org.opensearch.test.ClusterServiceUtils | ||
import org.opensearch.test.ClusterServiceUtils.setState | ||
import org.opensearch.test.OpenSearchTestCase | ||
import org.opensearch.test.OpenSearchTestCase.assertBusy | ||
import org.opensearch.threadpool.TestThreadPool | ||
import org.junit.Test | ||
import org.mockito.Mockito | ||
import org.opensearch.common.xcontent.NamedXContentRegistry | ||
import org.opensearch.replication.metadata.ReplicationOverallState | ||
import org.opensearch.replication.metadata.store.ReplicationMetadataStore | ||
import org.opensearch.replication.metadata.store.ReplicationMetadataStore.Companion.REPLICATION_CONFIG_SYSTEM_INDEX | ||
import org.opensearch.replication.metadata.store.ReplicationStoreMetadataType | ||
import java.util.* | ||
import java.util.concurrent.TimeUnit | ||
|
||
@ThreadLeakScope(ThreadLeakScope.Scope.NONE) | ||
class IndexReplicationTaskTests : OpenSearchTestCase() { | ||
|
||
companion object { | ||
var currentTaskState :IndexReplicationState = InitialState | ||
var stateChanges :Int = 0 | ||
var restoreNotNull = false | ||
|
||
var followerIndex = "follower-index" | ||
var connectionName = "leader-cluster" | ||
var remoteCluster = "remote-cluster" | ||
} | ||
|
||
var threadPool = TestThreadPool("ReplicationPluginTest") | ||
var clusterService = ClusterServiceUtils.createClusterService(threadPool) | ||
|
||
@Test | ||
fun testExecute() = runBlocking { | ||
val replicationTask: IndexReplicationTask = spy(createIndexReplicationTask()) | ||
var taskManager = Mockito.mock(TaskManager::class.java) | ||
replicationTask.setPersistent(taskManager) | ||
var rc = ReplicationContext(followerIndex) | ||
var rm = ReplicationMetadata(connectionName, ReplicationStoreMetadataType.INDEX.name, ReplicationOverallState.RUNNING.name, "reason", rc, rc, Settings.EMPTY) | ||
replicationTask.setReplicationMetadata(rm) | ||
|
||
//Update ClusterState to say restore started | ||
val state: ClusterState = clusterService.state() | ||
|
||
var newClusterState: ClusterState | ||
|
||
// Updating cluster state | ||
var builder: ClusterState.Builder | ||
val indices: MutableList<String> = ArrayList() | ||
indices.add(followerIndex) | ||
val snapshot = Snapshot("$REMOTE_REPOSITORY_PREFIX$connectionName", SnapshotId("randomAlphaOfLength", "randomAlphaOfLength")) | ||
val restoreEntry = RestoreInProgress.Entry("restoreUUID", snapshot, RestoreInProgress.State.INIT, Collections.unmodifiableList(ArrayList<String>(indices)), | ||
null) | ||
|
||
// Update metadata store index as well | ||
var metaBuilder = Metadata.builder() | ||
metaBuilder.put(IndexMetadata.builder(REPLICATION_CONFIG_SYSTEM_INDEX).settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(0)) | ||
var metadata = metaBuilder.build() | ||
var routingTableBuilder = RoutingTable.builder() | ||
routingTableBuilder.addAsNew(metadata.index(REPLICATION_CONFIG_SYSTEM_INDEX)) | ||
var routingTable = routingTableBuilder.build() | ||
|
||
builder = ClusterState.builder(state).routingTable(routingTable) | ||
builder.putCustom(RestoreInProgress.TYPE, RestoreInProgress.Builder( | ||
state.custom(RestoreInProgress.TYPE, RestoreInProgress.EMPTY)).add(restoreEntry).build()) | ||
|
||
newClusterState = builder.build() | ||
setState(clusterService, newClusterState) | ||
|
||
val job = this.launch{ | ||
replicationTask.execute(this, InitialState) | ||
} | ||
|
||
// Delay to let task execute | ||
delay(1000) | ||
Comment on lines
+135
to
+136
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit - |
||
|
||
// Assert we move to RESTORING .. This is blocking and won't let the test run | ||
assertBusy({ | ||
assertThat(currentTaskState == RestoreState).isTrue() | ||
}, 1, TimeUnit.SECONDS) | ||
|
||
|
||
//Complete the Restore | ||
metaBuilder = Metadata.builder() | ||
metaBuilder.put(IndexMetadata.builder(followerIndex).settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(0)) | ||
metadata = metaBuilder.build() | ||
routingTableBuilder = RoutingTable.builder() | ||
routingTableBuilder.addAsNew(metadata.index(followerIndex)) | ||
routingTable = routingTableBuilder.build() | ||
|
||
builder = ClusterState.builder(state).routingTable(routingTable) | ||
builder.putCustom(RestoreInProgress.TYPE, RestoreInProgress.Builder( | ||
state.custom(RestoreInProgress.TYPE, RestoreInProgress.EMPTY)).build()) | ||
|
||
newClusterState = builder.build() | ||
setState(clusterService, newClusterState) | ||
|
||
delay(1000) | ||
|
||
assertBusy { | ||
assertThat(currentTaskState == MonitoringState).isTrue() | ||
} | ||
|
||
job.cancel() | ||
|
||
} | ||
|
||
private fun createIndexReplicationTask() : IndexReplicationTask { | ||
var threadPool = TestThreadPool("IndexReplicationTask") | ||
//Hack Alert : Though it is meant to force rejection , this is to make overallTaskScope not null | ||
threadPool.startForcingRejections() | ||
clusterService = ClusterServiceUtils.createClusterService(threadPool) | ||
val settingsModule = Mockito.mock(SettingsModule::class.java) | ||
val spyClient = Mockito.spy<NoOpClient>(NoOpClient("testName")) | ||
|
||
val replicationMetadataManager = ReplicationMetadataManager(clusterService, spyClient, | ||
ReplicationMetadataStore(spyClient, clusterService, NamedXContentRegistry.EMPTY)) | ||
var persist = PersistentTasksService(clusterService, threadPool, spyClient) | ||
val state: ClusterState = clusterService.state() | ||
val tasks = PersistentTasksCustomMetadata.builder() | ||
var sId = ShardId(Index(followerIndex, "_na_"), 0) | ||
tasks.addTask<PersistentTaskParams>( "replication:0", ShardReplicationExecutor.TASK_NAME, ShardReplicationParams("remoteCluster", sId, sId), | ||
PersistentTasksCustomMetadata.Assignment("other_node_", "test assignment on other node")) | ||
|
||
val metadata = Metadata.builder(state.metadata()) | ||
metadata.putCustom(PersistentTasksCustomMetadata.TYPE, tasks.build()) | ||
val newClusterState: ClusterState = ClusterState.builder(state).metadata(metadata).build() | ||
|
||
setState(clusterService, newClusterState) | ||
|
||
doAnswer{ invocation -> spyClient }.`when`(spyClient).getRemoteClusterClient(any()) | ||
assert(spyClient.getRemoteClusterClient(remoteCluster) == spyClient) | ||
|
||
val replicationSettings = Mockito.mock(ReplicationSettings::class.java) | ||
replicationSettings.metadataSyncInterval = TimeValue(100, TimeUnit.MILLISECONDS) | ||
val cso = ClusterStateObserver(clusterService, logger, threadPool.threadContext) | ||
val indexReplicationTask = IndexReplicationTask(1, "type", "action", "description" , EMPTY_TASK_ID, | ||
ReplicationPlugin.REPLICATION_EXECUTOR_NAME_FOLLOWER, clusterService , threadPool, spyClient, IndexReplicationParams(connectionName, Index(followerIndex, "0"), followerIndex), | ||
persist, replicationMetadataManager, replicationSettings, settingsModule,cso) | ||
|
||
return indexReplicationTask | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: We can probably move the logic to create
ReplicationMetadata
,ClusterState
, etc objects to a helper utility