<s> from __future__ import print_function <EOL> import logging <EOL> import time <EOL> from collections import defaultdict <EOL> from ccmlib . node import Node <EOL> from cassandra . query import named_tuple_factory <EOL> from tests . integration import get_node , get_cluster <EOL> IP_FORMAT = '<STR_LIT>' <EOL> log = logging . getLogger ( __name__ ) <EOL> class CoordinatorStats ( ) : <EOL> def __init__ ( self ) : <EOL> self . coordinator_counts = defaultdict ( int ) <EOL> def add_coordinator ( self , future ) : <EOL> future . result ( ) <EOL> coordinator = future . _current_host . address <EOL> self . coordinator_counts [ coordinator ] += <NUM_LIT:1> <EOL> if future . _errors : <EOL> log . error ( '<STR_LIT>' , future . _errors ) <EOL> def reset_counts ( self ) : <EOL> self . coordinator_counts = defaultdict ( int ) <EOL> def get_query_count ( self , node ) : <EOL> ip = '<STR_LIT>' % node <EOL> return self . coordinator_counts [ ip ] <EOL> def assert_query_count_equals ( self , testcase , node , expected ) : <EOL> ip = '<STR_LIT>' % node <EOL> if self . get_query_count ( node ) != expected : <EOL> testcase . fail ( '<STR_LIT>' % ( <EOL> expected , ip , self . coordinator_counts [ ip ] , dict ( self . coordinator_counts ) ) ) <EOL> def create_schema ( cluster , session , keyspace , simple_strategy = True , <EOL> replication_factor = <NUM_LIT:1> , replication_strategy = None ) : <EOL> row_factory = session . row_factory <EOL> session . row_factory = named_tuple_factory <EOL> if keyspace in cluster . metadata . keyspaces . keys ( ) : <EOL> session . execute ( '<STR_LIT>' % keyspace , timeout = <NUM_LIT:20> ) <EOL> if simple_strategy : <EOL> ddl = "<STR_LIT>" "<STR_LIT>" <EOL> session . execute ( ddl % ( keyspace , replication_factor ) , timeout = <NUM_LIT:10> ) <EOL> else : <EOL> if not replication_strategy : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> ddl = "<STR_LIT>" "<STR_LIT>" <EOL> session . execute ( ddl % ( keyspace , str ( replication_strategy ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] ) , timeout = <NUM_LIT:10> ) <EOL> ddl = '<STR_LIT>' <EOL> session . execute ( ddl % keyspace , timeout = <NUM_LIT:10> ) <EOL> session . execute ( '<STR_LIT>' % keyspace ) <EOL> session . row_factory = row_factory <EOL> def start ( node ) : <EOL> get_node ( node ) . start ( ) <EOL> def stop ( node ) : <EOL> get_node ( node ) . stop ( ) <EOL> def force_stop ( node ) : <EOL> log . debug ( "<STR_LIT>" , node ) <EOL> get_node ( node ) . stop ( wait = False , gently = False ) <EOL> log . debug ( "<STR_LIT>" , node ) <EOL> def decommission ( node ) : <EOL> get_node ( node ) . decommission ( ) <EOL> get_node ( node ) . stop ( ) <EOL> def bootstrap ( node , data_center = None , token = None ) : <EOL> node_instance = Node ( '<STR_LIT>' % node , <EOL> get_cluster ( ) , <EOL> auto_bootstrap = False , <EOL> thrift_interface = ( IP_FORMAT % node , <NUM_LIT> ) , <EOL> storage_interface = ( IP_FORMAT % node , <NUM_LIT> ) , <EOL> jmx_port = str ( <NUM_LIT> + <NUM_LIT:100> * node ) , <EOL> remote_debug_port = <NUM_LIT:0> , <EOL> initial_token = token if token else node * <NUM_LIT:10> ) <EOL> get_cluster ( ) . add ( node_instance , is_seed = False , data_center = data_center ) <EOL> try : <EOL> start ( node ) <EOL> except : <EOL> try : <EOL> start ( node ) <EOL> except : <EOL> log . error ( '<STR_LIT>' ) <EOL> def ring ( node ) : <EOL> print ( '<STR_LIT>' % node ) <EOL> get_node ( node ) . nodetool ( '<STR_LIT>' ) <EOL> def wait_for_up ( cluster , node , wait = True ) : <EOL> tries = <NUM_LIT:0> <EOL> while tries < <NUM_LIT:100> : <EOL> host = cluster . metadata . get_host ( IP_FORMAT % node ) <EOL> if host and host . is_up : <EOL> log . debug ( "<STR_LIT>" , node ) <EOL> return <EOL> else : <EOL> log . debug ( "<STR_LIT>" ) <EOL> tries += <NUM_LIT:1> <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> raise RuntimeError ( "<STR_LIT>" . format ( IP_FORMAT . format ( node ) ) ) <EOL> def wait_for_down ( cluster , node , wait = True ) : <EOL> log . debug ( "<STR_LIT>" , node ) <EOL> tries = <NUM_LIT:0> <EOL> while tries < <NUM_LIT:100> : <EOL> host = cluster . metadata . get_host ( IP_FORMAT % node ) <EOL> if not host or not host . is_up : <EOL> log . debug ( "<STR_LIT>" , node ) <EOL> return <EOL> else : <EOL> log . debug ( "<STR_LIT>" ) <EOL> tries += <NUM_LIT:1> <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> raise RuntimeError ( "<STR_LIT>" . format ( IP_FORMAT . format ( node ) ) ) </s>
