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.Collections;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.hbase.CellScannable;
28  import org.apache.hadoop.hbase.DoNotRetryIOException;
29  import org.apache.hadoop.hbase.HConstants;
30  import org.apache.hadoop.hbase.HRegionLocation;
31  import org.apache.hadoop.hbase.TableName;
32  import org.apache.hadoop.hbase.ipc.PayloadCarryingRpcController;
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.protobuf.ServiceException;
44  
45  /**
46   * Callable that handles the <code>multi</code> method call going against a single
47   * regionserver; i.e. A {@link RegionServerCallable} for the multi call (It is not a
48   * {@link RegionServerCallable} that goes against multiple regions.
49   * @param <R>
50   */
51  class MultiServerCallable<R> extends RegionServerCallable<MultiResponse> {
52    private final MultiAction<R> multiAction;
53    private final boolean cellBlock;
54    private RpcControllerFactory rpcFactory;
55  
56    MultiServerCallable(final HConnection connection, final TableName tableName,
57        final HRegionLocation location, final RpcControllerFactory rpcFactory,
58        final MultiAction<R> multi) {
59      super(connection, tableName, null);
60      this.multiAction = multi;
61      this.rpcFactory = rpcFactory;
62      setLocation(location);
63      this.cellBlock = isCellBlock();
64    }
65  
66    MultiAction<R> getMulti() {
67      return this.multiAction;
68    }
69  
70    @Override
71    public MultiResponse call() throws IOException {
72      int countOfActions = this.multiAction.size();
73      if (countOfActions <= 0) throw new DoNotRetryIOException("No Actions");
74      MultiRequest.Builder multiRequestBuilder = MultiRequest.newBuilder();
75      RegionAction.Builder regionActionBuilder = RegionAction.newBuilder();
76      ClientProtos.Action.Builder actionBuilder = ClientProtos.Action.newBuilder();
77      MutationProto.Builder mutationBuilder = MutationProto.newBuilder();
78      List<CellScannable> cells = null;
79      // The multi object is a list of Actions by region.  Iterate by region.
80      long nonceGroup = multiAction.getNonceGroup();
81      if (nonceGroup != HConstants.NO_NONCE) {
82        multiRequestBuilder.setNonceGroup(nonceGroup);
83      }
84      for (Map.Entry<byte[], List<Action<R>>> e: this.multiAction.actions.entrySet()) {
85        final byte [] regionName = e.getKey();
86        final List<Action<R>> actions = e.getValue();
87        regionActionBuilder.clear();
88        regionActionBuilder.setRegion(RequestConverter.buildRegionSpecifier(
89          HBaseProtos.RegionSpecifier.RegionSpecifierType.REGION_NAME, regionName) );
90  
91  
92        if (this.cellBlock) {
93          // Presize.  Presume at least a KV per Action.  There are likely more.
94          if (cells == null) cells = new ArrayList<CellScannable>(countOfActions);
95          // Send data in cellblocks. The call to buildNoDataMultiRequest will skip RowMutations.
96          // They have already been handled above. Guess at count of cells
97          regionActionBuilder = RequestConverter.buildNoDataRegionAction(regionName, actions, cells,
98            regionActionBuilder, actionBuilder, mutationBuilder);
99        } else {
100         regionActionBuilder = RequestConverter.buildRegionAction(regionName, actions,
101           regionActionBuilder, actionBuilder, mutationBuilder);
102       }
103       multiRequestBuilder.addRegionAction(regionActionBuilder.build());
104     }
105 
106     // Controller optionally carries cell data over the proxy/service boundary and also
107     // optionally ferries cell response data back out again.
108     PayloadCarryingRpcController controller = rpcFactory.newController(cells);
109     controller.setPriority(getTableName());
110     ClientProtos.MultiResponse responseProto;
111     ClientProtos.MultiRequest requestProto = multiRequestBuilder.build();
112     try {
113       responseProto = getStub().multi(controller, requestProto);
114     } catch (ServiceException e) {
115       throw ProtobufUtil.getRemoteException(e);
116     }
117     return ResponseConverter.getResults(requestProto, responseProto, controller.cellScanner());
118   }
119 
120 
121 
122   /**
123    * @return True if we should send data in cellblocks.  This is an expensive call.  Cache the
124    * result if you can rather than call each time.
125    */
126   private boolean isCellBlock() {
127     // This is not exact -- the configuration could have changed on us after connection was set up
128     // but it will do for now.
129     HConnection connection = getConnection();
130     if (connection == null) return true; // Default is to do cellblocks.
131     Configuration configuration = connection.getConfiguration();
132     if (configuration == null) return true;
133     String codec = configuration.get(HConstants.RPC_CODEC_CONF_KEY, "");
134     return codec != null && codec.length() > 0;
135   }
136 
137   @Override
138   public void prepare(boolean reload) throws IOException {
139     // Use the location we were given in the constructor rather than go look it up.
140     setStub(getConnection().getClient(getLocation().getServerName()));
141   }
142 }