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