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;
019
020import java.io.IOException;
021import java.util.ArrayList;
022import java.util.Arrays;
023import java.util.List;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.client.Admin;
026import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
027import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
028import org.apache.hadoop.hbase.client.Connection;
029import org.apache.hadoop.hbase.client.ConnectionFactory;
030import org.apache.hadoop.hbase.testclassification.IntegrationTests;
031import org.apache.hadoop.hbase.util.Bytes;
032import org.apache.hadoop.hbase.util.HFileTestUtil;
033import org.apache.hadoop.hbase.util.LoadTestDataGeneratorWithMOB;
034import org.apache.hadoop.hbase.util.LoadTestTool;
035import org.apache.hadoop.util.ToolRunner;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038
039import org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine;
040
041/**
042 * Integration Test for MOB ingest.
043 */
044@Category(IntegrationTests.class)
045public class IntegrationTestIngestWithMOB extends IntegrationTestIngest {
046  private static final char COLON = ':';
047
048  private byte[] mobColumnFamily = HFileTestUtil.DEFAULT_COLUMN_FAMILY;
049  public static final String THRESHOLD = "threshold";
050  public static final String MIN_MOB_DATA_SIZE = "minMobDataSize";
051  public static final String MAX_MOB_DATA_SIZE = "maxMobDataSize";
052  private int threshold = 1024; // 1KB
053  private int minMobDataSize = 512; // 512B
054  private int maxMobDataSize = threshold * 5; // 5KB
055  private static final long JUNIT_RUN_TIME = 2 * 60 * 1000; // 2 minutes
056
057  // similar to LOAD_TEST_TOOL_INIT_ARGS except OPT_IN_MEMORY is removed
058  protected String[] LOAD_TEST_TOOL_MOB_INIT_ARGS = { LoadTestTool.OPT_COMPRESSION,
059    HFileTestUtil.OPT_DATA_BLOCK_ENCODING, LoadTestTool.OPT_ENCRYPTION,
060    LoadTestTool.OPT_NUM_REGIONS_PER_SERVER, LoadTestTool.OPT_REGION_REPLICATION, };
061
062  @Override
063  protected String[] getArgsForLoadTestToolInitTable() {
064    List<String> args = new ArrayList<>();
065    args.add("-tn");
066    args.add(getTablename().getNameAsString());
067    // pass all remaining args from conf with keys <test class name>.<load test tool arg>
068    String clazz = this.getClass().getSimpleName();
069    for (String arg : LOAD_TEST_TOOL_MOB_INIT_ARGS) {
070      String val = conf.get(String.format("%s.%s", clazz, arg));
071      if (val != null) {
072        args.add("-" + arg);
073        args.add(val);
074      }
075    }
076    args.add("-init_only");
077    return args.toArray(new String[args.size()]);
078  }
079
080  @Override
081  protected void addOptions() {
082    super.addOptions();
083    super.addOptWithArg(THRESHOLD, "The threshold to classify cells to mob data");
084    super.addOptWithArg(MIN_MOB_DATA_SIZE, "Minimum value size for mob data");
085    super.addOptWithArg(MAX_MOB_DATA_SIZE, "Maximum value size for mob data");
086  }
087
088  @Override
089  protected void processOptions(CommandLine cmd) {
090    super.processOptions(cmd);
091    if (cmd.hasOption(THRESHOLD)) {
092      threshold = Integer.parseInt(cmd.getOptionValue(THRESHOLD));
093    }
094    if (cmd.hasOption(MIN_MOB_DATA_SIZE)) {
095      minMobDataSize = Integer.parseInt(cmd.getOptionValue(MIN_MOB_DATA_SIZE));
096    }
097    if (cmd.hasOption(MAX_MOB_DATA_SIZE)) {
098      maxMobDataSize = Integer.parseInt(cmd.getOptionValue(MAX_MOB_DATA_SIZE));
099    }
100    if (minMobDataSize > maxMobDataSize) {
101      throw new IllegalArgumentException(
102        "The minMobDataSize should not be larger than minMobDataSize");
103    }
104  }
105
106  @Test
107  @Override
108  public void testIngest() throws Exception {
109    runIngestTest(JUNIT_RUN_TIME, 100, 10, 1024, 10, 20);
110  };
111
112  @Override
113  protected void initTable() throws IOException {
114    super.initTable();
115
116    TableName tableName = getTablename();
117    try (Connection connection = ConnectionFactory.createConnection();
118      Admin admin = connection.getAdmin()) {
119      HTableDescriptor tableDesc = admin.getTableDescriptor(tableName);
120      LOG.info("Disabling table " + getTablename());
121      admin.disableTable(tableName);
122      ColumnFamilyDescriptor mobColumn = tableDesc.getColumnFamily(mobColumnFamily);
123      ColumnFamilyDescriptor cfd = ColumnFamilyDescriptorBuilder.newBuilder(mobColumn)
124        .setMobEnabled(true).setMobThreshold((long) threshold).build();
125      admin.modifyColumnFamily(tableName, cfd);
126      LOG.info("Enabling table " + getTablename());
127      admin.enableTable(tableName);
128    }
129  }
130
131  @Override
132  protected String[] getArgsForLoadTestTool(String mode, String modeSpecificArg, long startKey,
133    long numKeys) {
134    String[] args = super.getArgsForLoadTestTool(mode, modeSpecificArg, startKey, numKeys);
135    List<String> tmp = new ArrayList<>(Arrays.asList(args));
136    // LoadTestDataGeneratorMOB:mobColumnFamily:minMobDataSize:maxMobDataSize
137    tmp.add(HIPHEN + LoadTestTool.OPT_GENERATOR);
138    StringBuilder sb = new StringBuilder(LoadTestDataGeneratorWithMOB.class.getName());
139    sb.append(COLON);
140    sb.append(Bytes.toString(mobColumnFamily));
141    sb.append(COLON);
142    sb.append(minMobDataSize);
143    sb.append(COLON);
144    sb.append(maxMobDataSize);
145    tmp.add(sb.toString());
146    return tmp.toArray(new String[tmp.size()]);
147  }
148
149  public static void main(String[] args) throws Exception {
150    Configuration conf = HBaseConfiguration.create();
151    IntegrationTestingUtility.setUseDistributedCluster(conf);
152    int ret = ToolRunner.run(conf, new IntegrationTestIngestWithMOB(), args);
153    System.exit(ret);
154  }
155}