@@ -108,12 +108,10 @@ public class StoreTests extends ESTestCase {
108
108
private static final Version MIN_SUPPORTED_LUCENE_VERSION = org .elasticsearch .Version .CURRENT
109
109
.minimumIndexCompatibilityVersion ().luceneVersion ;
110
110
111
- public void testRefCount () throws IOException {
111
+ public void testRefCount () {
112
112
final ShardId shardId = new ShardId ("index" , "_na_" , 1 );
113
- DirectoryService directoryService = new LuceneManagedDirectoryService (random ());
114
113
IndexSettings indexSettings = INDEX_SETTINGS ;
115
-
116
- Store store = new Store (shardId , indexSettings , directoryService , new DummyShardLock (shardId ));
114
+ Store store = new Store (shardId , indexSettings , StoreTests .newDirectory (random ()), new DummyShardLock (shardId ));
117
115
int incs = randomIntBetween (1 , 100 );
118
116
for (int i = 0 ; i < incs ; i ++) {
119
117
if (randomBoolean ()) {
@@ -310,8 +308,7 @@ public void testVerifyingIndexOutputWithBogusInput() throws IOException {
310
308
311
309
public void testNewChecksums () throws IOException {
312
310
final ShardId shardId = new ShardId ("index" , "_na_" , 1 );
313
- DirectoryService directoryService = new LuceneManagedDirectoryService (random ());
314
- Store store = new Store (shardId , INDEX_SETTINGS , directoryService , new DummyShardLock (shardId ));
311
+ Store store = new Store (shardId , INDEX_SETTINGS , StoreTests .newDirectory (random ()), new DummyShardLock (shardId ));
315
312
// set default codec - all segments need checksums
316
313
IndexWriter writer = new IndexWriter (store .directory (), newIndexWriterConfig (random (), new MockAnalyzer (random ())).setCodec (TestUtil .getDefaultCodec ()));
317
314
int docs = 1 + random ().nextInt (100 );
@@ -361,7 +358,7 @@ public void testNewChecksums() throws IOException {
361
358
assertConsistent (store , metadata );
362
359
363
360
TestUtil .checkIndex (store .directory ());
364
- assertDeleteContent (store , directoryService );
361
+ assertDeleteContent (store , store . directory () );
365
362
IOUtils .close (store );
366
363
}
367
364
@@ -469,32 +466,11 @@ private void corruptFile(Directory dir, String fileIn, String fileOut) throws IO
469
466
470
467
}
471
468
472
- public void assertDeleteContent (Store store , DirectoryService service ) throws IOException {
469
+ public void assertDeleteContent (Store store , Directory dir ) throws IOException {
473
470
deleteContent (store .directory ());
474
471
assertThat (Arrays .toString (store .directory ().listAll ()), store .directory ().listAll ().length , equalTo (0 ));
475
472
assertThat (store .stats ().sizeInBytes (), equalTo (0L ));
476
- assertThat (service .newDirectory ().listAll ().length , equalTo (0 ));
477
- }
478
-
479
- private static final class LuceneManagedDirectoryService extends DirectoryService {
480
- private final Directory dir ;
481
- private final Random random ;
482
-
483
- LuceneManagedDirectoryService (Random random ) {
484
- this (random , true );
485
- }
486
-
487
- LuceneManagedDirectoryService (Random random , boolean preventDoubleWrite ) {
488
- super (new ShardId (INDEX_SETTINGS .getIndex (), 1 ), INDEX_SETTINGS );
489
- dir = StoreTests .newDirectory (random );
490
- this .random = random ;
491
- }
492
-
493
- @ Override
494
- public Directory newDirectory () throws IOException {
495
- return dir ;
496
- }
497
-
473
+ assertThat (dir .listAll ().length , equalTo (0 ));
498
474
}
499
475
500
476
public static void assertConsistent (Store store , Store .MetadataSnapshot metadata ) throws IOException {
@@ -525,8 +501,7 @@ public void testRecoveryDiff() throws IOException, InterruptedException {
525
501
iwc .setMergePolicy (NoMergePolicy .INSTANCE );
526
502
iwc .setUseCompoundFile (random .nextBoolean ());
527
503
final ShardId shardId = new ShardId ("index" , "_na_" , 1 );
528
- DirectoryService directoryService = new LuceneManagedDirectoryService (random );
529
- Store store = new Store (shardId , INDEX_SETTINGS , directoryService , new DummyShardLock (shardId ));
504
+ Store store = new Store (shardId , INDEX_SETTINGS , StoreTests .newDirectory (random ()), new DummyShardLock (shardId ));
530
505
IndexWriter writer = new IndexWriter (store .directory (), iwc );
531
506
final boolean lotsOfSegments = rarely (random );
532
507
for (Document d : docs ) {
@@ -540,7 +515,7 @@ public void testRecoveryDiff() throws IOException, InterruptedException {
540
515
writer .commit ();
541
516
writer .close ();
542
517
first = store .getMetadata (null );
543
- assertDeleteContent (store , directoryService );
518
+ assertDeleteContent (store , store . directory () );
544
519
store .close ();
545
520
}
546
521
long time = new Date ().getTime ();
@@ -555,8 +530,7 @@ public void testRecoveryDiff() throws IOException, InterruptedException {
555
530
iwc .setMergePolicy (NoMergePolicy .INSTANCE );
556
531
iwc .setUseCompoundFile (random .nextBoolean ());
557
532
final ShardId shardId = new ShardId ("index" , "_na_" , 1 );
558
- DirectoryService directoryService = new LuceneManagedDirectoryService (random );
559
- store = new Store (shardId , INDEX_SETTINGS , directoryService , new DummyShardLock (shardId ));
533
+ store = new Store (shardId , INDEX_SETTINGS , StoreTests .newDirectory (random ()), new DummyShardLock (shardId ));
560
534
IndexWriter writer = new IndexWriter (store .directory (), iwc );
561
535
final boolean lotsOfSegments = rarely (random );
562
536
for (Document d : docs ) {
@@ -653,8 +627,7 @@ public void testRecoveryDiff() throws IOException, InterruptedException {
653
627
654
628
public void testCleanupFromSnapshot () throws IOException {
655
629
final ShardId shardId = new ShardId ("index" , "_na_" , 1 );
656
- DirectoryService directoryService = new LuceneManagedDirectoryService (random ());
657
- Store store = new Store (shardId , INDEX_SETTINGS , directoryService , new DummyShardLock (shardId ));
630
+ Store store = new Store (shardId , INDEX_SETTINGS , StoreTests .newDirectory (random ()), new DummyShardLock (shardId ));
658
631
// this time random codec....
659
632
IndexWriterConfig indexWriterConfig = newIndexWriterConfig (random (), new MockAnalyzer (random ())).setCodec (TestUtil .getDefaultCodec ());
660
633
// we keep all commits and that allows us clean based on multiple snapshots
@@ -741,11 +714,10 @@ public void testCleanupFromSnapshot() throws IOException {
741
714
742
715
public void testOnCloseCallback () throws IOException {
743
716
final ShardId shardId = new ShardId (new Index (randomRealisticUnicodeOfCodepointLengthBetween (1 , 10 ), "_na_" ), randomIntBetween (0 , 100 ));
744
- DirectoryService directoryService = new LuceneManagedDirectoryService (random ());
745
717
final AtomicInteger count = new AtomicInteger (0 );
746
718
final ShardLock lock = new DummyShardLock (shardId );
747
719
748
- Store store = new Store (shardId , INDEX_SETTINGS , directoryService , lock , theLock -> {
720
+ Store store = new Store (shardId , INDEX_SETTINGS , StoreTests . newDirectory ( random ()) , lock , theLock -> {
749
721
assertEquals (shardId , theLock .getShardId ());
750
722
assertEquals (lock , theLock );
751
723
count .incrementAndGet ();
@@ -762,11 +734,10 @@ public void testOnCloseCallback() throws IOException {
762
734
763
735
public void testStoreStats () throws IOException {
764
736
final ShardId shardId = new ShardId ("index" , "_na_" , 1 );
765
- DirectoryService directoryService = new LuceneManagedDirectoryService (random ());
766
737
Settings settings = Settings .builder ()
767
738
.put (IndexMetaData .SETTING_VERSION_CREATED , org .elasticsearch .Version .CURRENT )
768
739
.put (Store .INDEX_STORE_STATS_REFRESH_INTERVAL_SETTING .getKey (), TimeValue .timeValueMinutes (0 )).build ();
769
- Store store = new Store (shardId , IndexSettingsModule .newIndexSettings ("index" , settings ), directoryService ,
740
+ Store store = new Store (shardId , IndexSettingsModule .newIndexSettings ("index" , settings ), StoreTests . newDirectory ( random ()) ,
770
741
new DummyShardLock (shardId ));
771
742
long initialStoreSize = 0 ;
772
743
for (String extraFiles : store .directory ().listAll ()) {
@@ -857,8 +828,7 @@ protected Store.MetadataSnapshot createMetaDataSnapshot() {
857
828
858
829
public void testUserDataRead () throws IOException {
859
830
final ShardId shardId = new ShardId ("index" , "_na_" , 1 );
860
- DirectoryService directoryService = new LuceneManagedDirectoryService (random ());
861
- Store store = new Store (shardId , INDEX_SETTINGS , directoryService , new DummyShardLock (shardId ));
831
+ Store store = new Store (shardId , INDEX_SETTINGS , StoreTests .newDirectory (random ()), new DummyShardLock (shardId ));
862
832
IndexWriterConfig config = newIndexWriterConfig (random (), new MockAnalyzer (random ())).setCodec (TestUtil .getDefaultCodec ());
863
833
SnapshotDeletionPolicy deletionPolicy = new SnapshotDeletionPolicy (new KeepOnlyLastCommitDeletionPolicy ());
864
834
config .setIndexDeletionPolicy (deletionPolicy );
@@ -881,7 +851,7 @@ public void testUserDataRead() throws IOException {
881
851
assertThat (metadata .getCommitUserData ().get (Engine .SYNC_COMMIT_ID ), equalTo (syncId ));
882
852
assertThat (metadata .getCommitUserData ().get (Translog .TRANSLOG_GENERATION_KEY ), equalTo (translogId ));
883
853
TestUtil .checkIndex (store .directory ());
884
- assertDeleteContent (store , directoryService );
854
+ assertDeleteContent (store , store . directory () );
885
855
IOUtils .close (store );
886
856
}
887
857
@@ -907,8 +877,7 @@ public void testStreamStoreFilesMetaData() throws Exception {
907
877
public void testMarkCorruptedOnTruncatedSegmentsFile () throws IOException {
908
878
IndexWriterConfig iwc = newIndexWriterConfig ();
909
879
final ShardId shardId = new ShardId ("index" , "_na_" , 1 );
910
- DirectoryService directoryService = new LuceneManagedDirectoryService (random ());
911
- Store store = new Store (shardId , INDEX_SETTINGS , directoryService , new DummyShardLock (shardId ));
880
+ Store store = new Store (shardId , INDEX_SETTINGS , StoreTests .newDirectory (random ()), new DummyShardLock (shardId ));
912
881
IndexWriter writer = new IndexWriter (store .directory (), iwc );
913
882
914
883
int numDocs = 1 + random ().nextInt (10 );
@@ -959,15 +928,7 @@ public void testCanOpenIndex() throws IOException {
959
928
writer .commit ();
960
929
writer .close ();
961
930
assertTrue (Store .canOpenIndex (logger , tempDir , shardId , (id , l ) -> new DummyShardLock (id )));
962
-
963
- DirectoryService directoryService = new DirectoryService (shardId , INDEX_SETTINGS ) {
964
-
965
- @ Override
966
- public Directory newDirectory () throws IOException {
967
- return dir ;
968
- }
969
- };
970
- Store store = new Store (shardId , INDEX_SETTINGS , directoryService , new DummyShardLock (shardId ));
931
+ Store store = new Store (shardId , INDEX_SETTINGS , dir , new DummyShardLock (shardId ));
971
932
store .markStoreCorrupted (new CorruptIndexException ("foo" , "bar" ));
972
933
assertFalse (Store .canOpenIndex (logger , tempDir , shardId , (id , l ) -> new DummyShardLock (id )));
973
934
store .close ();
@@ -976,14 +937,7 @@ public Directory newDirectory() throws IOException {
976
937
public void testDeserializeCorruptionException () throws IOException {
977
938
final ShardId shardId = new ShardId ("index" , "_na_" , 1 );
978
939
final Directory dir = new RAMDirectory (); // I use ram dir to prevent that virusscanner being a PITA
979
- DirectoryService directoryService = new DirectoryService (shardId , INDEX_SETTINGS ) {
980
-
981
- @ Override
982
- public Directory newDirectory () throws IOException {
983
- return dir ;
984
- }
985
- };
986
- Store store = new Store (shardId , INDEX_SETTINGS , directoryService , new DummyShardLock (shardId ));
940
+ Store store = new Store (shardId , INDEX_SETTINGS , dir , new DummyShardLock (shardId ));
987
941
CorruptIndexException ex = new CorruptIndexException ("foo" , "bar" );
988
942
store .markStoreCorrupted (ex );
989
943
try {
@@ -1012,14 +966,7 @@ public Directory newDirectory() throws IOException {
1012
966
public void testCanReadOldCorruptionMarker () throws IOException {
1013
967
final ShardId shardId = new ShardId ("index" , "_na_" , 1 );
1014
968
final Directory dir = new RAMDirectory (); // I use ram dir to prevent that virusscanner being a PITA
1015
- DirectoryService directoryService = new DirectoryService (shardId , INDEX_SETTINGS ) {
1016
-
1017
- @ Override
1018
- public Directory newDirectory () throws IOException {
1019
- return dir ;
1020
- }
1021
- };
1022
- Store store = new Store (shardId , INDEX_SETTINGS , directoryService , new DummyShardLock (shardId ));
969
+ Store store = new Store (shardId , INDEX_SETTINGS , dir , new DummyShardLock (shardId ));
1023
970
1024
971
CorruptIndexException exception = new CorruptIndexException ("foo" , "bar" );
1025
972
String uuid = Store .CORRUPTED + UUIDs .randomBase64UUID ();
@@ -1079,8 +1026,7 @@ public Directory newDirectory() throws IOException {
1079
1026
1080
1027
public void testEnsureIndexHas6xCommitTags () throws IOException {
1081
1028
final ShardId shardId = new ShardId ("index" , "_na_" , 1 );
1082
- DirectoryService directoryService = new LuceneManagedDirectoryService (random ());
1083
- try (Store store = new Store (shardId , INDEX_SETTINGS , directoryService , new DummyShardLock (shardId ))) {
1029
+ try (Store store = new Store (shardId , INDEX_SETTINGS , StoreTests .newDirectory (random ()), new DummyShardLock (shardId ))) {
1084
1030
store .createEmpty ();
1085
1031
// remove the history uuid, seqno markers
1086
1032
try (IndexWriter writer = openWriter (store )) {
@@ -1145,8 +1091,7 @@ public void testEnsureIndexHas6xCommitTags() throws IOException {
1145
1091
1146
1092
public void testHistoryUUIDCanBeForced () throws IOException {
1147
1093
final ShardId shardId = new ShardId ("index" , "_na_" , 1 );
1148
- DirectoryService directoryService = new LuceneManagedDirectoryService (random ());
1149
- try (Store store = new Store (shardId , INDEX_SETTINGS , directoryService , new DummyShardLock (shardId ))) {
1094
+ try (Store store = new Store (shardId , INDEX_SETTINGS , StoreTests .newDirectory (random ()), new DummyShardLock (shardId ))) {
1150
1095
1151
1096
store .createEmpty ();
1152
1097
0 commit comments