Skip to content

Commit df45716

Browse files
imotovdakrone
authored andcommitted
Replace IndexMetaData.Custom with Map-based custom metadata (elastic#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
1 parent 1319b40 commit df45716

17 files changed

+404
-332
lines changed

server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexClusterStateUpdateRequest.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ public class CreateIndexClusterStateUpdateRequest extends ClusterStateUpdateRequ
5656

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

59-
private final Map<String, IndexMetaData.Custom> customs = new HashMap<>();
60-
6159
private final Set<ClusterBlock> blocks = new HashSet<>();
6260

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

89-
public CreateIndexClusterStateUpdateRequest customs(Map<String, IndexMetaData.Custom> customs) {
90-
this.customs.putAll(customs);
91-
return this;
92-
}
93-
9487
public CreateIndexClusterStateUpdateRequest blocks(Set<ClusterBlock> blocks) {
9588
this.blocks.addAll(blocks);
9689
return this;
@@ -149,10 +142,6 @@ public Set<Alias> aliases() {
149142
return aliases;
150143
}
151144

152-
public Map<String, IndexMetaData.Custom> customs() {
153-
return customs;
154-
}
155-
156145
public Set<ClusterBlock> blocks() {
157146
return blocks;
158147
}

server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.elasticsearch.action.support.ActiveShardCount;
3030
import org.elasticsearch.action.support.IndicesOptions;
3131
import org.elasticsearch.action.support.master.AcknowledgedRequest;
32-
import org.elasticsearch.cluster.metadata.IndexMetaData;
3332
import org.elasticsearch.common.ParseField;
3433
import org.elasticsearch.common.Strings;
3534
import org.elasticsearch.common.bytes.BytesArray;
@@ -87,8 +86,6 @@ public class CreateIndexRequest extends AcknowledgedRequest<CreateIndexRequest>
8786

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

90-
private final Map<String, IndexMetaData.Custom> customs = new HashMap<>();
91-
9289
private boolean updateAllTypes = false;
9390

9491
private ActiveShardCount waitForActiveShards = ActiveShardCount.DEFAULT;
@@ -397,16 +394,7 @@ public CreateIndexRequest source(Map<String, ?> source, DeprecationHandler depre
397394
found = true;
398395
aliases((Map<String, Object>) entry.getValue());
399396
} else {
400-
// maybe custom?
401-
IndexMetaData.Custom proto = IndexMetaData.lookupPrototype(name);
402-
if (proto != null) {
403-
found = true;
404-
try {
405-
customs.put(name, proto.fromMap((Map<String, Object>) entry.getValue()));
406-
} catch (IOException e) {
407-
throw new ElasticsearchParseException("failed to parse custom metadata for [{}]", name);
408-
}
409-
}
397+
throw new ElasticsearchParseException("unknown key [{}] for create index", name);
410398
}
411399
}
412400
if (!found) {
@@ -424,18 +412,6 @@ public Set<Alias> aliases() {
424412
return this.aliases;
425413
}
426414

427-
/**
428-
* Adds custom metadata to the index to be created.
429-
*/
430-
public CreateIndexRequest custom(IndexMetaData.Custom custom) {
431-
customs.put(custom.type(), custom);
432-
return this;
433-
}
434-
435-
public Map<String, IndexMetaData.Custom> customs() {
436-
return this.customs;
437-
}
438-
439415
/** True if all fields that span multiple types should be updated, false otherwise */
440416
public boolean updateAllTypes() {
441417
return updateAllTypes;
@@ -498,11 +474,13 @@ public void readFrom(StreamInput in) throws IOException {
498474
}
499475
mappings.put(type, source);
500476
}
501-
int customSize = in.readVInt();
502-
for (int i = 0; i < customSize; i++) {
503-
String type = in.readString();
504-
IndexMetaData.Custom customIndexMetaData = IndexMetaData.lookupPrototypeSafe(type).readFrom(in);
505-
customs.put(type, customIndexMetaData);
477+
if (in.getVersion().before(Version.V_7_0_0_alpha1)) {
478+
// This used to be the size of custom metadata classes
479+
int customSize = in.readVInt();
480+
assert customSize == 0 : "unexpected custom metadata when none is supported";
481+
if (customSize > 0) {
482+
throw new IllegalStateException("unexpected custom metadata when none is supported");
483+
}
506484
}
507485
int aliasesSize = in.readVInt();
508486
for (int i = 0; i < aliasesSize; i++) {
@@ -523,10 +501,9 @@ public void writeTo(StreamOutput out) throws IOException {
523501
out.writeString(entry.getKey());
524502
out.writeString(entry.getValue());
525503
}
526-
out.writeVInt(customs.size());
527-
for (Map.Entry<String, IndexMetaData.Custom> entry : customs.entrySet()) {
528-
out.writeString(entry.getKey());
529-
entry.getValue().writeTo(out);
504+
if (out.getVersion().before(Version.V_7_0_0_alpha1)) {
505+
// Size of custom index metadata, which is removed
506+
out.writeVInt(0);
530507
}
531508
out.writeVInt(aliases.size());
532509
for (Alias alias : aliases) {
@@ -562,10 +539,6 @@ public XContentBuilder innerToXContent(XContentBuilder builder, Params params) t
562539
alias.toXContent(builder, params);
563540
}
564541
builder.endObject();
565-
566-
for (Map.Entry<String, IndexMetaData.Custom> entry : customs.entrySet()) {
567-
builder.field(entry.getKey(), entry.getValue(), params);
568-
}
569542
return builder;
570543
}
571544
}

server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequestBuilder.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.elasticsearch.action.support.ActiveShardCount;
2424
import org.elasticsearch.action.support.master.AcknowledgedRequestBuilder;
2525
import org.elasticsearch.client.ElasticsearchClient;
26-
import org.elasticsearch.cluster.metadata.IndexMetaData;
2726
import org.elasticsearch.common.bytes.BytesReference;
2827
import org.elasticsearch.common.settings.Settings;
2928
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
@@ -224,14 +223,6 @@ public CreateIndexRequestBuilder setSource(Map<String, ?> source) {
224223
return this;
225224
}
226225

227-
/**
228-
* Adds custom metadata to the index to be created.
229-
*/
230-
public CreateIndexRequestBuilder addCustom(IndexMetaData.Custom custom) {
231-
request.custom(custom);
232-
return this;
233-
}
234-
235226
/**
236227
* Sets the settings and mappings as a single source.
237228
*/

server/src/main/java/org/elasticsearch/action/admin/indices/create/TransportCreateIndexAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected void masterOperation(final CreateIndexRequest request, final ClusterSt
7575
final CreateIndexClusterStateUpdateRequest updateRequest = new CreateIndexClusterStateUpdateRequest(request, cause, indexName, request.index(), request.updateAllTypes())
7676
.ackTimeout(request.timeout()).masterNodeTimeout(request.masterNodeTimeout())
7777
.settings(request.settings()).mappings(request.mappings())
78-
.aliases(request.aliases()).customs(request.customs())
78+
.aliases(request.aliases())
7979
.waitForActiveShards(request.waitForActiveShards());
8080

8181
createIndexService.createIndex(updateRequest, ActionListener.wrap(response ->

server/src/main/java/org/elasticsearch/action/admin/indices/shrink/TransportResizeAction.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ static CreateIndexClusterStateUpdateRequest prepareCreateIndexRequest(final Resi
180180
.masterNodeTimeout(targetIndex.masterNodeTimeout())
181181
.settings(targetIndex.settings())
182182
.aliases(targetIndex.aliases())
183-
.customs(targetIndex.customs())
184183
.waitForActiveShards(targetIndex.waitForActiveShards())
185184
.recoverFrom(metaData.getIndex())
186185
.resizeType(resizeRequest.getResizeType())

server/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequest.java

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
2828
import org.elasticsearch.action.support.IndicesOptions;
2929
import org.elasticsearch.action.support.master.MasterNodeRequest;
30-
import org.elasticsearch.cluster.metadata.IndexMetaData;
3130
import org.elasticsearch.common.Strings;
3231
import org.elasticsearch.common.bytes.BytesArray;
3332
import org.elasticsearch.common.bytes.BytesReference;
@@ -88,8 +87,6 @@ public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateR
8887

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

91-
private Map<String, IndexMetaData.Custom> customs = new HashMap<>();
92-
9390
private Integer version;
9491

9592
public PutIndexTemplateRequest() {
@@ -353,15 +350,7 @@ public PutIndexTemplateRequest source(Map templateSource) {
353350
} else if (name.equals("aliases")) {
354351
aliases((Map<String, Object>) entry.getValue());
355352
} else {
356-
// maybe custom?
357-
IndexMetaData.Custom proto = IndexMetaData.lookupPrototype(name);
358-
if (proto != null) {
359-
try {
360-
customs.put(name, proto.fromMap((Map<String, Object>) entry.getValue()));
361-
} catch (IOException e) {
362-
throw new ElasticsearchParseException("failed to parse custom metadata for [{}]", name);
363-
}
364-
}
353+
throw new ElasticsearchParseException("unknown key [{}] in the template ", name);
365354
}
366355
}
367356
return this;
@@ -395,15 +384,6 @@ public PutIndexTemplateRequest source(BytesReference source, XContentType xConte
395384
return source(XContentHelper.convertToMap(source, true, xContentType).v2());
396385
}
397386

398-
public PutIndexTemplateRequest custom(IndexMetaData.Custom custom) {
399-
customs.put(custom.type(), custom);
400-
return this;
401-
}
402-
403-
public Map<String, IndexMetaData.Custom> customs() {
404-
return this.customs;
405-
}
406-
407387
public Set<Alias> aliases() {
408388
return this.aliases;
409389
}
@@ -500,11 +480,13 @@ public void readFrom(StreamInput in) throws IOException {
500480
}
501481
mappings.put(type, mappingSource);
502482
}
503-
int customSize = in.readVInt();
504-
for (int i = 0; i < customSize; i++) {
505-
String type = in.readString();
506-
IndexMetaData.Custom customIndexMetaData = IndexMetaData.lookupPrototypeSafe(type).readFrom(in);
507-
customs.put(type, customIndexMetaData);
483+
if (in.getVersion().before(Version.V_7_0_0_alpha1)) {
484+
// Used to be used for custom index metadata
485+
int customSize = in.readVInt();
486+
assert customSize == 0 : "expected not to have any custom metadata";
487+
if (customSize > 0) {
488+
throw new IllegalStateException("unexpected custom metadata when none is supported");
489+
}
508490
}
509491
int aliasesSize = in.readVInt();
510492
for (int i = 0; i < aliasesSize; i++) {
@@ -531,10 +513,8 @@ public void writeTo(StreamOutput out) throws IOException {
531513
out.writeString(entry.getKey());
532514
out.writeString(entry.getValue());
533515
}
534-
out.writeVInt(customs.size());
535-
for (Map.Entry<String, IndexMetaData.Custom> entry : customs.entrySet()) {
536-
out.writeString(entry.getKey());
537-
entry.getValue().writeTo(out);
516+
if (out.getVersion().before(Version.V_7_0_0_alpha1)) {
517+
out.writeVInt(0);
538518
}
539519
out.writeVInt(aliases.size());
540520
for (Alias alias : aliases) {
@@ -570,10 +550,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
570550
}
571551
builder.endObject();
572552

573-
for (Map.Entry<String, IndexMetaData.Custom> entry : customs.entrySet()) {
574-
builder.field(entry.getKey(), entry.getValue(), params);
575-
}
576-
577553
return builder;
578554
}
579555
}

server/src/main/java/org/elasticsearch/action/admin/indices/template/put/TransportPutIndexTemplateAction.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ protected void masterOperation(final PutIndexTemplateRequest request, final Clus
8484
.settings(templateSettingsBuilder.build())
8585
.mappings(request.mappings())
8686
.aliases(request.aliases())
87-
.customs(request.customs())
8887
.create(request.create())
8988
.masterTimeout(request.masterNodeTimeout())
9089
.version(request.version()),

0 commit comments

Comments
 (0)