View Javadoc

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   * This coprocessor 'shallows' all the writes. It allows to test a pure
36   * write workload, going through all the communication layers.
37   * The reads will work as well, but they as we never write, they will always always
38   * return an empty structure. The WAL is also skipped.
39   * Obviously, the region will never be split automatically. It's up to the user
40   * to split and move it.
41   * <p/>
42   * For a table created like this:
43   * create 'usertable', {NAME => 'f1', VERSIONS => 1}
44   * <p/>
45   * You can then add the coprocessor with this command:
46   * alter 'usertable', 'coprocessor' => '|org.apache.hadoop.hbase.tool.WriteSinkCoprocessor|'
47   * <p/>
48   * And then
49   * put 'usertable', 'f1', 'f1', 'f1'
50   * <p/>
51   * scan 'usertable'
52   * Will return:
53   * 0 row(s) in 0.0050 seconds
54   * <p/>
55   */
56  public class WriteSinkCoprocessor extends BaseRegionObserver {
57    private static final Log LOG = LogFactory.getLog(WriteSinkCoprocessor.class);
58    private final AtomicLong ops = new AtomicLong();
59    private String regionName;
60  
61    @Override
62    public void preOpen(ObserverContext<RegionCoprocessorEnvironment> e) throws IOException {
63      regionName = e.getEnvironment().getRegion().getRegionInfo().getRegionNameAsString();
64    }
65  
66  
67    @Override
68    public void preBatchMutate(final ObserverContext<RegionCoprocessorEnvironment> c,
69                               final MiniBatchOperationInProgress<Mutation> miniBatchOp)
70        throws IOException {
71      if (ops.incrementAndGet() % 20000 == 0) {
72        LOG.info("Wrote " + ops.get() + " times in region " + regionName);
73      }
74  
75      for (int i = 0; i < miniBatchOp.size(); i++) {
76        miniBatchOp.setOperationStatus(i,
77            new OperationStatus(HConstants.OperationStatusCode.SUCCESS));
78      }
79      c.bypass();
80    }
81  }