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> 057 * @see #toByteArray() 058 */ 059 public static ClusterId parseFrom(final byte[] bytes) throws DeserializationException { 060 if (ProtobufUtil.isPBMagicPrefix(bytes)) { 061 int pblen = ProtobufUtil.lengthOfPBMagic(); 062 ClusterIdProtos.ClusterId.Builder builder = ClusterIdProtos.ClusterId.newBuilder(); 063 ClusterIdProtos.ClusterId cid = null; 064 try { 065 ProtobufUtil.mergeFrom(builder, bytes, pblen, bytes.length - pblen); 066 cid = builder.build(); 067 } catch (IOException e) { 068 throw new DeserializationException(e); 069 } 070 return convert(cid); 071 } else { 072 // Presume it was written out this way, the old way. 073 return new ClusterId(Bytes.toString(bytes)); 074 } 075 } 076 077 /** Returns A pb instance to represent this instance. */ 078 public ClusterIdProtos.ClusterId convert() { 079 ClusterIdProtos.ClusterId.Builder builder = ClusterIdProtos.ClusterId.newBuilder(); 080 return builder.setClusterId(this.id).build(); 081 } 082 083 /** Returns A {@link ClusterId} made from the passed in <code>cid</code> */ 084 public static ClusterId convert(final ClusterIdProtos.ClusterId cid) { 085 return new ClusterId(cid.getClusterId()); 086 } 087 088 /** 089 * @see java.lang.Object#toString() 090 */ 091 @Override 092 public String toString() { 093 return this.id; 094 } 095}