-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Better extension point for custom cluster state parts #21243
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
Closed
martijnvg
wants to merge
6
commits into
elastic:master
from
martijnvg:custom_cluster_metadata_plugin
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
976be0c
Attempt to make a better extension point for custom cluster metadata …
martijnvg 0989378
Make custum index matadata work and removed NOCOMMITS.
martijnvg c3f8d74
added tests and applied feedback
martijnvg e7c31be
Fix smaller feedbakc
martijnvg 7000b06
fixed compile and checkstyle errors, also changed looking custom ind…
martijnvg f191788
Simplified backwards test
martijnvg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ | |
| import org.elasticsearch.action.support.ActiveShardCount; | ||
| import org.elasticsearch.action.support.IndicesOptions; | ||
| import org.elasticsearch.action.support.master.AcknowledgedRequest; | ||
| import org.elasticsearch.cluster.CustomPrototypeRegistry; | ||
| import org.elasticsearch.cluster.metadata.IndexMetaData; | ||
| import org.elasticsearch.common.bytes.BytesArray; | ||
| import org.elasticsearch.common.bytes.BytesReference; | ||
|
|
@@ -359,10 +360,17 @@ public CreateIndexRequest source(byte[] source, int offset, int length) { | |
| * Sets the settings and mappings as a single source. | ||
| */ | ||
| public CreateIndexRequest source(BytesReference source) { | ||
| return source(source, CustomPrototypeRegistry.EMPTY); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the settings and mappings as a single source. | ||
| */ | ||
| public CreateIndexRequest source(BytesReference source, CustomPrototypeRegistry registry) { | ||
| XContentType xContentType = XContentFactory.xContentType(source); | ||
| if (xContentType != null) { | ||
| try (XContentParser parser = XContentFactory.xContent(xContentType).createParser(source)) { | ||
| source(parser.map()); | ||
| source(parser.map(), registry); | ||
| } catch (IOException e) { | ||
| throw new ElasticsearchParseException("failed to parse source for create index", e); | ||
| } | ||
|
|
@@ -375,8 +383,15 @@ public CreateIndexRequest source(BytesReference source) { | |
| /** | ||
| * Sets the settings and mappings as a single source. | ||
| */ | ||
| @SuppressWarnings("unchecked") | ||
| public CreateIndexRequest source(Map<String, ?> source) { | ||
| return source(source, CustomPrototypeRegistry.EMPTY); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the settings and mappings as a single source. | ||
| */ | ||
| @SuppressWarnings("unchecked") | ||
| public CreateIndexRequest source(Map<String, ?> source, CustomPrototypeRegistry registry) { | ||
| boolean found = false; | ||
| for (Map.Entry<String, ?> entry : source.entrySet()) { | ||
| String name = entry.getKey(); | ||
|
|
@@ -393,8 +408,7 @@ public CreateIndexRequest source(Map<String, ?> source) { | |
| found = true; | ||
| aliases((Map<String, Object>) entry.getValue()); | ||
| } else { | ||
| // maybe custom? | ||
| IndexMetaData.Custom proto = IndexMetaData.lookupPrototype(name); | ||
| IndexMetaData.Custom proto = registry.getIndexMetadataPrototype(name); | ||
| if (proto != null) { | ||
| found = true; | ||
| try { | ||
|
|
@@ -475,7 +489,6 @@ public CreateIndexRequest waitForActiveShards(final int waitForActiveShards) { | |
| return waitForActiveShards(ActiveShardCount.from(waitForActiveShards)); | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public void readFrom(StreamInput in) throws IOException { | ||
| super.readFrom(in); | ||
|
|
@@ -489,9 +502,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.type(), customIndexMetaData); | ||
| } | ||
| int aliasesSize = in.readVInt(); | ||
| for (int i = 0; i < aliasesSize; i++) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,14 +22,18 @@ | |
| import org.elasticsearch.cluster.action.index.MappingUpdatedAction; | ||
| import org.elasticsearch.cluster.action.index.NodeMappingRefreshAction; | ||
| import org.elasticsearch.cluster.action.shard.ShardStateAction; | ||
| import org.elasticsearch.cluster.metadata.IndexGraveyard; | ||
| import org.elasticsearch.cluster.metadata.IndexMetaData; | ||
| import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; | ||
| import org.elasticsearch.cluster.metadata.MetaData; | ||
| import org.elasticsearch.cluster.metadata.MetaDataCreateIndexService; | ||
| import org.elasticsearch.cluster.metadata.MetaDataDeleteIndexService; | ||
| import org.elasticsearch.cluster.metadata.MetaDataIndexAliasesService; | ||
| import org.elasticsearch.cluster.metadata.MetaDataIndexStateService; | ||
| import org.elasticsearch.cluster.metadata.MetaDataIndexTemplateService; | ||
| import org.elasticsearch.cluster.metadata.MetaDataMappingService; | ||
| import org.elasticsearch.cluster.metadata.MetaDataUpdateSettingsService; | ||
| import org.elasticsearch.cluster.metadata.RepositoriesMetaData; | ||
| import org.elasticsearch.cluster.routing.DelayedAllocationService; | ||
| import org.elasticsearch.cluster.routing.RoutingService; | ||
| import org.elasticsearch.cluster.routing.allocation.AllocationService; | ||
|
|
@@ -58,7 +62,9 @@ | |
| import org.elasticsearch.common.settings.Setting.Property; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.gateway.GatewayAllocator; | ||
| import org.elasticsearch.ingest.IngestMetadata; | ||
| import org.elasticsearch.plugins.ClusterPlugin; | ||
| import org.elasticsearch.script.ScriptMetaData; | ||
| import org.elasticsearch.tasks.TaskResultsService; | ||
|
|
||
| import java.util.Collection; | ||
|
|
@@ -86,6 +92,8 @@ public class ClusterModule extends AbstractModule { | |
| final Collection<AllocationDecider> allocationDeciders; | ||
| final ShardsAllocator shardsAllocator; | ||
|
|
||
| private final CustomPrototypeRegistry registry; | ||
|
|
||
| // pkg private so tests can mock | ||
| Class<? extends ClusterInfoService> clusterInfoServiceImpl = InternalClusterInfoService.class; | ||
|
|
||
|
|
@@ -95,6 +103,7 @@ public ClusterModule(Settings settings, ClusterService clusterService, List<Clus | |
| this.shardsAllocator = createShardsAllocator(settings, clusterService.getClusterSettings(), clusterPlugins); | ||
| this.clusterService = clusterService; | ||
| indexNameExpressionResolver = new IndexNameExpressionResolver(settings); | ||
| registry = createCustomPrototypeRegistry(clusterPlugins); | ||
| } | ||
|
|
||
| public IndexNameExpressionResolver getIndexNameExpressionResolver() { | ||
|
|
@@ -157,6 +166,46 @@ private static ShardsAllocator createShardsAllocator(Settings settings, ClusterS | |
| "ShardsAllocator factory for [" + allocatorName + "] returned null"); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a {@link CustomPrototypeRegistry} instance that registers built-in cluster state parts and custom | ||
| * cluster state parts provided by plugins | ||
| */ | ||
| public static CustomPrototypeRegistry createCustomPrototypeRegistry(Collection<ClusterPlugin> clusterPlugins) { | ||
| Map<String, ClusterState.Custom> customClusterStatePrototypes = new HashMap<>(); | ||
| customClusterStatePrototypes.put(SnapshotsInProgress.TYPE, SnapshotsInProgress.PROTO); | ||
| customClusterStatePrototypes.put(RestoreInProgress.TYPE, RestoreInProgress.PROTO); | ||
|
|
||
| Map<String, MetaData.Custom> customMetadataPrototypes = new HashMap<>(); | ||
| customMetadataPrototypes.put(RepositoriesMetaData.TYPE, RepositoriesMetaData.PROTO); | ||
| customMetadataPrototypes.put(IngestMetadata.TYPE, IngestMetadata.PROTO); | ||
| customMetadataPrototypes.put(ScriptMetaData.TYPE, ScriptMetaData.PROTO); | ||
| customMetadataPrototypes.put(IndexGraveyard.TYPE, IndexGraveyard.PROTO); | ||
|
|
||
| Map<String, IndexMetaData.Custom> customIndexMetadataPrototypes = new HashMap<>(); | ||
|
|
||
| for (ClusterPlugin clusterPlugin : clusterPlugins) { | ||
| for (ClusterState.Custom custom : clusterPlugin.getCustomClusterState()) { | ||
| ClusterState.Custom previous = customClusterStatePrototypes.putIfAbsent(custom.type(), custom); | ||
| if (previous != null) { | ||
| throw new IllegalStateException("Custom cluster state [" + custom.type() + "] already declared"); | ||
| } | ||
| } | ||
| for (MetaData.Custom custom : clusterPlugin.getCustomMetadata()) { | ||
| MetaData.Custom previous = customMetadataPrototypes.putIfAbsent(custom.type(), custom); | ||
| if (previous != null) { | ||
| throw new IllegalStateException("Custom metadata [" + custom.type() + "] already declared"); | ||
| } | ||
| } | ||
| for (IndexMetaData.Custom custom : clusterPlugin.getCustomIndexMetadata()) { | ||
| IndexMetaData.Custom previous = customIndexMetadataPrototypes.putIfAbsent(custom.type(), custom); | ||
| if (previous != null) { | ||
|
||
| throw new IllegalStateException("Custom index metadata [" + custom.type() + "] already declared"); | ||
| } | ||
| } | ||
| } | ||
| return new CustomPrototypeRegistry(customClusterStatePrototypes, customMetadataPrototypes, customIndexMetadataPrototypes); | ||
| } | ||
|
|
||
| @Override | ||
| protected void configure() { | ||
| bind(ClusterInfoService.class).to(clusterInfoServiceImpl).asEagerSingleton(); | ||
|
|
@@ -180,5 +229,10 @@ protected void configure() { | |
| bind(TaskResultsService.class).asEagerSingleton(); | ||
| bind(AllocationDeciders.class).toInstance(new AllocationDeciders(settings, allocationDeciders)); | ||
| bind(ShardsAllocator.class).toInstance(shardsAllocator); | ||
| bind(CustomPrototypeRegistry.class).toInstance(registry); | ||
| } | ||
|
|
||
| public CustomPrototypeRegistry getRegistry() { | ||
| return registry; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should throw an exception if the proto doesn't resolve to anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You made a
Safemethod for this already I think.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed, we should do that here. I'll use the
Safevariants for that.