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