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.client; 019 020import java.io.IOException; 021import java.lang.management.ManagementFactory; 022import java.util.List; 023import org.apache.hadoop.hbase.util.Addressing; 024import org.apache.hadoop.hbase.util.Bytes; 025import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 026import org.apache.yetus.audience.InterfaceAudience; 027import org.slf4j.Logger; 028import org.slf4j.LoggerFactory; 029 030import org.apache.hbase.thirdparty.com.google.common.base.Splitter; 031import org.apache.hbase.thirdparty.com.google.common.collect.Iterators; 032 033/** 034 * The class that is able to determine some unique strings for the client, such as an IP address, 035 * PID, and composite deterministic ID. 036 */ 037@InterfaceAudience.Private 038final class ClientIdGenerator { 039 private static final Logger LOG = LoggerFactory.getLogger(ClientIdGenerator.class); 040 041 private ClientIdGenerator() { 042 } 043 044 /** 045 * Returns a unique ID incorporating IP address, PID, TID and timer. Might be an overkill... Note 046 * though that new UUID in java by default is just a random number. 047 */ 048 public static byte[] generateClientId() { 049 byte[] selfBytes = getIpAddressBytes(); 050 Long pid = getPid(); 051 long tid = Thread.currentThread().getId(); 052 long ts = EnvironmentEdgeManager.currentTime(); 053 054 byte[] id = new byte[selfBytes.length + ((pid != null ? 1 : 0) + 2) * Bytes.SIZEOF_LONG]; 055 int offset = Bytes.putBytes(id, 0, selfBytes, 0, selfBytes.length); 056 if (pid != null) { 057 offset = Bytes.putLong(id, offset, pid); 058 } 059 offset = Bytes.putLong(id, offset, tid); 060 offset = Bytes.putLong(id, offset, ts); 061 assert offset == id.length; 062 return id; 063 } 064 065 /** Returns PID of the current process, if it can be extracted from JVM name, or null. */ 066 public static Long getPid() { 067 String name = ManagementFactory.getRuntimeMXBean().getName(); 068 List<String> nameParts = Splitter.on('@').splitToList(name); 069 if (nameParts.size() == 2) { // 12345@somewhere 070 try { 071 return Long.parseLong(Iterators.get(nameParts.iterator(), 0)); 072 } catch (NumberFormatException ex) { 073 LOG.warn("Failed to get PID from [" + name + "]", ex); 074 } 075 } else { 076 LOG.warn("Don't know how to get PID from [" + name + "]"); 077 } 078 return null; 079 } 080 081 /** 082 * Returns Some IPv4/IPv6 address available on the current machine that is up, not virtual and not 083 * a loopback address. Empty array if none can be found or error occurred. 084 */ 085 public static byte[] getIpAddressBytes() { 086 try { 087 return Addressing.getIpAddress().getAddress(); 088 } catch (IOException ex) { 089 LOG.warn("Failed to get IP address bytes", ex); 090 } 091 return new byte[0]; 092 } 093}