14
14
import org .opensearch .core .xcontent .ToXContent ;
15
15
import org .opensearch .core .xcontent .XContentBuilder ;
16
16
import org .opensearch .core .xcontent .XContentParser ;
17
+ import org .opensearch .index .remote .RemoteStoreEnums .PathHashAlgorithm ;
18
+ import org .opensearch .index .remote .RemoteStoreEnums .PathType ;
17
19
import org .opensearch .test .OpenSearchTestCase ;
18
20
19
21
import java .io .IOException ;
@@ -40,7 +42,10 @@ public void testToXContent() throws IOException {
40
42
String repositoryBasePath = "test-repo-basepath" ;
41
43
List <String > fileNames = new ArrayList <>(5 );
42
44
fileNames .addAll (Arrays .asList ("file1" , "file2" , "file3" , "file4" , "file5" ));
45
+
46
+ // Case 1 - Without remote path type fields
43
47
RemoteStoreShardShallowCopySnapshot shardShallowCopySnapshot = new RemoteStoreShardShallowCopySnapshot (
48
+ "1" ,
44
49
snapshot ,
45
50
indexVersion ,
46
51
primaryTerm ,
@@ -52,7 +57,9 @@ public void testToXContent() throws IOException {
52
57
indexUUID ,
53
58
remoteStoreRepository ,
54
59
repositoryBasePath ,
55
- fileNames
60
+ fileNames ,
61
+ null ,
62
+ null
56
63
);
57
64
String actual ;
58
65
try (XContentBuilder builder = MediaTypeRegistry .JSON .contentBuilder ()) {
@@ -66,6 +73,67 @@ public void testToXContent() throws IOException {
66
73
+ "\" test-rs-repository\" ,\" commit_generation\" :5,\" primary_term\" :3,\" remote_store_repository_base_path\" :"
67
74
+ "\" test-repo-basepath\" ,\" file_names\" :[\" file1\" ,\" file2\" ,\" file3\" ,\" file4\" ,\" file5\" ]}" ;
68
75
assert Objects .equals (actual , expectedXContent ) : "xContent is " + actual ;
76
+
77
+ // Case 2 - with just fixed type
78
+ shardShallowCopySnapshot = new RemoteStoreShardShallowCopySnapshot (
79
+ snapshot ,
80
+ indexVersion ,
81
+ primaryTerm ,
82
+ commitGeneration ,
83
+ startTime ,
84
+ time ,
85
+ totalFileCount ,
86
+ totalSize ,
87
+ indexUUID ,
88
+ remoteStoreRepository ,
89
+ repositoryBasePath ,
90
+ fileNames ,
91
+ PathType .FIXED ,
92
+ null
93
+ );
94
+ try (XContentBuilder builder = MediaTypeRegistry .JSON .contentBuilder ()) {
95
+ builder .startObject ();
96
+ shardShallowCopySnapshot .toXContent (builder , ToXContent .EMPTY_PARAMS );
97
+ builder .endObject ();
98
+ actual = builder .toString ();
99
+ }
100
+
101
+ expectedXContent = "{\" version\" :\" 2\" ,\" name\" :\" test-snapshot\" ,\" index_version\" :1,\" start_time\" :123,\" time\" :123,"
102
+ + "\" number_of_files\" :5,\" total_size\" :5,\" index_uuid\" :\" syzhajds-ashdlfj\" ,\" remote_store_repository\" :"
103
+ + "\" test-rs-repository\" ,\" commit_generation\" :5,\" primary_term\" :3,\" remote_store_repository_base_path\" :"
104
+ + "\" test-repo-basepath\" ,\" file_names\" :[\" file1\" ,\" file2\" ,\" file3\" ,\" file4\" ,\" file5\" ],\" path_type\" :0}" ;
105
+ assert Objects .equals (actual , expectedXContent ) : "xContent is " + actual ;
106
+
107
+ // Case 3 - with just hashed prefix type and hash algorithm
108
+ shardShallowCopySnapshot = new RemoteStoreShardShallowCopySnapshot (
109
+ snapshot ,
110
+ indexVersion ,
111
+ primaryTerm ,
112
+ commitGeneration ,
113
+ startTime ,
114
+ time ,
115
+ totalFileCount ,
116
+ totalSize ,
117
+ indexUUID ,
118
+ remoteStoreRepository ,
119
+ repositoryBasePath ,
120
+ fileNames ,
121
+ PathType .HASHED_PREFIX ,
122
+ PathHashAlgorithm .FNV_1A
123
+ );
124
+ try (XContentBuilder builder = MediaTypeRegistry .JSON .contentBuilder ()) {
125
+ builder .startObject ();
126
+ shardShallowCopySnapshot .toXContent (builder , ToXContent .EMPTY_PARAMS );
127
+ builder .endObject ();
128
+ actual = builder .toString ();
129
+ }
130
+
131
+ expectedXContent = "{\" version\" :\" 2\" ,\" name\" :\" test-snapshot\" ,\" index_version\" :1,\" start_time\" :123,\" time\" :123,"
132
+ + "\" number_of_files\" :5,\" total_size\" :5,\" index_uuid\" :\" syzhajds-ashdlfj\" ,\" remote_store_repository\" :"
133
+ + "\" test-rs-repository\" ,\" commit_generation\" :5,\" primary_term\" :3,\" remote_store_repository_base_path\" :"
134
+ + "\" test-repo-basepath\" ,\" file_names\" :[\" file1\" ,\" file2\" ,\" file3\" ,\" file4\" ,\" file5\" ],\" path_type\" :1"
135
+ + ",\" path_hash_algorithm\" :0}" ;
136
+ assert Objects .equals (actual , expectedXContent ) : "xContent is " + actual ;
69
137
}
70
138
71
139
public void testFromXContent () throws IOException {
@@ -83,6 +151,7 @@ public void testFromXContent() throws IOException {
83
151
List <String > fileNames = new ArrayList <>(5 );
84
152
fileNames .addAll (Arrays .asList ("file1" , "file2" , "file3" , "file4" , "file5" ));
85
153
RemoteStoreShardShallowCopySnapshot expectedShardShallowCopySnapshot = new RemoteStoreShardShallowCopySnapshot (
154
+ "1" ,
86
155
snapshot ,
87
156
indexVersion ,
88
157
primaryTerm ,
@@ -94,30 +163,76 @@ public void testFromXContent() throws IOException {
94
163
indexUUID ,
95
164
remoteStoreRepository ,
96
165
repositoryBasePath ,
97
- fileNames
166
+ fileNames ,
167
+ null ,
168
+ null
98
169
);
99
170
String xContent = "{\" version\" :\" 1\" ,\" name\" :\" test-snapshot\" ,\" index_version\" :1,\" start_time\" :123,\" time\" :123,"
100
171
+ "\" number_of_files\" :5,\" total_size\" :5,\" index_uuid\" :\" syzhajds-ashdlfj\" ,\" remote_store_repository\" :"
101
172
+ "\" test-rs-repository\" ,\" commit_generation\" :5,\" primary_term\" :3,\" remote_store_repository_base_path\" :"
102
173
+ "\" test-repo-basepath\" ,\" file_names\" :[\" file1\" ,\" file2\" ,\" file3\" ,\" file4\" ,\" file5\" ]}" ;
103
174
try (XContentParser parser = createParser (JsonXContent .jsonXContent , xContent )) {
104
175
RemoteStoreShardShallowCopySnapshot actualShardShallowCopySnapshot = RemoteStoreShardShallowCopySnapshot .fromXContent (parser );
105
- assertEquals (actualShardShallowCopySnapshot .snapshot (), expectedShardShallowCopySnapshot .snapshot ());
106
- assertEquals (
107
- actualShardShallowCopySnapshot .getRemoteStoreRepository (),
108
- expectedShardShallowCopySnapshot .getRemoteStoreRepository ()
109
- );
110
- assertEquals (actualShardShallowCopySnapshot .getCommitGeneration (), expectedShardShallowCopySnapshot .getCommitGeneration ());
111
- assertEquals (actualShardShallowCopySnapshot .getPrimaryTerm (), expectedShardShallowCopySnapshot .getPrimaryTerm ());
112
- assertEquals (actualShardShallowCopySnapshot .startTime (), expectedShardShallowCopySnapshot .startTime ());
113
- assertEquals (actualShardShallowCopySnapshot .time (), expectedShardShallowCopySnapshot .time ());
114
- assertEquals (actualShardShallowCopySnapshot .totalSize (), expectedShardShallowCopySnapshot .totalSize ());
115
- assertEquals (actualShardShallowCopySnapshot .totalFileCount (), expectedShardShallowCopySnapshot .totalFileCount ());
176
+ assert Objects .equals (expectedShardShallowCopySnapshot , actualShardShallowCopySnapshot );
177
+ }
178
+
179
+ // with pathType=PathType.FIXED
180
+ xContent = "{\" version\" :\" 2\" ,\" name\" :\" test-snapshot\" ,\" index_version\" :1,\" start_time\" :123,\" time\" :123,"
181
+ + "\" number_of_files\" :5,\" total_size\" :5,\" index_uuid\" :\" syzhajds-ashdlfj\" ,\" remote_store_repository\" :"
182
+ + "\" test-rs-repository\" ,\" commit_generation\" :5,\" primary_term\" :3,\" remote_store_repository_base_path\" :"
183
+ + "\" test-repo-basepath\" ,\" file_names\" :[\" file1\" ,\" file2\" ,\" file3\" ,\" file4\" ,\" file5\" ],\" path_type\" :0}" ;
184
+ expectedShardShallowCopySnapshot = new RemoteStoreShardShallowCopySnapshot (
185
+ "2" ,
186
+ snapshot ,
187
+ indexVersion ,
188
+ primaryTerm ,
189
+ commitGeneration ,
190
+ startTime ,
191
+ time ,
192
+ totalFileCount ,
193
+ totalSize ,
194
+ indexUUID ,
195
+ remoteStoreRepository ,
196
+ repositoryBasePath ,
197
+ fileNames ,
198
+ PathType .FIXED ,
199
+ null
200
+ );
201
+ try (XContentParser parser = createParser (JsonXContent .jsonXContent , xContent )) {
202
+ RemoteStoreShardShallowCopySnapshot actualShardShallowCopySnapshot = RemoteStoreShardShallowCopySnapshot .fromXContent (parser );
203
+ assert Objects .equals (expectedShardShallowCopySnapshot , actualShardShallowCopySnapshot );
204
+ }
205
+
206
+ // with pathType=PathType.HASHED_PREFIX and pathHashAlgorithm=PathHashAlgorithm.FNV_1A
207
+ xContent = "{\" version\" :\" 2\" ,\" name\" :\" test-snapshot\" ,\" index_version\" :1,\" start_time\" :123,\" time\" :123,"
208
+ + "\" number_of_files\" :5,\" total_size\" :5,\" index_uuid\" :\" syzhajds-ashdlfj\" ,\" remote_store_repository\" :"
209
+ + "\" test-rs-repository\" ,\" commit_generation\" :5,\" primary_term\" :3,\" remote_store_repository_base_path\" :"
210
+ + "\" test-repo-basepath\" ,\" file_names\" :[\" file1\" ,\" file2\" ,\" file3\" ,\" file4\" ,\" file5\" ],\" path_type\" :1,\" path_hash_algorithm\" :0}" ;
211
+ expectedShardShallowCopySnapshot = new RemoteStoreShardShallowCopySnapshot (
212
+ "2" ,
213
+ snapshot ,
214
+ indexVersion ,
215
+ primaryTerm ,
216
+ commitGeneration ,
217
+ startTime ,
218
+ time ,
219
+ totalFileCount ,
220
+ totalSize ,
221
+ indexUUID ,
222
+ remoteStoreRepository ,
223
+ repositoryBasePath ,
224
+ fileNames ,
225
+ PathType .HASHED_PREFIX ,
226
+ PathHashAlgorithm .FNV_1A
227
+ );
228
+ try (XContentParser parser = createParser (JsonXContent .jsonXContent , xContent )) {
229
+ RemoteStoreShardShallowCopySnapshot actualShardShallowCopySnapshot = RemoteStoreShardShallowCopySnapshot .fromXContent (parser );
230
+ assert Objects .equals (expectedShardShallowCopySnapshot , actualShardShallowCopySnapshot );
116
231
}
117
232
}
118
233
119
234
public void testFromXContentInvalid () throws IOException {
120
- final int iters = scaledRandomIntBetween ( 1 , 10 ) ;
235
+ final int iters = 14 ;
121
236
for (int iter = 0 ; iter < iters ; iter ++) {
122
237
String snapshot = "test-snapshot" ;
123
238
long indexVersion = 1 ;
@@ -133,10 +248,11 @@ public void testFromXContentInvalid() throws IOException {
133
248
List <String > fileNames = new ArrayList <>(5 );
134
249
fileNames .addAll (Arrays .asList ("file1" , "file2" , "file3" , "file4" , "file5" ));
135
250
String failure = null ;
136
- String version = RemoteStoreShardShallowCopySnapshot .DEFAULT_VERSION ;
137
- long length = Math .max (0 , Math .abs (randomLong ()));
251
+ String version = "1" ;
252
+ PathType pathType = null ;
253
+ PathHashAlgorithm pathHashAlgorithm = null ;
138
254
// random corruption
139
- switch (randomIntBetween ( 0 , 8 ) ) {
255
+ switch (iter ) {
140
256
case 0 :
141
257
snapshot = null ;
142
258
failure = "Invalid/Missing Snapshot Name" ;
@@ -170,6 +286,31 @@ public void testFromXContentInvalid() throws IOException {
170
286
failure = "Invalid Version Provided" ;
171
287
break ;
172
288
case 8 :
289
+ version = "2" ;
290
+ failure = "Invalid combination of pathType=null pathHashAlgorithm=null for version=2" ;
291
+ break ;
292
+ case 9 :
293
+ version = "1" ;
294
+ pathType = PathType .FIXED ;
295
+ failure = "Invalid combination of pathType=FIXED pathHashAlgorithm=null for version=1" ;
296
+ break ;
297
+ case 10 :
298
+ version = "1" ;
299
+ pathHashAlgorithm = PathHashAlgorithm .FNV_1A ;
300
+ failure = "Invalid combination of pathType=null pathHashAlgorithm=FNV_1A for version=1" ;
301
+ break ;
302
+ case 11 :
303
+ version = "2" ;
304
+ pathType = PathType .FIXED ;
305
+ pathHashAlgorithm = PathHashAlgorithm .FNV_1A ;
306
+ failure = "Invalid combination of pathType=FIXED pathHashAlgorithm=FNV_1A for version=2" ;
307
+ break ;
308
+ case 12 :
309
+ version = "2" ;
310
+ pathType = PathType .HASHED_PREFIX ;
311
+ pathHashAlgorithm = PathHashAlgorithm .FNV_1A ;
312
+ break ;
313
+ case 13 :
173
314
break ;
174
315
default :
175
316
fail ("shouldn't be here" );
@@ -194,6 +335,14 @@ public void testFromXContentInvalid() throws IOException {
194
335
builder .value (fileName );
195
336
}
196
337
builder .endArray ();
338
+ // We are handling NP check since a cluster can have indexes created earlier which do not have remote store
339
+ // path type and path hash algorithm in its custom data in index metadata.
340
+ if (Objects .nonNull (pathType )) {
341
+ builder .field (RemoteStoreShardShallowCopySnapshot .PATH_TYPE , pathType .getCode ());
342
+ }
343
+ if (Objects .nonNull (pathHashAlgorithm )) {
344
+ builder .field (RemoteStoreShardShallowCopySnapshot .PATH_HASH_ALGORITHM , pathHashAlgorithm .getCode ());
345
+ }
197
346
builder .endObject ();
198
347
byte [] xContent = BytesReference .toBytes (BytesReference .bytes (builder ));
199
348
@@ -211,7 +360,8 @@ public void testFromXContentInvalid() throws IOException {
211
360
assertEquals (remoteStoreShardShallowCopySnapshot .startTime (), startTime );
212
361
assertEquals (remoteStoreShardShallowCopySnapshot .time (), time );
213
362
assertEquals (remoteStoreShardShallowCopySnapshot .totalSize (), totalSize );
214
- assertEquals (remoteStoreShardShallowCopySnapshot .totalFileCount (), totalFileCount );
363
+ assertEquals (remoteStoreShardShallowCopySnapshot .getPathType (), pathType );
364
+ assertEquals (remoteStoreShardShallowCopySnapshot .getPathHashAlgorithm (), pathHashAlgorithm );
215
365
} else {
216
366
try (XContentParser parser = createParser (JsonXContent .jsonXContent , xContent )) {
217
367
parser .nextToken ();
0 commit comments