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