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.coprocessor;
19  
20  import java.io.IOException;
21  import java.util.List;
22  import java.util.SortedSet;
23  import java.util.TreeSet;
24  
25  import org.apache.hadoop.hbase.DoNotRetryIOException;
26  import org.apache.hadoop.hbase.HRegionInfo;
27  import org.apache.hadoop.hbase.client.Mutation;
28  import org.apache.hadoop.hbase.regionserver.HRegion;
29  import org.apache.hadoop.hbase.regionserver.WrongRegionException;
30  import org.apache.hadoop.hbase.util.Bytes;
31  
32  /**
33   * This class demonstrates how to implement atomic multi row transactions using
34   * {@link HRegion#mutateRowsWithLocks(java.util.Collection, java.util.Collection)}
35   * and Coprocessor endpoints.
36   */
37  public class MultiRowMutationEndpoint extends BaseEndpointCoprocessor implements
38      MultiRowMutationProtocol {
39  
40    @Override
41    public void mutateRows(List<Mutation> mutations) throws IOException {
42      // get the coprocessor environment
43      RegionCoprocessorEnvironment env = (RegionCoprocessorEnvironment) getEnvironment();
44  
45      // set of rows to lock, sorted to avoid deadlocks
46      SortedSet<byte[]> rowsToLock = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);
47  
48      HRegionInfo regionInfo = env.getRegion().getRegionInfo();
49      for (Mutation m : mutations) {
50        // check whether rows are in range for this region
51        if (!HRegion.rowIsInRange(regionInfo, m.getRow())) {
52          String msg = "Requested row out of range '"
53              + Bytes.toStringBinary(m.getRow()) + "'";
54          if (rowsToLock.isEmpty()) {
55            // if this is the first row, region might have moved,
56            // allow client to retry
57            throw new WrongRegionException(msg);
58          } else {
59            // rows are split between regions, do not retry
60            throw new DoNotRetryIOException(msg);
61          }
62        }
63        rowsToLock.add(m.getRow());
64      }
65      // call utility method on region
66      env.getRegion().mutateRowsWithLocks(mutations, rowsToLock);
67    }
68  }