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