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