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.CommonFSUtils; 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 = CommonFSUtils.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 CommonFSUtils.setRootDir(walConf, tableDir); 123 this.walConf = walConf; 124 wals = new WALFactory(walConf, "log_" + replicaId); 125 ChunkCreator.initialize(MemStoreLAB.CHUNK_SIZE_DEFAULT, false, 0, 0, 126 0, null, MemStoreLAB.INDEX_CHUNK_SIZE_PERCENTAGE_DEFAULT); 127 HRegion region = HRegion.createHRegion(info, TEST_UTIL.getDefaultRootDirPath(), conf, htd, 128 wals.getWAL(info)); 129 return region; 130 } 131 132 CountDownLatch latch = new CountDownLatch(1); 133 134 public class PutThread extends Thread { 135 HRegion region; 136 137 public PutThread(HRegion region) { 138 this.region = region; 139 } 140 141 @Override 142 public void run() { 143 try { 144 for (int i = 0; i < 100; i++) { 145 byte[] row = Bytes.toBytes("putRow" + i); 146 Put put = new Put(row); 147 put.addColumn("cf".getBytes(), Bytes.toBytes(0), Bytes.toBytes("")); 148 latch.await(); 149 region.batchMutate(new Mutation[] { put }); 150 Thread.sleep(10); 151 } 152 153 } catch (Throwable t) { 154 LOG.warn("Error happend when Increment: ", t); 155 } 156 } 157 } 158 159 public class IncThread extends Thread { 160 HRegion region; 161 162 public IncThread(HRegion region) { 163 this.region = region; 164 } 165 166 @Override 167 public void run() { 168 try { 169 for (int i = 0; i < 100; i++) { 170 byte[] row = Bytes.toBytes("incrementRow" + i); 171 Increment inc = new Increment(row); 172 inc.addColumn("cf".getBytes(), Bytes.toBytes(0), 1); 173 // inc.setDurability(Durability.ASYNC_WAL); 174 region.increment(inc); 175 latch.countDown(); 176 Thread.sleep(10); 177 } 178 179 } catch (Throwable t) { 180 LOG.warn("Error happend when Put: ", t); 181 } 182 } 183 } 184 185 @Before 186 public void setUp() throws IOException { 187 byte[][] families = new byte[][] { Bytes.toBytes("cf") }; 188 TableDescriptor htd = getTableDesc( 189 TableName.valueOf(name.getMethodName().replaceAll("[^0-9A-Za-z_]", "_")), families); 190 region = initHRegion(htd, HConstants.EMPTY_START_ROW, HConstants.EMPTY_END_ROW, 0); 191 } 192 193 @After 194 public void tearDown() throws IOException { 195 if (region != null) { 196 region.close(); 197 } 198 } 199 200 @AfterClass 201 public static void tearDownAfterClass() throws IOException { 202 TEST_UTIL.cleanupTestDir(); 203 } 204 205 private WAL.Reader createReader(Path logPath, Path oldWalsDir) throws IOException { 206 try { 207 return wals.createReader(fileSystem, logPath); 208 } catch (IOException e) { 209 return wals.createReader(fileSystem, new Path(oldWalsDir, logPath.getName())); 210 } 211 } 212 213 @Test 214 public void testWALMonotonicallyIncreasingSeqId() throws Exception { 215 List<Thread> putThreads = new ArrayList<>(); 216 for (int i = 0; i < 1; i++) { 217 putThreads.add(new PutThread(region)); 218 } 219 IncThread incThread = new IncThread(region); 220 for (int i = 0; i < 1; i++) { 221 putThreads.get(i).start(); 222 } 223 incThread.start(); 224 incThread.join(); 225 226 Path logPath = ((AbstractFSWAL<?>) region.getWAL()).getCurrentFileName(); 227 region.getWAL().rollWriter(); 228 Thread.sleep(10); 229 Path hbaseDir = new Path(walConf.get(HConstants.HBASE_DIR)); 230 Path oldWalsDir = new Path(hbaseDir, HConstants.HREGION_OLDLOGDIR_NAME); 231 try (WAL.Reader reader = createReader(logPath, oldWalsDir)) { 232 long currentMaxSeqid = 0; 233 for (WAL.Entry e; (e = reader.next()) != null;) { 234 if (!WALEdit.isMetaEditFamily(e.getEdit().getCells().get(0))) { 235 long currentSeqid = e.getKey().getSequenceId(); 236 if (currentSeqid > currentMaxSeqid) { 237 currentMaxSeqid = currentSeqid; 238 } else { 239 fail("Current max Seqid is " + currentMaxSeqid + 240 ", but the next seqid in wal is smaller:" + currentSeqid); 241 } 242 } 243 } 244 } 245 } 246}