View Javadoc

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