Skip to content

Commit

Permalink
Add unit tests for codec version
Browse files Browse the repository at this point in the history
Signed-off-by: Sooraj Sinha <soosinha@amazon.com>
  • Loading branch information
soosinha committed Aug 14, 2024
1 parent daed1ec commit 09cae0e
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ private static ClusterStateDiffManifest diffManifest(Object[] fields) {
);

private static final ConstructingObjectParser<ClusterMetadataManifest, Void> CURRENT_PARSER = PARSER_V3;
public static final int MANIFEST_CURRENT_CODEC_VERSION = CODEC_V3;

static {
declareParser(PARSER_V0, CODEC_V0);
Expand All @@ -256,7 +257,7 @@ private static ClusterStateDiffManifest diffManifest(Object[] fields) {
declareParser(PARSER_V3, CODEC_V3);
}

public static Integer getCodecForVersion(Version version) {
public static int getCodecForVersion(Version version) {
Integer codecVersion = -1;
Version maxVersion = Version.V_EMPTY;
for (Entry<Version, Integer> entry : CODEC_TO_VERSION_MAPPING.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public class RemoteClusterMetadataManifest extends AbstractRemoteWritableBlobEnt
public static final int SPLITTED_MANIFEST_FILE_LENGTH = 6;

public static final String METADATA_MANIFEST_NAME_FORMAT = "%s";
public static final int MANIFEST_CURRENT_CODEC_VERSION = ClusterMetadataManifest.CODEC_V3;
public static final String COMMITTED = "C";
public static final String PUBLISHED = "P";

Expand Down Expand Up @@ -150,7 +149,7 @@ int getManifestCodecVersion() {

private ChecksumBlobStoreFormat<ClusterMetadataManifest> getClusterMetadataManifestBlobStoreFormat() {
long codecVersion = getManifestCodecVersion();
if (codecVersion == MANIFEST_CURRENT_CODEC_VERSION) {
if (codecVersion == ClusterMetadataManifest.MANIFEST_CURRENT_CODEC_VERSION) {
return CLUSTER_METADATA_MANIFEST_FORMAT;
} else if (codecVersion == ClusterMetadataManifest.CODEC_V2) {
return CLUSTER_METADATA_MANIFEST_FORMAT_V2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@

import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;
import static org.opensearch.gateway.remote.ClusterMetadataManifest.MANIFEST_CURRENT_CODEC_VERSION;
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;
Expand Down Expand Up @@ -945,7 +946,7 @@ public void testHandlePrePublishAndCommitWhenRemoteStateEnabled() throws IOExcep
.previousClusterUUID(randomAlphaOfLength(10))
.clusterUUIDCommitted(true)
.build();
Mockito.when(remoteClusterStateService.writeFullMetadata(clusterState, previousClusterUUID))
Mockito.when(remoteClusterStateService.writeFullMetadata(clusterState, previousClusterUUID, MANIFEST_CURRENT_CODEC_VERSION))
.thenReturn(new RemoteClusterStateManifestInfo(manifest, "path/to/manifest"));

final PersistedStateRegistry persistedStateRegistry = persistedStateRegistry();
Expand Down Expand Up @@ -976,7 +977,7 @@ public void testHandlePrePublishAndCommitWhenRemoteStateEnabled() throws IOExcep

final CoordinationState coordinationState = createCoordinationState(persistedStateRegistry, node1, settings);
coordinationState.handlePrePublish(clusterState);
Mockito.verify(remoteClusterStateService, Mockito.times(1)).writeFullMetadata(clusterState, previousClusterUUID);
Mockito.verify(remoteClusterStateService, Mockito.times(1)).writeFullMetadata(clusterState, previousClusterUUID, MANIFEST_CURRENT_CODEC_VERSION);
assertThat(persistedStateRegistry.getPersistedState(PersistedStateType.REMOTE).getLastAcceptedState(), equalTo(clusterState));

Mockito.when(remoteClusterStateService.markLastStateAsCommitted(any(), any()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;

import static org.mockito.ArgumentMatchers.eq;
import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_INDEX_UUID;
import static org.opensearch.gateway.remote.ClusterMetadataManifest.MANIFEST_CURRENT_CODEC_VERSION;
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;
Expand Down Expand Up @@ -725,7 +727,7 @@ public void testRemotePersistedState() throws IOException {
final RemoteClusterStateService remoteClusterStateService = Mockito.mock(RemoteClusterStateService.class);
final ClusterMetadataManifest manifest = ClusterMetadataManifest.builder().clusterTerm(1L).stateVersion(5L).build();
final String previousClusterUUID = "prev-cluster-uuid";
Mockito.when(remoteClusterStateService.writeFullMetadata(Mockito.any(), Mockito.any()))
Mockito.when(remoteClusterStateService.writeFullMetadata(Mockito.any(), Mockito.any(), eq(MANIFEST_CURRENT_CODEC_VERSION)))
.thenReturn(new RemoteClusterStateManifestInfo(manifest, "path/to/manifest"));

Mockito.when(remoteClusterStateService.writeIncrementalMetadata(Mockito.any(), Mockito.any(), Mockito.any()))
Expand All @@ -742,7 +744,7 @@ public void testRemotePersistedState() throws IOException {
);

remotePersistedState.setLastAcceptedState(clusterState);
Mockito.verify(remoteClusterStateService).writeFullMetadata(clusterState, previousClusterUUID);
Mockito.verify(remoteClusterStateService).writeFullMetadata(clusterState, previousClusterUUID, MANIFEST_CURRENT_CODEC_VERSION);

assertThat(remotePersistedState.getLastAcceptedState(), equalTo(clusterState));
assertThat(remotePersistedState.getCurrentTerm(), equalTo(clusterTerm));
Expand All @@ -753,7 +755,7 @@ public void testRemotePersistedState() throws IOException {
);

remotePersistedState.setLastAcceptedState(secondClusterState);
Mockito.verify(remoteClusterStateService, times(1)).writeFullMetadata(secondClusterState, previousClusterUUID);
Mockito.verify(remoteClusterStateService, times(1)).writeFullMetadata(secondClusterState, previousClusterUUID, MANIFEST_CURRENT_CODEC_VERSION);

assertThat(remotePersistedState.getLastAcceptedState(), equalTo(secondClusterState));
assertThat(remotePersistedState.getCurrentTerm(), equalTo(clusterTerm));
Expand Down Expand Up @@ -786,7 +788,7 @@ public void testRemotePersistedStateNotCommitted() throws IOException {
.build();
Mockito.when(remoteClusterStateService.getLatestClusterMetadataManifest(Mockito.any(), Mockito.any()))
.thenReturn(Optional.of(manifest));
Mockito.when(remoteClusterStateService.writeFullMetadata(Mockito.any(), Mockito.any()))
Mockito.when(remoteClusterStateService.writeFullMetadata(Mockito.any(), Mockito.any(), eq(MANIFEST_CURRENT_CODEC_VERSION)))
.thenReturn(new RemoteClusterStateManifestInfo(manifest, "path/to/manifest"));

Mockito.when(remoteClusterStateService.writeIncrementalMetadata(Mockito.any(), Mockito.any(), Mockito.any()))
Expand All @@ -811,14 +813,14 @@ public void testRemotePersistedStateNotCommitted() throws IOException {
remotePersistedState.setLastAcceptedState(clusterState);
ArgumentCaptor<String> previousClusterUUIDCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<ClusterState> clusterStateCaptor = ArgumentCaptor.forClass(ClusterState.class);
Mockito.verify(remoteClusterStateService).writeFullMetadata(clusterStateCaptor.capture(), previousClusterUUIDCaptor.capture());
Mockito.verify(remoteClusterStateService).writeFullMetadata(clusterStateCaptor.capture(), previousClusterUUIDCaptor.capture(), eq(MANIFEST_CURRENT_CODEC_VERSION));
assertEquals(previousClusterUUID, previousClusterUUIDCaptor.getValue());
}

public void testRemotePersistedStateExceptionOnFullStateUpload() throws IOException {
final RemoteClusterStateService remoteClusterStateService = Mockito.mock(RemoteClusterStateService.class);
final String previousClusterUUID = "prev-cluster-uuid";
Mockito.doThrow(IOException.class).when(remoteClusterStateService).writeFullMetadata(Mockito.any(), Mockito.any());
Mockito.doThrow(IOException.class).when(remoteClusterStateService).writeFullMetadata(Mockito.any(), Mockito.any(), eq(MANIFEST_CURRENT_CODEC_VERSION));

CoordinationState.PersistedState remotePersistedState = new RemotePersistedState(remoteClusterStateService, previousClusterUUID);

Expand All @@ -835,7 +837,7 @@ public void testRemotePersistedStateFailureStats() throws IOException {
RemotePersistenceStats remoteStateStats = new RemotePersistenceStats();
final RemoteClusterStateService remoteClusterStateService = Mockito.mock(RemoteClusterStateService.class);
final String previousClusterUUID = "prev-cluster-uuid";
Mockito.doThrow(IOException.class).when(remoteClusterStateService).writeFullMetadata(Mockito.any(), Mockito.any());
Mockito.doThrow(IOException.class).when(remoteClusterStateService).writeFullMetadata(Mockito.any(), Mockito.any(), eq(MANIFEST_CURRENT_CODEC_VERSION));
when(remoteClusterStateService.getStats()).thenReturn(remoteStateStats);
doCallRealMethod().when(remoteClusterStateService).writeMetadataFailed();
CoordinationState.PersistedState remotePersistedState = new RemotePersistedState(remoteClusterStateService, previousClusterUUID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@

import static org.opensearch.gateway.remote.ClusterMetadataManifest.CODEC_V0;
import static org.opensearch.gateway.remote.ClusterMetadataManifest.CODEC_V1;
import static org.opensearch.gateway.remote.ClusterMetadataManifest.CODEC_V2;
import static org.opensearch.gateway.remote.ClusterMetadataManifest.CODEC_V3;
import static org.opensearch.gateway.remote.RemoteClusterStateAttributesManager.CLUSTER_BLOCKS;
import static org.opensearch.gateway.remote.RemoteClusterStateAttributesManager.DISCOVERY_NODES;
import static org.opensearch.gateway.remote.model.RemoteCoordinationMetadata.COORDINATION_METADATA;
Expand Down Expand Up @@ -115,7 +117,7 @@ public void testClusterMetadataManifestXContent() throws IOException {
.opensearchVersion(Version.CURRENT)
.nodeId("test-node-id")
.committed(false)
.codecVersion(ClusterMetadataManifest.CODEC_V2)
.codecVersion(CODEC_V2)
.indices(Collections.singletonList(uploadedIndexMetadata))
.previousClusterUUID("prev-cluster-uuid")
.clusterUUIDCommitted(true)
Expand Down Expand Up @@ -162,7 +164,7 @@ public void testClusterMetadataManifestSerializationEqualsHashCode() {
.opensearchVersion(Version.CURRENT)
.nodeId("B10RX1f5RJenMQvYccCgSQ")
.committed(true)
.codecVersion(ClusterMetadataManifest.CODEC_V3)
.codecVersion(CODEC_V3)
.indices(randomUploadedIndexMetadataList())
.previousClusterUUID("yfObdx8KSMKKrXf8UyHhM")
.clusterUUIDCommitted(true)
Expand Down Expand Up @@ -495,7 +497,7 @@ public void testClusterMetadataManifestXContentV2() throws IOException {
.opensearchVersion(Version.CURRENT)
.nodeId("test-node-id")
.committed(false)
.codecVersion(ClusterMetadataManifest.CODEC_V2)
.codecVersion(CODEC_V2)
.indices(Collections.singletonList(uploadedIndexMetadata))
.previousClusterUUID("prev-cluster-uuid")
.clusterUUIDCommitted(true)
Expand Down Expand Up @@ -561,7 +563,7 @@ public void testClusterMetadataManifestXContentV3() throws IOException {
.opensearchVersion(Version.CURRENT)
.nodeId("test-node-id")
.committed(false)
.codecVersion(ClusterMetadataManifest.CODEC_V3)
.codecVersion(CODEC_V3)
.indices(Collections.singletonList(uploadedIndexMetadata))
.previousClusterUUID("prev-cluster-uuid")
.clusterUUIDCommitted(true)
Expand Down Expand Up @@ -630,7 +632,7 @@ public void testClusterMetadataManifestXContentV2WithoutEphemeral() throws IOExc
.opensearchVersion(Version.CURRENT)
.nodeId("test-node-id")
.committed(false)
.codecVersion(ClusterMetadataManifest.CODEC_V2)
.codecVersion(CODEC_V2)
.indices(Collections.singletonList(uploadedIndexMetadata))
.previousClusterUUID("prev-cluster-uuid")
.clusterUUIDCommitted(true)
Expand Down Expand Up @@ -712,6 +714,17 @@ public void testUploadedIndexMetadataWithoutComponentPrefix() throws IOException
}
}

public void testGetCodecForVersion() {
assertEquals(-1, ClusterMetadataManifest.getCodecForVersion(Version.fromString("1.3.0")));
assertEquals(-1, ClusterMetadataManifest.getCodecForVersion(Version.V_2_1_0));
assertEquals(CODEC_V0, ClusterMetadataManifest.getCodecForVersion(Version.V_2_10_0));
assertEquals(CODEC_V1, ClusterMetadataManifest.getCodecForVersion(Version.V_2_12_0));
assertEquals(CODEC_V1, ClusterMetadataManifest.getCodecForVersion(Version.V_2_13_0));
assertEquals(CODEC_V2, ClusterMetadataManifest.getCodecForVersion(Version.V_2_15_0));
assertEquals(CODEC_V3, ClusterMetadataManifest.getCodecForVersion(Version.V_2_16_0));
assertEquals(CODEC_V3, ClusterMetadataManifest.getCodecForVersion(Version.V_2_17_0));
}

private UploadedIndexMetadata randomlyChangingUploadedIndexMetadata(UploadedIndexMetadata uploadedIndexMetadata) {
switch (randomInt(2)) {
case 0:
Expand Down
Loading

0 comments on commit 09cae0e

Please sign in to comment.