1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase;
19
20 import org.apache.hadoop.classification.InterfaceAudience;
21 import org.apache.hadoop.hbase.exceptions.DeserializationException;
22 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
23 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
24 import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
25 import org.apache.hadoop.hbase.util.Bytes;
26
27 import com.google.protobuf.InvalidProtocolBufferException;
28
29
30
31
32
33
34
35 @InterfaceAudience.Private
36 public class SplitLogTask {
37 private final ServerName originServer;
38 private final ZooKeeperProtos.SplitLogTask.State state;
39
40 public static class Unassigned extends SplitLogTask {
41 public Unassigned(final ServerName originServer) {
42 super(originServer, ZooKeeperProtos.SplitLogTask.State.UNASSIGNED);
43 }
44 }
45
46 public static class Owned extends SplitLogTask {
47 public Owned(final ServerName originServer) {
48 super(originServer, ZooKeeperProtos.SplitLogTask.State.OWNED);
49 }
50 }
51
52 public static class Resigned extends SplitLogTask {
53 public Resigned(final ServerName originServer) {
54 super(originServer, ZooKeeperProtos.SplitLogTask.State.RESIGNED);
55 }
56 }
57
58 public static class Done extends SplitLogTask {
59 public Done(final ServerName originServer) {
60 super(originServer, ZooKeeperProtos.SplitLogTask.State.DONE);
61 }
62 }
63
64 public static class Err extends SplitLogTask {
65 public Err(final ServerName originServer) {
66 super(originServer, ZooKeeperProtos.SplitLogTask.State.ERR);
67 }
68 }
69
70 SplitLogTask(final ZooKeeperProtos.SplitLogTask slt) {
71 this(ProtobufUtil.toServerName(slt.getServerName()), slt.getState());
72 }
73
74 SplitLogTask(final ServerName originServer, final ZooKeeperProtos.SplitLogTask.State state) {
75 this.originServer = originServer;
76 this.state = state;
77 }
78
79 public ServerName getServerName() {
80 return this.originServer;
81 }
82
83 public boolean isUnassigned(final ServerName sn) {
84 return this.originServer.equals(sn) && isUnassigned();
85 }
86
87 public boolean isUnassigned() {
88 return this.state == ZooKeeperProtos.SplitLogTask.State.UNASSIGNED;
89 }
90
91 public boolean isOwned(final ServerName sn) {
92 return this.originServer.equals(sn) && isOwned();
93 }
94
95 public boolean isOwned() {
96 return this.state == ZooKeeperProtos.SplitLogTask.State.OWNED;
97 }
98
99 public boolean isResigned(final ServerName sn) {
100 return this.originServer.equals(sn) && isResigned();
101 }
102
103 public boolean isResigned() {
104 return this.state == ZooKeeperProtos.SplitLogTask.State.RESIGNED;
105 }
106
107 public boolean isDone(final ServerName sn) {
108 return this.originServer.equals(sn) && isDone();
109 }
110
111 public boolean isDone() {
112 return this.state == ZooKeeperProtos.SplitLogTask.State.DONE;
113 }
114
115 public boolean isErr(final ServerName sn) {
116 return this.originServer.equals(sn) && isErr();
117 }
118
119 public boolean isErr() {
120 return this.state == ZooKeeperProtos.SplitLogTask.State.ERR;
121 }
122
123 @Override
124 public String toString() {
125 return this.state.toString() + " " + this.originServer.toString();
126 }
127
128 @Override
129 public boolean equals(Object obj) {
130 if (!(obj instanceof SplitLogTask)) return false;
131 SplitLogTask other = (SplitLogTask)obj;
132 return other.state.equals(this.state) && other.originServer.equals(this.originServer);
133 }
134
135 @Override
136 public int hashCode() {
137 int hash = 7;
138 hash = 31 * hash + this.state.hashCode();
139 return 31 * hash + this.originServer.hashCode();
140 }
141
142
143
144
145
146
147
148 public static SplitLogTask parseFrom(final byte [] data) throws DeserializationException {
149 ProtobufUtil.expectPBMagicPrefix(data);
150 try {
151 int prefixLen = ProtobufUtil.lengthOfPBMagic();
152 ZooKeeperProtos.SplitLogTask slt = ZooKeeperProtos.SplitLogTask.newBuilder().
153 mergeFrom(data, prefixLen, data.length - prefixLen).build();
154 return new SplitLogTask(slt);
155 } catch (InvalidProtocolBufferException e) {
156 throw new DeserializationException(Bytes.toStringBinary(data, 0, 64), e);
157 }
158 }
159
160
161
162
163
164 public byte [] toByteArray() {
165
166
167
168 HBaseProtos.ServerName snpb = ProtobufUtil.toServerName(this.originServer);
169 ZooKeeperProtos.SplitLogTask slts =
170 ZooKeeperProtos.SplitLogTask.newBuilder().setServerName(snpb).setState(this.state).build();
171 return ProtobufUtil.prependPBMagic(slts.toByteArray());
172 }
173 }