Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
Checking mergeability… Don’t worry, you can still create the pull request.
  • 1 commit
  • 1 file changed
  • 0 commit comments
  • 1 contributor
Commits on Mar 29, 2013
@shader shader Add basic host auto-detection to ConnectionPool. To enable, init Conn…
…ectionPool with auto_detect=True
887822e
Showing with 28 additions and 2 deletions.
  1. +28 −2 pycassa/pool.py
View
30 pycassa/pool.py
@@ -17,7 +17,7 @@
default_transport_factory)
from logging.pool_logger import PoolLogger
from util import as_interface
-from cassandra.ttypes import TimedOutException, UnavailableException
+from cassandra.ttypes import TimedOutException, UnavailableException, Compression, ConsistencyLevel
_BASE_BACKOFF = 0.01
@@ -277,6 +277,7 @@ def __init__(self, keyspace,
prefill=True,
socket_factory=default_socket_factory,
transport_factory=default_transport_factory,
+ auto_detect=False,
**kwargs):
"""
All connections in the pool will be opened to `keyspace`.
@@ -320,6 +321,9 @@ def __init__(self, keyspace,
If `prefill` is set to ``True``, `pool_size` connections will be opened
when the pool is created.
+ If `auto_detect` is set to ``True``, the connection pool will automatically try to
+ connect to nodes that are peers of the hosts in the server_list
+
Example Usage:
.. code-block:: python
@@ -376,12 +380,34 @@ def __init__(self, keyspace,
if kw in kwargs:
setattr(self, kw, kwargs[kw])
- self.set_server_list(server_list)
+ if auto_detect:
+ self.set_server_list(self.detect_nodes(server_list))
+ else:
+ self.set_server_list(server_list)
self._prefill = prefill
if self._prefill:
self.fill()
+ def detect_nodes(self, seed_list):
+ nodes = set(seed_list)
+ for seed in seed_list:
+ nodes = nodes.union(self.get_peers(seed))
+ return nodes
+
+ def get_peers(self, seed):
+ conn = self._get_new_wrapper(seed)
+ result = conn.execute_cql3_query('select * from system.peers', Compression.NONE, ConsistencyLevel.ONE)
+ try:
+ peers = [socket.inet_ntoa(col.value)
+ for row in result.rows
+ for col in row.columns
+ if col.name == 'peer']
+ except Exception as e:
+ peers = []
+ self.put(conn)
+ return peers
+
def set_server_list(self, server_list):
"""
Sets the server list that the pool will make connections to.

No commit comments for this range

Something went wrong with that request. Please try again.