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