99
1010package org .elasticsearch .action .admin .cluster .snapshots .status ;
1111
12+ import org .elasticsearch .ElasticsearchParseException ;
1213import org .elasticsearch .cluster .metadata .IndexMetadata ;
1314import org .elasticsearch .common .xcontent .XContentParserUtils ;
1415import org .elasticsearch .index .Index ;
1516import org .elasticsearch .index .shard .ShardId ;
1617import org .elasticsearch .test .AbstractXContentTestCase ;
18+ import org .elasticsearch .xcontent .ConstructingObjectParser ;
19+ import org .elasticsearch .xcontent .ObjectParser ;
20+ import org .elasticsearch .xcontent .ParseField ;
1721import org .elasticsearch .xcontent .XContentParser ;
1822
1923import java .io .IOException ;
2024import java .util .function .Predicate ;
2125
26+ import static org .elasticsearch .xcontent .ConstructingObjectParser .constructorArg ;
27+ import static org .elasticsearch .xcontent .ConstructingObjectParser .optionalConstructorArg ;
28+
2229public class SnapshotIndexShardStatusTests extends AbstractXContentTestCase <SnapshotIndexShardStatus > {
2330
2431 @ Override
@@ -48,7 +55,7 @@ protected Predicate<String> getRandomFieldsExcludeFilter() {
4855 protected SnapshotIndexShardStatus doParseInstance (XContentParser parser ) throws IOException {
4956 XContentParserUtils .ensureExpectedToken (XContentParser .Token .START_OBJECT , parser .nextToken (), parser );
5057 XContentParserUtils .ensureExpectedToken (XContentParser .Token .FIELD_NAME , parser .nextToken (), parser );
51- SnapshotIndexShardStatus status = SnapshotIndexShardStatus .fromXContent (parser , parser .currentName ());
58+ SnapshotIndexShardStatus status = SnapshotIndexShardStatusTests .fromXContent (parser , parser .currentName ());
5259 XContentParserUtils .ensureExpectedToken (XContentParser .Token .END_OBJECT , parser .nextToken (), parser );
5360 return status ;
5461 }
@@ -57,4 +64,58 @@ protected SnapshotIndexShardStatus doParseInstance(XContentParser parser) throws
5764 protected boolean supportsUnknownFields () {
5865 return true ;
5966 }
67+
68+ static final ObjectParser .NamedObjectParser <SnapshotIndexShardStatus , String > PARSER ;
69+
70+ static {
71+ ConstructingObjectParser <SnapshotIndexShardStatus , ShardId > innerParser = new ConstructingObjectParser <>(
72+ "snapshot_index_shard_status" ,
73+ true ,
74+ (Object [] parsedObjects , ShardId shard ) -> {
75+ int i = 0 ;
76+ String rawStage = (String ) parsedObjects [i ++];
77+ String nodeId = (String ) parsedObjects [i ++];
78+ String failure = (String ) parsedObjects [i ++];
79+ SnapshotStats stats = (SnapshotStats ) parsedObjects [i ];
80+
81+ SnapshotIndexShardStage stage ;
82+ try {
83+ stage = SnapshotIndexShardStage .valueOf (rawStage );
84+ } catch (IllegalArgumentException iae ) {
85+ throw new ElasticsearchParseException (
86+ "failed to parse snapshot index shard status [{}][{}], unknown stage [{}]" ,
87+ shard .getIndex ().getName (),
88+ shard .getId (),
89+ rawStage
90+ );
91+ }
92+ return new SnapshotIndexShardStatus (shard , stage , stats , nodeId , failure );
93+ }
94+ );
95+ innerParser .declareString (constructorArg (), new ParseField (SnapshotIndexShardStatus .Fields .STAGE ));
96+ innerParser .declareString (optionalConstructorArg (), new ParseField (SnapshotIndexShardStatus .Fields .NODE ));
97+ innerParser .declareString (optionalConstructorArg (), new ParseField (SnapshotIndexShardStatus .Fields .REASON ));
98+ innerParser .declareObject (constructorArg (), (p , c ) -> SnapshotStats .fromXContent (p ), new ParseField (SnapshotStats .Fields .STATS ));
99+ PARSER = (p , indexId , shardName ) -> {
100+ // Combine the index name in the context with the shard name passed in for the named object parser
101+ // into a ShardId to pass as context for the inner parser.
102+ int shard ;
103+ try {
104+ shard = Integer .parseInt (shardName );
105+ } catch (NumberFormatException nfe ) {
106+ throw new ElasticsearchParseException (
107+ "failed to parse snapshot index shard status [{}], expected numeric shard id but got [{}]" ,
108+ indexId ,
109+ shardName
110+ );
111+ }
112+ ShardId shardId = new ShardId (new Index (indexId , IndexMetadata .INDEX_UUID_NA_VALUE ), shard );
113+ return innerParser .parse (p , shardId );
114+ };
115+ }
116+
117+ public static SnapshotIndexShardStatus fromXContent (XContentParser parser , String indexId ) throws IOException {
118+ XContentParserUtils .ensureExpectedToken (XContentParser .Token .FIELD_NAME , parser .currentToken (), parser );
119+ return PARSER .parse (parser , indexId , parser .currentName ());
120+ }
60121}
0 commit comments