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
37
38
39
40 public class HMsg implements Writable {
41 public static final HMsg [] STOP_REGIONSERVER_ARRAY =
42 new HMsg [] {new HMsg(Type.STOP_REGIONSERVER)};
43 public static final HMsg [] EMPTY_HMSG_ARRAY = new HMsg[0];
44
45 public static enum Type {
46
47
48 STOP_REGIONSERVER,
49
50
51
52
53 REGION_SPLIT,
54
55
56
57
58
59
60 TESTING_BLOCK_REGIONSERVER,
61 }
62
63 private Type type = null;
64 private HRegionInfo info = null;
65 private byte[] message = null;
66 private HRegionInfo daughterA = null;
67 private HRegionInfo daughterB = null;
68
69
70 public HMsg() {
71 this(null);
72 }
73
74
75
76
77
78 public HMsg(final HMsg.Type type) {
79 this(type, new HRegionInfo(), null);
80 }
81
82
83
84
85
86
87 public HMsg(final HMsg.Type type, final HRegionInfo hri) {
88 this(type, hri, null);
89 }
90
91
92
93
94
95
96
97
98
99 public HMsg(final HMsg.Type type, final HRegionInfo hri, final byte[] msg) {
100 this(type, hri, null, null, msg);
101 }
102
103
104
105
106
107
108
109
110
111
112
113 public HMsg(final HMsg.Type type, final HRegionInfo hri,
114 final HRegionInfo daughterA, final HRegionInfo daughterB, final byte[] msg) {
115 this.type = type;
116 if (hri == null) {
117 throw new NullPointerException("Region cannot be null");
118 }
119 this.info = hri;
120 this.message = msg;
121 this.daughterA = daughterA;
122 this.daughterB = daughterB;
123 }
124
125
126
127
128 public HRegionInfo getRegionInfo() {
129 return this.info;
130 }
131
132
133 public Type getType() {
134 return this.type;
135 }
136
137
138
139
140
141 public boolean isType(final HMsg.Type other) {
142 return this.type.equals(other);
143 }
144
145
146 public byte[] getMessage() {
147 return this.message;
148 }
149
150
151
152
153
154 public HRegionInfo getDaughterA() {
155 return this.daughterA;
156 }
157
158
159
160
161
162 public HRegionInfo getDaughterB() {
163 return this.daughterB;
164 }
165
166
167
168
169 @Override
170 public String toString() {
171 StringBuilder sb = new StringBuilder();
172 sb.append(this.type.toString());
173
174 if (this.info != null && this.info.getRegionName().length > 0) {
175 sb.append(": ");
176 sb.append(this.info.getRegionNameAsString());
177 }
178 if (this.message != null && this.message.length > 0) {
179 sb.append(": " + Bytes.toString(this.message));
180 }
181 return sb.toString();
182 }
183
184
185
186
187 @Override
188 public boolean equals(Object obj) {
189 if (this == obj) {
190 return true;
191 }
192 if (obj == null) {
193 return false;
194 }
195 if (getClass() != obj.getClass()) {
196 return false;
197 }
198 HMsg that = (HMsg)obj;
199 return this.type.equals(that.type) &&
200 (this.info != null)? this.info.equals(that.info):
201 that.info == null;
202 }
203
204
205
206
207 @Override
208 public int hashCode() {
209 int result = this.type.hashCode();
210 if (this.info != null) {
211 result ^= this.info.hashCode();
212 }
213 return result;
214 }
215
216
217
218
219
220
221
222
223 public void write(DataOutput out) throws IOException {
224 out.writeInt(this.type.ordinal());
225 this.info.write(out);
226 if (this.message == null || this.message.length == 0) {
227 out.writeBoolean(false);
228 } else {
229 out.writeBoolean(true);
230 Bytes.writeByteArray(out, this.message);
231 }
232 if (this.type.equals(Type.REGION_SPLIT)) {
233 this.daughterA.write(out);
234 this.daughterB.write(out);
235 }
236 }
237
238
239
240
241 public void readFields(DataInput in) throws IOException {
242 int ordinal = in.readInt();
243 this.type = HMsg.Type.values()[ordinal];
244 this.info.readFields(in);
245 boolean hasMessage = in.readBoolean();
246 if (hasMessage) {
247 this.message = Bytes.readByteArray(in);
248 }
249 if (this.type.equals(Type.REGION_SPLIT)) {
250 this.daughterA = new HRegionInfo();
251 this.daughterB = new HRegionInfo();
252 this.daughterA.readFields(in);
253 this.daughterB.readFields(in);
254 }
255 }
256 }