Skip to content

Commit

Permalink
Remove PROTO-serialization from IndexMetaData.Custom (II)
Browse files Browse the repository at this point in the history
Switches IndexMetaData.Custom to standard named serialization.

Supersedes elastic#32683
  • Loading branch information
imotov committed Aug 9, 2018
1 parent 79375d3 commit 3f5d717
Show file tree
Hide file tree
Showing 12 changed files with 526 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,15 @@ public <T, C> T parseNamedObject(Class<T> categoryClass, String name, XContentPa
return categoryClass.cast(entry.parser.parse(parser, context));
}

/**
* Returns true if the given named object is supported by the current stream
*/
public <T> boolean supportsNamedObject(Class<T> categoryClass, String name) {
Map<String, Entry> parsers = registry.get(categoryClass);
if (parsers == null || registry.isEmpty()) {
return false;
}
return parsers.containsKey(name);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,32 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.DeprecationHandler;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedObjectNotFoundException;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.plugins.spi.NamedXContentProvider;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.ServiceLoader;
import java.util.Set;

import static org.elasticsearch.action.ValidateActions.addValidationError;
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
import static org.elasticsearch.common.settings.Settings.readSettingsFromStream;
import static org.elasticsearch.common.settings.Settings.writeSettingsToStream;
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;

/**
* A request to create an index. Best created with {@link org.elasticsearch.client.Requests#createIndexRequest(String)}.
Expand Down Expand Up @@ -374,8 +379,16 @@ public CreateIndexRequest source(BytesReference source, XContentType xContentTyp
/**
* Sets the settings and mappings as a single source.
*/
@SuppressWarnings("unchecked")
public CreateIndexRequest source(Map<String, ?> source, DeprecationHandler deprecationHandler) {
return source(source, deprecationHandler, null);
}


/**
* Sets the settings, mappings and customs as a single source, using specified registry to parse customs
*/
@SuppressWarnings("unchecked")
public CreateIndexRequest source(Map<String, ?> source, DeprecationHandler deprecationHandler, NamedXContentRegistry customsRegistry) {
for (Map.Entry<String, ?> entry : source.entrySet()) {
String name = entry.getKey();
if (SETTINGS.match(name, deprecationHandler)) {
Expand All @@ -389,15 +402,20 @@ public CreateIndexRequest source(Map<String, ?> source, DeprecationHandler depre
aliases((Map<String, Object>) entry.getValue());
} else {
// maybe custom?
IndexMetaData.Custom proto = IndexMetaData.lookupPrototype(name);
if (proto != null) {
if (entry.getValue() instanceof Map) {
if (customsRegistry == null) {
// Custom registry wasn't specified - we are in client mode let's try loading it from SPI
customsRegistry = new NamedXContentRegistry(getProvidedNamedXContents());
}
try {
customs.put(name, proto.fromMap((Map<String, Object>) entry.getValue()));
customs.put(name, IndexMetaData.parseCustom(name, (Map<String, Object>) entry.getValue(), deprecationHandler,
customsRegistry));
} catch (IOException e) {
throw new ElasticsearchParseException("failed to parse custom metadata for [{}]", name);
throw new ElasticsearchParseException("failed to parse custom metadata for [{}]", e, name);
} catch (NamedObjectNotFoundException e) {
throw new ElasticsearchParseException("unknown key [{}] for create index", e, name);
}
} else {
// found a key which is neither custom defined nor one of the supported ones
throw new ElasticsearchParseException("unknown key [{}] for create index", name);
}
}
Expand All @@ -417,7 +435,7 @@ public Set<Alias> aliases() {
* Adds custom metadata to the index to be created.
*/
public CreateIndexRequest custom(IndexMetaData.Custom custom) {
customs.put(custom.type(), custom);
customs.put(custom.getWriteableName(), custom);
return this;
}

Expand Down Expand Up @@ -476,9 +494,8 @@ public void readFrom(StreamInput in) throws IOException {
}
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);
IndexMetaData.Custom customIndexMetaData = in.readNamedWriteable(IndexMetaData.Custom.class);;
customs.put(customIndexMetaData.getWriteableName(), customIndexMetaData);
}
int aliasesSize = in.readVInt();
for (int i = 0; i < aliasesSize; i++) {
Expand All @@ -502,9 +519,8 @@ public void writeTo(StreamOutput out) throws IOException {
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);
for (IndexMetaData.Custom custom : customs.values()) {
out.writeNamedWriteable(custom);
}
out.writeVInt(aliases.size());
for (Alias alias : aliases) {
Expand Down Expand Up @@ -548,4 +564,15 @@ public XContentBuilder innerToXContent(XContentBuilder builder, Params params) t
}
return builder;
}

/**
* Loads and returns the {@link NamedXContentRegistry.Entry} parsers provided by plugins.
*/
public static List<NamedXContentRegistry.Entry> getProvidedNamedXContents() {
List<NamedXContentRegistry.Entry> entries = new ArrayList<>();
for (NamedXContentProvider service : ServiceLoader.load(NamedXContentProvider.class)) {
entries.addAll(service.getNamedXContentParsers());
}
return entries;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,14 @@ public CreateIndexRequestBuilder addAlias(Alias alias) {
return this;
}

/**
* Sets custom metadata on index creation
*/
public CreateIndexRequestBuilder setCustom(IndexMetaData.Custom custom) {
request.custom(custom);
return this;
}

/**
* Sets the settings and mappings as a single source.
*/
Expand Down Expand Up @@ -224,14 +232,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 @@ -39,6 +39,7 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.DeprecationHandler;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedObjectNotFoundException;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
Expand All @@ -61,9 +62,10 @@
import java.util.stream.Collectors;

import static org.elasticsearch.action.ValidateActions.addValidationError;
import static org.elasticsearch.action.admin.indices.create.CreateIndexRequest.getProvidedNamedXContents;
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
import static org.elasticsearch.common.settings.Settings.readSettingsFromStream;
import static org.elasticsearch.common.settings.Settings.writeSettingsToStream;
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;

/**
* A request to create an index template.
Expand Down Expand Up @@ -305,11 +307,16 @@ public PutIndexTemplateRequest source(XContentBuilder templateBuilder) {
}
}

@SuppressWarnings("unchecked")
public PutIndexTemplateRequest source(Map<String, Object> templateSource) {
return source(templateSource, null);
}

/**
* The template source definition.
*/
@SuppressWarnings("unchecked")
public PutIndexTemplateRequest source(Map<String, Object> templateSource) {
public PutIndexTemplateRequest source(Map<String, Object> templateSource, NamedXContentRegistry customsRegistry) {
Map<String, Object> source = templateSource;
for (Map.Entry<String, Object> entry : source.entrySet()) {
String name = entry.getKey();
Expand Down Expand Up @@ -354,13 +361,21 @@ public PutIndexTemplateRequest source(Map<String, Object> templateSource) {
aliases((Map<String, Object>) entry.getValue());
} else {
// maybe custom?
IndexMetaData.Custom proto = IndexMetaData.lookupPrototype(name);
if (proto != null) {
if (entry.getValue() instanceof Map) {
if (customsRegistry == null) {
// Custom registry wasn't specified - we are in client mode let's try loading it from SPI
customsRegistry = new NamedXContentRegistry(getProvidedNamedXContents());
}
try {
customs.put(name, proto.fromMap((Map<String, Object>) entry.getValue()));
customs.put(name, IndexMetaData.parseCustom(name, (Map<String, Object>) entry.getValue(),
LoggingDeprecationHandler.INSTANCE, customsRegistry));
} catch (IOException e) {
throw new ElasticsearchParseException("failed to parse custom metadata for [{}]", name);
throw new ElasticsearchParseException("failed to parse custom metadata for [{}]", e, name);
} catch (NamedObjectNotFoundException e) {
throw new ElasticsearchParseException("unknown key [{}] in the template", e, name);
}
} else {
throw new ElasticsearchParseException("unknown key [{}] in the template ", name);
}
}
}
Expand Down Expand Up @@ -388,6 +403,13 @@ public PutIndexTemplateRequest source(byte[] source, int offset, int length, XCo
return source(new BytesArray(source, offset, length), xContentType);
}

/**
* The template source definition that should be parsed using specified customs registry.
*/
public PutIndexTemplateRequest source(BytesReference source, XContentType xContentType, NamedXContentRegistry customsRegistry) {
return source(XContentHelper.convertToMap(source, true, xContentType).v2(), customsRegistry);
}

/**
* The template source definition.
*/
Expand All @@ -396,7 +418,7 @@ public PutIndexTemplateRequest source(BytesReference source, XContentType xConte
}

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

Expand Down Expand Up @@ -501,9 +523,8 @@ public void readFrom(StreamInput in) throws IOException {
}
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);
IndexMetaData.Custom customIndexMetaData = in.readNamedWriteable(IndexMetaData.Custom.class);
customs.put(customIndexMetaData.getWriteableName(), customIndexMetaData);
}
int aliasesSize = in.readVInt();
for (int i = 0; i < aliasesSize; i++) {
Expand Down Expand Up @@ -531,9 +552,8 @@ public void writeTo(StreamOutput out) throws IOException {
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);
for (IndexMetaData.Custom custom : customs.values()) {
out.writeNamedWriteable(custom);
}
out.writeVInt(aliases.size());
for (Alias alias : aliases) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.action.support.master.MasterNodeOperationRequestBuilder;
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.XContentBuilder;
Expand Down Expand Up @@ -210,6 +211,16 @@ public PutIndexTemplateRequestBuilder addMapping(String type, Map<String, Object
return this;
}

/**
* Sets custom metadata object.
*
* @param custom The custom index metadata object
*/
public PutIndexTemplateRequestBuilder setCustom(IndexMetaData.Custom custom) {
request.custom(custom);
return this;
}

/**
* The template source definition.
*/
Expand Down
Loading

0 comments on commit 3f5d717

Please sign in to comment.