/** * An object that generates IDs. * This is broken into a separate class in case * we ever want to support multiple worker threads * per process */ classIdWorker(val workerId: Long, val datacenterId: Long, private val reporter: Reporter, var sequence: Long = 0L) extendsSnowflake.Iface{ private[this] defgenCounter(agent: String) = { Stats.incr("ids_generated") Stats.incr("ids_generated_%s".format(agent)) } private[this] val exceptionCounter = Stats.getCounter("exceptions") private[this] val log = Logger.get private[this] val rand = newRandom
val twepoch = 1288834974657L
//机器标识位数
private[this] val workerIdBits = 5L
//数据中心标识位数 private[this] val datacenterIdBits = 5L
//机器ID最大值 private[this] val maxWorkerId = -1L ^ (-1L << workerIdBits)
//数据中心ID最大值 private[this] val maxDatacenterId = -1L ^ (-1L << datacenterIdBits)
//毫秒内自增位 private[this] val sequenceBits = 12L
//机器ID偏左移12位
private[this] val workerIdShift = sequenceBits
//数据中心ID左移17位 private[this] val datacenterIdShift = sequenceBits + workerIdBits
//时间毫秒左移22位 private[this] val timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits private[this] val sequenceMask = -1L ^ (-1L << sequenceBits)
private[this] var lastTimestamp = -1L
// sanity check for workerId if (workerId > maxWorkerId || workerId < 0) { exceptionCounter.incr(1) thrownewIllegalArgumentException("worker Id can't be greater than %d or less than 0".format(maxWorkerId)) }
if (datacenterId > maxDatacenterId || datacenterId < 0) { exceptionCounter.incr(1) thrownewIllegalArgumentException("datacenter Id can't be greater than %d or less than 0".format(maxDatacenterId)) }
log.info("worker starting. timestamp left shift %d, datacenter id bits %d, worker id bits %d, sequence bits %d, workerid %d", timestampLeftShift, datacenterIdBits, workerIdBits, sequenceBits, workerId)
defget_id(useragent: String): Long = { if (!validUseragent(useragent)) { exceptionCounter.incr(1) thrownewInvalidUserAgentError }
val id = nextId() genCounter(useragent)
reporter.report(newAuditLogEntry(id, useragent, rand.nextLong)) id }
defget_worker_id(): Long = workerId defget_datacenter_id(): Long = datacenterId defget_timestamp() = System.currentTimeMillis
protected[snowflake] defnextId(): Long = synchronized { var timestamp = timeGen()
//时间错误
if (timestamp < lastTimestamp) { exceptionCounter.incr(1) log.error("clock is moving backwards. Rejecting requests until %d.", lastTimestamp); thrownewInvalidSystemClock("Clock moved backwards. Refusing to generate id for %d milliseconds".format( lastTimestamp - timestamp)) }
publicIdWorker(finallong workerId){ super(); if (workerId > this.maxWorkerId || workerId < 0) { thrownew IllegalArgumentException(String.format( "worker Id can't be greater than %d or less than 0", this.maxWorkerId)); } this.workerId = workerId; }
publicsynchronizedlongnextId(){ long timestamp = this.timeGen(); if (this.lastTimestamp == timestamp) { this.sequence = (this.sequence + 1) & this.sequenceMask; if (this.sequence == 0) { System.out.println("###########" + sequenceMask); timestamp = this.tilNextMillis(this.lastTimestamp); } } else { this.sequence = 0; } if (timestamp < this.lastTimestamp) { try { thrownew Exception( String.format( "Clock moved backwards. Refusing to generate id for %d milliseconds", this.lastTimestamp - timestamp)); } catch (Exception e) { e.printStackTrace(); } }
function__construct($workId){ if($workId > self::$maxWorkerId || $workId< 0 ) { thrownewException("worker Id can't be greater than 15 or less than 0"); } self::$workerId=$workId;