HBASE-26412 Handle sink failure in RegionReplicationSink (#3815)
[hbase.git] / hbase-server / src / main / java / org / apache / hadoop / hbase / regionserver / MiniBatchOperationInProgress.java
blobac6b5dc382e329b4b8be8e9a4c83747247937023
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.hadoop.hbase.regionserver;
20 import org.apache.hadoop.hbase.HBaseInterfaceAudience;
21 import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
22 import org.apache.yetus.audience.InterfaceAudience;
23 import org.apache.hadoop.hbase.client.Mutation;
24 import org.apache.hadoop.hbase.wal.WALEdit;
26 /**
27 * Wraps together the mutations which are applied as a batch to the region and their operation
28 * status and WALEdits.
29 * @see org.apache.hadoop.hbase.coprocessor.RegionObserver#preBatchMutate(
30 * org.apache.hadoop.hbase.coprocessor.ObserverContext, MiniBatchOperationInProgress)
31 * @see org.apache.hadoop.hbase.coprocessor.RegionObserver#postBatchMutate(
32 * org.apache.hadoop.hbase.coprocessor.ObserverContext, MiniBatchOperationInProgress)
33 * @param T Pair<Mutation, Integer> pair of Mutations and associated rowlock ids .
35 @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.COPROC)
36 public class MiniBatchOperationInProgress<T> {
37 private final T[] operations;
38 private Mutation[][] operationsFromCoprocessors;
39 private final OperationStatus[] retCodeDetails;
40 private final WALEdit[] walEditsFromCoprocessors;
41 private final int firstIndex;
42 private final int lastIndexExclusive;
44 private int readyToWriteCount = 0;
45 private int cellCount = 0;
46 private int numOfPuts = 0;
47 private int numOfDeletes = 0;
48 private int numOfIncrements = 0;
49 private int numOfAppends = 0;
52 public MiniBatchOperationInProgress(T[] operations, OperationStatus[] retCodeDetails,
53 WALEdit[] walEditsFromCoprocessors, int firstIndex, int lastIndexExclusive,
54 int readyToWriteCount) {
55 Preconditions.checkArgument(readyToWriteCount <= (lastIndexExclusive - firstIndex));
56 this.operations = operations;
57 this.retCodeDetails = retCodeDetails;
58 this.walEditsFromCoprocessors = walEditsFromCoprocessors;
59 this.firstIndex = firstIndex;
60 this.lastIndexExclusive = lastIndexExclusive;
61 this.readyToWriteCount = readyToWriteCount;
64 /**
65 * @return The number of operations(Mutations) involved in this batch.
67 public int size() {
68 return this.lastIndexExclusive - this.firstIndex;
71 /**
72 * @param index
73 * @return The operation(Mutation) at the specified position.
75 public T getOperation(int index) {
76 return operations[getAbsoluteIndex(index)];
79 /**
80 * Sets the status code for the operation(Mutation) at the specified position.
81 * By setting this status, {@link org.apache.hadoop.hbase.coprocessor.RegionObserver}
82 * can make HRegion to skip Mutations.
83 * @param index
84 * @param opStatus
86 public void setOperationStatus(int index, OperationStatus opStatus) {
87 this.retCodeDetails[getAbsoluteIndex(index)] = opStatus;
90 /**
91 * @param index
92 * @return Gets the status code for the operation(Mutation) at the specified position.
94 public OperationStatus getOperationStatus(int index) {
95 return this.retCodeDetails[getAbsoluteIndex(index)];
98 /**
99 * Sets the walEdit for the operation(Mutation) at the specified position.
100 * @param index
101 * @param walEdit
103 public void setWalEdit(int index, WALEdit walEdit) {
104 this.walEditsFromCoprocessors[getAbsoluteIndex(index)] = walEdit;
108 * @param index
109 * @return Gets the walEdit for the operation(Mutation) at the specified position.
111 public WALEdit getWalEdit(int index) {
112 return this.walEditsFromCoprocessors[getAbsoluteIndex(index)];
115 private int getAbsoluteIndex(int index) {
116 if (index < 0 || this.firstIndex + index >= this.lastIndexExclusive) {
117 throw new ArrayIndexOutOfBoundsException(index);
119 return this.firstIndex + index;
123 * Add more Mutations corresponding to the Mutation at the given index to be committed atomically
124 * in the same batch. These mutations are applied to the WAL and applied to the memstore as well.
125 * The timestamp of the cells in the given Mutations MUST be obtained from the original mutation.
126 * <b>Note:</b> The durability from CP will be replaced by the durability of corresponding mutation.
127 * <b>Note:</b> Currently only supports Put and Delete operations.
128 * @param index the index that corresponds to the original mutation index in the batch
129 * @param newOperations the Mutations to add
131 public void addOperationsFromCP(int index, Mutation[] newOperations) {
132 if (this.operationsFromCoprocessors == null) {
133 // lazy allocation to save on object allocation in case this is not used
134 this.operationsFromCoprocessors = new Mutation[operations.length][];
136 this.operationsFromCoprocessors[getAbsoluteIndex(index)] = newOperations;
139 public Mutation[] getOperationsFromCoprocessors(int index) {
140 return operationsFromCoprocessors == null ? null :
141 operationsFromCoprocessors[getAbsoluteIndex(index)];
144 public int getReadyToWriteCount() {
145 return readyToWriteCount;
148 public int getLastIndexExclusive() {
149 return lastIndexExclusive;
152 public int getCellCount() {
153 return cellCount;
156 public void addCellCount(int cellCount) {
157 this.cellCount += cellCount;
160 public int getNumOfPuts() {
161 return numOfPuts;
164 public void incrementNumOfPuts() {
165 this.numOfPuts += 1;
168 public int getNumOfDeletes() {
169 return numOfDeletes;
172 public void incrementNumOfDeletes() {
173 this.numOfDeletes += 1;
176 public int getNumOfIncrements() {
177 return numOfIncrements;
180 public void incrementNumOfIncrements() {
181 this.numOfIncrements += 1;
184 public int getNumOfAppends() {
185 return numOfAppends;
188 public void incrementNumOfAppends() {
189 this.numOfAppends += 1;