Skip to content

Instantly share code, notes, and snippets.

@lorcan
Last active December 22, 2015 23:39
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 lorcan/6548266 to your computer and use it in GitHub Desktop.
Save lorcan/6548266 to your computer and use it in GitHub Desktop.
Handling pycassa.pool.AllServersUnavailable exception. Response to http://stackoverflow.com/questions/15906550/how-to-handle-allserversunavailable-exception
import pycassa
import time
import string
from pycassa.pool import ConnectionPool
from pycassa.columnfamily import ColumnFamily
import uuid
import random
import sys
pool = ConnectionPool('testdb')
test_cf = ColumnFamily(pool,'test')
test2_cf = ColumnFamily(pool,'test2')
test3_cf = ColumnFamily(pool,'test3')
test_batch = test_cf.batch(queue_size=1000)
test2_batch = test2_cf.batch(queue_size=1000)
test3_batch = test3_cf.batch(queue_size=1000)
chars=string.ascii_uppercase
counter = 0
while True:
counter += 1
uid = uuid.uuid1()
junk = ''.join(random.choice(chars) for x in range(50))
tryCount = 5 # 5 is probably unnecessarily high
while tryCount > 0:
try:
test_batch.insert(uid, {'junk':junk})
test2_batch.insert(uid, {'junk':junk})
test3_batch.insert(uid, {'junk':junk})
tryCount = -1
except pycassa.pool.AllServersUnavailable as e:
print "Trying to insert [" + str(uid) + "] but got error " + str(e) + " (attempt " + str(tryCount) + "). Backing off for a minute to let Cassandra settle down"
time.sleep(60) # A delay of 60s is probably unnecessarily high
tryCount = tryCount - 1
sys.stdout.write(str(counter)+'\n')
pool.dispose()
use testdb;
create column family test and key_validation_class = 'UUIDType';
create column family test2 and key_validation_class = 'UUIDType';
create column family test3 and key_validation_class = 'UUIDType';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment