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