Skip to content

Commit

Permalink
Add UnitTests
Browse files Browse the repository at this point in the history
Signed-off-by: Shivansh Arora <[email protected]>
  • Loading branch information
shiv0408 committed Feb 20, 2024
1 parent f6a2431 commit f3e853b
Show file tree
Hide file tree
Showing 4 changed files with 319 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1784,7 +1784,9 @@ public static void toXContent(Metadata metadata, XContentBuilder builder, ToXCon
builder.endObject();
}

builder.startObject("templates");
metadata.templatesMetadata().toXContent(builder, params);
builder.endObject();

if (context == XContentContext.API) {
builder.startObject("indices");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,12 @@ public ClusterMetadataManifest writeIncrementalMetadata(
previousClusterState.metadata(),
clusterState.metadata()
) == false;
final Map<String, UploadedMetadataAttribute> previousStateCustomMap = new HashMap<>(previousManifest.getCustomMetadataMap());
final Map<String, Metadata.Custom> customsToUpload = getUpdatedCustoms(clusterState, previousClusterState);
final Map<String, UploadedMetadataAttribute> allUploadedCustomMap = new HashMap<>(previousManifest.getCustomMetadataMap());
for (final String custom : clusterState.metadata().customs().keySet()) {
previousStateCustomMap.remove(custom);
}

// Write Index Metadata
final Map<String, Long> previousStateIndexMetadataVersionByName = new HashMap<>();
Expand Down Expand Up @@ -396,7 +401,8 @@ public ClusterMetadataManifest writeIncrementalMetadata(
uploadedMetadataResults.uploadedIndexMetadata.forEach(
uploadedIndexMetadata -> allUploadedIndexMetadata.put(uploadedIndexMetadata.getIndexName(), uploadedIndexMetadata)
);

allUploadedCustomMap.putAll(uploadedMetadataResults.uploadedCustomMetadataMap);
previousStateCustomMap.keySet().forEach(allUploadedCustomMap::remove);
for (String removedIndexName : previousStateIndexMetadataVersionByName.keySet()) {
allUploadedIndexMetadata.remove(removedIndexName);
}
Expand All @@ -414,7 +420,7 @@ public ClusterMetadataManifest writeIncrementalMetadata(
? uploadedMetadataResults.uploadedTemplatesMetadata
: previousManifest.getTemplatesMetadata(),
firstUpload || !customsToUpload.isEmpty()
? uploadedMetadataResults.uploadedCustomMetadataMap
? allUploadedCustomMap
: previousManifest.getCustomMetadataMap(),
false
);
Expand Down Expand Up @@ -1649,7 +1655,7 @@ public RemotePersistenceStats getStats() {
return remoteStateStats;
}

private class UploadedMetadataResults {
private static class UploadedMetadataResults {
List<UploadedIndexMetadata> uploadedIndexMetadata;
Map<String, UploadedMetadataAttribute> uploadedCustomMetadataMap;
UploadedMetadataAttribute uploadedCoordinationMetadata;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.gateway.remote.ClusterMetadataManifest.UploadedIndexMetadata;
import org.opensearch.gateway.remote.ClusterMetadataManifest.UploadedMetadataAttribute;
import org.opensearch.test.EqualsHashCodeTestUtils;
import org.opensearch.test.OpenSearchTestCase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

public class ClusterMetadataManifestTests extends OpenSearchTestCase {
Expand Down Expand Up @@ -53,7 +55,7 @@ public void testClusterMetadataManifestXContentV0() throws IOException {
}
}

public void testClusterMetadataManifestXContent() throws IOException {
public void testClusterMetadataManifestXContentV1() throws IOException {
UploadedIndexMetadata uploadedIndexMetadata = new UploadedIndexMetadata("test-index", "test-uuid", "/test/upload/path");
ClusterMetadataManifest originalManifest = new ClusterMetadataManifest(
1L,
Expand All @@ -74,6 +76,37 @@ public void testClusterMetadataManifestXContent() throws IOException {
originalManifest.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();

try (XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder))) {
final ClusterMetadataManifest fromXContentManifest = ClusterMetadataManifest.fromXContentV1(parser);
assertEquals(originalManifest, fromXContentManifest);
}
}

public void testClusterMetadataManifestXContent() throws IOException {
UploadedIndexMetadata uploadedIndexMetadata = new UploadedIndexMetadata("test-index", "test-uuid", "/test/upload/path");
ClusterMetadataManifest originalManifest = new ClusterMetadataManifest(
1L,
1L,
"test-cluster-uuid",
"test-state-uuid",
Version.CURRENT,
"test-node-id",
false,
ClusterMetadataManifest.CODEC_V2,
null,
Collections.singletonList(uploadedIndexMetadata),
"prev-cluster-uuid",
true,
new UploadedMetadataAttribute(RemoteClusterStateService.COORDINATION_METADATA, "coordination-file"),
new UploadedMetadataAttribute(RemoteClusterStateService.SETTING_METADATA, "setting-file"),
new UploadedMetadataAttribute(RemoteClusterStateService.TEMPLATES_METADATA, "templates-file"),
new HashMap<>()
);
final XContentBuilder builder = JsonXContent.contentBuilder();
builder.startObject();
originalManifest.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();

try (XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder))) {
final ClusterMetadataManifest fromXContentManifest = ClusterMetadataManifest.fromXContent(parser);
assertEquals(originalManifest, fromXContentManifest);
Expand All @@ -89,11 +122,15 @@ public void testClusterMetadataManifestSerializationEqualsHashCode() {
Version.CURRENT,
"B10RX1f5RJenMQvYccCgSQ",
true,
1,
"test-global-metadata-file",
2,
null,
randomUploadedIndexMetadataList(),
"yfObdx8KSMKKrXf8UyHhM",
true
true,
new UploadedMetadataAttribute(RemoteClusterStateService.COORDINATION_METADATA, "coordination-file"),
new UploadedMetadataAttribute(RemoteClusterStateService.SETTING_METADATA, "setting-file"),
new UploadedMetadataAttribute(RemoteClusterStateService.TEMPLATES_METADATA, "templates-file"),
new HashMap<>()
);
{ // Mutate Cluster Term
EqualsHashCodeTestUtils.checkEqualsAndHashCode(
Expand Down
Loading

0 comments on commit f3e853b

Please sign in to comment.