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.master.region;
019
020import static org.junit.Assert.assertThrows;
021
022import java.io.IOException;
023import java.time.Duration;
024import java.util.List;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.fs.FileSystem;
027import org.apache.hadoop.fs.Path;
028import org.apache.hadoop.hbase.Abortable;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.client.Put;
031import org.apache.hadoop.hbase.io.asyncfs.monitor.StreamSlowMonitor;
032import org.apache.hadoop.hbase.regionserver.wal.AbstractFSWAL;
033import org.apache.hadoop.hbase.regionserver.wal.AsyncFSWAL;
034import org.apache.hadoop.hbase.regionserver.wal.WALActionsListener;
035import org.apache.hadoop.hbase.regionserver.wal.WALSyncTimeoutIOException;
036import org.apache.hadoop.hbase.testclassification.MasterTests;
037import org.apache.hadoop.hbase.testclassification.MediumTests;
038import org.apache.hadoop.hbase.util.Bytes;
039import org.apache.hadoop.hbase.util.CommonFSUtils;
040import org.apache.hadoop.hbase.wal.AsyncFSWALProvider;
041import org.apache.hadoop.hbase.wal.WALFactory;
042import org.apache.hadoop.hbase.wal.WALProvider;
043import org.junit.ClassRule;
044import org.junit.Test;
045import org.junit.experimental.categories.Category;
046
047import org.apache.hbase.thirdparty.io.netty.channel.Channel;
048import org.apache.hbase.thirdparty.io.netty.channel.EventLoopGroup;
049
050@Category({ MasterTests.class, MediumTests.class })
051public class TestMasterRegionWALSyncTimeoutIOException extends MasterRegionTestBase {
052
053  @ClassRule
054  public static final HBaseClassTestRule CLASS_RULE =
055    HBaseClassTestRule.forClass(TestMasterRegionWALSyncTimeoutIOException.class);
056
057  private static final Duration WAL_SYNC_TIMEOUT = Duration.ofSeconds(3);
058
059  private static volatile boolean testWalTimeout = false;
060
061  @Override
062  protected void configure(Configuration conf) throws IOException {
063    conf.setClass(WALFactory.WAL_PROVIDER, SlowAsyncFSWALProvider.class, WALProvider.class);
064    conf.setLong(AbstractFSWAL.WAL_SYNC_TIMEOUT_MS, WAL_SYNC_TIMEOUT.toMillis());
065  }
066
067  @Override
068  protected void configure(MasterRegionParams params) {
069    params.flushIntervalMs(Duration.ofSeconds(1).toMillis());
070  }
071
072  @Test
073  public void testUpdateWalSyncWriteException() {
074    testWalTimeout = true;
075    assertThrows(WALSyncTimeoutIOException.class, () -> {
076      for (int i = 0; i < 10; i++) {
077        region.update(
078          r -> r.put(new Put(Bytes.toBytes("0")).addColumn(CF1, QUALIFIER, Bytes.toBytes("0"))));
079        Thread.sleep(Duration.ofSeconds(1).toMillis());
080      }
081    });
082  }
083
084  public static class SlowAsyncFSWAL extends AsyncFSWAL {
085
086    public SlowAsyncFSWAL(FileSystem fs, Abortable abortable, Path rootDir, String logDir,
087      String archiveDir, Configuration conf, List<WALActionsListener> listeners,
088      boolean failIfWALExists, String prefix, String suffix, EventLoopGroup eventLoopGroup,
089      Class<? extends Channel> channelClass, StreamSlowMonitor monitor) throws IOException {
090      super(fs, abortable, rootDir, logDir, archiveDir, conf, listeners, failIfWALExists, prefix,
091        suffix, null, null, eventLoopGroup, channelClass, monitor);
092    }
093
094    @Override
095    protected void atHeadOfRingBufferEventHandlerAppend() {
096      if (testWalTimeout) {
097        try {
098          Thread.sleep(WAL_SYNC_TIMEOUT.plusSeconds(1).toMillis());
099        } catch (InterruptedException e) {
100          throw new RuntimeException(e);
101        }
102      }
103      super.atHeadOfRingBufferEventHandlerAppend();
104    }
105  }
106
107  public static class SlowAsyncFSWALProvider extends AsyncFSWALProvider {
108
109    @Override
110    protected AsyncFSWAL createWAL() throws IOException {
111      return new SlowAsyncFSWAL(CommonFSUtils.getWALFileSystem(conf), this.abortable,
112        CommonFSUtils.getWALRootDir(conf), getWALDirectoryName(factory.getFactoryId()),
113        getWALArchiveDirectoryName(conf, factory.getFactoryId()), conf, listeners, true, logPrefix,
114        META_WAL_PROVIDER_ID.equals(providerId) ? META_WAL_PROVIDER_ID : null, eventLoopGroup,
115        channelClass, factory.getExcludeDatanodeManager().getStreamSlowMonitor(providerId));
116    }
117  }
118}