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.assertNotEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.io.IOException;
024import java.util.ArrayList;
025import java.util.List;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseTestingUtil;
028import org.apache.hadoop.hbase.TableName;
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.BeforeClass;
034import org.junit.ClassRule;
035import org.junit.Test;
036import org.junit.experimental.categories.Category;
037
038@Category({ MediumTests.class, ClientTests.class })
039public class TestRegionLocationCaching {
040
041  @ClassRule
042  public static final HBaseClassTestRule CLASS_RULE =
043    HBaseClassTestRule.forClass(TestRegionLocationCaching.class);
044
045  private final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
046  private static int SLAVES = 1;
047  private static TableName TABLE_NAME = TableName.valueOf("TestRegionLocationCaching");
048  private static byte[] FAMILY = Bytes.toBytes("testFamily");
049  private static byte[] QUALIFIER = Bytes.toBytes("testQualifier");
050
051  @BeforeClass
052  public static void setUpBeforeClass() throws Exception {
053    TEST_UTIL.startMiniCluster(SLAVES);
054    TEST_UTIL.createTable(TABLE_NAME, new byte[][] { FAMILY });
055    TEST_UTIL.waitUntilAllRegionsAssigned(TABLE_NAME);
056  }
057
058  @AfterClass
059  public static void tearDownAfterClass() throws Exception {
060    TEST_UTIL.shutdownMiniCluster();
061  }
062
063  @Test
064  public void testCachingForHTableSinglePut() throws Exception {
065    byte[] row = Bytes.toBytes("htable_single_put");
066    byte[] value = Bytes.toBytes("value");
067
068    Put put = new Put(row);
069    put.addColumn(FAMILY, QUALIFIER, value);
070
071    try (Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME)) {
072      table.put(put);
073    }
074
075    checkRegionLocationIsCached(TABLE_NAME, TEST_UTIL.getConnection());
076    checkExistence(TABLE_NAME, row, FAMILY, QUALIFIER);
077  }
078
079  @Test
080  public void testCachingForHTableMultiPut() throws Exception {
081    List<Put> multiput = new ArrayList<Put>();
082    for (int i = 0; i < 10; i++) {
083      Put put = new Put(Bytes.toBytes("htable_multi_put" + i));
084      byte[] value = Bytes.toBytes("value_" + i);
085      put.addColumn(FAMILY, QUALIFIER, value);
086      multiput.add(put);
087    }
088
089    try (Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME)) {
090      table.put(multiput);
091    }
092    checkRegionLocationIsCached(TABLE_NAME, TEST_UTIL.getConnection());
093    for (int i = 0; i < 10; i++) {
094      checkExistence(TABLE_NAME, Bytes.toBytes("htable_multi_put" + i), FAMILY, QUALIFIER);
095    }
096  }
097
098  /**
099   * Method to check whether the cached region location is non-empty for the given table. It repeats
100   * the same check several times as clearing of cache by some async operations may not reflect
101   * immediately.
102   */
103  private void checkRegionLocationIsCached(final TableName tableName, final Connection conn)
104    throws InterruptedException, IOException {
105    for (int count = 0; count < 50; count++) {
106      int number = ((AsyncConnectionImpl) conn.toAsyncConnection()).getLocator()
107        .getNumberOfCachedRegionLocations(tableName);
108      assertNotEquals("Expected non-zero number of cached region locations", 0, number);
109      Thread.sleep(100);
110    }
111  }
112
113  /**
114   * Method to check whether the passed row exists in the given table
115   */
116  private static void checkExistence(final TableName tableName, final byte[] row,
117    final byte[] family, final byte[] qualifier) throws Exception {
118    // verify that the row exists
119    Result r;
120    Get get = new Get(row);
121    get.addColumn(family, qualifier);
122    int nbTry = 0;
123    try (Table table = TEST_UTIL.getConnection().getTable(tableName)) {
124      do {
125        assertTrue("Failed to get row after " + nbTry + " tries", nbTry < 50);
126        nbTry++;
127        Thread.sleep(100);
128        r = table.get(get);
129      } while (r == null || r.getValue(family, qualifier) == null);
130    }
131  }
132}