1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.TableName;
27 import org.apache.hadoop.hbase.HRegionLocation;
28 import org.apache.hadoop.hbase.ipc.PayloadCarryingRpcController;
29 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
30 import org.apache.hadoop.hbase.protobuf.RequestConverter;
31 import org.apache.hadoop.hbase.protobuf.ResponseConverter;
32 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
33 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MultiRequest;
34
35 import com.google.protobuf.ServiceException;
36
37
38
39
40
41
42
43 class MultiServerCallable<R> extends RegionServerCallable<MultiResponse> {
44 private final MultiAction<R> multi;
45
46 MultiServerCallable(final HConnection connection, final TableName tableName,
47 final HRegionLocation location, final MultiAction<R> multi) {
48 super(connection, tableName, null);
49 this.multi = multi;
50 setLocation(location);
51 }
52
53 MultiAction<R> getMulti() {
54 return this.multi;
55 }
56
57 @Override
58 public MultiResponse call() throws IOException {
59 MultiResponse response = new MultiResponse();
60
61 for (Map.Entry<byte[], List<Action<R>>> e: this.multi.actions.entrySet()) {
62 byte[] regionName = e.getKey();
63 int rowMutations = 0;
64 List<Action<R>> actions = e.getValue();
65 for (Action<R> action : actions) {
66 Row row = action.getAction();
67
68
69 if (row instanceof RowMutations) {
70 try {
71 RowMutations rms = (RowMutations)row;
72
73
74 List<CellScannable> cells = new ArrayList<CellScannable>(rms.getMutations().size());
75
76 MultiRequest multiRequest =
77 RequestConverter.buildNoDataMultiRequest(regionName, rms, cells);
78
79
80 getStub().multi(new PayloadCarryingRpcController(cells), multiRequest);
81
82 response.add(regionName, action.getOriginalIndex(), Result.EMPTY_RESULT);
83 } catch (ServiceException se) {
84 response.add(regionName, action.getOriginalIndex(),
85 ProtobufUtil.getRemoteException(se));
86 }
87 rowMutations++;
88 }
89 }
90
91 if (actions.size() > rowMutations) {
92 Exception ex = null;
93 List<Object> results = null;
94
95
96 List<CellScannable> cells = new ArrayList<CellScannable>(actions.size() - rowMutations);
97 try {
98
99
100 MultiRequest multiRequest =
101 RequestConverter.buildNoDataMultiRequest(regionName, actions, cells);
102
103
104 PayloadCarryingRpcController controller = new PayloadCarryingRpcController(cells);
105 ClientProtos.MultiResponse responseProto = getStub().multi(controller, multiRequest);
106 results = ResponseConverter.getResults(responseProto, controller.cellScanner());
107 } catch (ServiceException se) {
108 ex = ProtobufUtil.getRemoteException(se);
109 }
110 for (int i = 0, n = actions.size(); i < n; i++) {
111 int originalIndex = actions.get(i).getOriginalIndex();
112 response.add(regionName, originalIndex, results == null ? ex : results.get(i));
113 }
114 }
115 }
116 return response;
117 }
118
119 @Override
120 public void prepare(boolean reload) throws IOException {
121
122 setStub(getConnection().getClient(getLocation().getServerName()));
123 }
124 }