View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.master;
20  
21  import static org.apache.hadoop.hbase.regionserver.HRegion.warmupHRegion;
22  import java.io.IOException;
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.HBaseTestingUtility;
27  import org.apache.hadoop.hbase.HRegionInfo;
28  import org.apache.hadoop.hbase.HTableDescriptor;
29  import org.apache.hadoop.hbase.MiniHBaseCluster;
30  import org.apache.hadoop.hbase.TableName;
31  import org.apache.hadoop.hbase.Waiter;
32  import org.apache.hadoop.hbase.client.Put;
33  import org.apache.hadoop.hbase.client.Table;
34  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos;
35  import org.apache.hadoop.hbase.regionserver.HRegion;
36  import org.apache.hadoop.hbase.regionserver.HRegionServer;
37  import org.apache.hadoop.hbase.testclassification.LargeTests;
38  import org.apache.hadoop.hbase.util.Bytes;
39  import org.junit.experimental.categories.Category;
40  import org.junit.BeforeClass;
41  import org.junit.AfterClass;
42  import org.junit.Before;
43  import org.junit.After;
44  import org.junit.Test;
45  
46  /**
47   * Run tests that use the HBase clients; {@link HTable}.
48   * Sets up the HBase mini cluster once at start and runs through all client tests.
49   * Each creates a table named for the method and does its stuff against that.
50   */
51  @Category(LargeTests.class)
52  @SuppressWarnings ("deprecation")
53  public class TestWarmupRegion {
54    final Log LOG = LogFactory.getLog(getClass());
55    protected TableName TABLENAME = TableName.valueOf("testPurgeFutureDeletes");
56    protected final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
57    private static byte [] ROW = Bytes.toBytes("testRow");
58    private static byte [] FAMILY = Bytes.toBytes("testFamily");
59    private static byte [] QUALIFIER = Bytes.toBytes("testQualifier");
60    private static byte [] VALUE = Bytes.toBytes("testValue");
61    private static byte[] COLUMN = Bytes.toBytes("column");
62    private static int numRows = 10000;
63    protected static int SLAVES = 3;
64    private static MiniHBaseCluster myCluster;
65    private static Table table;
66  
67    /**
68     * @throws java.lang.Exception
69     */
70    @BeforeClass
71    public static void setUpBeforeClass() throws Exception {
72      Configuration conf = TEST_UTIL.getConfiguration();
73      TEST_UTIL.startMiniCluster(SLAVES);
74    }
75  
76    /**
77     * @throws java.lang.Exception
78     */
79    @AfterClass
80    public static void tearDownAfterClass() throws Exception {
81      TEST_UTIL.shutdownMiniCluster();
82    }
83  
84    /**
85     * @throws java.lang.Exception
86     */
87    @Before
88    public void setUp() throws Exception {
89      table = TEST_UTIL.createTable(TABLENAME, FAMILY);
90  
91      // future timestamp
92      for (int i = 0; i < numRows; i++) {
93        long ts = System.currentTimeMillis() * 2;
94        Put put = new Put(ROW, ts);
95        put.add(FAMILY, COLUMN, VALUE);
96        table.put(put);
97      }
98  
99      // major compaction, purged future deletes
100     TEST_UTIL.getHBaseAdmin().flush(TABLENAME);
101     TEST_UTIL.getHBaseAdmin().majorCompact(TABLENAME);
102 
103     // waiting for the major compaction to complete
104     TEST_UTIL.waitFor(6000, new Waiter.Predicate<IOException>() {
105       @Override
106       public boolean evaluate() throws IOException {
107         return TEST_UTIL.getHBaseAdmin().getCompactionState(TABLENAME) ==
108             AdminProtos.GetRegionInfoResponse.CompactionState.NONE;
109       }
110     });
111 
112     table.close();
113   }
114 
115 
116   /**
117    * @throws java.lang.Exception
118    */
119   @After
120   public void tearDown() throws Exception {
121     // Nothing to do.
122   }
123 
124   protected void runwarmup()  throws InterruptedException{
125     Thread thread = new Thread(new Runnable() {
126       @Override
127       public void run() {
128         HRegionServer rs = TEST_UTIL.getMiniHBaseCluster().getRegionServer(0);
129         HRegion region = TEST_UTIL.getMiniHBaseCluster().getRegions(TABLENAME).get(0);
130         HRegionInfo info = region.getRegionInfo();
131 
132         try {
133           HTableDescriptor htd = table.getTableDescriptor();
134           for (int i = 0; i < 10; i++) {
135             warmupHRegion(info, htd, rs.getWAL(info), rs.getConfiguration(), rs, null);
136           }
137 
138         } catch (IOException ie) {
139           LOG.error("Failed warming up region " + info.getRegionNameAsString(), ie);
140         }
141       }
142     });
143     thread.start();
144     thread.join();
145   }
146 
147   /**
148    * Basic client side validation of HBASE-4536
149    */
150    @Test
151    public void testWarmup() throws Exception {
152      int serverid = 0;
153      HRegion region = TEST_UTIL.getMiniHBaseCluster().getRegions(TABLENAME).get(0);
154      HRegionInfo info = region.getRegionInfo();
155      runwarmup();
156      for (int i = 0; i < 10; i++) {
157        HRegionServer rs = TEST_UTIL.getMiniHBaseCluster().getRegionServer(serverid);
158        byte [] destName = Bytes.toBytes(rs.getServerName().toString());
159        TEST_UTIL.getMiniHBaseCluster().getMaster().move(info.getEncodedNameAsBytes(), destName);
160        serverid = (serverid + 1) % 2;
161      }
162    }
163 }