View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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 }