Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -149,59 +138,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
return builder;
}

static final ObjectParser.NamedObjectParser<SnapshotIndexShardStatus, String> PARSER;
static {
ConstructingObjectParser<SnapshotIndexShardStatus, ShardId> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SnapshotIndexShardStatus> {

@Override
Expand Down Expand Up @@ -48,7 +55,7 @@ protected Predicate<String> 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;
}
Expand All @@ -57,4 +64,58 @@ protected SnapshotIndexShardStatus doParseInstance(XContentParser parser) throws
protected boolean supportsUnknownFields() {
return true;
}

static final ObjectParser.NamedObjectParser<SnapshotIndexShardStatus, String> PARSER;

static {
ConstructingObjectParser<SnapshotIndexShardStatus, ShardId> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class SnapshotIndexStatusTests extends AbstractXContentTestCase<SnapshotI
innerParser.declareObject(constructorArg(), (p, c) -> 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));
Expand Down