1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase;
21
22 import java.io.DataInput;
23 import java.io.DataOutput;
24 import java.io.IOException;
25
26 import org.apache.hadoop.hbase.util.Bytes;
27 import org.apache.hadoop.io.Writable;
28
29
30
31
32
33
34
35
36 public class HMsg implements Writable {
37 public static final HMsg REGIONSERVER_QUIESCE =
38 new HMsg(Type.MSG_REGIONSERVER_QUIESCE);
39 public static final HMsg REGIONSERVER_STOP =
40 new HMsg(Type.MSG_REGIONSERVER_STOP);
41 public static final HMsg [] EMPTY_HMSG_ARRAY = new HMsg[0];
42
43
44
45
46 public static enum Type {
47
48 MSG_NONE,
49
50
51
52 MSG_REGION_OPEN,
53
54
55 MSG_REGION_CLOSE,
56
57
58 MSG_REGION_SPLIT,
59
60
61 MSG_REGION_COMPACT,
62
63
64 MSG_REGIONSERVER_STOP,
65
66
67
68
69 MSG_REGION_CLOSE_WITHOUT_REPORT,
70
71
72 MSG_REGIONSERVER_QUIESCE,
73
74
75
76 MSG_REPORT_OPEN,
77
78
79 MSG_REPORT_CLOSE,
80
81
82 MSG_REPORT_PROCESS_OPEN,
83
84
85
86
87
88
89
90
91 MSG_REPORT_SPLIT,
92
93
94
95
96
97
98
99 MSG_REPORT_EXITING,
100
101
102
103
104 MSG_REPORT_QUIESCED,
105
106
107
108
109 MSG_REGION_FLUSH,
110
111
112
113
114 MSG_REGION_MAJOR_COMPACT,
115
116
117
118
119
120
121
122 MSG_REPORT_SPLIT_INCLUDES_DAUGHTERS,
123
124
125
126
127
128
129 TESTING_MSG_BLOCK_RS,
130 }
131
132 private Type type = null;
133 private HRegionInfo info = null;
134 private byte[] message = null;
135 private HRegionInfo daughterA = null;
136 private HRegionInfo daughterB = null;
137
138
139 public HMsg() {
140 this(Type.MSG_NONE);
141 }
142
143
144
145
146
147 public HMsg(final HMsg.Type type) {
148 this(type, new HRegionInfo(), null);
149 }
150
151
152
153
154
155
156 public HMsg(final HMsg.Type type, final HRegionInfo hri) {
157 this(type, hri, null);
158 }
159
160
161
162
163
164
165
166
167
168 public HMsg(final HMsg.Type type, final HRegionInfo hri, final byte[] msg) {
169 this(type, hri, null, null, msg);
170 }
171
172
173
174
175
176
177
178
179
180
181
182 public HMsg(final HMsg.Type type, final HRegionInfo hri,
183 final HRegionInfo daughterA, final HRegionInfo daughterB, final byte[] msg) {
184 if (type == null) {
185 throw new NullPointerException("Message type cannot be null");
186 }
187 this.type = type;
188 if (hri == null) {
189 throw new NullPointerException("Region cannot be null");
190 }
191 this.info = hri;
192 this.message = msg;
193 this.daughterA = daughterA;
194 this.daughterB = daughterB;
195 }
196
197
198
199
200 public HRegionInfo getRegionInfo() {
201 return this.info;
202 }
203
204
205 public Type getType() {
206 return this.type;
207 }
208
209
210
211
212
213 public boolean isType(final HMsg.Type other) {
214 return this.type.equals(other);
215 }
216
217
218 public byte[] getMessage() {
219 return this.message;
220 }
221
222
223
224
225
226 public HRegionInfo getDaughterA() {
227 return this.daughterA;
228 }
229
230
231
232
233
234 public HRegionInfo getDaughterB() {
235 return this.daughterB;
236 }
237
238
239
240
241 @Override
242 public String toString() {
243 StringBuilder sb = new StringBuilder();
244 sb.append(this.type.toString());
245
246 if (this.info != null && this.info.getRegionName().length > 0) {
247 sb.append(": ");
248 sb.append(this.info.getRegionNameAsString());
249 }
250 if (this.message != null && this.message.length > 0) {
251 sb.append(": " + Bytes.toString(this.message));
252 }
253 return sb.toString();
254 }
255
256
257
258
259 @Override
260 public boolean equals(Object obj) {
261 if (this == obj) {
262 return true;
263 }
264 if (obj == null) {
265 return false;
266 }
267 if (getClass() != obj.getClass()) {
268 return false;
269 }
270 HMsg that = (HMsg)obj;
271 return this.type.equals(that.type) &&
272 (this.info != null)? this.info.equals(that.info):
273 that.info == null;
274 }
275
276
277
278
279 @Override
280 public int hashCode() {
281 int result = this.type.hashCode();
282 if (this.info != null) {
283 result ^= this.info.hashCode();
284 }
285 return result;
286 }
287
288
289
290
291
292
293
294
295 public void write(DataOutput out) throws IOException {
296 out.writeInt(this.type.ordinal());
297 this.info.write(out);
298 if (this.message == null || this.message.length == 0) {
299 out.writeBoolean(false);
300 } else {
301 out.writeBoolean(true);
302 Bytes.writeByteArray(out, this.message);
303 }
304 if (this.type.equals(Type.MSG_REPORT_SPLIT_INCLUDES_DAUGHTERS)) {
305 this.daughterA.write(out);
306 this.daughterB.write(out);
307 }
308 }
309
310
311
312
313 public void readFields(DataInput in) throws IOException {
314 int ordinal = in.readInt();
315 this.type = HMsg.Type.values()[ordinal];
316 this.info.readFields(in);
317 boolean hasMessage = in.readBoolean();
318 if (hasMessage) {
319 this.message = Bytes.readByteArray(in);
320 }
321 if (this.type.equals(Type.MSG_REPORT_SPLIT_INCLUDES_DAUGHTERS)) {
322 this.daughterA = new HRegionInfo();
323 this.daughterB = new HRegionInfo();
324 this.daughterA.readFields(in);
325 this.daughterB.readFields(in);
326 }
327 }
328 }