Skip to content

Instantly share code, notes, and snippets.

@jessedearing
Created March 18, 2016 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessedearing/542e05f7f036b3abb988 to your computer and use it in GitHub Desktop.
Save jessedearing/542e05f7f036b3abb988 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
if len(sys.argv) != 3:
print "usage: cassandra_tokens.py <current_size> <new_size>"
sys.exit(1)
CURRENT_NODES = int(sys.argv[1])
GOING_TO_NODES = int(sys.argv[2])
RING_SIZE = 2**127
def tokens(n):
rv = []
for x in xrange(n):
rv.append(RING_SIZE / n * x)
return rv
def print_layout(nodes):
i = 0
for n in nodes:
i += 1
print "%d:\t%d" % (i, n)
current = tokens(CURRENT_NODES)
go = tokens(GOING_TO_NODES)
print "Existing Layout: "
print_layout(current)
print ""
print ""
print "New Layout: "
print_layout(go)
print ""
print ""
i = 0
j = 0
pending = []
while j != len(go) and i != len(current):
cur = current[i]
next = go[j]
if next == cur:
if pending:
for x in pending:
print x
pending = []
print "[%d] Old Node %d stays at %d" % (j+1, i+1, cur)
i += 1
if next > cur:
if pending:
for x in pending:
print x
pending = []
#diff = (next - cur)
print "[%d] Old Node %d moves to %d" % (j+1, i+1, next)
i += 1
if next < cur:
pending.insert(0, "[%d] New Node added at %d" % (j+1, next))
j += 1
if pending:
for x in pending:
print x
pending = []
while j != len(go):
print "[%d] Add new node %d at %d" % (j+1, j+1, go[j])
j += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment