1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.client;
19
20 import static org.junit.Assert.assertArrayEquals;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNull;
23 import static org.junit.Assert.assertNotNull;
24
25 import java.io.IOException;
26
27 import org.apache.hadoop.hbase.HBaseTestingUtility;
28 import org.apache.hadoop.hbase.HRegionInfo;
29 import org.apache.hadoop.hbase.TableName;
30 import org.apache.hadoop.hbase.regionserver.HRegionServer;
31 import org.apache.hadoop.hbase.testclassification.ClientTests;
32 import org.apache.hadoop.hbase.testclassification.LargeTests;
33 import org.apache.hadoop.hbase.util.Bytes;
34 import org.apache.hadoop.hbase.util.JVMClusterUtil;
35 import org.junit.AfterClass;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.experimental.categories.Category;
41 import org.junit.rules.TestName;
42
43 @Category({ LargeTests.class, ClientTests.class })
44 public class TestMvccConsistentScanner {
45
46 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
47
48 private static Connection CONN;
49
50 private static final byte[] CF = Bytes.toBytes("cf");
51
52 private static final byte[] CQ1 = Bytes.toBytes("cq1");
53
54 private static final byte[] CQ2 = Bytes.toBytes("cq2");
55
56 private static final byte[] CQ3 = Bytes.toBytes("cq3");
57 @Rule
58 public TestName testName = new TestName();
59
60 private TableName tableName;
61
62 @BeforeClass
63 public static void setUpBeforeClass() throws Exception {
64 UTIL.startMiniCluster(2);
65 CONN = ConnectionFactory.createConnection(UTIL.getConfiguration());
66 }
67
68 @AfterClass
69 public static void tearDownAfterClass() throws Exception {
70 CONN.close();
71 UTIL.shutdownMiniCluster();
72 }
73
74 @Before
75 public void setUp() throws IOException, InterruptedException {
76 tableName = TableName.valueOf(testName.getMethodName().replaceAll("[^0-9a-zA-Z]", "_"));
77 UTIL.createTable(tableName, CF);
78 UTIL.waitTableAvailable(tableName);
79 }
80
81 private void put(byte[] row, byte[] cq, byte[] value) throws IOException {
82 try (Table table = CONN.getTable(tableName)) {
83 table.put(new Put(row).addColumn(CF, cq, value));
84 }
85 }
86
87 private void move() throws IOException, InterruptedException {
88 HRegionInfo region = UTIL.getHBaseCluster().getRegions(tableName).get(0).getRegionInfo();
89 HRegionServer rs = null;
90 for (JVMClusterUtil.RegionServerThread thread : UTIL.getHBaseCluster()
91 .getRegionServerThreads()) {
92 if (!thread.getRegionServer().getOnlineTables().contains(tableName)) {
93 rs = thread.getRegionServer();
94 break;
95 }
96 }
97 assertNotNull(rs);
98 UTIL.getHBaseAdmin().move(region.getEncodedNameAsBytes(),
99 Bytes.toBytes(rs.getServerName().getServerName()));
100 while (UTIL.getRSForFirstRegionInTable(tableName) != rs) {
101 Thread.sleep(100);
102 }
103 }
104
105 @Test
106 public void testRowAtomic() throws IOException, InterruptedException {
107 byte[] row = Bytes.toBytes("row");
108 put(row, CQ1, Bytes.toBytes(1));
109 put(row, CQ2, Bytes.toBytes(2));
110 try (Table table = CONN.getTable(tableName);
111 ResultScanner scanner = table.getScanner(new Scan().setBatch(1).setCaching(1))) {
112 Result result = scanner.next();
113 assertEquals(1, result.rawCells().length);
114 assertEquals(1, Bytes.toInt(result.getValue(CF, CQ1)));
115 move();
116 put(row, CQ3, Bytes.toBytes(3));
117 result = scanner.next();
118 assertEquals(1, result.rawCells().length);
119 assertEquals(2, Bytes.toInt(result.getValue(CF, CQ2)));
120 assertNull(scanner.next());
121 }
122 }
123
124 @Test
125 public void testCrossRowAtomicInRegion() throws IOException, InterruptedException {
126 put(Bytes.toBytes("row1"), CQ1, Bytes.toBytes(1));
127 put(Bytes.toBytes("row2"), CQ1, Bytes.toBytes(2));
128 try (Table table = CONN.getTable(tableName);
129 ResultScanner scanner = table.getScanner(new Scan().setCaching(1))) {
130 Result result = scanner.next();
131 assertArrayEquals(Bytes.toBytes("row1"), result.getRow());
132 assertEquals(1, Bytes.toInt(result.getValue(CF, CQ1)));
133 move();
134 put(Bytes.toBytes("row3"), CQ1, Bytes.toBytes(3));
135 result = scanner.next();
136 assertArrayEquals(Bytes.toBytes("row2"), result.getRow());
137 assertEquals(2, Bytes.toInt(result.getValue(CF, CQ1)));
138 assertNull(scanner.next());
139 }
140 }
141 }