Browse Source
* rewrite code generate,fix bit shift * fix ut * add algorithm from licenses file * add algorithm from licenses file * add algorithm from licenses file * add algorithm from licenses file * add algorithm from licenses file * fix ut3.0.0/version-upgrade
JinYong Li
3 years ago
committed by
GitHub
17 changed files with 142 additions and 139 deletions
@ -0,0 +1,74 @@
|
||||
/** Copyright 2010-2012 Twitter, Inc.*/ |
||||
|
||||
package org.apache.dolphinscheduler.common.utils; |
||||
|
||||
import java.net.InetAddress; |
||||
import java.net.UnknownHostException; |
||||
import java.util.Objects; |
||||
|
||||
/** |
||||
* Rewriting based on Twitter snowflake algorithm |
||||
*/ |
||||
public class CodeGenerateUtils { |
||||
// start timestamp
|
||||
private static final long START_TIMESTAMP = 1609430400000L; //2021-01-01 00:00:00
|
||||
// Each machine generates 32 in the same millisecond
|
||||
private static final long LOW_DIGIT_BIT = 5L; |
||||
private static final long MIDDLE_BIT = 2L; |
||||
private static final long MAX_LOW_DIGIT = ~(-1L << LOW_DIGIT_BIT); |
||||
// The displacement to the left
|
||||
private static final long MIDDLE_LEFT = LOW_DIGIT_BIT; |
||||
private static final long HIGH_DIGIT_LEFT = LOW_DIGIT_BIT + MIDDLE_BIT; |
||||
private final long machineHash; |
||||
private long lowDigit = 0L; |
||||
private long recordMillisecond = -1L; |
||||
|
||||
private static final long SYSTEM_TIMESTAMP = System.currentTimeMillis(); |
||||
private static final long SYSTEM_NANOTIME = System.nanoTime(); |
||||
|
||||
private CodeGenerateUtils() throws CodeGenerateException { |
||||
try { |
||||
this.machineHash = Math.abs(Objects.hash(InetAddress.getLocalHost().getHostName())) % (2 << (MIDDLE_BIT - 1)); |
||||
} catch (UnknownHostException e) { |
||||
throw new CodeGenerateException(e.getMessage()); |
||||
} |
||||
} |
||||
|
||||
private static CodeGenerateUtils instance = null; |
||||
|
||||
public static synchronized CodeGenerateUtils getInstance() throws CodeGenerateException { |
||||
if (instance == null) { |
||||
instance = new CodeGenerateUtils(); |
||||
} |
||||
return instance; |
||||
} |
||||
|
||||
public synchronized long genCode() throws CodeGenerateException { |
||||
long nowtMillisecond = systemMillisecond(); |
||||
if (nowtMillisecond < recordMillisecond) { |
||||
throw new CodeGenerateException("New code exception because time is set back."); |
||||
} |
||||
if (nowtMillisecond == recordMillisecond) { |
||||
lowDigit = (lowDigit + 1) & MAX_LOW_DIGIT; |
||||
if (lowDigit == 0L) { |
||||
while (nowtMillisecond <= recordMillisecond) { |
||||
nowtMillisecond = systemMillisecond(); |
||||
} |
||||
} |
||||
} else { |
||||
lowDigit = 0L; |
||||
} |
||||
recordMillisecond = nowtMillisecond; |
||||
return (nowtMillisecond - START_TIMESTAMP) << HIGH_DIGIT_LEFT | machineHash << MIDDLE_LEFT | lowDigit; |
||||
} |
||||
|
||||
private long systemMillisecond() { |
||||
return SYSTEM_TIMESTAMP + (System.nanoTime() - SYSTEM_NANOTIME) / 1000000; |
||||
} |
||||
|
||||
public static class CodeGenerateException extends Exception { |
||||
public CodeGenerateException(String message) { |
||||
super(message); |
||||
} |
||||
} |
||||
} |
@ -1,94 +0,0 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You 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 org.apache.dolphinscheduler.common.utils; |
||||
|
||||
import java.net.InetAddress; |
||||
import java.net.UnknownHostException; |
||||
import java.util.Objects; |
||||
|
||||
public class SnowFlakeUtils { |
||||
// start timestamp
|
||||
private static final long START_TIMESTAMP = 1609430400000L; //2021-01-01 00:00:00
|
||||
// Each machine generates 32 in the same millisecond
|
||||
private static final long SEQUENCE_BIT = 5; |
||||
private static final long MACHINE_BIT = 2; |
||||
private static final long MAX_SEQUENCE = ~(-1L << SEQUENCE_BIT); |
||||
// The displacement to the left
|
||||
private static final long MACHINE_LEFT = SEQUENCE_BIT + MACHINE_BIT; |
||||
private static final long TIMESTAMP_LEFT = SEQUENCE_BIT + MACHINE_BIT + MACHINE_LEFT; |
||||
private final int machineId; |
||||
private long sequence = 0L; |
||||
private long lastTimestamp = -1L; |
||||
|
||||
private static final long SYSTEM_TIMESTAMP = System.currentTimeMillis(); |
||||
private static final long SYSTEM_NANOTIME = System.nanoTime(); |
||||
|
||||
private SnowFlakeUtils() throws SnowFlakeException { |
||||
try { |
||||
this.machineId = Math.abs(Objects.hash(InetAddress.getLocalHost().getHostName())) % 4; |
||||
} catch (UnknownHostException e) { |
||||
throw new SnowFlakeException(e.getMessage()); |
||||
} |
||||
} |
||||
|
||||
private static SnowFlakeUtils instance = null; |
||||
|
||||
public static synchronized SnowFlakeUtils getInstance() throws SnowFlakeException { |
||||
if (instance == null) { |
||||
instance = new SnowFlakeUtils(); |
||||
} |
||||
return instance; |
||||
} |
||||
|
||||
public synchronized long nextId() throws SnowFlakeException { |
||||
long currStmp = nowTimestamp(); |
||||
if (currStmp < lastTimestamp) { |
||||
throw new SnowFlakeException("Clock moved backwards. Refusing to generate id"); |
||||
} |
||||
if (currStmp == lastTimestamp) { |
||||
sequence = (sequence + 1) & MAX_SEQUENCE; |
||||
if (sequence == 0L) { |
||||
currStmp = getNextMill(); |
||||
} |
||||
} else { |
||||
sequence = 0L; |
||||
} |
||||
lastTimestamp = currStmp; |
||||
return (currStmp - START_TIMESTAMP) << TIMESTAMP_LEFT |
||||
| machineId << MACHINE_LEFT |
||||
| sequence; |
||||
} |
||||
|
||||
private long getNextMill() { |
||||
long mill = nowTimestamp(); |
||||
while (mill <= lastTimestamp) { |
||||
mill = nowTimestamp(); |
||||
} |
||||
return mill; |
||||
} |
||||
|
||||
private long nowTimestamp() { |
||||
return SYSTEM_TIMESTAMP + (System.nanoTime() - SYSTEM_NANOTIME) / 1000000; |
||||
} |
||||
|
||||
public static class SnowFlakeException extends Exception { |
||||
public SnowFlakeException(String message) { |
||||
super(message); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,11 @@
|
||||
Copyright 2010-2012 Twitter, Inc. |
||||
|
||||
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. |
@ -0,0 +1,11 @@
|
||||
Copyright 2010-2012 Twitter, Inc. |
||||
|
||||
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. |
Loading…
Reference in new issue