1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase;
20
21 import java.security.InvalidParameterException;
22 import java.util.Map;
23 import java.util.Set;
24 import java.util.TreeMap;
25 import java.util.concurrent.atomic.AtomicLong;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.hbase.client.HTable;
31 import org.apache.hadoop.hbase.client.Result;
32 import org.apache.hadoop.hbase.client.ResultScanner;
33 import org.apache.hadoop.hbase.client.Scan;
34 import org.apache.hadoop.hbase.filter.CompareFilter;
35 import org.apache.hadoop.hbase.filter.Filter;
36 import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
37 import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
38 import org.apache.hadoop.hbase.util.Bytes;
39 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
40 import org.apache.hadoop.hbase.util.MultiThreadedWriter;
41 import org.apache.hadoop.hbase.util.RegionSplitter;
42 import org.apache.hadoop.hbase.util.test.LoadTestDataGenerator;
43 import org.apache.hadoop.hbase.util.test.LoadTestKVGenerator;
44 import org.junit.After;
45 import org.junit.Assert;
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.junit.experimental.categories.Category;
49
50
51
52
53
54
55 @Category(IntegrationTests.class)
56 public class IntegrationTestLazyCfLoading {
57 private static final TableName TABLE_NAME =
58 TableName.valueOf(IntegrationTestLazyCfLoading.class.getSimpleName());
59 private static final String TIMEOUT_KEY = "hbase.%s.timeout";
60 private static final String ENCODING_KEY = "hbase.%s.datablock.encoding";
61
62
63 private static final int DEFAULT_TIMEOUT_MINUTES = 10;
64
65 private static final int NUM_SERVERS = 1;
66
67 private static final int REGIONS_PER_SERVER = 3;
68 private static final int KEYS_TO_WRITE_PER_SERVER = 20000;
69 private static final int WRITER_THREADS = 10;
70 private static final int WAIT_BETWEEN_SCANS_MS = 1000;
71
72 private static final Log LOG = LogFactory.getLog(IntegrationTestLazyCfLoading.class);
73 private IntegrationTestingUtility util = new IntegrationTestingUtility();
74 private final DataGenerator dataGen = new DataGenerator();
75
76
77
78
79
80
81
82 private static class DataGenerator extends LoadTestDataGenerator {
83 private static final int MIN_DATA_SIZE = 4096;
84 private static final int MAX_DATA_SIZE = 65536;
85 public static final byte[] ESSENTIAL_CF = Bytes.toBytes("essential");
86 public static final byte[] JOINED_CF1 = Bytes.toBytes("joined");
87 public static final byte[] JOINED_CF2 = Bytes.toBytes("joined2");
88 public static final byte[] FILTER_COLUMN = Bytes.toBytes("filter");
89 public static final byte[] VALUE_COLUMN = Bytes.toBytes("val");
90 public static final long ACCEPTED_VALUE = 1L;
91
92 private static final Map<byte[], byte[][]> columnMap = new TreeMap<byte[], byte[][]>(
93 Bytes.BYTES_COMPARATOR);
94
95 private final AtomicLong expectedNumberOfKeys = new AtomicLong(0);
96 private final AtomicLong totalNumberOfKeys = new AtomicLong(0);
97
98 public DataGenerator() {
99 super(MIN_DATA_SIZE, MAX_DATA_SIZE);
100 columnMap.put(ESSENTIAL_CF, new byte[][] { FILTER_COLUMN });
101 columnMap.put(JOINED_CF1, new byte[][] { FILTER_COLUMN, VALUE_COLUMN });
102 columnMap.put(JOINED_CF2, new byte[][] { VALUE_COLUMN });
103 }
104
105 public long getExpectedNumberOfKeys() {
106 return expectedNumberOfKeys.get();
107 }
108
109 public long getTotalNumberOfKeys() {
110 return totalNumberOfKeys.get();
111 }
112
113 @Override
114 public byte[] getDeterministicUniqueKey(long keyBase) {
115 return LoadTestKVGenerator.md5PrefixedKey(keyBase).getBytes();
116 }
117
118 @Override
119 public byte[][] getColumnFamilies() {
120 return columnMap.keySet().toArray(new byte[columnMap.size()][]);
121 }
122
123 @Override
124 public byte[][] generateColumnsForCf(byte[] rowKey, byte[] cf) {
125 return columnMap.get(cf);
126 }
127
128 @Override
129 public byte[] generateValue(byte[] rowKey, byte[] cf, byte[] column) {
130 if (Bytes.BYTES_COMPARATOR.compare(column, FILTER_COLUMN) == 0) {
131
132 long value = Long.parseLong(Bytes.toString(rowKey, 0, 4), 16) & ACCEPTED_VALUE;
133 if (Bytes.BYTES_COMPARATOR.compare(cf, ESSENTIAL_CF) == 0) {
134 totalNumberOfKeys.incrementAndGet();
135 if (value == ACCEPTED_VALUE) {
136 expectedNumberOfKeys.incrementAndGet();
137 }
138 }
139 return Bytes.toBytes(value);
140 } else if (Bytes.BYTES_COMPARATOR.compare(column, VALUE_COLUMN) == 0) {
141 return kvGenerator.generateRandomSizeValue(rowKey, cf, column);
142 }
143 String error = "Unknown column " + Bytes.toString(column);
144 assert false : error;
145 throw new InvalidParameterException(error);
146 }
147
148 @Override
149 public boolean verify(byte[] rowKey, byte[] cf, byte[] column, byte[] value) {
150 if (Bytes.BYTES_COMPARATOR.compare(column, FILTER_COLUMN) == 0) {
151
152 return Bytes.toLong(value) == ACCEPTED_VALUE;
153 } else if (Bytes.BYTES_COMPARATOR.compare(column, VALUE_COLUMN) == 0) {
154 return LoadTestKVGenerator.verify(value, rowKey, cf, column);
155 }
156 return false;
157 }
158
159 @Override
160 public boolean verify(byte[] rowKey, byte[] cf, Set<byte[]> columnSet) {
161 return columnMap.get(cf).length == columnSet.size();
162 }
163
164 public Filter getScanFilter() {
165 SingleColumnValueFilter scf = new SingleColumnValueFilter(ESSENTIAL_CF, FILTER_COLUMN,
166 CompareFilter.CompareOp.EQUAL, Bytes.toBytes(ACCEPTED_VALUE));
167 scf.setFilterIfMissing(true);
168 return scf;
169 }
170 }
171
172 @Before
173 public void setUp() throws Exception {
174 LOG.info("Initializing cluster with " + NUM_SERVERS + " servers");
175 util.initializeCluster(NUM_SERVERS);
176 LOG.info("Done initializing cluster");
177 createTable();
178
179
180
181 Thread.sleep(3000);
182 }
183
184 private void createTable() throws Exception {
185 deleteTable();
186 LOG.info("Creating table");
187 Configuration conf = util.getConfiguration();
188 String encodingKey = String.format(ENCODING_KEY, this.getClass().getSimpleName());
189 DataBlockEncoding blockEncoding = DataBlockEncoding.valueOf(conf.get(encodingKey, "FAST_DIFF"));
190 HTableDescriptor htd = new HTableDescriptor(TABLE_NAME);
191 for (byte[] cf : dataGen.getColumnFamilies()) {
192 HColumnDescriptor hcd = new HColumnDescriptor(cf);
193 hcd.setDataBlockEncoding(blockEncoding);
194 htd.addFamily(hcd);
195 }
196 int serverCount = util.getHBaseClusterInterface().getClusterStatus().getServersSize();
197 byte[][] splits = new RegionSplitter.HexStringSplit().split(serverCount * REGIONS_PER_SERVER);
198 util.getHBaseAdmin().createTable(htd, splits);
199 LOG.info("Created table");
200 }
201
202 private void deleteTable() throws Exception {
203 if (util.getHBaseAdmin().tableExists(TABLE_NAME)) {
204 LOG.info("Deleting table");
205 util.deleteTable(TABLE_NAME);
206 LOG.info("Deleted table");
207 }
208 }
209
210 @After
211 public void tearDown() throws Exception {
212 deleteTable();
213 LOG.info("Restoring the cluster");
214 util.restoreCluster();
215 LOG.info("Done restoring the cluster");
216 }
217
218 @Test
219 public void testReadersAndWriters() throws Exception {
220 Configuration conf = util.getConfiguration();
221 String timeoutKey = String.format(TIMEOUT_KEY, this.getClass().getSimpleName());
222 long maxRuntime = conf.getLong(timeoutKey, DEFAULT_TIMEOUT_MINUTES);
223 long serverCount = util.getHBaseClusterInterface().getClusterStatus().getServersSize();
224 long keysToWrite = serverCount * KEYS_TO_WRITE_PER_SERVER;
225 HTable table = new HTable(conf, TABLE_NAME);
226
227
228
229 MultiThreadedWriter writer =
230 new MultiThreadedWriter(dataGen, conf, TABLE_NAME);
231 writer.setMultiPut(true);
232
233 LOG.info("Starting writer; the number of keys to write is " + keysToWrite);
234
235 writer.start(1, keysToWrite, WRITER_THREADS);
236
237
238 long now = EnvironmentEdgeManager.currentTimeMillis();
239 long timeLimit = now + (maxRuntime * 60000);
240 boolean isWriterDone = false;
241 while (now < timeLimit && !isWriterDone) {
242 LOG.info("Starting the scan; wrote approximately "
243 + dataGen.getTotalNumberOfKeys() + " keys");
244 isWriterDone = writer.isDone();
245 if (isWriterDone) {
246 LOG.info("Scanning full result, writer is done");
247 }
248 Scan scan = new Scan();
249 for (byte[] cf : dataGen.getColumnFamilies()) {
250 scan.addFamily(cf);
251 }
252 scan.setFilter(dataGen.getScanFilter());
253 scan.setLoadColumnFamiliesOnDemand(true);
254
255
256
257 long onesGennedBeforeScan = dataGen.getExpectedNumberOfKeys();
258 long startTs = EnvironmentEdgeManager.currentTimeMillis();
259 ResultScanner results = table.getScanner(scan);
260 long resultCount = 0;
261 Result result = null;
262
263 while ((result = results.next()) != null) {
264 boolean isOk = writer.verifyResultAgainstDataGenerator(result, true, true);
265 Assert.assertTrue("Failed to verify [" + Bytes.toString(result.getRow())+ "]", isOk);
266 ++resultCount;
267 }
268 long timeTaken = EnvironmentEdgeManager.currentTimeMillis() - startTs;
269
270 long onesGennedAfterScan = dataGen.getExpectedNumberOfKeys();
271 Assert.assertTrue("Read " + resultCount + " keys when at most " + onesGennedAfterScan
272 + " were generated ", onesGennedAfterScan >= resultCount);
273 if (isWriterDone) {
274 Assert.assertTrue("Read " + resultCount + " keys; the writer is done and "
275 + onesGennedAfterScan + " keys were generated", onesGennedAfterScan == resultCount);
276 } else if (onesGennedBeforeScan * 0.9 > resultCount) {
277 LOG.warn("Read way too few keys (" + resultCount + "/" + onesGennedBeforeScan
278 + ") - there might be a problem, or the writer might just be slow");
279 }
280 LOG.info("Scan took " + timeTaken + "ms");
281 if (!isWriterDone) {
282 Thread.sleep(WAIT_BETWEEN_SCANS_MS);
283 now = EnvironmentEdgeManager.currentTimeMillis();
284 }
285 }
286 Assert.assertEquals("There are write failures", 0, writer.getNumWriteFailures());
287 Assert.assertTrue("Writer is not done", isWriterDone);
288
289 }
290 }