1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
import org.slf4j.Logger; import org.slf4j.LoggerFactory;
import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Enumeration; import java.util.Random;
public class SnowflakeIDGenerator implements IDGenerator { private static Logger log = LoggerFactory.getLogger(SnowflakeIDGenerator.class);
private final long datacenterIdBits = 10L; private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); private final long sequenceBits = 12L;
private final long datacenterIdShift = sequenceBits; private final long timestampLeftShift = sequenceBits + datacenterIdBits; private final long sequenceMask = -1L ^ (-1L << sequenceBits);
private final long twepoch = 1288834974657L; private long datacenterId;
private volatile long lastTimestamp = -1L; private volatile long sequence = 0L;
public SnowflakeIDGenerator(long datacenterId) { if(datacenterId == 0) { try { this.datacenterId = getDatacenterId(); } catch (SocketException | UnknownHostException | NullPointerException e) { log.warn("SNOWFLAKE: could not determine machine address; using random datacenter ID"); Random rnd = new Random(); this.datacenterId = rnd.nextInt((int)maxDatacenterId) + 1; } } else { this.datacenterId = datacenterId; }
if (this.datacenterId > maxDatacenterId || datacenterId < 0){ log.warn("SNOWFLAKE: datacenterId > maxDatacenterId; using random datacenter ID"); Random rnd = new Random(); this.datacenterId = rnd.nextInt((int)maxDatacenterId) + 1; } log.info("SNOWFLAKE: initialised with datacenter ID {}", this.datacenterId); }
protected long tilNextMillis(long lastTimestamp){ long timestamp = System.currentTimeMillis(); while (timestamp <= lastTimestamp) { timestamp = System.currentTimeMillis(); } return timestamp; }
protected long getDatacenterId() throws SocketException, UnknownHostException { NetworkInterface network = null;
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); while (en.hasMoreElements()) { NetworkInterface nint = en.nextElement(); if (!nint.isLoopback() && nint.getHardwareAddress() != null) { network = nint; break; } }
byte[] mac = network.getHardwareAddress();
Random rnd = new Random(); byte rndByte = (byte)(rnd.nextInt() & 0x000000FF);
return ((0x000000FF & (long)mac[mac.length-1]) | (0x0000FF00 & (((long)rndByte)<<8)))>>6; }
@Override public synchronized long nextId() { long timestamp = System.currentTimeMillis(); if(timestamp<lastTimestamp) { log.warn("Clock moved backwards. Refusing to generate id for {} milliseconds.",(lastTimestamp - timestamp)); try { Thread.sleep((lastTimestamp - timestamp)); } catch (InterruptedException e) { } } if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; if (sequence == 0) { timestamp = tilNextMillis(lastTimestamp); } } else { sequence = 0; } lastTimestamp = timestamp; long id = ((timestamp - twepoch) << timestampLeftShift) | (datacenterId << datacenterIdShift) | sequence;
if(id < 0) { log.warn("ID is smaller than 0: {}",id); } return id; }
@Override public void shutdown() { } }
|