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.RpcCallback;
21
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.apache.hadoop.hbase.CellScannable;
27 import org.apache.hadoop.hbase.CellScanner;
28 import org.apache.hadoop.hbase.CellUtil;
29 import org.apache.hadoop.hbase.HConstants;
30 import org.apache.hadoop.hbase.TableName;
31 import org.apache.hadoop.hbase.classification.InterfaceAudience;
32
33
34
35
36
37
38
39 @InterfaceAudience.Private
40 public class HBaseRpcControllerImpl implements HBaseRpcController {
41
42
43
44 private Integer callTimeout;
45
46 private boolean done = false;
47
48 private boolean cancelled = false;
49
50 private final List<RpcCallback<Object>> cancellationCbs = new ArrayList<>();
51
52 private IOException exception;
53
54
55
56
57
58
59 private int priority = HConstants.PRIORITY_UNSET;
60
61
62
63
64
65
66
67 private CellScanner cellScanner;
68
69 public HBaseRpcControllerImpl() {
70 this((CellScanner) null);
71 }
72
73 public HBaseRpcControllerImpl(final CellScanner cellScanner) {
74 this.cellScanner = cellScanner;
75 }
76
77 public HBaseRpcControllerImpl(final List<CellScannable> cellIterables) {
78 this.cellScanner = cellIterables == null ? null : CellUtil.createCellScanner(cellIterables);
79 }
80
81
82
83
84 @Override
85 public CellScanner cellScanner() {
86 return cellScanner;
87 }
88
89 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "IS2_INCONSISTENT_SYNC",
90 justification = "The only possible race method is startCancel")
91 @Override
92 public void setCellScanner(final CellScanner cellScanner) {
93 this.cellScanner = cellScanner;
94 }
95
96 @Override
97 public void setPriority(int priority) {
98 this.priority = Math.max(this.priority, priority);
99 }
100
101 @Override
102 public void setPriority(final TableName tn) {
103 setPriority(
104 tn != null && tn.isSystemTable() ? HConstants.SYSTEMTABLE_QOS : HConstants.NORMAL_QOS);
105 }
106
107 @Override
108 public int getPriority() {
109 return priority < 0 ? HConstants.NORMAL_QOS : priority;
110 }
111
112 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "IS2_INCONSISTENT_SYNC",
113 justification = "The only possible race method is startCancel")
114 @Override
115 public void reset() {
116 priority = 0;
117 cellScanner = null;
118 exception = null;
119 callTimeout = null;
120
121
122
123
124 synchronized (this) {
125 done = false;
126 cancelled = false;
127 cancellationCbs.clear();
128 }
129 }
130
131 @Override
132 public int getCallTimeout() {
133 if (callTimeout != null) {
134 return callTimeout.intValue();
135 } else {
136 return 0;
137 }
138 }
139
140 @Override
141 public void setCallTimeout(int callTimeout) {
142 this.callTimeout = callTimeout;
143 }
144
145 @Override
146 public boolean hasCallTimeout() {
147 return callTimeout != null;
148 }
149
150 @Override
151 public synchronized String errorText() {
152 if (!done || exception == null) {
153 return null;
154 }
155 return exception.getMessage();
156 }
157
158 @Override
159 public synchronized boolean failed() {
160 return done && this.exception != null;
161 }
162
163 @Override
164 public synchronized boolean isCanceled() {
165 return cancelled;
166 }
167
168 @Override
169 public void notifyOnCancel(RpcCallback<Object> callback) {
170 synchronized (this) {
171 if (done) {
172 return;
173 }
174 if (!cancelled) {
175 cancellationCbs.add(callback);
176 return;
177 }
178 }
179
180 callback.run(null);
181 }
182
183 @Override
184 public synchronized void setFailed(String reason) {
185 if (done) {
186 return;
187 }
188 done = true;
189 exception = new IOException(reason);
190 }
191
192 @Override
193 public synchronized void setFailed(IOException e) {
194 if (done) {
195 return;
196 }
197 done = true;
198 exception = e;
199 }
200
201 @Override
202 public synchronized IOException getFailed() {
203 return done ? exception : null;
204 }
205
206 @Override
207 public synchronized void setDone(CellScanner cellScanner) {
208 if (done) {
209 return;
210 }
211 done = true;
212 this.cellScanner = cellScanner;
213 }
214
215 @Override
216 public void startCancel() {
217
218
219 List<RpcCallback<Object>> cbs;
220 synchronized (this) {
221 if (done) {
222 return;
223 }
224 done = true;
225 cancelled = true;
226 cbs = new ArrayList<>(cancellationCbs);
227 }
228 for (RpcCallback<?> cb : cbs) {
229 cb.run(null);
230 }
231 }
232
233 @Override
234 public synchronized void notifyOnCancel(RpcCallback<Object> callback, CancellationCallback action)
235 throws IOException {
236 if (cancelled) {
237 action.run(true);
238 } else {
239 cancellationCbs.add(callback);
240 action.run(false);
241 }
242 }
243
244 }