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.regionserver.wal.AsyncFSWAL;
027import org.apache.hadoop.hbase.regionserver.wal.FailedLogCloseException;
028import org.apache.hadoop.hbase.regionserver.wal.WALActionsListener;
029import org.apache.hadoop.hbase.testclassification.MediumTests;
030import org.apache.hadoop.hbase.testclassification.RegionServerTests;
031import org.apache.hadoop.hbase.util.CommonFSUtils;
032import org.apache.hadoop.hbase.util.Pair;
033import org.junit.AfterClass;
034import org.junit.BeforeClass;
035import org.junit.ClassRule;
036import org.junit.experimental.categories.Category;
037
038import org.apache.hbase.thirdparty.io.netty.channel.Channel;
039import org.apache.hbase.thirdparty.io.netty.channel.EventLoopGroup;
040
041/**
042 * Testcase for HBASE-22539
043 */
044@Category({ RegionServerTests.class, MediumTests.class })
045public class TestAsyncFSWALCorruptionDueToDanglingByteBuffer
046  extends WALCorruptionDueToDanglingByteBufferTestBase {
047
048  @ClassRule
049  public static final HBaseClassTestRule CLASS_RULE =
050    HBaseClassTestRule.forClass(TestAsyncFSWALCorruptionDueToDanglingByteBuffer.class);
051
052  public static final class PauseWAL extends AsyncFSWAL {
053
054    public PauseWAL(FileSystem fs, Path rootDir, String logDir, String archiveDir,
055      Configuration conf, List<WALActionsListener> listeners, boolean failIfWALExists,
056      String prefix, String suffix, EventLoopGroup eventLoopGroup,
057      Class<? extends Channel> channelClass) throws FailedLogCloseException, IOException {
058      super(fs, rootDir, logDir, archiveDir, conf, listeners, failIfWALExists, prefix, suffix,
059        eventLoopGroup, channelClass);
060    }
061
062    @Override
063    protected void atHeadOfRingBufferEventHandlerAppend() {
064      if (ARRIVE != null) {
065        ARRIVE.countDown();
066        try {
067          RESUME.await();
068        } catch (InterruptedException e) {
069        }
070      }
071    }
072  }
073
074  public static final class PauseWALProvider extends AbstractFSWALProvider<PauseWAL> {
075
076    private EventLoopGroup eventLoopGroup;
077
078    private Class<? extends Channel> channelClass;
079
080    @Override
081    protected PauseWAL createWAL() throws IOException {
082      return new PauseWAL(CommonFSUtils.getWALFileSystem(conf), CommonFSUtils.getWALRootDir(conf),
083        getWALDirectoryName(factory.factoryId), getWALArchiveDirectoryName(conf, factory.factoryId),
084        conf, listeners, true, logPrefix,
085        META_WAL_PROVIDER_ID.equals(providerId) ? META_WAL_PROVIDER_ID : null, eventLoopGroup,
086        channelClass);
087    }
088
089    @Override
090    protected void doInit(Configuration conf) throws IOException {
091      Pair<EventLoopGroup, Class<? extends Channel>> eventLoopGroupAndChannelClass =
092        NettyAsyncFSWALConfigHelper.getEventLoopConfig(conf);
093      eventLoopGroup = eventLoopGroupAndChannelClass.getFirst();
094      channelClass = eventLoopGroupAndChannelClass.getSecond();
095    }
096  }
097
098  @BeforeClass
099  public static void setUp() throws Exception {
100    UTIL.getConfiguration().setClass(WALFactory.WAL_PROVIDER, PauseWALProvider.class,
101      WALProvider.class);
102    UTIL.startMiniCluster(1);
103    UTIL.createTable(TABLE_NAME, CF);
104    UTIL.waitTableAvailable(TABLE_NAME);
105  }
106
107  @AfterClass
108  public static void tearDown() throws Exception {
109    UTIL.shutdownMiniCluster();
110  }
111}