1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.regionserver;
19
20 import static org.junit.Assert.assertEquals;
21
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.concurrent.CountDownLatch;
26
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.fs.FileSystem;
29 import org.apache.hadoop.fs.Path;
30 import org.apache.hadoop.hbase.HBaseConfiguration;
31 import org.apache.hadoop.hbase.HConstants;
32 import org.apache.hadoop.hbase.HRegionInfo;
33 import org.apache.hadoop.hbase.HTableDescriptor;
34 import org.apache.hadoop.hbase.KeyValue;
35 import org.apache.hadoop.hbase.SmallTests;
36 import org.apache.hadoop.hbase.MultithreadedTestUtil;
37 import org.apache.hadoop.hbase.MultithreadedTestUtil.TestContext;
38 import org.apache.hadoop.hbase.MultithreadedTestUtil.TestThread;
39 import org.apache.hadoop.hbase.client.Mutation;
40 import org.apache.hadoop.hbase.client.Put;
41 import org.apache.hadoop.hbase.client.Scan;
42 import org.apache.hadoop.hbase.filter.BinaryComparator;
43 import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
44 import org.apache.hadoop.hbase.io.HeapSize;
45 import org.apache.hadoop.hbase.regionserver.wal.HLog;
46 import org.apache.hadoop.hbase.util.Bytes;
47 import org.apache.hadoop.hbase.util.Pair;
48 import org.junit.Test;
49 import org.junit.experimental.categories.Category;
50
51 import com.google.common.collect.Lists;
52
53
54
55
56
57
58
59
60 @Category(SmallTests.class)
61 public class TestHBase7051 {
62
63 private static CountDownLatch latch = new CountDownLatch(1);
64 private enum TestStep {
65 INIT,
66 PUT_STARTED,
67 PUT_COMPLETED,
68 CHECKANDPUT_STARTED,
69 CHECKANDPUT_COMPLETED
70
71 }
72 private static volatile TestStep testStep = TestStep.INIT;
73 private final String family = "f1";
74
75 @Test
76 public void testPutAndCheckAndPutInParallel() throws Exception {
77
78 final String tableName = "testPutAndCheckAndPut";
79 Configuration conf = HBaseConfiguration.create();
80 conf.setClass(HConstants.REGION_IMPL, MockHRegion.class, HeapSize.class);
81 final MockHRegion region = (MockHRegion) TestHRegion.initHRegion(Bytes.toBytes(tableName),
82 tableName, conf, Bytes.toBytes(family));
83
84 List<Pair<Mutation, Integer>> putsAndLocks = Lists.newArrayList();
85 Put[] puts = new Put[1];
86 Put put = new Put(Bytes.toBytes("r1"));
87 put.add(Bytes.toBytes(family), Bytes.toBytes("q1"), Bytes.toBytes("10"));
88 puts[0] = put;
89 Pair<Mutation, Integer> pair = new Pair<Mutation, Integer>(puts[0], null);
90
91 putsAndLocks.add(pair);
92
93 region.batchMutate(putsAndLocks.toArray(new Pair[0]));
94 MultithreadedTestUtil.TestContext ctx =
95 new MultithreadedTestUtil.TestContext(conf);
96 ctx.addThread(new PutThread(ctx, region));
97 ctx.addThread(new CheckAndPutThread(ctx, region));
98 ctx.startThreads();
99 while (testStep != TestStep.CHECKANDPUT_COMPLETED) {
100 Thread.sleep(100);
101 }
102 ctx.stop();
103 Scan s = new Scan();
104 RegionScanner scanner = region.getScanner(s);
105 List<KeyValue> results = new ArrayList<KeyValue>();
106 scanner.next(results, 2);
107 for (KeyValue keyValue : results) {
108 assertEquals("50",Bytes.toString(keyValue.getValue()));
109 }
110
111 }
112
113 private class PutThread extends TestThread {
114 private MockHRegion region;
115 PutThread(TestContext ctx, MockHRegion region) {
116 super(ctx);
117 this.region = region;
118 }
119
120 public void doWork() throws Exception {
121 List<Pair<Mutation, Integer>> putsAndLocks = Lists.newArrayList();
122 Put[] puts = new Put[1];
123 Put put = new Put(Bytes.toBytes("r1"));
124 put.add(Bytes.toBytes(family), Bytes.toBytes("q1"), Bytes.toBytes("50"));
125 puts[0] = put;
126 Pair<Mutation, Integer> pair = new Pair<Mutation, Integer>(puts[0], null);
127 putsAndLocks.add(pair);
128 testStep = TestStep.PUT_STARTED;
129 region.batchMutate(putsAndLocks.toArray(new Pair[0]));
130 }
131 }
132
133 private class CheckAndPutThread extends TestThread {
134 private MockHRegion region;
135 CheckAndPutThread(TestContext ctx, MockHRegion region) {
136 super(ctx);
137 this.region = region;
138 }
139
140 public void doWork() throws Exception {
141 Put[] puts = new Put[1];
142 Put put = new Put(Bytes.toBytes("r1"));
143 put.add(Bytes.toBytes(family), Bytes.toBytes("q1"), Bytes.toBytes("11"));
144 puts[0] = put;
145 while (testStep != TestStep.PUT_COMPLETED) {
146 Thread.sleep(100);
147 }
148 testStep = TestStep.CHECKANDPUT_STARTED;
149 region.checkAndMutate(Bytes.toBytes("r1"), Bytes.toBytes(family), Bytes.toBytes("q1"),
150 CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("10")), put, null, true);
151 testStep = TestStep.CHECKANDPUT_COMPLETED;
152 }
153 }
154
155 public static class MockHRegion extends HRegion {
156
157 public MockHRegion(Path tableDir, HLog log, FileSystem fs, Configuration conf,
158 final HRegionInfo regionInfo, final HTableDescriptor htd, RegionServerServices rsServices) {
159 super(tableDir, log, fs, conf, regionInfo, htd, rsServices);
160 }
161
162 @Override
163 public void releaseRowLock(Integer lockId) {
164 if (testStep == TestStep.INIT) {
165 super.releaseRowLock(lockId);
166 return;
167 }
168
169 if (testStep == TestStep.PUT_STARTED) {
170 try {
171 testStep = TestStep.PUT_COMPLETED;
172 super.releaseRowLock(lockId);
173
174
175
176
177
178
179
180
181
182
183 latch.await();
184 Thread.sleep(1000);
185 } catch (InterruptedException e) {
186 Thread.currentThread().interrupt();
187 }
188 }
189 else if (testStep == TestStep.CHECKANDPUT_STARTED) {
190 super.releaseRowLock(lockId);
191 }
192 }
193
194 @Override
195 public Integer getLock(Integer lockid, byte[] row, boolean waitForLock) throws IOException {
196 if (testStep == TestStep.CHECKANDPUT_STARTED) {
197 latch.countDown();
198 }
199 return super.getLock(lockid, row, waitForLock);
200 }
201
202 }
203
204 }