Skip to content

Commit 1975071

Browse files
committed
Add UTs
Signed-off-by: Ashish Singh <[email protected]>
1 parent e21d937 commit 1975071

File tree

6 files changed

+241
-114
lines changed

6 files changed

+241
-114
lines changed

Diff for: server/src/main/java/org/opensearch/index/remote/RemoteStorePathStrategy.java

+4
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ BlobPath hashPath() {
171171
return BlobPath.cleanPath().add(shardId).add(indexUUID());
172172
}
173173

174+
public String shardId() {
175+
return shardId;
176+
}
177+
174178
/**
175179
* Returns a new builder for {@link SnapshotShardPathInput}.
176180
*/

Diff for: server/src/main/java/org/opensearch/repositories/blobstore/BlobStoreRepository.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -2076,17 +2076,24 @@ private Set<String> writeNewIndexShardPaths(
20762076
private String writeIndexShardPaths(IndexId indexId, SnapshotId snapshotId, int shardCount) {
20772077
try {
20782078
List<String> paths = getShardPaths(indexId, shardCount);
2079-
String pathType = String.valueOf(indexId.getShardPathType());
2080-
String pathHashAlgorithm = String.valueOf(FNV_1A_COMPOSITE_1.getCode());
2079+
int pathType = indexId.getShardPathType();
2080+
int pathHashAlgorithm = FNV_1A_COMPOSITE_1.getCode();
20812081
String blobName = String.join(
20822082
SnapshotShardPaths.DELIMITER,
20832083
indexId.getId(),
20842084
indexId.getName(),
20852085
String.valueOf(shardCount),
2086-
pathType,
2087-
pathHashAlgorithm
2086+
String.valueOf(pathType),
2087+
String.valueOf(pathHashAlgorithm)
2088+
);
2089+
SnapshotShardPaths shardPaths = new SnapshotShardPaths(
2090+
paths,
2091+
indexId.getId(),
2092+
indexId.getName(),
2093+
shardCount,
2094+
PathType.fromCode(pathType),
2095+
PathHashAlgorithm.fromCode(pathHashAlgorithm)
20882096
);
2089-
SnapshotShardPaths shardPaths = new SnapshotShardPaths(paths);
20902097
SNAPSHOT_SHARD_PATHS_FORMAT.writeAsyncWithUrgentPriority(
20912098
shardPaths,
20922099
snapshotShardPathBlobContainer(),

Diff for: server/src/main/java/org/opensearch/snapshots/SnapshotShardPaths.java

+41-48
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@
88

99
package org.opensearch.snapshots;
1010

11-
import com.fasterxml.jackson.core.JsonParseException;
12-
13-
import org.opensearch.OpenSearchParseException;
1411
import org.opensearch.core.xcontent.ToXContent;
1512
import org.opensearch.core.xcontent.XContentBuilder;
1613
import org.opensearch.core.xcontent.XContentParser;
14+
import org.opensearch.index.remote.RemoteStoreEnums.PathHashAlgorithm;
15+
import org.opensearch.index.remote.RemoteStoreEnums.PathType;
1716

1817
import java.io.IOException;
19-
import java.util.ArrayList;
20-
import java.util.Collections;
2118
import java.util.List;
2219

2320
/**
@@ -34,19 +31,49 @@ public class SnapshotShardPaths implements ToXContent {
3431
public static final String FILE_NAME_FORMAT = "%s";
3532

3633
private static final String PATHS_FIELD = "paths";
34+
private static final String INDEX_ID_FIELD = "indexId";
35+
private static final String INDEX_NAME_FIELD = "indexName";
36+
private static final String NUMBER_OF_SHARDS_FIELD = "number_of_shards";
37+
private static final String SHARD_PATH_TYPE_FIELD = "shard_path_type";
38+
private static final String SHARD_PATH_HASH_ALGORITHM_FIELD = "shard_path_hash_algorithm";
3739

3840
private final List<String> paths;
39-
40-
public SnapshotShardPaths(List<String> paths) {
41-
this.paths = Collections.unmodifiableList(paths);
42-
}
43-
44-
public List<String> getPaths() {
45-
return paths;
41+
private final String indexId;
42+
private final String indexName;
43+
private final int numberOfShards;
44+
private final PathType shardPathType;
45+
private final PathHashAlgorithm shardPathHashAlgorithm;
46+
47+
public SnapshotShardPaths(
48+
List<String> paths,
49+
String indexId,
50+
String indexName,
51+
int numberOfShards,
52+
PathType shardPathType,
53+
PathHashAlgorithm shardPathHashAlgorithm
54+
) {
55+
assert !paths.isEmpty() : "paths must not be empty";
56+
assert indexId != null && !indexId.isEmpty() : "indexId must not be empty";
57+
assert indexName != null && !indexName.isEmpty() : "indexName must not be empty";
58+
assert numberOfShards > 0 : "numberOfShards must be > 0";
59+
assert shardPathType != null : "shardPathType must not be null";
60+
assert shardPathHashAlgorithm != null : "shardPathHashAlgorithm must not be null";
61+
62+
this.paths = paths;
63+
this.indexId = indexId;
64+
this.indexName = indexName;
65+
this.numberOfShards = numberOfShards;
66+
this.shardPathType = shardPathType;
67+
this.shardPathHashAlgorithm = shardPathHashAlgorithm;
4668
}
4769

4870
@Override
4971
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
72+
builder.field(INDEX_ID_FIELD, indexId);
73+
builder.field(INDEX_NAME_FIELD, indexName);
74+
builder.field(NUMBER_OF_SHARDS_FIELD, numberOfShards);
75+
builder.field(SHARD_PATH_TYPE_FIELD, shardPathType.getCode());
76+
builder.field(SHARD_PATH_HASH_ALGORITHM_FIELD, shardPathHashAlgorithm.getCode());
5077
builder.startArray(PATHS_FIELD);
5178
for (String path : paths) {
5279
builder.value(path);
@@ -55,41 +82,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
5582
return builder;
5683
}
5784

58-
public static SnapshotShardPaths fromXContent(XContentParser parser) throws IOException {
59-
List<String> paths = new ArrayList<>();
60-
61-
try {
62-
XContentParser.Token token = parser.currentToken();
63-
if (token == null) {
64-
token = parser.nextToken();
65-
}
66-
67-
if (token != XContentParser.Token.START_OBJECT) {
68-
throw new OpenSearchParseException("Expected a start object");
69-
}
70-
71-
token = parser.nextToken();
72-
if (token == XContentParser.Token.END_OBJECT) {
73-
throw new OpenSearchParseException("Missing [" + PATHS_FIELD + "] field");
74-
}
75-
76-
while (token != XContentParser.Token.END_OBJECT) {
77-
String fieldName = parser.currentName();
78-
if (PATHS_FIELD.equals(fieldName)) {
79-
if (parser.nextToken() != XContentParser.Token.START_ARRAY) {
80-
throw new OpenSearchParseException("Expected an array for field [" + PATHS_FIELD + "]");
81-
}
82-
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
83-
paths.add(parser.text());
84-
}
85-
} else {
86-
throw new OpenSearchParseException("Unexpected field [" + fieldName + "]");
87-
}
88-
token = parser.nextToken();
89-
}
90-
} catch (JsonParseException e) {
91-
throw new OpenSearchParseException("Failed to parse SnapshotIndexIdPaths", e);
92-
}
93-
return new SnapshotShardPaths(paths);
85+
public static SnapshotShardPaths fromXContent(XContentParser ignored) {
86+
throw new UnsupportedOperationException("SnapshotShardPaths.fromXContent() is not supported");
9487
}
9588
}

Diff for: server/src/test/java/org/opensearch/index/remote/RemoteStoreEnumsTests.java

+42
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.opensearch.index.remote.RemoteStoreEnums.DataType;
1515
import org.opensearch.index.remote.RemoteStoreEnums.PathType;
1616
import org.opensearch.index.remote.RemoteStorePathStrategy.ShardDataPathInput;
17+
import org.opensearch.index.remote.RemoteStorePathStrategy.SnapshotShardPathInput;
1718
import org.opensearch.test.OpenSearchTestCase;
1819

1920
import java.util.ArrayList;
@@ -597,6 +598,47 @@ public void testGeneratePathForHashedInfixType() {
597598
assertTrue(new ParameterizedMessage("expected={} actual={}", expected, actual).getFormattedMessage(), actual.startsWith(expected));
598599
}
599600

601+
public void testGeneratePathForSnapshotShardPathInput() {
602+
BlobPath blobPath = BlobPath.cleanPath().add("xjsdhj").add("ddjsha").add("yudy7sd").add("32hdhua7").add("89jdij");
603+
String indexUUID = "dsdkjsu8832njn";
604+
String shardId = "10";
605+
SnapshotShardPathInput pathInput = SnapshotShardPathInput.builder()
606+
.basePath(blobPath)
607+
.indexUUID(indexUUID)
608+
.shardId(shardId)
609+
.build();
610+
611+
// FIXED PATH
612+
BlobPath result = FIXED.path(pathInput, null);
613+
String expected = "xjsdhj/ddjsha/yudy7sd/32hdhua7/89jdij/indices/dsdkjsu8832njn/10/";
614+
String actual = result.buildAsString();
615+
assertEquals(new ParameterizedMessage("expected={} actual={}", expected, actual).getFormattedMessage(), actual, expected);
616+
617+
// HASHED_PREFIX - FNV_1A_COMPOSITE_1
618+
result = HASHED_PREFIX.path(pathInput, FNV_1A_COMPOSITE_1);
619+
expected = "_11001000010110/xjsdhj/ddjsha/yudy7sd/32hdhua7/89jdij/indices/dsdkjsu8832njn/10/";
620+
actual = result.buildAsString();
621+
assertEquals(new ParameterizedMessage("expected={} actual={}", expected, actual).getFormattedMessage(), actual, expected);
622+
623+
// HASHED_PREFIX - FNV_1A_BASE64
624+
result = HASHED_PREFIX.path(pathInput, FNV_1A_BASE64);
625+
expected = "_yFiSl_VGGM/xjsdhj/ddjsha/yudy7sd/32hdhua7/89jdij/indices/dsdkjsu8832njn/10/";
626+
actual = result.buildAsString();
627+
assertEquals(new ParameterizedMessage("expected={} actual={}", expected, actual).getFormattedMessage(), actual, expected);
628+
629+
// HASHED_INFIX - FNV_1A_COMPOSITE_1
630+
result = HASHED_INFIX.path(pathInput, FNV_1A_COMPOSITE_1);
631+
expected = "xjsdhj/ddjsha/yudy7sd/32hdhua7/89jdij/_11001000010110/indices/dsdkjsu8832njn/10/";
632+
actual = result.buildAsString();
633+
assertEquals(new ParameterizedMessage("expected={} actual={}", expected, actual).getFormattedMessage(), actual, expected);
634+
635+
// HASHED_INFIX - FNV_1A_BASE64
636+
result = HASHED_INFIX.path(pathInput, FNV_1A_BASE64);
637+
expected = "xjsdhj/ddjsha/yudy7sd/32hdhua7/89jdij/_yFiSl_VGGM/indices/dsdkjsu8832njn/10/";
638+
actual = result.buildAsString();
639+
assertEquals(new ParameterizedMessage("expected={} actual={}", expected, actual).getFormattedMessage(), actual, expected);
640+
}
641+
600642
private String derivePath(String basePath, ShardDataPathInput pathInput) {
601643
return "".equals(basePath)
602644
? String.join(

Diff for: server/src/test/java/org/opensearch/index/remote/RemoteStorePathStrategyTests.java

+40
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,46 @@ public void testFixedSubPath() {
8282
.dataType(DATA)
8383
.build();
8484
assertEquals(BlobPath.cleanPath().add(INDEX_UUID).add(SHARD_ID).add(TRANSLOG.getName()).add(DATA.getName()), input2.fixedSubPath());
85+
}
86+
87+
public void testSnapshotShardPathInput() {
88+
assertThrows(NullPointerException.class, () -> RemoteStorePathStrategy.SnapshotShardPathInput.builder().build());
89+
assertThrows(
90+
NullPointerException.class,
91+
() -> RemoteStorePathStrategy.SnapshotShardPathInput.builder().basePath(BASE_PATH).build()
92+
);
93+
assertThrows(
94+
NullPointerException.class,
95+
() -> RemoteStorePathStrategy.SnapshotShardPathInput.builder().indexUUID(INDEX_UUID).build()
96+
);
97+
assertThrows(NullPointerException.class, () -> RemoteStorePathStrategy.SnapshotShardPathInput.builder().shardId(SHARD_ID).build());
98+
99+
RemoteStorePathStrategy.SnapshotShardPathInput input = RemoteStorePathStrategy.SnapshotShardPathInput.builder()
100+
.basePath(BASE_PATH)
101+
.indexUUID(INDEX_UUID)
102+
.shardId(SHARD_ID)
103+
.build();
104+
assertEquals(BASE_PATH, input.basePath());
105+
assertEquals(INDEX_UUID, input.indexUUID());
106+
assertEquals(SHARD_ID, input.shardId());
107+
}
108+
109+
public void testSnapshotShardPathInputFixedSubPath() {
110+
RemoteStorePathStrategy.SnapshotShardPathInput input = RemoteStorePathStrategy.SnapshotShardPathInput.builder()
111+
.basePath(BASE_PATH)
112+
.indexUUID(INDEX_UUID)
113+
.shardId(SHARD_ID)
114+
.build();
115+
assertEquals(BlobPath.cleanPath().add("indices").add(INDEX_UUID).add(SHARD_ID), input.fixedSubPath());
116+
}
85117

118+
public void testSnapshotShardPathInputHashPath() {
119+
RemoteStorePathStrategy.SnapshotShardPathInput input = RemoteStorePathStrategy.SnapshotShardPathInput.builder()
120+
.basePath(BASE_PATH)
121+
.indexUUID(INDEX_UUID)
122+
.shardId(SHARD_ID)
123+
.build();
124+
assertEquals(BlobPath.cleanPath().add(SHARD_ID).add(INDEX_UUID), input.hashPath());
86125
}
126+
87127
}

0 commit comments

Comments
 (0)