1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.client;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.hbase.HBaseTestingUtility;
28 import org.apache.hadoop.hbase.HRegionLocation;
29 import org.apache.hadoop.hbase.testclassification.LargeTests;
30 import org.apache.hadoop.hbase.MiniHBaseCluster;
31 import org.apache.hadoop.hbase.TableName;
32 import org.apache.hadoop.hbase.util.Bytes;
33 import org.junit.AfterClass;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.junit.experimental.categories.Category;
37
38 @Category(LargeTests.class)
39 public class TestHTableMultiplexerFlushCache {
40 final Log LOG = LogFactory.getLog(getClass());
41 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
42 private static byte[] FAMILY = Bytes.toBytes("testFamily");
43 private static byte[] QUALIFIER1 = Bytes.toBytes("testQualifier_1");
44 private static byte[] QUALIFIER2 = Bytes.toBytes("testQualifier_2");
45 private static byte[] VALUE1 = Bytes.toBytes("testValue1");
46 private static byte[] VALUE2 = Bytes.toBytes("testValue2");
47 private static int SLAVES = 3;
48 private static int PER_REGIONSERVER_QUEUE_SIZE = 100000;
49
50
51
52
53 @BeforeClass
54 public static void setUpBeforeClass() throws Exception {
55 TEST_UTIL.startMiniCluster(SLAVES);
56 }
57
58
59
60
61 @AfterClass
62 public static void tearDownAfterClass() throws Exception {
63 TEST_UTIL.shutdownMiniCluster();
64 }
65
66 private static void checkExistence(HTable htable, byte[] row, byte[] family, byte[] quality,
67 byte[] value) throws Exception {
68
69 Result r;
70 Get get = new Get(row);
71 get.addColumn(family, quality);
72 int nbTry = 0;
73 do {
74 assertTrue("Fail to get from " + htable.getName() + " after " + nbTry + " tries", nbTry < 50);
75 nbTry++;
76 Thread.sleep(100);
77 r = htable.get(get);
78 } while (r == null || r.getValue(family, quality) == null);
79 assertEquals("value", Bytes.toStringBinary(value),
80 Bytes.toStringBinary(r.getValue(family, quality)));
81 }
82
83 @Test
84 public void testOnRegionChange() throws Exception {
85 TableName TABLE = TableName.valueOf("testOnRegionChange");
86 final int NUM_REGIONS = 10;
87 HTable htable = TEST_UTIL.createTable(TABLE, new byte[][] { FAMILY }, 3,
88 Bytes.toBytes("aaaaa"), Bytes.toBytes("zzzzz"), NUM_REGIONS);
89
90 HTableMultiplexer multiplexer = new HTableMultiplexer(TEST_UTIL.getConfiguration(),
91 PER_REGIONSERVER_QUEUE_SIZE);
92
93 byte[][] startRows = htable.getStartKeys();
94 byte[] row = startRows[1];
95 assertTrue("2nd region should not start with empty row", row != null && row.length > 0);
96
97 Put put = new Put(row).add(FAMILY, QUALIFIER1, VALUE1);
98 assertTrue("multiplexer.put returns", multiplexer.put(TABLE, put));
99
100 checkExistence(htable, row, FAMILY, QUALIFIER1, VALUE1);
101
102
103 HRegionLocation loc = htable.getRegionLocation(row);
104 MiniHBaseCluster hbaseCluster = TEST_UTIL.getHBaseCluster();
105 hbaseCluster.stopRegionServer(loc.getServerName());
106 TEST_UTIL.waitUntilAllRegionsAssigned(TABLE);
107
108
109 put = new Put(row).add(FAMILY, QUALIFIER2, VALUE2);
110 assertTrue("multiplexer.put returns", multiplexer.put(TABLE, put));
111
112 checkExistence(htable, row, FAMILY, QUALIFIER2, VALUE2);
113 }
114 }