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  
19  package org.apache.hadoop.hbase;
20  
21  import java.util.UUID;
22  
23  import org.apache.hadoop.hbase.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.exceptions.DeserializationException;
25  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
26  import org.apache.hadoop.hbase.protobuf.generated.ClusterIdProtos;
27  import org.apache.hadoop.hbase.util.Bytes;
28  
29  import com.google.protobuf.InvalidProtocolBufferException;
30  
31  /**
32   * The identifier for this cluster.
33   * It is serialized to the filesystem and up into zookeeper.  This is a container for the id.
34   * Also knows how to serialize and deserialize the cluster id.
35   */
36  @InterfaceAudience.Private
37  public class ClusterId {
38    private final String id;
39  
40    /**
41     * New ClusterID.  Generates a uniqueid.
42     */
43    public ClusterId() {
44      this(UUID.randomUUID().toString());
45    }
46  
47    public ClusterId(final String uuid) {
48      this.id = uuid;
49    }
50  
51    /**
52     * @return The clusterid serialized using pb w/ pb magic prefix
53     */
54    public byte [] toByteArray() {
55      return ProtobufUtil.prependPBMagic(convert().toByteArray());
56    }
57  
58    /**
59     * @param bytes A pb serialized {@link ClusterId} instance with pb magic prefix
60     * @return An instance of {@link ClusterId} made from <code>bytes</code>
61     * @throws DeserializationException
62     * @see #toByteArray()
63     */
64    public static ClusterId parseFrom(final byte [] bytes) throws DeserializationException {
65      if (ProtobufUtil.isPBMagicPrefix(bytes)) {
66        int pblen = ProtobufUtil.lengthOfPBMagic();
67        ClusterIdProtos.ClusterId.Builder builder = ClusterIdProtos.ClusterId.newBuilder();
68        ClusterIdProtos.ClusterId cid = null;
69        try {
70          cid = builder.mergeFrom(bytes, pblen, bytes.length - pblen).build();
71        } catch (InvalidProtocolBufferException e) {
72          throw new DeserializationException(e);
73        }
74        return convert(cid);
75      } else {
76        // Presume it was written out this way, the old way.
77        return new ClusterId(Bytes.toString(bytes));
78      }
79    }
80  
81    /**
82     * @return A pb instance to represent this instance.
83     */
84    ClusterIdProtos.ClusterId convert() {
85      ClusterIdProtos.ClusterId.Builder builder = ClusterIdProtos.ClusterId.newBuilder();
86      return builder.setClusterId(this.id).build();
87    }
88  
89    /**
90     * @param cid
91     * @return A {@link ClusterId} made from the passed in <code>cid</code>
92     */
93    static ClusterId convert(final ClusterIdProtos.ClusterId cid) {
94      return new ClusterId(cid.getClusterId());
95    }
96  
97    /**
98     * @see java.lang.Object#toString()
99     */
100   @Override
101   public String toString() {
102     return this.id;
103   }
104 }