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