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