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 */ 018 019package org.apache.hadoop.hbase; 020 021import java.io.IOException; 022import java.util.UUID; 023 024import org.apache.yetus.audience.InterfaceAudience; 025import org.apache.hadoop.hbase.exceptions.DeserializationException; 026import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; 027import org.apache.hadoop.hbase.shaded.protobuf.generated.ClusterIdProtos; 028import org.apache.hadoop.hbase.util.Bytes; 029 030/** 031 * The identifier for this cluster. 032 * It is serialized to the filesystem and up into zookeeper. This is a container for the id. 033 * Also knows how to serialize and deserialize the cluster id. 034 */ 035@InterfaceAudience.Private 036public class ClusterId { 037 private final String id; 038 039 /** 040 * New ClusterID. Generates a uniqueid. 041 */ 042 public ClusterId() { 043 this(UUID.randomUUID().toString()); 044 } 045 046 public ClusterId(final String uuid) { 047 this.id = uuid; 048 } 049 050 /** 051 * @return The clusterid serialized using pb w/ pb magic prefix 052 */ 053 public byte [] toByteArray() { 054 return ProtobufUtil.prependPBMagic(convert().toByteArray()); 055 } 056 057 /** 058 * @param bytes A pb serialized {@link ClusterId} instance with pb magic prefix 059 * @return An instance of {@link ClusterId} made from <code>bytes</code> 060 * @throws DeserializationException 061 * @see #toByteArray() 062 */ 063 public static ClusterId parseFrom(final byte [] bytes) throws DeserializationException { 064 if (ProtobufUtil.isPBMagicPrefix(bytes)) { 065 int pblen = ProtobufUtil.lengthOfPBMagic(); 066 ClusterIdProtos.ClusterId.Builder builder = ClusterIdProtos.ClusterId.newBuilder(); 067 ClusterIdProtos.ClusterId cid = null; 068 try { 069 ProtobufUtil.mergeFrom(builder, bytes, pblen, bytes.length - pblen); 070 cid = builder.build(); 071 } catch (IOException e) { 072 throw new DeserializationException(e); 073 } 074 return convert(cid); 075 } else { 076 // Presume it was written out this way, the old way. 077 return new ClusterId(Bytes.toString(bytes)); 078 } 079 } 080 081 /** 082 * @return A pb instance to represent this instance. 083 */ 084 public ClusterIdProtos.ClusterId convert() { 085 ClusterIdProtos.ClusterId.Builder builder = ClusterIdProtos.ClusterId.newBuilder(); 086 return builder.setClusterId(this.id).build(); 087 } 088 089 /** 090 * @param cid 091 * @return A {@link ClusterId} made from the passed in <code>cid</code> 092 */ 093 public static ClusterId convert(final ClusterIdProtos.ClusterId cid) { 094 return new ClusterId(cid.getClusterId()); 095 } 096 097 /** 098 * @see java.lang.Object#toString() 099 */ 100 @Override 101 public String toString() { 102 return this.id; 103 } 104}