2020import static org .apache .hadoop .hbase .HConstants .BUCKET_CACHE_IOENGINE_KEY ;
2121import static org .apache .hadoop .hbase .HConstants .BUCKET_CACHE_SIZE_KEY ;
2222import static org .apache .hadoop .hbase .io .hfile .CacheConfig .CACHE_BLOCKS_ON_WRITE_KEY ;
23+ import static org .apache .hadoop .hbase .io .hfile .CacheConfig .EVICT_BLOCKS_ON_CLOSE_KEY ;
2324import static org .apache .hadoop .hbase .io .hfile .CacheConfig .EVICT_BLOCKS_ON_SPLIT_KEY ;
2425import static org .apache .hadoop .hbase .io .hfile .CacheConfig .PREFETCH_BLOCKS_ON_OPEN_KEY ;
2526import static org .junit .Assert .assertTrue ;
2627
28+ import java .io .IOException ;
2729import java .util .ArrayList ;
2830import java .util .Collection ;
2931import java .util .List ;
4749import org .slf4j .LoggerFactory ;
4850
4951@ Category ({ MiscTests .class , MediumTests .class })
50- public class TestSplitWithCache {
52+ public class TestCacheEviction {
5153
5254 @ ClassRule
5355 public static final HBaseClassTestRule CLASS_RULE =
54- HBaseClassTestRule .forClass (TestSplitWithCache .class );
56+ HBaseClassTestRule .forClass (TestCacheEviction .class );
5557
56- private static final Logger LOG = LoggerFactory .getLogger (TestSplitWithCache .class );
58+ private static final Logger LOG = LoggerFactory .getLogger (TestCacheEviction .class );
5759
5860 private static final HBaseTestingUtil UTIL = new HBaseTestingUtil ();
5961
@@ -69,42 +71,44 @@ public static void setUp() throws Exception {
6971
7072 @ Test
7173 public void testEvictOnSplit () throws Exception {
72- doTest ("testEvictOnSplit" , true ,
74+ doTestEvictOnSplit ("testEvictOnSplit" , true ,
7375 (f , m ) -> Waiter .waitFor (UTIL .getConfiguration (), 1000 , () -> m .get (f ) != null ),
7476 (f , m ) -> Waiter .waitFor (UTIL .getConfiguration (), 1000 , () -> m .get (f ) == null ));
7577 }
7678
7779 @ Test
7880 public void testDoesntEvictOnSplit () throws Exception {
79- doTest ("testDoesntEvictOnSplit" , false ,
81+ doTestEvictOnSplit ("testDoesntEvictOnSplit" , false ,
8082 (f , m ) -> Waiter .waitFor (UTIL .getConfiguration (), 1000 , () -> m .get (f ) != null ),
8183 (f , m ) -> Waiter .waitFor (UTIL .getConfiguration (), 1000 , () -> m .get (f ) != null ));
8284 }
8385
84- private void doTest (String table , boolean evictOnSplit ,
86+ @ Test
87+ public void testEvictOnClose () throws Exception {
88+ doTestEvictOnClose ("testEvictOnClose" , true ,
89+ (f , m ) -> Waiter .waitFor (UTIL .getConfiguration (), 1000 , () -> m .get (f ) != null ),
90+ (f , m ) -> Waiter .waitFor (UTIL .getConfiguration (), 1000 , () -> m .get (f ) == null ));
91+ }
92+
93+ @ Test
94+ public void testDoesntEvictOnClose () throws Exception {
95+ doTestEvictOnClose ("testDoesntEvictOnClose" , false ,
96+ (f , m ) -> Waiter .waitFor (UTIL .getConfiguration (), 1000 , () -> m .get (f ) != null ),
97+ (f , m ) -> Waiter .waitFor (UTIL .getConfiguration (), 1000 , () -> m .get (f ) != null ));
98+ }
99+
100+ private void doTestEvictOnSplit (String table , boolean evictOnSplit ,
85101 BiConsumer <String , Map <String , Pair <String , Long >>> predicateBeforeSplit ,
86102 BiConsumer <String , Map <String , Pair <String , Long >>> predicateAfterSplit ) throws Exception {
87- UTIL .getConfiguration ().setBoolean (EVICT_BLOCKS_ON_SPLIT_KEY , evictOnSplit );
88103 UTIL .startMiniCluster (1 );
89104 try {
90105 TableName tableName = TableName .valueOf (table );
91- byte [] family = Bytes .toBytes ("CF" );
92- TableDescriptor td = TableDescriptorBuilder .newBuilder (tableName )
93- .setColumnFamily (ColumnFamilyDescriptorBuilder .of (family )).build ();
94- UTIL .getAdmin ().createTable (td );
95- UTIL .waitTableAvailable (tableName );
96- Table tbl = UTIL .getConnection ().getTable (tableName );
97- List <Put > puts = new ArrayList <>();
98- for (int i = 0 ; i < 1000 ; i ++) {
99- Put p = new Put (Bytes .toBytes ("row-" + i ));
100- p .addColumn (family , Bytes .toBytes (1 ), Bytes .toBytes ("val-" + i ));
101- puts .add (p );
102- }
103- tbl .put (puts );
104- UTIL .getAdmin ().flush (tableName );
106+ createAndCacheTable (tableName );
105107 Collection <HStoreFile > files =
106108 UTIL .getMiniHBaseCluster ().getRegions (tableName ).get (0 ).getStores ().get (0 ).getStorefiles ();
107109 checkCacheForBlocks (tableName , files , predicateBeforeSplit );
110+ UTIL .getMiniHBaseCluster ().getRegionServer (0 ).getConfiguration ()
111+ .setBoolean (EVICT_BLOCKS_ON_SPLIT_KEY , evictOnSplit );
108112 UTIL .getAdmin ().split (tableName , Bytes .toBytes ("row-500" ));
109113 Waiter .waitFor (UTIL .getConfiguration (), 30000 ,
110114 () -> UTIL .getMiniHBaseCluster ().getRegions (tableName ).size () == 2 );
@@ -113,7 +117,43 @@ private void doTest(String table, boolean evictOnSplit,
113117 } finally {
114118 UTIL .shutdownMiniCluster ();
115119 }
120+ }
116121
122+ private void doTestEvictOnClose (String table , boolean evictOnClose ,
123+ BiConsumer <String , Map <String , Pair <String , Long >>> predicateBeforeClose ,
124+ BiConsumer <String , Map <String , Pair <String , Long >>> predicateAfterClose ) throws Exception {
125+ UTIL .startMiniCluster (1 );
126+ try {
127+ TableName tableName = TableName .valueOf (table );
128+ createAndCacheTable (tableName );
129+ Collection <HStoreFile > files =
130+ UTIL .getMiniHBaseCluster ().getRegions (tableName ).get (0 ).getStores ().get (0 ).getStorefiles ();
131+ checkCacheForBlocks (tableName , files , predicateBeforeClose );
132+ UTIL .getMiniHBaseCluster ().getRegionServer (0 ).getConfiguration ()
133+ .setBoolean (EVICT_BLOCKS_ON_CLOSE_KEY , evictOnClose );
134+ UTIL .getAdmin ().disableTable (tableName );
135+ UTIL .waitUntilNoRegionsInTransition ();
136+ checkCacheForBlocks (tableName , files , predicateAfterClose );
137+ } finally {
138+ UTIL .shutdownMiniCluster ();
139+ }
140+ }
141+
142+ private void createAndCacheTable (TableName tableName ) throws IOException , InterruptedException {
143+ byte [] family = Bytes .toBytes ("CF" );
144+ TableDescriptor td = TableDescriptorBuilder .newBuilder (tableName )
145+ .setColumnFamily (ColumnFamilyDescriptorBuilder .of (family )).build ();
146+ UTIL .getAdmin ().createTable (td );
147+ UTIL .waitTableAvailable (tableName );
148+ Table tbl = UTIL .getConnection ().getTable (tableName );
149+ List <Put > puts = new ArrayList <>();
150+ for (int i = 0 ; i < 1000 ; i ++) {
151+ Put p = new Put (Bytes .toBytes ("row-" + i ));
152+ p .addColumn (family , Bytes .toBytes (1 ), Bytes .toBytes ("val-" + i ));
153+ puts .add (p );
154+ }
155+ tbl .put (puts );
156+ UTIL .getAdmin ().flush (tableName );
117157 }
118158
119159 private void checkCacheForBlocks (TableName tableName , Collection <HStoreFile > files ,
0 commit comments