Skip to content

Commit 89de9fd

Browse files
Cleanup Blobstore Repository Metadata Serialization (elastic#62727) (elastic#63249)
Follow ups to elastic#62684 making use of shorter utility for corruption checks.
1 parent 509fa46 commit 89de9fd

File tree

4 files changed

+95
-116
lines changed

4 files changed

+95
-116
lines changed

server/src/main/java/org/elasticsearch/index/snapshots/blobstore/BlobStoreIndexShardSnapshot.java

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.elasticsearch.common.xcontent.ToXContentFragment;
3131
import org.elasticsearch.common.xcontent.XContentBuilder;
3232
import org.elasticsearch.common.xcontent.XContentParser;
33+
import org.elasticsearch.common.xcontent.XContentParserUtils;
3334
import org.elasticsearch.index.store.StoreFileMetadata;
3435

3536
import java.io.IOException;
@@ -509,36 +510,33 @@ public static BlobStoreIndexShardSnapshot fromXContent(XContentParser parser) th
509510
XContentParser.Token token = parser.currentToken();
510511
if (token == XContentParser.Token.START_OBJECT) {
511512
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
512-
if (token == XContentParser.Token.FIELD_NAME) {
513-
String currentFieldName = parser.currentName();
514-
token = parser.nextToken();
515-
if (token.isValue()) {
516-
if (PARSE_NAME.match(currentFieldName, parser.getDeprecationHandler())) {
517-
snapshot = parser.text();
518-
} else if (PARSE_INDEX_VERSION.match(currentFieldName, parser.getDeprecationHandler())) {
519-
// The index-version is needed for backward compatibility with v 1.0
520-
indexVersion = parser.longValue();
521-
} else if (PARSE_START_TIME.match(currentFieldName, parser.getDeprecationHandler())) {
522-
startTime = parser.longValue();
523-
} else if (PARSE_TIME.match(currentFieldName, parser.getDeprecationHandler())) {
524-
time = parser.longValue();
525-
} else if (PARSE_INCREMENTAL_FILE_COUNT.match(currentFieldName, parser.getDeprecationHandler())) {
526-
incrementalFileCount = parser.intValue();
527-
} else if (PARSE_INCREMENTAL_SIZE.match(currentFieldName, parser.getDeprecationHandler())) {
528-
incrementalSize = parser.longValue();
529-
} else {
530-
throw new ElasticsearchParseException("unknown parameter [{}]", currentFieldName);
531-
}
532-
} else if (token == XContentParser.Token.START_ARRAY) {
533-
if (PARSE_FILES.match(currentFieldName, parser.getDeprecationHandler())) {
534-
while ((parser.nextToken()) != XContentParser.Token.END_ARRAY) {
535-
indexFiles.add(FileInfo.fromXContent(parser));
536-
}
537-
} else {
538-
throw new ElasticsearchParseException("unknown parameter [{}]", currentFieldName);
513+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser::getTokenLocation);
514+
final String currentFieldName = parser.currentName();
515+
token = parser.nextToken();
516+
if (token.isValue()) {
517+
if (PARSE_NAME.match(currentFieldName, parser.getDeprecationHandler())) {
518+
snapshot = parser.text();
519+
} else if (PARSE_INDEX_VERSION.match(currentFieldName, parser.getDeprecationHandler())) {
520+
// The index-version is needed for backward compatibility with v 1.0
521+
indexVersion = parser.longValue();
522+
} else if (PARSE_START_TIME.match(currentFieldName, parser.getDeprecationHandler())) {
523+
startTime = parser.longValue();
524+
} else if (PARSE_TIME.match(currentFieldName, parser.getDeprecationHandler())) {
525+
time = parser.longValue();
526+
} else if (PARSE_INCREMENTAL_FILE_COUNT.match(currentFieldName, parser.getDeprecationHandler())) {
527+
incrementalFileCount = parser.intValue();
528+
} else if (PARSE_INCREMENTAL_SIZE.match(currentFieldName, parser.getDeprecationHandler())) {
529+
incrementalSize = parser.longValue();
530+
} else {
531+
throw new ElasticsearchParseException("unknown parameter [{}]", currentFieldName);
532+
}
533+
} else if (token == XContentParser.Token.START_ARRAY) {
534+
if (PARSE_FILES.match(currentFieldName, parser.getDeprecationHandler())) {
535+
while ((parser.nextToken()) != XContentParser.Token.END_ARRAY) {
536+
indexFiles.add(FileInfo.fromXContent(parser));
539537
}
540538
} else {
541-
throw new ElasticsearchParseException("unexpected token [{}]", token);
539+
throw new ElasticsearchParseException("unknown parameter [{}]", currentFieldName);
542540
}
543541
} else {
544542
throw new ElasticsearchParseException("unexpected token [{}]", token);

server/src/main/java/org/elasticsearch/index/snapshots/blobstore/BlobStoreIndexShardSnapshots.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.elasticsearch.common.xcontent.ToXContentFragment;
2525
import org.elasticsearch.common.xcontent.XContentBuilder;
2626
import org.elasticsearch.common.xcontent.XContentParser;
27+
import org.elasticsearch.common.xcontent.XContentParserUtils;
2728
import org.elasticsearch.index.snapshots.blobstore.BlobStoreIndexShardSnapshot.FileInfo;
2829

2930
import java.io.IOException;
@@ -237,9 +238,7 @@ public static BlobStoreIndexShardSnapshots fromXContent(XContentParser parser) t
237238
Map<String, FileInfo> files = new HashMap<>();
238239
if (token == XContentParser.Token.START_OBJECT) {
239240
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
240-
if (token != XContentParser.Token.FIELD_NAME) {
241-
throw new ElasticsearchParseException("unexpected token [{}]", token);
242-
}
241+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser::getTokenLocation);
243242
String currentFieldName = parser.currentName();
244243
token = parser.nextToken();
245244
if (token == XContentParser.Token.START_ARRAY) {
@@ -255,13 +254,10 @@ public static BlobStoreIndexShardSnapshots fromXContent(XContentParser parser) t
255254
throw new ElasticsearchParseException("unknown object [{}]", currentFieldName);
256255
}
257256
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
258-
if (token != XContentParser.Token.FIELD_NAME) {
259-
throw new ElasticsearchParseException("unknown object [{}]", currentFieldName);
260-
}
257+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser::getTokenLocation);
261258
String snapshot = parser.currentName();
262-
if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
263-
throw new ElasticsearchParseException("unknown object [{}]", currentFieldName);
264-
}
259+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(),
260+
parser::getTokenLocation);
265261
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
266262
if (token == XContentParser.Token.FIELD_NAME) {
267263
currentFieldName = parser.currentName();

server/src/main/java/org/elasticsearch/repositories/RepositoryData.java

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -529,9 +529,7 @@ public IndexMetaDataGenerations indexMetaDataGenerations() {
529529
* from cached bytes that we trust to not contain broken generations.
530530
*/
531531
public static RepositoryData snapshotsFromXContent(XContentParser parser, long genId, boolean fixBrokenShardGens) throws IOException {
532-
if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
533-
throw new ElasticsearchParseException("start object expected");
534-
}
532+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
535533

536534
final Map<String, SnapshotId> snapshots = new HashMap<>();
537535
final Map<String, SnapshotState> snapshotStates = new HashMap<>();
@@ -551,15 +549,13 @@ public static RepositoryData snapshotsFromXContent(XContentParser parser, long g
551549
parseIndices(parser, fixBrokenShardGens, snapshots, indexSnapshots, indexLookup, shardGenerations);
552550
break;
553551
case INDEX_METADATA_IDENTIFIERS:
554-
if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
555-
throw new ElasticsearchParseException("start object expected [" + INDEX_METADATA_IDENTIFIERS + "]");
556-
}
552+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(),
553+
parser::getTokenLocation);
557554
indexMetaIdentifiers = parser.mapStrings();
558555
break;
559556
case MIN_VERSION:
560-
if (parser.nextToken() != XContentParser.Token.VALUE_STRING) {
561-
throw new ElasticsearchParseException("version string expected [min_version]");
562-
}
557+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.VALUE_STRING, parser.nextToken(),
558+
parser::getTokenLocation);
563559
final Version version = Version.fromString(parser.text());
564560
assert SnapshotsService.useShardGenerations(version);
565561
break;
@@ -615,9 +611,7 @@ private static IndexMetaDataGenerations buildIndexMetaGenerations(Map<SnapshotId
615611
private static void parseSnapshots(XContentParser parser, Map<String, SnapshotId> snapshots, Map<String, SnapshotState> snapshotStates,
616612
Map<String, Version> snapshotVersions,
617613
Map<SnapshotId, Map<String, String>> indexMetaLookup) throws IOException {
618-
if (parser.nextToken() != XContentParser.Token.START_ARRAY) {
619-
throw new ElasticsearchParseException("expected array for [" + SNAPSHOTS + "]");
620-
}
614+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_ARRAY, parser.nextToken(), parser::getTokenLocation);
621615
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
622616
String name = null;
623617
String uuid = null;
@@ -673,18 +667,14 @@ private static void parseSnapshots(XContentParser parser, Map<String, SnapshotId
673667
private static void parseIndices(XContentParser parser, boolean fixBrokenShardGens, Map<String, SnapshotId> snapshots,
674668
Map<IndexId, List<SnapshotId>> indexSnapshots, Map<String, IndexId> indexLookup,
675669
ShardGenerations.Builder shardGenerations) throws IOException {
676-
if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
677-
throw new ElasticsearchParseException("start object expected [indices]");
678-
}
670+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
679671
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
680672
final String indexName = parser.currentName();
681673
final List<SnapshotId> snapshotIds = new ArrayList<>();
682674
final List<String> gens = new ArrayList<>();
683675

684676
IndexId indexId = null;
685-
if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
686-
throw new ElasticsearchParseException("start object expected index[" + indexName + "]");
687-
}
677+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
688678
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
689679
final String indexMetaFieldName = parser.currentName();
690680
final XContentParser.Token currentToken = parser.nextToken();
@@ -693,9 +683,7 @@ private static void parseIndices(XContentParser parser, boolean fixBrokenShardGe
693683
indexId = new IndexId(indexName, parser.text());
694684
break;
695685
case SNAPSHOTS:
696-
if (currentToken != XContentParser.Token.START_ARRAY) {
697-
throw new ElasticsearchParseException("start array expected [snapshots]");
698-
}
686+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_ARRAY, currentToken, parser::getTokenLocation);
699687
XContentParser.Token currToken;
700688
while ((currToken = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
701689
final String uuid;

0 commit comments

Comments
 (0)