diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotIndexShardStatus.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotIndexShardStatus.java index ca3611d279e81..74567e838eca8 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotIndexShardStatus.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotIndexShardStatus.java @@ -9,28 +9,17 @@ package org.elasticsearch.action.admin.cluster.snapshots.status; -import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.action.support.broadcast.BroadcastShardResponse; -import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.xcontent.XContentParserUtils; -import org.elasticsearch.index.Index; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.index.snapshots.IndexShardSnapshotStatus; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Objects; -import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; -import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg; - public class SnapshotIndexShardStatus extends BroadcastShardResponse implements ToXContentFragment { private final SnapshotIndexShardStage stage; @@ -149,59 +138,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return builder; } - static final ObjectParser.NamedObjectParser PARSER; - static { - ConstructingObjectParser innerParser = new ConstructingObjectParser<>( - "snapshot_index_shard_status", - true, - (Object[] parsedObjects, ShardId shard) -> { - int i = 0; - String rawStage = (String) parsedObjects[i++]; - String nodeId = (String) parsedObjects[i++]; - String failure = (String) parsedObjects[i++]; - SnapshotStats stats = (SnapshotStats) parsedObjects[i]; - - SnapshotIndexShardStage stage; - try { - stage = SnapshotIndexShardStage.valueOf(rawStage); - } catch (IllegalArgumentException iae) { - throw new ElasticsearchParseException( - "failed to parse snapshot index shard status [{}][{}], unknown stage [{}]", - shard.getIndex().getName(), - shard.getId(), - rawStage - ); - } - return new SnapshotIndexShardStatus(shard, stage, stats, nodeId, failure); - } - ); - innerParser.declareString(constructorArg(), new ParseField(Fields.STAGE)); - innerParser.declareString(optionalConstructorArg(), new ParseField(Fields.NODE)); - innerParser.declareString(optionalConstructorArg(), new ParseField(Fields.REASON)); - innerParser.declareObject(constructorArg(), (p, c) -> SnapshotStats.fromXContent(p), new ParseField(SnapshotStats.Fields.STATS)); - PARSER = (p, indexId, shardName) -> { - // Combine the index name in the context with the shard name passed in for the named object parser - // into a ShardId to pass as context for the inner parser. - int shard; - try { - shard = Integer.parseInt(shardName); - } catch (NumberFormatException nfe) { - throw new ElasticsearchParseException( - "failed to parse snapshot index shard status [{}], expected numeric shard id but got [{}]", - indexId, - shardName - ); - } - ShardId shardId = new ShardId(new Index(indexId, IndexMetadata.INDEX_UUID_NA_VALUE), shard); - return innerParser.parse(p, shardId); - }; - } - - public static SnapshotIndexShardStatus fromXContent(XContentParser parser, String indexId) throws IOException { - XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.currentToken(), parser); - return PARSER.parse(parser, indexId, parser.currentName()); - } - @Override public boolean equals(Object o) { if (this == o) { diff --git a/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotIndexShardStatusTests.java b/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotIndexShardStatusTests.java index d570a928581c6..f79bd5f6ad864 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotIndexShardStatusTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotIndexShardStatusTests.java @@ -9,16 +9,23 @@ package org.elasticsearch.action.admin.cluster.snapshots.status; +import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.xcontent.XContentParserUtils; import org.elasticsearch.index.Index; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.function.Predicate; +import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; +import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg; + public class SnapshotIndexShardStatusTests extends AbstractXContentTestCase { @Override @@ -48,7 +55,7 @@ protected Predicate getRandomFieldsExcludeFilter() { protected SnapshotIndexShardStatus doParseInstance(XContentParser parser) throws IOException { XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser); XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.nextToken(), parser); - SnapshotIndexShardStatus status = SnapshotIndexShardStatus.fromXContent(parser, parser.currentName()); + SnapshotIndexShardStatus status = SnapshotIndexShardStatusTests.fromXContent(parser, parser.currentName()); XContentParserUtils.ensureExpectedToken(XContentParser.Token.END_OBJECT, parser.nextToken(), parser); return status; } @@ -57,4 +64,58 @@ protected SnapshotIndexShardStatus doParseInstance(XContentParser parser) throws protected boolean supportsUnknownFields() { return true; } + + static final ObjectParser.NamedObjectParser PARSER; + + static { + ConstructingObjectParser innerParser = new ConstructingObjectParser<>( + "snapshot_index_shard_status", + true, + (Object[] parsedObjects, ShardId shard) -> { + int i = 0; + String rawStage = (String) parsedObjects[i++]; + String nodeId = (String) parsedObjects[i++]; + String failure = (String) parsedObjects[i++]; + SnapshotStats stats = (SnapshotStats) parsedObjects[i]; + + SnapshotIndexShardStage stage; + try { + stage = SnapshotIndexShardStage.valueOf(rawStage); + } catch (IllegalArgumentException iae) { + throw new ElasticsearchParseException( + "failed to parse snapshot index shard status [{}][{}], unknown stage [{}]", + shard.getIndex().getName(), + shard.getId(), + rawStage + ); + } + return new SnapshotIndexShardStatus(shard, stage, stats, nodeId, failure); + } + ); + innerParser.declareString(constructorArg(), new ParseField(SnapshotIndexShardStatus.Fields.STAGE)); + innerParser.declareString(optionalConstructorArg(), new ParseField(SnapshotIndexShardStatus.Fields.NODE)); + innerParser.declareString(optionalConstructorArg(), new ParseField(SnapshotIndexShardStatus.Fields.REASON)); + innerParser.declareObject(constructorArg(), (p, c) -> SnapshotStats.fromXContent(p), new ParseField(SnapshotStats.Fields.STATS)); + PARSER = (p, indexId, shardName) -> { + // Combine the index name in the context with the shard name passed in for the named object parser + // into a ShardId to pass as context for the inner parser. + int shard; + try { + shard = Integer.parseInt(shardName); + } catch (NumberFormatException nfe) { + throw new ElasticsearchParseException( + "failed to parse snapshot index shard status [{}], expected numeric shard id but got [{}]", + indexId, + shardName + ); + } + ShardId shardId = new ShardId(new Index(indexId, IndexMetadata.INDEX_UUID_NA_VALUE), shard); + return innerParser.parse(p, shardId); + }; + } + + public static SnapshotIndexShardStatus fromXContent(XContentParser parser, String indexId) throws IOException { + XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.currentToken(), parser); + return PARSER.parse(parser, indexId, parser.currentName()); + } } diff --git a/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotIndexStatusTests.java b/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotIndexStatusTests.java index 403d948409bf2..27e47b9a961e7 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotIndexStatusTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotIndexStatusTests.java @@ -60,7 +60,7 @@ public class SnapshotIndexStatusTests extends AbstractXContentTestCase SnapshotStats.fromXContent(p), new ParseField(SnapshotStats.Fields.STATS)); innerParser.declareNamedObjects( constructorArg(), - SnapshotIndexShardStatus.PARSER, + SnapshotIndexShardStatusTests.PARSER, new ParseField(SnapshotIndexStatus.Fields.SHARDS) ); PARSER = ((p, c, name) -> innerParser.apply(p, name));