-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
AsyncCall.java
200 lines (168 loc) · 5.98 KB
/
AsyncCall.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.hbase.ipc;
import com.google.protobuf.Descriptors;
import com.google.protobuf.Message;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.hbase.CellScanner;
import org.apache.hadoop.hbase.classification.InterfaceAudience;
import org.apache.hadoop.hbase.client.MetricsConnection;
import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
import org.apache.hadoop.hbase.util.ExceptionUtil;
import org.apache.hadoop.ipc.RemoteException;
/**
* Represents an Async Hbase call and its response.
*
* Responses are passed on to its given doneHandler and failures to the rpcController
*
* @param <T> Type of message returned
* @param <M> Message returned in communication to be converted
*/
@InterfaceAudience.Private
public class AsyncCall<M extends Message, T> extends Promise<T> {
private static final Log LOG = LogFactory.getLog(AsyncCall.class.getName());
final int id;
private final AsyncRpcChannelImpl channel;
final Descriptors.MethodDescriptor method;
final Message param;
final Message responseDefaultType;
private final MessageConverter<M,T> messageConverter;
private final IOExceptionConverter exceptionConverter;
final long rpcTimeout;
// For only the request
private final CellScanner cellScanner;
private final int priority;
final MetricsConnection clientMetrics;
final MetricsConnection.CallStats callStats;
/**
* Constructor
*
* @param channel which initiated call
* @param connectId connection id
* @param md the method descriptor
* @param param parameters to send to Server
* @param cellScanner cellScanner containing cells to send as request
* @param responseDefaultType the default response type
* @param messageConverter converts the messages to what is the expected output
* @param exceptionConverter converts exceptions to expected format. Can be null
* @param rpcTimeout timeout for this call in ms
* @param priority for this request
* @param metrics MetricsConnection to which the metrics are stored for this request
*/
public AsyncCall(AsyncRpcChannelImpl channel, int connectId, Descriptors.MethodDescriptor
md, Message param, CellScanner cellScanner, M responseDefaultType, MessageConverter<M, T>
messageConverter, IOExceptionConverter exceptionConverter, long rpcTimeout, int priority,
MetricsConnection metrics) {
super(channel.getEventExecutor());
this.channel = channel;
this.id = connectId;
this.method = md;
this.param = param;
this.responseDefaultType = responseDefaultType;
this.messageConverter = messageConverter;
this.exceptionConverter = exceptionConverter;
this.rpcTimeout = rpcTimeout;
this.priority = priority;
this.cellScanner = cellScanner;
this.callStats = MetricsConnection.newCallStats();
callStats.setStartTime(EnvironmentEdgeManager.currentTime());
this.clientMetrics = metrics;
}
/**
* Get the start time
*
* @return start time for the call
*/
public long getStartTime() {
return this.callStats.getStartTime();
}
@Override
public String toString() {
return "callId=" + this.id + ", method=" + this.method.getName() +
", rpcTimeout=" + this.rpcTimeout + ", param {" +
(this.param != null ? ProtobufUtil.getShortTextFormat(this.param) : "") + "}";
}
/**
* Set success with a cellBlockScanner
*
* @param value to set
* @param cellBlockScanner to set
*/
public void setSuccess(M value, CellScanner cellBlockScanner) {
callStats.setCallTimeMs(EnvironmentEdgeManager.currentTime() - callStats.getStartTime());
if (LOG.isTraceEnabled()) {
LOG.trace("Call: " + method.getName() + ", callTime: " + callStats.getCallTimeMs() + "ms");
}
if (clientMetrics != null) {
clientMetrics.updateRpc(method, param, callStats);
}
try {
this.setSuccess(
this.messageConverter.convert(value, cellBlockScanner)
);
} catch (IOException e) {
this.setFailed(e);
}
}
/**
* Set failed
*
* @param exception to set
*/
public void setFailed(IOException exception) {
if (ExceptionUtil.isInterrupt(exception)) {
exception = ExceptionUtil.asInterrupt(exception);
}
if (exception instanceof RemoteException) {
exception = ((RemoteException) exception).unwrapRemoteException();
}
if (this.exceptionConverter != null) {
exception = this.exceptionConverter.convert(exception);
}
this.setFailure(exception);
}
/**
* Get the rpc timeout
*
* @return current timeout for this call
*/
public long getRpcTimeout() {
return rpcTimeout;
}
/**
* @return Priority for this call
*/
public int getPriority() {
return priority;
}
/**
* Get the cellScanner for this request.
* @return CellScanner
*/
public CellScanner cellScanner() {
return cellScanner;
}
@Override
public boolean cancel(boolean mayInterupt){
this.channel.removePendingCall(this.id);
return super.cancel(mayInterupt);
}
}