@@ -104,12 +104,10 @@ public class StoreTests extends ESTestCase {
104
104
private static final Version MIN_SUPPORTED_LUCENE_VERSION = org .elasticsearch .Version .CURRENT
105
105
.minimumIndexCompatibilityVersion ().luceneVersion ;
106
106
107
- public void testRefCount () throws IOException {
107
+ public void testRefCount () {
108
108
final ShardId shardId = new ShardId ("index" , "_na_" , 1 );
109
- DirectoryService directoryService = new LuceneManagedDirectoryService (random ());
110
109
IndexSettings indexSettings = INDEX_SETTINGS ;
111
-
112
- Store store = new Store (shardId , indexSettings , directoryService , new DummyShardLock (shardId ));
110
+ Store store = new Store (shardId , indexSettings , StoreTests .newDirectory (random ()), new DummyShardLock (shardId ));
113
111
int incs = randomIntBetween (1 , 100 );
114
112
for (int i = 0 ; i < incs ; i ++) {
115
113
if (randomBoolean ()) {
@@ -296,8 +294,7 @@ public void testVerifyingIndexOutputWithBogusInput() throws IOException {
296
294
297
295
public void testNewChecksums () throws IOException {
298
296
final ShardId shardId = new ShardId ("index" , "_na_" , 1 );
299
- DirectoryService directoryService = new LuceneManagedDirectoryService (random ());
300
- Store store = new Store (shardId , INDEX_SETTINGS , directoryService , new DummyShardLock (shardId ));
297
+ Store store = new Store (shardId , INDEX_SETTINGS , StoreTests .newDirectory (random ()), new DummyShardLock (shardId ));
301
298
// set default codec - all segments need checksums
302
299
IndexWriter writer = new IndexWriter (store .directory (), newIndexWriterConfig (random (), new MockAnalyzer (random ())).setCodec (TestUtil .getDefaultCodec ()));
303
300
int docs = 1 + random ().nextInt (100 );
@@ -347,7 +344,7 @@ public void testNewChecksums() throws IOException {
347
344
assertConsistent (store , metadata );
348
345
349
346
TestUtil .checkIndex (store .directory ());
350
- assertDeleteContent (store , directoryService );
347
+ assertDeleteContent (store , store . directory () );
351
348
IOUtils .close (store );
352
349
}
353
350
@@ -455,32 +452,11 @@ private void corruptFile(Directory dir, String fileIn, String fileOut) throws IO
455
452
456
453
}
457
454
458
- public void assertDeleteContent (Store store , DirectoryService service ) throws IOException {
455
+ public void assertDeleteContent (Store store , Directory dir ) throws IOException {
459
456
deleteContent (store .directory ());
460
457
assertThat (Arrays .toString (store .directory ().listAll ()), store .directory ().listAll ().length , equalTo (0 ));
461
458
assertThat (store .stats ().sizeInBytes (), equalTo (0L ));
462
- assertThat (service .newDirectory ().listAll ().length , equalTo (0 ));
463
- }
464
-
465
- private static final class LuceneManagedDirectoryService extends DirectoryService {
466
- private final Directory dir ;
467
- private final Random random ;
468
-
469
- LuceneManagedDirectoryService (Random random ) {
470
- this (random , true );
471
- }
472
-
473
- LuceneManagedDirectoryService (Random random , boolean preventDoubleWrite ) {
474
- super (new ShardId (INDEX_SETTINGS .getIndex (), 1 ), INDEX_SETTINGS );
475
- dir = StoreTests .newDirectory (random );
476
- this .random = random ;
477
- }
478
-
479
- @ Override
480
- public Directory newDirectory () throws IOException {
481
- return dir ;
482
- }
483
-
459
+ assertThat (dir .listAll ().length , equalTo (0 ));
484
460
}
485
461
486
462
public static void assertConsistent (Store store , Store .MetadataSnapshot metadata ) throws IOException {
@@ -511,8 +487,7 @@ public void testRecoveryDiff() throws IOException, InterruptedException {
511
487
iwc .setMergePolicy (NoMergePolicy .INSTANCE );
512
488
iwc .setUseCompoundFile (random .nextBoolean ());
513
489
final ShardId shardId = new ShardId ("index" , "_na_" , 1 );
514
- DirectoryService directoryService = new LuceneManagedDirectoryService (random );
515
- Store store = new Store (shardId , INDEX_SETTINGS , directoryService , new DummyShardLock (shardId ));
490
+ Store store = new Store (shardId , INDEX_SETTINGS , StoreTests .newDirectory (random ()), new DummyShardLock (shardId ));
516
491
IndexWriter writer = new IndexWriter (store .directory (), iwc );
517
492
final boolean lotsOfSegments = rarely (random );
518
493
for (Document d : docs ) {
@@ -526,7 +501,7 @@ public void testRecoveryDiff() throws IOException, InterruptedException {
526
501
writer .commit ();
527
502
writer .close ();
528
503
first = store .getMetadata (null );
529
- assertDeleteContent (store , directoryService );
504
+ assertDeleteContent (store , store . directory () );
530
505
store .close ();
531
506
}
532
507
long time = new Date ().getTime ();
@@ -541,8 +516,7 @@ public void testRecoveryDiff() throws IOException, InterruptedException {
541
516
iwc .setMergePolicy (NoMergePolicy .INSTANCE );
542
517
iwc .setUseCompoundFile (random .nextBoolean ());
543
518
final ShardId shardId = new ShardId ("index" , "_na_" , 1 );
544
- DirectoryService directoryService = new LuceneManagedDirectoryService (random );
545
- store = new Store (shardId , INDEX_SETTINGS , directoryService , new DummyShardLock (shardId ));
519
+ store = new Store (shardId , INDEX_SETTINGS , StoreTests .newDirectory (random ()), new DummyShardLock (shardId ));
546
520
IndexWriter writer = new IndexWriter (store .directory (), iwc );
547
521
final boolean lotsOfSegments = rarely (random );
548
522
for (Document d : docs ) {
@@ -639,8 +613,7 @@ public void testRecoveryDiff() throws IOException, InterruptedException {
639
613
640
614
public void testCleanupFromSnapshot () throws IOException {
641
615
final ShardId shardId = new ShardId ("index" , "_na_" , 1 );
642
- DirectoryService directoryService = new LuceneManagedDirectoryService (random ());
643
- Store store = new Store (shardId , INDEX_SETTINGS , directoryService , new DummyShardLock (shardId ));
616
+ Store store = new Store (shardId , INDEX_SETTINGS , StoreTests .newDirectory (random ()), new DummyShardLock (shardId ));
644
617
// this time random codec....
645
618
IndexWriterConfig indexWriterConfig = newIndexWriterConfig (random (), new MockAnalyzer (random ())).setCodec (TestUtil .getDefaultCodec ());
646
619
// we keep all commits and that allows us clean based on multiple snapshots
@@ -727,11 +700,10 @@ public void testCleanupFromSnapshot() throws IOException {
727
700
728
701
public void testOnCloseCallback () throws IOException {
729
702
final ShardId shardId = new ShardId (new Index (randomRealisticUnicodeOfCodepointLengthBetween (1 , 10 ), "_na_" ), randomIntBetween (0 , 100 ));
730
- DirectoryService directoryService = new LuceneManagedDirectoryService (random ());
731
703
final AtomicInteger count = new AtomicInteger (0 );
732
704
final ShardLock lock = new DummyShardLock (shardId );
733
705
734
- Store store = new Store (shardId , INDEX_SETTINGS , directoryService , lock , theLock -> {
706
+ Store store = new Store (shardId , INDEX_SETTINGS , StoreTests . newDirectory ( random ()) , lock , theLock -> {
735
707
assertEquals (shardId , theLock .getShardId ());
736
708
assertEquals (lock , theLock );
737
709
count .incrementAndGet ();
@@ -748,11 +720,10 @@ public void testOnCloseCallback() throws IOException {
748
720
749
721
public void testStoreStats () throws IOException {
750
722
final ShardId shardId = new ShardId ("index" , "_na_" , 1 );
751
- DirectoryService directoryService = new LuceneManagedDirectoryService (random ());
752
723
Settings settings = Settings .builder ()
753
724
.put (IndexMetaData .SETTING_VERSION_CREATED , org .elasticsearch .Version .CURRENT )
754
725
.put (Store .INDEX_STORE_STATS_REFRESH_INTERVAL_SETTING .getKey (), TimeValue .timeValueMinutes (0 )).build ();
755
- Store store = new Store (shardId , IndexSettingsModule .newIndexSettings ("index" , settings ), directoryService ,
726
+ Store store = new Store (shardId , IndexSettingsModule .newIndexSettings ("index" , settings ), StoreTests . newDirectory ( random ()) ,
756
727
new DummyShardLock (shardId ));
757
728
long initialStoreSize = 0 ;
758
729
for (String extraFiles : store .directory ().listAll ()) {
@@ -843,8 +814,7 @@ protected Store.MetadataSnapshot createMetaDataSnapshot() {
843
814
844
815
public void testUserDataRead () throws IOException {
845
816
final ShardId shardId = new ShardId ("index" , "_na_" , 1 );
846
- DirectoryService directoryService = new LuceneManagedDirectoryService (random ());
847
- Store store = new Store (shardId , INDEX_SETTINGS , directoryService , new DummyShardLock (shardId ));
817
+ Store store = new Store (shardId , INDEX_SETTINGS , StoreTests .newDirectory (random ()), new DummyShardLock (shardId ));
848
818
IndexWriterConfig config = newIndexWriterConfig (random (), new MockAnalyzer (random ())).setCodec (TestUtil .getDefaultCodec ());
849
819
SnapshotDeletionPolicy deletionPolicy = new SnapshotDeletionPolicy (new KeepOnlyLastCommitDeletionPolicy ());
850
820
config .setIndexDeletionPolicy (deletionPolicy );
@@ -867,7 +837,7 @@ public void testUserDataRead() throws IOException {
867
837
assertThat (metadata .getCommitUserData ().get (Engine .SYNC_COMMIT_ID ), equalTo (syncId ));
868
838
assertThat (metadata .getCommitUserData ().get (Translog .TRANSLOG_GENERATION_KEY ), equalTo (translogId ));
869
839
TestUtil .checkIndex (store .directory ());
870
- assertDeleteContent (store , directoryService );
840
+ assertDeleteContent (store , store . directory () );
871
841
IOUtils .close (store );
872
842
}
873
843
@@ -893,8 +863,7 @@ public void testStreamStoreFilesMetaData() throws Exception {
893
863
public void testMarkCorruptedOnTruncatedSegmentsFile () throws IOException {
894
864
IndexWriterConfig iwc = newIndexWriterConfig ();
895
865
final ShardId shardId = new ShardId ("index" , "_na_" , 1 );
896
- DirectoryService directoryService = new LuceneManagedDirectoryService (random ());
897
- Store store = new Store (shardId , INDEX_SETTINGS , directoryService , new DummyShardLock (shardId ));
866
+ Store store = new Store (shardId , INDEX_SETTINGS , StoreTests .newDirectory (random ()), new DummyShardLock (shardId ));
898
867
IndexWriter writer = new IndexWriter (store .directory (), iwc );
899
868
900
869
int numDocs = 1 + random ().nextInt (10 );
@@ -945,15 +914,7 @@ public void testCanOpenIndex() throws IOException {
945
914
writer .commit ();
946
915
writer .close ();
947
916
assertTrue (Store .canOpenIndex (logger , tempDir , shardId , (id , l ) -> new DummyShardLock (id )));
948
-
949
- DirectoryService directoryService = new DirectoryService (shardId , INDEX_SETTINGS ) {
950
-
951
- @ Override
952
- public Directory newDirectory () throws IOException {
953
- return dir ;
954
- }
955
- };
956
- Store store = new Store (shardId , INDEX_SETTINGS , directoryService , new DummyShardLock (shardId ));
917
+ Store store = new Store (shardId , INDEX_SETTINGS , dir , new DummyShardLock (shardId ));
957
918
store .markStoreCorrupted (new CorruptIndexException ("foo" , "bar" ));
958
919
assertFalse (Store .canOpenIndex (logger , tempDir , shardId , (id , l ) -> new DummyShardLock (id )));
959
920
store .close ();
@@ -962,14 +923,7 @@ public Directory newDirectory() throws IOException {
962
923
public void testDeserializeCorruptionException () throws IOException {
963
924
final ShardId shardId = new ShardId ("index" , "_na_" , 1 );
964
925
final Directory dir = new RAMDirectory (); // I use ram dir to prevent that virusscanner being a PITA
965
- DirectoryService directoryService = new DirectoryService (shardId , INDEX_SETTINGS ) {
966
-
967
- @ Override
968
- public Directory newDirectory () throws IOException {
969
- return dir ;
970
- }
971
- };
972
- Store store = new Store (shardId , INDEX_SETTINGS , directoryService , new DummyShardLock (shardId ));
926
+ Store store = new Store (shardId , INDEX_SETTINGS , dir , new DummyShardLock (shardId ));
973
927
CorruptIndexException ex = new CorruptIndexException ("foo" , "bar" );
974
928
store .markStoreCorrupted (ex );
975
929
try {
@@ -998,14 +952,7 @@ public Directory newDirectory() throws IOException {
998
952
public void testCanReadOldCorruptionMarker () throws IOException {
999
953
final ShardId shardId = new ShardId ("index" , "_na_" , 1 );
1000
954
final Directory dir = new RAMDirectory (); // I use ram dir to prevent that virusscanner being a PITA
1001
- DirectoryService directoryService = new DirectoryService (shardId , INDEX_SETTINGS ) {
1002
-
1003
- @ Override
1004
- public Directory newDirectory () throws IOException {
1005
- return dir ;
1006
- }
1007
- };
1008
- Store store = new Store (shardId , INDEX_SETTINGS , directoryService , new DummyShardLock (shardId ));
955
+ Store store = new Store (shardId , INDEX_SETTINGS , dir , new DummyShardLock (shardId ));
1009
956
1010
957
CorruptIndexException exception = new CorruptIndexException ("foo" , "bar" );
1011
958
String uuid = Store .CORRUPTED + UUIDs .randomBase64UUID ();
@@ -1065,8 +1012,7 @@ public Directory newDirectory() throws IOException {
1065
1012
1066
1013
public void testEnsureIndexHasHistoryUUID () throws IOException {
1067
1014
final ShardId shardId = new ShardId ("index" , "_na_" , 1 );
1068
- DirectoryService directoryService = new LuceneManagedDirectoryService (random ());
1069
- try (Store store = new Store (shardId , INDEX_SETTINGS , directoryService , new DummyShardLock (shardId ))) {
1015
+ try (Store store = new Store (shardId , INDEX_SETTINGS , StoreTests .newDirectory (random ()), new DummyShardLock (shardId ))) {
1070
1016
1071
1017
store .createEmpty ();
1072
1018
@@ -1098,8 +1044,7 @@ public void testEnsureIndexHasHistoryUUID() throws IOException {
1098
1044
1099
1045
public void testHistoryUUIDCanBeForced () throws IOException {
1100
1046
final ShardId shardId = new ShardId ("index" , "_na_" , 1 );
1101
- DirectoryService directoryService = new LuceneManagedDirectoryService (random ());
1102
- try (Store store = new Store (shardId , INDEX_SETTINGS , directoryService , new DummyShardLock (shardId ))) {
1047
+ try (Store store = new Store (shardId , INDEX_SETTINGS , StoreTests .newDirectory (random ()), new DummyShardLock (shardId ))) {
1103
1048
1104
1049
store .createEmpty ();
1105
1050
0 commit comments