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
26 /**
27 * A generator of random data (keys/cfs/columns/values) for load testing.
28 * Contains LoadTestKVGenerator as a matter of convenience...
29 */
30 @InterfaceAudience.Private
31 public abstract class LoadTestDataGenerator {
32 protected LoadTestKVGenerator kvGenerator;
33
34 // The mutate info column stores information
35 // about update done to this column family this row.
36 public final static byte[] MUTATE_INFO = "mutate_info".getBytes();
37
38 // The increment column always has a long value,
39 // which can be incremented later on during updates.
40 public final static byte[] INCREMENT = "increment".getBytes();
41
42 protected String[] args;
43
44 public LoadTestDataGenerator() {
45
46 }
47
48 /**
49 * Initializes the object.
50 * @param minValueSize minimum size of the value generated by
51 * {@link #generateValue(byte[], byte[], byte[])}.
52 * @param maxValueSize maximum size of the value generated by
53 * {@link #generateValue(byte[], byte[], byte[])}.
54 */
55 public LoadTestDataGenerator(int minValueSize, int maxValueSize) {
56 this.kvGenerator = new LoadTestKVGenerator(minValueSize, maxValueSize);
57 }
58
59 /**
60 * initialize the LoadTestDataGenerator
61 *
62 * @param args
63 * init args
64 */
65 public void initialize(String[] args) {
66 this.args = args;
67 }
68
69 /**
70 * Generates a deterministic, unique hashed row key from a number. That way, the user can
71 * keep track of numbers, without messing with byte array and ensuring key distribution.
72 * @param keyBase Base number for a key, such as a loop counter.
73 */
74 public abstract byte[] getDeterministicUniqueKey(long keyBase);
75
76 /**
77 * Gets column families for the load test table.
78 * @return The array of byte[]s representing column family names.
79 */
80 public abstract byte[][] getColumnFamilies();
81
82 /**
83 * Generates an applicable set of columns to be used for a particular key and family.
84 * @param rowKey The row key to generate for.
85 * @param cf The column family name to generate for.
86 * @return The array of byte[]s representing column names.
87 */
88 public abstract byte[][] generateColumnsForCf(byte[] rowKey, byte[] cf);
89
90 /**
91 * Generates a value to be used for a particular row/cf/column.
92 * @param rowKey The row key to generate for.
93 * @param cf The column family name to generate for.
94 * @param column The column name to generate for.
95 * @return The value to use.
96 */
97 public abstract byte[] generateValue(byte[] rowKey, byte[] cf, byte[] column);
98
99 /**
100 * Checks that columns for a rowKey and cf are valid if generated via
101 * {@link #generateColumnsForCf(byte[], byte[])}
102 * @param rowKey The row key to verify for.
103 * @param cf The column family name to verify for.
104 * @param columnSet The column set (for example, encountered by read).
105 * @return True iff valid.
106 */
107 public abstract boolean verify(byte[] rowKey, byte[] cf, Set<byte[]> columnSet);
108
109 /**
110 * Checks that value for a rowKey/cf/column is valid if generated via
111 * {@link #generateValue(byte[], byte[], byte[])}
112 * @param rowKey The row key to verify for.
113 * @param cf The column family name to verify for.
114 * @param column The column name to verify for.
115 * @param value The value (for example, encountered by read).
116 * @return True iff valid.
117 */
118 public abstract boolean verify(byte[] rowKey, byte[] cf, byte[] column, byte[] value);
119
120 /**
121 * Giving a chance for the LoadTestDataGenerator to change the Mutation load.
122 * @param rowkeyBase
123 * @param m
124 * @return updated Mutation
125 * @throws IOException
126 */
127 public Mutation beforeMutate(long rowkeyBase, Mutation m) throws IOException {
128 return m;
129 }
130
131 /**
132 * Giving a chance for the LoadTestDataGenerator to change the Get load.
133 * @param rowkeyBase
134 * @param get
135 * @return updated Get
136 * @throws IOException
137 */
138 public Get beforeGet(long rowkeyBase, Get get) throws IOException {
139 return get;
140 }
141
142 /**
143 * Return the arguments passed to the generator as list of object
144 * @return
145 */
146 public String[] getArgs() {
147 return this.args;
148 }
149 }