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.wal;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertNotNull;
023import static org.junit.Assert.assertTrue;
024
025import java.util.ArrayList;
026import java.util.List;
027
028import org.apache.hadoop.fs.Path;
029import org.apache.hadoop.hbase.Cell;
030import org.apache.hadoop.hbase.CellUtil;
031import org.apache.hadoop.hbase.HBaseClassTestRule;
032import org.apache.hadoop.hbase.HBaseTestingUtility;
033import org.apache.hadoop.hbase.MiniHBaseCluster;
034import org.apache.hadoop.hbase.ServerName;
035import org.apache.hadoop.hbase.TableName;
036import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
037import org.apache.hadoop.hbase.client.Get;
038import org.apache.hadoop.hbase.client.Put;
039import org.apache.hadoop.hbase.client.Result;
040import org.apache.hadoop.hbase.client.Table;
041import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
042import org.apache.hadoop.hbase.testclassification.LargeTests;
043import org.apache.hadoop.hbase.testclassification.RegionServerTests;
044import org.apache.hadoop.hbase.util.Bytes;
045import org.apache.hadoop.hbase.util.CommonFSUtils;
046import org.junit.AfterClass;
047import org.junit.BeforeClass;
048import org.junit.ClassRule;
049import org.junit.Test;
050import org.junit.experimental.categories.Category;
051
052@Category({RegionServerTests.class, LargeTests.class})
053public class TestWALSplitWithDeletedTableData {
054
055  @ClassRule
056  public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule
057      .forClass(TestWALSplitWithDeletedTableData.class);
058  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
059
060  @BeforeClass
061  public static void setup() throws Exception {
062    TEST_UTIL.startMiniCluster(2);
063  }
064
065  @AfterClass
066  public static void tearDown() throws Exception {
067    TEST_UTIL.shutdownMiniCluster();
068  }
069
070  @Test
071  public void testWALSplitWithDeletedTableData() throws Exception {
072    final byte[] CFNAME = Bytes.toBytes("f1");
073    final byte[] QNAME = Bytes.toBytes("q1");
074    final byte[] VALUE = Bytes.toBytes("v1");
075    final TableName t1 = TableName.valueOf("t1");
076    final TableName t2 = TableName.valueOf("t2");
077    final byte[][] splitRows = { Bytes.toBytes("a"), Bytes.toBytes("b"), Bytes.toBytes("c"),
078        Bytes.toBytes("d") };
079    TableDescriptorBuilder htdBuilder1 = TableDescriptorBuilder.newBuilder(t1);
080    htdBuilder1.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(CFNAME).build());
081    Table tab1 = TEST_UTIL.createTable(htdBuilder1.build(), splitRows);
082    TableDescriptorBuilder htdBuilder2 = TableDescriptorBuilder.newBuilder(t2);
083    htdBuilder2.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(CFNAME).build());
084    Table tab2 = TEST_UTIL.createTable(htdBuilder2.build(), splitRows);
085    List<Put> puts = new ArrayList<Put>(4);
086    byte[][] rks = { Bytes.toBytes("ac"), Bytes.toBytes("ba"), Bytes.toBytes("ca"),
087        Bytes.toBytes("dd") };
088    for (byte[] rk : rks) {
089      puts.add(new Put(rk).addColumn(CFNAME, QNAME, VALUE));
090    }
091    tab1.put(puts);
092    tab2.put(puts);
093    MiniHBaseCluster cluster = TEST_UTIL.getMiniHBaseCluster();
094    TEST_UTIL.deleteTable(t1);
095    Path tableDir = CommonFSUtils.getWALTableDir(TEST_UTIL.getConfiguration(), t1);
096    // Dropping table 't1' removed the table directory from the WAL FS completely
097    assertFalse(TEST_UTIL.getDFSCluster().getFileSystem().exists(tableDir));
098    ServerName rs1 = cluster.getRegionServer(1).getServerName();
099    // Kill one RS and wait for the WAL split and replay be over.
100    cluster.killRegionServer(rs1);
101    cluster.waitForRegionServerToStop(rs1, 60 * 1000);
102    assertEquals(1, cluster.getNumLiveRegionServers());
103    Thread.sleep(1 * 1000);
104    TEST_UTIL.waitUntilNoRegionsInTransition(60 * 1000);
105    // Table 't1' is dropped. Assert table directory does not exist in WAL FS after WAL split.
106    assertFalse(TEST_UTIL.getDFSCluster().getFileSystem().exists(tableDir));
107    // Assert the table t2 region's data getting replayed after WAL split and available
108    for (byte[] rk : rks) {
109      Result result = tab2.get(new Get(rk));
110      assertFalse(result.isEmpty());
111      Cell cell = result.getColumnLatestCell(CFNAME, QNAME);
112      assertNotNull(cell);
113      assertTrue(CellUtil.matchingValue(cell, VALUE));
114    }
115  }
116}