View Javadoc

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.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertNotNull;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.hadoop.hbase.HBaseTestingUtility;
27  import org.apache.hadoop.hbase.HColumnDescriptor;
28  import org.apache.hadoop.hbase.HConstants;
29  import org.apache.hadoop.hbase.HTableDescriptor;
30  import org.apache.hadoop.hbase.TableName;
31  import org.apache.hadoop.hbase.client.Admin;
32  import org.apache.hadoop.hbase.client.Put;
33  import org.apache.hadoop.hbase.client.Scan;
34  import org.apache.hadoop.hbase.client.Table;
35  import org.apache.hadoop.hbase.master.cleaner.TimeToLiveHFileCleaner;
36  import org.apache.hadoop.hbase.regionserver.compactions.CompactionConfiguration;
37  import org.apache.hadoop.hbase.testclassification.LargeTests;
38  import org.apache.hadoop.hbase.util.Bytes;
39  import org.apache.hadoop.hbase.util.JVMClusterUtil;
40  import org.junit.After;
41  import org.junit.AfterClass;
42  import org.junit.Before;
43  import org.junit.BeforeClass;
44  import org.junit.Test;
45  import org.junit.experimental.categories.Category;
46  import org.slf4j.Logger;
47  import org.slf4j.LoggerFactory;
48  
49  @Category({LargeTests.class})
50  public class TestCleanupCompactedFileAfterFailover {
51  
52    private static final Logger LOG =
53        LoggerFactory.getLogger(TestCleanupCompactedFileAfterFailover.class);
54  
55    private static HBaseTestingUtility TEST_UTIL;
56    private static Admin admin;
57    private static Table table;
58  
59    private static TableName TABLE_NAME = TableName.valueOf("TestCleanupCompactedFileAfterFailover");
60    private static byte[] ROW = Bytes.toBytes("row");
61    private static byte[] FAMILY = Bytes.toBytes("cf");
62    private static byte[] QUALIFIER = Bytes.toBytes("cq");
63    private static byte[] VALUE = Bytes.toBytes("value");
64    private static final int RS_NUMBER = 5;
65  
66    @BeforeClass
67    public static void beforeClass() throws Exception {
68      TEST_UTIL = new HBaseTestingUtility();
69      // Set the scanner lease to 20min, so the scanner can't be closed by RegionServer
70      TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD, 1200000);
71      TEST_UTIL.getConfiguration()
72          .setInt(CompactionConfiguration.HBASE_HSTORE_COMPACTION_MIN_KEY, 100);
73      TEST_UTIL.getConfiguration().set("dfs.blocksize", "64000");
74      TEST_UTIL.getConfiguration().set("dfs.namenode.fs-limits.min-block-size", "1024");
75      TEST_UTIL.getConfiguration().set(TimeToLiveHFileCleaner.TTL_CONF_KEY, "0");
76      TEST_UTIL.startMiniCluster(RS_NUMBER);
77      admin = TEST_UTIL.getHBaseAdmin();
78    }
79  
80    @AfterClass
81    public static void afterClass() throws Exception {
82      TEST_UTIL.shutdownMiniCluster();
83    }
84  
85    @Before
86    public void before() throws Exception {
87      HTableDescriptor htd = new HTableDescriptor(TABLE_NAME);
88      htd.addFamily(new HColumnDescriptor(FAMILY));
89      admin.createTable(htd);
90      TEST_UTIL.waitTableAvailable(TABLE_NAME, 30000);
91      table = TEST_UTIL.getConnection().getTable(TABLE_NAME);
92    }
93  
94    @After
95    public void after() throws Exception {
96      admin.disableTable(TABLE_NAME);
97      admin.deleteTable(TABLE_NAME);
98    }
99  
100   @Test
101   public void testCleanupAfterFailoverWithCompactOnce() throws Exception {
102     testCleanupAfterFailover(1);
103   }
104 
105   @Test
106   public void testCleanupAfterFailoverWithCompactTwice() throws Exception {
107     testCleanupAfterFailover(2);
108   }
109 
110   @Test
111   public void testCleanupAfterFailoverWithCompactThreeTimes() throws Exception {
112     testCleanupAfterFailover(3);
113   }
114 
115   private void testCleanupAfterFailover(int compactNum) throws Exception {
116     HRegionServer rsServedTable = null;
117     List<Region> regions = new ArrayList<>();
118     for (JVMClusterUtil.RegionServerThread rsThread : TEST_UTIL.getHBaseCluster()
119         .getLiveRegionServerThreads()) {
120       HRegionServer rs = rsThread.getRegionServer();
121       if (rs.getOnlineTables().contains(TABLE_NAME)) {
122         regions.addAll(rs.getOnlineRegions(TABLE_NAME));
123         rsServedTable = rs;
124       }
125     }
126     assertNotNull(rsServedTable);
127     assertEquals("Table should only have one region", 1, regions.size());
128     HRegion region = (HRegion)regions.get(0);
129     HStore store = (HStore)region.getStore(FAMILY);
130 
131     writeDataAndFlush(3, region);
132     assertEquals(3, store.getStorefilesCount());
133 
134     // Open a scanner and not close, then the storefile will be referenced
135     store.getScanner(new Scan(), null, Long.MAX_VALUE);
136 
137     region.compact(true);
138     assertEquals(1, store.getStorefilesCount());
139     // The compacted file should not be archived as there are references by user scanner
140     assertEquals(3, store.getStoreEngine().getStoreFileManager().getCompactedfiles().size());
141 
142     for (int i = 1; i < compactNum; i++) {
143       // Compact again
144       region.compact(true);
145       assertEquals(1, store.getStorefilesCount());
146       store.closeAndArchiveCompactedFiles();
147       // Compacted storefiles still be 3 as the new compacted storefile was archived
148       assertEquals(3, store.getStoreEngine().getStoreFileManager().getCompactedfiles().size());
149     }
150 
151     int walNum = rsServedTable.getWALs().size();
152     // Roll WAL
153     rsServedTable.walRoller.requestRollAll();
154     // Flush again
155     region.flush(true);
156     // The WAL which contains compaction event marker should be archived
157     assertEquals("The old WAL should be archived", walNum, rsServedTable.getWALs().size());
158 
159     rsServedTable.kill();
160     // Sleep to wait failover
161     Thread.sleep(3000);
162     TEST_UTIL.waitTableAvailable(TABLE_NAME, 30000);
163 
164     regions.clear();
165     for (JVMClusterUtil.RegionServerThread rsThread : TEST_UTIL.getHBaseCluster()
166         .getLiveRegionServerThreads()) {
167       HRegionServer rs = rsThread.getRegionServer();
168       if (rs != rsServedTable && rs.getOnlineTables().contains(TABLE_NAME)) {
169         regions.addAll(rs.getOnlineRegions(TABLE_NAME));
170       }
171     }
172     assertEquals("Table should only have one region", 1, regions.size());
173     region = (HRegion)regions.get(0);
174     store = (HStore)region.getStore(FAMILY);
175     // The compacted storefile should be cleaned and only have 1 storefile
176     assertEquals(1, store.getStorefilesCount());
177   }
178 
179   private void writeDataAndFlush(int fileNum, HRegion region) throws Exception {
180     for (int i = 0; i < fileNum; i++) {
181       for (int j = 0; j < 100; j++) {
182         table.put(new Put(concat(ROW, j)).addColumn(FAMILY, QUALIFIER, concat(VALUE, j)));
183       }
184       region.flush(true);
185     }
186   }
187 
188   private byte[] concat(byte[] base, int index) {
189     return Bytes.toBytes(Bytes.toString(base) + "-" + index);
190   }
191 }