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;
021
022import java.util.ArrayList;
023import java.util.List;
024import java.util.concurrent.CountDownLatch;
025
026import org.apache.hadoop.hbase.HBaseTestingUtil;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.client.Durability;
029import org.apache.hadoop.hbase.client.Get;
030import org.apache.hadoop.hbase.client.Put;
031import org.apache.hadoop.hbase.client.Result;
032import org.apache.hadoop.hbase.client.Table;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.junit.Test;
035import org.slf4j.Logger;
036import org.slf4j.LoggerFactory;
037
038public abstract class WALCorruptionWithMultiPutDueToDanglingByteBufferTestBase {
039
040  private static final Logger LOG = LoggerFactory
041    .getLogger(WALCorruptionWithMultiPutDueToDanglingByteBufferTestBase.class);
042
043  protected static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
044
045  protected static CountDownLatch ARRIVE;
046
047  protected static CountDownLatch RESUME;
048
049  protected static TableName TABLE_NAME = TableName
050    .valueOf("WALCorruptionWithMultiPutDueToDanglingByteBufferTestBase");
051
052  protected static byte[] CF = Bytes.toBytes("cf");
053
054  protected static byte[] CQ = Bytes.toBytes("cq");
055
056  private byte[] getBytes(String prefix, int index) {
057    return Bytes.toBytes(String.format("%s-%08d", prefix, index));
058  }
059
060  @Test
061  public void test() throws Exception {
062    LOG.info("Stop WAL appending...");
063    ARRIVE = new CountDownLatch(1);
064    RESUME = new CountDownLatch(1);
065    try (Table table = UTIL.getConnection().getTable(TABLE_NAME)) {
066      LOG.info("Put totally 100 rows in batches of 5 with " + Durability.ASYNC_WAL + "...");
067      int batchSize = 5;
068      List<Put> puts = new ArrayList<>(batchSize);
069      for (int i = 1; i <= 100; i++) {
070        Put p = new Put(getBytes("row", i)).addColumn(CF, CQ, getBytes("value", i))
071          .setDurability(Durability.ASYNC_WAL);
072        puts.add(p);
073        if (i % batchSize == 0) {
074          table.put(puts);
075          LOG.info("Wrote batch of {} rows from row {}", batchSize,
076            Bytes.toString(puts.get(0).getRow()));
077          puts.clear();
078          // Wait for few of the minibatches in 1st batch of puts to go through the WAL write.
079          // The WAL write will pause then
080          if (ARRIVE != null) {
081            ARRIVE.await();
082            ARRIVE = null;
083          }
084        }
085      }
086      LOG.info("Resume WAL appending...");
087      RESUME.countDown();
088      LOG.info("Put a single row to force a WAL sync...");
089      table.put(new Put(Bytes.toBytes("row")).addColumn(CF, CQ, Bytes.toBytes("value")));
090      LOG.info("Abort the only region server");
091      UTIL.getMiniHBaseCluster().abortRegionServer(0);
092      LOG.info("Start a new region server");
093      UTIL.getMiniHBaseCluster().startRegionServerAndWait(30000);
094      UTIL.waitTableAvailable(TABLE_NAME);
095      LOG.info("Check if all rows are still valid");
096      for (int i = 1; i <= 100; i++) {
097        Result result = table.get(new Get(getBytes("row", i)));
098        assertEquals(Bytes.toString(getBytes("value", i)), Bytes.toString(result.getValue(CF, CQ)));
099      }
100      Result result = table.get(new Get(Bytes.toBytes("row")));
101      assertEquals("value", Bytes.toString(result.getValue(CF, CQ)));
102    }
103  }
104}