Review Board 1.7.22


multi-row local transactions.

Review Request #3748 - Created Feb. 3, 2012 and submitted

Lars Hofhansl
trunk
HBASE-5229
Reviewers
hbase
hbase
This builds on HBASE-3584, HBASE-5203, and HBASE-5304.

Multiple Rows can be locked and applied atomically as long as the application ensures that all rows reside in the same Region (by presplitting or a custom RegionSplitPolicy).
At SFDC we can use this to colocate subsets of a tenant's data and allow atomic operations over these subsets.

Obviously this is an advanced features and this prominently called out in the Javadoc.
Tests added to TestFromClientSide and TestAtomicOperation
http://svn.apache.org/repos/asf/hbase/trunk/src/main/java/org/apache/hadoop/hbase/coprocessor/MultiRowMutationEndpoint.java
New File

    
   
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
}
http://svn.apache.org/repos/asf/hbase/trunk/src/main/java/org/apache/hadoop/hbase/coprocessor/MultiRowMutationProtocol.java
New File
 
http://svn.apache.org/repos/asf/hbase/trunk/src/main/java/org/apache/hadoop/hbase/regionserver/HRegion.java
Revision 1241536 New Change
 
http://svn.apache.org/repos/asf/hbase/trunk/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java
Revision 1241536 New Change
 
http://svn.apache.org/repos/asf/hbase/trunk/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide.java
Revision 1241536 New Change
 
http://svn.apache.org/repos/asf/hbase/trunk/src/test/java/org/apache/hadoop/hbase/regionserver/TestAtomicOperation.java
Revision 1241536 New Change
 
  1. http://svn.apache.org/repos/asf/hbase/trunk/src/main/java/org/apache/hadoop/hbase/coprocessor/MultiRowMutationEndpoint.java: Loading...
  2. http://svn.apache.org/repos/asf/hbase/trunk/src/main/java/org/apache/hadoop/hbase/coprocessor/MultiRowMutationProtocol.java: Loading...
  3. http://svn.apache.org/repos/asf/hbase/trunk/src/main/java/org/apache/hadoop/hbase/regionserver/HRegion.java: Loading...
  4. http://svn.apache.org/repos/asf/hbase/trunk/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java: Loading...
  5. http://svn.apache.org/repos/asf/hbase/trunk/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide.java: Loading...
  6. http://svn.apache.org/repos/asf/hbase/trunk/src/test/java/org/apache/hadoop/hbase/regionserver/TestAtomicOperation.java: Loading...