View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to you under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14   * License for the specific language governing permissions and limitations
15   * under the License.
16   */
17  package org.apache.hadoop.hbase.util.test;
18  
19  import java.io.IOException;
20  import java.util.Set;
21  
22  import org.apache.hadoop.hbase.classification.InterfaceAudience;
23  import org.apache.hadoop.hbase.client.Get;
24  import org.apache.hadoop.hbase.client.Mutation;
25  import org.apache.hadoop.hbase.util.LoadTestKVGenerator;
26  
27  /**
28   * A generator of random data (keys/cfs/columns/values) for load testing.
29   * Contains LoadTestKVGenerator as a matter of convenience...
30   */
31  @InterfaceAudience.Private
32  public abstract class LoadTestDataGenerator {
33    protected LoadTestKVGenerator kvGenerator;
34  
35    // The mutate info column stores information
36    // about update done to this column family this row.
37    public final static byte[] MUTATE_INFO = "mutate_info".getBytes();
38  
39    // The increment column always has a long value,
40    // which can be incremented later on during updates.
41    public final static byte[] INCREMENT = "increment".getBytes();
42  
43    protected String[] args;
44  
45    public LoadTestDataGenerator() {
46  
47    }
48  
49    /**
50     * Initializes the object.
51     * @param minValueSize minimum size of the value generated by
52     * {@link #generateValue(byte[], byte[], byte[])}.
53     * @param maxValueSize maximum size of the value generated by
54     * {@link #generateValue(byte[], byte[], byte[])}.
55     */
56    public LoadTestDataGenerator(int minValueSize, int maxValueSize) {
57      this.kvGenerator = new LoadTestKVGenerator(minValueSize, maxValueSize);
58    }
59  
60    /**
61     * initialize the LoadTestDataGenerator
62     *
63     * @param args
64     *          init args
65     */
66    public void initialize(String[] args) {
67      this.args = args;
68    }
69  
70    /**
71     * Generates a deterministic, unique hashed row key from a number. That way, the user can
72     * keep track of numbers, without messing with byte array and ensuring key distribution.
73     * @param keyBase Base number for a key, such as a loop counter.
74     */
75    public abstract byte[] getDeterministicUniqueKey(long keyBase);
76  
77    /**
78     * Gets column families for the load test table.
79     * @return The array of byte[]s representing column family names.
80     */
81    public abstract byte[][] getColumnFamilies();
82  
83    /**
84     * Generates an applicable set of columns to be used for a particular key and family.
85     * @param rowKey The row key to generate for.
86     * @param cf The column family name to generate for.
87     * @return The array of byte[]s representing column names.
88     */
89    public abstract byte[][] generateColumnsForCf(byte[] rowKey, byte[] cf);
90  
91    /**
92     * Generates a value to be used for a particular row/cf/column.
93     * @param rowKey The row key to generate for.
94     * @param cf The column family name to generate for.
95     * @param column The column name to generate for.
96     * @return The value to use.
97     */
98    public abstract byte[] generateValue(byte[] rowKey, byte[] cf, byte[] column);
99  
100   /**
101    * Checks that columns for a rowKey and cf are valid if generated via
102    * {@link #generateColumnsForCf(byte[], byte[])}
103    * @param rowKey The row key to verify for.
104    * @param cf The column family name to verify for.
105    * @param columnSet The column set (for example, encountered by read).
106    * @return True iff valid.
107    */
108   public abstract boolean verify(byte[] rowKey, byte[] cf, Set<byte[]> columnSet);
109 
110   /**
111    * Checks that value for a rowKey/cf/column is valid if generated via
112    * {@link #generateValue(byte[], byte[], byte[])}
113    * @param rowKey The row key to verify for.
114    * @param cf The column family name to verify for.
115    * @param column The column name to verify for.
116    * @param value The value (for example, encountered by read).
117    * @return True iff valid.
118    */
119   public abstract boolean verify(byte[] rowKey, byte[] cf, byte[] column, byte[] value);
120 
121   /**
122    * Giving a chance for the LoadTestDataGenerator to change the Mutation load.
123    * @param rowkeyBase
124    * @param m
125    * @return updated Mutation
126    * @throws IOException
127    */
128   public Mutation beforeMutate(long rowkeyBase, Mutation m) throws IOException {
129     return m;
130   }
131 
132   /**
133    * Giving a chance for the LoadTestDataGenerator to change the Get load.
134    * @param rowkeyBase
135    * @param get
136    * @return updated Get
137    * @throws IOException
138    */
139   public Get beforeGet(long rowkeyBase, Get get) throws IOException {
140     return get;
141   }
142 
143   /**
144    * @return the arguments passed to the generator as a list of objects.
145    */
146   public String[] getArgs() {
147     return this.args;
148   }
149 }