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.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.util.ArrayList;
024import java.util.List;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseTestingUtility;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.testclassification.ClientTests;
029import org.apache.hadoop.hbase.testclassification.LargeTests;
030import org.apache.hadoop.hbase.util.Bytes;
031import org.junit.AfterClass;
032import org.junit.BeforeClass;
033import org.junit.ClassRule;
034import org.junit.Rule;
035import org.junit.Test;
036import org.junit.experimental.categories.Category;
037import org.junit.rules.TestName;
038import org.slf4j.Logger;
039import org.slf4j.LoggerFactory;
040
041@Category({LargeTests.class, ClientTests.class})
042public class TestHTableMultiplexer {
043
044  @ClassRule
045  public static final HBaseClassTestRule CLASS_RULE =
046      HBaseClassTestRule.forClass(TestHTableMultiplexer.class);
047
048  private static final Logger LOG = LoggerFactory.getLogger(TestHTableMultiplexer.class);
049  private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
050  private static byte[] FAMILY = Bytes.toBytes("testFamily");
051  private static byte[] QUALIFIER = Bytes.toBytes("testQualifier");
052  private static byte[] VALUE1 = Bytes.toBytes("testValue1");
053  private static byte[] VALUE2 = Bytes.toBytes("testValue2");
054  private static int SLAVES = 3;
055  private static int PER_REGIONSERVER_QUEUE_SIZE = 100000;
056
057  @Rule
058  public TestName name = new TestName();
059
060  /**
061   * @throws java.lang.Exception
062   */
063  @BeforeClass
064  public static void setUpBeforeClass() throws Exception {
065    TEST_UTIL.startMiniCluster(SLAVES);
066  }
067
068  /**
069   * @throws java.lang.Exception
070   */
071  @AfterClass
072  public static void tearDownAfterClass() throws Exception {
073    TEST_UTIL.shutdownMiniCluster();
074  }
075
076  private static void checkExistence(Table htable, byte[] row, byte[] family, byte[] quality)
077      throws Exception {
078    // verify that the Get returns the correct result
079    Result r;
080    Get get = new Get(row);
081    get.addColumn(FAMILY, QUALIFIER);
082    int nbTry = 0;
083    do {
084      assertTrue("Fail to get from " + htable.getName() + " after " + nbTry + " tries", nbTry < 50);
085      nbTry++;
086      Thread.sleep(100);
087      r = htable.get(get);
088    } while (r == null || r.getValue(FAMILY, QUALIFIER) == null);
089    assertEquals("value", Bytes.toStringBinary(VALUE1),
090      Bytes.toStringBinary(r.getValue(FAMILY, QUALIFIER)));
091  }
092
093  @Test
094  public void testHTableMultiplexer() throws Exception {
095    final TableName tableName1 = TableName.valueOf(name.getMethodName() + "_1");
096    final TableName tableName2 = TableName.valueOf(name.getMethodName() + "_2");
097    final int NUM_REGIONS = 10;
098    final int VERSION = 3;
099    List<Put> failedPuts;
100    boolean success;
101
102    HTableMultiplexer multiplexer = new HTableMultiplexer(TEST_UTIL.getConfiguration(),
103        PER_REGIONSERVER_QUEUE_SIZE);
104
105    Table htable1 =
106        TEST_UTIL.createTable(tableName1, new byte[][] { FAMILY }, VERSION,
107        Bytes.toBytes("aaaaa"), Bytes.toBytes("zzzzz"), NUM_REGIONS);
108    Table htable2 =
109        TEST_UTIL.createTable(tableName2, new byte[][] { FAMILY }, VERSION, Bytes.toBytes("aaaaa"),
110          Bytes.toBytes("zzzzz"), NUM_REGIONS);
111    TEST_UTIL.waitUntilAllRegionsAssigned(tableName1);
112    TEST_UTIL.waitUntilAllRegionsAssigned(tableName2);
113
114    try (RegionLocator rl = TEST_UTIL.getConnection().getRegionLocator(tableName1)) {
115      byte[][] startRows = rl.getStartKeys();
116      byte[][] endRows = rl.getEndKeys();
117
118      // SinglePut case
119      for (int i = 0; i < NUM_REGIONS; i++) {
120        byte [] row = startRows[i];
121        if (row == null || row.length <= 0) continue;
122        Put put = new Put(row).addColumn(FAMILY, QUALIFIER, VALUE1);
123        success = multiplexer.put(tableName1, put);
124        assertTrue("multiplexer.put returns", success);
125
126        put = new Put(row).addColumn(FAMILY, QUALIFIER, VALUE1);
127        success = multiplexer.put(tableName2, put);
128        assertTrue("multiplexer.put failed", success);
129
130        LOG.info("Put for " + Bytes.toStringBinary(startRows[i]) + " @ iteration " + (i + 1));
131
132        // verify that the Get returns the correct result
133        checkExistence(htable1, startRows[i], FAMILY, QUALIFIER);
134        checkExistence(htable2, startRows[i], FAMILY, QUALIFIER);
135      }
136
137      // MultiPut case
138      List<Put> multiput = new ArrayList<>();
139      for (int i = 0; i < NUM_REGIONS; i++) {
140        byte [] row = endRows[i];
141        if (row == null || row.length <= 0) continue;
142        Put put = new Put(row);
143        put.addColumn(FAMILY, QUALIFIER, VALUE2);
144        multiput.add(put);
145      }
146      failedPuts = multiplexer.put(tableName1, multiput);
147      assertTrue(failedPuts == null);
148
149      // verify that the Get returns the correct result
150      for (int i = 0; i < NUM_REGIONS; i++) {
151        byte [] row = endRows[i];
152        if (row == null || row.length <= 0) continue;
153        Get get = new Get(row);
154        get.addColumn(FAMILY, QUALIFIER);
155        Result r;
156        int nbTry = 0;
157        do {
158          assertTrue(nbTry++ < 50);
159          Thread.sleep(100);
160          r = htable1.get(get);
161        } while (r == null || r.getValue(FAMILY, QUALIFIER) == null ||
162            Bytes.compareTo(VALUE2, r.getValue(FAMILY, QUALIFIER)) != 0);
163      }
164    }
165  }
166}