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.client; 019 020import static org.junit.Assert.assertArrayEquals; 021import static org.junit.Assert.assertEquals; 022import static org.junit.Assert.assertNull; 023 024import java.io.IOException; 025import org.apache.hadoop.hbase.HBaseClassTestRule; 026import org.apache.hadoop.hbase.HBaseTestingUtil; 027import org.apache.hadoop.hbase.TableName; 028import org.apache.hadoop.hbase.regionserver.HRegionServer; 029import org.apache.hadoop.hbase.testclassification.ClientTests; 030import org.apache.hadoop.hbase.testclassification.MediumTests; 031import org.apache.hadoop.hbase.util.Bytes; 032import org.junit.AfterClass; 033import org.junit.Before; 034import org.junit.BeforeClass; 035import org.junit.ClassRule; 036import org.junit.Rule; 037import org.junit.Test; 038import org.junit.experimental.categories.Category; 039import org.junit.rules.TestName; 040 041@Category({ MediumTests.class, ClientTests.class }) 042public class TestMvccConsistentScanner { 043 044 @ClassRule 045 public static final HBaseClassTestRule CLASS_RULE = 046 HBaseClassTestRule.forClass(TestMvccConsistentScanner.class); 047 048 private static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); 049 050 private static Connection CONN; 051 052 private static final byte[] CF = Bytes.toBytes("cf"); 053 054 private static final byte[] CQ1 = Bytes.toBytes("cq1"); 055 056 private static final byte[] CQ2 = Bytes.toBytes("cq2"); 057 058 private static final byte[] CQ3 = Bytes.toBytes("cq3"); 059 @Rule 060 public TestName testName = new TestName(); 061 062 private TableName tableName; 063 064 @BeforeClass 065 public static void setUpBeforeClass() throws Exception { 066 UTIL.startMiniCluster(2); 067 CONN = ConnectionFactory.createConnection(UTIL.getConfiguration()); 068 } 069 070 @AfterClass 071 public static void tearDownAfterClass() throws Exception { 072 CONN.close(); 073 UTIL.shutdownMiniCluster(); 074 } 075 076 @Before 077 public void setUp() throws IOException, InterruptedException { 078 tableName = TableName.valueOf(testName.getMethodName().replaceAll("[^0-9a-zA-Z]", "_")); 079 UTIL.createTable(tableName, CF); 080 UTIL.waitTableAvailable(tableName); 081 } 082 083 private void put(byte[] row, byte[] cq, byte[] value) throws IOException { 084 try (Table table = CONN.getTable(tableName)) { 085 table.put(new Put(row).addColumn(CF, cq, value)); 086 } 087 } 088 089 private void move() throws IOException, InterruptedException { 090 RegionInfo region = 091 UTIL.getHBaseCluster().getRegions(tableName).stream().findAny().get().getRegionInfo(); 092 HRegionServer rs = 093 UTIL.getHBaseCluster().getRegionServerThreads().stream().map(t -> t.getRegionServer()) 094 .filter(r -> !r.getOnlineTables().contains(tableName)).findAny().get(); 095 UTIL.getAdmin().move(region.getEncodedNameAsBytes(), rs.getServerName()); 096 while (UTIL.getRSForFirstRegionInTable(tableName) != rs) { 097 Thread.sleep(100); 098 } 099 } 100 101 @Test 102 public void testRowAtomic() throws IOException, InterruptedException { 103 byte[] row = Bytes.toBytes("row"); 104 put(row, CQ1, Bytes.toBytes(1)); 105 put(row, CQ2, Bytes.toBytes(2)); 106 try (Table table = CONN.getTable(tableName); 107 ResultScanner scanner = table.getScanner(new Scan().setBatch(1).setCaching(1))) { 108 Result result = scanner.next(); 109 assertEquals(1, result.rawCells().length); 110 assertEquals(1, Bytes.toInt(result.getValue(CF, CQ1))); 111 move(); 112 put(row, CQ3, Bytes.toBytes(3)); 113 result = scanner.next(); 114 assertEquals(1, result.rawCells().length); 115 assertEquals(2, Bytes.toInt(result.getValue(CF, CQ2))); 116 assertNull(scanner.next()); 117 } 118 } 119 120 @Test 121 public void testCrossRowAtomicInRegion() throws IOException, InterruptedException { 122 put(Bytes.toBytes("row1"), CQ1, Bytes.toBytes(1)); 123 put(Bytes.toBytes("row2"), CQ1, Bytes.toBytes(2)); 124 try (Table table = CONN.getTable(tableName); 125 ResultScanner scanner = table.getScanner(new Scan().setCaching(1))) { 126 Result result = scanner.next(); 127 assertArrayEquals(Bytes.toBytes("row1"), result.getRow()); 128 assertEquals(1, Bytes.toInt(result.getValue(CF, CQ1))); 129 move(); 130 put(Bytes.toBytes("row3"), CQ1, Bytes.toBytes(3)); 131 result = scanner.next(); 132 assertArrayEquals(Bytes.toBytes("row2"), result.getRow()); 133 assertEquals(2, Bytes.toInt(result.getValue(CF, CQ1))); 134 assertNull(scanner.next()); 135 } 136 } 137}