Permalink
Browse files

[split] keep track of ids generated by user-agent

  • Loading branch information...
1 parent 4254273 commit 129c82c3fa08bfe28c4d11f4ef9bf9905b8bc986 Ryan King committed Mar 1, 2012
View
2 LICENSE
@@ -1,4 +1,4 @@
-Copyright 2010-2011 Twitter, Inc.
+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
View
40 src/main/scala/com/twitter/service/snowflake/IdWorker.scala
@@ -1,4 +1,4 @@
-/** Copyright 2010-2011 Twitter, Inc.*/
+/** Copyright 2010-2012 Twitter, Inc.*/
package com.twitter.service.snowflake
import com.twitter.ostrich.stats.Stats
@@ -12,28 +12,30 @@ import com.twitter.logging.Logger
* we ever want to support multiple worker threads
* per process
*/
-class IdWorker(workerId: Long, datacenterId: Long, private val reporter: Reporter)
- extends Snowflake.Iface {
- private val genCounter = Stats.getCounter("ids_generated")
- private val exceptionCounter = Stats.getCounter("exceptions")
- private val log = Logger.get
- private val rand = new Random
+class IdWorker(val workerId: Long, val datacenterId: Long, private val reporter: Reporter, var sequence: Long = 0L)
+extends Snowflake.Iface {
+ private[this] def genCounter(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 = new Random
val twepoch = 1288834974657L
- var sequence = 0L //TODO after 2.8 make this a constructor param with a default of 0
- private val workerIdBits = 5L
- private val datacenterIdBits = 5L
- private val maxWorkerId = -1L ^ (-1L << workerIdBits)
- private val maxDatacenterId = -1L ^ (-1L << datacenterIdBits)
- private val sequenceBits = 12L
+ private[this] val workerIdBits = 5L
+ private[this] val datacenterIdBits = 5L
+ private[this] val maxWorkerId = -1L ^ (-1L << workerIdBits)
+ private[this] val maxDatacenterId = -1L ^ (-1L << datacenterIdBits)
+ private[this] val sequenceBits = 12L
- private val workerIdShift = sequenceBits
- private val datacenterIdShift = sequenceBits + workerIdBits
- private val timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits
- private val sequenceMask = -1L ^ (-1L << sequenceBits)
+ private[this] val workerIdShift = sequenceBits
+ private[this] val datacenterIdShift = sequenceBits + workerIdBits
+ private[this] val timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits
+ private[this] val sequenceMask = -1L ^ (-1L << sequenceBits)
- private var lastTimestamp = -1L
+ private[this] var lastTimestamp = -1L
// sanity check for workerId
if (workerId > maxWorkerId || workerId < 0) {
@@ -56,6 +58,7 @@ class IdWorker(workerId: Long, datacenterId: Long, private val reporter: Reporte
}
val id = nextId()
+ genCounter(useragent)
reporter.report(new AuditLogEntry(id, useragent, rand.nextLong))
id
@@ -85,7 +88,6 @@ class IdWorker(workerId: Long, datacenterId: Long, private val reporter: Reporte
}
lastTimestamp = timestamp
- genCounter.incr()
((timestamp - twepoch) << timestampLeftShift) |
(datacenterId << datacenterIdShift) |
(workerId << workerIdShift) |
View
2 src/main/scala/com/twitter/service/snowflake/Reporter.scala
@@ -1,4 +1,4 @@
-/** Copyright 2010-2011 Twitter, Inc.*/
+/** Copyright 2010-2012 Twitter, Inc.*/
package com.twitter.service.snowflake
import com.twitter.ostrich.admin.BackgroundProcess
View
4 src/main/scala/com/twitter/service/snowflake/SnowflakeServer.scala
@@ -1,4 +1,4 @@
-/** Copyright 2010-2011 Twitter, Inc. */
+/** Copyright 2010-2012 Twitter, Inc. */
package com.twitter.service.snowflake
import com.twitter.service.snowflake.client.SnowflakeClient
@@ -43,7 +43,7 @@ class SnowflakeServer(serverPort: Int, datacenterId: Int, workerId: Int, workerI
skipSanityChecks: Boolean, startupSleepMs: Int, thriftServerThreads: Int,
reporter: Reporter, zkClient: ZooKeeperClient) extends Service {
- private val log = Logger.get
+ private[this] val log = Logger.get
var server: TServer = null
Stats.addGauge("datacenter_id") { datacenterId }
View
2 src/main/scala/com/twitter/service/snowflake/client/SnowflakeClient.scala
@@ -1,4 +1,4 @@
-/** Copyright 2010-2011 Twitter, Inc. */
+/** Copyright 2010-2012 Twitter, Inc. */
package com.twitter.service.snowflake.client
import com.twitter.service.snowflake.gen.Snowflake
View
2 src/main/scala/com/twitter/service/snowflake/client/ThriftClient.scala
@@ -1,4 +1,4 @@
-/** Copyright 2010-2011 Twitter, Inc. */
+/** Copyright 2010-2012 Twitter, Inc. */
package com.twitter.service.snowflake.client
import org.apache.thrift.TException
View
2 src/main/thrift/Snowflake.thrift
@@ -1,4 +1,4 @@
-/* Copyright 2010-2011 Twitter, Inc. */
+/* Copyright 2010-2012 Twitter, Inc. */
namespace java com.twitter.service.snowflake.gen
exception InvalidSystemClock {
View
9 src/test/scala/com/twitter/service/snowflake/IdWorkerSpec.scala
@@ -1,8 +1,9 @@
package com.twitter.service.snowflake
-import org.specs._
import com.twitter.logging.Logger
+import com.twitter.ostrich.stats.Stats
import com.twitter.service.snowflake.gen.InvalidSystemClock
+import org.specs._
class IdWorkerSpec extends Specification {
val workerMask = 0x000000000001F000L
@@ -192,7 +193,13 @@ class IdWorkerSpec extends Specification {
val id3 = worker.nextId
(id3 >> 22) must be_==(1)
(id3 & sequenceMask ) must be_==(2)
+ }
+ "increment the right stats" in {
+ val worker = new IdWorker(0, 0, reporter)
+ worker.get_id("foo-bar")
+ Stats.getCounter("ids_generated")() must be_==(1)
+ Stats.getCounter("ids_generated_foo-bar")() must be_==(1)
}
}

0 comments on commit 129c82c

Please sign in to comment.