Skip to content

Commit

Permalink
add UT for index and cluster block recovery
Browse files Browse the repository at this point in the history
Signed-off-by: bansvaru <[email protected]>
  • Loading branch information
linuxpi committed Oct 17, 2023
1 parent bb3ffe3 commit f803053
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.Version;
import org.opensearch.action.admin.cluster.remotestore.restore.RestoreRemoteStoreRequest;
import org.opensearch.cluster.ClusterState;
import org.opensearch.cluster.ClusterStateUpdateTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,8 @@ public void testGatewayForRemoteStateForInitialBootstrap() throws IOException {
remoteClusterStateService,
remoteStoreRestoreService,
persistedStateRegistry,
ClusterState.EMPTY_STATE
ClusterState.EMPTY_STATE,
false
);
final CoordinationState.PersistedState lucenePersistedState = gateway.getPersistedState();
PersistedState remotePersistedState = persistedStateRegistry.getPersistedState(PersistedStateType.REMOTE);
Expand Down Expand Up @@ -886,7 +887,8 @@ public void testGatewayForRemoteStateForNodeReplacement() throws IOException {
remoteClusterStateService,
remoteStoreRestoreService,
persistedStateRegistry,
ClusterState.EMPTY_STATE
ClusterState.EMPTY_STATE,
false
);
final CoordinationState.PersistedState lucenePersistedState = gateway.getPersistedState();
PersistedState remotePersistedState = persistedStateRegistry.getPersistedState(PersistedStateType.REMOTE);
Expand Down Expand Up @@ -918,7 +920,13 @@ public void testGatewayForRemoteStateForNodeReboot() throws IOException {
.clusterUUID(randomAlphaOfLength(10))
.build()
);
gateway = newGatewayForRemoteState(remoteClusterStateService, remoteStoreRestoreService, persistedStateRegistry, clusterState);
gateway = newGatewayForRemoteState(
remoteClusterStateService,
remoteStoreRestoreService,
persistedStateRegistry,
clusterState,
false
);
final CoordinationState.PersistedState lucenePersistedState = gateway.getPersistedState();
PersistedState remotePersistedState = persistedStateRegistry.getPersistedState(PersistedStateType.REMOTE);
verifyNoInteractions(remoteClusterStateService);
Expand All @@ -933,13 +941,75 @@ public void testGatewayForRemoteStateForNodeReboot() throws IOException {
}
}

public void testGatewayForRemoteStateForInitialBootstrapBlocksApplied() throws IOException {
MockGatewayMetaState gateway = null;
try {
final RemoteClusterStateService remoteClusterStateService = mock(RemoteClusterStateService.class);
when(remoteClusterStateService.getLastKnownUUIDFromRemote(clusterName.value())).thenReturn("test-cluster-uuid");

final IndexMetadata indexMetadata = IndexMetadata.builder("test-index1")
.settings(
settings(Version.CURRENT).put(SETTING_INDEX_UUID, randomAlphaOfLength(10))
.put(IndexMetadata.INDEX_READ_ONLY_SETTING.getKey(), true)
)
.numberOfShards(5)
.numberOfReplicas(1)
.build();

final ClusterState clusterState = createClusterState(
randomNonNegativeLong(),
Metadata.builder()
.coordinationMetadata(CoordinationMetadata.builder().term(randomLong()).build())
.put(indexMetadata, false)
.clusterUUID(ClusterState.UNKNOWN_UUID)
.persistentSettings(Settings.builder().put(Metadata.SETTING_READ_ONLY_SETTING.getKey(), true).build())
.build()
);

final RemoteStoreRestoreService remoteStoreRestoreService = mock(RemoteStoreRestoreService.class);
when(remoteStoreRestoreService.restore(any(), any(), anyBoolean(), any())).thenReturn(
RemoteRestoreResult.build("test-cluster-uuid", null, clusterState)
);
final PersistedStateRegistry persistedStateRegistry = persistedStateRegistry();
gateway = newGatewayForRemoteState(
remoteClusterStateService,
remoteStoreRestoreService,
persistedStateRegistry,
clusterState,
true
);
PersistedState remotePersistedState = persistedStateRegistry.getPersistedState(PersistedStateType.REMOTE);
PersistedState lucenePersistedState = persistedStateRegistry.getPersistedState(PersistedStateType.LOCAL);
verify(remoteClusterStateService).getLastKnownUUIDFromRemote(clusterName.value()); // change this
verify(remoteStoreRestoreService).restore(any(ClusterState.class), any(String.class), anyBoolean(), any(String[].class));
assertThat(remotePersistedState.getLastAcceptedState(), nullValue());
assertThat(
Metadata.isGlobalStateEquals(lucenePersistedState.getLastAcceptedState().metadata(), clusterState.metadata()),
equalTo(true)
);
assertThat(
lucenePersistedState.getLastAcceptedState().blocks().hasGlobalBlock(Metadata.CLUSTER_READ_ONLY_BLOCK),
equalTo(true)
);
assertThat(
IndexMetadata.INDEX_READ_ONLY_SETTING.get(
lucenePersistedState.getLastAcceptedState().metadata().index("test-index1").getSettings()
),
equalTo(true)
);
} finally {
IOUtils.close(gateway);
}
}

private MockGatewayMetaState newGatewayForRemoteState(
RemoteClusterStateService remoteClusterStateService,
RemoteStoreRestoreService remoteStoreRestoreService,
PersistedStateRegistry persistedStateRegistry,
ClusterState currentState
ClusterState currentState,
boolean prepareFullState
) throws IOException {
MockGatewayMetaState gateway = new MockGatewayMetaState(localNode, bigArrays);
MockGatewayMetaState gateway = new MockGatewayMetaState(localNode, bigArrays, prepareFullState);
String randomRepoName = "randomRepoName";
String stateRepoTypeAttributeKey = String.format(
Locale.getDefault(),
Expand All @@ -963,6 +1033,7 @@ private MockGatewayMetaState newGatewayForRemoteState(
when(clusterService.getClusterSettings()).thenReturn(
new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)
);
when(transportService.getLocalNode()).thenReturn(mock(DiscoveryNode.class));
final PersistedClusterStateService persistedClusterStateService = new PersistedClusterStateService(
nodeEnvironment,
xContentRegistry(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public class MockGatewayMetaState extends GatewayMetaState {
private final BigArrays bigArrays;
private final RemoteClusterStateService remoteClusterStateService;
private final RemoteStoreRestoreService remoteStoreRestoreService;
private boolean prepareFullState = false;

public MockGatewayMetaState(DiscoveryNode localNode, BigArrays bigArrays, boolean prepareFullState) {
this(localNode, bigArrays);
this.prepareFullState = prepareFullState;
}

public MockGatewayMetaState(DiscoveryNode localNode, BigArrays bigArrays) {
this.localNode = localNode;
Expand Down Expand Up @@ -99,8 +105,12 @@ Metadata upgradeMetadataForNode(

@Override
ClusterState prepareInitialClusterState(TransportService transportService, ClusterService clusterService, ClusterState clusterState) {
// Just set localNode here, not to mess with ClusterService and IndicesService mocking
return ClusterStateUpdaters.setLocalNode(clusterState, localNode);
if (prepareFullState) {
return super.prepareInitialClusterState(transportService, clusterService, clusterState);
} else {
// Just set localNode here, not to mess with ClusterService and IndicesService mocking
return ClusterStateUpdaters.setLocalNode(clusterState, localNode);
}
}

@Override
Expand All @@ -113,6 +123,16 @@ public void start(
NodeEnvironment nodeEnvironment,
NamedXContentRegistry xContentRegistry,
PersistedStateRegistry persistedStateRegistry
) {
start(settings, nodeEnvironment, xContentRegistry, persistedStateRegistry, false);
}

public void start(
Settings settings,
NodeEnvironment nodeEnvironment,
NamedXContentRegistry xContentRegistry,
PersistedStateRegistry persistedStateRegistry,
boolean prepareFullState
) {
final TransportService transportService = mock(TransportService.class);
when(transportService.getThreadPool()).thenReturn(mock(ThreadPool.class));
Expand All @@ -126,6 +146,7 @@ public void start(
} catch (IOException e) {
throw new AssertionError(e);
}
this.prepareFullState = prepareFullState;
start(
settings,
transportService,
Expand Down

0 comments on commit f803053

Please sign in to comment.