Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: Shivansh Arora <[email protected]>
  • Loading branch information
shiv0408 committed Aug 9, 2024
1 parent 9b8880e commit 64c1773
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@
import org.opensearch.gateway.remote.model.RemoteClusterStateManifestInfo;
import org.opensearch.repositories.fs.FsRepository;
import org.opensearch.test.EqualsHashCodeTestUtils;
import org.opensearch.test.FeatureFlagSetter;
import org.opensearch.test.OpenSearchTestCase;
import org.junit.Before;

import java.io.IOException;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
Expand All @@ -66,9 +68,12 @@

import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;
import static org.opensearch.common.util.FeatureFlags.REMOTE_PUBLICATION_EXPERIMENTAL;
import static org.opensearch.common.util.FeatureFlags.initializeFeatureFlags;
import static org.opensearch.node.remotestore.RemoteStoreNodeAttribute.REMOTE_STORE_CLUSTER_STATE_REPOSITORY_NAME_ATTRIBUTE_KEY;
import static org.opensearch.node.remotestore.RemoteStoreNodeAttribute.REMOTE_STORE_REPOSITORY_SETTINGS_ATTRIBUTE_KEY_PREFIX;
import static org.opensearch.node.remotestore.RemoteStoreNodeAttribute.REMOTE_STORE_REPOSITORY_TYPE_ATTRIBUTE_KEY_FORMAT;
import static org.opensearch.node.remotestore.RemoteStoreNodeAttribute.REMOTE_STORE_ROUTING_TABLE_REPOSITORY_NAME_ATTRIBUTE_KEY;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
Expand All @@ -84,6 +89,7 @@ public class CoordinationStateTests extends OpenSearchTestCase {
private DiscoveryNode node1;
private DiscoveryNode node2;
private DiscoveryNode node3;
private DiscoveryNode nodeWithPub;

private ClusterState initialStateNode1;
private ClusterState initialStateNode2;
Expand All @@ -100,6 +106,15 @@ public void setupNodes() {
node1 = createNode("node1");
node2 = createNode("node2");
node3 = createNode("node3");
nodeWithPub = createNode(
"nodeWithPub",
Map.of(
REMOTE_STORE_CLUSTER_STATE_REPOSITORY_NAME_ATTRIBUTE_KEY,
"",
REMOTE_STORE_ROUTING_TABLE_REPOSITORY_NAME_ATTRIBUTE_KEY,
""
)
);

initialStateNode1 = clusterState(0L, 0L, node1, VotingConfiguration.EMPTY_CONFIG, VotingConfiguration.EMPTY_CONFIG, 42L);
initialStateNode2 = clusterState(0L, 0L, node2, VotingConfiguration.EMPTY_CONFIG, VotingConfiguration.EMPTY_CONFIG, 42L);
Expand All @@ -122,6 +137,10 @@ public void setupNodes() {
}

public static DiscoveryNode createNode(String id) {
return createNode(id, Collections.emptyMap());
}

public static DiscoveryNode createNode(String id, Map<String, String> attributes) {
final TransportAddress address = buildNewFakeTransportAddress();
return new DiscoveryNode(
"",
Expand All @@ -130,7 +149,7 @@ public static DiscoveryNode createNode(String id) {
address.address().getHostString(),
address.getAddress(),
address,
Collections.emptyMap(),
attributes,
DiscoveryNodeRole.BUILT_IN_ROLES,
Version.CURRENT
);
Expand Down Expand Up @@ -982,14 +1001,28 @@ public void testHandlePrePublishAndCommitWhenRemoteStateEnabled() throws IOExcep
);
}

public void testHandlePublishRequestOnFollowerWhenRemoteStateEnabled() {
public void testHandlePublishRequestOnFollowerWhenRemotePublicationEnabled() {
final RemoteClusterStateService remoteClusterStateService = Mockito.mock(RemoteClusterStateService.class);
DiscoveryNode nodeWithPub = createNode(
"nodeWithPub",
Map.of(
REMOTE_STORE_CLUSTER_STATE_REPOSITORY_NAME_ATTRIBUTE_KEY,
"",
REMOTE_STORE_ROUTING_TABLE_REPOSITORY_NAME_ATTRIBUTE_KEY,
""
)
);
// cluster manager is node1 and node2 is a follower node
VotingConfiguration initialConfig = VotingConfiguration.of(node1);
ClusterState state1 = clusterState(
0L,
0L,
DiscoveryNodes.builder().add(node1).add(node2).clusterManagerNodeId(node1.getId()).localNodeId(node2.getId()).build(),
DiscoveryNodes.builder()
.add(node1)
.add(nodeWithPub)
.clusterManagerNodeId(node1.getId())
.localNodeId(nodeWithPub.getId())
.build(),
initialConfig,
initialConfig,
42L
Expand All @@ -1001,10 +1034,14 @@ public void testHandlePublishRequestOnFollowerWhenRemoteStateEnabled() {
new RemotePersistedState(remoteClusterStateService, state1.metadata().clusterUUID())
);

final CoordinationState coordinationState = createCoordinationState(persistedStateRegistry, node2, remoteStateSettings());
final CoordinationState coordinationState = createCoordinationState(
persistedStateRegistry,
nodeWithPub,
remotePublicationSettings()
);
coordinationState.setInitialState(state1);
long newTerm = randomLongBetween(1, 10);
StartJoinRequest startJoinRequest = new StartJoinRequest(node2, newTerm);
StartJoinRequest startJoinRequest = new StartJoinRequest(nodeWithPub, newTerm);

coordinationState.handleStartJoin(startJoinRequest);

Expand Down Expand Up @@ -1047,11 +1084,16 @@ public void testHandlePublishRequestOnFollowerWhenRemoteStateEnabled() {

public void testHandleCommitOnFollowerNodeWhenRemoteStateEnabled() {
RemoteClusterStateService remoteClusterStateService = mock(RemoteClusterStateService.class);
VotingConfiguration initialConfig = VotingConfiguration.of(node1, node2);
VotingConfiguration initialConfig = VotingConfiguration.of(node1, nodeWithPub);
ClusterState state1 = clusterState(
0L,
0L,
DiscoveryNodes.builder().add(node1).add(node2).clusterManagerNodeId(node1.getId()).localNodeId(node2.getId()).build(),
DiscoveryNodes.builder()
.add(node1)
.add(nodeWithPub)
.clusterManagerNodeId(node1.getId())
.localNodeId(nodeWithPub.getId())
.build(),
initialConfig,
initialConfig,
42L
Expand All @@ -1063,21 +1105,30 @@ public void testHandleCommitOnFollowerNodeWhenRemoteStateEnabled() {
new RemotePersistedState(remoteClusterStateService, state1.metadata().clusterUUID())
);

final CoordinationState coordinationState = createCoordinationState(persistedStateRegistry, node2, remoteStateSettings());
final CoordinationState coordinationState = createCoordinationState(
persistedStateRegistry,
nodeWithPub,
remotePublicationSettings()
);
coordinationState.setInitialState(state1);
long newTerm = randomLongBetween(1, 10);
StartJoinRequest startJoinRequest = new StartJoinRequest(node2, newTerm);
StartJoinRequest startJoinRequest = new StartJoinRequest(nodeWithPub, newTerm);

Join v1 = cs1.handleStartJoin(startJoinRequest);
Join v2 = coordinationState.handleStartJoin(startJoinRequest);
assertTrue(coordinationState.handleJoin(v1));
assertTrue(coordinationState.handleJoin(v2));
assertTrue(coordinationState.electionWon());
VotingConfiguration newConfig = VotingConfiguration.of(node1, node2, node3);
VotingConfiguration newConfig = VotingConfiguration.of(node1, nodeWithPub, node3);
ClusterState state2 = clusterState(
startJoinRequest.getTerm(),
2L,
DiscoveryNodes.builder().add(node1).add(node2).clusterManagerNodeId(node1.getId()).localNodeId(node2.getId()).build(),
DiscoveryNodes.builder()
.add(node1)
.add(nodeWithPub)
.clusterManagerNodeId(node1.getId())
.localNodeId(nodeWithPub.getId())
.build(),
initialConfig,
newConfig,
7L
Expand Down Expand Up @@ -1232,4 +1283,11 @@ private static Settings remoteStateSettings() {
.build();
return settings;
}

private static Settings remotePublicationSettings() {
FeatureFlagSetter.set(REMOTE_PUBLICATION_EXPERIMENTAL);
Settings remoteStateSettings = Settings.builder().put(remoteStateSettings()).put(REMOTE_PUBLICATION_EXPERIMENTAL, true).build();
initializeFeatureFlags(remoteStateSettings);
return remoteStateSettings;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -793,17 +793,25 @@ public void testRemotePersistedState() throws IOException {

public void testRemotePersistentState_FollowerNode() throws IOException {
final RemoteClusterStateService remoteClusterStateService = Mockito.mock(RemoteClusterStateService.class);
final ClusterMetadataManifest manifest = ClusterMetadataManifest.builder().clusterTerm(1L).stateVersion(5L).build();
final ClusterMetadataManifest manifest = ClusterMetadataManifest.builder()
.clusterTerm(1L)
.stateVersion(5L)
.committed(false)
.build();
final String previousClusterUUID = "prev-cluster-uuid";
RemotePersistedState remotePersistedState = new RemotePersistedState(remoteClusterStateService, previousClusterUUID);

assertNull(remotePersistedState.getLastAcceptedState());
assertNull(remotePersistedState.getLastAcceptedManifest());
assertEquals(0, remotePersistedState.getCurrentTerm());

final long clusterTerm = randomNonNegativeLong();
final ClusterState clusterState = createClusterState(
randomNonNegativeLong(),
Metadata.builder().coordinationMetadata(CoordinationMetadata.builder().term(clusterTerm).build()).build(),
Metadata.builder()
.coordinationMetadata(CoordinationMetadata.builder().term(clusterTerm).build())
.clusterUUIDCommitted(true)
.build(),
false
);

Expand All @@ -817,7 +825,10 @@ public void testRemotePersistentState_FollowerNode() throws IOException {

final ClusterState secondClusterState = createClusterState(
randomNonNegativeLong(),
Metadata.builder().coordinationMetadata(CoordinationMetadata.builder().term(clusterTerm).build()).build(),
Metadata.builder()
.coordinationMetadata(CoordinationMetadata.builder().term(clusterTerm).build())
.clusterUUIDCommitted(false)
.build(),
false
);

Expand All @@ -826,14 +837,15 @@ public void testRemotePersistentState_FollowerNode() throws IOException {

assertEquals(secondClusterState, remotePersistedState.getLastAcceptedState());
assertEquals(clusterTerm, remotePersistedState.getCurrentTerm());
assertFalse(remotePersistedState.getLastAcceptedManifest().isCommitted());

remotePersistedState.markLastAcceptedStateAsCommitted();
Mockito.verify(remoteClusterStateService, never()).markLastStateAsCommitted(Mockito.any(), Mockito.any());

assertEquals(secondClusterState, remotePersistedState.getLastAcceptedState());
assertEquals(clusterTerm, remotePersistedState.getCurrentTerm());
assertFalse(remotePersistedState.getLastAcceptedState().metadata().clusterUUIDCommitted());
assertFalse(remotePersistedState.getLastAcceptedManifest().isCommitted());
assertTrue(remotePersistedState.getLastAcceptedManifest().isCommitted());

final ClusterState thirdClusterState = ClusterState.builder(secondClusterState)
.metadata(Metadata.builder(secondClusterState.getMetadata()).clusterUUID(randomAlphaOfLength(10)).build())
Expand Down

0 comments on commit 64c1773

Please sign in to comment.