Skip to content

Commit 39779d2

Browse files
committed
Add versioning for UploadedIndexMetadata
Signed-off-by: Sooraj Sinha <[email protected]>
1 parent 5303ebc commit 39779d2

File tree

2 files changed

+59
-34
lines changed

2 files changed

+59
-34
lines changed

server/src/main/java/org/opensearch/gateway/remote/ClusterMetadataManifest.java

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.opensearch.core.xcontent.ToXContentFragment;
2121
import org.opensearch.core.xcontent.XContentBuilder;
2222
import org.opensearch.core.xcontent.XContentParser;
23+
import org.opensearch.gateway.remote.ClusterMetadataManifest.Builder;
2324

2425
import java.io.IOException;
2526
import java.util.ArrayList;
@@ -243,7 +244,7 @@ private static void declareParser(ConstructingObjectParser<ClusterMetadataManife
243244
parser.declareBoolean(ConstructingObjectParser.constructorArg(), COMMITTED_FIELD);
244245
parser.declareObjectArray(
245246
ConstructingObjectParser.constructorArg(),
246-
(p, c) -> UploadedIndexMetadata.fromXContent(p),
247+
(p, c) -> UploadedIndexMetadata.fromXContent(p, codec_version),
247248
INDICES_FIELD
248249
);
249250
parser.declareString(ConstructingObjectParser.constructorArg(), PREVIOUS_CLUSTER_UUID);
@@ -277,7 +278,7 @@ private static void declareParser(ConstructingObjectParser<ClusterMetadataManife
277278
parser.declareLong(ConstructingObjectParser.constructorArg(), ROUTING_TABLE_VERSION_FIELD);
278279
parser.declareObjectArray(
279280
ConstructingObjectParser.constructorArg(),
280-
(p, c) -> UploadedIndexMetadata.fromXContent(p),
281+
(p, c) -> UploadedIndexMetadata.fromXContent(p, codec_version),
281282
INDICES_ROUTING_FIELD
282283
);
283284
parser.declareNamedObject(
@@ -1112,16 +1113,30 @@ private static String componentPrefix(Object[] fields) {
11121113
return (String) fields[3];
11131114
}
11141115

1115-
private static final ConstructingObjectParser<UploadedIndexMetadata, Void> PARSER = new ConstructingObjectParser<>(
1116+
private static final ConstructingObjectParser<UploadedIndexMetadata, Void> PARSER_V0 = new ConstructingObjectParser<>(
1117+
"uploaded_index_metadata",
1118+
fields -> new UploadedIndexMetadata(indexName(fields), indexUUID(fields), uploadedFilename(fields))
1119+
);
1120+
1121+
private static final ConstructingObjectParser<UploadedIndexMetadata, Void> PARSER_V2 = new ConstructingObjectParser<>(
11161122
"uploaded_index_metadata",
11171123
fields -> new UploadedIndexMetadata(indexName(fields), indexUUID(fields), uploadedFilename(fields), componentPrefix(fields))
11181124
);
11191125

1126+
private static final ConstructingObjectParser<UploadedIndexMetadata, Void> CURRENT_PARSER = PARSER_V2;
1127+
11201128
static {
1121-
PARSER.declareString(ConstructingObjectParser.constructorArg(), INDEX_NAME_FIELD);
1122-
PARSER.declareString(ConstructingObjectParser.constructorArg(), INDEX_UUID_FIELD);
1123-
PARSER.declareString(ConstructingObjectParser.constructorArg(), UPLOADED_FILENAME_FIELD);
1124-
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), COMPONENT_PREFIX_FIELD);
1129+
declareParser(PARSER_V0, CODEC_V0);
1130+
declareParser(PARSER_V2, CODEC_V2);
1131+
}
1132+
1133+
private static void declareParser(ConstructingObjectParser<UploadedIndexMetadata, Void> parser, long codec_version) {
1134+
parser.declareString(ConstructingObjectParser.constructorArg(), INDEX_NAME_FIELD);
1135+
parser.declareString(ConstructingObjectParser.constructorArg(), INDEX_UUID_FIELD);
1136+
parser.declareString(ConstructingObjectParser.constructorArg(), UPLOADED_FILENAME_FIELD);
1137+
if (codec_version >= CODEC_V2) {
1138+
parser.declareString(ConstructingObjectParser.constructorArg(), COMPONENT_PREFIX_FIELD);
1139+
}
11251140
}
11261141

11271142
static final String COMPONENT_PREFIX = "index--";
@@ -1130,15 +1145,32 @@ private static String componentPrefix(Object[] fields) {
11301145
private final String indexUUID;
11311146
private final String uploadedFilename;
11321147

1148+
private long codecVersion = CODEC_V2;
1149+
11331150
public UploadedIndexMetadata(String indexName, String indexUUID, String uploadedFileName) {
1134-
this(indexName, indexUUID, uploadedFileName, COMPONENT_PREFIX);
1151+
this(indexName, indexUUID, uploadedFileName, CODEC_V2);
1152+
}
1153+
1154+
public UploadedIndexMetadata(String indexName, String indexUUID, String uploadedFileName, long codecVersion) {
1155+
this(indexName, indexUUID, uploadedFileName, COMPONENT_PREFIX, codecVersion);
11351156
}
11361157

11371158
public UploadedIndexMetadata(String indexName, String indexUUID, String uploadedFileName, String componentPrefix) {
1159+
this(indexName, indexUUID, uploadedFileName, COMPONENT_PREFIX, CODEC_V2);
1160+
}
1161+
1162+
public UploadedIndexMetadata(
1163+
String indexName,
1164+
String indexUUID,
1165+
String uploadedFileName,
1166+
String componentPrefix,
1167+
long codecVersion
1168+
) {
11381169
this.componentPrefix = componentPrefix;
11391170
this.indexName = indexName;
11401171
this.indexUUID = indexUUID;
11411172
this.uploadedFilename = uploadedFileName;
1173+
this.codecVersion = codecVersion;
11421174
}
11431175

11441176
public UploadedIndexMetadata(StreamInput in) throws IOException {
@@ -1175,10 +1207,13 @@ public String getComponentPrefix() {
11751207

11761208
@Override
11771209
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
1178-
return builder.field(INDEX_NAME_FIELD.getPreferredName(), getIndexName())
1210+
builder.field(INDEX_NAME_FIELD.getPreferredName(), getIndexName())
11791211
.field(INDEX_UUID_FIELD.getPreferredName(), getIndexUUID())
1180-
.field(UPLOADED_FILENAME_FIELD.getPreferredName(), getUploadedFilePath())
1181-
.field(COMPONENT_PREFIX_FIELD.getPreferredName(), getComponentPrefix());
1212+
.field(UPLOADED_FILENAME_FIELD.getPreferredName(), getUploadedFilePath());
1213+
if (codecVersion >= CODEC_V2) {
1214+
builder.field(COMPONENT_PREFIX_FIELD.getPreferredName(), getComponentPrefix());
1215+
}
1216+
return builder;
11821217
}
11831218

11841219
@Override
@@ -1214,9 +1249,13 @@ public String toString() {
12141249
return Strings.toString(MediaTypeRegistry.JSON, this);
12151250
}
12161251

1217-
public static UploadedIndexMetadata fromXContent(XContentParser parser) throws IOException {
1218-
return PARSER.parse(parser, null);
1252+
public static UploadedIndexMetadata fromXContent(XContentParser parser, long codecVersion) throws IOException {
1253+
if (codecVersion >= CODEC_V2) {
1254+
return CURRENT_PARSER.parse(parser, null);
1255+
}
1256+
return PARSER_V0.parse(parser, null);
12191257
}
1258+
12201259
}
12211260

12221261
/**

server/src/test/java/org/opensearch/gateway/remote/ClusterMetadataManifestTests.java

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
public class ClusterMetadataManifestTests extends OpenSearchTestCase {
4949

5050
public void testClusterMetadataManifestXContentV0() throws IOException {
51-
UploadedIndexMetadata uploadedIndexMetadata = new UploadedIndexMetadata("test-index", "test-uuid", "/test/upload/path");
51+
UploadedIndexMetadata uploadedIndexMetadata = new UploadedIndexMetadata("test-index", "test-uuid", "/test/upload/path", CODEC_V0);
5252
ClusterMetadataManifest originalManifest = ClusterMetadataManifest.builder()
5353
.clusterTerm(1L)
5454
.stateVersion(1L)
@@ -74,7 +74,7 @@ public void testClusterMetadataManifestXContentV0() throws IOException {
7474
}
7575

7676
public void testClusterMetadataManifestXContentV1() throws IOException {
77-
UploadedIndexMetadata uploadedIndexMetadata = new UploadedIndexMetadata("test-index", "test-uuid", "/test/upload/path");
77+
UploadedIndexMetadata uploadedIndexMetadata = new UploadedIndexMetadata("test-index", "test-uuid", "/test/upload/path", CODEC_V1);
7878
ClusterMetadataManifest originalManifest = ClusterMetadataManifest.builder()
7979
.clusterTerm(1L)
8080
.stateVersion(1L)
@@ -620,21 +620,20 @@ public void testUploadedIndexMetadataSerializationEqualsHashCode() {
620620
}
621621

622622
public void testUploadedIndexMetadataWithoutComponentPrefix() throws IOException {
623-
final UploadedIndexMetadata originalUploadedIndexMetadata = new UploadedIndexMetadataV1(
623+
final UploadedIndexMetadata originalUploadedIndexMetadata = new UploadedIndexMetadata(
624624
"test-index",
625625
"test-index-uuid",
626-
"test_file_name"
626+
"test_file_name",
627+
CODEC_V1
627628
);
628629
final XContentBuilder builder = JsonXContent.contentBuilder();
629630
builder.startObject();
630631
originalUploadedIndexMetadata.toXContent(builder, ToXContent.EMPTY_PARAMS);
631632
builder.endObject();
632633

633634
try (XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder))) {
634-
final UploadedIndexMetadata fromXContentUploadedIndexMetadata = UploadedIndexMetadata.fromXContent(parser);
635-
assertEquals(originalUploadedIndexMetadata.getIndexName(), fromXContentUploadedIndexMetadata.getIndexName());
636-
assertEquals(originalUploadedIndexMetadata.getIndexUUID(), fromXContentUploadedIndexMetadata.getIndexUUID());
637-
assertEquals(originalUploadedIndexMetadata.getUploadedFilename(), fromXContentUploadedIndexMetadata.getUploadedFilename());
635+
final UploadedIndexMetadata fromXContentUploadedIndexMetadata = UploadedIndexMetadata.fromXContent(parser, 1L);
636+
assertEquals(originalUploadedIndexMetadata, fromXContentUploadedIndexMetadata);
638637
}
639638
}
640639

@@ -662,17 +661,4 @@ private UploadedIndexMetadata randomlyChangingUploadedIndexMetadata(UploadedInde
662661
return uploadedIndexMetadata;
663662
}
664663

665-
private static class UploadedIndexMetadataV1 extends UploadedIndexMetadata {
666-
667-
public UploadedIndexMetadataV1(String indexName, String indexUUID, String uploadedFileName) {
668-
super(indexName, indexUUID, uploadedFileName);
669-
}
670-
671-
@Override
672-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
673-
return builder.field("index_name", getIndexName())
674-
.field("index_uuid", getIndexUUID())
675-
.field("uploaded_filename", getUploadedFilePath());
676-
}
677-
}
678664
}

0 commit comments

Comments
 (0)