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 java.io.IOException;
21
22 import org.apache.hadoop.hbase.util.ByteStringer;
23
24 import org.apache.hadoop.hbase.classification.InterfaceAudience;
25 import org.apache.hadoop.hbase.exceptions.DeserializationException;
26 import org.apache.hadoop.hbase.executor.EventType;
27 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
28 import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
29 import org.apache.hadoop.hbase.util.Bytes;
30
31
32
33
34
35
36
37
38 @InterfaceAudience.Private
39 public class RegionTransition {
40 private final ZooKeeperProtos.RegionTransition rt;
41
42
43
44
45 private RegionTransition() {
46 this(null);
47 }
48
49 private RegionTransition(final ZooKeeperProtos.RegionTransition rt) {
50 this.rt = rt;
51 }
52
53 public EventType getEventType() {
54 return EventType.get(this.rt.getEventTypeCode());
55 }
56
57 public ServerName getServerName() {
58 return ProtobufUtil.toServerName(this.rt.getServerName());
59 }
60
61 public long getCreateTime() {
62 return this.rt.getCreateTime();
63 }
64
65
66
67
68 public byte [] getRegionName() {
69 return this.rt.getRegionName().toByteArray();
70 }
71
72 public byte [] getPayload() {
73 return this.rt.getPayload().toByteArray();
74 }
75
76 @Override
77 public String toString() {
78 byte [] payload = getPayload();
79 return "region=" + Bytes.toStringBinary(getRegionName()) + ", state=" + getEventType() +
80 ", servername=" + getServerName() + ", createTime=" + this.getCreateTime() +
81 ", payload.length=" + (payload == null? 0: payload.length);
82 }
83
84
85
86
87
88
89
90 public static RegionTransition createRegionTransition(final EventType type,
91 final byte [] regionName, final ServerName sn) {
92 return createRegionTransition(type, regionName, sn, null);
93 }
94
95
96
97
98
99
100
101
102 public static RegionTransition createRegionTransition(final EventType type,
103 final byte [] regionName, final ServerName sn, final byte [] payload) {
104 org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.ServerName pbsn =
105 org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.ServerName.newBuilder().
106 setHostName(sn.getHostname()).setPort(sn.getPort()).setStartCode(sn.getStartcode()).build();
107 ZooKeeperProtos.RegionTransition.Builder builder = ZooKeeperProtos.RegionTransition.newBuilder().
108 setEventTypeCode(type.getCode()).setRegionName(ByteStringer.wrap(regionName)).
109 setServerName(pbsn);
110 builder.setCreateTime(System.currentTimeMillis());
111 if (payload != null) builder.setPayload(ByteStringer.wrap(payload));
112 return new RegionTransition(builder.build());
113 }
114
115
116
117
118
119
120
121 public static RegionTransition parseFrom(final byte [] data) throws DeserializationException {
122 ProtobufUtil.expectPBMagicPrefix(data);
123 try {
124 int prefixLen = ProtobufUtil.lengthOfPBMagic();
125 ZooKeeperProtos.RegionTransition.Builder builder =
126 ZooKeeperProtos.RegionTransition.newBuilder();
127 ProtobufUtil.mergeFrom(builder, data, prefixLen, data.length - prefixLen);
128 return new RegionTransition(builder.build());
129 } catch (IOException e) {
130 throw new DeserializationException(e);
131 }
132 }
133
134
135
136
137
138 public byte [] toByteArray() {
139 return ProtobufUtil.prependPBMagic(this.rt.toByteArray());
140 }
141 }