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