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.master;
019
020import static org.apache.hadoop.hbase.regionserver.HRegion.warmupHRegion;
021import static org.junit.Assert.assertTrue;
022
023import java.io.IOException;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseTestingUtility;
027import org.apache.hadoop.hbase.HTableDescriptor;
028import org.apache.hadoop.hbase.MiniHBaseCluster;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.Waiter;
031import org.apache.hadoop.hbase.client.CompactionState;
032import org.apache.hadoop.hbase.client.Put;
033import org.apache.hadoop.hbase.client.RegionInfo;
034import org.apache.hadoop.hbase.client.Table;
035import org.apache.hadoop.hbase.regionserver.HRegion;
036import org.apache.hadoop.hbase.regionserver.HRegionServer;
037import org.apache.hadoop.hbase.testclassification.LargeTests;
038import org.apache.hadoop.hbase.testclassification.MasterTests;
039import org.apache.hadoop.hbase.util.Bytes;
040import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
041import org.junit.After;
042import org.junit.AfterClass;
043import org.junit.Before;
044import org.junit.BeforeClass;
045import org.junit.ClassRule;
046import org.junit.Test;
047import org.junit.experimental.categories.Category;
048import org.slf4j.Logger;
049import org.slf4j.LoggerFactory;
050
051/**
052 * Run tests that use the HBase clients; {@link org.apache.hadoop.hbase.client.TableBuilder}. Sets
053 * up the HBase mini cluster once at start and runs through all client tests. Each creates a table
054 * named for the method and does its stuff against that.
055 */
056@Category({ MasterTests.class, LargeTests.class })
057@SuppressWarnings("deprecation")
058public class TestWarmupRegion {
059
060  @ClassRule
061  public static final HBaseClassTestRule CLASS_RULE =
062    HBaseClassTestRule.forClass(TestWarmupRegion.class);
063
064  private static final Logger LOG = LoggerFactory.getLogger(TestWarmupRegion.class);
065  protected TableName TABLENAME = TableName.valueOf("testPurgeFutureDeletes");
066  protected final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
067  private static byte[] ROW = Bytes.toBytes("testRow");
068  private static byte[] FAMILY = Bytes.toBytes("testFamily");
069  private static byte[] QUALIFIER = Bytes.toBytes("testQualifier");
070  private static byte[] VALUE = Bytes.toBytes("testValue");
071  private static byte[] COLUMN = Bytes.toBytes("column");
072  private static int numRows = 10000;
073  protected static int SLAVES = 3;
074  private static MiniHBaseCluster myCluster;
075  private static Table table;
076
077  /**
078   * @throws java.lang.Exception
079   */
080  @BeforeClass
081  public static void setUpBeforeClass() throws Exception {
082    Configuration conf = TEST_UTIL.getConfiguration();
083    TEST_UTIL.startMiniCluster(SLAVES);
084  }
085
086  /**
087   * @throws java.lang.Exception
088   */
089  @AfterClass
090  public static void tearDownAfterClass() throws Exception {
091    TEST_UTIL.shutdownMiniCluster();
092  }
093
094  /**
095   * @throws java.lang.Exception
096   */
097  @Before
098  public void setUp() throws Exception {
099    table = TEST_UTIL.createTable(TABLENAME, FAMILY);
100
101    // future timestamp
102    for (int i = 0; i < numRows; i++) {
103      long ts = EnvironmentEdgeManager.currentTime() * 2;
104      Put put = new Put(ROW, ts);
105      put.addColumn(FAMILY, COLUMN, VALUE);
106      table.put(put);
107    }
108
109    // major compaction, purged future deletes
110    TEST_UTIL.getAdmin().flush(TABLENAME);
111    TEST_UTIL.getAdmin().majorCompact(TABLENAME);
112
113    // waiting for the major compaction to complete
114    TEST_UTIL.waitFor(6000, new Waiter.Predicate<IOException>() {
115      @Override
116      public boolean evaluate() throws IOException {
117        return TEST_UTIL.getAdmin().getCompactionState(TABLENAME) == CompactionState.NONE;
118      }
119    });
120
121    table.close();
122  }
123
124  /**
125   * @throws java.lang.Exception
126   */
127  @After
128  public void tearDown() throws Exception {
129    // Nothing to do.
130  }
131
132  protected void runwarmup() throws InterruptedException {
133    Thread thread = new Thread(new Runnable() {
134      @Override
135      public void run() {
136        HRegionServer rs = TEST_UTIL.getMiniHBaseCluster().getRegionServer(0);
137        HRegion region = TEST_UTIL.getMiniHBaseCluster().getRegions(TABLENAME).get(0);
138        RegionInfo info = region.getRegionInfo();
139
140        try {
141          HTableDescriptor htd = table.getTableDescriptor();
142          for (int i = 0; i < 10; i++) {
143            warmupHRegion(info, htd, rs.getWAL(info), rs.getConfiguration(), rs, null);
144          }
145
146        } catch (IOException ie) {
147          LOG.error("Failed warming up region " + info.getRegionNameAsString(), ie);
148        }
149      }
150    });
151    thread.start();
152    thread.join();
153  }
154
155  /**
156   * Basic client side validation of HBASE-4536
157   */
158  @Test
159  public void testWarmup() throws Exception {
160    int serverid = 0;
161    HRegion region = TEST_UTIL.getMiniHBaseCluster().getRegions(TABLENAME).get(0);
162    RegionInfo info = region.getRegionInfo();
163    runwarmup();
164    for (int i = 0; i < 10; i++) {
165      HRegionServer rs = TEST_UTIL.getMiniHBaseCluster().getRegionServer(serverid);
166      byte[] destName = Bytes.toBytes(rs.getServerName().toString());
167      assertTrue(destName != null);
168      LOG.info("i=" + i);
169      TEST_UTIL.getMiniHBaseCluster().getMaster().move(info.getEncodedNameAsBytes(), destName);
170      serverid = (serverid + 1) % 2;
171    }
172  }
173}