forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SearchableSnapshotDirectory should not evict cache files when closed (e…
…lastic#66173) This commit changes the SearchableSnapshotDirectory so that it does not evict all its cache files at closing time, but instead delegates this work to the CacheService. This change is motivated by the fact that Lucene directories are closed as the consequence of applying a new cluster state and as such the closing is executed within the cluster state applier thread; and we want to minimize disk IO operations in such thread (like deleting a lot of evicted cache files). It is also motivated by the future of the searchable snapshot cache which should become persistent. This change is built on top of the existing SearchableSnapshotIndexEventListener and a new SearchableSnapshotIndexFoldersDeletionListener (see elastic#65926) that are used to detect when a searchable snapshot index (or searchable snapshot shard) is removed from a data node. When such a thing happens, the listeners notify the CacheService that maintains an internal list of removed shards. This list is used to evict the cache files associated to these shards as soon as possible (but not in the cluster state applier thread) or right before the same searchable snapshot shard is being built again on the same node. In other situations like opening/closing a searchable snapshot shard then the cache files are not evicted anymore and should be reused.
- Loading branch information
Showing
7 changed files
with
358 additions
and
17 deletions.
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
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.