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;
18
19 import java.util.Random;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.hadoop.hbase.classification.InterfaceAudience;
24
25 /**
26 * A generator of random keys and values for load testing. Keys are generated
27 * by converting numeric indexes to strings and prefixing them with an MD5
28 * hash. Values are generated by selecting value size in the configured range
29 * and generating a pseudo-random sequence of bytes seeded by key, column
30 * qualifier, and value size.
31 */
32 @InterfaceAudience.Private
33 public class LoadTestKVGenerator {
34
35 private static final Log LOG = LogFactory.getLog(LoadTestKVGenerator.class);
36 private static int logLimit = 10;
37
38 /** A random number generator for determining value size */
39 private Random randomForValueSize = new Random();
40
41 private final int minValueSize;
42 private final int maxValueSize;
43
44 public LoadTestKVGenerator(int minValueSize, int maxValueSize) {
45 if (minValueSize <= 0 || maxValueSize <= 0) {
46 throw new IllegalArgumentException("Invalid min/max value sizes: " +
47 minValueSize + ", " + maxValueSize);
48 }
49 this.minValueSize = minValueSize;
50 this.maxValueSize = maxValueSize;
51 }
52
53 /**
54 * Verifies that the given byte array is the same as what would be generated
55 * for the given seed strings (row/cf/column/...). We are assuming that the
56 * value size is correct, and only verify the actual bytes. However, if the
57 * min/max value sizes are set sufficiently high, an accidental match should be
58 * extremely improbable.
59 */
60 public static boolean verify(byte[] value, byte[]... seedStrings) {
61 byte[] expectedData = getValueForRowColumn(value.length, seedStrings);
62 boolean equals = Bytes.equals(expectedData, value);
63 if (!equals && LOG.isDebugEnabled() && logLimit > 0) {
64 LOG.debug("verify failed, expected value: " + Bytes.toStringBinary(expectedData)
65 + " actual value: "+ Bytes.toStringBinary(value));
66 logLimit--; // this is not thread safe, but at worst we will have more logging
67 }
68 return equals;
69 }
70
71 /**
72 * Converts the given key to string, and prefixes it with the MD5 hash of
73 * the index's string representation.
74 */
75 public static String md5PrefixedKey(long key) {
76 String stringKey = Long.toString(key);
77 String md5hash = MD5Hash.getMD5AsHex(Bytes.toBytes(stringKey));
78
79 // flip the key to randomize
80 return md5hash + "-" + stringKey;
81 }
82
83 /**
84 * Generates a value for the given key index and column qualifier. Size is
85 * selected randomly in the configured range. The generated value depends
86 * only on the combination of the strings passed (key/cf/column/...) and the selected
87 * value size. This allows to verify the actual value bytes when reading, as done
88 * in {#verify(byte[], byte[]...)}
89 * This method is as thread-safe as Random class. It appears that the worst bug ever
90 * found with the latter is that multiple threads will get some duplicate values, which
91 * we don't care about.
92 */
93 public byte[] generateRandomSizeValue(byte[]... seedStrings) {
94 int dataSize = minValueSize;
95 if(minValueSize != maxValueSize) {
96 dataSize = minValueSize + randomForValueSize.nextInt(Math.abs(maxValueSize - minValueSize));
97 }
98 return getValueForRowColumn(dataSize, seedStrings);
99 }
100
101 /**
102 * Generates random bytes of the given size for the given row and column
103 * qualifier. The random seed is fully determined by these parameters.
104 */
105 private static byte[] getValueForRowColumn(int dataSize, byte[]... seedStrings) {
106 long seed = dataSize;
107 for (byte[] str : seedStrings) {
108 final String bytesString = Bytes.toString(str);
109 if (bytesString != null) {
110 seed += bytesString.hashCode();
111 }
112 }
113 Random seededRandom = new Random(seed);
114 byte[] randomBytes = new byte[dataSize];
115 seededRandom.nextBytes(randomBytes);
116 return randomBytes;
117 }
118
119 }