Skip to content

Commit 001b78f

Browse files
imotovdakrone
authored andcommitted
Replace IndexMetaData.Custom with Map-based custom metadata (#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 #32683
1 parent af2eaf2 commit 001b78f

17 files changed

+406
-335
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
@@ -55,8 +55,6 @@ public class CreateIndexClusterStateUpdateRequest extends ClusterStateUpdateRequ
5555

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

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

6260
private ActiveShardCount waitForActiveShards = ActiveShardCount.DEFAULT;
@@ -83,11 +81,6 @@ public CreateIndexClusterStateUpdateRequest aliases(Set<Alias> aliases) {
8381
return this;
8482
}
8583

86-
public CreateIndexClusterStateUpdateRequest customs(Map<String, IndexMetaData.Custom> customs) {
87-
this.customs.putAll(customs);
88-
return this;
89-
}
90-
9184
public CreateIndexClusterStateUpdateRequest blocks(Set<ClusterBlock> blocks) {
9285
this.blocks.addAll(blocks);
9386
return this;
@@ -146,10 +139,6 @@ public Set<Alias> aliases() {
146139
return aliases;
147140
}
148141

149-
public Map<String, IndexMetaData.Custom> customs() {
150-
return customs;
151-
}
152-
153142
public Set<ClusterBlock> blocks() {
154143
return blocks;
155144
}

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

Lines changed: 12 additions & 41 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;
@@ -58,9 +57,9 @@
5857
import java.util.Set;
5958

6059
import static org.elasticsearch.action.ValidateActions.addValidationError;
60+
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
6161
import static org.elasticsearch.common.settings.Settings.readSettingsFromStream;
6262
import static org.elasticsearch.common.settings.Settings.writeSettingsToStream;
63-
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
6463

6564
/**
6665
* A request to create an index. Best created with {@link org.elasticsearch.client.Requests#createIndexRequest(String)}.
@@ -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 ActiveShardCount waitForActiveShards = ActiveShardCount.DEFAULT;
9390

9491
public CreateIndexRequest() {
@@ -388,18 +385,7 @@ public CreateIndexRequest source(Map<String, ?> source, DeprecationHandler depre
388385
} else if (ALIASES.match(name, deprecationHandler)) {
389386
aliases((Map<String, Object>) entry.getValue());
390387
} else {
391-
// maybe custom?
392-
IndexMetaData.Custom proto = IndexMetaData.lookupPrototype(name);
393-
if (proto != null) {
394-
try {
395-
customs.put(name, proto.fromMap((Map<String, Object>) entry.getValue()));
396-
} catch (IOException e) {
397-
throw new ElasticsearchParseException("failed to parse custom metadata for [{}]", name);
398-
}
399-
} else {
400-
// found a key which is neither custom defined nor one of the supported ones
401-
throw new ElasticsearchParseException("unknown key [{}] for create index", name);
402-
}
388+
throw new ElasticsearchParseException("unknown key [{}] for create index", name);
403389
}
404390
}
405391
return this;
@@ -413,18 +399,6 @@ public Set<Alias> aliases() {
413399
return this.aliases;
414400
}
415401

416-
/**
417-
* Adds custom metadata to the index to be created.
418-
*/
419-
public CreateIndexRequest custom(IndexMetaData.Custom custom) {
420-
customs.put(custom.type(), custom);
421-
return this;
422-
}
423-
424-
public Map<String, IndexMetaData.Custom> customs() {
425-
return this.customs;
426-
}
427-
428402
public ActiveShardCount waitForActiveShards() {
429403
return waitForActiveShards;
430404
}
@@ -474,11 +448,13 @@ public void readFrom(StreamInput in) throws IOException {
474448
}
475449
mappings.put(type, source);
476450
}
477-
int customSize = in.readVInt();
478-
for (int i = 0; i < customSize; i++) {
479-
String type = in.readString();
480-
IndexMetaData.Custom customIndexMetaData = IndexMetaData.lookupPrototypeSafe(type).readFrom(in);
481-
customs.put(type, customIndexMetaData);
451+
if (in.getVersion().before(Version.V_7_0_0_alpha1)) {
452+
// This used to be the size of custom metadata classes
453+
int customSize = in.readVInt();
454+
assert customSize == 0 : "unexpected custom metadata when none is supported";
455+
if (customSize > 0) {
456+
throw new IllegalStateException("unexpected custom metadata when none is supported");
457+
}
482458
}
483459
int aliasesSize = in.readVInt();
484460
for (int i = 0; i < aliasesSize; i++) {
@@ -501,10 +477,9 @@ public void writeTo(StreamOutput out) throws IOException {
501477
out.writeString(entry.getKey());
502478
out.writeString(entry.getValue());
503479
}
504-
out.writeVInt(customs.size());
505-
for (Map.Entry<String, IndexMetaData.Custom> entry : customs.entrySet()) {
506-
out.writeString(entry.getKey());
507-
entry.getValue().writeTo(out);
480+
if (out.getVersion().before(Version.V_7_0_0_alpha1)) {
481+
// Size of custom index metadata, which is removed
482+
out.writeVInt(0);
508483
}
509484
out.writeVInt(aliases.size());
510485
for (Alias alias : aliases) {
@@ -542,10 +517,6 @@ public XContentBuilder innerToXContent(XContentBuilder builder, Params params) t
542517
alias.toXContent(builder, params);
543518
}
544519
builder.endObject();
545-
546-
for (Map.Entry<String, IndexMetaData.Custom> entry : customs.entrySet()) {
547-
builder.field(entry.getKey(), entry.getValue(), params);
548-
}
549520
return builder;
550521
}
551522
}

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())
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
@@ -185,7 +185,6 @@ static CreateIndexClusterStateUpdateRequest prepareCreateIndexRequest(final Resi
185185
.masterNodeTimeout(targetIndex.masterNodeTimeout())
186186
.settings(targetIndex.settings())
187187
.aliases(targetIndex.aliases())
188-
.customs(targetIndex.customs())
189188
.waitForActiveShards(targetIndex.waitForActiveShards())
190189
.recoverFrom(metaData.getIndex())
191190
.resizeType(resizeRequest.getResizeType())

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

Lines changed: 11 additions & 35 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;
@@ -61,9 +60,9 @@
6160
import java.util.stream.Collectors;
6261

6362
import static org.elasticsearch.action.ValidateActions.addValidationError;
63+
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
6464
import static org.elasticsearch.common.settings.Settings.readSettingsFromStream;
6565
import static org.elasticsearch.common.settings.Settings.writeSettingsToStream;
66-
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
6766

6867
/**
6968
* A request to create an index template.
@@ -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<String, Object> 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
}
@@ -494,11 +474,13 @@ public void readFrom(StreamInput in) throws IOException {
494474
String mappingSource = in.readString();
495475
mappings.put(type, mappingSource);
496476
}
497-
int customSize = in.readVInt();
498-
for (int i = 0; i < customSize; i++) {
499-
String type = in.readString();
500-
IndexMetaData.Custom customIndexMetaData = IndexMetaData.lookupPrototypeSafe(type).readFrom(in);
501-
customs.put(type, customIndexMetaData);
477+
if (in.getVersion().before(Version.V_7_0_0_alpha1)) {
478+
// Used to be used for custom index metadata
479+
int customSize = in.readVInt();
480+
assert customSize == 0 : "expected not to have any custom metadata";
481+
if (customSize > 0) {
482+
throw new IllegalStateException("unexpected custom metadata when none is supported");
483+
}
502484
}
503485
int aliasesSize = in.readVInt();
504486
for (int i = 0; i < aliasesSize; i++) {
@@ -525,10 +507,8 @@ public void writeTo(StreamOutput out) throws IOException {
525507
out.writeString(entry.getKey());
526508
out.writeString(entry.getValue());
527509
}
528-
out.writeVInt(customs.size());
529-
for (Map.Entry<String, IndexMetaData.Custom> entry : customs.entrySet()) {
530-
out.writeString(entry.getKey());
531-
entry.getValue().writeTo(out);
510+
if (out.getVersion().before(Version.V_7_0_0_alpha1)) {
511+
out.writeVInt(0);
532512
}
533513
out.writeVInt(aliases.size());
534514
for (Alias alias : aliases) {
@@ -565,10 +545,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
565545
}
566546
builder.endObject();
567547

568-
for (Map.Entry<String, IndexMetaData.Custom> entry : customs.entrySet()) {
569-
builder.field(entry.getKey(), entry.getValue(), params);
570-
}
571-
572548
return builder;
573549
}
574550
}

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)