|
22 | 22 | import org.elasticsearch.ElasticsearchParseException; |
23 | 23 | import org.elasticsearch.action.support.broadcast.BroadcastShardResponse; |
24 | 24 | import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 25 | +import org.elasticsearch.common.ParseField; |
25 | 26 | import org.elasticsearch.common.io.stream.StreamInput; |
26 | 27 | import org.elasticsearch.common.io.stream.StreamOutput; |
| 28 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 29 | +import org.elasticsearch.common.xcontent.ObjectParser; |
27 | 30 | import org.elasticsearch.common.xcontent.ToXContentFragment; |
28 | 31 | import org.elasticsearch.common.xcontent.XContentBuilder; |
29 | 32 | import org.elasticsearch.common.xcontent.XContentParser; |
|
34 | 37 |
|
35 | 38 | import java.io.IOException; |
36 | 39 |
|
| 40 | +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; |
| 41 | +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg; |
| 42 | + |
37 | 43 | public class SnapshotIndexShardStatus extends BroadcastShardResponse implements ToXContentFragment { |
38 | 44 |
|
39 | 45 | private SnapshotIndexShardStage stage = SnapshotIndexShardStage.INIT; |
@@ -167,57 +173,50 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws |
167 | 173 | return builder; |
168 | 174 | } |
169 | 175 |
|
170 | | - public static SnapshotIndexShardStatus fromXContent(XContentParser parser, String indexId) throws IOException { |
171 | | - XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.currentToken(), parser::getTokenLocation); |
172 | | - String shardName = parser.currentName(); |
173 | | - int shard; |
174 | | - try { |
175 | | - shard = Integer.parseInt(shardName); |
176 | | - } catch (NumberFormatException nfe) { |
177 | | - throw new ElasticsearchParseException( |
178 | | - "failed to parse snapshot index shard status [{}], expected numeric shard id but got [{}]", indexId, shardName); |
179 | | - } |
180 | | - ShardId shardId = new ShardId(new Index(indexId, IndexMetaData.INDEX_UUID_NA_VALUE), shard); |
181 | | - XContentParser.Token token = parser.nextToken(); |
182 | | - XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser::getTokenLocation); |
183 | | - SnapshotIndexShardStage stage = null; |
184 | | - String nodeId = null; |
185 | | - String failure = null; |
186 | | - SnapshotStats stats = null; |
187 | | - while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { |
188 | | - if (token.equals(XContentParser.Token.FIELD_NAME)) { |
189 | | - String currentName = parser.currentName(); |
190 | | - if (currentName.equals(Fields.STAGE)) { |
191 | | - XContentParserUtils.ensureExpectedToken(XContentParser.Token.VALUE_STRING, parser.nextToken(), |
192 | | - parser::getTokenLocation); |
193 | | - try { |
194 | | - stage = SnapshotIndexShardStage.valueOf(parser.text()); |
195 | | - } catch (IllegalArgumentException iae) { |
196 | | - throw new ElasticsearchParseException( |
197 | | - "failed to parse snapshot index shard status [{}][{}], unknonwn stage [{}]", indexId, shardId.getId(), |
198 | | - parser.text()); |
199 | | - } |
200 | | - } else if (currentName.equals(Fields.NODE)) { |
201 | | - XContentParserUtils.ensureExpectedToken(XContentParser.Token.VALUE_STRING, parser.nextToken(), |
202 | | - parser::getTokenLocation); |
203 | | - nodeId = parser.text(); |
204 | | - } else if (currentName.equals(Fields.REASON)) { |
205 | | - XContentParserUtils.ensureExpectedToken(XContentParser.Token.VALUE_STRING, parser.nextToken(), |
206 | | - parser::getTokenLocation); |
207 | | - failure = parser.text(); |
208 | | - } else if (currentName.equals(SnapshotStats.Fields.STATS)) { |
209 | | - stats = SnapshotStats.fromXContent(parser); |
210 | | - } else { |
| 176 | + static final ObjectParser.NamedObjectParser<SnapshotIndexShardStatus, String> PARSER; |
| 177 | + static { |
| 178 | + ConstructingObjectParser<SnapshotIndexShardStatus, ShardId> innerParser = new ConstructingObjectParser<>( |
| 179 | + "snapshot_index_shard_status", false, |
| 180 | + (Object[] parsedObjects, ShardId shard) -> { |
| 181 | + int i = 0; |
| 182 | + String rawStage = (String) parsedObjects[i++]; |
| 183 | + String nodeId = (String) parsedObjects[i++]; |
| 184 | + String failure = (String) parsedObjects[i++]; |
| 185 | + SnapshotStats stats = (SnapshotStats) parsedObjects[i]; |
| 186 | + |
| 187 | + SnapshotIndexShardStage stage; |
| 188 | + try { |
| 189 | + stage = SnapshotIndexShardStage.valueOf(rawStage); |
| 190 | + } catch (IllegalArgumentException iae) { |
211 | 191 | throw new ElasticsearchParseException( |
212 | | - "failed to parse snapshot index shard status [{}][{}], unknown field [{}]", indexId, shardId.getId(), |
213 | | - currentName); |
| 192 | + "failed to parse snapshot index shard status [{}][{}], unknonwn stage [{}]", |
| 193 | + shard.getIndex().getName(), shard.getId(), rawStage); |
214 | 194 | } |
215 | | - } else { |
| 195 | + return new SnapshotIndexShardStatus(shard, stage, stats, nodeId, failure); |
| 196 | + } |
| 197 | + ); |
| 198 | + innerParser.declareString(constructorArg(), new ParseField(Fields.STAGE)); |
| 199 | + innerParser.declareString(optionalConstructorArg(), new ParseField(Fields.NODE)); |
| 200 | + innerParser.declareString(optionalConstructorArg(), new ParseField(Fields.REASON)); |
| 201 | + innerParser.declareObject(constructorArg(), (p, c) -> SnapshotStats.fromXContent(p), new ParseField(SnapshotStats.Fields.STATS)); |
| 202 | + PARSER = (p, indexId, shardName) -> { |
| 203 | + // Combine the index name in the context with the shard name passed in for the named object parser |
| 204 | + // into a ShardId to pass as context for the inner parser. |
| 205 | + int shard; |
| 206 | + try { |
| 207 | + shard = Integer.parseInt(shardName); |
| 208 | + } catch (NumberFormatException nfe) { |
216 | 209 | throw new ElasticsearchParseException( |
217 | | - "failed to parse snapshot index shard status [{}][{}]", indexId, shardId.getId()); |
| 210 | + "failed to parse snapshot index shard status [{}], expected numeric shard id but got [{}]", indexId, shardName); |
218 | 211 | } |
219 | | - } |
220 | | - return new SnapshotIndexShardStatus(shardId, stage, stats, nodeId, failure); |
| 212 | + ShardId shardId = new ShardId(new Index(indexId, IndexMetaData.INDEX_UUID_NA_VALUE), shard); |
| 213 | + return innerParser.parse(p, shardId); |
| 214 | + }; |
| 215 | + } |
| 216 | + |
| 217 | + public static SnapshotIndexShardStatus fromXContent(XContentParser parser, String indexId) throws IOException { |
| 218 | + XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.currentToken(), parser::getTokenLocation); |
| 219 | + return PARSER.parse(parser, indexId, parser.currentName()); |
221 | 220 | } |
222 | 221 |
|
223 | 222 | @Override |
|
0 commit comments