-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Add Clear Cache API for Searchable Snapshots #53009
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
Merged
tlrx
merged 6 commits into
elastic:feature/searchable-snapshots
from
tlrx:add-clear-cache-api
Mar 3, 2020
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
57937a3
Add Clear Cache API for Searchable Snapshots
tlrx 544f056
unwrapCacheDirectory
tlrx f1ce506
clear_indices_searchable_snapshots_cache_action
tlrx 244b89e
Qualify index nb of shard setting
tlrx 587819a
Ru same query
tlrx 7021af1
Merge branch 'feature/searchable-snapshots' into add-clear-cache-api
elasticmachine 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
145 changes: 145 additions & 0 deletions
145
...plugin/searchable-snapshots/qa/rest/src/test/resources/rest-api-spec/test/clear_cache.yml
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,145 @@ | ||
--- | ||
setup: | ||
|
||
- do: | ||
indices.create: | ||
index: docs | ||
body: | ||
settings: | ||
number_of_shards: 1 | ||
number_of_replicas: 0 | ||
|
||
- do: | ||
bulk: | ||
body: | ||
- index: | ||
_index: docs | ||
_id: 1 | ||
- field: doc | ||
- index: | ||
_index: docs | ||
_id: 2 | ||
- field: doc | ||
- index: | ||
_index: docs | ||
_id: 3 | ||
- field: other | ||
|
||
- do: | ||
snapshot.create_repository: | ||
repository: repository-fs | ||
body: | ||
type: fs | ||
settings: | ||
location: "repository-fs" | ||
|
||
# Remove the snapshot if a previous test failed to delete it. | ||
# Useful for third party tests that runs the test against a real external service. | ||
- do: | ||
snapshot.delete: | ||
repository: repository-fs | ||
snapshot: snapshot | ||
ignore: 404 | ||
|
||
- do: | ||
snapshot.create: | ||
repository: repository-fs | ||
snapshot: snapshot | ||
wait_for_completion: true | ||
|
||
- do: | ||
indices.delete: | ||
index: docs | ||
|
||
- do: | ||
snapshot.create_repository: | ||
repository: repository-searchable-snapshots | ||
body: | ||
type: searchable | ||
settings: | ||
delegate_type: fs | ||
location: "repository-fs" | ||
|
||
--- | ||
teardown: | ||
|
||
- do: | ||
snapshot.delete: | ||
repository: repository-fs | ||
snapshot: snapshot | ||
ignore: 404 | ||
|
||
- do: | ||
snapshot.delete_repository: | ||
repository: repository-fs | ||
|
||
- do: | ||
snapshot.delete_repository: | ||
repository: repository-searchable-snapshots | ||
|
||
--- | ||
"Clear searchable snapshots cache": | ||
- skip: | ||
version: " - 7.99.99" | ||
reason: searchable snapshots introduced in 8.0 | ||
|
||
- do: | ||
catch: missing | ||
searchable_snapshots.clear_cache: {} | ||
|
||
- match: { error.root_cause.0.type: "resource_not_found_exception" } | ||
- match: { error.root_cause.0.reason: "No searchable snapshots indices found" } | ||
|
||
- do: | ||
catch: missing | ||
searchable_snapshots.clear_cache: | ||
index: _all | ||
|
||
- match: { error.root_cause.0.type: "resource_not_found_exception" } | ||
- match: { error.root_cause.0.reason: "No searchable snapshots indices found" } | ||
|
||
- do: | ||
catch: missing | ||
searchable_snapshots.clear_cache: | ||
index: "unknown" | ||
|
||
- do: | ||
indices.create: | ||
index: non_searchable_snapshot_index | ||
|
||
- do: | ||
catch: missing | ||
searchable_snapshots.clear_cache: | ||
index: non_* | ||
|
||
- match: { error.root_cause.0.type: "resource_not_found_exception" } | ||
- match: { error.root_cause.0.reason: "No searchable snapshots indices found" } | ||
|
||
- do: | ||
snapshot.restore: | ||
repository: repository-searchable-snapshots | ||
snapshot: snapshot | ||
wait_for_completion: true | ||
|
||
- match: { snapshot.snapshot: snapshot } | ||
- match: { snapshot.shards.failed: 0 } | ||
- match: { snapshot.shards.successful: 1 } | ||
|
||
- do: | ||
search: | ||
rest_total_hits_as_int: true | ||
index: docs | ||
body: | ||
query: | ||
match: | ||
field: "doc" | ||
|
||
- match: { hits.total: 2 } | ||
|
||
- do: | ||
searchable_snapshots.clear_cache: | ||
index: "docs" | ||
|
||
- match: { _shards.total: 1 } | ||
- match: { _shards.failed: 0 } | ||
|
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
118 changes: 118 additions & 0 deletions
118
...icsearch/xpack/searchablesnapshots/action/AbstractTransportSearchableSnapshotsAction.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,118 @@ | ||
/* | ||
* 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.action; | ||
|
||
import org.apache.lucene.store.Directory; | ||
import org.apache.lucene.store.FilterDirectory; | ||
import org.elasticsearch.ResourceNotFoundException; | ||
import org.elasticsearch.action.support.ActionFilters; | ||
import org.elasticsearch.action.support.broadcast.BroadcastRequest; | ||
import org.elasticsearch.action.support.broadcast.BroadcastResponse; | ||
import org.elasticsearch.action.support.broadcast.node.TransportBroadcastByNodeAction; | ||
import org.elasticsearch.cluster.ClusterState; | ||
import org.elasticsearch.cluster.block.ClusterBlockException; | ||
import org.elasticsearch.cluster.block.ClusterBlockLevel; | ||
import org.elasticsearch.cluster.metadata.IndexMetaData; | ||
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; | ||
import org.elasticsearch.cluster.routing.ShardRouting; | ||
import org.elasticsearch.cluster.routing.ShardsIterator; | ||
import org.elasticsearch.cluster.service.ClusterService; | ||
import org.elasticsearch.common.Nullable; | ||
import org.elasticsearch.common.io.stream.Writeable; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.index.shard.IndexShard; | ||
import org.elasticsearch.indices.IndicesService; | ||
import org.elasticsearch.transport.TransportService; | ||
import org.elasticsearch.xpack.searchablesnapshots.InMemoryNoOpCommitDirectory; | ||
import org.elasticsearch.xpack.searchablesnapshots.cache.CacheDirectory; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.elasticsearch.index.IndexModule.INDEX_STORE_TYPE_SETTING; | ||
import static org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshotRepository.SNAPSHOT_CACHE_ENABLED_SETTING; | ||
import static org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshotRepository.SNAPSHOT_DIRECTORY_FACTORY_KEY; | ||
|
||
public abstract class AbstractTransportSearchableSnapshotsAction | ||
<Request extends BroadcastRequest<Request>, Response extends BroadcastResponse, ShardOperationResult extends Writeable> | ||
extends TransportBroadcastByNodeAction<Request, Response, ShardOperationResult> { | ||
|
||
private final IndicesService indicesService; | ||
|
||
AbstractTransportSearchableSnapshotsAction(String actionName, ClusterService clusterService, TransportService transportService, | ||
ActionFilters actionFilters, IndexNameExpressionResolver resolver, | ||
Writeable.Reader<Request> request, String executor, IndicesService indicesService) { | ||
super(actionName, clusterService, transportService, actionFilters, resolver, request, executor); | ||
this.indicesService = indicesService; | ||
} | ||
|
||
AbstractTransportSearchableSnapshotsAction(String actionName, ClusterService clusterService, TransportService transportService, | ||
ActionFilters actionFilters, IndexNameExpressionResolver resolver, | ||
Writeable.Reader<Request> request, String executor, IndicesService indicesService, | ||
boolean canTripCircuitBreaker) { | ||
super(actionName, clusterService, transportService, actionFilters, resolver, request, executor, canTripCircuitBreaker); | ||
this.indicesService = indicesService; | ||
} | ||
|
||
@Override | ||
protected ClusterBlockException checkGlobalBlock(ClusterState state, Request request) { | ||
return state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_READ); | ||
} | ||
|
||
@Override | ||
protected ClusterBlockException checkRequestBlock(ClusterState state, Request request, String[] indices) { | ||
return state.blocks().indicesBlockedException(ClusterBlockLevel.METADATA_READ, indices); | ||
} | ||
|
||
@Override | ||
protected ShardsIterator shards(ClusterState state, Request request, String[] concreteIndices) { | ||
final List<String> searchableSnapshotIndices = new ArrayList<>(); | ||
for (String concreteIndex : concreteIndices) { | ||
IndexMetaData indexMetaData = state.metaData().index(concreteIndex); | ||
if (indexMetaData != null) { | ||
Settings indexSettings = indexMetaData.getSettings(); | ||
if (INDEX_STORE_TYPE_SETTING.get(indexSettings).equals(SNAPSHOT_DIRECTORY_FACTORY_KEY)) { | ||
if (SNAPSHOT_CACHE_ENABLED_SETTING.get(indexSettings)) { | ||
searchableSnapshotIndices.add(concreteIndex); | ||
} | ||
} | ||
} | ||
} | ||
if (searchableSnapshotIndices.isEmpty()) { | ||
throw new ResourceNotFoundException("No searchable snapshots indices found"); | ||
} | ||
return state.routingTable().allShards(searchableSnapshotIndices.toArray(new String[0])); | ||
} | ||
|
||
@Override | ||
protected ShardOperationResult shardOperation(Request request, ShardRouting shardRouting) throws IOException { | ||
final IndexShard indexShard = indicesService.indexServiceSafe(shardRouting.index()).getShard(shardRouting.id()); | ||
final CacheDirectory cacheDirectory = unwrap(indexShard.store().directory()); | ||
assert cacheDirectory != null; | ||
assert cacheDirectory.getShardId().equals(shardRouting.shardId()); | ||
return executeShardOperation(request, shardRouting, cacheDirectory); | ||
} | ||
|
||
protected abstract ShardOperationResult executeShardOperation(Request request, ShardRouting shardRouting, | ||
CacheDirectory cacheDirectory) throws IOException; | ||
|
||
@Nullable | ||
private static CacheDirectory unwrap(Directory dir) { | ||
while (dir != null) { | ||
if (dir instanceof CacheDirectory) { | ||
return (CacheDirectory) dir; | ||
} else if (dir instanceof InMemoryNoOpCommitDirectory) { | ||
dir = ((InMemoryNoOpCommitDirectory) dir).getRealDirectory(); | ||
} else if (dir instanceof FilterDirectory) { | ||
dir = ((FilterDirectory) dir).getDelegate(); | ||
} else { | ||
dir = null; | ||
} | ||
} | ||
return null; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...g/elasticsearch/xpack/searchablesnapshots/action/ClearSearchableSnapshotsCacheAction.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,18 @@ | ||
/* | ||
* 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.action; | ||
|
||
import org.elasticsearch.action.ActionType; | ||
|
||
public class ClearSearchableSnapshotsCacheAction extends ActionType<ClearSearchableSnapshotsCacheResponse> { | ||
|
||
public static final ClearSearchableSnapshotsCacheAction INSTANCE = new ClearSearchableSnapshotsCacheAction(); | ||
static final String NAME = "cluster:admin/xpack/searchable_snapshots/cache/clear"; | ||
|
||
private ClearSearchableSnapshotsCacheAction() { | ||
super(NAME, ClearSearchableSnapshotsCacheResponse::new); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
.../elasticsearch/xpack/searchablesnapshots/action/ClearSearchableSnapshotsCacheRequest.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,27 @@ | ||
/* | ||
* 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.action; | ||
|
||
import org.elasticsearch.action.support.IndicesOptions; | ||
import org.elasticsearch.action.support.broadcast.BroadcastRequest; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
|
||
import java.io.IOException; | ||
|
||
public class ClearSearchableSnapshotsCacheRequest extends BroadcastRequest<ClearSearchableSnapshotsCacheRequest> { | ||
|
||
public ClearSearchableSnapshotsCacheRequest(StreamInput in) throws IOException { | ||
super(in); | ||
} | ||
|
||
public ClearSearchableSnapshotsCacheRequest(String... indices) { | ||
super(indices); | ||
} | ||
|
||
protected ClearSearchableSnapshotsCacheRequest(String[] indices, IndicesOptions indicesOptions) { | ||
super(indices, indicesOptions); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...elasticsearch/xpack/searchablesnapshots/action/ClearSearchableSnapshotsCacheResponse.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,25 @@ | ||
/* | ||
* 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.action; | ||
|
||
import org.elasticsearch.action.support.DefaultShardOperationFailedException; | ||
import org.elasticsearch.action.support.broadcast.BroadcastResponse; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public class ClearSearchableSnapshotsCacheResponse extends BroadcastResponse { | ||
|
||
ClearSearchableSnapshotsCacheResponse(StreamInput in) throws IOException { | ||
super(in); | ||
} | ||
|
||
ClearSearchableSnapshotsCacheResponse(int totalShards, int successfulShards, int failedShards, | ||
List<DefaultShardOperationFailedException> shardFailures) { | ||
super(totalShards, successfulShards, failedShards, shardFailures); | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.