3737import org .apache .hadoop .hbase .client .TableDescriptor ;
3838import org .apache .hadoop .hbase .client .TableDescriptorBuilder ;
3939import org .apache .hadoop .hbase .regionserver .HStoreFile ;
40+ import org .apache .hadoop .hbase .regionserver .storefiletracker .StoreFileTrackerFactory ;
4041import org .apache .hadoop .hbase .testclassification .MediumTests ;
4142import org .apache .hadoop .hbase .testclassification .MiscTests ;
4243import org .apache .hadoop .hbase .util .Bytes ;
@@ -67,6 +68,7 @@ public static void setUp() throws Exception {
6768 UTIL .getConfiguration ().setBoolean (PREFETCH_BLOCKS_ON_OPEN_KEY , true );
6869 UTIL .getConfiguration ().set (BUCKET_CACHE_IOENGINE_KEY , "offheap" );
6970 UTIL .getConfiguration ().setInt (BUCKET_CACHE_SIZE_KEY , 200 );
71+ UTIL .getConfiguration ().set (StoreFileTrackerFactory .TRACKER_IMPL , "FILE" );
7072 }
7173
7274 @ Test
@@ -103,7 +105,7 @@ private void doTestEvictOnSplit(String table, boolean evictOnSplit,
103105 UTIL .startMiniCluster (1 );
104106 try {
105107 TableName tableName = TableName .valueOf (table );
106- createAndCacheTable (tableName );
108+ createTable (tableName , true );
107109 Collection <HStoreFile > files =
108110 UTIL .getMiniHBaseCluster ().getRegions (tableName ).get (0 ).getStores ().get (0 ).getStorefiles ();
109111 checkCacheForBlocks (tableName , files , predicateBeforeSplit );
@@ -125,7 +127,7 @@ private void doTestEvictOnClose(String table, boolean evictOnClose,
125127 UTIL .startMiniCluster (1 );
126128 try {
127129 TableName tableName = TableName .valueOf (table );
128- createAndCacheTable (tableName );
130+ createTable (tableName , true );
129131 Collection <HStoreFile > files =
130132 UTIL .getMiniHBaseCluster ().getRegions (tableName ).get (0 ).getStores ().get (0 ).getStorefiles ();
131133 checkCacheForBlocks (tableName , files , predicateBeforeClose );
@@ -139,7 +141,8 @@ private void doTestEvictOnClose(String table, boolean evictOnClose,
139141 }
140142 }
141143
142- private void createAndCacheTable (TableName tableName ) throws IOException , InterruptedException {
144+ private void createTable (TableName tableName , boolean shouldFlushTable )
145+ throws IOException , InterruptedException {
143146 byte [] family = Bytes .toBytes ("CF" );
144147 TableDescriptor td = TableDescriptorBuilder .newBuilder (tableName )
145148 .setColumnFamily (ColumnFamilyDescriptorBuilder .of (family )).build ();
@@ -153,7 +156,10 @@ private void createAndCacheTable(TableName tableName) throws IOException, Interr
153156 puts .add (p );
154157 }
155158 tbl .put (puts );
156- UTIL .getAdmin ().flush (tableName );
159+ if (shouldFlushTable ) {
160+ UTIL .getAdmin ().flush (tableName );
161+ Thread .sleep (5000 );
162+ }
157163 }
158164
159165 private void checkCacheForBlocks (TableName tableName , Collection <HStoreFile > files ,
@@ -167,4 +173,44 @@ private void checkCacheForBlocks(TableName tableName, Collection<HStoreFile> fil
167173 });
168174 });
169175 }
176+
177+ @ Test
178+ public void testNoCacheWithoutFlush () throws Exception {
179+ UTIL .startMiniCluster (1 );
180+ try {
181+ TableName tableName = TableName .valueOf ("tableNoCache" );
182+ createTable (tableName , false );
183+ checkRegionCached (tableName , false );
184+ } finally {
185+ UTIL .shutdownMiniCluster ();
186+ }
187+ }
188+
189+ @ Test
190+ public void testCacheWithFlush () throws Exception {
191+ UTIL .startMiniCluster (1 );
192+ try {
193+ TableName tableName = TableName .valueOf ("tableWithFlush" );
194+ createTable (tableName , true );
195+ checkRegionCached (tableName , true );
196+ } finally {
197+ UTIL .shutdownMiniCluster ();
198+ }
199+ }
200+
201+ private void checkRegionCached (TableName tableName , boolean isCached ) throws IOException {
202+ UTIL .getMiniHBaseCluster ().getRegions (tableName ).forEach (r -> {
203+ try {
204+ UTIL .getMiniHBaseCluster ().getClusterMetrics ().getLiveServerMetrics ().forEach ((sn , sm ) -> {
205+ for (Map .Entry <byte [], RegionMetrics > rm : sm .getRegionMetrics ().entrySet ()) {
206+ if (rm .getValue ().getNameAsString ().equals (r .getRegionInfo ().getRegionNameAsString ())) {
207+ assertTrue (isCached == (rm .getValue ().getCurrentRegionCachedRatio () > 0.0f ));
208+ }
209+ }
210+ });
211+ } catch (IOException e ) {
212+ throw new RuntimeException (e );
213+ }
214+ });
215+ }
170216}
0 commit comments