Skip to content
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

CORE: Allow Setting Index Autocreate to _template #32964

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
c509131
CORE: Allow Setting Index Autocreate to _template
original-brownbear Aug 14, 2018
7426322
revert doc change
original-brownbear Aug 17, 2018
fca252d
Merge remote-tracking branch 'elastic/master' into 20640
original-brownbear Aug 19, 2018
e4d3392
change serialization
original-brownbear Aug 19, 2018
d42bb72
set default template
original-brownbear Aug 20, 2018
cffac2c
set default template
original-brownbear Aug 20, 2018
7407c3f
Merge remote-tracking branch 'elastic/master' into 20640
original-brownbear Aug 20, 2018
6fda7f0
Merge remote-tracking branch 'elastic/master' into 20640
original-brownbear Aug 20, 2018
c4ce75a
fix tests
original-brownbear Aug 20, 2018
e4ffb87
fix another test
original-brownbear Aug 20, 2018
4cd80c3
Merge remote-tracking branch 'elastic/master' into 20640
original-brownbear Aug 20, 2018
d88b87f
Merge remote-tracking branch 'elastic/master' into 20640
original-brownbear Aug 20, 2018
5d76ab0
Merge remote-tracking branch 'elastic/master' into 20640
original-brownbear Aug 20, 2018
5091085
fix test
original-brownbear Aug 20, 2018
09131e8
Merge remote-tracking branch 'elastic/master' into 20640
original-brownbear Aug 21, 2018
05aabbf
Merge remote-tracking branch 'elastic/master' into 20640
original-brownbear Aug 21, 2018
fbd9efd
Merge remote-tracking branch 'elastic/master' into 20640
original-brownbear Aug 21, 2018
c350dbf
Merge remote-tracking branch 'elastic/master' into 20640
original-brownbear Aug 21, 2018
881e86b
Merge remote-tracking branch 'elastic/master' into 20640
original-brownbear Sep 3, 2018
453a0b5
merge in master
original-brownbear Sep 3, 2018
76bcfb2
Merge remote-tracking branch 'elastic/master' into 20640
original-brownbear Sep 4, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,9 @@ private void checkSnapshot(String snapshotName, int count, Version tookOnVersion
Map<String, Object> aliases = new HashMap<>();
aliases.put("alias1", emptyMap());
aliases.put("alias2", singletonMap("filter", singletonMap("term", singletonMap("version", tookOnVersion.toString()))));
if (runningAgainstOldCluster == false) {
expectedTemplate.put("auto_create_index", false);
}
expectedTemplate.put("aliases", aliases);
expectedTemplate = singletonMap("test_template", expectedTemplate);
if (false == expectedTemplate.equals(getTemplateResponse)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.elasticsearch.action.admin.indices.template.put;

import java.util.ArrayList;
import org.elasticsearch.ElasticsearchGenerationException;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.Version;
Expand Down Expand Up @@ -75,6 +76,8 @@ public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateR

private String cause = "";

private boolean autoCreateIndex;

private List<String> indexPatterns;

private int order;
Expand Down Expand Up @@ -135,6 +138,15 @@ public List<String> patterns() {
return this.indexPatterns;
}

public PutIndexTemplateRequest autoCreateIndex(boolean autoCreateIndex) {
this.autoCreateIndex = autoCreateIndex;
return this;
}

public boolean autoCreateIndex() {
return autoCreateIndex;
}

public PutIndexTemplateRequest order(int order) {
this.order = order;
return this;
Expand Down Expand Up @@ -325,6 +337,8 @@ public PutIndexTemplateRequest source(Map<String, Object> templateSource) {
} else {
throw new IllegalArgumentException("Malformed [template] value, should be a string or a list of strings");
}
} else if (name.equals("auto_create_index")) {
autoCreateIndex(XContentMapValues.nodeBooleanValue(entry.getValue(), false));
} else if (name.equals("order")) {
order(XContentMapValues.nodeIntegerValue(entry.getValue(), order()));
} else if ("version".equals(name)) {
Expand Down Expand Up @@ -461,7 +475,11 @@ public void readFrom(StreamInput in) throws IOException {
name = in.readString();

if (in.getVersion().onOrAfter(Version.V_6_0_0_alpha1)) {
indexPatterns = in.readList(StreamInput::readString);
List<String> patterns = in.readList(StreamInput::readString);
if (in.getVersion().onOrAfter(Version.V_7_0_0_alpha1)) {
autoCreateIndex = Boolean.parseBoolean(patterns.remove(patterns.size() - 1));
}
indexPatterns = patterns;
} else {
indexPatterns = Collections.singletonList(in.readString());
}
Expand Down Expand Up @@ -495,7 +513,13 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeString(cause);
out.writeString(name);
if (out.getVersion().onOrAfter(Version.V_6_0_0_alpha1)) {
out.writeStringList(indexPatterns);
if (out.getVersion().onOrAfter(Version.V_7_0_0_alpha1)) {
List<String> patternsAndAutoCreate = new ArrayList<>(indexPatterns);
patternsAndAutoCreate.add(Boolean.toString(autoCreateIndex));
out.writeStringList(patternsAndAutoCreate);
} else {
out.writeStringList(indexPatterns);
}
} else {
out.writeString(indexPatterns.size() > 0 ? indexPatterns.get(0) : "");
}
Expand All @@ -520,6 +544,7 @@ public void writeTo(StreamOutput out) throws IOException {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.field("index_patterns", indexPatterns);
builder.field("auto_create_index", autoCreateIndex);
builder.field("order", order);
if (version != null) {
builder.field("version", version);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public PutIndexTemplateRequestBuilder setPatterns(List<String> indexPatterns) {
return this;
}

public PutIndexTemplateRequestBuilder setAutoCreateIndex(boolean autoCreateIndex) {
request.autoCreateIndex(autoCreateIndex);
return this;
}

/**
* Sets the order of this template if more than one template matches.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ protected void masterOperation(final PutIndexTemplateRequest request, final Clus
indexScopedSettings.validate(templateSettingsBuilder.build(), true); // templates must be consistent with regards to dependencies
indexTemplateService.putTemplate(new MetaDataIndexTemplateService.PutRequest(cause, request.name())
.patterns(request.patterns())
.autoCreateIndex(request.autoCreateIndex())
.order(request.order())
.settings(templateSettingsBuilder.build())
.mappings(request.mappings())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.metadata.MetaDataIndexTemplateService;
import org.elasticsearch.common.Booleans;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.Tuple;
Expand All @@ -41,6 +42,8 @@
*/
public final class AutoCreateIndex {

public static final String AUTO_CREATE_TEMPLATE_BASED = "_template";

public static final Setting<AutoCreate> AUTO_CREATE_INDEX_SETTING =
new Setting<>("action.auto_create_index", "true", AutoCreate::new, Property.NodeScope, Setting.Property.Dynamic);

Expand All @@ -62,6 +65,10 @@ public boolean needToCheck() {
return this.autoCreate.autoCreateIndex;
}

public boolean templateBased() {
return autoCreate.templateBased;
}

/**
* Should the index be auto created?
* @throws IndexNotFoundException if the index doesn't exist and shouldn't be auto created
Expand All @@ -70,6 +77,24 @@ public boolean shouldAutoCreate(String index, ClusterState state) {
if (resolver.hasIndexOrAlias(index, state)) {
return false;
}
if (templateBased()) {
return MetaDataIndexTemplateService.findTemplates(state.metaData(), index).stream().findFirst()
.map(template -> {
if (template.autoCreateIndex()) {
return true;
}
throw new IndexNotFoundException(
"no such index and [" + AUTO_CREATE_INDEX_SETTING.getKey()
+ "] is set to [_template] but the matching template does not allow for auto creating the index.",
index
);
}).orElseThrow(
() -> new IndexNotFoundException(
"no such index and [" + AUTO_CREATE_INDEX_SETTING.getKey()
+ "] is set to [_template] but there is no matching template.", index
)
);
}
// One volatile read, so that all checks are done against the same instance:
final AutoCreate autoCreate = this.autoCreate;
if (autoCreate.autoCreateIndex == false) {
Expand Down Expand Up @@ -108,45 +133,58 @@ void setAutoCreate(AutoCreate autoCreate) {

static class AutoCreate {
private final boolean autoCreateIndex;
private final boolean templateBased;
private final List<Tuple<String, Boolean>> expressions;
private final String string;

private AutoCreate(String value) {
boolean autoCreateIndex;
List<Tuple<String, Boolean>> expressions = new ArrayList<>();
try {
autoCreateIndex = Booleans.parseBoolean(value);
} catch (IllegalArgumentException ex) {
if (AUTO_CREATE_TEMPLATE_BASED.equals(value)) {
autoCreateIndex = true;
templateBased = true;
} else {
templateBased = false;
try {
String[] patterns = Strings.commaDelimitedListToStringArray(value);
for (String pattern : patterns) {
if (pattern == null || pattern.trim().length() == 0) {
throw new IllegalArgumentException("Can't parse [" + value + "] for setting [action.auto_create_index] must "
+ "be either [true, false, or a comma separated list of index patterns]");
}
pattern = pattern.trim();
Tuple<String, Boolean> expression;
if (pattern.startsWith("-")) {
if (pattern.length() == 1) {
throw new IllegalArgumentException("Can't parse [" + value + "] for setting [action.auto_create_index] "
+ "must contain an index name after [-]");
autoCreateIndex = Booleans.parseBoolean(value);
} catch (IllegalArgumentException ex) {
try {
String[] patterns = Strings.commaDelimitedListToStringArray(value);
for (String pattern : patterns) {
if (pattern == null || pattern.trim().length() == 0) {
throw new IllegalArgumentException(
"Can't parse [" + value + "] for setting [action.auto_create_index] must "
+ "be either [true, false, or a comma separated list of index patterns]"
);
}
expression = new Tuple<>(pattern.substring(1), false);
} else if(pattern.startsWith("+")) {
if (pattern.length() == 1) {
throw new IllegalArgumentException("Can't parse [" + value + "] for setting [action.auto_create_index] "
+ "must contain an index name after [+]");
pattern = pattern.trim();
Tuple<String, Boolean> expression;
if (pattern.startsWith("-")) {
if (pattern.length() == 1) {
throw new IllegalArgumentException(
"Can't parse [" + value + "] for setting [action.auto_create_index] "
+ "must contain an index name after [-]"
);
}
expression = new Tuple<>(pattern.substring(1), false);
} else if (pattern.startsWith("+")) {
if (pattern.length() == 1) {
throw new IllegalArgumentException(
"Can't parse [" + value + "] for setting [action.auto_create_index] "
+ "must contain an index name after [+]"
);
}
expression = new Tuple<>(pattern.substring(1), true);
} else {
expression = new Tuple<>(pattern, true);
}
expression = new Tuple<>(pattern.substring(1), true);
} else {
expression = new Tuple<>(pattern, true);
expressions.add(expression);
}
expressions.add(expression);
autoCreateIndex = true;
} catch (IllegalArgumentException ex1) {
ex1.addSuppressed(ex);
throw ex1;
}
autoCreateIndex = true;
} catch (IllegalArgumentException ex1) {
ex1.addSuppressed(ex);
throw ex1;
}
}
this.expressions = expressions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public class IndexTemplateMetaData extends AbstractDiffable<IndexTemplateMetaDat

private final List<String> patterns;

private final boolean autoCreateIndex;

private final Settings settings;

// the mapping source should always include the type as top level
Expand All @@ -90,14 +92,16 @@ public class IndexTemplateMetaData extends AbstractDiffable<IndexTemplateMetaDat
public IndexTemplateMetaData(String name, int order, Integer version,
List<String> patterns, Settings settings,
ImmutableOpenMap<String, CompressedXContent> mappings,
ImmutableOpenMap<String, AliasMetaData> aliases) {
ImmutableOpenMap<String, AliasMetaData> aliases,
boolean autoCreateIndex) {
if (patterns == null || patterns.isEmpty()) {
throw new IllegalArgumentException("Index patterns must not be null or empty; got " + patterns);
}
this.name = name;
this.order = order;
this.version = version;
this.patterns= patterns;
this.autoCreateIndex = autoCreateIndex;
this.settings = settings;
this.mappings = mappings;
this.aliases = aliases;
Expand Down Expand Up @@ -137,6 +141,14 @@ public List<String> getPatterns() {
return this.patterns;
}

public boolean getAutoCreateIndex() {
return this.autoCreateIndex;
}

public boolean autoCreateIndex() {
return this.autoCreateIndex;
}

public Settings settings() {
return this.settings;
}
Expand Down Expand Up @@ -196,7 +208,11 @@ public static IndexTemplateMetaData readFrom(StreamInput in) throws IOException
Builder builder = new Builder(in.readString());
builder.order(in.readInt());
if (in.getVersion().onOrAfter(Version.V_6_0_0_alpha1)) {
builder.patterns(in.readList(StreamInput::readString));
List<String> indexPatterns = in.readList(StreamInput::readString);
if (in.getVersion().onOrAfter(Version.V_7_0_0_alpha1)) {
builder.autoCreateIndex(Boolean.parseBoolean(indexPatterns.remove(indexPatterns.size() - 1)));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a serialized boolean to the end of this list feels really dirty, but I couldn't get the BwC tests to pass when adding a new byte to the message (even when wrapping writing that boolean in a if (in.getVersion().onOrAfter(Version.V_7_0_0_alpha1)) {). Maybe I'm making a mistake here?

if (in.getVersion().onOrAfter(Version.V_7_0_0_alpha1)) {`
 autoCreateIndex = in.readBoolean();
}

did not work for me in BwC at least.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this can be resolved in a different way since the issues mentions users will have to update their templates when using the new auto create mode and BwC isn't really an issue?

}
builder.patterns(indexPatterns);
} else {
builder.patterns(Collections.singletonList(in.readString()));
}
Expand Down Expand Up @@ -231,7 +247,14 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeString(name);
out.writeInt(order);
if (out.getVersion().onOrAfter(Version.V_6_0_0_alpha1)) {
out.writeStringList(patterns);
final List<String> indexPatterns;
if (out.getVersion().onOrAfter(Version.V_7_0_0_alpha1)) {
indexPatterns = new ArrayList<>(patterns);
indexPatterns.add(Boolean.toString(autoCreateIndex));
} else {
indexPatterns = patterns;
}
out.writeStringList(indexPatterns);
} else {
out.writeString(patterns.get(0));
}
Expand All @@ -254,7 +277,7 @@ public void writeTo(StreamOutput out) throws IOException {
public static class Builder {

private static final Set<String> VALID_FIELDS = Sets.newHashSet(
"template", "order", "mappings", "settings", "index_patterns", "aliases", "version");
"template", "order", "mappings", "settings", "index_patterns", "auto_create_index" , "aliases", "version");

private String name;

Expand All @@ -264,6 +287,8 @@ public static class Builder {

private List<String> indexPatterns;

private boolean autoCreateIndex;

private Settings settings = Settings.Builder.EMPTY_SETTINGS;

private final ImmutableOpenMap.Builder<String, CompressedXContent> mappings;
Expand Down Expand Up @@ -302,6 +327,10 @@ public Builder patterns(List<String> indexPatterns) {
return this;
}

public Builder autoCreateIndex(boolean autoCreateIndex) {
this.autoCreateIndex = autoCreateIndex;
return this;
}

public Builder settings(Settings.Builder settings) {
this.settings = settings.build();
Expand Down Expand Up @@ -339,7 +368,10 @@ public Builder putAlias(AliasMetaData.Builder aliasMetaData) {
}

public IndexTemplateMetaData build() {
return new IndexTemplateMetaData(name, order, version, indexPatterns, settings, mappings.build(), aliases.build());
return new IndexTemplateMetaData(
name, order, version, indexPatterns, settings, mappings.build(), aliases.build(), autoCreateIndex
);

}

public static void toXContent(IndexTemplateMetaData indexTemplateMetaData, XContentBuilder builder, ToXContent.Params params)
Expand All @@ -359,6 +391,7 @@ public static void toInnerXContent(IndexTemplateMetaData indexTemplateMetaData,
builder.field("version", indexTemplateMetaData.version());
}
builder.field("index_patterns", indexTemplateMetaData.patterns());
builder.field("auto_create_index", indexTemplateMetaData.autoCreateIndex());

builder.startObject("settings");
indexTemplateMetaData.settings().toXContent(builder, params);
Expand Down Expand Up @@ -456,6 +489,8 @@ public static IndexTemplateMetaData fromXContent(XContentParser parser, String t
builder.order(parser.intValue());
} else if ("version".equals(currentFieldName)) {
builder.version(parser.intValue());
} else if ("auto_create_index".equals(currentFieldName)) {
builder.autoCreateIndex(parser.booleanValue());
}
}
}
Expand Down
Loading