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.client;
19  
20  import java.io.IOException;
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.hadoop.hbase.CellScannable;
27  import org.apache.hadoop.hbase.CellUtil;
28  import org.apache.hadoop.hbase.DoNotRetryIOException;
29  import org.apache.hadoop.hbase.HConstants;
30  import org.apache.hadoop.hbase.HRegionInfo;
31  import org.apache.hadoop.hbase.HRegionLocation;
32  import org.apache.hadoop.hbase.ServerName;
33  import org.apache.hadoop.hbase.TableName;
34  import org.apache.hadoop.hbase.ipc.RpcControllerFactory;
35  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
36  import org.apache.hadoop.hbase.protobuf.RequestConverter;
37  import org.apache.hadoop.hbase.protobuf.ResponseConverter;
38  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
39  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MultiRequest;
40  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MutationProto;
41  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.RegionAction;
42  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
43  
44  import com.google.common.annotations.VisibleForTesting;
45  import com.google.protobuf.ServiceException;
46  
47  /**
48   * Callable that handles the <code>multi</code> method call going against a single
49   * regionserver; i.e. A {@link RegionServerCallable} for the multi call (It is not a
50   * {@link RegionServerCallable} that goes against multiple regions.
51   * @param <R>
52   */
53  class MultiServerCallable<R> extends PayloadCarryingServerCallable<MultiResponse> {
54    private final MultiAction<R> multiAction;
55    private final boolean cellBlock;
56    private final RetryingTimeTracker tracker;
57    private final int rpcTimeout;
58  
59    MultiServerCallable(final ClusterConnection connection, final TableName tableName,
60        final ServerName location, RpcControllerFactory rpcFactory, final MultiAction<R> multi,
61        int rpcTimeout, RetryingTimeTracker tracker, int priority) {
62      super(connection, tableName, null, rpcFactory, priority);
63      this.multiAction = multi;
64      // RegionServerCallable has HRegionLocation field, but this is a multi-region request.
65      // Using region info from parent HRegionLocation would be a mistake for this class; so
66      // we will store the server here, and throw if someone tries to obtain location/regioninfo.
67      this.location = new HRegionLocation(null, location);
68      this.cellBlock = isCellBlock();
69      this.tracker = tracker;
70      this.rpcTimeout = rpcTimeout;
71    }
72  
73    @Override
74    protected HRegionLocation getLocation() {
75      throw new RuntimeException("Cannot get region location for multi-region request");
76    }
77  
78    @Override
79    public HRegionInfo getHRegionInfo() {
80      throw new RuntimeException("Cannot get region info for multi-region request");
81    }
82  
83    MultiAction<R> getMulti() {
84      return this.multiAction;
85    }
86  
87    @Override
88    public MultiResponse call(int operationTimeout) throws IOException {
89      int remainingTime = tracker.getRemainingTime(operationTimeout);
90      if (remainingTime <= 1) {
91        // "1" is a special return value in RetryingTimeTracker, see its implementation.
92        throw new DoNotRetryIOException("Operation Timeout");
93      }
94      int callTimeout = Math.min(rpcTimeout, remainingTime);
95      int countOfActions = this.multiAction.size();
96      if (countOfActions <= 0) throw new DoNotRetryIOException("No Actions");
97      MultiRequest.Builder multiRequestBuilder = MultiRequest.newBuilder();
98      RegionAction.Builder regionActionBuilder = RegionAction.newBuilder();
99      ClientProtos.Action.Builder actionBuilder = ClientProtos.Action.newBuilder();
100     MutationProto.Builder mutationBuilder = MutationProto.newBuilder();
101 
102     // Pre-size. Presume at least a KV per Action. There are likely more.
103     List<CellScannable> cells =
104         (this.cellBlock ? new ArrayList<CellScannable>(countOfActions) : null);
105 
106     long nonceGroup = multiAction.getNonceGroup();
107     if (nonceGroup != HConstants.NO_NONCE) {
108       multiRequestBuilder.setNonceGroup(nonceGroup);
109     }
110     // Index to track RegionAction within the MultiRequest
111     int regionActionIndex = -1;
112     // Map from a created RegionAction for a RowMutations to the original index within
113     // its original list of actions
114     Map<Integer, Integer> rowMutationsIndexMap = new HashMap<>();
115     // The multi object is a list of Actions by region.  Iterate by region.
116     for (Map.Entry<byte[], List<Action<R>>> e: this.multiAction.actions.entrySet()) {
117       final byte [] regionName = e.getKey();
118       final List<Action<R>> actions = e.getValue();
119       regionActionBuilder.clear();
120       regionActionBuilder.setRegion(RequestConverter.buildRegionSpecifier(
121         HBaseProtos.RegionSpecifier.RegionSpecifierType.REGION_NAME, regionName) );
122 
123       int rowMutations = 0;
124       for (Action<R> action : actions) {
125         Row row = action.getAction();
126         // Row Mutations are a set of Puts and/or Deletes all to be applied atomically
127         // on the one row. We do separate RegionAction for each RowMutations.
128         // We maintain a map to keep track of this RegionAction and the original Action index.
129         if (row instanceof RowMutations) {
130           RowMutations rms = (RowMutations)row;
131           if (this.cellBlock) {
132             // Build a multi request absent its Cell payload. Send data in cellblocks.
133             regionActionBuilder = RequestConverter.buildNoDataRegionAction(regionName, rms, cells,
134               regionActionBuilder, actionBuilder, mutationBuilder);
135           } else {
136             regionActionBuilder = RequestConverter.buildRegionAction(regionName, rms);
137           }
138           regionActionBuilder.setAtomic(true);
139           multiRequestBuilder.addRegionAction(regionActionBuilder.build());
140           regionActionIndex++;
141           rowMutationsIndexMap.put(regionActionIndex, action.getOriginalIndex());
142           rowMutations++;
143 
144           regionActionBuilder.clear();
145           regionActionBuilder.setRegion(RequestConverter.buildRegionSpecifier(
146             HBaseProtos.RegionSpecifier.RegionSpecifierType.REGION_NAME, regionName) );
147         }
148       }
149 
150       if (actions.size() > rowMutations) {
151         if (this.cellBlock) {
152           // Send data in cellblocks. The call to buildNoDataRegionAction will skip RowMutations.
153           // They have already been handled above. Guess at count of cells
154           regionActionBuilder = RequestConverter.buildNoDataRegionAction(regionName, actions, cells,
155             regionActionBuilder, actionBuilder, mutationBuilder);
156         } else {
157           regionActionBuilder = RequestConverter.buildRegionAction(regionName, actions,
158             regionActionBuilder, actionBuilder, mutationBuilder);
159         }
160         multiRequestBuilder.addRegionAction(regionActionBuilder.build());
161         regionActionIndex++;
162       }
163     }
164 
165     // Controller optionally carries cell data over the proxy/service boundary and also
166     // optionally ferries cell response data back out again.
167     controller.reset();
168     if (cells != null) controller.setCellScanner(CellUtil.createCellScanner(cells));
169     controller.setPriority(getTableName());
170     controller.setPriority(getPriority());
171     controller.setCallTimeout(callTimeout);
172     ClientProtos.MultiResponse responseProto;
173     ClientProtos.MultiRequest requestProto = multiRequestBuilder.build();
174     try {
175       responseProto = getStub().multi(controller, requestProto);
176     } catch (ServiceException e) {
177       throw ProtobufUtil.getRemoteException(e);
178     }
179     if (responseProto == null) return null; // Occurs on cancel
180     return ResponseConverter.getResults(requestProto, rowMutationsIndexMap,
181       responseProto, controller.cellScanner());
182   }
183 
184   /**
185    * @return True if we should send data in cellblocks.  This is an expensive call.  Cache the
186    * result if you can rather than call each time.
187    */
188   private boolean isCellBlock() {
189     // This is not exact -- the configuration could have changed on us after connection was set up
190     // but it will do for now.
191     HConnection connection = getConnection();
192     if (!(connection instanceof ClusterConnection)) return true; // Default is to do cellblocks.
193     return ((ClusterConnection) connection).hasCellBlockSupport();
194   }
195 
196   @Override
197   public void prepare(boolean reload) throws IOException {
198     // Use the location we were given in the constructor rather than go look it up.
199     setStub(getConnection().getClient(this.location.getServerName()));
200   }
201 
202   @VisibleForTesting
203   ServerName getServerName() {
204     return location.getServerName();
205   }
206 }