Skip to content

Instantly share code, notes, and snippets.

@beltran
Last active March 21, 2017 19:08
Show Gist options
  • Save beltran/41999db20794dfbac91bff6264f032ff to your computer and use it in GitHub Desktop.
Save beltran/41999db20794dfbac91bff6264f032ff to your computer and use it in GitHub Desktop.
from cassandra import cluster as cassandra_cluster
from cassandra.concurrent import execute_concurrent
from itertools import cycle
from threading import Event
import logging
log = logging.getLogger()
log.setLevel('ERROR')
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter("%(asctime)s [%(levelname)s] %(name)s: %(message)s"))
log.addHandler(handler)
cluster = cassandra_cluster.Cluster()
session = cluster.connect()
session.execute('''
CREATE KEYSPACE IF NOT EXISTS test3rf
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '3'}''')
session.execute('''
CREATE TABLE IF NOT EXISTS test3rf.test (
k int PRIMARY KEY,
v int )''')
statements_and_params = zip(cycle(["INSERT INTO test3rf.test (k, v) VALUES (%s, 0)"]),
[(i,) for i in range(100)])
execute_concurrent(session, list(statements_and_params))
def handle_page(rows, future):
if future.has_more_pages:
future.start_fetching_next_page()
else:
event.set()
def handle_error(err):
event.set()
print("Error ocurred")
count = 0
while 1:
cluster = cassandra_cluster.Cluster(["127.0.0.1", "127.0.0.2", "127.0.0.3"])
session = cluster.connect()
session.default_fetch_size = 2
future = session.execute_async("SELECT * FROM test3rf.test", timeout=20)
event = Event()
future.add_callbacks(callback=handle_page, callback_args=(future, ), errback=handle_error)
event.wait()
count += 1
print ("Iteration {0} {1}".format(count, cluster.connection_class))
cluster.shutdown()
from tests.integration import use_cluster, get_node, use_singledc
import time
from random import randint
import sys
import os
use_singledc()
print("Start now the script")
time.sleep(10)
while 1:
print("Stopping nodes")
node_one = 1
node = get_node(node_one)
node_two = get_node(2)
node.stop()
node_two.stop()
time.sleep(3)
node.start()
node_two.start()
print("Nodes active now\n")
time.sleep(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment