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 java.io.IOException;
021import java.util.List;
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.fs.FileSystem;
024import org.apache.hadoop.fs.Path;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.io.ByteBuffAllocator;
027import org.apache.hadoop.hbase.ipc.RpcServerFactory;
028import org.apache.hadoop.hbase.ipc.SimpleRpcServer;
029import org.apache.hadoop.hbase.regionserver.HRegion;
030import org.apache.hadoop.hbase.regionserver.wal.FSHLog;
031import org.apache.hadoop.hbase.regionserver.wal.WALActionsListener;
032import org.apache.hadoop.hbase.testclassification.MediumTests;
033import org.apache.hadoop.hbase.testclassification.RegionServerTests;
034import org.apache.hadoop.hbase.util.CommonFSUtils;
035import org.junit.AfterClass;
036import org.junit.BeforeClass;
037import org.junit.ClassRule;
038import org.junit.experimental.categories.Category;
039
040@Category({ RegionServerTests.class, MediumTests.class })
041public class TestFSHLogCorruptionWithMultiPutDueToDanglingByteBuffer
042  extends WALCorruptionWithMultiPutDueToDanglingByteBufferTestBase {
043
044  @ClassRule
045  public static final HBaseClassTestRule CLASS_RULE =
046    HBaseClassTestRule.forClass(TestFSHLogCorruptionWithMultiPutDueToDanglingByteBuffer.class);
047
048  public static final class PauseWAL extends FSHLog {
049
050    private int testTableWalAppendsCount = 0;
051
052    public PauseWAL(FileSystem fs, Path rootDir, String logDir, String archiveDir,
053      Configuration conf, List<WALActionsListener> listeners, boolean failIfWALExists,
054      String prefix, String suffix) throws IOException {
055      super(fs, rootDir, logDir, archiveDir, conf, listeners, failIfWALExists, prefix, suffix);
056    }
057
058    @Override
059    protected void atHeadOfRingBufferEventHandlerAppend() {
060      // Let the 1st Append go through. The write thread will wait for this to go through before
061      // calling further put()
062      if (ARRIVE != null) { // Means appends as part of puts in testcase
063        // Sleep for a second so that RS handler thread put all the mini batch WAL appends to ring
064        // buffer.
065        if (testTableWalAppendsCount == 0) {
066          try {
067            Thread.sleep(1000);
068          } catch (InterruptedException e) {
069          }
070        }
071        // Let the first minibatch write go through. When 2nd one comes, notify the waiting test
072        // case for doing further batch puts and make this WAL append thread to pause
073        if (testTableWalAppendsCount == 1) {
074          ARRIVE.countDown();
075          try {
076            RESUME.await();
077          } catch (InterruptedException e) {
078          }
079        }
080        testTableWalAppendsCount++;
081      }
082    }
083  }
084
085  public static final class PauseWALProvider extends AbstractFSWALProvider<PauseWAL> {
086
087    @Override
088    protected PauseWAL createWAL() throws IOException {
089      return new PauseWAL(CommonFSUtils.getWALFileSystem(conf), CommonFSUtils.getWALRootDir(conf),
090        getWALDirectoryName(factory.factoryId), getWALArchiveDirectoryName(conf, factory.factoryId),
091        conf, listeners, true, logPrefix,
092        META_WAL_PROVIDER_ID.equals(providerId) ? META_WAL_PROVIDER_ID : null);
093    }
094
095    @Override
096    protected void doInit(Configuration conf) throws IOException {
097    }
098  }
099
100  @BeforeClass
101  public static void setUp() throws Exception {
102    UTIL.getConfiguration().setClass(WALFactory.WAL_PROVIDER, PauseWALProvider.class,
103      WALProvider.class);
104    UTIL.getConfiguration().setInt(HRegion.HBASE_REGIONSERVER_MINIBATCH_SIZE, 1);
105    UTIL.getConfiguration().set(RpcServerFactory.CUSTOM_RPC_SERVER_IMPL_CONF_KEY,
106      SimpleRpcServer.class.getName());
107    UTIL.getConfiguration().setInt(ByteBuffAllocator.MAX_BUFFER_COUNT_KEY, 1);
108    UTIL.getConfiguration().setInt(ByteBuffAllocator.BUFFER_SIZE_KEY, 1024);
109    UTIL.getConfiguration().setInt(ByteBuffAllocator.MIN_ALLOCATE_SIZE_KEY, 500);
110    UTIL.startMiniCluster(1);
111    UTIL.createTable(TABLE_NAME, CF);
112    UTIL.waitTableAvailable(TABLE_NAME);
113  }
114
115  @AfterClass
116  public static void tearDown() throws Exception {
117    UTIL.shutdownMiniCluster();
118  }
119}