001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.regionserver;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertNotNull;
022
023import java.util.ArrayList;
024import java.util.List;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseTestingUtil;
027import org.apache.hadoop.hbase.HConstants;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.client.Admin;
030import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
031import org.apache.hadoop.hbase.client.Put;
032import org.apache.hadoop.hbase.client.Scan;
033import org.apache.hadoop.hbase.client.Table;
034import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
035import org.apache.hadoop.hbase.master.cleaner.TimeToLiveHFileCleaner;
036import org.apache.hadoop.hbase.regionserver.compactions.CompactionConfiguration;
037import org.apache.hadoop.hbase.testclassification.LargeTests;
038import org.apache.hadoop.hbase.util.Bytes;
039import org.apache.hadoop.hbase.util.JVMClusterUtil;
040import org.junit.After;
041import org.junit.AfterClass;
042import org.junit.Before;
043import org.junit.BeforeClass;
044import org.junit.ClassRule;
045import org.junit.Test;
046import org.junit.experimental.categories.Category;
047import org.slf4j.Logger;
048import org.slf4j.LoggerFactory;
049
050@Category({ LargeTests.class })
051public class TestCleanupCompactedFileAfterFailover {
052
053  private static final Logger LOG =
054    LoggerFactory.getLogger(TestCleanupCompactedFileAfterFailover.class);
055
056  @ClassRule
057  public static final HBaseClassTestRule CLASS_RULE =
058    HBaseClassTestRule.forClass(TestCleanupCompactedFileAfterFailover.class);
059
060  private static HBaseTestingUtil TEST_UTIL;
061  private static Admin admin;
062  private static Table table;
063
064  private static TableName TABLE_NAME = TableName.valueOf("TestCleanupCompactedFileAfterFailover");
065  private static byte[] ROW = Bytes.toBytes("row");
066  private static byte[] FAMILY = Bytes.toBytes("cf");
067  private static byte[] QUALIFIER = Bytes.toBytes("cq");
068  private static byte[] VALUE = Bytes.toBytes("value");
069  private static final int RS_NUMBER = 5;
070
071  @BeforeClass
072  public static void beforeClass() throws Exception {
073    TEST_UTIL = new HBaseTestingUtil();
074    // Set the scanner lease to 20min, so the scanner can't be closed by RegionServer
075    TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD, 1200000);
076    TEST_UTIL.getConfiguration().setInt(CompactionConfiguration.HBASE_HSTORE_COMPACTION_MIN_KEY,
077      100);
078    TEST_UTIL.getConfiguration().set("dfs.blocksize", "64000");
079    TEST_UTIL.getConfiguration().set("dfs.namenode.fs-limits.min-block-size", "1024");
080    TEST_UTIL.getConfiguration().set(TimeToLiveHFileCleaner.TTL_CONF_KEY, "0");
081    TEST_UTIL.startMiniCluster(RS_NUMBER);
082    admin = TEST_UTIL.getAdmin();
083  }
084
085  @AfterClass
086  public static void afterClass() throws Exception {
087    TEST_UTIL.shutdownMiniCluster();
088  }
089
090  @Before
091  public void before() throws Exception {
092    TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(TABLE_NAME);
093    builder.setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY));
094    admin.createTable(builder.build());
095    TEST_UTIL.waitTableAvailable(TABLE_NAME);
096    table = TEST_UTIL.getConnection().getTable(TABLE_NAME);
097  }
098
099  @After
100  public void after() throws Exception {
101    admin.disableTable(TABLE_NAME);
102    admin.deleteTable(TABLE_NAME);
103  }
104
105  @Test
106  public void testCleanupAfterFailoverWithCompactOnce() throws Exception {
107    testCleanupAfterFailover(1);
108  }
109
110  @Test
111  public void testCleanupAfterFailoverWithCompactTwice() throws Exception {
112    testCleanupAfterFailover(2);
113  }
114
115  @Test
116  public void testCleanupAfterFailoverWithCompactThreeTimes() throws Exception {
117    testCleanupAfterFailover(3);
118  }
119
120  private void testCleanupAfterFailover(int compactNum) throws Exception {
121    HRegionServer rsServedTable = null;
122    List<HRegion> regions = new ArrayList<>();
123    for (JVMClusterUtil.RegionServerThread rsThread : TEST_UTIL.getHBaseCluster()
124      .getLiveRegionServerThreads()) {
125      HRegionServer rs = rsThread.getRegionServer();
126      if (rs.getOnlineTables().contains(TABLE_NAME)) {
127        regions.addAll(rs.getRegions(TABLE_NAME));
128        rsServedTable = rs;
129      }
130    }
131    assertNotNull(rsServedTable);
132    assertEquals("Table should only have one region", 1, regions.size());
133    HRegion region = regions.get(0);
134    HStore store = region.getStore(FAMILY);
135
136    writeDataAndFlush(3, region);
137    assertEquals(3, store.getStorefilesCount());
138
139    // Open a scanner and not close, then the storefile will be referenced
140    store.getScanner(new Scan(), null, 0);
141
142    region.compact(true);
143    assertEquals(1, store.getStorefilesCount());
144    // The compacted file should not be archived as there are references by user scanner
145    assertEquals(3, store.getStoreEngine().getStoreFileManager().getCompactedfiles().size());
146
147    for (int i = 1; i < compactNum; i++) {
148      // Compact again
149      region.compact(true);
150      assertEquals(1, store.getStorefilesCount());
151      store.closeAndArchiveCompactedFiles();
152      // Compacted storefiles still be 3 as the new compacted storefile was archived
153      assertEquals(3, store.getStoreEngine().getStoreFileManager().getCompactedfiles().size());
154    }
155
156    int walNum = rsServedTable.getWALs().size();
157    // Roll WAL
158    rsServedTable.getWalRoller().requestRollAll();
159    // Flush again
160    region.flush(true);
161    // The WAL which contains compaction event marker should be archived
162    assertEquals("The old WAL should be archived", walNum, rsServedTable.getWALs().size());
163
164    rsServedTable.kill();
165    // Sleep to wait failover
166    Thread.sleep(3000);
167    TEST_UTIL.waitTableAvailable(TABLE_NAME);
168
169    regions.clear();
170    for (JVMClusterUtil.RegionServerThread rsThread : TEST_UTIL.getHBaseCluster()
171      .getLiveRegionServerThreads()) {
172      HRegionServer rs = rsThread.getRegionServer();
173      if (rs != rsServedTable && rs.getOnlineTables().contains(TABLE_NAME)) {
174        regions.addAll(rs.getRegions(TABLE_NAME));
175      }
176    }
177    assertEquals("Table should only have one region", 1, regions.size());
178    region = regions.get(0);
179    store = region.getStore(FAMILY);
180    // The compacted storefile should be cleaned and only have 1 storefile
181    assertEquals(1, store.getStorefilesCount());
182  }
183
184  private void writeDataAndFlush(int fileNum, HRegion region) throws Exception {
185    for (int i = 0; i < fileNum; i++) {
186      for (int j = 0; j < 100; j++) {
187        table.put(new Put(concat(ROW, j)).addColumn(FAMILY, QUALIFIER, concat(VALUE, j)));
188      }
189      region.flush(true);
190    }
191  }
192
193  private byte[] concat(byte[] base, int index) {
194    return Bytes.toBytes(Bytes.toString(base) + "-" + index);
195  }
196}