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.regionserver;
19  
20  import java.io.IOException;
21  import java.util.Collection;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.hadoop.hbase.exceptions.DoNotRetryIOException;
26  import org.apache.hadoop.hbase.Cell;
27  import org.apache.hadoop.hbase.KeyValue;
28  import org.apache.hadoop.hbase.KeyValueUtil;
29  import org.apache.hadoop.hbase.client.Delete;
30  import org.apache.hadoop.hbase.client.Mutation;
31  import org.apache.hadoop.hbase.client.Put;
32  import org.apache.hadoop.hbase.protobuf.generated.MultiRowMutationProcessorProtos.MultiRowMutationProcessorRequest;
33  import org.apache.hadoop.hbase.protobuf.generated.MultiRowMutationProcessorProtos.MultiRowMutationProcessorResponse;
34  import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
35  import org.apache.hadoop.hbase.util.Bytes;
36  
37  /**
38   * A <code>MultiRowProcessor</code> that performs multiple puts and deletes.
39   */
40  class MultiRowMutationProcessor extends BaseRowProcessor<MultiRowMutationProcessorRequest,
41  MultiRowMutationProcessorResponse> {
42    Collection<byte[]> rowsToLock;
43    Collection<Mutation> mutations;
44  
45    MultiRowMutationProcessor(Collection<Mutation> mutations,
46                              Collection<byte[]> rowsToLock) {
47      this.rowsToLock = rowsToLock;
48      this.mutations = mutations;
49    }
50  
51    @Override
52    public Collection<byte[]> getRowsToLock() {
53      return rowsToLock;
54    }
55  
56    @Override
57    public boolean readOnly() {
58      return false;
59    }
60    
61    @Override
62    public MultiRowMutationProcessorResponse getResult() {
63      return MultiRowMutationProcessorResponse.getDefaultInstance();
64    }
65  
66    @Override
67    public void process(long now,
68                        HRegion region,
69                        List<KeyValue> mutationKvs,
70                        WALEdit walEdit) throws IOException {
71      byte[] byteNow = Bytes.toBytes(now);
72      // Check mutations and apply edits to a single WALEdit
73      for (Mutation m : mutations) {
74        if (m instanceof Put) {
75          Map<byte[], List<? extends Cell>> familyMap = m.getFamilyMap();
76          region.checkFamilies(familyMap.keySet());
77          region.checkTimestamps(familyMap, now);
78          region.updateKVTimestamps(familyMap.values(), byteNow);
79        } else if (m instanceof Delete) {
80          Delete d = (Delete) m;
81          region.prepareDelete(d);
82          region.prepareDeleteTimestamps(d.getFamilyMap(), byteNow);
83        } else {
84          throw new DoNotRetryIOException(
85              "Action must be Put or Delete. But was: "
86              + m.getClass().getName());
87        }
88        for (List<? extends Cell> cells: m.getFamilyMap().values()) {
89          boolean writeToWAL = m.getWriteToWAL();
90          for (Cell cell : cells) {
91            KeyValue kv = KeyValueUtil.ensureKeyValue(cell);
92            mutationKvs.add(kv);
93            if (writeToWAL) {
94              walEdit.add(kv);
95            }
96          }
97        }
98      }
99    }
100 
101   @Override
102   public void preProcess(HRegion region, WALEdit walEdit) throws IOException {
103     RegionCoprocessorHost coprocessorHost = region.getCoprocessorHost();
104     if (coprocessorHost != null) {
105       for (Mutation m : mutations) {
106         if (m instanceof Put) {
107           if (coprocessorHost.prePut((Put) m, walEdit, m.getWriteToWAL())) {
108             // by pass everything
109             return;
110           }
111         } else if (m instanceof Delete) {
112           Delete d = (Delete) m;
113           region.prepareDelete(d);
114           if (coprocessorHost.preDelete(d, walEdit, d.getWriteToWAL())) {
115             // by pass everything
116             return;
117           }
118         }
119       }
120     }
121   }
122 
123   @Override
124   public void postProcess(HRegion region, WALEdit walEdit) throws IOException {
125     RegionCoprocessorHost coprocessorHost = region.getCoprocessorHost();
126     if (coprocessorHost != null) {
127       for (Mutation m : mutations) {
128         if (m instanceof Put) {
129           coprocessorHost.postPut((Put) m, walEdit, m.getWriteToWAL());
130         } else if (m instanceof Delete) {
131           coprocessorHost.postDelete((Delete) m, walEdit, m.getWriteToWAL());
132         }
133       }
134     }
135   }
136 
137   @Override
138   public MultiRowMutationProcessorRequest getRequestData() {
139     return MultiRowMutationProcessorRequest.getDefaultInstance();
140   }
141 
142   @Override
143   public void initialize(MultiRowMutationProcessorRequest msg) {
144     //nothing
145   }
146 }