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 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   * Current state of a region in transition.  Holds state of a region as it moves through the
33   * steps that take it from offline to open, etc.  Used by regionserver, master, and zk packages.
34   * Encapsulates protobuf serialization/deserialization so we don't leak generated pb outside this
35   * class.  Create an instance using createRegionTransition(EventType, byte[], ServerName).
36   * <p>Immutable
37   */
38  @InterfaceAudience.Private
39  public class RegionTransition {
40    private final ZooKeeperProtos.RegionTransition rt;
41  
42    /**
43     * Shutdown constructor
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     * @return Full region name
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     * @param type
86     * @param regionName
87     * @param sn
88     * @return a serialized pb {@link RegionTransition}
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     * @param type
97     * @param regionName
98     * @param sn
99     * @param payload May be null
100    * @return a serialized pb {@link RegionTransition}
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    * @param data Serialized date to parse.
117    * @return A RegionTransition instance made of the passed <code>data</code>
118    * @throws DeserializationException 
119    * @see #toByteArray()
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    * @return This instance serialized into a byte array
136    * @see #parseFrom(byte[])
137    */
138   public byte [] toByteArray() {
139     return ProtobufUtil.prependPBMagic(this.rt.toByteArray());
140   }
141 }