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