View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase;
19  
20  import java.io.IOException;
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.List;
24  
25  import org.apache.commons.cli.CommandLine;
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.hbase.client.HBaseAdmin;
28  import org.apache.hadoop.hbase.util.Bytes;
29  import org.apache.hadoop.hbase.util.LoadTestDataGeneratorWithMOB;
30  import org.apache.hadoop.hbase.util.LoadTestTool;
31  import org.apache.hadoop.util.ToolRunner;
32  import org.junit.Test;
33  import org.junit.experimental.categories.Category;
34  import org.apache.hadoop.hbase.testclassification.IntegrationTests;
35  
36  /**
37   * Integration Test for MOB ingest.
38   */
39  @Category(IntegrationTests.class)
40  public class IntegrationTestIngestWithMOB extends IntegrationTestIngest {
41    private static final char COLON = ':';
42  
43    private byte[] mobColumnFamily = LoadTestTool.DEFAULT_COLUMN_FAMILY;
44    public static final String THRESHOLD = "threshold";
45    public static final String MIN_MOB_DATA_SIZE = "minMobDataSize";
46    public static final String MAX_MOB_DATA_SIZE = "maxMobDataSize";
47    private int threshold = 1024; // 1KB
48    private int minMobDataSize = 512; // 512B
49    private int maxMobDataSize = threshold * 5; // 5KB
50    private static final long JUNIT_RUN_TIME = 2 * 60 * 1000; // 2 minutes
51  
52    //similar to LOAD_TEST_TOOL_INIT_ARGS except OPT_IN_MEMORY is removed
53    protected String[] LOAD_TEST_TOOL_MOB_INIT_ARGS = {
54        LoadTestTool.OPT_COMPRESSION,
55        LoadTestTool.OPT_DATA_BLOCK_ENCODING,
56        LoadTestTool.OPT_ENCRYPTION,
57        LoadTestTool.OPT_NUM_REGIONS_PER_SERVER,
58        LoadTestTool.OPT_REGION_REPLICATION,
59    };
60  
61    @Override
62    protected String[] getArgsForLoadTestToolInitTable() {
63      List<String> args = new ArrayList<String>();
64      args.add("-tn");
65      args.add(getTablename().getNameAsString());
66      // pass all remaining args from conf with keys <test class name>.<load test tool arg>
67      String clazz = this.getClass().getSimpleName();
68      for (String arg : LOAD_TEST_TOOL_MOB_INIT_ARGS) {
69        String val = conf.get(String.format("%s.%s", clazz, arg));
70        if (val != null) {
71          args.add("-" + arg);
72          args.add(val);
73        }
74      }
75      args.add("-init_only");
76      return args.toArray(new String[args.size()]);
77    }
78  
79    @Override
80    protected void addOptions() {
81      super.addOptions();
82      super.addOptWithArg(THRESHOLD, "The threshold to classify cells to mob data");
83      super.addOptWithArg(MIN_MOB_DATA_SIZE, "Minimum value size for mob data");
84      super.addOptWithArg(MAX_MOB_DATA_SIZE, "Maximum value size for mob data");
85    }
86  
87    @Override
88    protected void processOptions(CommandLine cmd) {
89      super.processOptions(cmd);
90      if (cmd.hasOption(THRESHOLD)) {
91        threshold = Integer.parseInt(cmd.getOptionValue(THRESHOLD));
92      }
93      if (cmd.hasOption(MIN_MOB_DATA_SIZE)) {
94        minMobDataSize = Integer.parseInt(cmd.getOptionValue(MIN_MOB_DATA_SIZE));
95      }
96      if (cmd.hasOption(MAX_MOB_DATA_SIZE)) {
97        maxMobDataSize = Integer.parseInt(cmd.getOptionValue(MAX_MOB_DATA_SIZE));
98      }
99      if (minMobDataSize > maxMobDataSize) {
100       throw new IllegalArgumentException(
101           "The minMobDataSize should not be larger than minMobDataSize");
102     }
103   }
104 
105   @Test
106   public void testIngest() throws Exception {
107     runIngestTest(JUNIT_RUN_TIME, 100, 10, 1024, 10, 20);
108   };
109 
110   @Override
111   protected void initTable() throws IOException {
112     super.initTable();
113 
114     byte[] tableName = getTablename().getName();
115     HBaseAdmin admin = new HBaseAdmin(conf);
116     HTableDescriptor tableDesc = admin.getTableDescriptor(tableName);
117     LOG.info("Disabling table " + getTablename());
118     admin.disableTable(tableName);
119     for (HColumnDescriptor columnDescriptor : tableDesc.getFamilies()) {
120       if(Arrays.equals(columnDescriptor.getName(), mobColumnFamily)) {
121         columnDescriptor.setMobEnabled(true);
122         columnDescriptor.setMobThreshold((long) threshold);
123         admin.modifyColumn(tableName, columnDescriptor);
124       }
125     }
126     LOG.info("Enabling table " + getTablename());
127     admin.enableTable(tableName);
128     admin.close();
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<String>(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 }