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    AbstractTestLogRolling.setUpBeforeClass();
057  }
058
059  public static class SlowSyncLogWriter extends AsyncProtobufLogWriter {
060
061    public SlowSyncLogWriter(EventLoopGroup eventLoopGroup, Class<? extends Channel> channelClass) {
062      super(eventLoopGroup, channelClass);
063    }
064
065    @Override
066    public CompletableFuture<Long> sync(boolean forceSync) {
067      CompletableFuture<Long> future = new CompletableFuture<>();
068      super.sync(forceSync).whenCompleteAsync((lengthAfterFlush, error) -> {
069        EXECUTOR.schedule(() -> {
070          if (error != null) {
071            future.completeExceptionally(error);
072          } else {
073            future.complete(lengthAfterFlush);
074          }
075        }, syncLatencyMillis, TimeUnit.MILLISECONDS);
076      });
077      return future;
078    }
079  }
080
081  @Override
082  protected void setSlowLogWriter(Configuration conf) {
083    conf.set(AsyncFSWALProvider.WRITER_IMPL, SlowSyncLogWriter.class.getName());
084  }
085
086  @Override
087  protected void setDefaultLogWriter(Configuration conf) {
088    conf.set(AsyncFSWALProvider.WRITER_IMPL, AsyncProtobufLogWriter.class.getName());
089  }
090
091  @Test
092  public void testSlowSyncLogRolling() throws Exception {
093    // Create the test table
094    TableDescriptor desc = TableDescriptorBuilder.newBuilder(TableName.valueOf(getName()))
095      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(HConstants.CATALOG_FAMILY)).build();
096    admin.createTable(desc);
097    try (Table table = TEST_UTIL.getConnection().getTable(desc.getTableName())) {
098      server = TEST_UTIL.getRSForFirstRegionInTable(desc.getTableName());
099      RegionInfo region = server.getRegions(desc.getTableName()).get(0).getRegionInfo();
100      final AbstractFSWAL<?> log = getWALAndRegisterSlowSyncHook(region);
101
102      // Set default log writer, no additional latency to any sync on the hlog.
103      checkSlowSync(log, table, -1, 10, false);
104
105      // Adds 5000 ms of latency to any sync on the hlog. This will trip the other threshold.
106      // Write some data. Should only take one sync.
107      checkSlowSync(log, table, 5000, 1, true);
108
109      // Set default log writer, no additional latency to any sync on the hlog.
110      checkSlowSync(log, table, -1, 10, false);
111    }
112  }
113
114  @Test
115  public void testLogRollOnDatanodeDeath() throws IOException, InterruptedException {
116    dfsCluster.startDataNodes(TEST_UTIL.getConfiguration(), 3, true, null, null);
117    tableName = getName();
118    Table table = createTestTable(tableName);
119    TEST_UTIL.waitUntilAllRegionsAssigned(table.getName());
120    doPut(table, 1);
121    server = TEST_UTIL.getRSForFirstRegionInTable(table.getName());
122    RegionInfo hri = server.getRegions(table.getName()).get(0).getRegionInfo();
123    AsyncFSWAL wal = (AsyncFSWAL) server.getWAL(hri);
124    int numRolledLogFiles = AsyncFSWALProvider.getNumRolledLogFiles(wal);
125    DatanodeInfo[] dnInfos = wal.getPipeline();
126    DataNodeProperties dnProp = TEST_UTIL.getDFSCluster().stopDataNode(dnInfos[0].getName());
127    TEST_UTIL.getDFSCluster().restartDataNode(dnProp);
128    doPut(table, 2);
129    assertEquals(numRolledLogFiles + 1, AsyncFSWALProvider.getNumRolledLogFiles(wal));
130  }
131}