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.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.HBaseClassTestRule; 036import org.apache.hadoop.hbase.HConstants; 037import org.apache.hadoop.hbase.KeyValue; 038import org.apache.hadoop.hbase.TableName; 039import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 040import org.apache.hadoop.hbase.client.RegionInfo; 041import org.apache.hadoop.hbase.client.RegionInfoBuilder; 042import org.apache.hadoop.hbase.client.TableDescriptor; 043import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 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.FutureUtils; 053import org.apache.hadoop.hbase.util.Threads; 054import org.apache.hadoop.hbase.wal.WALEdit; 055import org.apache.hadoop.hbase.wal.WALKey; 056import org.apache.hadoop.hbase.wal.WALKeyImpl; 057import org.apache.hadoop.hbase.wal.WALProvider.AsyncWriter; 058import org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder; 059import org.junit.AfterClass; 060import org.junit.BeforeClass; 061import org.junit.ClassRule; 062import org.junit.Test; 063import org.junit.experimental.categories.Category; 064 065import org.apache.hbase.thirdparty.io.netty.channel.Channel; 066import org.apache.hbase.thirdparty.io.netty.channel.EventLoopGroup; 067import org.apache.hbase.thirdparty.io.netty.channel.nio.NioEventLoopGroup; 068import org.apache.hbase.thirdparty.io.netty.channel.socket.nio.NioSocketChannel; 069 070/** 071 * Provides AsyncFSWAL test cases. 072 */ 073@Category({ RegionServerTests.class, LargeTests.class }) 074public class TestAsyncFSWAL extends AbstractTestFSWAL { 075 076 @ClassRule 077 public static final HBaseClassTestRule CLASS_RULE = 078 HBaseClassTestRule.forClass(TestAsyncFSWAL.class); 079 080 private static EventLoopGroup GROUP; 081 082 private static Class<? extends Channel> CHANNEL_CLASS; 083 084 @BeforeClass 085 public static void setUpBeforeClass() throws Exception { 086 GROUP = 087 new NioEventLoopGroup(1, new ThreadFactoryBuilder().setNameFormat("TestAsyncFSWAL-pool-%d") 088 .setDaemon(true).setUncaughtExceptionHandler(Threads.LOGGING_EXCEPTION_HANDLER).build()); 089 CHANNEL_CLASS = NioSocketChannel.class; 090 AbstractTestFSWAL.setUpBeforeClass(); 091 } 092 093 @AfterClass 094 public static void tearDownAfterClass() throws Exception { 095 AbstractTestFSWAL.tearDownAfterClass(); 096 GROUP.shutdownGracefully(); 097 } 098 099 @Override 100 protected AbstractFSWAL<?> newWAL(FileSystem fs, Path rootDir, String logDir, String archiveDir, 101 Configuration conf, List<WALActionsListener> listeners, boolean failIfWALExists, 102 String prefix, String suffix) throws IOException { 103 AsyncFSWAL asyncFSWAL = new AsyncFSWAL(fs, rootDir, logDir, archiveDir, conf, 104 listeners, failIfWALExists, prefix, suffix, GROUP, CHANNEL_CLASS); 105 asyncFSWAL.init(); 106 return asyncFSWAL; 107 } 108 109 @Override 110 protected AbstractFSWAL<?> newSlowWAL(FileSystem fs, Path rootDir, String logDir, 111 String archiveDir, Configuration conf, List<WALActionsListener> listeners, 112 boolean failIfWALExists, String prefix, String suffix, final Runnable action) 113 throws IOException { 114 AsyncFSWAL asyncFSWAL = new AsyncFSWAL(fs, rootDir, logDir, archiveDir, conf, listeners, 115 failIfWALExists, prefix, suffix, GROUP, CHANNEL_CLASS) { 116 @Override 117 protected void atHeadOfRingBufferEventHandlerAppend() { 118 action.run(); 119 super.atHeadOfRingBufferEventHandlerAppend(); 120 } 121 122 }; 123 asyncFSWAL.init(); 124 return asyncFSWAL; 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 = System.currentTimeMillis(); 140 String testName = currentTest.getMethodName(); 141 AtomicInteger failedCount = new AtomicInteger(0); 142 try (LogRoller roller = new LogRoller(services); 143 AsyncFSWAL wal = new AsyncFSWAL(FS, CommonFSUtils.getWALRootDir(CONF), DIR.toString(), 144 testName, CONF, null, true, null, null, GROUP, CHANNEL_CLASS) { 145 146 @Override 147 protected AsyncWriter createWriterInstance(Path path) throws IOException { 148 AsyncWriter writer = super.createWriterInstance(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 cols.add(new KeyValue(row, row, row, timestamp + index, row)); 201 WALKeyImpl key = new WALKeyImpl(ri.getEncodedNameAsBytes(), td.getTableName(), 202 SequenceId.NO_SEQUENCE_ID, timestamp, WALKey.EMPTY_UUIDS, HConstants.NO_NONCE, 203 HConstants.NO_NONCE, mvcc, scopes); 204 try { 205 wal.append(ri, key, cols, true); 206 } catch (IOException e) { 207 // should not happen 208 throw new UncheckedIOException(e); 209 } 210 try { 211 wal.sync(); 212 } catch (IOException e) { 213 error.set(e); 214 } 215 } 216 }; 217 } 218 for (Thread t : threads) { 219 t.start(); 220 } 221 for (Thread t : threads) { 222 t.join(); 223 } 224 assertNull(error.get()); 225 } 226 } 227}