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.regionserver.wal;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021
022import java.io.IOException;
023import java.util.concurrent.CompletableFuture;
024import java.util.concurrent.TimeUnit;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.HConstants;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
029import org.apache.hadoop.hbase.client.RegionInfo;
030import org.apache.hadoop.hbase.client.Table;
031import org.apache.hadoop.hbase.client.TableDescriptor;
032import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
033import org.apache.hadoop.hbase.io.asyncfs.FanOutOneBlockAsyncDFSOutputHelper;
034import org.apache.hadoop.hbase.testclassification.LargeTests;
035import org.apache.hadoop.hbase.testclassification.VerySlowRegionServerTests;
036import org.apache.hadoop.hbase.wal.AsyncFSWALProvider;
037import org.apache.hadoop.hbase.wal.WALFactory;
038import org.apache.hadoop.hdfs.MiniDFSCluster.DataNodeProperties;
039import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
040import org.junit.jupiter.api.BeforeAll;
041import org.junit.jupiter.api.Tag;
042import org.junit.jupiter.api.Test;
043
044import org.apache.hbase.thirdparty.io.netty.channel.Channel;
045import org.apache.hbase.thirdparty.io.netty.channel.EventLoopGroup;
046
047@Tag(VerySlowRegionServerTests.TAG)
048@Tag(LargeTests.TAG)
049public class TestAsyncLogRolling extends AbstractTestLogRolling {
050
051  @BeforeAll
052  public static void setUpBeforeClass() throws Exception {
053    Configuration conf = TestAsyncLogRolling.TEST_UTIL.getConfiguration();
054    conf.setInt(FanOutOneBlockAsyncDFSOutputHelper.ASYNC_DFS_OUTPUT_CREATE_MAX_RETRIES, 100);
055    conf.set(WALFactory.WAL_PROVIDER, "asyncfs");
056  }
057
058  public static class SlowSyncLogWriter extends AsyncProtobufLogWriter {
059
060    public SlowSyncLogWriter(EventLoopGroup eventLoopGroup, Class<? extends Channel> channelClass) {
061      super(eventLoopGroup, channelClass);
062    }
063
064    @Override
065    public CompletableFuture<Long> sync(boolean forceSync) {
066      CompletableFuture<Long> future = new CompletableFuture<>();
067      super.sync(forceSync).whenCompleteAsync((lengthAfterFlush, error) -> {
068        EXECUTOR.schedule(() -> {
069          if (error != null) {
070            future.completeExceptionally(error);
071          } else {
072            future.complete(lengthAfterFlush);
073          }
074        }, syncLatencyMillis, TimeUnit.MILLISECONDS);
075      });
076      return future;
077    }
078  }
079
080  @Override
081  protected void setSlowLogWriter(Configuration conf) {
082    conf.set(AsyncFSWALProvider.WRITER_IMPL, SlowSyncLogWriter.class.getName());
083  }
084
085  @Override
086  protected void setDefaultLogWriter(Configuration conf) {
087    conf.set(AsyncFSWALProvider.WRITER_IMPL, AsyncProtobufLogWriter.class.getName());
088  }
089
090  @Test
091  public void testSlowSyncLogRolling() throws Exception {
092    // Create the test table
093    TableDescriptor desc = TableDescriptorBuilder.newBuilder(TableName.valueOf(getName()))
094      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(HConstants.CATALOG_FAMILY)).build();
095    admin.createTable(desc);
096    try (Table table = TEST_UTIL.getConnection().getTable(desc.getTableName())) {
097      server = TEST_UTIL.getRSForFirstRegionInTable(desc.getTableName());
098      RegionInfo region = server.getRegions(desc.getTableName()).get(0).getRegionInfo();
099      final AbstractFSWAL<?> log = getWALAndRegisterSlowSyncHook(region);
100
101      // Set default log writer, no additional latency to any sync on the hlog.
102      checkSlowSync(log, table, -1, 10, false);
103
104      // Adds 5000 ms of latency to any sync on the hlog. This will trip the other threshold.
105      // Write some data. Should only take one sync.
106      checkSlowSync(log, table, 5000, 1, true);
107
108      // Set default log writer, no additional latency to any sync on the hlog.
109      checkSlowSync(log, table, -1, 10, false);
110    }
111  }
112
113  @Test
114  public void testLogRollOnDatanodeDeath() throws IOException, InterruptedException {
115    dfsCluster.startDataNodes(TEST_UTIL.getConfiguration(), 3, true, null, null);
116    tableName = getName();
117    Table table = createTestTable(tableName);
118    TEST_UTIL.waitUntilAllRegionsAssigned(table.getName());
119    doPut(table, 1);
120    server = TEST_UTIL.getRSForFirstRegionInTable(table.getName());
121    RegionInfo hri = server.getRegions(table.getName()).get(0).getRegionInfo();
122    AsyncFSWAL wal = (AsyncFSWAL) server.getWAL(hri);
123    int numRolledLogFiles = AsyncFSWALProvider.getNumRolledLogFiles(wal);
124    DatanodeInfo[] dnInfos = wal.getPipeline();
125    DataNodeProperties dnProp = TEST_UTIL.getDFSCluster().stopDataNode(dnInfos[0].getName());
126    TEST_UTIL.getDFSCluster().restartDataNode(dnProp);
127    doPut(table, 2);
128    assertEquals(numRolledLogFiles + 1, AsyncFSWALProvider.getNumRolledLogFiles(wal));
129  }
130}