Skip to content

Commit

Permalink
Replace IndexMetaData.Custom with Map-based custom metadata (elastic#…
Browse files Browse the repository at this point in the history
…32749)

This PR removes the deprecated `Custom` class in `IndexMetaData`, in favor
of a `Map<String, DiffableStringMap>` that is used to store custom index
metadata. As part of this, there is now no way to set this metadata in a
template or create index request (since it's only set by plugins, or dedicated
REST endpoints).

The `Map<String, DiffableStringMap>` is intended to be a namespaced `Map<String,
String>` (`DiffableStringMap` implements `Map<String, String>`, so the signature
is more like `Map<String, Map<String, String>>`). This is so we can do things
like:

``` java
Map<String, String> ccrMeta = indexMetaData.getCustom("ccr");
```

And then have complete control over the metadata. This also means any
plugin/feature that uses this has to manage its own BWC, as the map is just
serialized as a map. It also means that if metadata is put in the map that isn't
used (for instance, if a plugin were removed), it causes no failures the way
an unregistered `Setting` would.

The reason I use a custom `DiffableStringMap` here rather than a plain
`Map<String, String>` is so the map can be diffed with previous cluster state
updates for serialization.

Supersedes elastic#32683
  • Loading branch information
imotov authored and dakrone committed Aug 30, 2018
1 parent 1319b40 commit df45716
Show file tree
Hide file tree
Showing 17 changed files with 404 additions and 332 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ public class CreateIndexClusterStateUpdateRequest extends ClusterStateUpdateRequ

private final Set<Alias> aliases = new HashSet<>();

private final Map<String, IndexMetaData.Custom> customs = new HashMap<>();

private final Set<ClusterBlock> blocks = new HashSet<>();

private ActiveShardCount waitForActiveShards = ActiveShardCount.DEFAULT;
Expand Down Expand Up @@ -86,11 +84,6 @@ public CreateIndexClusterStateUpdateRequest aliases(Set<Alias> aliases) {
return this;
}

public CreateIndexClusterStateUpdateRequest customs(Map<String, IndexMetaData.Custom> customs) {
this.customs.putAll(customs);
return this;
}

public CreateIndexClusterStateUpdateRequest blocks(Set<ClusterBlock> blocks) {
this.blocks.addAll(blocks);
return this;
Expand Down Expand Up @@ -149,10 +142,6 @@ public Set<Alias> aliases() {
return aliases;
}

public Map<String, IndexMetaData.Custom> customs() {
return customs;
}

public Set<ClusterBlock> blocks() {
return blocks;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.elasticsearch.action.support.ActiveShardCount;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.master.AcknowledgedRequest;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray;
Expand Down Expand Up @@ -87,8 +86,6 @@ public class CreateIndexRequest extends AcknowledgedRequest<CreateIndexRequest>

private final Set<Alias> aliases = new HashSet<>();

private final Map<String, IndexMetaData.Custom> customs = new HashMap<>();

private boolean updateAllTypes = false;

private ActiveShardCount waitForActiveShards = ActiveShardCount.DEFAULT;
Expand Down Expand Up @@ -397,16 +394,7 @@ public CreateIndexRequest source(Map<String, ?> source, DeprecationHandler depre
found = true;
aliases((Map<String, Object>) entry.getValue());
} else {
// maybe custom?
IndexMetaData.Custom proto = IndexMetaData.lookupPrototype(name);
if (proto != null) {
found = true;
try {
customs.put(name, proto.fromMap((Map<String, Object>) entry.getValue()));
} catch (IOException e) {
throw new ElasticsearchParseException("failed to parse custom metadata for [{}]", name);
}
}
throw new ElasticsearchParseException("unknown key [{}] for create index", name);
}
}
if (!found) {
Expand All @@ -424,18 +412,6 @@ public Set<Alias> aliases() {
return this.aliases;
}

/**
* Adds custom metadata to the index to be created.
*/
public CreateIndexRequest custom(IndexMetaData.Custom custom) {
customs.put(custom.type(), custom);
return this;
}

public Map<String, IndexMetaData.Custom> customs() {
return this.customs;
}

/** True if all fields that span multiple types should be updated, false otherwise */
public boolean updateAllTypes() {
return updateAllTypes;
Expand Down Expand Up @@ -498,11 +474,13 @@ public void readFrom(StreamInput in) throws IOException {
}
mappings.put(type, source);
}
int customSize = in.readVInt();
for (int i = 0; i < customSize; i++) {
String type = in.readString();
IndexMetaData.Custom customIndexMetaData = IndexMetaData.lookupPrototypeSafe(type).readFrom(in);
customs.put(type, customIndexMetaData);
if (in.getVersion().before(Version.V_7_0_0_alpha1)) {
// This used to be the size of custom metadata classes
int customSize = in.readVInt();
assert customSize == 0 : "unexpected custom metadata when none is supported";
if (customSize > 0) {
throw new IllegalStateException("unexpected custom metadata when none is supported");
}
}
int aliasesSize = in.readVInt();
for (int i = 0; i < aliasesSize; i++) {
Expand All @@ -523,10 +501,9 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeString(entry.getKey());
out.writeString(entry.getValue());
}
out.writeVInt(customs.size());
for (Map.Entry<String, IndexMetaData.Custom> entry : customs.entrySet()) {
out.writeString(entry.getKey());
entry.getValue().writeTo(out);
if (out.getVersion().before(Version.V_7_0_0_alpha1)) {
// Size of custom index metadata, which is removed
out.writeVInt(0);
}
out.writeVInt(aliases.size());
for (Alias alias : aliases) {
Expand Down Expand Up @@ -562,10 +539,6 @@ public XContentBuilder innerToXContent(XContentBuilder builder, Params params) t
alias.toXContent(builder, params);
}
builder.endObject();

for (Map.Entry<String, IndexMetaData.Custom> entry : customs.entrySet()) {
builder.field(entry.getKey(), entry.getValue(), params);
}
return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.elasticsearch.action.support.ActiveShardCount;
import org.elasticsearch.action.support.master.AcknowledgedRequestBuilder;
import org.elasticsearch.client.ElasticsearchClient;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
Expand Down Expand Up @@ -224,14 +223,6 @@ public CreateIndexRequestBuilder setSource(Map<String, ?> source) {
return this;
}

/**
* Adds custom metadata to the index to be created.
*/
public CreateIndexRequestBuilder addCustom(IndexMetaData.Custom custom) {
request.custom(custom);
return this;
}

/**
* Sets the settings and mappings as a single source.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected void masterOperation(final CreateIndexRequest request, final ClusterSt
final CreateIndexClusterStateUpdateRequest updateRequest = new CreateIndexClusterStateUpdateRequest(request, cause, indexName, request.index(), request.updateAllTypes())
.ackTimeout(request.timeout()).masterNodeTimeout(request.masterNodeTimeout())
.settings(request.settings()).mappings(request.mappings())
.aliases(request.aliases()).customs(request.customs())
.aliases(request.aliases())
.waitForActiveShards(request.waitForActiveShards());

createIndexService.createIndex(updateRequest, ActionListener.wrap(response ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ static CreateIndexClusterStateUpdateRequest prepareCreateIndexRequest(final Resi
.masterNodeTimeout(targetIndex.masterNodeTimeout())
.settings(targetIndex.settings())
.aliases(targetIndex.aliases())
.customs(targetIndex.customs())
.waitForActiveShards(targetIndex.waitForActiveShards())
.recoverFrom(metaData.getIndex())
.resizeType(resizeRequest.getResizeType())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.master.MasterNodeRequest;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
Expand Down Expand Up @@ -88,8 +87,6 @@ public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateR

private final Set<Alias> aliases = new HashSet<>();

private Map<String, IndexMetaData.Custom> customs = new HashMap<>();

private Integer version;

public PutIndexTemplateRequest() {
Expand Down Expand Up @@ -353,15 +350,7 @@ public PutIndexTemplateRequest source(Map templateSource) {
} else if (name.equals("aliases")) {
aliases((Map<String, Object>) entry.getValue());
} else {
// maybe custom?
IndexMetaData.Custom proto = IndexMetaData.lookupPrototype(name);
if (proto != null) {
try {
customs.put(name, proto.fromMap((Map<String, Object>) entry.getValue()));
} catch (IOException e) {
throw new ElasticsearchParseException("failed to parse custom metadata for [{}]", name);
}
}
throw new ElasticsearchParseException("unknown key [{}] in the template ", name);
}
}
return this;
Expand Down Expand Up @@ -395,15 +384,6 @@ public PutIndexTemplateRequest source(BytesReference source, XContentType xConte
return source(XContentHelper.convertToMap(source, true, xContentType).v2());
}

public PutIndexTemplateRequest custom(IndexMetaData.Custom custom) {
customs.put(custom.type(), custom);
return this;
}

public Map<String, IndexMetaData.Custom> customs() {
return this.customs;
}

public Set<Alias> aliases() {
return this.aliases;
}
Expand Down Expand Up @@ -500,11 +480,13 @@ public void readFrom(StreamInput in) throws IOException {
}
mappings.put(type, mappingSource);
}
int customSize = in.readVInt();
for (int i = 0; i < customSize; i++) {
String type = in.readString();
IndexMetaData.Custom customIndexMetaData = IndexMetaData.lookupPrototypeSafe(type).readFrom(in);
customs.put(type, customIndexMetaData);
if (in.getVersion().before(Version.V_7_0_0_alpha1)) {
// Used to be used for custom index metadata
int customSize = in.readVInt();
assert customSize == 0 : "expected not to have any custom metadata";
if (customSize > 0) {
throw new IllegalStateException("unexpected custom metadata when none is supported");
}
}
int aliasesSize = in.readVInt();
for (int i = 0; i < aliasesSize; i++) {
Expand All @@ -531,10 +513,8 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeString(entry.getKey());
out.writeString(entry.getValue());
}
out.writeVInt(customs.size());
for (Map.Entry<String, IndexMetaData.Custom> entry : customs.entrySet()) {
out.writeString(entry.getKey());
entry.getValue().writeTo(out);
if (out.getVersion().before(Version.V_7_0_0_alpha1)) {
out.writeVInt(0);
}
out.writeVInt(aliases.size());
for (Alias alias : aliases) {
Expand Down Expand Up @@ -570,10 +550,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
}
builder.endObject();

for (Map.Entry<String, IndexMetaData.Custom> entry : customs.entrySet()) {
builder.field(entry.getKey(), entry.getValue(), params);
}

return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ protected void masterOperation(final PutIndexTemplateRequest request, final Clus
.settings(templateSettingsBuilder.build())
.mappings(request.mappings())
.aliases(request.aliases())
.customs(request.customs())
.create(request.create())
.masterTimeout(request.masterNodeTimeout())
.version(request.version()),
Expand Down
Loading

0 comments on commit df45716

Please sign in to comment.