View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase;
19  
20  import com.google.protobuf.HBaseZeroCopyByteString;
21  import com.google.protobuf.InvalidProtocolBufferException;
22  
23  import org.apache.hadoop.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.exceptions.DeserializationException;
25  import org.apache.hadoop.hbase.executor.EventType;
26  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
27  import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
28  import org.apache.hadoop.hbase.util.Bytes;
29  
30  /**
31   * Current state of a region in transition.  Holds state of a region as it moves through the
32   * steps that take it from offline to open, etc.  Used by regionserver, master, and zk packages.
33   * Encapsulates protobuf serialization/deserialization so we don't leak generated pb outside this
34   * class.  Create an instance using createRegionTransition(EventType, byte[], ServerName).
35   * <p>Immutable
36   */
37  @InterfaceAudience.Private
38  public class RegionTransition {
39    private final ZooKeeperProtos.RegionTransition rt;
40  
41    /**
42     * Shutdown constructor
43     */
44    private RegionTransition() {
45      this(null);
46    }
47  
48    private RegionTransition(final ZooKeeperProtos.RegionTransition rt) {
49      this.rt = rt;
50    }
51  
52    public EventType getEventType() {
53      return EventType.get(this.rt.getEventTypeCode());
54    }
55  
56    public ServerName getServerName() {
57      return ProtobufUtil.toServerName(this.rt.getServerName());
58    }
59  
60    public long getCreateTime() {
61      return this.rt.getCreateTime();
62    }
63  
64    /**
65     * @return Full region name
66     */
67    public byte [] getRegionName() {
68      return this.rt.getRegionName().toByteArray();
69    }
70  
71    public byte [] getPayload() {
72      return this.rt.getPayload().toByteArray();
73    }
74  
75    @Override
76    public String toString() {
77      byte [] payload = getPayload();
78      return "region=" + Bytes.toStringBinary(getRegionName()) + ", state=" + getEventType() +
79        ", servername=" + getServerName() + ", createTime=" + this.getCreateTime() +
80        ", payload.length=" + (payload == null? 0: payload.length);
81    }
82  
83    /**
84     * @param type
85     * @param regionName
86     * @param sn
87     * @return a serialized pb {@link RegionTransition}
88     */
89    public static RegionTransition createRegionTransition(final EventType type,
90        final byte [] regionName, final ServerName sn) {
91      return createRegionTransition(type, regionName, sn, null);
92    }
93  
94    /**
95     * @param type
96     * @param regionName
97     * @param sn
98     * @param payload May be null
99     * @return a serialized pb {@link RegionTransition}
100    */
101   public static RegionTransition createRegionTransition(final EventType type,
102       final byte [] regionName, final ServerName sn, final byte [] payload) {
103     org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.ServerName pbsn =
104       org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.ServerName.newBuilder().
105         setHostName(sn.getHostname()).setPort(sn.getPort()).setStartCode(sn.getStartcode()).build();
106     ZooKeeperProtos.RegionTransition.Builder builder = ZooKeeperProtos.RegionTransition.newBuilder().
107       setEventTypeCode(type.getCode()).setRegionName(HBaseZeroCopyByteString.wrap(regionName)).
108         setServerName(pbsn);
109     builder.setCreateTime(System.currentTimeMillis());
110     if (payload != null) builder.setPayload(HBaseZeroCopyByteString.wrap(payload));
111     return new RegionTransition(builder.build());
112   }
113 
114   /**
115    * @param data Serialized date to parse.
116    * @return A RegionTransition instance made of the passed <code>data</code>
117    * @throws DeserializationException 
118    * @see #toByteArray()
119    */
120   public static RegionTransition parseFrom(final byte [] data) throws DeserializationException {
121     ProtobufUtil.expectPBMagicPrefix(data);
122     try {
123       int prefixLen = ProtobufUtil.lengthOfPBMagic();
124       ZooKeeperProtos.RegionTransition rt = ZooKeeperProtos.RegionTransition.newBuilder().
125         mergeFrom(data, prefixLen, data.length - prefixLen).build();
126       return new RegionTransition(rt);
127     } catch (InvalidProtocolBufferException e) {
128       throw new DeserializationException(e);
129     }
130   }
131 
132   /**
133    * @return This instance serialized into a byte array
134    * @see #parseFrom(byte[])
135    */
136   public byte [] toByteArray() {
137     return ProtobufUtil.prependPBMagic(this.rt.toByteArray());
138   }
139 }