001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019package org.apache.hadoop.hbase.regionserver; 020 021import org.apache.hbase.thirdparty.com.google.common.hash.Hashing; 022 023import java.util.concurrent.atomic.AtomicInteger; 024 025import org.apache.hadoop.hbase.ServerName; 026import org.apache.yetus.audience.InterfaceAudience; 027 028/** 029 * Generate a new style scanner id to prevent collision with previous started server or other RSs. 030 * We have 64 bits to use. 031 * The first 32 bits are MurmurHash32 of ServerName string "host,port,ts". 032 * The ServerName contains both host, port, and start timestamp so it can prevent collision. 033 * The lowest 32bit is generated by atomic int. 034 */ 035@InterfaceAudience.Private 036public class ScannerIdGenerator { 037 038 private final long serverNameHash; 039 private final AtomicInteger scannerIdGen = new AtomicInteger(0); 040 041 public ScannerIdGenerator(ServerName serverName) { 042 long hash = Hashing.murmur3_32().hashString(serverName.toString(), 043 java.nio.charset.StandardCharsets.UTF_8).asInt(); 044 this.serverNameHash = hash << 32; 045 } 046 047 public long generateNewScannerId() { 048 return (scannerIdGen.incrementAndGet() & 0x00000000FFFFFFFFL) | serverNameHash; 049 } 050 051 public static void main(final String [] args) { 052 ScannerIdGenerator sig = new ScannerIdGenerator(ServerName.valueOf("a.example.org,1234,5678")); 053 for (int i = 0; i < 10; i++) { 054 System.out.println(sig.generateNewScannerId()); 055 } 056 } 057}