Skip to content

Instantly share code, notes, and snippets.

@pcn
Last active December 27, 2015 14:29
Show Gist options
  • Select an option

  • Save pcn/7340876 to your computer and use it in GitHub Desktop.

Select an option

Save pcn/7340876 to your computer and use it in GitHub Desktop.
Generating tokens the priam way
#!/usr/bin/env python
import sys
import decimal
from decimal import Decimal
# Translated from netflix/priam
# MINIMUM_TOKEN = Decimal(0)
# MAXIMUM_TOKEN = Decimal(2) ** Decimal(127)
MINIMUM_TOKEN = 0
MAXIMUM_TOKEN = 2 ** 127
decimal.getcontext().prec = 40
def initial_token(size, position, offset):
return ((MAXIMUM_TOKEN / size) * position) + offset
#*
# Creates a token given the following parameter
#
# @param my_slot
# -- Slot where this instance has to be.
# @param rac_count
# -- Rac count is the numeber of RAC's
# @param rac_size
# -- number of memberships in the rac
# @param region
# -- name of the DC where it this token is created.
#/
#
# def create_token(my_slot, total_count, region_name):
# return initial_token(Decimal(total_count), Decimal(my_slot), Decimal(region_offset(region_name))).normalize()
def create_token(my_slot, total_count, region_name):
return initial_token(total_count, my_slot, region_offset(region_name))
def region_offset(region):
return java_string_hashcode(region)
def java_string_hashcode(s):
h = 0
for c in s:
h = (31 * h + ord(c)) & 0xFFFFFFFF
return ((h + 0x80000000) & 0xFFFFFFFF) - 0x80000000
def create_n_nodes_tokens_in(argv = sys.argv[:]):
count = int(argv[1])
region = argv[2]
for position in range(count):
print "{0}: {1:.0f}".format(region, create_token(position, count, region))
def main():
create_n_nodes_tokens_in()
if __name__ == '__main__':
main()
user> (pprint_token_list 12 "us-east-1")
("us-east-1 0: 1808575600"
"us-east-1 1: 14178431955039102644307275311465584410"
"us-east-1 2: 28356863910078205288614550621122593220"
"us-east-1 3: 42535295865117307932921825930779602030"
"us-east-1 4: 56713727820156410577229101240436610840"
"us-east-1 5: 70892159775195513221536376550093619650"
"us-east-1 6: 85070591730234615865843651859750628460"
"us-east-1 7: 99249023685273718510150927169407637270"
"us-east-1 8: 113427455640312821154458202479064646080"
"us-east-1 9: 127605887595351923798765477788721654890"
"us-east-1 10: 141784319550391026443072753098378663700"
"us-east-1 11: 155962751505430129087380028408035672510")
nil
user>
12 node:
(sedeploy)753 pn@pn-mbp 00:40 ~/dvcs/github/netflix/Priam $ python ~/tmp/assign_token.py 12 us-east-1
us-east-1: 1808575600
us-east-1: 14178431955039101857246194831382806528
us-east-1: 28356863910078203714492389662765613056
us-east-1: 42535295865117307932921825928971026432
us-east-1: 56713727820156407428984779325531226112
us-east-1: 70892159775195516369780698461381853184
us-east-1: 85070591730234615865843651857942052864
us-east-1: 99249023685273724806639570993792679936
us-east-1: 113427455640312814857969558651062452224
us-east-1: 127605887595351923798765477786913079296
us-east-1: 141784319550391032739561396922763706368
us-east-1: 155962751505430122790891384580033478656
@ianschenck

Copy link
Copy Markdown

λ ~/Downloads/ java -jar jython-standalone-2.5.3.jar get_tokens.py 12 us-east-1
us-east-1: 1808575600
us-east-1: 14178431955039102644307275311465584410
us-east-1: 28356863910078205288614550621122593220
us-east-1: 42535295865117307932921825930779602030
us-east-1: 56713727820156410577229101240436610840
us-east-1: 70892159775195513221536376550093619650
us-east-1: 85070591730234615865843651859750628460
us-east-1: 99249023685273718510150927169407637270
us-east-1: 113427455640312821154458202479064646080
us-east-1: 127605887595351923798765477788721654890
us-east-1: 141784319550391026443072753098378663700
us-east-1: 155962751505430129087380028408035672510

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment