-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Add searchable snapshots index template for snapshot cache indices #59701
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
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5fdd84a
Add searchable snapshots index template
tlrx 1feb55d
Fix action origin
tlrx b7cf647
use composable index templates
tlrx 5f033bf
remove log
tlrx 7e242f0
Merge branch 'master' into add-searchable-snapshots-index-template
tlrx c15a56c
add hidden
tlrx a25ae80
fix template again
tlrx 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
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
32 changes: 32 additions & 0 deletions
32
x-pack/plugin/core/src/main/resources/searchable-snapshots-template.json
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 |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| { | ||
| "index_patterns": [ | ||
| ".snapshots-${xpack.searchable_snapshots.template.version}" | ||
| ], | ||
| "priority": 100, | ||
| "version": ${xpack.searchable_snapshots.template.version}, | ||
| "template": { | ||
| "aliases": { | ||
| ".snapshots": {} | ||
| }, | ||
| "settings": { | ||
| "number_of_shards": 1, | ||
| "number_of_replicas": 0, | ||
| "auto_expand_replicas": "0-1", | ||
| "hidden": true | ||
| }, | ||
| "mappings": { | ||
| "_doc": { | ||
| "_meta": { | ||
| "searchable-snapshot-version": "${xpack.searchable_snapshots.version}" | ||
| }, | ||
| "dynamic": false, | ||
| "properties": { | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "_meta": { | ||
| "description": "default index template for searchable snapshots cache installed by x-pack", | ||
| "managed": true | ||
| } | ||
tlrx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
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
78 changes: 78 additions & 0 deletions
78
...java/org/elasticsearch/xpack/searchablesnapshots/SearchableSnapshotsTemplateRegistry.java
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 |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.searchablesnapshots; | ||
|
|
||
| import org.elasticsearch.Version; | ||
| import org.elasticsearch.client.Client; | ||
| import org.elasticsearch.cluster.service.ClusterService; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.common.xcontent.NamedXContentRegistry; | ||
| import org.elasticsearch.threadpool.ThreadPool; | ||
| import org.elasticsearch.xpack.core.template.IndexTemplateConfig; | ||
| import org.elasticsearch.xpack.core.template.IndexTemplateRegistry; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import static org.elasticsearch.xpack.core.ClientHelper.SEARCHABLE_SNAPSHOTS_ORIGIN; | ||
| import static org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshotsConstants.SEARCHABLE_SNAPSHOTS_FEATURE_ENABLED; | ||
|
|
||
| public class SearchableSnapshotsTemplateRegistry extends IndexTemplateRegistry { | ||
|
|
||
| // history (please add a comment why you increased the version here) | ||
| // version 1: initial | ||
| static final int INDEX_TEMPLATE_VERSION = 1; | ||
|
|
||
| static final String SNAPSHOTS_CACHE_TEMPLATE_NAME = ".snapshots"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we call this something more specific, e.g. |
||
|
|
||
| private SearchableSnapshotsTemplateRegistry( | ||
| Settings nodeSettings, | ||
| ClusterService clusterService, | ||
| ThreadPool threadPool, | ||
| Client client, | ||
| NamedXContentRegistry xContentRegistry | ||
| ) { | ||
| super(nodeSettings, clusterService, threadPool, client, xContentRegistry); | ||
| } | ||
|
|
||
| public static SearchableSnapshotsTemplateRegistry register( | ||
| Settings nodeSettings, | ||
| ClusterService clusterService, | ||
| ThreadPool threadPool, | ||
| Client client, | ||
| NamedXContentRegistry xContentRegistry | ||
| ) { | ||
| // registers a cluster state listener | ||
| return new SearchableSnapshotsTemplateRegistry(nodeSettings, clusterService, threadPool, client, xContentRegistry); | ||
| } | ||
|
|
||
| @Override | ||
| protected String getOrigin() { | ||
| return SEARCHABLE_SNAPSHOTS_ORIGIN; | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean requiresMasterNode() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| protected List<IndexTemplateConfig> getComposableTemplateConfigs() { | ||
| if (SEARCHABLE_SNAPSHOTS_FEATURE_ENABLED) { | ||
| return List.of( | ||
| new IndexTemplateConfig( | ||
| SNAPSHOTS_CACHE_TEMPLATE_NAME, | ||
| "/searchable-snapshots-template.json", | ||
| INDEX_TEMPLATE_VERSION, | ||
| "xpack.searchable_snapshots.template.version", | ||
| Map.of("xpack.searchable_snapshots.version", Version.CURRENT.toString()) | ||
| ) | ||
| ); | ||
| } | ||
| return List.of(); | ||
| } | ||
| } | ||
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
192 changes: 192 additions & 0 deletions
192
...org/elasticsearch/xpack/searchablesnapshots/SearchableSnapshotsTemplateRegistryTests.java
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 |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.searchablesnapshots; | ||
|
|
||
| import org.elasticsearch.Version; | ||
| import org.elasticsearch.action.ActionListener; | ||
| import org.elasticsearch.action.ActionRequest; | ||
| import org.elasticsearch.action.ActionResponse; | ||
| import org.elasticsearch.action.ActionType; | ||
| import org.elasticsearch.action.admin.indices.template.put.PutComposableIndexTemplateAction; | ||
| import org.elasticsearch.action.support.PlainActionFuture; | ||
| import org.elasticsearch.action.support.master.AcknowledgedResponse; | ||
| import org.elasticsearch.cluster.ClusterName; | ||
| import org.elasticsearch.cluster.ClusterState; | ||
| import org.elasticsearch.cluster.metadata.AliasMetadata; | ||
| import org.elasticsearch.cluster.metadata.ComposableIndexTemplate; | ||
| import org.elasticsearch.cluster.metadata.IndexTemplateMetadata; | ||
| import org.elasticsearch.cluster.metadata.Metadata; | ||
| import org.elasticsearch.cluster.metadata.Template; | ||
| import org.elasticsearch.cluster.node.DiscoveryNode; | ||
| import org.elasticsearch.cluster.node.DiscoveryNodes; | ||
| import org.elasticsearch.cluster.service.ClusterService; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.test.ClusterServiceUtils; | ||
| import org.elasticsearch.test.ESTestCase; | ||
| import org.elasticsearch.test.client.NoOpClient; | ||
| import org.elasticsearch.threadpool.TestThreadPool; | ||
| import org.elasticsearch.threadpool.ThreadPool; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.function.BiFunction; | ||
|
|
||
| import static java.util.Collections.emptyMap; | ||
| import static org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshotsTemplateRegistry.INDEX_TEMPLATE_VERSION; | ||
| import static org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshotsTemplateRegistry.SNAPSHOTS_CACHE_TEMPLATE_NAME; | ||
| import static org.hamcrest.Matchers.equalTo; | ||
| import static org.hamcrest.Matchers.hasItem; | ||
| import static org.hamcrest.Matchers.hasSize; | ||
| import static org.hamcrest.Matchers.instanceOf; | ||
| import static org.hamcrest.Matchers.is; | ||
| import static org.hamcrest.Matchers.nullValue; | ||
|
|
||
| public class SearchableSnapshotsTemplateRegistryTests extends ESTestCase { | ||
|
|
||
| private void executeTest( | ||
| final ClusterState.Builder clusterState, | ||
| final BiFunction<ActionType<?>, ActionRequest, ActionResponse> consumer | ||
| ) { | ||
| final ThreadPool threadPool = new TestThreadPool(getTestName()); | ||
| try (ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool)) { | ||
| final NoOpClient client = new NoOpClient(threadPool) { | ||
| @Override | ||
| @SuppressWarnings("unchecked") | ||
| protected <Request extends ActionRequest, Response extends ActionResponse> void doExecute( | ||
| ActionType<Response> action, | ||
| Request request, | ||
| ActionListener<Response> listener | ||
| ) { | ||
| try { | ||
| listener.onResponse((Response) consumer.apply(action, request)); | ||
| } catch (Exception e) { | ||
| listener.onFailure(e); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| SearchableSnapshotsTemplateRegistry.register(Settings.EMPTY, clusterService, threadPool, client, xContentRegistry()); | ||
| ClusterServiceUtils.setState(clusterService, clusterState); | ||
| } catch (Exception e) { | ||
| throw new AssertionError("", e); | ||
| } finally { | ||
| terminate(threadPool); | ||
| } | ||
| } | ||
|
|
||
| public void testTemplateDoesNotExist() throws Exception { | ||
| final ClusterState.Builder clusterState = newEmptyClusterState(); | ||
|
|
||
| final PlainActionFuture<PutComposableIndexTemplateAction.Request> executed = PlainActionFuture.newFuture(); | ||
| executeTest(clusterState, (action, request) -> { | ||
| if (action instanceof PutComposableIndexTemplateAction) { | ||
| assertThat(request, instanceOf(PutComposableIndexTemplateAction.Request.class)); | ||
| executed.onResponse((PutComposableIndexTemplateAction.Request) request); | ||
| return new AcknowledgedResponse(true); | ||
| } else { | ||
| executed.onFailure(unsupportedAction(action)); | ||
| return new AcknowledgedResponse(false); | ||
| } | ||
| }); | ||
|
|
||
| assertComposableIndexTemplateRequest(executed.get()); | ||
| } | ||
|
|
||
| public void testTemplateAlreadyExists() { | ||
| final ClusterState.Builder clusterState = newEmptyClusterState(); | ||
| clusterState.metadata( | ||
| Metadata.builder() | ||
| .put( | ||
| ".snapshots", | ||
| new ComposableIndexTemplate( | ||
| List.of(".snapshots-0"), | ||
| new Template(Settings.EMPTY, null, Map.of(".snapshots", AliasMetadata.builder(".snapshots").build())), | ||
| null, | ||
| 123L, | ||
| 1L, | ||
| emptyMap() | ||
| ) | ||
| ) | ||
| .build() | ||
| ); | ||
|
|
||
| final AtomicBoolean executed = new AtomicBoolean(false); | ||
| executeTest(clusterState, (action, request) -> { | ||
| executed.set(true); | ||
| fail("Index template should not be created"); | ||
| return null; | ||
| }); | ||
| assertThat(executed.get(), is(false)); | ||
| } | ||
|
|
||
| public void testTemplateShouldBeUpdated() throws Exception { | ||
| final ClusterState.Builder clusterState = newEmptyClusterState(); | ||
| clusterState.metadata( | ||
| Metadata.builder() | ||
| .put( | ||
| IndexTemplateMetadata.builder(".snapshots") | ||
| .patterns(List.of(randomAlphaOfLength(10))) | ||
| .version(randomBoolean() ? 0 : null) // old or non-existent version | ||
| .putAlias(AliasMetadata.builder(randomAlphaOfLength(10)).build()) | ||
| .build() | ||
| ) | ||
| .build() | ||
| ); | ||
|
|
||
| final PlainActionFuture<PutComposableIndexTemplateAction.Request> executed = PlainActionFuture.newFuture(); | ||
| executeTest(clusterState, (action, request) -> { | ||
| if (action instanceof PutComposableIndexTemplateAction) { | ||
| assertThat(request, instanceOf(PutComposableIndexTemplateAction.Request.class)); | ||
| executed.onResponse((PutComposableIndexTemplateAction.Request) request); | ||
| return new AcknowledgedResponse(true); | ||
| } else { | ||
| executed.onFailure(unsupportedAction(action)); | ||
| return new AcknowledgedResponse(false); | ||
| } | ||
| }); | ||
|
|
||
| assertComposableIndexTemplateRequest(executed.get()); | ||
| } | ||
|
|
||
| public void testThatMissingMasterNodeDoesNothing() { | ||
| final ClusterState.Builder clusterState = newEmptyClusterState(); | ||
| clusterState.nodes(DiscoveryNodes.builder(clusterState.nodes()).masterNodeId(null)); | ||
|
|
||
| final AtomicBoolean executed = new AtomicBoolean(false); | ||
| executeTest(clusterState, (action, request) -> { | ||
| executed.set(true); | ||
| fail("if the master is missing nothing should happen"); | ||
| return null; | ||
| }); | ||
| assertThat(clusterState.build().nodes().getMasterNode(), nullValue()); | ||
| assertThat(executed.get(), is(false)); | ||
| } | ||
|
|
||
| private static void assertComposableIndexTemplateRequest(final PutComposableIndexTemplateAction.Request templateRequest) { | ||
| assertThat(templateRequest.name(), equalTo(SNAPSHOTS_CACHE_TEMPLATE_NAME)); | ||
| final ComposableIndexTemplate indexTemplate = templateRequest.indexTemplate(); | ||
| assertThat(indexTemplate.version(), equalTo((long) INDEX_TEMPLATE_VERSION)); | ||
| assertThat(indexTemplate.indexPatterns(), hasItem(equalTo(SNAPSHOTS_CACHE_TEMPLATE_NAME + "-" + INDEX_TEMPLATE_VERSION))); | ||
| assertThat(indexTemplate.indexPatterns(), hasSize(1)); | ||
| final List<String> aliases = new ArrayList<>(indexTemplate.template().aliases().keySet()); | ||
| assertThat(aliases, hasItem(equalTo(SNAPSHOTS_CACHE_TEMPLATE_NAME))); | ||
| assertThat(aliases, hasSize(1)); | ||
| } | ||
|
|
||
| private Exception unsupportedAction(ActionType<?> action) { | ||
| return new UnsupportedOperationException("The test [" + getTestName() + " ] does not support action [" + action.name() + ']'); | ||
| } | ||
|
|
||
| private ClusterState.Builder newEmptyClusterState() { | ||
| final DiscoveryNode node = new DiscoveryNode(randomAlphaOfLength(10), buildNewFakeTransportAddress(), Version.CURRENT); | ||
| return ClusterState.builder(new ClusterName(getTestName())) | ||
| .version(0L) | ||
| .nodes(DiscoveryNodes.builder().add(node).localNodeId(node.getId()).masterNodeId(node.getId())); | ||
| } | ||
| } |
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.
Should
.snapshotsindices also haveindex.hidden: trueset in their settings? (I can't remember right now whether dot-prefixed indices automatically are hidden)