27
27
import org .elasticsearch .repositories .Repository ;
28
28
import org .elasticsearch .repositories .blobstore .BlobStoreRepository ;
29
29
import org .elasticsearch .snapshots .SnapshotId ;
30
+ import org .elasticsearch .xpack .searchablesnapshots .cache .CacheDirectory ;
31
+ import org .elasticsearch .xpack .searchablesnapshots .cache .CacheService ;
30
32
31
33
import java .io .IOException ;
34
+ import java .nio .file .Path ;
32
35
import java .util .HashMap ;
33
36
import java .util .Map ;
34
37
import java .util .function .Function ;
@@ -53,6 +56,8 @@ public class SearchableSnapshotRepository extends FilterRepository {
53
56
Setting .simpleString ("index.store.snapshot.snapshot_uuid" , Setting .Property .IndexScope , Setting .Property .PrivateIndex );
54
57
public static final Setting <String > SNAPSHOT_INDEX_ID_SETTING =
55
58
Setting .simpleString ("index.store.snapshot.index_uuid" , Setting .Property .IndexScope , Setting .Property .PrivateIndex );
59
+ public static final Setting <Boolean > SNAPSHOT_CACHE_ENABLED_SETTING =
60
+ Setting .boolSetting ("index.store.snapshot.cache.enabled" , true , Setting .Property .IndexScope );
56
61
57
62
public static final String SNAPSHOT_DIRECTORY_FACTORY_KEY = "snapshot" ;
58
63
@@ -64,12 +69,12 @@ public class SearchableSnapshotRepository extends FilterRepository {
64
69
public SearchableSnapshotRepository (Repository in ) {
65
70
super (in );
66
71
if (in instanceof BlobStoreRepository == false ) {
67
- throw new IllegalArgumentException ("Repository [" + in + "] does not support searchable snapshots" );
72
+ throw new IllegalArgumentException ("Repository [" + in + "] does not support searchable snapshots" );
68
73
}
69
74
blobStoreRepository = (BlobStoreRepository ) in ;
70
75
}
71
76
72
- private Directory makeDirectory (IndexSettings indexSettings , ShardPath shardPath ) throws IOException {
77
+ private Directory makeDirectory (IndexSettings indexSettings , ShardPath shardPath , CacheService cacheService ) throws IOException {
73
78
74
79
IndexId indexId = new IndexId (indexSettings .getIndex ().getName (), SNAPSHOT_INDEX_ID_SETTING .get (indexSettings .getSettings ()));
75
80
BlobContainer blobContainer = blobStoreRepository .shardContainer (indexId , shardPath .getShardId ().id ());
@@ -78,10 +83,14 @@ private Directory makeDirectory(IndexSettings indexSettings, ShardPath shardPath
78
83
SNAPSHOT_SNAPSHOT_ID_SETTING .get (indexSettings .getSettings ()));
79
84
BlobStoreIndexShardSnapshot snapshot = blobStoreRepository .loadShardSnapshot (blobContainer , snapshotId );
80
85
81
- final SearchableSnapshotDirectory searchableSnapshotDirectory = new SearchableSnapshotDirectory (snapshot , blobContainer );
82
- final InMemoryNoOpCommitDirectory inMemoryNoOpCommitDirectory = new InMemoryNoOpCommitDirectory (searchableSnapshotDirectory );
86
+ Directory directory = new SearchableSnapshotDirectory (snapshot , blobContainer );
87
+ if (SNAPSHOT_CACHE_ENABLED_SETTING .get (indexSettings .getSettings ())) {
88
+ final Path cacheDir = shardPath .getDataPath ().resolve ("snapshots" ).resolve (snapshotId .getUUID ());
89
+ directory = new CacheDirectory (directory , cacheService , cacheDir );
90
+ }
91
+ directory = new InMemoryNoOpCommitDirectory (directory );
83
92
84
- try (IndexWriter indexWriter = new IndexWriter (inMemoryNoOpCommitDirectory , new IndexWriterConfig ())) {
93
+ try (IndexWriter indexWriter = new IndexWriter (directory , new IndexWriterConfig ())) {
85
94
final Map <String , String > userData = new HashMap <>();
86
95
indexWriter .getLiveCommitData ().forEach (e -> userData .put (e .getKey (), e .getValue ()));
87
96
@@ -94,7 +103,7 @@ private Directory makeDirectory(IndexSettings indexSettings, ShardPath shardPath
94
103
indexWriter .commit ();
95
104
}
96
105
97
- return inMemoryNoOpCommitDirectory ;
106
+ return directory ;
98
107
}
99
108
100
109
@ Override
@@ -136,17 +145,21 @@ public Repository create(RepositoryMetaData metaData, Function<String, Factory>
136
145
};
137
146
}
138
147
139
- public static IndexStorePlugin .DirectoryFactory newDirectoryFactory (final Supplier <RepositoriesService > repositoriesService ) {
148
+ public static IndexStorePlugin .DirectoryFactory newDirectoryFactory (final Supplier <RepositoriesService > repositoriesService ,
149
+ final Supplier <CacheService > cacheService ) {
140
150
return (indexSettings , shardPath ) -> {
141
151
final RepositoriesService repositories = repositoriesService .get ();
142
152
assert repositories != null ;
143
153
144
154
final Repository repository = repositories .repository (SNAPSHOT_REPOSITORY_SETTING .get (indexSettings .getSettings ()));
145
155
if (repository instanceof SearchableSnapshotRepository == false ) {
146
- throw new IllegalArgumentException ("Repository [" + repository + "] is not searchable" );
156
+ throw new IllegalArgumentException ("Repository [" + repository + "] is not searchable" );
147
157
}
148
158
149
- return ((SearchableSnapshotRepository )repository ).makeDirectory (indexSettings , shardPath );
159
+ final CacheService cache = cacheService .get ();
160
+ assert cache != null ;
161
+
162
+ return ((SearchableSnapshotRepository ) repository ).makeDirectory (indexSettings , shardPath , cache );
150
163
};
151
164
}
152
165
}
0 commit comments