Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit 129c82c3fa08bfe28c4d11f4ef9bf9905b8bc986 1 parent 4254273
Ryan King authored
2  LICENSE
... ...
@@ -1,4 +1,4 @@
1  
-Copyright 2010-2011 Twitter, Inc.
  1
+Copyright 2010-2012 Twitter, Inc.
2 2
 
3 3
 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this 
4 4
 file except in compliance with the License. You may obtain a copy of the License at
40  src/main/scala/com/twitter/service/snowflake/IdWorker.scala
... ...
@@ -1,4 +1,4 @@
1  
-/** Copyright 2010-2011 Twitter, Inc.*/
  1
+/** Copyright 2010-2012 Twitter, Inc.*/
2 2
 package com.twitter.service.snowflake
3 3
 
4 4
 import com.twitter.ostrich.stats.Stats
@@ -12,28 +12,30 @@ import com.twitter.logging.Logger
12 12
  * we ever want to support multiple worker threads
13 13
  * per process
14 14
  */
15  
-class IdWorker(workerId: Long, datacenterId: Long, private val reporter: Reporter)
16  
-    extends Snowflake.Iface {
17  
-  private val genCounter = Stats.getCounter("ids_generated")
18  
-  private val exceptionCounter = Stats.getCounter("exceptions")
19  
-  private val log = Logger.get
20  
-  private val rand = new Random
  15
+class IdWorker(val workerId: Long, val datacenterId: Long, private val reporter: Reporter, var sequence: Long = 0L)
  16
+extends Snowflake.Iface {
  17
+  private[this] def genCounter(agent: String) = {
  18
+    Stats.incr("ids_generated")
  19
+    Stats.incr("ids_generated_%s".format(agent))
  20
+  }
  21
+  private[this] val exceptionCounter = Stats.getCounter("exceptions")
  22
+  private[this] val log = Logger.get
  23
+  private[this] val rand = new Random
21 24
 
22 25
   val twepoch = 1288834974657L
23 26
 
24  
-  var sequence = 0L //TODO after 2.8 make this a constructor param with a default of 0
25  
-  private val workerIdBits = 5L
26  
-  private val datacenterIdBits = 5L
27  
-  private val maxWorkerId = -1L ^ (-1L << workerIdBits)
28  
-  private val maxDatacenterId = -1L ^ (-1L << datacenterIdBits)
29  
-  private val sequenceBits = 12L
  27
+  private[this] val workerIdBits = 5L
  28
+  private[this] val datacenterIdBits = 5L
  29
+  private[this] val maxWorkerId = -1L ^ (-1L << workerIdBits)
  30
+  private[this] val maxDatacenterId = -1L ^ (-1L << datacenterIdBits)
  31
+  private[this] val sequenceBits = 12L
30 32
 
31  
-  private val workerIdShift = sequenceBits
32  
-  private val datacenterIdShift = sequenceBits + workerIdBits
33  
-  private val timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits
34  
-  private val sequenceMask = -1L ^ (-1L << sequenceBits)
  33
+  private[this] val workerIdShift = sequenceBits
  34
+  private[this] val datacenterIdShift = sequenceBits + workerIdBits
  35
+  private[this] val timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits
  36
+  private[this] val sequenceMask = -1L ^ (-1L << sequenceBits)
35 37
 
36  
-  private var lastTimestamp = -1L
  38
+  private[this] var lastTimestamp = -1L
37 39
 
38 40
   // sanity check for workerId
39 41
   if (workerId > maxWorkerId || workerId < 0) {
@@ -56,6 +58,7 @@ class IdWorker(workerId: Long, datacenterId: Long, private val reporter: Reporte
56 58
     }
57 59
 
58 60
     val id = nextId()
  61
+    genCounter(useragent)
59 62
 
60 63
     reporter.report(new AuditLogEntry(id, useragent, rand.nextLong))
61 64
     id
@@ -85,7 +88,6 @@ class IdWorker(workerId: Long, datacenterId: Long, private val reporter: Reporte
85 88
     }
86 89
 
87 90
     lastTimestamp = timestamp
88  
-    genCounter.incr()
89 91
     ((timestamp - twepoch) << timestampLeftShift) |
90 92
       (datacenterId << datacenterIdShift) |
91 93
       (workerId << workerIdShift) | 
2  src/main/scala/com/twitter/service/snowflake/Reporter.scala
... ...
@@ -1,4 +1,4 @@
1  
-/** Copyright 2010-2011 Twitter, Inc.*/
  1
+/** Copyright 2010-2012 Twitter, Inc.*/
2 2
 package com.twitter.service.snowflake
3 3
 
4 4
 import com.twitter.ostrich.admin.BackgroundProcess
4  src/main/scala/com/twitter/service/snowflake/SnowflakeServer.scala
... ...
@@ -1,4 +1,4 @@
1  
-/** Copyright 2010-2011 Twitter, Inc. */
  1
+/** Copyright 2010-2012 Twitter, Inc. */
2 2
 package com.twitter.service.snowflake
3 3
 
4 4
 import com.twitter.service.snowflake.client.SnowflakeClient
@@ -43,7 +43,7 @@ class SnowflakeServer(serverPort: Int, datacenterId: Int, workerId: Int, workerI
43 43
     skipSanityChecks: Boolean, startupSleepMs: Int, thriftServerThreads: Int,
44 44
     reporter: Reporter, zkClient: ZooKeeperClient) extends Service {
45 45
 
46  
-  private val log = Logger.get
  46
+  private[this] val log = Logger.get
47 47
   var server: TServer = null
48 48
 
49 49
   Stats.addGauge("datacenter_id") { datacenterId }
2  src/main/scala/com/twitter/service/snowflake/client/SnowflakeClient.scala
... ...
@@ -1,4 +1,4 @@
1  
-/** Copyright 2010-2011 Twitter, Inc. */
  1
+/** Copyright 2010-2012 Twitter, Inc. */
2 2
 package com.twitter.service.snowflake.client
3 3
 import com.twitter.service.snowflake.gen.Snowflake
4 4
 
2  src/main/scala/com/twitter/service/snowflake/client/ThriftClient.scala
... ...
@@ -1,4 +1,4 @@
1  
-/** Copyright 2010-2011 Twitter, Inc. */
  1
+/** Copyright 2010-2012 Twitter, Inc. */
2 2
 package com.twitter.service.snowflake.client
3 3
 
4 4
 import org.apache.thrift.TException
2  src/main/thrift/Snowflake.thrift
... ...
@@ -1,4 +1,4 @@
1  
-/* Copyright 2010-2011 Twitter, Inc. */
  1
+/* Copyright 2010-2012 Twitter, Inc. */
2 2
 namespace java com.twitter.service.snowflake.gen
3 3
 
4 4
 exception InvalidSystemClock {
9  src/test/scala/com/twitter/service/snowflake/IdWorkerSpec.scala
... ...
@@ -1,8 +1,9 @@
1 1
 package com.twitter.service.snowflake
2 2
 
3  
-import org.specs._
4 3
 import com.twitter.logging.Logger
  4
+import com.twitter.ostrich.stats.Stats
5 5
 import com.twitter.service.snowflake.gen.InvalidSystemClock
  6
+import org.specs._
6 7
 
7 8
 class IdWorkerSpec extends Specification {
8 9
   val workerMask     = 0x000000000001F000L
@@ -192,7 +193,13 @@ class IdWorkerSpec extends Specification {
192 193
       val id3 = worker.nextId
193 194
       (id3 >> 22) must be_==(1)
194 195
       (id3 & sequenceMask ) must be_==(2)
  196
+    }
195 197
 
  198
+    "increment the right stats" in {
  199
+      val worker = new IdWorker(0, 0, reporter)
  200
+      worker.get_id("foo-bar")
  201
+      Stats.getCounter("ids_generated")() must be_==(1)
  202
+      Stats.getCounter("ids_generated_foo-bar")() must be_==(1)
196 203
     }
197 204
   }
198 205
 

0 notes on commit 129c82c

Please sign in to comment.
Something went wrong with that request. Please try again.