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