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 com.google.protobuf.RpcCallback;
23
24 import io.netty.util.Timeout;
25
26 import java.io.IOException;
27
28 import org.apache.hadoop.hbase.CellScanner;
29 import org.apache.hadoop.hbase.classification.InterfaceAudience;
30 import org.apache.hadoop.hbase.client.MetricsConnection;
31 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
32 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
33 import org.apache.htrace.Span;
34 import org.apache.htrace.Trace;
35
36
37 @InterfaceAudience.Private
38 class Call {
39 final int id;
40 final Message param;
41
42
43
44
45 CellScanner cells;
46 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "IS2_INCONSISTENT_SYNC",
47 justification = "Direct access is only allowed after done")
48 Message response;
49
50 Message responseDefaultType;
51 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "IS2_INCONSISTENT_SYNC",
52 justification = "Direct access is only allowed after done")
53 IOException error;
54 private boolean done;
55 final Descriptors.MethodDescriptor md;
56 final int timeout;
57 final int priority;
58 final MetricsConnection.CallStats callStats;
59 final RpcCallback<Call> callback;
60 final Span span;
61 Timeout timeoutTask;
62
63 protected Call(int id, final Descriptors.MethodDescriptor md, Message param,
64 final CellScanner cells, final Message responseDefaultType, int timeout, int priority,
65 RpcCallback<Call> callback, MetricsConnection.CallStats callStats) {
66 this.param = param;
67 this.md = md;
68 this.cells = cells;
69 this.callStats = callStats;
70 this.callStats.setStartTime(EnvironmentEdgeManager.currentTime());
71 this.responseDefaultType = responseDefaultType;
72 this.id = id;
73 this.timeout = timeout;
74 this.priority = priority;
75 this.callback = callback;
76 this.span = Trace.currentSpan();
77 }
78
79 @Override
80 public String toString() {
81 return "callId: " + this.id + " methodName: " + this.md.getName() + " param {"
82 + (this.param != null ? ProtobufUtil.getShortTextFormat(this.param) : "") + "}";
83 }
84
85
86
87
88 public void setTimeout(IOException error) {
89 synchronized (this) {
90 if (done) {
91 return;
92 }
93 this.done = true;
94 this.error = error;
95 }
96 callback.run(this);
97 }
98
99 private void callComplete() {
100 if (timeoutTask != null) {
101 timeoutTask.cancel();
102 }
103 callback.run(this);
104 }
105
106
107
108
109
110 public void setException(IOException error) {
111 synchronized (this) {
112 if (done) {
113 return;
114 }
115 this.done = true;
116 this.error = error;
117 }
118 callComplete();
119 }
120
121
122
123
124
125
126 public void setResponse(Message response, final CellScanner cells) {
127 synchronized (this) {
128 if (done) {
129 return;
130 }
131 this.done = true;
132 this.response = response;
133 this.cells = cells;
134 }
135 callComplete();
136 }
137
138 public synchronized boolean isDone() {
139 return done;
140 }
141
142 public long getStartTime() {
143 return this.callStats.getStartTime();
144 }
145 }