1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.ipc;
20
21 import java.io.IOException;
22 import java.util.concurrent.atomic.AtomicReference;
23
24 import org.apache.hadoop.hbase.classification.InterfaceAudience;
25
26 import com.google.protobuf.RpcCallback;
27 import com.google.protobuf.RpcController;
28
29 @InterfaceAudience.Private
30 public class TimeLimitedRpcController implements RpcController {
31
32
33
34
35 protected volatile Integer callTimeout;
36 protected volatile boolean cancelled = false;
37 protected final AtomicReference<RpcCallback<Object>> cancellationCb =
38 new AtomicReference<RpcCallback<Object>>(null);
39
40 protected final AtomicReference<RpcCallback<IOException>> failureCb =
41 new AtomicReference<RpcCallback<IOException>>(null);
42
43 private IOException exception;
44
45 public Integer getCallTimeout() {
46 return callTimeout;
47 }
48
49 public void setCallTimeout(int callTimeout) {
50 this.callTimeout = callTimeout;
51 }
52
53 public boolean hasCallTimeout(){
54 return callTimeout != null;
55 }
56
57 @Override
58 public String errorText() {
59 if (exception != null) {
60 return exception.getMessage();
61 } else {
62 return null;
63 }
64 }
65
66
67
68
69
70 @Override
71 public boolean failed() {
72 return this.exception != null;
73 }
74
75 @Override
76 public boolean isCanceled() {
77 return cancelled;
78 }
79
80 @Override
81 public void notifyOnCancel(RpcCallback<Object> cancellationCb) {
82 this.cancellationCb.set(cancellationCb);
83 if (this.cancelled) {
84 cancellationCb.run(null);
85 }
86 }
87
88
89
90
91
92
93
94 public void notifyOnFail(RpcCallback<IOException> failureCb) {
95 this.failureCb.set(failureCb);
96 if (this.exception != null) {
97 failureCb.run(this.exception);
98 }
99 }
100
101 @Override
102 public void reset() {
103 exception = null;
104 cancelled = false;
105 failureCb.set(null);
106 cancellationCb.set(null);
107 callTimeout = null;
108 }
109
110 @Override
111 public void setFailed(String reason) {
112 this.exception = new IOException(reason);
113 if (this.failureCb.get() != null) {
114 this.failureCb.get().run(this.exception);
115 }
116 }
117
118
119
120
121
122
123
124 public void setFailed(IOException e) {
125 this.exception = e;
126 if (this.failureCb.get() != null) {
127 this.failureCb.get().run(this.exception);
128 }
129 }
130
131 @Override
132 public void startCancel() {
133 cancelled = true;
134 if (cancellationCb.get() != null) {
135 cancellationCb.get().run(null);
136 }
137 }
138 }