-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Create data stream aliases from template #73867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 12 commits
52c9402
ebdbce5
cc8c7c4
5d21f2f
738b9e6
b0848d6
36847c8
0aebdb4
49968e4
001b0d5
39973f6
991a36b
e12bd60
1399903
59e57af
0f5c6a9
43e1d5f
45cc1f5
86dc1f3
f75425b
4edd676
3b70768
f3a97c8
77e2a32
8d1e1e7
33f2359
833677a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
|
||
package org.elasticsearch.cluster.metadata; | ||
|
||
import org.elasticsearch.Version; | ||
import org.elasticsearch.cluster.AbstractDiffable; | ||
import org.elasticsearch.cluster.Diff; | ||
import org.elasticsearch.core.Nullable; | ||
|
@@ -23,6 +24,8 @@ | |
import org.elasticsearch.index.mapper.MapperService; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
@@ -256,35 +259,63 @@ public String toString() { | |
|
||
public static class DataStreamTemplate implements Writeable, ToXContentObject { | ||
|
||
public static final Version DATA_STREAM_ALIAS_VERSION = Version.V_8_0_0; | ||
|
||
private static final ParseField HIDDEN = new ParseField("hidden"); | ||
private static final ParseField ALIASES = new ParseField("aliases"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think changes to this file can be undone as well? |
||
|
||
public static final ConstructingObjectParser<DataStreamTemplate, Void> PARSER = new ConstructingObjectParser<>( | ||
"data_stream_template", | ||
false, | ||
a -> new DataStreamTemplate(a[0] != null && (boolean) a[0])); | ||
args -> { | ||
boolean hidden = args[0] != null && (boolean) args[0]; | ||
@SuppressWarnings("unchecked") | ||
Map<String, AliasMetadata> aliases = (Map<String, AliasMetadata>) args[1]; | ||
return new DataStreamTemplate(hidden, aliases); | ||
} | ||
); | ||
|
||
static { | ||
PARSER.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), HIDDEN); | ||
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> { | ||
Map<String, AliasMetadata> aliasMap = new HashMap<>(); | ||
while ((p.nextToken()) != XContentParser.Token.END_OBJECT) { | ||
AliasMetadata alias = AliasMetadata.Builder.fromXContent(p); | ||
aliasMap.put(alias.alias(), alias); | ||
} | ||
return aliasMap; | ||
}, ALIASES); | ||
} | ||
|
||
private final boolean hidden; | ||
|
||
@Nullable | ||
private final Map<String, AliasMetadata> aliases; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can reuse the alias definitions in |
||
|
||
public DataStreamTemplate() { | ||
this(false); | ||
this(false, null); | ||
} | ||
|
||
public DataStreamTemplate(boolean hidden) { | ||
public DataStreamTemplate(boolean hidden, Map<String, AliasMetadata> aliases) { | ||
this.hidden = hidden; | ||
this.aliases = aliases; | ||
} | ||
|
||
DataStreamTemplate(StreamInput in) throws IOException { | ||
hidden = in.readBoolean(); | ||
aliases = in.getVersion().onOrAfter(DATA_STREAM_ALIAS_VERSION) | ||
? in.readBoolean() ? in.readMap(StreamInput::readString, AliasMetadata::new) : null | ||
: null; | ||
} | ||
|
||
public String getTimestampField() { | ||
return FIXED_TIMESTAMP_FIELD; | ||
} | ||
|
||
public Collection<AliasMetadata> getAliases() { | ||
return aliases == null ? null : aliases.values(); | ||
} | ||
|
||
/** | ||
* @return a mapping snippet for a backing index with `_data_stream_timestamp` meta field mapper properly configured. | ||
*/ | ||
|
@@ -300,12 +331,27 @@ public boolean isHidden() { | |
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeBoolean(hidden); | ||
if (out.getVersion().onOrAfter(DATA_STREAM_ALIAS_VERSION)) { | ||
if (aliases != null) { | ||
out.writeBoolean(true); | ||
out.writeMap(aliases, StreamOutput::writeString, (innerOut, alias) -> alias.writeTo(innerOut)); | ||
} else { | ||
out.writeBoolean(false); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
builder.field("hidden", hidden); | ||
if (aliases != null) { | ||
builder.startObject("aliases"); | ||
for (var alias : aliases.values()) { | ||
alias.toXContent(builder, params); | ||
} | ||
builder.endObject(); | ||
} | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
@@ -315,12 +361,12 @@ public boolean equals(Object o) { | |
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
DataStreamTemplate that = (DataStreamTemplate) o; | ||
return hidden == that.hidden; | ||
return hidden == that.hidden && Objects.equals(aliases, that.aliases); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(hidden); | ||
return Objects.hash(hidden, aliases); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -222,6 +222,13 @@ static ClusterState createDataStream(MetadataCreateIndexService metadataCreateIn | |
logger.info("adding data stream [{}] with write index [{}] and backing indices [{}]", dataStreamName, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe enhance this log line with the fact that aliases were created as well (if template has aliases)? |
||
writeIndex.getIndex().getName(), | ||
Strings.arrayToCommaDelimitedString(backingIndices.stream().map(i -> i.getIndex().getName()).toArray())); | ||
|
||
if (template.getDataStreamTemplate().getAliases() != null) { | ||
for (var alias : template.getDataStreamTemplate().getAliases()) { | ||
builder.put(alias.getAlias(), dataStreamName, alias.writeIndex()); | ||
} | ||
} | ||
|
||
return ClusterState.builder(currentState).metadata(builder).build(); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this field is needed? I also don't see that is currently is used in this pr.
In
MetadataCreateDataStreamService
, data stream aliases can be created without this information? The fact that aDataStreamTemplate
is defined should be sufficient?