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