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; 019 020import static org.junit.Assert.fail; 021 022import java.io.IOException; 023import java.util.ArrayList; 024import java.util.Arrays; 025import java.util.List; 026import java.util.concurrent.CountDownLatch; 027import org.apache.hadoop.conf.Configuration; 028import org.apache.hadoop.fs.FileSystem; 029import org.apache.hadoop.fs.Path; 030import org.apache.hadoop.hbase.HBaseClassTestRule; 031import org.apache.hadoop.hbase.HBaseTestingUtility; 032import org.apache.hadoop.hbase.HConstants; 033import org.apache.hadoop.hbase.TableName; 034import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 035import org.apache.hadoop.hbase.client.Increment; 036import org.apache.hadoop.hbase.client.Mutation; 037import org.apache.hadoop.hbase.client.Put; 038import org.apache.hadoop.hbase.client.RegionInfo; 039import org.apache.hadoop.hbase.client.RegionInfoBuilder; 040import org.apache.hadoop.hbase.client.TableDescriptor; 041import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 042import org.apache.hadoop.hbase.regionserver.wal.AbstractFSWAL; 043import org.apache.hadoop.hbase.testclassification.RegionServerTests; 044import org.apache.hadoop.hbase.testclassification.SmallTests; 045import org.apache.hadoop.hbase.util.Bytes; 046import org.apache.hadoop.hbase.util.FSUtils; 047import org.apache.hadoop.hbase.wal.WAL; 048import org.apache.hadoop.hbase.wal.WALEdit; 049import org.apache.hadoop.hbase.wal.WALFactory; 050import org.junit.After; 051import org.junit.AfterClass; 052import org.junit.Before; 053import org.junit.ClassRule; 054import org.junit.Rule; 055import org.junit.Test; 056import org.junit.experimental.categories.Category; 057import org.junit.rules.TestName; 058import org.junit.runner.RunWith; 059import org.junit.runners.Parameterized; 060import org.junit.runners.Parameterized.Parameter; 061import org.junit.runners.Parameterized.Parameters; 062import org.slf4j.Logger; 063import org.slf4j.LoggerFactory; 064 065/** 066 * Test for HBASE-17471. 067 * <p> 068 * MVCCPreAssign is added by HBASE-16698, but pre-assign mvcc is only used in put/delete path. Other 069 * write paths like increment/append still assign mvcc in ringbuffer's consumer thread. If put and 070 * increment are used parallel. Then seqid in WAL may not increase monotonically Disorder in wals 071 * will lead to data loss. 072 * <p> 073 * This case use two thread to put and increment at the same time in a single region. Then check the 074 * seqid in WAL. If seqid is wal is not monotonically increasing, this case will fail 075 */ 076@RunWith(Parameterized.class) 077@Category({ RegionServerTests.class, SmallTests.class }) 078public class TestWALMonotonicallyIncreasingSeqId { 079 080 @ClassRule 081 public static final HBaseClassTestRule CLASS_RULE = 082 HBaseClassTestRule.forClass(TestWALMonotonicallyIncreasingSeqId.class); 083 084 private final Logger LOG = LoggerFactory.getLogger(getClass()); 085 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 086 private static Path testDir = TEST_UTIL.getDataTestDir("TestWALMonotonicallyIncreasingSeqId"); 087 private WALFactory wals; 088 private FileSystem fileSystem; 089 private Configuration walConf; 090 private HRegion region; 091 092 @Parameter 093 public String walProvider; 094 095 @Rule 096 public TestName name = new TestName(); 097 098 @Parameters(name = "{index}: wal={0}") 099 public static List<Object[]> data() { 100 return Arrays.asList(new Object[] { "asyncfs" }, new Object[] { "filesystem" }); 101 } 102 103 private TableDescriptor getTableDesc(TableName tableName, byte[]... families) { 104 TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName); 105 Arrays.stream(families).map( 106 f -> ColumnFamilyDescriptorBuilder.newBuilder(f).setMaxVersions(Integer.MAX_VALUE).build()) 107 .forEachOrdered(builder::setColumnFamily); 108 return builder.build(); 109 } 110 111 private HRegion initHRegion(TableDescriptor htd, byte[] startKey, byte[] stopKey, int replicaId) 112 throws IOException { 113 Configuration conf = TEST_UTIL.getConfiguration(); 114 conf.set("hbase.wal.provider", walProvider); 115 conf.setBoolean("hbase.hregion.mvcc.preassign", false); 116 Path tableDir = FSUtils.getTableDir(testDir, htd.getTableName()); 117 118 RegionInfo info = RegionInfoBuilder.newBuilder(htd.getTableName()).setStartKey(startKey) 119 .setEndKey(stopKey).setReplicaId(replicaId).setRegionId(0).build(); 120 fileSystem = tableDir.getFileSystem(conf); 121 final Configuration walConf = new Configuration(conf); 122 FSUtils.setRootDir(walConf, tableDir); 123 this.walConf = walConf; 124 wals = new WALFactory(walConf, "log_" + replicaId); 125 ChunkCreator.initialize(MemStoreLABImpl.CHUNK_SIZE_DEFAULT, false, 0, 0, 0, null); 126 HRegion region = HRegion.createHRegion(info, TEST_UTIL.getDefaultRootDirPath(), conf, htd, 127 wals.getWAL(info)); 128 return region; 129 } 130 131 CountDownLatch latch = new CountDownLatch(1); 132 133 public class PutThread extends Thread { 134 HRegion region; 135 136 public PutThread(HRegion region) { 137 this.region = region; 138 } 139 140 @Override 141 public void run() { 142 try { 143 for (int i = 0; i < 100; i++) { 144 byte[] row = Bytes.toBytes("putRow" + i); 145 Put put = new Put(row); 146 put.addColumn("cf".getBytes(), Bytes.toBytes(0), Bytes.toBytes("")); 147 latch.await(); 148 region.batchMutate(new Mutation[] { put }); 149 Thread.sleep(10); 150 } 151 152 } catch (Throwable t) { 153 LOG.warn("Error happend when Increment: ", t); 154 } 155 } 156 } 157 158 public class IncThread extends Thread { 159 HRegion region; 160 161 public IncThread(HRegion region) { 162 this.region = region; 163 } 164 165 @Override 166 public void run() { 167 try { 168 for (int i = 0; i < 100; i++) { 169 byte[] row = Bytes.toBytes("incrementRow" + i); 170 Increment inc = new Increment(row); 171 inc.addColumn("cf".getBytes(), Bytes.toBytes(0), 1); 172 // inc.setDurability(Durability.ASYNC_WAL); 173 region.increment(inc); 174 latch.countDown(); 175 Thread.sleep(10); 176 } 177 178 } catch (Throwable t) { 179 LOG.warn("Error happend when Put: ", t); 180 } 181 } 182 } 183 184 @Before 185 public void setUp() throws IOException { 186 byte[][] families = new byte[][] { Bytes.toBytes("cf") }; 187 TableDescriptor htd = getTableDesc( 188 TableName.valueOf(name.getMethodName().replaceAll("[^0-9A-Za-z_]", "_")), families); 189 region = initHRegion(htd, HConstants.EMPTY_START_ROW, HConstants.EMPTY_END_ROW, 0); 190 } 191 192 @After 193 public void tearDown() throws IOException { 194 if (region != null) { 195 region.close(); 196 } 197 } 198 199 @AfterClass 200 public static void tearDownAfterClass() throws IOException { 201 TEST_UTIL.cleanupTestDir(); 202 } 203 204 private WAL.Reader createReader(Path logPath, Path oldWalsDir) throws IOException { 205 try { 206 return wals.createReader(fileSystem, logPath); 207 } catch (IOException e) { 208 return wals.createReader(fileSystem, new Path(oldWalsDir, logPath.getName())); 209 } 210 } 211 212 @Test 213 public void testWALMonotonicallyIncreasingSeqId() throws Exception { 214 List<Thread> putThreads = new ArrayList<>(); 215 for (int i = 0; i < 1; i++) { 216 putThreads.add(new PutThread(region)); 217 } 218 IncThread incThread = new IncThread(region); 219 for (int i = 0; i < 1; i++) { 220 putThreads.get(i).start(); 221 } 222 incThread.start(); 223 incThread.join(); 224 225 Path logPath = ((AbstractFSWAL<?>) region.getWAL()).getCurrentFileName(); 226 region.getWAL().rollWriter(); 227 Thread.sleep(10); 228 Path hbaseDir = new Path(walConf.get(HConstants.HBASE_DIR)); 229 Path oldWalsDir = new Path(hbaseDir, HConstants.HREGION_OLDLOGDIR_NAME); 230 try (WAL.Reader reader = createReader(logPath, oldWalsDir)) { 231 long currentMaxSeqid = 0; 232 for (WAL.Entry e; (e = reader.next()) != null;) { 233 if (!WALEdit.isMetaEditFamily(e.getEdit().getCells().get(0))) { 234 long currentSeqid = e.getKey().getSequenceId(); 235 if (currentSeqid > currentMaxSeqid) { 236 currentMaxSeqid = currentSeqid; 237 } else { 238 fail("Current max Seqid is " + currentMaxSeqid + 239 ", but the next seqid in wal is smaller:" + currentSeqid); 240 } 241 } 242 } 243 } 244 } 245}