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