Skip to content

Commit

Permalink
Fix commented UTs
Browse files Browse the repository at this point in the history
  • Loading branch information
Shailendra Singh committed Aug 6, 2024
1 parent a15e675 commit 7622041
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.opensearch.cluster.ClusterName;
import org.opensearch.cluster.ClusterState;
import org.opensearch.cluster.Diff;
import org.opensearch.cluster.DiffableUtils;
import org.opensearch.cluster.coordination.CoordinationMetadata;
import org.opensearch.cluster.metadata.IndexMetadata;
import org.opensearch.cluster.metadata.Metadata;
Expand Down Expand Up @@ -650,8 +649,6 @@ public void testGetAsyncIndexRoutingDiffWriteAction() throws Exception {
ClusterState previousState = generateClusterStateWithOneIndex(indexName, 5, 1, false).build();
ClusterState currentState = generateClusterStateWithOneIndex(indexName, 5, 2, true).build();

//RoutingTableIncrementalDiff diff = new RoutingTableIncrementalDiff(previousState.getRoutingTable(), currentState.getRoutingTable());

Iterable<String> remotePath = new BlobPath().add("base-path")
.add(
Base64.getUrlEncoder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*//*
*/


package org.opensearch.gateway.remote.routingtable;

import org.opensearch.Version;
import org.opensearch.cluster.ClusterState;
import org.opensearch.cluster.Diff;
import org.opensearch.cluster.metadata.IndexMetadata;
import org.opensearch.cluster.metadata.Metadata;
import org.opensearch.cluster.routing.IndexRoutingTable;
import org.opensearch.cluster.routing.RoutingTable;
import org.opensearch.cluster.routing.RoutingTableIncrementalDiff;
import org.opensearch.common.blobstore.BlobPath;
Expand All @@ -36,14 +33,12 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.opensearch.gateway.remote.RemoteClusterStateServiceTests.generateClusterStateWithOneIndex;
import static org.opensearch.gateway.remote.routingtable.RemoteRoutingTableDiff.ROUTING_TABLE_DIFF_FILE;
import static org.opensearch.gateway.remote.routingtable.RemoteRoutingTableDiff.ROUTING_TABLE_DIFF_METADATA_PREFIX;
import static org.opensearch.gateway.remote.routingtable.RemoteRoutingTableDiff.ROUTING_TABLE_DIFF_PATH_TOKEN;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.hamcrest.Matchers.nullValue;
Expand Down Expand Up @@ -88,22 +83,11 @@ public void tearDown() throws Exception {
}

public void testClusterUUID() {
Map<String, Diff<IndexRoutingTable>> diffs = new HashMap<>();
String indexName = randomAlphaOfLength(randomIntBetween(1, 50));
int numberOfShards = randomIntBetween(1, 10);
int numberOfReplicas = randomIntBetween(1, 10);
IndexMetadata indexMetadata = IndexMetadata.builder(indexName)
.settings(settings(Version.CURRENT))
.numberOfShards(numberOfShards)
.numberOfReplicas(numberOfReplicas)
.build();
IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex()).initializeAsNew(indexMetadata).build();
diffs.put(indexName, indexRoutingTable.diff(indexRoutingTable));
ClusterState previousState = generateClusterStateWithOneIndex(indexName, 5, 1, false).build();
ClusterState currentState = generateClusterStateWithOneIndex(indexName, 5, 2, true).build();

RoutingTableIncrementalDiff routingTableIncrementalDiff = new RoutingTableIncrementalDiff(diffs);
RoutingTableIncrementalDiff routingTableIncrementalDiff = new RoutingTableIncrementalDiff(previousState.getRoutingTable(), currentState.getRoutingTable());

RemoteRoutingTableDiff remoteDiffForUpload = new RemoteRoutingTableDiff(
routingTableIncrementalDiff,
Expand All @@ -119,21 +103,11 @@ public void testClusterUUID() {
}

public void testFullBlobName() {
Map<String, Diff<IndexRoutingTable>> diffs = new HashMap<>();
String indexName = randomAlphaOfLength(randomIntBetween(1, 50));
int numberOfShards = randomIntBetween(1, 10);
int numberOfReplicas = randomIntBetween(1, 10);
IndexMetadata indexMetadata = IndexMetadata.builder(indexName)
.settings(settings(Version.CURRENT))
.numberOfShards(numberOfShards)
.numberOfReplicas(numberOfReplicas)
.build();
ClusterState previousState = generateClusterStateWithOneIndex(indexName, 5, 1, false).build();
ClusterState currentState = generateClusterStateWithOneIndex(indexName, 5, 2, true).build();

IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex()).initializeAsNew(indexMetadata).build();
diffs.put(indexName, indexRoutingTable.diff(indexRoutingTable));
RoutingTableIncrementalDiff routingTableIncrementalDiff = new RoutingTableIncrementalDiff(diffs);
RoutingTableIncrementalDiff routingTableIncrementalDiff = new RoutingTableIncrementalDiff(previousState.getRoutingTable(), currentState.getRoutingTable());

RemoteRoutingTableDiff remoteDiffForUpload = new RemoteRoutingTableDiff(
routingTableIncrementalDiff,
Expand All @@ -149,21 +123,11 @@ public void testFullBlobName() {
}

public void testBlobFileName() {
Map<String, Diff<IndexRoutingTable>> diffs = new HashMap<>();
String indexName = randomAlphaOfLength(randomIntBetween(1, 50));
int numberOfShards = randomIntBetween(1, 10);
int numberOfReplicas = randomIntBetween(1, 10);
IndexMetadata indexMetadata = IndexMetadata.builder(indexName)
.settings(settings(Version.CURRENT))
.numberOfShards(numberOfShards)
.numberOfReplicas(numberOfReplicas)
.build();
IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex()).initializeAsNew(indexMetadata).build();
ClusterState previousState = generateClusterStateWithOneIndex(indexName, 5, 1, false).build();
ClusterState currentState = generateClusterStateWithOneIndex(indexName, 5, 2, true).build();

diffs.put(indexName, indexRoutingTable.diff(indexRoutingTable));
RoutingTableIncrementalDiff routingTableIncrementalDiff = new RoutingTableIncrementalDiff(diffs);
RoutingTableIncrementalDiff routingTableIncrementalDiff = new RoutingTableIncrementalDiff(previousState.getRoutingTable(), currentState.getRoutingTable());

RemoteRoutingTableDiff remoteDiffForUpload = new RemoteRoutingTableDiff(
routingTableIncrementalDiff,
Expand All @@ -179,21 +143,11 @@ public void testBlobFileName() {
}

public void testBlobPathParameters() {
Map<String, Diff<IndexRoutingTable>> diffs = new HashMap<>();
String indexName = randomAlphaOfLength(randomIntBetween(1, 50));
int numberOfShards = randomIntBetween(1, 10);
int numberOfReplicas = randomIntBetween(1, 10);
ClusterState previousState = generateClusterStateWithOneIndex(indexName, 5, 1, false).build();
ClusterState currentState = generateClusterStateWithOneIndex(indexName, 5, 2, true).build();

IndexMetadata indexMetadata = IndexMetadata.builder(indexName)
.settings(settings(Version.CURRENT))
.numberOfShards(numberOfShards)
.numberOfReplicas(numberOfReplicas)
.build();
IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex()).initializeAsNew(indexMetadata).build();
diffs.put(indexName, indexRoutingTable.diff(indexRoutingTable));
RoutingTableIncrementalDiff routingTableIncrementalDiff = new RoutingTableIncrementalDiff(diffs);
RoutingTableIncrementalDiff routingTableIncrementalDiff = new RoutingTableIncrementalDiff(previousState.getRoutingTable(), currentState.getRoutingTable());

RemoteRoutingTableDiff remoteDiffForUpload = new RemoteRoutingTableDiff(
routingTableIncrementalDiff,
Expand All @@ -211,21 +165,11 @@ public void testBlobPathParameters() {
}

public void testGenerateBlobFileName() {
Map<String, Diff<IndexRoutingTable>> diffs = new HashMap<>();
String indexName = randomAlphaOfLength(randomIntBetween(1, 50));
int numberOfShards = randomIntBetween(1, 10);
int numberOfReplicas = randomIntBetween(1, 10);
IndexMetadata indexMetadata = IndexMetadata.builder(indexName)
.settings(settings(Version.CURRENT))
.numberOfShards(numberOfShards)
.numberOfReplicas(numberOfReplicas)
.build();
ClusterState previousState = generateClusterStateWithOneIndex(indexName, 5, 1, false).build();
ClusterState currentState = generateClusterStateWithOneIndex(indexName, 5, 2, true).build();

IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex()).initializeAsNew(indexMetadata).build();
diffs.put(indexName, indexRoutingTable.diff(indexRoutingTable));
RoutingTableIncrementalDiff routingTableIncrementalDiff = new RoutingTableIncrementalDiff(diffs);
RoutingTableIncrementalDiff routingTableIncrementalDiff = new RoutingTableIncrementalDiff(previousState.getRoutingTable(), currentState.getRoutingTable());

RemoteRoutingTableDiff remoteDiffForUpload = new RemoteRoutingTableDiff(
routingTableIncrementalDiff,
Expand All @@ -244,21 +188,11 @@ public void testGenerateBlobFileName() {
}

public void testGetUploadedMetadata() throws IOException {
Map<String, Diff<IndexRoutingTable>> diffs = new HashMap<>();
String indexName = randomAlphaOfLength(randomIntBetween(1, 50));
int numberOfShards = randomIntBetween(1, 10);
int numberOfReplicas = randomIntBetween(1, 10);
IndexMetadata indexMetadata = IndexMetadata.builder(indexName)
.settings(settings(Version.CURRENT))
.numberOfShards(numberOfShards)
.numberOfReplicas(numberOfReplicas)
.build();
IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex()).initializeAsNew(indexMetadata).build();
ClusterState previousState = generateClusterStateWithOneIndex(indexName, 5, 1, false).build();
ClusterState currentState = generateClusterStateWithOneIndex(indexName, 5, 2, true).build();

diffs.put(indexName, indexRoutingTable.diff(indexRoutingTable));
RoutingTableIncrementalDiff routingTableIncrementalDiff = new RoutingTableIncrementalDiff(diffs);
RoutingTableIncrementalDiff routingTableIncrementalDiff = new RoutingTableIncrementalDiff(previousState.getRoutingTable(), currentState.getRoutingTable());

RemoteRoutingTableDiff remoteDiffForUpload = new RemoteRoutingTableDiff(
routingTableIncrementalDiff,
Expand All @@ -278,42 +212,29 @@ public void testStreamOperations() throws IOException {
int numberOfShards = randomIntBetween(1, 10);
int numberOfReplicas = randomIntBetween(1, 10);

Metadata metadata = Metadata.builder()
.put(
IndexMetadata.builder(indexName)
.settings(settings(Version.CURRENT))
.numberOfShards(numberOfShards)
.numberOfReplicas(numberOfReplicas)
)
.build();
RoutingTable initialRoutingTable = RoutingTable.builder().addAsNew(metadata.index(indexName)).build();
Map<String, Diff<IndexRoutingTable>> diffs = new HashMap<>();
initialRoutingTable.getIndicesRouting().values().forEach(indexRoutingTable -> {
diffs.put(indexName, indexRoutingTable.diff(indexRoutingTable));
RoutingTableIncrementalDiff routingTableIncrementalDiff = new RoutingTableIncrementalDiff(diffs);
RemoteRoutingTableDiff remoteDiffForUpload = new RemoteRoutingTableDiff(
routingTableIncrementalDiff,
clusterUUID,
compressor,
STATE_TERM,
STATE_VERSION
);
assertThrows(AssertionError.class, remoteDiffForUpload::getUploadedMetadata);
try (InputStream inputStream = remoteDiffForUpload.serialize()) {
remoteDiffForUpload.setFullBlobName(BlobPath.cleanPath());
assertThat(inputStream.available(), greaterThan(0));
routingTableIncrementalDiff = remoteDiffForUpload.deserialize(inputStream);
assertEquals(remoteDiffForUpload.getDiffs().size(), routingTableIncrementalDiff.getDiffs().size());
} catch (IOException e) {
throw new RuntimeException(e);
}
});
ClusterState previousState = generateClusterStateWithOneIndex(indexName, numberOfShards, numberOfReplicas, false).build();
ClusterState currentState = generateClusterStateWithOneIndex(indexName, numberOfShards, numberOfReplicas + 1, true).build();

RoutingTableIncrementalDiff routingTableIncrementalDiff = new RoutingTableIncrementalDiff(previousState.getRoutingTable(), currentState.getRoutingTable());

RemoteRoutingTableDiff remoteDiffForUpload = new RemoteRoutingTableDiff(
routingTableIncrementalDiff,
clusterUUID,
compressor,
STATE_TERM,
STATE_VERSION
);

// Serialize the remote diff
InputStream inputStream = remoteDiffForUpload.serialize();

// Create a new instance for deserialization
RemoteRoutingTableDiff remoteDiffForDownload = new RemoteRoutingTableDiff(TEST_BLOB_NAME, clusterUUID, compressor);

// Deserialize the remote diff
Diff<RoutingTable> deserializedDiff = remoteDiffForDownload.deserialize(inputStream);

// Assert that the indices routing table created from routingTableIncrementalDiff and deserializedDiff is equal
assertEquals(routingTableIncrementalDiff.apply(previousState.getRoutingTable()).getIndicesRouting(), deserializedDiff.apply(previousState.getRoutingTable()).getIndicesRouting());
}
}
*/

0 comments on commit 7622041

Please sign in to comment.