View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.regionserver;
19  
20  import java.io.IOException;
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.concurrent.atomic.AtomicLong;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.fs.FileSystem;
29  import org.apache.hadoop.fs.Path;
30  import org.apache.hadoop.hbase.HBaseConfiguration;
31  import org.apache.hadoop.hbase.HBaseTestingUtility;
32  import org.apache.hadoop.hbase.HColumnDescriptor;
33  import org.apache.hadoop.hbase.HTableDescriptor;
34  import org.apache.hadoop.hbase.KeyValue;
35  import org.apache.hadoop.hbase.LargeTests;
36  import org.apache.hadoop.hbase.MultithreadedTestUtil.RepeatingTestThread;
37  import org.apache.hadoop.hbase.MultithreadedTestUtil.TestContext;
38  import org.apache.hadoop.hbase.TableExistsException;
39  import org.apache.hadoop.hbase.TableName;
40  import org.apache.hadoop.hbase.client.HConnection;
41  import org.apache.hadoop.hbase.client.HTable;
42  import org.apache.hadoop.hbase.client.RegionServerCallable;
43  import org.apache.hadoop.hbase.client.Result;
44  import org.apache.hadoop.hbase.client.ResultScanner;
45  import org.apache.hadoop.hbase.client.RpcRetryingCaller;
46  import org.apache.hadoop.hbase.client.RpcRetryingCallerFactory;
47  import org.apache.hadoop.hbase.client.Scan;
48  import org.apache.hadoop.hbase.io.compress.Compression;
49  import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
50  import org.apache.hadoop.hbase.io.hfile.CacheConfig;
51  import org.apache.hadoop.hbase.io.hfile.HFile;
52  import org.apache.hadoop.hbase.io.hfile.HFileContext;
53  import org.apache.hadoop.hbase.io.hfile.HFileContextBuilder;
54  import org.apache.hadoop.hbase.protobuf.RequestConverter;
55  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos;
56  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.CompactRegionRequest;
57  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.BulkLoadHFileRequest;
58  import org.apache.hadoop.hbase.util.Bytes;
59  import org.apache.hadoop.hbase.util.Pair;
60  import org.junit.Test;
61  import org.junit.experimental.categories.Category;
62  
63  import com.google.common.collect.Lists;
64  
65  /**
66   * Tests bulk loading of HFiles and shows the atomicity or lack of atomicity of
67   * the region server's bullkLoad functionality.
68   */
69  @Category(LargeTests.class)
70  public class TestHRegionServerBulkLoad {
71    final static Log LOG = LogFactory.getLog(TestHRegionServerBulkLoad.class);
72    private static HBaseTestingUtility UTIL = new HBaseTestingUtility();
73    private final static Configuration conf = UTIL.getConfiguration();
74    private final static byte[] QUAL = Bytes.toBytes("qual");
75    private final static int NUM_CFS = 10;
76    public static int BLOCKSIZE = 64 * 1024;
77    public static Algorithm COMPRESSION = Compression.Algorithm.NONE;
78  
79    private final static byte[][] families = new byte[NUM_CFS][];
80    static {
81      for (int i = 0; i < NUM_CFS; i++) {
82        families[i] = Bytes.toBytes(family(i));
83      }
84    }
85  
86    static byte[] rowkey(int i) {
87      return Bytes.toBytes(String.format("row_%08d", i));
88    }
89  
90    static String family(int i) {
91      return String.format("family_%04d", i);
92    }
93  
94    /**
95     * Create an HFile with the given number of rows with a specified value.
96     */
97    public static void createHFile(FileSystem fs, Path path, byte[] family,
98        byte[] qualifier, byte[] value, int numRows) throws IOException {
99      HFileContext context = new HFileContextBuilder().withBlockSize(BLOCKSIZE)
100                             .withCompression(COMPRESSION)
101                             .build();
102     HFile.Writer writer = HFile
103         .getWriterFactory(conf, new CacheConfig(conf))
104         .withPath(fs, path)
105         .withFileContext(context)
106         .create();
107     long now = System.currentTimeMillis();
108     try {
109       // subtract 2 since iterateOnSplits doesn't include boundary keys
110       for (int i = 0; i < numRows; i++) {
111         KeyValue kv = new KeyValue(rowkey(i), family, qualifier, now, value);
112         writer.append(kv);
113       }
114     } finally {
115       writer.close();
116     }
117   }
118 
119   /**
120    * Thread that does full scans of the table looking for any partially
121    * completed rows.
122    *
123    * Each iteration of this loads 10 hdfs files, which occupies 5 file open file
124    * handles. So every 10 iterations (500 file handles) it does a region
125    * compaction to reduce the number of open file handles.
126    */
127   public static class AtomicHFileLoader extends RepeatingTestThread {
128     final AtomicLong numBulkLoads = new AtomicLong();
129     final AtomicLong numCompactions = new AtomicLong();
130     private String tableName;
131 
132     public AtomicHFileLoader(String tableName, TestContext ctx,
133         byte targetFamilies[][]) throws IOException {
134       super(ctx);
135       this.tableName = tableName;
136     }
137 
138     public void doAnAction() throws Exception {
139       long iteration = numBulkLoads.getAndIncrement();
140       Path dir =  UTIL.getDataTestDirOnTestFS(String.format("bulkLoad_%08d",
141           iteration));
142 
143       // create HFiles for different column families
144       FileSystem fs = UTIL.getTestFileSystem();
145       byte[] val = Bytes.toBytes(String.format("%010d", iteration));
146       final List<Pair<byte[], String>> famPaths = new ArrayList<Pair<byte[], String>>(
147           NUM_CFS);
148       for (int i = 0; i < NUM_CFS; i++) {
149         Path hfile = new Path(dir, family(i));
150         byte[] fam = Bytes.toBytes(family(i));
151         createHFile(fs, hfile, fam, QUAL, val, 1000);
152         famPaths.add(new Pair<byte[], String>(fam, hfile.toString()));
153       }
154 
155       // bulk load HFiles
156       final HConnection conn = UTIL.getHBaseAdmin().getConnection();
157       TableName tbl = TableName.valueOf(tableName);
158       RegionServerCallable<Void> callable =
159           new RegionServerCallable<Void>(conn, tbl, Bytes.toBytes("aaa")) {
160         @Override
161         public Void call() throws Exception {
162           LOG.debug("Going to connect to server " + getLocation() + " for row "
163               + Bytes.toStringBinary(getRow()));
164           byte[] regionName = getLocation().getRegionInfo().getRegionName();
165           BulkLoadHFileRequest request =
166             RequestConverter.buildBulkLoadHFileRequest(famPaths, regionName, true);
167           getStub().bulkLoadHFile(null, request);
168           return null;
169         }
170       };
171       RpcRetryingCallerFactory factory = new RpcRetryingCallerFactory(conf);
172       RpcRetryingCaller<Void> caller = factory.<Void> newCaller();
173       caller.callWithRetries(callable);
174 
175       // Periodically do compaction to reduce the number of open file handles.
176       if (numBulkLoads.get() % 10 == 0) {
177         // 10 * 50 = 500 open file handles!
178         callable = new RegionServerCallable<Void>(conn, tbl, Bytes.toBytes("aaa")) {
179           @Override
180           public Void call() throws Exception {
181             LOG.debug("compacting " + getLocation() + " for row "
182                 + Bytes.toStringBinary(getRow()));
183             AdminProtos.AdminService.BlockingInterface server =
184               conn.getAdmin(getLocation().getServerName());
185             CompactRegionRequest request =
186               RequestConverter.buildCompactRegionRequest(
187                 getLocation().getRegionInfo().getRegionName(), true, null);
188             server.compactRegion(null, request);
189             numCompactions.incrementAndGet();
190             return null;
191           }
192         };
193         caller.callWithRetries(callable);
194       }
195     }
196   }
197 
198   /**
199    * Thread that does full scans of the table looking for any partially
200    * completed rows.
201    */
202   public static class AtomicScanReader extends RepeatingTestThread {
203     byte targetFamilies[][];
204     HTable table;
205     AtomicLong numScans = new AtomicLong();
206     AtomicLong numRowsScanned = new AtomicLong();
207     String TABLE_NAME;
208 
209     public AtomicScanReader(String TABLE_NAME, TestContext ctx,
210         byte targetFamilies[][]) throws IOException {
211       super(ctx);
212       this.TABLE_NAME = TABLE_NAME;
213       this.targetFamilies = targetFamilies;
214       table = new HTable(conf, TABLE_NAME);
215     }
216 
217     public void doAnAction() throws Exception {
218       Scan s = new Scan();
219       for (byte[] family : targetFamilies) {
220         s.addFamily(family);
221       }
222       ResultScanner scanner = table.getScanner(s);
223 
224       for (Result res : scanner) {
225         byte[] lastRow = null, lastFam = null, lastQual = null;
226         byte[] gotValue = null;
227         for (byte[] family : targetFamilies) {
228           byte qualifier[] = QUAL;
229           byte thisValue[] = res.getValue(family, qualifier);
230           if (gotValue != null && thisValue != null
231               && !Bytes.equals(gotValue, thisValue)) {
232 
233             StringBuilder msg = new StringBuilder();
234             msg.append("Failed on scan ").append(numScans)
235                 .append(" after scanning ").append(numRowsScanned)
236                 .append(" rows!\n");
237             msg.append("Current  was " + Bytes.toString(res.getRow()) + "/"
238                 + Bytes.toString(family) + ":" + Bytes.toString(qualifier)
239                 + " = " + Bytes.toString(thisValue) + "\n");
240             msg.append("Previous  was " + Bytes.toString(lastRow) + "/"
241                 + Bytes.toString(lastFam) + ":" + Bytes.toString(lastQual)
242                 + " = " + Bytes.toString(gotValue));
243             throw new RuntimeException(msg.toString());
244           }
245 
246           lastFam = family;
247           lastQual = qualifier;
248           lastRow = res.getRow();
249           gotValue = thisValue;
250         }
251         numRowsScanned.getAndIncrement();
252       }
253       numScans.getAndIncrement();
254     }
255   }
256 
257   /**
258    * Creates a table with given table name and specified number of column
259    * families if the table does not already exist.
260    */
261   private void setupTable(String table, int cfs) throws IOException {
262     try {
263       LOG.info("Creating table " + table);
264       HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(table));
265       for (int i = 0; i < 10; i++) {
266         htd.addFamily(new HColumnDescriptor(family(i)));
267       }
268 
269       UTIL.getHBaseAdmin().createTable(htd);
270     } catch (TableExistsException tee) {
271       LOG.info("Table " + table + " already exists");
272     }
273   }
274 
275   /**
276    * Atomic bulk load.
277    */
278   @Test
279   public void testAtomicBulkLoad() throws Exception {
280     String TABLE_NAME = "atomicBulkLoad";
281 
282     int millisToRun = 30000;
283     int numScanners = 50;
284 
285     UTIL.startMiniCluster(1);
286     try {
287       runAtomicBulkloadTest(TABLE_NAME, millisToRun, numScanners);
288     } finally {
289       UTIL.shutdownMiniCluster();
290     }
291   }
292 
293   void runAtomicBulkloadTest(String tableName, int millisToRun, int numScanners)
294       throws Exception {
295     setupTable(tableName, 10);
296 
297     TestContext ctx = new TestContext(UTIL.getConfiguration());
298 
299     AtomicHFileLoader loader = new AtomicHFileLoader(tableName, ctx, null);
300     ctx.addThread(loader);
301 
302     List<AtomicScanReader> scanners = Lists.newArrayList();
303     for (int i = 0; i < numScanners; i++) {
304       AtomicScanReader scanner = new AtomicScanReader(tableName, ctx, families);
305       scanners.add(scanner);
306       ctx.addThread(scanner);
307     }
308 
309     ctx.startThreads();
310     ctx.waitFor(millisToRun);
311     ctx.stop();
312 
313     LOG.info("Loaders:");
314     LOG.info("  loaded " + loader.numBulkLoads.get());
315     LOG.info("  compations " + loader.numCompactions.get());
316 
317     LOG.info("Scanners:");
318     for (AtomicScanReader scanner : scanners) {
319       LOG.info("  scanned " + scanner.numScans.get());
320       LOG.info("  verified " + scanner.numRowsScanned.get() + " rows");
321     }
322   }
323 
324   /**
325    * Run test on an HBase instance for 5 minutes. This assumes that the table
326    * under test only has a single region.
327    */
328   public static void main(String args[]) throws Exception {
329     try {
330       Configuration c = HBaseConfiguration.create();
331       TestHRegionServerBulkLoad test = new TestHRegionServerBulkLoad();
332       test.setConf(c);
333       test.runAtomicBulkloadTest("atomicTableTest", 5 * 60 * 1000, 50);
334     } finally {
335       System.exit(0); // something hangs (believe it is lru threadpool)
336     }
337   }
338 
339   private void setConf(Configuration c) {
340     UTIL = new HBaseTestingUtility(c);
341   }
342 
343 }
344