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 implements ClusterIdFile {
035  private final String id;
036
037  public static class Parser implements ClusterIdFileParser<ClusterId> {
038
039    @Override
040    public String getFileName() {
041      return HConstants.CLUSTER_ID_FILE_NAME;
042    }
043
044    /**
045     * Parse the serialized representation of the {@link ClusterId}
046     * @param bytes A pb serialized {@link ClusterId} instance with pb magic prefix
047     * @return An instance of {@link ClusterId} made from <code>bytes</code>
048     * @see #toByteArray()
049     */
050    @Override
051    public ClusterId parseFrom(byte[] bytes) throws DeserializationException {
052      if (ProtobufUtil.isPBMagicPrefix(bytes)) {
053        int pblen = ProtobufUtil.lengthOfPBMagic();
054        ClusterIdProtos.ClusterId.Builder builder = ClusterIdProtos.ClusterId.newBuilder();
055        ClusterIdProtos.ClusterId cid = null;
056        try {
057          ProtobufUtil.mergeFrom(builder, bytes, pblen, bytes.length - pblen);
058          cid = builder.build();
059        } catch (IOException e) {
060          throw new DeserializationException(e);
061        }
062        return convert(cid);
063      } else {
064        // Presume it was written out this way, the old way.
065        return new ClusterId(Bytes.toString(bytes));
066      }
067    }
068
069    @Override
070    public ClusterId readString(String input) {
071      return new ClusterId(input);
072    }
073  }
074
075  /**
076   * New ClusterID. Generates a uniqueid.
077   */
078  public ClusterId() {
079    this(UUID.randomUUID().toString());
080  }
081
082  public ClusterId(final String uuid) {
083    this.id = uuid;
084  }
085
086  /** Returns The clusterid serialized using pb w/ pb magic prefix */
087  public byte[] toByteArray() {
088    return ProtobufUtil.prependPBMagic(convert().toByteArray());
089  }
090
091  /** Returns A pb instance to represent this instance. */
092  public ClusterIdProtos.ClusterId convert() {
093    ClusterIdProtos.ClusterId.Builder builder = ClusterIdProtos.ClusterId.newBuilder();
094    return builder.setClusterId(this.id).build();
095  }
096
097  /** Returns A {@link ClusterId} made from the passed in <code>cid</code> */
098  public static ClusterId convert(final ClusterIdProtos.ClusterId cid) {
099    return new ClusterId(cid.getClusterId());
100  }
101
102  /**
103   * @see java.lang.Object#toString()
104   */
105  @Override
106  public String toString() {
107    return this.id;
108  }
109}