Skip to content

Commit 432cc70

Browse files
author
sukriti sinha
committed
Added Unit test
1 parent 77e4f53 commit 432cc70

File tree

6 files changed

+623
-0
lines changed

6 files changed

+623
-0
lines changed

server/src/main/java/org/opensearch/action/admin/cluster/remotestore/metadata/TransportRemoteStoreMetadataAction.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.opensearch.action.support.ActionFilters;
1414
import org.opensearch.action.support.TransportAction;
1515
import org.opensearch.cluster.ClusterState;
16+
import org.opensearch.cluster.block.ClusterBlockLevel;
1617
import org.opensearch.cluster.metadata.IndexMetadata;
1718
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
1819
import org.opensearch.cluster.service.ClusterService;
@@ -87,6 +88,7 @@ public TransportRemoteStoreMetadataAction(
8788
protected void doExecute(Task task, RemoteStoreMetadataRequest request, ActionListener<RemoteStoreMetadataResponse> listener) {
8889
try {
8990
ClusterState state = clusterService.state();
91+
clusterService.state().blocks().globalBlockedRaiseException(ClusterBlockLevel.METADATA_READ);
9092
String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(state, request);
9193
if (concreteIndices.length == 0) {
9294
listener.onResponse(new RemoteStoreMetadataResponse(new RemoteStoreMetadata[0], 0, 0, 0, Collections.emptyList()));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.action.admin.cluster.remotestore.metadata;
10+
11+
import org.opensearch.common.io.stream.BytesStreamOutput;
12+
import org.opensearch.core.common.io.stream.StreamInput;
13+
import org.opensearch.test.OpenSearchTestCase;
14+
15+
import static org.hamcrest.Matchers.equalTo;
16+
17+
public class RemoteStoreMetadataRequestTests extends OpenSearchTestCase {
18+
19+
public void testAddIndexName() throws Exception {
20+
RemoteStoreMetadataRequest request = new RemoteStoreMetadataRequest();
21+
request.indices("test-index");
22+
RemoteStoreMetadataRequest deserializedRequest = roundTripRequest(request);
23+
assertRequestsEqual(request, deserializedRequest);
24+
}
25+
26+
public void testAddShardId() throws Exception {
27+
RemoteStoreMetadataRequest request = new RemoteStoreMetadataRequest();
28+
request.indices("test-index");
29+
request.shards("0");
30+
RemoteStoreMetadataRequest deserializedRequest = roundTripRequest(request);
31+
assertRequestsEqual(request, deserializedRequest);
32+
}
33+
34+
public void testAddMultipleShards() throws Exception {
35+
RemoteStoreMetadataRequest request = new RemoteStoreMetadataRequest();
36+
request.indices("test-index");
37+
request.shards("0", "1", "2");
38+
RemoteStoreMetadataRequest deserializedRequest = roundTripRequest(request);
39+
assertRequestsEqual(request, deserializedRequest);
40+
}
41+
42+
private static RemoteStoreMetadataRequest roundTripRequest(RemoteStoreMetadataRequest request) throws Exception {
43+
try (BytesStreamOutput out = new BytesStreamOutput()) {
44+
request.writeTo(out);
45+
try (StreamInput in = out.bytes().streamInput()) {
46+
return new RemoteStoreMetadataRequest(in);
47+
}
48+
}
49+
}
50+
51+
private static void assertRequestsEqual(RemoteStoreMetadataRequest request1, RemoteStoreMetadataRequest request2) {
52+
assertThat(request1.indices(), equalTo(request2.indices()));
53+
assertThat(request1.shards(), equalTo(request2.shards()));
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.action.admin.cluster.remotestore.metadata;
10+
11+
import org.opensearch.common.xcontent.XContentFactory;
12+
import org.opensearch.common.xcontent.XContentHelper;
13+
import org.opensearch.core.action.support.DefaultShardOperationFailedException;
14+
import org.opensearch.core.common.bytes.BytesReference;
15+
import org.opensearch.core.index.shard.ShardId;
16+
import org.opensearch.core.xcontent.XContentBuilder;
17+
import org.opensearch.test.OpenSearchTestCase;
18+
import org.opensearch.threadpool.TestThreadPool;
19+
import org.opensearch.threadpool.ThreadPool;
20+
21+
import java.util.ArrayList;
22+
import java.util.Map;
23+
24+
import static org.opensearch.core.xcontent.ToXContent.EMPTY_PARAMS;
25+
import static org.opensearch.action.admin.cluster.remotestore.metadata.RemoteStoreMetadataTestHelper.*;
26+
27+
public class RemoteStoreMetadataResponseTests extends OpenSearchTestCase {
28+
private ThreadPool threadPool;
29+
private ShardId shardId;
30+
31+
@Override
32+
public void setUp() throws Exception {
33+
super.setUp();
34+
threadPool = new TestThreadPool("remote_store_metadata_test");
35+
shardId = new ShardId("index", "uuid", 0);
36+
}
37+
38+
@Override
39+
public void tearDown() throws Exception {
40+
super.tearDown();
41+
threadPool.shutdownNow();
42+
}
43+
44+
public void testSerializationForSingleShard() throws Exception {
45+
RemoteStoreMetadata metadata = createTestMetadata(
46+
createTestSegmentMetadata(),
47+
createTestTranslogMetadata(),
48+
"test-index",
49+
0
50+
);
51+
52+
RemoteStoreMetadataResponse response = new RemoteStoreMetadataResponse(
53+
new RemoteStoreMetadata[]{metadata},
54+
1,
55+
1,
56+
0,
57+
new ArrayList<DefaultShardOperationFailedException>()
58+
);
59+
60+
XContentBuilder builder = XContentFactory.jsonBuilder();
61+
response.toXContent(builder, EMPTY_PARAMS);
62+
Map<String, Object> jsonResponseObject = XContentHelper.convertToMap(
63+
BytesReference.bytes(builder),
64+
false,
65+
builder.contentType()
66+
).v2();
67+
68+
validateResponseContent(jsonResponseObject, "test-index", 1);
69+
}
70+
71+
public void testSerializationForMultipleShards() throws Exception {
72+
RemoteStoreMetadata metadata1 = createTestMetadata(
73+
createTestSegmentMetadata(),
74+
createTestTranslogMetadata(),
75+
"test-index",
76+
0
77+
);
78+
RemoteStoreMetadata metadata2 = createTestMetadata(
79+
createTestSegmentMetadata(),
80+
createTestTranslogMetadata(),
81+
"test-index",
82+
1
83+
);
84+
85+
RemoteStoreMetadataResponse response = new RemoteStoreMetadataResponse(
86+
new RemoteStoreMetadata[]{metadata1, metadata2},
87+
2,
88+
2,
89+
0,
90+
new ArrayList<DefaultShardOperationFailedException>()
91+
);
92+
93+
XContentBuilder builder = XContentFactory.jsonBuilder();
94+
response.toXContent(builder, EMPTY_PARAMS);
95+
Map<String, Object> jsonResponseObject = XContentHelper.convertToMap(
96+
BytesReference.bytes(builder),
97+
false,
98+
builder.contentType()
99+
).v2();
100+
101+
validateResponseContent(jsonResponseObject, "test-index", 2);
102+
}
103+
104+
public void testSerializationForMultipleIndices() throws Exception {
105+
RemoteStoreMetadata metadata1 = createTestMetadata(
106+
createTestSegmentMetadata(),
107+
createTestTranslogMetadata(),
108+
"index1",
109+
0
110+
);
111+
RemoteStoreMetadata metadata2 = createTestMetadata(
112+
createTestSegmentMetadata(),
113+
createTestTranslogMetadata(),
114+
"index2",
115+
0
116+
);
117+
118+
RemoteStoreMetadataResponse response = new RemoteStoreMetadataResponse(
119+
new RemoteStoreMetadata[]{metadata1, metadata2},
120+
2,
121+
2,
122+
0,
123+
new ArrayList<DefaultShardOperationFailedException>()
124+
);
125+
126+
XContentBuilder builder = XContentFactory.jsonBuilder();
127+
response.toXContent(builder, EMPTY_PARAMS);
128+
Map<String, Object> jsonResponseObject = XContentHelper.convertToMap(
129+
BytesReference.bytes(builder),
130+
false,
131+
builder.contentType()
132+
).v2();
133+
134+
Map<String, Object> indicesObject = (Map<String, Object>) jsonResponseObject.get("indices");
135+
assertTrue(indicesObject.containsKey("index1"));
136+
assertTrue(indicesObject.containsKey("index2"));
137+
}
138+
139+
private void validateResponseContent(Map<String, Object> responseMap, String indexName, int expectedShards) {
140+
Map<String, Object> metadataShardsObject = (Map<String, Object>) responseMap.get("_shards");
141+
assertEquals(expectedShards, metadataShardsObject.get("total"));
142+
assertEquals(expectedShards, metadataShardsObject.get("successful"));
143+
assertEquals(0, metadataShardsObject.get("failed"));
144+
145+
Map<String, Object> indicesObject = (Map<String, Object>) responseMap.get("indices");
146+
assertTrue(indicesObject.containsKey(indexName));
147+
148+
Map<String, Object> indexData = (Map<String, Object>) indicesObject.get(indexName);
149+
assertNotNull(indexData.get("shards"));
150+
}
151+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.action.admin.cluster.remotestore.metadata;
10+
11+
import java.util.HashMap;
12+
import java.util.Map;
13+
14+
/**
15+
* Helper class for unit testing RemoteStoreMetadata and RemoteStoreMetadataResponse.
16+
*/
17+
public class RemoteStoreMetadataTestHelper {
18+
19+
public static RemoteStoreMetadata createTestMetadata(
20+
Map<String, Object> segmentMetadata,
21+
Map<String, Object> translogMetadata,
22+
String indexName,
23+
int shardId
24+
) {
25+
return new RemoteStoreMetadata(segmentMetadata, translogMetadata, indexName, shardId);
26+
}
27+
28+
public static Map<String, Object> createTestSegmentMetadata() {
29+
Map<String, Object> uploadedSegment = new HashMap<>();
30+
uploadedSegment.put("original_name", "segment_1");
31+
uploadedSegment.put("checksum", "abc123");
32+
uploadedSegment.put("length", 1024L);
33+
34+
Map<String, Object> uploadedSegments = new HashMap<>();
35+
uploadedSegments.put("seg_1", uploadedSegment);
36+
37+
Map<String, Object> replicationCheckpoint = new HashMap<>();
38+
replicationCheckpoint.put("shard_id", "index[0]");
39+
replicationCheckpoint.put("primary_term", 1L);
40+
replicationCheckpoint.put("generation", 1L);
41+
replicationCheckpoint.put("version", 1L);
42+
replicationCheckpoint.put("length", 12345L);
43+
replicationCheckpoint.put("codec", "Lucene80");
44+
replicationCheckpoint.put("created_timestamp", System.currentTimeMillis());
45+
46+
Map<String, Object> metadata = new HashMap<>();
47+
metadata.put("uploaded_segments", uploadedSegments);
48+
metadata.put("replication_checkpoint", replicationCheckpoint);
49+
metadata.put("generation", 1L);
50+
metadata.put("primary_term", 1L);
51+
52+
return Map.of("metadata__segment1", metadata);
53+
}
54+
55+
public static Map<String, Object> createTestTranslogMetadata() {
56+
Map<String, String> genToTermMap = new HashMap<>();
57+
genToTermMap.put("1", "1");
58+
59+
Map<String, Object> content = new HashMap<>();
60+
content.put("primary_term", 1L);
61+
content.put("generation", 1L);
62+
content.put("min_translog_generation", 1L);
63+
content.put("generation_to_term_mapping", genToTermMap);
64+
65+
Map<String, Object> metadata = new HashMap<>();
66+
metadata.put("primary_term", 1L);
67+
metadata.put("generation", 1L);
68+
metadata.put("timestamp", System.currentTimeMillis());
69+
metadata.put("node_id_hash", "test-node");
70+
metadata.put("min_translog_gen", 1L);
71+
metadata.put("min_primary_term", "1");
72+
metadata.put("version", "1");
73+
metadata.put("content", content);
74+
75+
return Map.of("metadata__translog1", metadata);
76+
}
77+
}

0 commit comments

Comments
 (0)