45
45
import org .opensearch .core .index .shard .ShardId ;
46
46
import org .opensearch .gateway .MetadataStateFormat ;
47
47
import org .opensearch .index .IndexSettings ;
48
+ import org .opensearch .index .store .IndexStoreListener ;
48
49
import org .opensearch .node .Node ;
49
50
import org .opensearch .test .IndexSettingsModule ;
50
51
import org .opensearch .test .NodeRoles ;
65
66
import java .util .concurrent .atomic .AtomicInteger ;
66
67
import java .util .concurrent .atomic .AtomicReference ;
67
68
68
- import static org .opensearch .test .NodeRoles .nonClusterManagerNode ;
69
- import static org .opensearch .test .NodeRoles .nonDataNode ;
70
69
import static org .hamcrest .CoreMatchers .equalTo ;
71
70
import static org .hamcrest .Matchers .arrayWithSize ;
72
71
import static org .hamcrest .Matchers .containsString ;
73
72
import static org .hamcrest .Matchers .empty ;
74
73
import static org .hamcrest .Matchers .not ;
75
74
import static org .hamcrest .Matchers .startsWith ;
75
+ import static org .opensearch .test .NodeRoles .nonClusterManagerNode ;
76
+ import static org .opensearch .test .NodeRoles .nonDataNode ;
76
77
77
78
@ LuceneTestCase .SuppressFileSystems ("ExtrasFS" ) // TODO: fix test to allow extras
78
79
public class NodeEnvironmentTests extends OpenSearchTestCase {
@@ -360,24 +361,39 @@ protected void doRun() throws Exception {
360
361
}
361
362
362
363
public void testIndexStoreListener () throws Exception {
363
- final AtomicInteger shardCounter = new AtomicInteger (0 );
364
- final AtomicInteger indexCounter = new AtomicInteger (0 );
364
+ final AtomicInteger shardCounter1 = new AtomicInteger (0 );
365
+ final AtomicInteger shardCounter2 = new AtomicInteger (0 );
366
+ final AtomicInteger indexCounter1 = new AtomicInteger (0 );
367
+ final AtomicInteger indexCounter2 = new AtomicInteger (0 );
365
368
final Index index = new Index ("foo" , "fooUUID" );
366
369
final ShardId shardId = new ShardId (index , 0 );
367
- final NodeEnvironment .IndexStoreListener listener = new NodeEnvironment .IndexStoreListener () {
370
+ final IndexStoreListener listener1 = new IndexStoreListener () {
371
+ @ Override
372
+ public void beforeShardPathDeleted (ShardId inShardId , IndexSettings indexSettings , NodeEnvironment env ) {
373
+ assertEquals (shardId , inShardId );
374
+ shardCounter1 .incrementAndGet ();
375
+ }
376
+
377
+ @ Override
378
+ public void beforeIndexPathDeleted (Index inIndex , IndexSettings indexSettings , NodeEnvironment env ) {
379
+ assertEquals (index , inIndex );
380
+ indexCounter1 .incrementAndGet ();
381
+ }
382
+ };
383
+ final IndexStoreListener listener2 = new IndexStoreListener () {
368
384
@ Override
369
385
public void beforeShardPathDeleted (ShardId inShardId , IndexSettings indexSettings , NodeEnvironment env ) {
370
386
assertEquals (shardId , inShardId );
371
- shardCounter .incrementAndGet ();
387
+ shardCounter2 .incrementAndGet ();
372
388
}
373
389
374
390
@ Override
375
391
public void beforeIndexPathDeleted (Index inIndex , IndexSettings indexSettings , NodeEnvironment env ) {
376
392
assertEquals (index , inIndex );
377
- indexCounter .incrementAndGet ();
393
+ indexCounter2 .incrementAndGet ();
378
394
}
379
395
};
380
- final NodeEnvironment env = newNodeEnvironment (listener );
396
+ final NodeEnvironment env = newNodeEnvironment (new IndexStoreListener . CompositeIndexStoreListener ( List . of ( listener1 , listener2 )) );
381
397
382
398
for (Path path : env .indexPaths (index )) {
383
399
Files .createDirectories (path .resolve ("0" ));
@@ -386,26 +402,30 @@ public void beforeIndexPathDeleted(Index inIndex, IndexSettings indexSettings, N
386
402
for (Path path : env .indexPaths (index )) {
387
403
assertTrue (Files .exists (path .resolve ("0" )));
388
404
}
389
- assertEquals (0 , shardCounter .get ());
405
+ assertEquals (0 , shardCounter1 .get ());
406
+ assertEquals (0 , shardCounter2 .get ());
390
407
391
408
env .deleteShardDirectorySafe (new ShardId (index , 0 ), idxSettings );
392
409
393
410
for (Path path : env .indexPaths (index )) {
394
411
assertFalse (Files .exists (path .resolve ("0" )));
395
412
}
396
- assertEquals (1 , shardCounter .get ());
413
+ assertEquals (1 , shardCounter1 .get ());
414
+ assertEquals (1 , shardCounter2 .get ());
397
415
398
416
for (Path path : env .indexPaths (index )) {
399
417
assertTrue (Files .exists (path ));
400
418
}
401
- assertEquals (0 , indexCounter .get ());
419
+ assertEquals (0 , indexCounter1 .get ());
420
+ assertEquals (0 , indexCounter2 .get ());
402
421
403
422
env .deleteIndexDirectorySafe (index , 5000 , idxSettings );
404
423
405
424
for (Path path : env .indexPaths (index )) {
406
425
assertFalse (Files .exists (path ));
407
426
}
408
- assertEquals (1 , indexCounter .get ());
427
+ assertEquals (1 , indexCounter1 .get ());
428
+ assertEquals (1 , indexCounter2 .get ());
409
429
assertTrue ("LockedShards: " + env .lockedShards (), env .lockedShards ().isEmpty ());
410
430
env .close ();
411
431
}
@@ -680,7 +700,7 @@ public NodeEnvironment newNodeEnvironment() throws IOException {
680
700
return newNodeEnvironment (Settings .EMPTY );
681
701
}
682
702
683
- public NodeEnvironment newNodeEnvironment (NodeEnvironment . IndexStoreListener listener ) throws IOException {
703
+ public NodeEnvironment newNodeEnvironment (IndexStoreListener listener ) throws IOException {
684
704
Settings build = buildEnvSettings (Settings .EMPTY );
685
705
return new NodeEnvironment (build , TestEnvironment .newEnvironment (build ), listener );
686
706
}
0 commit comments