/
IndexAbstraction.java
477 lines (406 loc) · 16.3 KB
/
IndexAbstraction.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
* 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.metadata;
import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.cluster.metadata.DataStream.TimestampField;
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.time.DateFormatters;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.IndexMode;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.mapper.DateFieldMapper;
import org.elasticsearch.xcontent.XContent;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentParserConfiguration;
import org.elasticsearch.xcontent.XContentType;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
/**
* An index abstraction is a reference to one or more concrete indices.
* An index abstraction has a unique name and encapsulates all the {@link Index} instances it is pointing to.
* Also depending on type it may refer to a single or many concrete indices and may or may not have a write index.
*/
public interface IndexAbstraction {
/**
* @return the type of the index abstraction
*/
Type getType();
/**
* @return the name of the index abstraction
*/
String getName();
/**
* @return All {@link Index} of all concrete indices this index abstraction is referring to.
*/
List<Index> getIndices();
/**
* A write index is a dedicated concrete index, that accepts all the new documents that belong to an index abstraction.
* <p>
* A write index may also be a regular concrete index of a index abstraction and may therefore also be returned
* by {@link #getIndices()}. An index abstraction may also not have a dedicated write index.
*
* @return the write index of this index abstraction or
* <code>null</code> if this index abstraction doesn't have a write index.
*/
@Nullable
Index getWriteIndex();
default Index getWriteIndex(IndexRequest request, Metadata metadata) {
return getWriteIndex();
}
/**
* @return the data stream to which this index belongs or <code>null</code> if this is not a concrete index or
* if it is a concrete index that does not belong to a data stream.
*/
@Nullable
DataStream getParentDataStream();
/**
* @return whether this index abstraction is hidden or not
*/
boolean isHidden();
/**
* @return whether this index abstraction should be treated as a system index or not
*/
boolean isSystem();
/**
* @return whether this index abstraction is related to data streams
*/
default boolean isDataStreamRelated() {
return false;
}
/**
* An index abstraction type.
*/
enum Type {
/**
* An index abstraction that refers to a single concrete index.
* This concrete index is also the write index.
*/
CONCRETE_INDEX("concrete index"),
/**
* An index abstraction that refers to an alias.
* An alias typically refers to many concrete indices and
* may have a write index.
*/
ALIAS("alias"),
/**
* An index abstraction that refers to a data stream.
* A data stream typically has multiple backing indices, the latest of which
* is the target for index requests.
*/
DATA_STREAM("data_stream");
private final String displayName;
Type(String displayName) {
this.displayName = displayName;
}
public String getDisplayName() {
return displayName;
}
}
/**
* Represents an concrete index and encapsulates its {@link IndexMetadata}
*/
class ConcreteIndex implements IndexAbstraction {
private final Index concreteIndex;
private final boolean isHidden;
private final boolean isSystem;
private final DataStream dataStream;
public ConcreteIndex(IndexMetadata indexMetadata, DataStream dataStream) {
// note: don't capture a reference to the indexMetadata here
this.concreteIndex = indexMetadata.getIndex();
this.isHidden = indexMetadata.isHidden();
this.isSystem = indexMetadata.isSystem();
this.dataStream = dataStream;
}
public ConcreteIndex(IndexMetadata indexMetadata) {
this(indexMetadata, null);
}
@Override
public String getName() {
return concreteIndex.getName();
}
@Override
public Type getType() {
return Type.CONCRETE_INDEX;
}
@Override
public List<Index> getIndices() {
return List.of(concreteIndex);
}
@Override
public Index getWriteIndex() {
return concreteIndex;
}
@Override
public DataStream getParentDataStream() {
return dataStream;
}
@Override
public boolean isHidden() {
return isHidden;
}
@Override
public boolean isSystem() {
return isSystem;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ConcreteIndex that = (ConcreteIndex) o;
return isHidden == that.isHidden
&& isSystem == that.isSystem
&& concreteIndex.equals(that.concreteIndex)
&& Objects.equals(dataStream, that.dataStream);
}
@Override
public int hashCode() {
return Objects.hash(concreteIndex, isHidden, isSystem, dataStream);
}
}
/**
* Represents an alias and groups all {@link IndexMetadata} instances sharing the same alias name together.
*/
class Alias implements IndexAbstraction {
private final String aliasName;
private final List<Index> referenceIndices;
private final Index writeIndex;
private final boolean isHidden;
private final boolean isSystem;
private final boolean dataStreamAlias;
public Alias(AliasMetadata aliasMetadata, List<IndexMetadata> indexMetadatas) {
// note: don't capture a reference to any of these indexMetadatas here
this.aliasName = aliasMetadata.getAlias();
this.referenceIndices = new ArrayList<>(indexMetadatas.size());
boolean isSystem = true;
Index widx = null;
for (IndexMetadata imd : indexMetadatas) {
this.referenceIndices.add(imd.getIndex());
if (Boolean.TRUE.equals(imd.getAliases().get(aliasName).writeIndex())) {
if (widx != null) {
throw new IllegalStateException("write indices size can only be 0 or 1, but is at least 2");
}
widx = imd.getIndex();
}
isSystem = isSystem && imd.isSystem();
}
this.referenceIndices.sort(Index.COMPARE_BY_NAME);
if (widx == null && indexMetadatas.size() == 1 && indexMetadatas.get(0).getAliases().get(aliasName).writeIndex() == null) {
widx = indexMetadatas.get(0).getIndex();
}
this.writeIndex = widx;
this.isHidden = aliasMetadata.isHidden() == null ? false : aliasMetadata.isHidden();
this.isSystem = isSystem;
dataStreamAlias = false;
}
public Alias(DataStreamAlias dataStreamAlias, List<Index> indicesOfAllDataStreams, Index writeIndexOfWriteDataStream) {
this.aliasName = dataStreamAlias.getName();
this.referenceIndices = indicesOfAllDataStreams;
this.writeIndex = writeIndexOfWriteDataStream;
this.isHidden = false;
this.isSystem = false;
this.dataStreamAlias = true;
}
@Override
public Type getType() {
return Type.ALIAS;
}
public String getName() {
return aliasName;
}
@Override
public List<Index> getIndices() {
return referenceIndices;
}
@Nullable
public Index getWriteIndex() {
return writeIndex;
}
@Override
public DataStream getParentDataStream() {
// aliases may not be part of a data stream
return null;
}
@Override
public boolean isHidden() {
return isHidden;
}
@Override
public boolean isSystem() {
return isSystem;
}
@Override
public boolean isDataStreamRelated() {
return dataStreamAlias;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Alias alias = (Alias) o;
return isHidden == alias.isHidden
&& isSystem == alias.isSystem
&& dataStreamAlias == alias.dataStreamAlias
&& aliasName.equals(alias.aliasName)
&& referenceIndices.equals(alias.referenceIndices)
&& Objects.equals(writeIndex, alias.writeIndex);
}
@Override
public int hashCode() {
return Objects.hash(aliasName, referenceIndices, writeIndex, isHidden, isSystem, dataStreamAlias);
}
}
class DataStream implements IndexAbstraction {
public static final XContentParserConfiguration TS_EXTRACT_CONFIG = XContentParserConfiguration.EMPTY.withFiltering(
Set.of(TimestampField.FIXED_TIMESTAMP_FIELD),
null,
false
);
public static final DateFormatter TIMESTAMP_FORMATTER = DateFormatter.forPattern(
"strict_date_optional_time_nanos||strict_date_optional_time||epoch_millis"
);
private final org.elasticsearch.cluster.metadata.DataStream dataStream;
public DataStream(org.elasticsearch.cluster.metadata.DataStream dataStream) {
this.dataStream = dataStream;
}
@Override
public String getName() {
return dataStream.getName();
}
@Override
public Type getType() {
return Type.DATA_STREAM;
}
@Override
public List<Index> getIndices() {
return dataStream.getIndices();
}
public Index getWriteIndex() {
return dataStream.getWriteIndex();
}
@Override
public Index getWriteIndex(IndexRequest request, Metadata metadata) {
if (request.opType() != DocWriteRequest.OpType.CREATE) {
return getWriteIndex();
}
if (dataStream.getIndexMode() != IndexMode.TIME_SERIES) {
return getWriteIndex();
}
Instant timestamp;
Object rawTimestamp = request.getRawTimestamp();
if (rawTimestamp != null) {
timestamp = getTimeStampFromRaw(rawTimestamp);
} else {
timestamp = getTimestampFromParser(request.source(), request.getContentType());
}
timestamp = timestamp.truncatedTo(ChronoUnit.SECONDS);
Index result = dataStream.selectTimeSeriesWriteIndex(timestamp, metadata);
if (result == null) {
String timestampAsString = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.format(timestamp);
String writeableIndicesString = dataStream.getIndices()
.stream()
.map(metadata::index)
.map(IndexMetadata::getSettings)
.map(
settings -> "["
+ settings.get(IndexSettings.TIME_SERIES_START_TIME.getKey())
+ ","
+ settings.get(IndexSettings.TIME_SERIES_END_TIME.getKey())
+ "]"
)
.collect(Collectors.joining());
throw new IllegalArgumentException(
"the document timestamp ["
+ timestampAsString
+ "] is outside of ranges of currently writable indices ["
+ writeableIndicesString
+ "]"
);
}
return result;
}
static Instant getTimeStampFromRaw(Object rawTimestamp) {
try {
if (rawTimestamp instanceof Long lTimestamp) {
return Instant.ofEpochMilli(lTimestamp);
} else if (rawTimestamp instanceof String sTimestamp) {
return DateFormatters.from(TIMESTAMP_FORMATTER.parse(sTimestamp), TIMESTAMP_FORMATTER.locale()).toInstant();
} else {
throw new IllegalArgumentException("timestamp [" + rawTimestamp + "] type [" + rawTimestamp.getClass() + "] error");
}
} catch (Exception e) {
throw new IllegalArgumentException("Error get data stream timestamp field: " + e.getMessage(), e);
}
}
static Instant getTimestampFromParser(BytesReference source, XContentType xContentType) {
XContent xContent = xContentType.xContent();
try (XContentParser parser = xContent.createParser(TS_EXTRACT_CONFIG, source.streamInput())) {
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.nextToken(), parser);
return switch (parser.nextToken()) {
case VALUE_STRING -> DateFormatters.from(TIMESTAMP_FORMATTER.parse(parser.text()), TIMESTAMP_FORMATTER.locale())
.toInstant();
case VALUE_NUMBER -> Instant.ofEpochMilli(parser.longValue());
default -> throw new ParsingException(
parser.getTokenLocation(),
String.format(
Locale.ROOT,
"Failed to parse object: expecting token of type [%s] or [%s] but found [%s]",
XContentParser.Token.VALUE_STRING,
XContentParser.Token.VALUE_NUMBER,
parser.currentToken()
)
);
};
} catch (Exception e) {
throw new IllegalArgumentException("Error extracting data stream timestamp field: " + e.getMessage(), e);
}
}
@Override
public DataStream getParentDataStream() {
// a data stream cannot have a parent data stream
return null;
}
@Override
public boolean isHidden() {
return dataStream.isHidden();
}
@Override
public boolean isSystem() {
return dataStream.isSystem();
}
@Override
public boolean isDataStreamRelated() {
return true;
}
public org.elasticsearch.cluster.metadata.DataStream getDataStream() {
return dataStream;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DataStream that = (DataStream) o;
return dataStream.equals(that.dataStream);
}
@Override
public int hashCode() {
return Objects.hash(dataStream);
}
}
}