View Javadoc

1   /**
2    * Copyright The Apache Software Foundation
3    *
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
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
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.
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     * @throws java.lang.Exception
52     */
53    @BeforeClass
54    public static void setUpBeforeClass() throws Exception {
55      TEST_UTIL.startMiniCluster(SLAVES);
56    }
57  
58    /**
59     * @throws java.lang.Exception
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      // verify that the Get returns the correct result
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     // Now let's shutdown the regionserver and let regions moved to other servers.
103     HRegionLocation loc = htable.getRegionLocation(row);
104     MiniHBaseCluster hbaseCluster = TEST_UTIL.getHBaseCluster(); 
105     hbaseCluster.stopRegionServer(loc.getServerName());
106     TEST_UTIL.waitUntilAllRegionsAssigned(TABLE);
107 
108     // put with multiplexer.
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 }