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.coprocessor.example;
019
020import java.io.IOException;
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.fs.Path;
023import org.apache.hadoop.hbase.HBaseTestingUtil;
024import org.apache.hadoop.hbase.HConstants;
025import org.apache.hadoop.hbase.SingleProcessHBaseCluster;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.client.Table;
028import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
029import org.apache.hadoop.hbase.master.MasterFileSystem;
030import org.apache.hadoop.hbase.regionserver.Region;
031import org.apache.hadoop.hbase.util.Bytes;
032import org.apache.hadoop.hbase.util.CommonFSUtils;
033import org.apache.hadoop.hbase.util.HFileTestUtil;
034import org.junit.After;
035import org.slf4j.Logger;
036import org.slf4j.LoggerFactory;
037
038public class TestRefreshHFilesBase {
039  protected static final Logger LOG = LoggerFactory.getLogger(TestRefreshHFilesBase.class);
040  protected static final HBaseTestingUtil HTU = new HBaseTestingUtil();
041  protected static final int NUM_RS = 2;
042  protected static final TableName TABLE_NAME = TableName.valueOf("testRefreshRegionHFilesEP");
043  protected static final byte[] FAMILY = Bytes.toBytes("family");
044  protected static final byte[] QUALIFIER = Bytes.toBytes("qualifier");
045  protected static final byte[][] SPLIT_KEY = new byte[][] { Bytes.toBytes("30") };
046  protected static final int NUM_ROWS = 5;
047  protected static final String HFILE_NAME = "123abcdef";
048
049  protected static Configuration CONF = HTU.getConfiguration();
050  protected static SingleProcessHBaseCluster cluster;
051  protected static Table table;
052
053  public static void setUp(String regionImpl) {
054    try {
055      CONF.set(HConstants.REGION_IMPL, regionImpl);
056      CONF.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2);
057
058      CONF.setStrings(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
059        RefreshHFilesEndpoint.class.getName());
060      cluster = HTU.startMiniCluster(NUM_RS);
061
062      // Create table
063      table = HTU.createTable(TABLE_NAME, FAMILY, SPLIT_KEY);
064
065      // this will create 2 regions spread across slaves
066      HTU.loadNumericRows(table, FAMILY, 1, 20);
067      HTU.flush(TABLE_NAME);
068    } catch (Exception ex) {
069      LOG.error("Couldn't finish setup", ex);
070    }
071  }
072
073  @After
074  public void tearDown() throws Exception {
075    HTU.shutdownMiniCluster();
076  }
077
078  protected void addHFilesToRegions() throws IOException {
079    MasterFileSystem mfs = HTU.getMiniHBaseCluster().getMaster().getMasterFileSystem();
080    Path tableDir = CommonFSUtils.getTableDir(mfs.getRootDir(), TABLE_NAME);
081    for (Region region : cluster.getRegions(TABLE_NAME)) {
082      Path regionDir = new Path(tableDir, region.getRegionInfo().getEncodedName());
083      Path familyDir = new Path(regionDir, Bytes.toString(FAMILY));
084      HFileTestUtil.createHFile(HTU.getConfiguration(), HTU.getTestFileSystem(),
085        new Path(familyDir, HFILE_NAME), FAMILY, QUALIFIER, Bytes.toBytes("50"),
086        Bytes.toBytes("60"), NUM_ROWS);
087    }
088  }
089}