|
| 1 | +from threading import RLock |
| 2 | + |
| 3 | +from policies import RoundRobinPolicy, SimpleConvictionPolicy, ExponentialReconnectionPolicy |
| 4 | +from connection import Connection |
| 5 | + |
| 6 | +class Session(object): |
| 7 | + |
| 8 | + def __init__(self, cluster, hosts): |
| 9 | + self.cluster = cluster |
| 10 | + self.hosts = hosts |
| 11 | + |
| 12 | + self._lock = RLock() |
| 13 | + self._is_shutdown = False |
| 14 | + self._pools = {} |
| 15 | + self._load_balancer = RoundRobinPolicy() |
| 16 | + |
| 17 | + def execute(self, query): |
| 18 | + pass |
| 19 | + |
| 20 | + def execute_async(self, query): |
| 21 | + pass |
| 22 | + |
| 23 | + def prepare(self, query): |
| 24 | + pass |
| 25 | + |
| 26 | + def shutdown(self): |
| 27 | + pass |
| 28 | + |
| 29 | + |
| 30 | +class Cluster(object): |
| 31 | + |
| 32 | + port = 9042 |
| 33 | + |
| 34 | + auth_provider = None |
| 35 | + |
| 36 | + load_balancing_policy = None |
| 37 | + reconnecting_policy = None |
| 38 | + retry_policy = None |
| 39 | + |
| 40 | + compression = None |
| 41 | + metrics_enabled = False |
| 42 | + pooling_options = None |
| 43 | + socket_options = None |
| 44 | + |
| 45 | + def __init__(self, contact_points): |
| 46 | + self.contact_points = contact_points |
| 47 | + self.sessions = set() |
| 48 | + self.metadata = None |
| 49 | + self.conviction_policy_factory = SimpleConvictionPolicy |
| 50 | + |
| 51 | + self._is_shutdown = False |
| 52 | + |
| 53 | + def connect(keyspace=None): |
| 54 | + return Session() |
| 55 | + |
| 56 | + def shutdown(): |
| 57 | + pass |
| 58 | + |
| 59 | + |
| 60 | +class NoHostAvailable(Exception): |
| 61 | + pass |
| 62 | + |
| 63 | + |
| 64 | +class ControlConnection(object): |
| 65 | + |
| 66 | + def __init__(self, cluster, metadata): |
| 67 | + self._cluster = cluster |
| 68 | + self._balancing_policy = RoundRobinPolicy() |
| 69 | + self._balancing_policy.populate(cluster, metadata.hosts) |
| 70 | + self._reconnection_policy = ExponentialReconnectionPolicy(2 * 1000, 5 * 60 * 1000) |
| 71 | + self._connection = None |
| 72 | + |
| 73 | + self._is_shutdown = False |
| 74 | + |
| 75 | + def connect(self): |
| 76 | + if self._is_shutdown: |
| 77 | + return |
| 78 | + |
| 79 | + def _reconnect(self): |
| 80 | + errors = {} |
| 81 | + for host in self._balancing_policy: |
| 82 | + try: |
| 83 | + return self._connect_to(host) |
| 84 | + except Exception, exc: |
| 85 | + # TODO logging, catch particular exception types |
| 86 | + errors[host] = exc |
| 87 | + pass |
| 88 | + |
| 89 | + raise NoHostAvailable("Unable to connect to any servers", errors) |
| 90 | + |
| 91 | + def _connect_to(self, host): |
| 92 | + # TODO create with cluster connection factory |
| 93 | + # connection = self._cluster.connection_factory.open(host) |
| 94 | + connection = Connection(host) |
| 95 | + |
| 96 | + def shutdown(self): |
| 97 | + self._is_shutdown = True |
| 98 | + if self._connection: |
| 99 | + self._connection.close() |
| 100 | + |
| 101 | + def refresh_schema(self, keyspace=None, table=None): |
| 102 | + pass |
| 103 | + |
| 104 | + def refresh_node_list_and_token_map(self): |
| 105 | + pass |
0 commit comments