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.Server; 039import org.apache.hadoop.hbase.TableName; 040import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 041import org.apache.hadoop.hbase.client.RegionInfo; 042import org.apache.hadoop.hbase.client.RegionInfoBuilder; 043import org.apache.hadoop.hbase.client.TableDescriptor; 044import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 045import org.apache.hadoop.hbase.regionserver.LogRoller; 046import org.apache.hadoop.hbase.regionserver.MultiVersionConcurrencyControl; 047import org.apache.hadoop.hbase.regionserver.RegionServerServices; 048import org.apache.hadoop.hbase.regionserver.SequenceId; 049import org.apache.hadoop.hbase.testclassification.LargeTests; 050import org.apache.hadoop.hbase.testclassification.RegionServerTests; 051import org.apache.hadoop.hbase.util.Bytes; 052import org.apache.hadoop.hbase.util.CommonFSUtils; 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.WALKey; 057import org.apache.hadoop.hbase.wal.WALKeyImpl; 058import org.apache.hadoop.hbase.wal.WALProvider.AsyncWriter; 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 = new NioEventLoopGroup(1, Threads.newDaemonThreadFactory("TestAsyncFSWAL")); 087 CHANNEL_CLASS = NioSocketChannel.class; 088 AbstractTestFSWAL.setUpBeforeClass(); 089 } 090 091 @AfterClass 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, 100 String prefix, String suffix) throws IOException { 101 return new AsyncFSWAL(fs, rootDir, logDir, archiveDir, conf, listeners, failIfWALExists, prefix, 102 suffix, GROUP, CHANNEL_CLASS); 103 } 104 105 @Override 106 protected AbstractFSWAL<?> newSlowWAL(FileSystem fs, Path rootDir, String logDir, 107 String archiveDir, Configuration conf, List<WALActionsListener> listeners, 108 boolean failIfWALExists, String prefix, String suffix, final Runnable action) 109 throws IOException { 110 return new AsyncFSWAL(fs, rootDir, logDir, archiveDir, conf, listeners, failIfWALExists, prefix, 111 suffix, GROUP, CHANNEL_CLASS) { 112 113 @Override 114 protected void atHeadOfRingBufferEventHandlerAppend() { 115 action.run(); 116 super.atHeadOfRingBufferEventHandlerAppend(); 117 } 118 119 }; 120 } 121 122 @Test 123 public void testBrokenWriter() throws Exception { 124 Server server = mock(Server.class); 125 when(server.getConfiguration()).thenReturn(CONF); 126 RegionServerServices services = mock(RegionServerServices.class); 127 TableDescriptor td = TableDescriptorBuilder.newBuilder(TableName.valueOf("table")) 128 .setColumnFamily(ColumnFamilyDescriptorBuilder.of("row")).build(); 129 RegionInfo ri = RegionInfoBuilder.newBuilder(td.getTableName()).build(); 130 MultiVersionConcurrencyControl mvcc = new MultiVersionConcurrencyControl(); 131 NavigableMap<byte[], Integer> scopes = new TreeMap<>(Bytes.BYTES_COMPARATOR); 132 for (byte[] fam : td.getColumnFamilyNames()) { 133 scopes.put(fam, 0); 134 } 135 long timestamp = System.currentTimeMillis(); 136 String testName = currentTest.getMethodName(); 137 AtomicInteger failedCount = new AtomicInteger(0); 138 try (LogRoller roller = new LogRoller(server, services); 139 AsyncFSWAL wal = new AsyncFSWAL(FS, CommonFSUtils.getWALRootDir(CONF), DIR.toString(), 140 testName, CONF, null, true, null, null, GROUP, CHANNEL_CLASS) { 141 142 @Override 143 protected AsyncWriter createWriterInstance(Path path) throws IOException { 144 AsyncWriter writer = super.createWriterInstance(path); 145 return new AsyncWriter() { 146 147 @Override 148 public void close() throws IOException { 149 writer.close(); 150 } 151 152 @Override 153 public long getLength() { 154 return writer.getLength(); 155 } 156 157 @Override 158 public CompletableFuture<Long> sync() { 159 CompletableFuture<Long> result = writer.sync(); 160 if (failedCount.incrementAndGet() < 1000) { 161 CompletableFuture<Long> future = new CompletableFuture<>(); 162 FutureUtils.addListener(result, 163 (r, e) -> future.completeExceptionally(new IOException("Inject Error"))); 164 return future; 165 } else { 166 return result; 167 } 168 } 169 170 @Override 171 public void append(Entry entry) { 172 writer.append(entry); 173 } 174 }; 175 } 176 }) { 177 roller.addWAL(wal); 178 roller.start(); 179 int numThreads = 10; 180 AtomicReference<Exception> error = new AtomicReference<>(); 181 Thread[] threads = new Thread[numThreads]; 182 for (int i = 0; i < 10; i++) { 183 final int index = i; 184 threads[index] = new Thread("Write-Thread-" + index) { 185 186 @Override 187 public void run() { 188 byte[] row = Bytes.toBytes("row" + index); 189 WALEdit cols = new WALEdit(); 190 cols.add(new KeyValue(row, row, row, timestamp + index, row)); 191 WALKeyImpl key = new WALKeyImpl(ri.getEncodedNameAsBytes(), td.getTableName(), 192 SequenceId.NO_SEQUENCE_ID, timestamp, WALKey.EMPTY_UUIDS, HConstants.NO_NONCE, 193 HConstants.NO_NONCE, mvcc, scopes); 194 try { 195 wal.append(ri, key, cols, true); 196 } catch (IOException e) { 197 // should not happen 198 throw new UncheckedIOException(e); 199 } 200 try { 201 wal.sync(); 202 } catch (IOException e) { 203 error.set(e); 204 } 205 } 206 }; 207 } 208 for (Thread t : threads) { 209 t.start(); 210 } 211 for (Thread t : threads) { 212 t.join(); 213 } 214 assertNull(error.get()); 215 } 216 } 217}