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.assertNull; 021import static org.mockito.Mockito.mock; 022import static org.mockito.Mockito.when; 023 024import java.io.IOException; 025import java.io.UncheckedIOException; 026import java.util.List; 027import java.util.NavigableMap; 028import java.util.TreeMap; 029import java.util.concurrent.CompletableFuture; 030import java.util.concurrent.atomic.AtomicInteger; 031import java.util.concurrent.atomic.AtomicReference; 032import org.apache.hadoop.conf.Configuration; 033import org.apache.hadoop.fs.FileSystem; 034import org.apache.hadoop.fs.Path; 035import org.apache.hadoop.hbase.HConstants; 036import org.apache.hadoop.hbase.KeyValue; 037import org.apache.hadoop.hbase.TableName; 038import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 039import org.apache.hadoop.hbase.client.RegionInfo; 040import org.apache.hadoop.hbase.client.RegionInfoBuilder; 041import org.apache.hadoop.hbase.client.TableDescriptor; 042import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 043import org.apache.hadoop.hbase.io.asyncfs.monitor.StreamSlowMonitor; 044import org.apache.hadoop.hbase.regionserver.LogRoller; 045import org.apache.hadoop.hbase.regionserver.MultiVersionConcurrencyControl; 046import org.apache.hadoop.hbase.regionserver.RegionServerServices; 047import org.apache.hadoop.hbase.regionserver.SequenceId; 048import org.apache.hadoop.hbase.testclassification.LargeTests; 049import org.apache.hadoop.hbase.testclassification.RegionServerTests; 050import org.apache.hadoop.hbase.util.Bytes; 051import org.apache.hadoop.hbase.util.CommonFSUtils; 052import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 053import org.apache.hadoop.hbase.util.FutureUtils; 054import org.apache.hadoop.hbase.util.Threads; 055import org.apache.hadoop.hbase.wal.WALEdit; 056import org.apache.hadoop.hbase.wal.WALEditInternalHelper; 057import org.apache.hadoop.hbase.wal.WALKey; 058import org.apache.hadoop.hbase.wal.WALKeyImpl; 059import org.apache.hadoop.hbase.wal.WALProvider.AsyncWriter; 060import org.junit.jupiter.api.AfterAll; 061import org.junit.jupiter.api.BeforeAll; 062import org.junit.jupiter.api.Tag; 063import org.junit.jupiter.api.Test; 064 065import org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder; 066import org.apache.hbase.thirdparty.io.netty.channel.Channel; 067import org.apache.hbase.thirdparty.io.netty.channel.EventLoopGroup; 068import org.apache.hbase.thirdparty.io.netty.channel.nio.NioEventLoopGroup; 069import org.apache.hbase.thirdparty.io.netty.channel.socket.nio.NioSocketChannel; 070 071/** 072 * Provides AsyncFSWAL test cases. 073 */ 074@Tag(RegionServerTests.TAG) 075@Tag(LargeTests.TAG) 076public class TestAsyncFSWAL extends AbstractTestFSWAL { 077 078 private static EventLoopGroup GROUP; 079 080 private static Class<? extends Channel> CHANNEL_CLASS; 081 082 @BeforeAll 083 public static void setUpBeforeClass() throws Exception { 084 GROUP = 085 new NioEventLoopGroup(1, new ThreadFactoryBuilder().setNameFormat("TestAsyncFSWAL-pool-%d") 086 .setDaemon(true).setUncaughtExceptionHandler(Threads.LOGGING_EXCEPTION_HANDLER).build()); 087 CHANNEL_CLASS = NioSocketChannel.class; 088 AbstractTestFSWAL.setUpBeforeClass(); 089 } 090 091 @AfterAll 092 public static void tearDownAfterClass() throws Exception { 093 AbstractTestFSWAL.tearDownAfterClass(); 094 GROUP.shutdownGracefully(); 095 } 096 097 @Override 098 protected AbstractFSWAL<?> newWAL(FileSystem fs, Path rootDir, String logDir, String archiveDir, 099 Configuration conf, List<WALActionsListener> listeners, boolean failIfWALExists, String prefix, 100 String suffix) throws IOException { 101 AsyncFSWAL wal = new AsyncFSWAL(fs, null, rootDir, logDir, archiveDir, conf, listeners, 102 failIfWALExists, prefix, suffix, null, null, GROUP, CHANNEL_CLASS, 103 StreamSlowMonitor.create(conf, "monitor")); 104 wal.init(); 105 return wal; 106 } 107 108 @Override 109 protected AbstractFSWAL<?> newSlowWAL(FileSystem fs, Path rootDir, String logDir, 110 String archiveDir, Configuration conf, List<WALActionsListener> listeners, 111 boolean failIfWALExists, String prefix, String suffix, final Runnable action) 112 throws IOException { 113 AsyncFSWAL wal = new AsyncFSWAL(fs, null, rootDir, logDir, archiveDir, conf, listeners, 114 failIfWALExists, prefix, suffix, null, null, GROUP, CHANNEL_CLASS, 115 StreamSlowMonitor.create(conf, "monitor")) { 116 117 @Override 118 protected void atHeadOfRingBufferEventHandlerAppend() { 119 action.run(); 120 super.atHeadOfRingBufferEventHandlerAppend(); 121 } 122 }; 123 wal.init(); 124 return wal; 125 } 126 127 @Test 128 public void testBrokenWriter() throws Exception { 129 RegionServerServices services = mock(RegionServerServices.class); 130 when(services.getConfiguration()).thenReturn(CONF); 131 TableDescriptor td = TableDescriptorBuilder.newBuilder(TableName.valueOf("table")) 132 .setColumnFamily(ColumnFamilyDescriptorBuilder.of("row")).build(); 133 RegionInfo ri = RegionInfoBuilder.newBuilder(td.getTableName()).build(); 134 MultiVersionConcurrencyControl mvcc = new MultiVersionConcurrencyControl(); 135 NavigableMap<byte[], Integer> scopes = new TreeMap<>(Bytes.BYTES_COMPARATOR); 136 for (byte[] fam : td.getColumnFamilyNames()) { 137 scopes.put(fam, 0); 138 } 139 long timestamp = EnvironmentEdgeManager.currentTime(); 140 AtomicInteger failedCount = new AtomicInteger(0); 141 try (LogRoller roller = new LogRoller(services); 142 AsyncFSWAL wal = new AsyncFSWAL(FS, null, CommonFSUtils.getWALRootDir(CONF), DIR.toString(), 143 testName, CONF, null, true, null, null, null, null, GROUP, CHANNEL_CLASS, 144 StreamSlowMonitor.create(CONF, "monitorForSuffix")) { 145 146 @Override 147 protected AsyncWriter createWriterInstance(FileSystem fs, Path path) throws IOException { 148 AsyncWriter writer = super.createWriterInstance(fs, path); 149 return new AsyncWriter() { 150 151 @Override 152 public void close() throws IOException { 153 writer.close(); 154 } 155 156 @Override 157 public long getLength() { 158 return writer.getLength(); 159 } 160 161 @Override 162 public long getSyncedLength() { 163 return writer.getSyncedLength(); 164 } 165 166 @Override 167 public CompletableFuture<Long> sync(boolean forceSync) { 168 CompletableFuture<Long> result = writer.sync(forceSync); 169 if (failedCount.incrementAndGet() < 1000) { 170 CompletableFuture<Long> future = new CompletableFuture<>(); 171 FutureUtils.addListener(result, 172 (r, e) -> future.completeExceptionally(new IOException("Inject Error"))); 173 return future; 174 } else { 175 return result; 176 } 177 } 178 179 @Override 180 public void append(Entry entry) { 181 writer.append(entry); 182 } 183 }; 184 } 185 }) { 186 wal.init(); 187 roller.addWAL(wal); 188 roller.start(); 189 int numThreads = 10; 190 AtomicReference<Exception> error = new AtomicReference<>(); 191 Thread[] threads = new Thread[numThreads]; 192 for (int i = 0; i < 10; i++) { 193 final int index = i; 194 threads[index] = new Thread("Write-Thread-" + index) { 195 196 @Override 197 public void run() { 198 byte[] row = Bytes.toBytes("row" + index); 199 WALEdit cols = new WALEdit(); 200 WALEditInternalHelper.addExtendedCell(cols, 201 new KeyValue(row, row, row, timestamp + index, row)); 202 WALKeyImpl key = new WALKeyImpl(ri.getEncodedNameAsBytes(), td.getTableName(), 203 SequenceId.NO_SEQUENCE_ID, timestamp, WALKey.EMPTY_UUIDS, HConstants.NO_NONCE, 204 HConstants.NO_NONCE, mvcc, scopes); 205 try { 206 wal.append(ri, key, cols, true); 207 } catch (IOException e) { 208 // should not happen 209 throw new UncheckedIOException(e); 210 } 211 try { 212 wal.sync(); 213 } catch (IOException e) { 214 error.set(e); 215 } 216 } 217 }; 218 } 219 for (Thread t : threads) { 220 t.start(); 221 } 222 for (Thread t : threads) { 223 t.join(); 224 } 225 assertNull(error.get()); 226 } 227 } 228}