summaryrefslogtreecommitdiff
path: root/core/src/main/java/org/elasticsearch/snapshots/SnapshotShardFailure.java
blob: 7facd49088f13a3c218f95f003dd982b9177e51b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.snapshots;

import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.action.ShardOperationFailedException;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.index.snapshots.IndexShardSnapshotFailedException;
import org.elasticsearch.rest.RestStatus;

import java.io.IOException;

/**
 * Stores information about failures that occurred during shard snapshotting process
 */
public class SnapshotShardFailure implements ShardOperationFailedException {
    private ShardId shardId;

    private String reason;

    @Nullable
    private String nodeId;

    private RestStatus status;

    private SnapshotShardFailure() {

    }

    /**
     * Constructs new snapshot shard failure object
     *
     * @param nodeId  node where failure occurred
     * @param shardId shard id
     * @param reason  failure reason
     */
    public SnapshotShardFailure(@Nullable String nodeId, ShardId shardId, String reason) {
        this.nodeId = nodeId;
        this.shardId = shardId;
        this.reason = reason;
        status = RestStatus.INTERNAL_SERVER_ERROR;
    }

    /**
     * Returns index where failure occurred
     *
     * @return index
     */
    @Override
    public String index() {
        return this.shardId.getIndexName();
    }

    /**
     * Returns shard id where failure occurred
     *
     * @return shard id
     */
    @Override
    public int shardId() {
        return this.shardId.id();
    }

    /**
     * Returns reason for the failure
     *
     * @return reason for the failure
     */
    @Override
    public String reason() {
        return this.reason;
    }

    /**
     * Returns REST status corresponding to this failure
     *
     * @return REST status
     */
    @Override
    public RestStatus status() {
        return status;
    }

    @Override
    public Throwable getCause() {
        return new IndexShardSnapshotFailedException(shardId, reason);
    }

    /**
     * Returns node id where failure occurred
     *
     * @return node id
     */
    @Nullable
    public String nodeId() {
        return nodeId;
    }

    /**
     * Reads shard failure information from stream input
     *
     * @param in stream input
     * @return shard failure information
     */
    public static SnapshotShardFailure readSnapshotShardFailure(StreamInput in) throws IOException {
        SnapshotShardFailure exp = new SnapshotShardFailure();
        exp.readFrom(in);
        return exp;
    }

    @Override
    public void readFrom(StreamInput in) throws IOException {
        nodeId = in.readOptionalString();
        shardId = ShardId.readShardId(in);
        reason = in.readString();
        status = RestStatus.readFrom(in);
    }

    @Override
    public void writeTo(StreamOutput out) throws IOException {
        out.writeOptionalString(nodeId);
        shardId.writeTo(out);
        out.writeString(reason);
        RestStatus.writeTo(out, status);
    }

    @Override
    public String toString() {
        return shardId + " failed, reason [" + reason + "]";
    }

    /**
     * Serializes snapshot failure information into JSON
     *
     * @param snapshotShardFailure snapshot failure information
     * @param builder              XContent builder
     * @param params               additional parameters
     */
    public static void toXContent(SnapshotShardFailure snapshotShardFailure, XContentBuilder builder, ToXContent.Params params) throws IOException {
        builder.startObject();
        snapshotShardFailure.toXContent(builder, params);
        builder.endObject();
    }

    /**
     * Deserializes snapshot failure information from JSON
     *
     * @param parser JSON parser
     * @return snapshot failure information
     */
    public static SnapshotShardFailure fromXContent(XContentParser parser) throws IOException {
        SnapshotShardFailure snapshotShardFailure = new SnapshotShardFailure();

        XContentParser.Token token = parser.currentToken();
        String index = null;
        String index_uuid = IndexMetaData.INDEX_UUID_NA_VALUE;
        int shardId = -1;
        if (token == XContentParser.Token.START_OBJECT) {
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    String currentFieldName = parser.currentName();
                    token = parser.nextToken();
                    if (token.isValue()) {
                        if ("index".equals(currentFieldName)) {
                            index = parser.text();
                        } else if ("index_uuid".equals(currentFieldName)) {
                            index_uuid = parser.text();
                        } else if ("node_id".equals(currentFieldName)) {
                            snapshotShardFailure.nodeId = parser.text();
                        } else if ("reason".equals(currentFieldName)) {
                            snapshotShardFailure.reason = parser.text();
                        } else if ("shard_id".equals(currentFieldName)) {
                            shardId = parser.intValue();
                        } else if ("status".equals(currentFieldName)) {
                            snapshotShardFailure.status = RestStatus.valueOf(parser.text());
                        } else {
                            throw new ElasticsearchParseException("unknown parameter [{}]", currentFieldName);
                        }
                    }
                } else {
                    throw new ElasticsearchParseException("unexpected token [{}]", token);
                }
            }
        } else {
            throw new ElasticsearchParseException("unexpected token [{}]", token);
        }
        if (index == null) {
            throw new ElasticsearchParseException("index name was not set");
        }
        if (shardId == -1) {
            throw new ElasticsearchParseException("index shard was not set");
        }
        snapshotShardFailure.shardId = new ShardId(index, index_uuid, shardId);
        return snapshotShardFailure;
    }

    @Override
    public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
        builder.field("index", shardId.getIndexName());
        builder.field("index_uuid", shardId.getIndexName());
        builder.field("shard_id", shardId.id());
        builder.field("reason", reason);
        if (nodeId != null) {
            builder.field("node_id", nodeId);
        }
        builder.field("status", status.name());
        return builder;
    }
}