forked from datastax/python-driver
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RackAwareRoundRobinPolicy for host selection
- Loading branch information
1 parent
c9b24b7
commit bc88863
Showing
6 changed files
with
313 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import logging | ||
import unittest | ||
|
||
from cassandra.cluster import Cluster | ||
from cassandra.policies import ConstantReconnectionPolicy, RackAwareRoundRobinPolicy | ||
|
||
from tests.integration import PROTOCOL_VERSION, get_cluster, use_multidc | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
def setup_module(): | ||
use_multidc({'DC1': {'RC1': 2, 'RC2': 2}, 'DC2': {'RC1': 2}}) | ||
|
||
class RackAwareRoundRobinPolicyTests(unittest.TestCase): | ||
@classmethod | ||
def setup_class(cls): | ||
cls.cluster = Cluster(contact_points=[node.address() for node in get_cluster().nodelist()], protocol_version=PROTOCOL_VERSION, | ||
load_balancing_policy=RackAwareRoundRobinPolicy("DC1", "RC1", used_hosts_per_remote_dc=0), | ||
reconnection_policy=ConstantReconnectionPolicy(1)) | ||
cls.session = cls.cluster.connect() | ||
cls.create_ks_and_cf(cls) | ||
cls.create_data(cls.session) | ||
|
||
@classmethod | ||
def teardown_class(cls): | ||
cls.cluster.shutdown() | ||
|
||
def create_ks_and_cf(self): | ||
self.session.execute( | ||
""" | ||
DROP KEYSPACE IF EXISTS test1 | ||
""" | ||
) | ||
self.session.execute( | ||
""" | ||
CREATE KEYSPACE test1 | ||
WITH replication = { | ||
'class': 'NetworkTopologyStrategy', | ||
'replication_factor': 1 | ||
} | ||
""") | ||
|
||
self.session.execute( | ||
""" | ||
CREATE TABLE test1.table1 (pk int, ck int, v int, PRIMARY KEY (pk, ck)); | ||
""") | ||
|
||
@staticmethod | ||
def create_data(session): | ||
prepared = session.prepare( | ||
""" | ||
INSERT INTO test1.table1 (pk, ck, v) VALUES (?, ?, ?) | ||
""") | ||
|
||
for i in range(50): | ||
bound = prepared.bind((i, i%5, i%2)) | ||
session.execute(bound) | ||
|
||
def get_hosts_from_tracing(self, results): | ||
traces = results.get_query_trace() | ||
events = traces.events | ||
host_set = set() | ||
for event in events: | ||
LOGGER.info("TRACE EVENT: %s %s %s", event.source, event.thread_name, event.description) | ||
host_set.add(event.source) | ||
|
||
trace_id = results.response_future.get_query_trace_ids()[0] | ||
traces = self.session.execute("SELECT * FROM system_traces.events WHERE session_id = %s", (trace_id,)) | ||
events = [event for event in traces] | ||
host_set = set() | ||
for event in events: | ||
LOGGER.info("TRACE EVENT: %s %s", event.source, event.activity) | ||
host_set.add(event.source) | ||
|
||
return host_set | ||
|
||
def test_rack_aware(self): | ||
prepared = self.session.prepare( | ||
""" | ||
SELECT pk, ck, v FROM test1.table1 WHERE pk = ? | ||
""") | ||
for i in range(20): | ||
bound = prepared.bind([(i)]) | ||
results = self.session.execute(bound, trace=True) | ||
self.assertEqual(results, [(i, i%5, i%2)]) | ||
queried_hosts = self.get_hosts_from_tracing(results) | ||
self.assertTrue(queried_hosts.issubset(set(["127.0.0.1", "127.0.0.2", "127.0.0.3", "127.0.0.4"]))) |
Oops, something went wrong.