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.classification.InterfaceAudience;
34  import org.apache.hadoop.hbase.ipc.HBaseRpcController;
35  import org.apache.hadoop.hbase.ipc.RpcControllerFactory;
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  
46  /**
47   * Callable that handles the <code>multi</code> method call going against a single
48   * regionserver; i.e. A RegionServerCallable for the multi call (It is NOT a
49   * RegionServerCallable that goes against multiple regions).
50   * @param <R>
51   */
52  @InterfaceAudience.Private
53  class MultiServerCallable<R> extends CancellableRegionServerCallable<MultiResponse> {
54    private final MultiAction<R> multiAction;
55    private final boolean cellBlock;
56
57    MultiServerCallable(final ClusterConnection connection, final TableName tableName,
58        final ServerName location, RpcControllerFactory rpcFactory, final MultiAction<R> multi) {
59      super(connection, tableName, null, rpcFactory);
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    }
67
68    @Override
69    protected HRegionLocation getLocation() {
70      throw new RuntimeException("Cannot get region location for multi-region request");
71    }
72
73    @Override
74    public HRegionInfo getHRegionInfo() {
75      throw new RuntimeException("Cannot get region info for multi-region request");
76    }
77
78    MultiAction<R> getMulti() {
79      return this.multiAction;
80    }
81
82    @Override
83    protected MultiResponse rpcCall() throws Exception {
84      int countOfActions = this.multiAction.size();
85      if (countOfActions <= 0) throw new DoNotRetryIOException("No Actions");
86      MultiRequest.Builder multiRequestBuilder = MultiRequest.newBuilder();
87      RegionAction.Builder regionActionBuilder = RegionAction.newBuilder();
88      ClientProtos.Action.Builder actionBuilder = ClientProtos.Action.newBuilder();
89      MutationProto.Builder mutationBuilder = MutationProto.newBuilder();
90      List<CellScannable> cells = null;
91      // The multi object is a list of Actions by region.  Iterate by region.
92      long nonceGroup = multiAction.getNonceGroup();
93      if (nonceGroup != HConstants.NO_NONCE) {
94        multiRequestBuilder.setNonceGroup(nonceGroup);
95      }
96      for (Map.Entry<byte[], List<Action<R>>> e: this.multiAction.actions.entrySet()) {
97        final byte [] regionName = e.getKey();
98        final List<Action<R>> actions = e.getValue();
99        regionActionBuilder.clear();
100       regionActionBuilder.setRegion(RequestConverter.buildRegionSpecifier(
101           HBaseProtos.RegionSpecifier.RegionSpecifierType.REGION_NAME, regionName));
102       if (this.cellBlock) {
103         // Pre-size. Presume at least a KV per Action.  There are likely more.
104         if (cells == null) cells = new ArrayList<CellScannable>(countOfActions);
105         // Send data in cellblocks. The call to buildNoDataMultiRequest will skip RowMutations.
106         // They have already been handled above. Guess at count of cells
107         regionActionBuilder = RequestConverter.buildNoDataRegionAction(regionName, actions, cells,
108           regionActionBuilder, actionBuilder, mutationBuilder);
109       } else {
110         regionActionBuilder = RequestConverter.buildRegionAction(regionName, actions,
111           regionActionBuilder, actionBuilder, mutationBuilder);
112       }
113       multiRequestBuilder.addRegionAction(regionActionBuilder.build());
114     }
115
116     if (cells != null) {
117       setRpcControllerCellScanner(CellUtil.createCellScanner(cells));
118     }
119     ClientProtos.MultiRequest requestProto = multiRequestBuilder.build();
120     ClientProtos.MultiResponse responseProto = getStub().multi(getRpcController(), requestProto);
121     if (responseProto == null) return null; // Occurs on cancel
122     return ResponseConverter.getResults(requestProto, responseProto, getRpcControllerCellScanner());
123   }
124
125   /**
126    * @return True if we should send data in cellblocks.  This is an expensive call.  Cache the
127    * result if you can rather than call each time.
128    */
129   private boolean isCellBlock() {
130     // This is not exact -- the configuration could have changed on us after connection was set up
131     // but it will do for now.
132     ClusterConnection conn = getConnection();
133     return conn.hasCellBlockSupport();
134   }
135
136   @Override
137   public void prepare(boolean reload) throws IOException {
138     // Use the location we were given in the constructor rather than go look it up.
139     setStub(getConnection().getClient(this.location.getServerName()));
140   }
141
142   @VisibleForTesting
143   ServerName getServerName() {
144     return location.getServerName();
145   }
146 }