@Test public void testMaskWorkerId() throws Exception { final Integer workerId = 0x1F; final Integer datacenterId = 0; final IdWorker worker = new IdWorker(workerId, datacenterId); for (int i = 0; i < 1000; i++) { Long id = worker.nextId(); assertThat((id & WORKER_MASK) >> 12).isEqualTo( Long.valueOf(workerId)); } }
public synchronized long nextId() throws InvalidSystemClock { long timestamp = timeGen(); long curSequence = 0L; final long prevTimestamp = lastTimestamp.get(); if (timestamp < prevTimestamp) { exceptionsCounter.inc(); LOGGER.error( "clock is moving backwards. Rejecting requests until {}", prevTimestamp); throw new InvalidSystemClock( String.format( "Clock moved backwards. Refusing to generate id for %d milliseconds", (prevTimestamp - timestamp))); } if (prevTimestamp == timestamp) { curSequence = sequence.incrementAndGet() & SEQUENCE_MASK; if (curSequence == 0) { timestamp = tilNextMillis(prevTimestamp); } } else { curSequence = 0L; sequence.set(0L); } lastTimestamp.set(timestamp); final long id = ((timestamp - TWEPOCH) << TIMESTAMP_LEFT_SHIFT) | (datacenterId << DATACENTER_ID_SHIFT) | (workerId << WORKER_ID_SHIFT) | curSequence; LOGGER.trace( "prevTimestamp = {}, timestamp = {}, sequence = {}, id = {}", prevTimestamp, timestamp, sequence, id); return id; }
IdWorker { public synchronized long nextId() throws InvalidSystemClock { long timestamp = timeGen(); long curSequence = 0L; final long prevTimestamp = lastTimestamp.get(); if (timestamp < prevTimestamp) { exceptionsCounter.inc(); LOGGER.error( "clock is moving backwards. Rejecting requests until {}", prevTimestamp); throw new InvalidSystemClock( String.format( "Clock moved backwards. Refusing to generate id for %d milliseconds", (prevTimestamp - timestamp))); } if (prevTimestamp == timestamp) { curSequence = sequence.incrementAndGet() & SEQUENCE_MASK; if (curSequence == 0) { timestamp = tilNextMillis(prevTimestamp); } } else { curSequence = 0L; sequence.set(0L); } lastTimestamp.set(timestamp); final long id = ((timestamp - TWEPOCH) << TIMESTAMP_LEFT_SHIFT) | (datacenterId << DATACENTER_ID_SHIFT) | (workerId << WORKER_ID_SHIFT) | curSequence; LOGGER.trace( "prevTimestamp = {}, timestamp = {}, sequence = {}, id = {}", prevTimestamp, timestamp, sequence, id); return id; } }
IdWorker { public synchronized long nextId() throws InvalidSystemClock { long timestamp = timeGen(); long curSequence = 0L; final long prevTimestamp = lastTimestamp.get(); if (timestamp < prevTimestamp) { exceptionsCounter.inc(); LOGGER.error( "clock is moving backwards. Rejecting requests until {}", prevTimestamp); throw new InvalidSystemClock( String.format( "Clock moved backwards. Refusing to generate id for %d milliseconds", (prevTimestamp - timestamp))); } if (prevTimestamp == timestamp) { curSequence = sequence.incrementAndGet() & SEQUENCE_MASK; if (curSequence == 0) { timestamp = tilNextMillis(prevTimestamp); } } else { curSequence = 0L; sequence.set(0L); } lastTimestamp.set(timestamp); final long id = ((timestamp - TWEPOCH) << TIMESTAMP_LEFT_SHIFT) | (datacenterId << DATACENTER_ID_SHIFT) | (workerId << WORKER_ID_SHIFT) | curSequence; LOGGER.trace( "prevTimestamp = {}, timestamp = {}, sequence = {}, id = {}", prevTimestamp, timestamp, sequence, id); return id; } IdWorker(final int workerId, final int datacenterId);  IdWorker(final int workerId, final int datacenterId,
            final long startSequence);  IdWorker(final int workerId, final int datacenterId,
            final boolean validateUserAgent);  IdWorker(final int workerId, final int datacenterId,
            final long startSequence, final boolean validateUserAgent);  IdWorker(final int workerId, final int datacenterId,
            final long startSequence, final boolean validateUserAgent,
            final MetricRegistry registry); }
IdWorker { public synchronized long nextId() throws InvalidSystemClock { long timestamp = timeGen(); long curSequence = 0L; final long prevTimestamp = lastTimestamp.get(); if (timestamp < prevTimestamp) { exceptionsCounter.inc(); LOGGER.error( "clock is moving backwards. Rejecting requests until {}", prevTimestamp); throw new InvalidSystemClock( String.format( "Clock moved backwards. Refusing to generate id for %d milliseconds", (prevTimestamp - timestamp))); } if (prevTimestamp == timestamp) { curSequence = sequence.incrementAndGet() & SEQUENCE_MASK; if (curSequence == 0) { timestamp = tilNextMillis(prevTimestamp); } } else { curSequence = 0L; sequence.set(0L); } lastTimestamp.set(timestamp); final long id = ((timestamp - TWEPOCH) << TIMESTAMP_LEFT_SHIFT) | (datacenterId << DATACENTER_ID_SHIFT) | (workerId << WORKER_ID_SHIFT) | curSequence; LOGGER.trace( "prevTimestamp = {}, timestamp = {}, sequence = {}, id = {}", prevTimestamp, timestamp, sequence, id); return id; } IdWorker(final int workerId, final int datacenterId);  IdWorker(final int workerId, final int datacenterId,
            final long startSequence);  IdWorker(final int workerId, final int datacenterId,
            final boolean validateUserAgent);  IdWorker(final int workerId, final int datacenterId,
            final long startSequence, final boolean validateUserAgent);  IdWorker(final int workerId, final int datacenterId,
            final long startSequence, final boolean validateUserAgent,
            final MetricRegistry registry); long getId(final String agent); int getWorkerId(); int getDatacenterId(); long getTimestamp(); long getSequence(); void setSequence(final long value); synchronized long nextId(); boolean isValidUserAgent(final String agent); }
IdWorker { public synchronized long nextId() throws InvalidSystemClock { long timestamp = timeGen(); long curSequence = 0L; final long prevTimestamp = lastTimestamp.get(); if (timestamp < prevTimestamp) { exceptionsCounter.inc(); LOGGER.error( "clock is moving backwards. Rejecting requests until {}", prevTimestamp); throw new InvalidSystemClock( String.format( "Clock moved backwards. Refusing to generate id for %d milliseconds", (prevTimestamp - timestamp))); } if (prevTimestamp == timestamp) { curSequence = sequence.incrementAndGet() & SEQUENCE_MASK; if (curSequence == 0) { timestamp = tilNextMillis(prevTimestamp); } } else { curSequence = 0L; sequence.set(0L); } lastTimestamp.set(timestamp); final long id = ((timestamp - TWEPOCH) << TIMESTAMP_LEFT_SHIFT) | (datacenterId << DATACENTER_ID_SHIFT) | (workerId << WORKER_ID_SHIFT) | curSequence; LOGGER.trace( "prevTimestamp = {}, timestamp = {}, sequence = {}, id = {}", prevTimestamp, timestamp, sequence, id); return id; } IdWorker(final int workerId, final int datacenterId);  IdWorker(final int workerId, final int datacenterId,
            final long startSequence);  IdWorker(final int workerId, final int datacenterId,
            final boolean validateUserAgent);  IdWorker(final int workerId, final int datacenterId,
            final long startSequence, final boolean validateUserAgent);  IdWorker(final int workerId, final int datacenterId,
            final long startSequence, final boolean validateUserAgent,
            final MetricRegistry registry); long getId(final String agent); int getWorkerId(); int getDatacenterId(); long getTimestamp(); long getSequence(); void setSequence(final long value); synchronized long nextId(); boolean isValidUserAgent(final String agent); static final long TWEPOCH; }