1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19 package org.apache.hadoop.hbase.tool;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.hadoop.hbase.HConstants;
24 import org.apache.hadoop.hbase.client.Mutation;
25 import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver;
26 import org.apache.hadoop.hbase.coprocessor.ObserverContext;
27 import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
28 import org.apache.hadoop.hbase.regionserver.MiniBatchOperationInProgress;
29 import org.apache.hadoop.hbase.regionserver.OperationStatus;
30
31 import java.io.IOException;
32 import java.util.concurrent.atomic.AtomicLong;
33
34 /**
35 * <p>
36 * This coprocessor 'shallows' all the writes. It allows to test a pure
37 * write workload, going through all the communication layers.
38 * The reads will work as well, but they as we never write, they will always always
39 * return an empty structure. The WAL is also skipped.
40 * Obviously, the region will never be split automatically. It's up to the user
41 * to split and move it.
42 * </p>
43 * <p>
44 * For a table created like this:
45 * create 'usertable', {NAME => 'f1', VERSIONS => 1}
46 * </p>
47 * <p>
48 * You can then add the coprocessor with this command:
49 * alter 'usertable', 'coprocessor' => '|org.apache.hadoop.hbase.tool.WriteSinkCoprocessor|'
50 * </p>
51 * <p>
52 * And then
53 * put 'usertable', 'f1', 'f1', 'f1'
54 * </p>
55 * <p>
56 * scan 'usertable'
57 * Will return:
58 * 0 row(s) in 0.0050 seconds
59 * </p>
60 */
61 public class WriteSinkCoprocessor extends BaseRegionObserver {
62 private static final Log LOG = LogFactory.getLog(WriteSinkCoprocessor.class);
63 private final AtomicLong ops = new AtomicLong();
64 private String regionName;
65
66 @Override
67 public void preOpen(ObserverContext<RegionCoprocessorEnvironment> e) throws IOException {
68 regionName = e.getEnvironment().getRegion().getRegionInfo().getRegionNameAsString();
69 }
70
71
72 @Override
73 public void preBatchMutate(final ObserverContext<RegionCoprocessorEnvironment> c,
74 final MiniBatchOperationInProgress<Mutation> miniBatchOp)
75 throws IOException {
76 if (ops.incrementAndGet() % 20000 == 0) {
77 LOG.info("Wrote " + ops.get() + " times in region " + regionName);
78 }
79
80 for (int i = 0; i < miniBatchOp.size(); i++) {
81 miniBatchOp.setOperationStatus(i,
82 new OperationStatus(HConstants.OperationStatusCode.SUCCESS));
83 }
84 c.bypass();
85 }
86 }