1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.ipc;
19
20 import com.google.protobuf.Descriptors;
21 import com.google.protobuf.Message;
22 import org.apache.hadoop.hbase.CellScanner;
23 import org.apache.hadoop.hbase.classification.InterfaceAudience;
24 import org.apache.hadoop.hbase.client.MetricsConnection;
25 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
26 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
27
28 import java.io.IOException;
29
30
31 @InterfaceAudience.Private
32 public class Call {
33 final int id;
34 final Message param;
35
36
37
38
39 CellScanner cells;
40 Message response;
41
42 Message responseDefaultType;
43 IOException error;
44 volatile boolean done;
45 final Descriptors.MethodDescriptor md;
46 final int timeout;
47 final MetricsConnection.CallStats callStats;
48
49 protected Call(int id, final Descriptors.MethodDescriptor md, Message param,
50 final CellScanner cells, final Message responseDefaultType, int timeout,
51 MetricsConnection.CallStats callStats) {
52 this.param = param;
53 this.md = md;
54 this.cells = cells;
55 this.callStats = callStats;
56 this.callStats.setStartTime(EnvironmentEdgeManager.currentTime());
57 this.responseDefaultType = responseDefaultType;
58 this.id = id;
59 this.timeout = timeout;
60 }
61
62
63
64
65
66 public boolean checkAndSetTimeout() {
67 if (timeout == 0){
68 return false;
69 }
70
71 long waitTime = EnvironmentEdgeManager.currentTime() - getStartTime();
72 if (waitTime >= timeout) {
73 IOException ie = new CallTimeoutException("Call id=" + id +
74 ", waitTime=" + waitTime + ", operationTimeout=" + timeout + " expired.");
75 setException(ie);
76 return true;
77 } else {
78 return false;
79 }
80 }
81
82 public int remainingTime() {
83 if (timeout == 0) {
84 return Integer.MAX_VALUE;
85 }
86
87 int remaining = timeout - (int) (EnvironmentEdgeManager.currentTime() - getStartTime());
88 return remaining > 0 ? remaining : 0;
89 }
90
91 @Override
92 public String toString() {
93 return "callId: " + this.id + " methodName: " + this.md.getName() + " param {" +
94 (this.param != null? ProtobufUtil.getShortTextFormat(this.param): "") + "}";
95 }
96
97
98
99 protected synchronized void callComplete() {
100 this.done = true;
101 notify();
102 }
103
104
105
106
107
108
109 public void setException(IOException error) {
110 this.error = error;
111 callComplete();
112 }
113
114
115
116
117
118
119
120
121 public void setResponse(Message response, final CellScanner cells) {
122 this.response = response;
123 this.cells = cells;
124 callComplete();
125 }
126
127 public long getStartTime() {
128 return this.callStats.getStartTime();
129 }
130 }