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.ByteString;
21  import com.google.protobuf.InvalidProtocolBufferException;
22  import org.apache.hadoop.classification.InterfaceAudience;
23  import org.apache.hadoop.hbase.exceptions.DeserializationException;
24  import org.apache.hadoop.hbase.executor.EventType;
25  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
26  import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
27  import org.apache.hadoop.hbase.util.Bytes;
28  
29  /**
30   * Current state of a region in transition.  Holds state of a region as it moves through the
31   * steps that take it from offline to open, etc.  Used by regionserver, master, and zk packages.
32   * Encapsulates protobuf serialization/deserialization so we don't leak generated pb outside this
33   * class.  Create an instance using createRegionTransition(EventType, byte[], ServerName).
34   * <p>Immutable
35   */
36  @InterfaceAudience.Private
37  public class RegionTransition {
38    private final ZooKeeperProtos.RegionTransition rt;
39  
40    /**
41     * Shutdown constructor
42     */
43    private RegionTransition() {
44      this(null);
45    }
46  
47    private RegionTransition(final ZooKeeperProtos.RegionTransition rt) {
48      this.rt = rt;
49    }
50  
51    public EventType getEventType() {
52      return EventType.get(this.rt.getEventTypeCode());
53    }
54  
55    public ServerName getServerName() {
56      return ProtobufUtil.toServerName(this.rt.getServerName());
57    }
58  
59    public long getCreateTime() {
60      return this.rt.getCreateTime();
61    }
62  
63    /**
64     * @return Full region name
65     */
66    public byte [] getRegionName() {
67      return this.rt.getRegionName().toByteArray();
68    }
69  
70    public byte [] getPayload() {
71      return this.rt.getPayload().toByteArray();
72    }
73  
74    @Override
75    public String toString() {
76      byte [] payload = getPayload();
77      return "region=" + Bytes.toStringBinary(getRegionName()) + ", state=" + getEventType() +
78        ", servername=" + getServerName() + ", createTime=" + this.getCreateTime() +
79        ", payload.length=" + (payload == null? 0: payload.length);
80    }
81  
82    /**
83     * @param type
84     * @param regionName
85     * @param sn
86     * @return a serialized pb {@link RegionTransition}
87     */
88    public static RegionTransition createRegionTransition(final EventType type,
89        final byte [] regionName, final ServerName sn) {
90      return createRegionTransition(type, regionName, sn, null);
91    }
92  
93    /**
94     * @param type
95     * @param regionName
96     * @param sn
97     * @param payload May be null
98     * @return a serialized pb {@link RegionTransition}
99     */
100   public static RegionTransition createRegionTransition(final EventType type,
101       final byte [] regionName, final ServerName sn, final byte [] payload) {
102     org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.ServerName pbsn =
103       org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.ServerName.newBuilder().
104         setHostName(sn.getHostname()).setPort(sn.getPort()).setStartCode(sn.getStartcode()).build();
105     ZooKeeperProtos.RegionTransition.Builder builder = ZooKeeperProtos.RegionTransition.newBuilder().
106       setEventTypeCode(type.getCode()).setRegionName(ByteString.copyFrom(regionName)).
107         setServerName(pbsn);
108     builder.setCreateTime(System.currentTimeMillis());
109     if (payload != null) builder.setPayload(ByteString.copyFrom(payload));
110     return new RegionTransition(builder.build());
111   }
112 
113   /**
114    * @param data Serialized date to parse.
115    * @return A RegionTransition instance made of the passed <code>data</code>
116    * @throws DeserializationException 
117    * @see #toByteArray()
118    */
119   public static RegionTransition parseFrom(final byte [] data) throws DeserializationException {
120     ProtobufUtil.expectPBMagicPrefix(data);
121     try {
122       int prefixLen = ProtobufUtil.lengthOfPBMagic();
123       ZooKeeperProtos.RegionTransition rt = ZooKeeperProtos.RegionTransition.newBuilder().
124         mergeFrom(data, prefixLen, data.length - prefixLen).build();
125       return new RegionTransition(rt);
126     } catch (InvalidProtocolBufferException e) {
127       throw new DeserializationException(e);
128     }
129   }
130 
131   /**
132    * @return This instance serialized into a byte array
133    * @see #parseFrom(byte[])
134    */
135   public byte [] toByteArray() {
136     return ProtobufUtil.prependPBMagic(this.rt.toByteArray());
137   }
138 }