-
Notifications
You must be signed in to change notification settings - Fork 24.3k
/
RecoverySource.java
321 lines (259 loc) · 9.78 KB
/
RecoverySource.java
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.cluster.routing;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.index.IndexVersion;
import org.elasticsearch.repositories.IndexId;
import org.elasticsearch.snapshots.Snapshot;
import org.elasticsearch.xcontent.ToXContent;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
import java.io.IOException;
import java.util.Objects;
/**
* Represents the recovery source of a shard. Available recovery types are:
*
* - {@link EmptyStoreRecoverySource} recovery from an empty store
* - {@link ExistingStoreRecoverySource} recovery from an existing store
* - {@link PeerRecoverySource} recovery from a primary on another node
* - {@link SnapshotRecoverySource} recovery from a snapshot
* - {@link LocalShardsRecoverySource} recovery from other shards of another index on the same node
*/
public abstract class RecoverySource implements Writeable, ToXContentObject {
@Override
public final XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
builder.startObject();
builder.field("type", getType());
addAdditionalFields(builder, params);
return builder.endObject();
}
/**
* to be overridden by subclasses
*/
public void addAdditionalFields(XContentBuilder builder, ToXContent.Params params) throws IOException {
}
public static RecoverySource readFrom(StreamInput in) throws IOException {
Type type = Type.values()[in.readByte()];
return switch (type) {
case EMPTY_STORE -> EmptyStoreRecoverySource.INSTANCE;
case EXISTING_STORE -> ExistingStoreRecoverySource.read(in);
case PEER -> PeerRecoverySource.INSTANCE;
case SNAPSHOT -> new SnapshotRecoverySource(in);
case LOCAL_SHARDS -> LocalShardsRecoverySource.INSTANCE;
};
}
@Override
public final void writeTo(StreamOutput out) throws IOException {
out.writeByte((byte) getType().ordinal());
writeAdditionalFields(out);
}
/**
* to be overridden by subclasses
*/
protected void writeAdditionalFields(StreamOutput out) throws IOException {
}
public enum Type {
EMPTY_STORE,
EXISTING_STORE,
PEER,
SNAPSHOT,
LOCAL_SHARDS
}
public abstract Type getType();
public boolean shouldBootstrapNewHistoryUUID() {
return false;
}
public boolean expectEmptyRetentionLeases() {
return true;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RecoverySource that = (RecoverySource) o;
return getType() == that.getType();
}
@Override
public int hashCode() {
return getType().hashCode();
}
/**
* Recovery from a fresh copy
*/
public static final class EmptyStoreRecoverySource extends RecoverySource {
public static final EmptyStoreRecoverySource INSTANCE = new EmptyStoreRecoverySource();
@Override
public Type getType() {
return Type.EMPTY_STORE;
}
@Override
public String toString() {
return "new shard recovery";
}
}
/**
* Recovery from an existing on-disk store
*/
public static final class ExistingStoreRecoverySource extends RecoverySource {
/**
* Special allocation id that shard has during initialization on allocate_stale_primary
*/
public static final String FORCED_ALLOCATION_ID = "_forced_allocation_";
public static final ExistingStoreRecoverySource INSTANCE = new ExistingStoreRecoverySource(false);
public static final ExistingStoreRecoverySource FORCE_STALE_PRIMARY_INSTANCE = new ExistingStoreRecoverySource(true);
private final boolean bootstrapNewHistoryUUID;
private ExistingStoreRecoverySource(boolean bootstrapNewHistoryUUID) {
this.bootstrapNewHistoryUUID = bootstrapNewHistoryUUID;
}
private static ExistingStoreRecoverySource read(StreamInput in) throws IOException {
return in.readBoolean() ? FORCE_STALE_PRIMARY_INSTANCE : INSTANCE;
}
@Override
public void addAdditionalFields(XContentBuilder builder, Params params) throws IOException {
builder.field("bootstrap_new_history_uuid", bootstrapNewHistoryUUID);
}
@Override
protected void writeAdditionalFields(StreamOutput out) throws IOException {
out.writeBoolean(bootstrapNewHistoryUUID);
}
@Override
public boolean shouldBootstrapNewHistoryUUID() {
return bootstrapNewHistoryUUID;
}
@Override
public Type getType() {
return Type.EXISTING_STORE;
}
@Override
public String toString() {
return "existing store recovery; bootstrap_history_uuid=" + bootstrapNewHistoryUUID;
}
@Override
public boolean expectEmptyRetentionLeases() {
return bootstrapNewHistoryUUID;
}
}
/**
* recovery from other shards on same node (shrink index action)
*/
public static class LocalShardsRecoverySource extends RecoverySource {
public static final LocalShardsRecoverySource INSTANCE = new LocalShardsRecoverySource();
private LocalShardsRecoverySource() {}
@Override
public Type getType() {
return Type.LOCAL_SHARDS;
}
@Override
public String toString() {
return "local shards recovery";
}
}
/**
* recovery from a snapshot
*/
public static class SnapshotRecoverySource extends RecoverySource {
public static final String NO_API_RESTORE_UUID = "_no_api_";
private final String restoreUUID;
private final Snapshot snapshot;
private final IndexId index;
private final IndexVersion version;
public SnapshotRecoverySource(String restoreUUID, Snapshot snapshot, IndexVersion version, IndexId indexId) {
this.restoreUUID = restoreUUID;
this.snapshot = Objects.requireNonNull(snapshot);
this.version = Objects.requireNonNull(version);
this.index = Objects.requireNonNull(indexId);
}
SnapshotRecoverySource(StreamInput in) throws IOException {
restoreUUID = in.readString();
snapshot = new Snapshot(in);
version = IndexVersion.readVersion(in);
index = new IndexId(in);
}
public String restoreUUID() {
return restoreUUID;
}
public Snapshot snapshot() {
return snapshot;
}
/**
* Gets the {@link IndexId} of the recovery source. May contain {@link IndexMetadata#INDEX_UUID_NA_VALUE} as the index uuid if it
* was created by an older version master in a mixed version cluster.
*
* @return IndexId
*/
public IndexId index() {
return index;
}
public IndexVersion version() {
return version;
}
@Override
protected void writeAdditionalFields(StreamOutput out) throws IOException {
out.writeString(restoreUUID);
snapshot.writeTo(out);
IndexVersion.writeVersion(version, out);
index.writeTo(out);
}
@Override
public Type getType() {
return Type.SNAPSHOT;
}
@Override
public void addAdditionalFields(XContentBuilder builder, ToXContent.Params params) throws IOException {
builder.field("repository", snapshot.getRepository())
.field("snapshot", snapshot.getSnapshotId().getName())
.field("version", version.toReleaseVersion())
.field("index", index.getName())
.field("restoreUUID", restoreUUID);
}
@Override
public String toString() {
return "snapshot recovery [" + restoreUUID + "] from " + snapshot;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
SnapshotRecoverySource that = (SnapshotRecoverySource) o;
return restoreUUID.equals(that.restoreUUID)
&& snapshot.equals(that.snapshot)
&& index.equals(that.index)
&& version.equals(that.version);
}
@Override
public int hashCode() {
return Objects.hash(restoreUUID, snapshot, index, version);
}
}
/**
* peer recovery from a primary shard
*/
public static class PeerRecoverySource extends RecoverySource {
public static final PeerRecoverySource INSTANCE = new PeerRecoverySource();
private PeerRecoverySource() {}
@Override
public Type getType() {
return Type.PEER;
}
@Override
public String toString() {
return "peer recovery";
}
@Override
public boolean expectEmptyRetentionLeases() {
return false;
}
}
}