HBASE-17532 Replaced explicit type with diamond operator
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / client / TestHTableMultiplexer.java
blob5c47de0c46fe6904e3d1ebbc89ce771599761b37
1 /**
2 * Copyright The Apache Software Foundation
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 package org.apache.hadoop.hbase.client;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
25 import java.util.ArrayList;
26 import java.util.List;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.hadoop.hbase.HBaseTestingUtility;
31 import org.apache.hadoop.hbase.TableName;
32 import org.apache.hadoop.hbase.testclassification.ClientTests;
33 import org.apache.hadoop.hbase.testclassification.LargeTests;
34 import org.apache.hadoop.hbase.util.Bytes;
35 import org.junit.AfterClass;
36 import org.junit.BeforeClass;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.experimental.categories.Category;
40 import org.junit.rules.TestName;
42 @Category({LargeTests.class, ClientTests.class})
43 public class TestHTableMultiplexer {
44 private static final Log LOG = LogFactory.getLog(TestHTableMultiplexer.class);
45 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
46 private static byte[] FAMILY = Bytes.toBytes("testFamily");
47 private static byte[] QUALIFIER = Bytes.toBytes("testQualifier");
48 private static byte[] VALUE1 = Bytes.toBytes("testValue1");
49 private static byte[] VALUE2 = Bytes.toBytes("testValue2");
50 private static int SLAVES = 3;
51 private static int PER_REGIONSERVER_QUEUE_SIZE = 100000;
53 @Rule
54 public TestName name = new TestName();
56 /**
57 * @throws java.lang.Exception
59 @BeforeClass
60 public static void setUpBeforeClass() throws Exception {
61 TEST_UTIL.startMiniCluster(SLAVES);
64 /**
65 * @throws java.lang.Exception
67 @AfterClass
68 public static void tearDownAfterClass() throws Exception {
69 TEST_UTIL.shutdownMiniCluster();
72 private static void checkExistence(Table htable, byte[] row, byte[] family, byte[] quality)
73 throws Exception {
74 // verify that the Get returns the correct result
75 Result r;
76 Get get = new Get(row);
77 get.addColumn(FAMILY, QUALIFIER);
78 int nbTry = 0;
79 do {
80 assertTrue("Fail to get from " + htable.getName() + " after " + nbTry + " tries", nbTry < 50);
81 nbTry++;
82 Thread.sleep(100);
83 r = htable.get(get);
84 } while (r == null || r.getValue(FAMILY, QUALIFIER) == null);
85 assertEquals("value", Bytes.toStringBinary(VALUE1),
86 Bytes.toStringBinary(r.getValue(FAMILY, QUALIFIER)));
89 @Test
90 public void testHTableMultiplexer() throws Exception {
91 final TableName tableName1 = TableName.valueOf(name.getMethodName() + "_1");
92 final TableName tableName2 = TableName.valueOf(name.getMethodName() + "_2");
93 final int NUM_REGIONS = 10;
94 final int VERSION = 3;
95 List<Put> failedPuts;
96 boolean success;
98 HTableMultiplexer multiplexer = new HTableMultiplexer(TEST_UTIL.getConfiguration(),
99 PER_REGIONSERVER_QUEUE_SIZE);
101 Table htable1 =
102 TEST_UTIL.createTable(tableName1, new byte[][] { FAMILY }, VERSION,
103 Bytes.toBytes("aaaaa"), Bytes.toBytes("zzzzz"), NUM_REGIONS);
104 Table htable2 =
105 TEST_UTIL.createTable(tableName2, new byte[][] { FAMILY }, VERSION, Bytes.toBytes("aaaaa"),
106 Bytes.toBytes("zzzzz"), NUM_REGIONS);
107 TEST_UTIL.waitUntilAllRegionsAssigned(tableName1);
108 TEST_UTIL.waitUntilAllRegionsAssigned(tableName2);
110 try (RegionLocator rl = TEST_UTIL.getConnection().getRegionLocator(tableName1)) {
111 byte[][] startRows = rl.getStartKeys();
112 byte[][] endRows = rl.getEndKeys();
114 // SinglePut case
115 for (int i = 0; i < NUM_REGIONS; i++) {
116 byte [] row = startRows[i];
117 if (row == null || row.length <= 0) continue;
118 Put put = new Put(row).addColumn(FAMILY, QUALIFIER, VALUE1);
119 success = multiplexer.put(tableName1, put);
120 assertTrue("multiplexer.put returns", success);
122 put = new Put(row).addColumn(FAMILY, QUALIFIER, VALUE1);
123 success = multiplexer.put(tableName2, put);
124 assertTrue("multiplexer.put failed", success);
126 LOG.info("Put for " + Bytes.toStringBinary(startRows[i]) + " @ iteration " + (i + 1));
128 // verify that the Get returns the correct result
129 checkExistence(htable1, startRows[i], FAMILY, QUALIFIER);
130 checkExistence(htable2, startRows[i], FAMILY, QUALIFIER);
133 // MultiPut case
134 List<Put> multiput = new ArrayList<>();
135 for (int i = 0; i < NUM_REGIONS; i++) {
136 byte [] row = endRows[i];
137 if (row == null || row.length <= 0) continue;
138 Put put = new Put(row);
139 put.addColumn(FAMILY, QUALIFIER, VALUE2);
140 multiput.add(put);
142 failedPuts = multiplexer.put(tableName1, multiput);
143 assertTrue(failedPuts == null);
145 // verify that the Get returns the correct result
146 for (int i = 0; i < NUM_REGIONS; i++) {
147 byte [] row = endRows[i];
148 if (row == null || row.length <= 0) continue;
149 Get get = new Get(row);
150 get.addColumn(FAMILY, QUALIFIER);
151 Result r;
152 int nbTry = 0;
153 do {
154 assertTrue(nbTry++ < 50);
155 Thread.sleep(100);
156 r = htable1.get(get);
157 } while (r == null || r.getValue(FAMILY, QUALIFIER) == null ||
158 Bytes.compareTo(VALUE2, r.getValue(FAMILY, QUALIFIER)) != 0);