def initialise(keyspace="indigo", hosts=('127.0.0.1', ), strategy='SimpleStrategy', repl_factor=1): """Initialise Cassandra connection""" num_retries = 6 retry_timeout = 1 for retry in xrange(num_retries): try: logger.info('Connecting to Cassandra keyspace "{2}" ' 'on "{0}" with strategy "{1}"'.format( hosts, strategy, keyspace)) connection.setup(hosts, keyspace, protocol_version=3) if strategy is 'SimpleStrategy': create_keyspace_simple(keyspace, repl_factor, True) else: create_keyspace_network_topology(keyspace, {}, True) break except dse.cluster.NoHostAvailable: logger.warning( 'Unable to connect to Cassandra. Retrying in {0} seconds...'. format(retry_timeout)) time.sleep(retry_timeout) retry_timeout *= 2
def connect(): """Connect to a Cassandra cluster. keyspace, hosts, strategy variables are used to configure the connection. See the cfg object in the :mod:`radon.model.config` module, . :return: A boolean which indicates if the connection is successful :rtype: bool""" num_retries = 5 retry_timeout = 2 keyspace = cfg.dse_keyspace hosts = cfg.dse_host strategy = (cfg.dse_strategy, ) for _ in range(num_retries): try: cfg.logger.info('Connecting to Cassandra keyspace "{2}" ' 'on "{0}" with strategy "{1}"'.format( hosts, strategy, keyspace)) profile = ExecutionProfile( load_balancing_policy=WhiteListRoundRobinPolicy(hosts)) profiles = {EXEC_PROFILE_DEFAULT: profile} connection.setup( hosts, keyspace, protocol_version=3, execution_profiles=profiles, ) return True except NoHostAvailable: cfg.logger.warning( "Unable to connect to Cassandra on {0}. Retrying in {1} seconds..." .format(hosts, retry_timeout)) time.sleep(retry_timeout) return False
def setup_connection(keyspace_name): connection.setup([DSE_IP], consistency=ConsistencyLevel.ONE, protocol_version=PROTOCOL_VERSION, default_keyspace=keyspace_name)
def test_connection_setup_with_setup(self): connection.setup(hosts=None, default_keyspace=None) self.assertIsNotNone( connection.get_connection("default").cluster.metadata.get_host( "127.0.0.1"))