def test_schedule_infinite_attempts(self): delay = 2 max_attempts = None crp = ConstantReconnectionPolicy(delay=delay, max_attempts=max_attempts) # this is infinite. we'll just verify one more than default for _, d in zip(range(65), crp.new_schedule()): self.assertEqual(d, delay)
def test_schedule(self): """ Test ConstantReconnectionPolicy schedule """ delay = 2 max_attempts = 100 policy = ConstantReconnectionPolicy(delay=delay, max_attempts=max_attempts) schedule = list(policy.new_schedule()) self.assertEqual(len(schedule), max_attempts) for i, delay in enumerate(schedule): self.assertEqual(delay, delay)
def __init__(self): self.cluster = Cluster( contact_points=['127.0.0.1', '127.0.0.2'], default_retry_policy=DowngradingConsistencyRetryPolicy(), reconnection_policy=ConstantReconnectionPolicy(20.0, 10) ) self.session = self.cluster.connect()
class MockCluster(object): max_schema_agreement_wait = Cluster.max_schema_agreement_wait load_balancing_policy = RoundRobinPolicy() reconnection_policy = ConstantReconnectionPolicy(2) down_host = None def __init__(self): self.metadata = MockMetadata() self.added_hosts = [] self.removed_hosts = [] self.scheduler = Mock(spec=_Scheduler) self.executor = Mock(spec=ThreadPoolExecutor) def add_host(self, address, signal=False): host = Host(address, SimpleConvictionPolicy) self.added_hosts.append(host) return host def remove_host(self, host): self.removed_hosts.append(host) def on_up(self, host): pass def on_down(self, host, is_host_addition): self.down_host = host
def _local_connect(self): """ assumes single node Cassandra cluster """ connection.setup(hosts=self.hosts, default_keyspace=self.keyspace, consistency=CL.ONE, port=self.port, cql_version=self.cql_version, lazy_connect=False, retry_connect=True, compression=True, auth_provider=None, load_balancing_policy=RoundRobinPolicy(), protocol_version=4, executor_threads=2, reconnection_policy=ConstantReconnectionPolicy(3.0, 5), default_retry_policy=RetryPolicy(), conviction_policy_factory=None, metrics_enabled=False, connection_class=None, ssl_options=None, sockopts=None, max_schema_agreement_wait=10, control_connection_timeout=2.0, idle_heartbeat_interval=30, schema_event_refresh_window=2, topology_event_refresh_window=10, connect_timeout=self.connect_timeout) return
class MockCluster(object): max_schema_agreement_wait = 5 profile_manager = ProfileManager() reconnection_policy = ConstantReconnectionPolicy(2) address_translator = IdentityTranslator() down_host = None contact_points = [] is_shutdown = False def __init__(self): self.metadata = MockMetadata() self.added_hosts = [] self.removed_hosts = [] self.scheduler = Mock(spec=_Scheduler) self.executor = Mock(spec=ThreadPoolExecutor) self.profile_manager.profiles[EXEC_PROFILE_DEFAULT] = ExecutionProfile(RoundRobinPolicy()) def add_host(self, address, datacenter, rack, signal=False, refresh_nodes=True): host = Host(address, SimpleConvictionPolicy, datacenter, rack) self.added_hosts.append(host) return host def remove_host(self, host): self.removed_hosts.append(host) def on_up(self, host): pass def on_down(self, host, is_host_addition): self.down_host = host
def test_constant_reconnection_policy(self): self.assertEqual( insights_registry.serialize(ConstantReconnectionPolicy(3, 200)), {'type': 'ConstantReconnectionPolicy', 'namespace': 'cassandra.policies', 'options': {'delay': 3, 'max_attempts': 200} } )
def get_session() -> Session: auth = PlainTextAuthProvider(username=CASSANDRA_USERNAME, password=CASSANDRA_PASSWORD) rp = ConstantReconnectionPolicy(5, max_attempts=None) cluster = Cluster( contact_points=CASSANDRA_HOSTS, port=CASSANDRA_PORT, auth_provider=auth, reconnection_policy=rp, ) return cluster.connect()
def test_schedule_negative_max_attempts(self): """ Test how negative max_attempts are handled """ delay = 2 max_attempts = -100 try: ConstantReconnectionPolicy(delay=delay, max_attempts=max_attempts) self.fail('max_attempts should throw ValueError when negative') except ValueError: pass
def connect(self, *args, **kwargs): auth = PlainTextAuthProvider(username=self.user, password=self.password) cluster = Cluster(contact_points=self.host, auth_provider=auth, protocol_version=3, load_balancing_policy=default_lbp_factory(), default_retry_policy=RetryPolicy(), reconnection_policy=ConstantReconnectionPolicy( delay=1, max_attempts=10), *args, **kwargs) return cluster
def __init__(self, url, dbname, consistency=None): self.isShutdown = False self.url = url self.dbname = dbname self.nSuccess = 0 self.nErrors = 0 self.nActive = 0 self.consistency = ConsistencyLevel.ONE if consistency is None else consistency self.cluster = Cluster( url, control_connection_timeout=30.0, reconnection_policy=ConstantReconnectionPolicy(15.0, max_attempts=None), default_retry_policy=ConstantSpeculativeExecutionPolicy(30, 10)) self.cur = self.cluster.connect(dbname) self.cur.default_timeout = 60.0
def __init__(self, config: AppConfig): self.cfg = config hosts = self.cfg.CAS_HOST.split(',') auth_provider = PlainTextAuthProvider(self.cfg.CAS_USER, self.cfg.CAS_PWD) reconnection_policy = ConstantReconnectionPolicy(delay=2.5, max_attempts=None) balancing_policy = WhiteListRoundRobinPolicy(hosts) self.cluster = Cluster( hosts, load_balancing_policy=balancing_policy, auth_provider=auth_provider, reconnection_policy=reconnection_policy, ) self.connect() self.prepared_statements = {}
def _get_session(self) -> Session: if self._username and self._password: auth_provider = PlainTextAuthProvider(username=self._username, password=self._password) else: auth_provider = None cluster = Cluster( contact_points=self._contact_points, port=self._port, # load_balancing_policy= reconnection_policy=ConstantReconnectionPolicy(delay=5.0), default_retry_policy=RetryPolicy(), auth_provider=auth_provider) session = cluster.connect() session.set_keyspace('nexustiles') return session
def test_resolve_and_reconnect_on_node_down(self): self.connect(self.creds, idle_heartbeat_interval=1, idle_heartbeat_timeout=1, reconnection_policy=ConstantReconnectionPolicy(120)) self.assertEqual(len(self.hosts_up()), 3) CLOUD_PROXY_SERVER.stop_node(1) wait_until_not_raised( lambda: self.assertEqual(len(self.hosts_up()), 2), 0.02, 250) host = [h for h in self.cluster.metadata.all_hosts() if not h.is_up][0] with patch.object(SniEndPoint, "resolve", wraps=host.endpoint.resolve) as mocked_resolve: CLOUD_PROXY_SERVER.start_node(1) wait_until_not_raised( lambda: self.assertEqual(len(self.hosts_up()), 3), 0.02, 250) mocked_resolve.assert_called_once()
def _connect_to_cassandra(keyspace): """ Connect to the Cassandra cluster and return the session. """ if 'BACKEND_STORAGE_IP' in os.environ: host = os.environ['BACKEND_STORAGE_IP'] else: host = '127.0.0.1' retry=RetryPolicy() retry.RETRY=10 cluster = Cluster( ['127.0.0.1',host,], reconnection_policy=ConstantReconnectionPolicy(5.0, 100), load_balancing_policy=DCAwareRoundRobinPolicy(local_dc="ASH2"), default_retry_policy=retry ) session = cluster.connect(keyspace) session.default_timeout = 99999 session.default_fetch_size = 1000 return session
# coding: utf8 from cassandra.cluster import Cluster from cassandra.policies import ConstantReconnectionPolicy, DCAwareRoundRobinPolicy from json import dumps from kafka import KafkaProducer import time import io import socket # $ sudo pip install python-dateutil cluster = Cluster( contact_points=['192.168.1.233', '192.168.1.77', '192.168.1.27'], idle_heartbeat_interval=5, load_balancing_policy=DCAwareRoundRobinPolicy(), reconnection_policy=ConstantReconnectionPolicy(delay=5, max_attempts=50), idle_heartbeat_timeout=5) cluster.connect_timeout = 30 session = cluster.connect('temperature') sleep_time = 1 #in seconds # pip install kafka-python producer = KafkaProducer(bootstrap_servers=['192.168.1.167:9092'], value_serializer=lambda x: dumps(x).encode('utf-8')) # test # /test # main endless loop
def setup_class(cls): cls.cluster = Cluster( protocol_version=4, load_balancing_policy=TokenAwarePolicy(RoundRobinPolicy()), reconnection_policy=ConstantReconnectionPolicy(1)) cls.session = cls.cluster.connect()
from cassandra.cluster import Cluster, ExecutionProfile, EXEC_PROFILE_DEFAULT from cassandra.auth import PlainTextAuthProvider from cassandra import ReadTimeout from cassandra.query import BatchStatement, SimpleStatement from cassandra.policies import WhiteListRoundRobinPolicy, ConstantReconnectionPolicy import uuid from cassandra.cqlengine import columns from cassandra.cqlengine import connection from datetime import datetime from cassandra.cqlengine.management import sync_table from cassandra.cqlengine.models import Model cloud_config = {'secure_connect_bundle': 'secure-connect-elko.zip'} auth_provider = PlainTextAuthProvider('elko', 'elkoelko') profile = ExecutionProfile(retry_policy=ConstantReconnectionPolicy(delay=10), request_timeout=15) cluster = Cluster(cloud=cloud_config, auth_provider=auth_provider, execution_profiles={'EXEC_PROFILE_DEFAULT': profile}) session = cluster.connect() row = session.execute("select release_version from system.local").all() if row: print(row[0]) else: print("An error occurred.") # Executing Queries session.set_keyspace('elko')
# coding: utf8 from cassandra.cluster import Cluster from cassandra.policies import ConstantReconnectionPolicy, DCAwareRoundRobinPolicy import subprocess import lcddriver import time import csv import sys import datetime as dt import dateutil.parser import os # Settings cluster = Cluster(contact_points=['192.168.1.20'], idle_heartbeat_interval=5,load_balancing_policy=DCAwareRoundRobinPolicy(), reconnection_policy=ConstantReconnectionPolicy(delay=5, max_attempts=50), idle_heartbeat_timeout=5) cluster.connect_timeout = 30 session = cluster.connect('stock') display = lcddriver.lcd() checked_quotes_last_time = 0 checked_frequency = 60 * 60 # in seconds lang = "en" #lang = "pl" messages = { "curr_en": "$", "curr_pl": "zl", "sum_en": "Sum", "sum_pl": "Razem", "stock_en": "Stock", "stock_pl": "Akcje",