Skip to content
Snippets Groups Projects
Unverified Commit 0944fe57 authored by selfishlover's avatar selfishlover Committed by GitHub
Browse files

optimize: improve UUIDGenerator using "history time" version of snowflake algorithm (#3175)

parent f6193e30
No related branches found
No related tags found
No related merge requests found
......@@ -14,161 +14,174 @@
* limitations under the License.
*/
package io.seata.common.util;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
/**
* @author funkye
* @author selfishlover
*/
public class IdWorker {
private volatile static IdWorker idWorker = null;
/**
* Start time cut (2020-05-03)
*/
private final long twepoch = 1588435200000L;
/**
* The number of bits occupied by the machine id
* The number of bits occupied by workerId
*/
private final long workerIdBits = 10L;
private final int workerIdBits = 10;
/**
* Maximum supported machine id, the result is 1023 (this shift algorithm can quickly calculate the largest decimal
* number that can be represented by a few binary numbers)
* The number of bits occupied by timestamp
*/
private final long maxWorkerId = -1L ^ (-1L << workerIdBits);
private final int timestampBits = 41;
/**
* The number of bits the sequence occupies in id
* The number of bits occupied by sequence
*/
private final long sequenceBits = 12L;
private final int sequenceBits = 12;
/**
* Machine ID left 12 digits
* Maximum supported machine id, the result is 1023
*/
private final long workerIdShift = sequenceBits;
private final int maxWorkerId = ~(-1 << workerIdBits);
/**
* Time truncated to the left by 22 bits (10 + 12)
* business meaning: machine ID (0 ~ 1023)
* actual layout in memory:
* highest 1 bit: 0
* middle 10 bit: workerId
* lowest 53 bit: all 0
*/
private final long timestampLeftShift = sequenceBits + workerIdBits;
private long workerId;
/**
* Generate sequence mask
* timestamp and sequence mix in one Long
* highest 11 bit: not used
* middle 41 bit: timestamp
* lowest 12 bit: sequence
*/
private final long sequenceMask = -1L ^ (-1L << sequenceBits);
private AtomicLong timestampAndSequence;
/**
* Machine ID (0 ~ 1023)
* mask that help to extract timestamp and sequence from a long
*/
private long workerId;
private final long timestampAndSequenceMask = ~(-1L << (timestampBits + sequenceBits));
/**
* Sequence in milliseconds (0 ~ 4095)
* instantiate an IdWorker using given workerId
* @param workerId if null, then will auto assign one
*/
private long sequence = 0L;
public IdWorker(Long workerId) {
initTimestampAndSequence();
initWorkerId(workerId);
}
/**
* Time of last ID generation
* init first timestamp and sequence immediately
*/
private long lastTimestamp = -1L;
private void initTimestampAndSequence() {
long timestamp = getNewestTimestamp();
long timestampWithSequence = timestamp << sequenceBits;
this.timestampAndSequence = new AtomicLong(timestampWithSequence);
}
/**
* Constructor
*
* @param workerId
* Job ID (0 ~ 1023)
* init workerId
* @param workerId if null, then auto generate one
*/
public IdWorker(long workerId) {
private void initWorkerId(Long workerId) {
if (workerId == null) {
workerId = generateWorkerId();
}
if (workerId > maxWorkerId || workerId < 0) {
throw new IllegalArgumentException(
String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
String message = String.format("worker Id can't be greater than %d or less than 0", maxWorkerId);
throw new IllegalArgumentException(message);
}
this.workerId = workerId;
this.workerId = workerId << (timestampBits + sequenceBits);
}
/**
* Get the next ID (the method is thread-safe)
*
* @return SnowflakeId
* get next UUID(base on snowflake algorithm), which look like:
* highest 1 bit: always 0
* next 10 bit: workerId
* next 41 bit: timestamp
* lowest 12 bit: sequence
* @return UUID
*/
public synchronized long nextId() {
long timestamp = timeGen();
if (timestamp < lastTimestamp) {
throw new RuntimeException(String.format(
"clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
}
if (lastTimestamp == timestamp) {
sequence = (sequence + 1) & sequenceMask;
if (sequence == 0) {
timestamp = tilNextMillis(lastTimestamp);
}
} else {
sequence = 0L;
}
lastTimestamp = timestamp;
return ((timestamp - twepoch) << timestampLeftShift) | (workerId << workerIdShift) | sequence;
public long nextId() {
waitIfNecessary();
long next = timestampAndSequence.incrementAndGet();
long timestampWithSequence = next & timestampAndSequenceMask;
return workerId | timestampWithSequence;
}
/**
* Block until the next millisecond until a new timestamp is obtained
*
* @param lastTimestamp
* Time of last ID generation
* @return Current timestamp
* block current thread if the QPS of acquiring UUID is too high
* that current sequence space is exhausted
*/
protected long tilNextMillis(long lastTimestamp) {
long timestamp = timeGen();
while (timestamp <= lastTimestamp) {
timestamp = timeGen();
private void waitIfNecessary() {
long currentWithSequence = timestampAndSequence.get();
long current = currentWithSequence >>> sequenceBits;
long newest = getNewestTimestamp();
if (current >= newest) {
try {
Thread.sleep(5);
} catch (InterruptedException ignore) {
// don't care
}
}
return timestamp;
}
/**
* Returns the current time in milliseconds
*
* @return Current time (ms)
* get newest timestamp relative to twepoch
*/
protected long timeGen() {
return System.currentTimeMillis();
}
public static IdWorker getInstance() {
if (idWorker == null) {
synchronized (IdWorker.class) {
if (idWorker == null) {
init(initWorkerId());
}
}
}
return idWorker;
private long getNewestTimestamp() {
return System.currentTimeMillis() - twepoch;
}
public static long initWorkerId() {
InetAddress address;
/**
* auto generate workerId, try using mac first, if failed, then randomly generate one
* @return workerId
*/
private long generateWorkerId() {
try {
address = InetAddress.getLocalHost();
} catch (final UnknownHostException e) {
throw new IllegalStateException("Cannot get LocalHost InetAddress, please check your network!",e);
return generateWorkerIdBaseOnMac();
} catch (Exception e) {
return generateRandomWorkerId();
}
byte[] ipAddressByteArray = address.getAddress();
return ((ipAddressByteArray[ipAddressByteArray.length - 2] & 0B11) << Byte.SIZE) + (ipAddressByteArray[ipAddressByteArray.length - 1] & 0xFF);
}
public static void init(Long serverNodeId) {
if (idWorker == null) {
synchronized (IdWorker.class) {
if (idWorker == null) {
idWorker = new IdWorker(serverNodeId);
}
/**
* use lowest 10 bit of available MAC as workerId
* @return workerId
* @throws Exception when there is no available mac found
*/
private long generateWorkerIdBaseOnMac() throws Exception {
Enumeration<NetworkInterface> all = NetworkInterface.getNetworkInterfaces();
while (all.hasMoreElements()) {
NetworkInterface networkInterface = all.nextElement();
boolean isLoopback = networkInterface.isLoopback();
boolean isVirtual = networkInterface.isVirtual();
if (isLoopback || isVirtual) {
continue;
}
byte[] mac = networkInterface.getHardwareAddress();
return ((mac[4] & 0B11) << 8) | (mac[5] & 0xFF);
}
throw new RuntimeException("no available mac found");
}
/**
* randomly generate one as workerId
* @return workerId
*/
private long generateRandomWorkerId() {
return new Random().nextInt(maxWorkerId + 1);
}
}
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.seata.common.util;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertEquals;
class IdWorkerTest {
@Test
void testNegativeWorkerId() {
assertThrows(IllegalArgumentException.class, () -> {
new IdWorker(-1L);
}, "should throw IllegalArgumentException when workerId is negative");
}
@Test
void testTooLargeWorkerId() {
assertThrows(IllegalArgumentException.class, () -> {
new IdWorker(1024L);
}, "should throw IllegalArgumentException when workerId is bigger than 1023");
}
@Test
void testNextId() {
IdWorker worker = new IdWorker(null);
long id1 = worker.nextId();
long id2 = worker.nextId();
assertEquals(1L, id2 - id1, "increment step should be 1");
}
}
\ No newline at end of file
......@@ -18,7 +18,6 @@ package io.seata.server;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import io.seata.common.util.IdWorker;
import io.seata.common.util.StringUtils;
import io.seata.config.ConfigurationFactory;
import io.seata.core.constants.ConfigurationKeys;
......@@ -78,9 +77,6 @@ public class ParameterParser {
System.exit(0);
}
}
if (this.serverNode == null) {
this.serverNode = IdWorker.initWorkerId();
}
if (StringUtils.isNotBlank(seataEnv)) {
System.setProperty(ENV_PROPERTY_KEY, seataEnv);
}
......
......@@ -24,22 +24,28 @@ import io.seata.common.util.IdWorker;
*/
public class UUIDGenerator {
private static volatile IdWorker idWorker;
/**
* Generate uuid long.
*
* @return the long
* generate UUID using snowflake algorithm
* @return UUID
*/
public static long generateUUID() {
return IdWorker.getInstance().nextId();
if (idWorker == null) {
synchronized (UUIDGenerator.class) {
if (idWorker == null) {
init(null);
}
}
}
return idWorker.nextId();
}
/**
* Init.
*
* @param serverNode the server node id
* init IdWorker
* @param serverNode the server node id, consider as machine id in snowflake
*/
public static void init(Long serverNode) {
IdWorker.init(serverNode);
idWorker = new IdWorker(serverNode);
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment