001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase;
019
020import java.io.IOException;
021import java.util.UUID;
022import org.apache.hadoop.hbase.exceptions.DeserializationException;
023import org.apache.hadoop.hbase.util.Bytes;
024import org.apache.yetus.audience.InterfaceAudience;
025
026import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
027import org.apache.hadoop.hbase.shaded.protobuf.generated.ClusterIdProtos;
028
029/**
030 * The identifier for this cluster. It is serialized to the filesystem and up into zookeeper. This
031 * is a container for the id. Also knows how to serialize and deserialize the cluster id.
032 */
033@InterfaceAudience.Private
034public class ClusterId {
035  private final String id;
036
037  /**
038   * New ClusterID. Generates a uniqueid.
039   */
040  public ClusterId() {
041    this(UUID.randomUUID().toString());
042  }
043
044  public ClusterId(final String uuid) {
045    this.id = uuid;
046  }
047
048  /** Returns The clusterid serialized using pb w/ pb magic prefix */
049  public byte[] toByteArray() {
050    return ProtobufUtil.prependPBMagic(convert().toByteArray());
051  }
052
053  /**
054   * Parse the serialized representation of the {@link ClusterId}
055   * @param bytes A pb serialized {@link ClusterId} instance with pb magic prefix
056   * @return An instance of {@link ClusterId} made from <code>bytes</code> n * @see #toByteArray()
057   */
058  public static ClusterId parseFrom(final byte[] bytes) throws DeserializationException {
059    if (ProtobufUtil.isPBMagicPrefix(bytes)) {
060      int pblen = ProtobufUtil.lengthOfPBMagic();
061      ClusterIdProtos.ClusterId.Builder builder = ClusterIdProtos.ClusterId.newBuilder();
062      ClusterIdProtos.ClusterId cid = null;
063      try {
064        ProtobufUtil.mergeFrom(builder, bytes, pblen, bytes.length - pblen);
065        cid = builder.build();
066      } catch (IOException e) {
067        throw new DeserializationException(e);
068      }
069      return convert(cid);
070    } else {
071      // Presume it was written out this way, the old way.
072      return new ClusterId(Bytes.toString(bytes));
073    }
074  }
075
076  /** Returns A pb instance to represent this instance. */
077  public ClusterIdProtos.ClusterId convert() {
078    ClusterIdProtos.ClusterId.Builder builder = ClusterIdProtos.ClusterId.newBuilder();
079    return builder.setClusterId(this.id).build();
080  }
081
082  /**
083   * n * @return A {@link ClusterId} made from the passed in <code>cid</code>
084   */
085  public static ClusterId convert(final ClusterIdProtos.ClusterId cid) {
086    return new ClusterId(cid.getClusterId());
087  }
088
089  /**
090   * @see java.lang.Object#toString()
091   */
092  @Override
093  public String toString() {
094    return this.id;
095  }
096}