|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.hbase.regionserver; |
| 19 | + |
| 20 | +import static org.apache.hadoop.hbase.HConstants.HFILE_BLOCK_CACHE_SIZE_KEY; |
| 21 | +import static org.apache.hadoop.hbase.HConstants.ROW_CACHE_EVICT_ON_CLOSE_KEY; |
| 22 | +import static org.apache.hadoop.hbase.HConstants.ROW_CACHE_SIZE_KEY; |
| 23 | +import static org.junit.Assert.assertArrayEquals; |
| 24 | +import static org.junit.Assert.assertEquals; |
| 25 | + |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.List; |
| 28 | +import org.apache.hadoop.conf.Configuration; |
| 29 | +import org.apache.hadoop.hbase.HBaseClassTestRule; |
| 30 | +import org.apache.hadoop.hbase.HBaseTestingUtil; |
| 31 | +import org.apache.hadoop.hbase.SingleProcessHBaseCluster; |
| 32 | +import org.apache.hadoop.hbase.TableName; |
| 33 | +import org.apache.hadoop.hbase.client.*; |
| 34 | +import org.apache.hadoop.hbase.testclassification.MediumTests; |
| 35 | +import org.apache.hadoop.hbase.testclassification.RegionServerTests; |
| 36 | +import org.apache.hadoop.hbase.util.Bytes; |
| 37 | +import org.junit.ClassRule; |
| 38 | +import org.junit.Rule; |
| 39 | +import org.junit.Test; |
| 40 | +import org.junit.experimental.categories.Category; |
| 41 | +import org.junit.rules.TestName; |
| 42 | +import org.junit.runner.RunWith; |
| 43 | +import org.junit.runners.Parameterized; |
| 44 | + |
| 45 | +@Category({ RegionServerTests.class, MediumTests.class }) |
| 46 | +@RunWith(Parameterized.class) |
| 47 | +public class TestRowCacheEvictOnClose { |
| 48 | + @ClassRule |
| 49 | + public static final HBaseClassTestRule CLASS_RULE = |
| 50 | + HBaseClassTestRule.forClass(TestRowCacheEvictOnClose.class); |
| 51 | + |
| 52 | + private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); |
| 53 | + private static final byte[] CF1 = Bytes.toBytes("cf1"); |
| 54 | + private static final byte[] Q1 = Bytes.toBytes("q1"); |
| 55 | + private static final byte[] Q2 = Bytes.toBytes("q2"); |
| 56 | + |
| 57 | + @Rule |
| 58 | + public TestName testName = new TestName(); |
| 59 | + |
| 60 | + @Parameterized.Parameter |
| 61 | + public boolean evictOnClose; |
| 62 | + |
| 63 | + @Parameterized.Parameters |
| 64 | + public static List<Object[]> params() { |
| 65 | + return Arrays.asList(new Object[][] { { true }, { false } }); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testEvictOnClose() throws Exception { |
| 70 | + Configuration conf = TEST_UTIL.getConfiguration(); |
| 71 | + |
| 72 | + // Enable row cache |
| 73 | + conf.setFloat(ROW_CACHE_SIZE_KEY, 0.01f); |
| 74 | + conf.setFloat(HFILE_BLOCK_CACHE_SIZE_KEY, 0.39f); |
| 75 | + |
| 76 | + // Set ROW_CACHE_EVICT_ON_CLOSE to true |
| 77 | + conf.setBoolean(ROW_CACHE_EVICT_ON_CLOSE_KEY, evictOnClose); |
| 78 | + |
| 79 | + // Start cluster |
| 80 | + SingleProcessHBaseCluster cluster = TEST_UTIL.startMiniCluster(); |
| 81 | + cluster.waitForActiveAndReadyMaster(); |
| 82 | + Admin admin = TEST_UTIL.getAdmin(); |
| 83 | + |
| 84 | + RowCache rowCache = TEST_UTIL.getHBaseCluster().getRegionServer(0).getRSRpcServices() |
| 85 | + .getRowCacheService().getRowCache(); |
| 86 | + |
| 87 | + // Create table with row cache enabled |
| 88 | + ColumnFamilyDescriptor cf1 = ColumnFamilyDescriptorBuilder.newBuilder(CF1).build(); |
| 89 | + TableName tableName = TableName.valueOf(testName.getMethodName().replaceAll("[\\[\\]]", "_")); |
| 90 | + TableDescriptor td = TableDescriptorBuilder.newBuilder(tableName).setRowCacheEnabled(true) |
| 91 | + .setColumnFamily(cf1).build(); |
| 92 | + admin.createTable(td); |
| 93 | + Table table = admin.getConnection().getTable(tableName); |
| 94 | + |
| 95 | + int numRows = 10; |
| 96 | + |
| 97 | + // Put rows |
| 98 | + for (int i = 0; i < numRows; i++) { |
| 99 | + byte[] rowKey = ("row" + i).getBytes(); |
| 100 | + Put put = new Put(rowKey); |
| 101 | + put.addColumn(CF1, Q1, Bytes.toBytes(0L)); |
| 102 | + put.addColumn(CF1, Q2, "12".getBytes()); |
| 103 | + table.put(put); |
| 104 | + } |
| 105 | + // Need to flush because the row cache is not populated when reading only from the memstore. |
| 106 | + admin.flush(tableName); |
| 107 | + |
| 108 | + // Populate row caches |
| 109 | + for (int i = 0; i < numRows; i++) { |
| 110 | + byte[] rowKey = ("row" + i).getBytes(); |
| 111 | + Get get = new Get(rowKey); |
| 112 | + Result result = table.get(get); |
| 113 | + assertArrayEquals(rowKey, result.getRow()); |
| 114 | + assertArrayEquals(Bytes.toBytes(0L), result.getValue(CF1, Q1)); |
| 115 | + assertArrayEquals("12".getBytes(), result.getValue(CF1, Q2)); |
| 116 | + } |
| 117 | + |
| 118 | + // Verify row cache has some entries |
| 119 | + assertEquals(numRows, rowCache.getCount()); |
| 120 | + |
| 121 | + // Disable table |
| 122 | + admin.disableTable(tableName); |
| 123 | + |
| 124 | + // Verify row cache is cleared on table close |
| 125 | + assertEquals(evictOnClose ? 0 : numRows, rowCache.getCount()); |
| 126 | + |
| 127 | + admin.deleteTable(tableName); |
| 128 | + TEST_UTIL.shutdownMiniCluster(); |
| 129 | + } |
| 130 | +} |
0 commit comments