-
Notifications
You must be signed in to change notification settings - Fork 25k
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
SearchableSnapshotDirectory should not evict cache files when closed #66173
Merged
tlrx
merged 9 commits into
elastic:master
from
tlrx:use-listeners-to-manage-cache-files-for-removed-shards
Dec 11, 2020
+358
−20
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
aeeabc7
Use listeners to manage cache files associated with removed shards
tlrx 6c1a6e9
Merge branch 'master' into use-listeners-to-manage-cache-files-for-re…
tlrx 58b7bd9
invalidate single cache file
tlrx 082e0c8
readLock
tlrx 393deaf
simplify markShardAsEvictedInCache
tlrx 1555ce5
fix messed up imports
tlrx c1c9e54
Merge branch 'master' into use-listeners-to-manage-cache-files-for-re…
tlrx 3395cf9
feedback
tlrx 02aa9c9
Merge branch 'master' into use-listeners-to-manage-cache-files-for-re…
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 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 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 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 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
76 changes: 76 additions & 0 deletions
76
...asticsearch/xpack/searchablesnapshots/SearchableSnapshotIndexFoldersDeletionListener.java
This file contains 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,76 @@ | ||
/* | ||
* 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.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.elasticsearch.index.Index; | ||
import org.elasticsearch.index.IndexSettings; | ||
import org.elasticsearch.index.shard.ShardId; | ||
import org.elasticsearch.plugins.IndexStorePlugin; | ||
import org.elasticsearch.repositories.IndexId; | ||
import org.elasticsearch.snapshots.SnapshotId; | ||
import org.elasticsearch.xpack.searchablesnapshots.cache.CacheService; | ||
|
||
import java.nio.file.Path; | ||
import java.util.Objects; | ||
import java.util.function.Supplier; | ||
|
||
import static org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshots.SNAPSHOT_INDEX_ID_SETTING; | ||
import static org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshots.SNAPSHOT_INDEX_NAME_SETTING; | ||
import static org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshots.SNAPSHOT_SNAPSHOT_ID_SETTING; | ||
import static org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshots.SNAPSHOT_SNAPSHOT_NAME_SETTING; | ||
|
||
/** | ||
* This {@link IndexStorePlugin.IndexFoldersDeletionListener} is called when an index folder or a shard folder is deleted from the disk. If | ||
* the index (or the shard) is a backed by a snapshot this listener notifies the {@link CacheService} that the cache files associated to the | ||
* shard(s) must be evicted. | ||
*/ | ||
public class SearchableSnapshotIndexFoldersDeletionListener implements IndexStorePlugin.IndexFoldersDeletionListener { | ||
|
||
private static final Logger logger = LogManager.getLogger(SearchableSnapshotIndexEventListener.class); | ||
|
||
private final Supplier<CacheService> cacheService; | ||
|
||
public SearchableSnapshotIndexFoldersDeletionListener(Supplier<CacheService> cacheService) { | ||
this.cacheService = Objects.requireNonNull(cacheService); | ||
} | ||
|
||
@Override | ||
public void beforeIndexFoldersDeleted(Index index, IndexSettings indexSettings, Path[] indexPaths) { | ||
if (SearchableSnapshotsConstants.isSearchableSnapshotStore(indexSettings.getSettings())) { | ||
for (int shard = 0; shard < indexSettings.getNumberOfShards(); shard++) { | ||
markShardAsEvictedInCache(new ShardId(index, shard), indexSettings); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void beforeShardFoldersDeleted(ShardId shardId, IndexSettings indexSettings, Path[] shardPaths) { | ||
if (SearchableSnapshotsConstants.isSearchableSnapshotStore(indexSettings.getSettings())) { | ||
markShardAsEvictedInCache(shardId, indexSettings); | ||
} | ||
} | ||
|
||
private void markShardAsEvictedInCache(ShardId shardId, IndexSettings indexSettings) { | ||
final CacheService cacheService = this.cacheService.get(); | ||
assert cacheService != null : "cache service not initialized"; | ||
|
||
logger.debug("{} marking shard as evicted in searchable snapshots cache (reason: cache files deleted from disk)", shardId); | ||
cacheService.markShardAsEvictedInCache( | ||
new SnapshotId( | ||
SNAPSHOT_SNAPSHOT_NAME_SETTING.get(indexSettings.getSettings()), | ||
SNAPSHOT_SNAPSHOT_ID_SETTING.get(indexSettings.getSettings()) | ||
), | ||
new IndexId( | ||
SNAPSHOT_INDEX_NAME_SETTING.get(indexSettings.getSettings()), | ||
SNAPSHOT_INDEX_ID_SETTING.get(indexSettings.getSettings()) | ||
), | ||
shardId | ||
); | ||
} | ||
} |
This file contains 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
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.
Would holding the
readLock
not be enough?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.
It is enough, I'm not sure why I used and documented the usage of the write lock here. I pushed 082e0c8 to use the read lock instead (as it should prevent any mutation anyway)