Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 15 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
196 changes: 104 additions & 92 deletions common/src/main/java/io/seata/common/util/IdWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,161 +14,173 @@
* 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
*/
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;
Copy link
Contributor

@l81893521 l81893521 Dec 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if currentWithSequence will never be negative number, I think use

>>

is enought.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currentWithSequence could be a negative number(though it would be a long long time to reach this~). What ever, >>> and >> have the same speed for cpu.

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() {
l81893521 marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
46 changes: 46 additions & 0 deletions common/src/test/java/io/seata/common/util/IdWorkerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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");
}
}
4 changes: 0 additions & 4 deletions server/src/main/java/io/seata/server/ParameterParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
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;
Expand Down Expand Up @@ -77,9 +76,6 @@ private void init(String[] args) {
System.exit(0);
}
}
if (this.serverNode == null) {
this.serverNode = IdWorker.initWorkerId();
}
if (StringUtils.isNotBlank(seataEnv)) {
System.setProperty(ENV_PROPERTY_KEY, seataEnv);
}
Expand Down
24 changes: 15 additions & 9 deletions server/src/main/java/io/seata/server/UUIDGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,28 @@
*/
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);
}

}