3636from cassandra .connection import DefaultEndPoint , UnixSocketEndPoint
3737from cassandra .pool import Host
3838from cassandra .query import Statement
39+ from cassandra .tablets import Tablets , Tablet
3940
4041
4142class LoadBalancingPolicyTest (unittest .TestCase ):
@@ -582,7 +583,8 @@ class TokenAwarePolicyTest(unittest.TestCase):
582583 def test_wrap_round_robin (self ):
583584 cluster = Mock (spec = Cluster )
584585 cluster .metadata = Mock (spec = Metadata )
585- cluster .control_connection ._tablets_routing_v1 = False
586+ cluster .metadata ._tablets = Mock (spec = Tablets )
587+ cluster .metadata ._tablets .table_has_tablets .return_value = []
586588 hosts = [Host (DefaultEndPoint (str (i )), SimpleConvictionPolicy ) for i in range (4 )]
587589 for host in hosts :
588590 host .set_up ()
@@ -614,7 +616,8 @@ def get_replicas(keyspace, packed_key):
614616 def test_wrap_dc_aware (self ):
615617 cluster = Mock (spec = Cluster )
616618 cluster .metadata = Mock (spec = Metadata )
617- cluster .control_connection ._tablets_routing_v1 = False
619+ cluster .metadata ._tablets = Mock (spec = Tablets )
620+ cluster .metadata ._tablets .table_has_tablets .return_value = []
618621 hosts = [Host (DefaultEndPoint (str (i )), SimpleConvictionPolicy ) for i in range (4 )]
619622 for host in hosts :
620623 host .set_up ()
@@ -744,9 +747,10 @@ def test_statement_keyspace(self):
744747
745748 cluster = Mock (spec = Cluster )
746749 cluster .metadata = Mock (spec = Metadata )
747- cluster .control_connection . _tablets_routing_v1 = False
750+ cluster .metadata . _tablets = Mock ( spec = Tablets )
748751 replicas = hosts [2 :]
749752 cluster .metadata .get_replicas .return_value = replicas
753+ cluster .metadata ._tablets .table_has_tablets .return_value = []
750754
751755 child_policy = Mock ()
752756 child_policy .make_query_plan .return_value = hosts
@@ -803,7 +807,8 @@ def test_shuffles_if_given_keyspace_and_routing_key(self):
803807
804808 @test_category policy
805809 """
806- self ._assert_shuffle (keyspace = 'keyspace' , routing_key = 'routing_key' )
810+ self ._assert_shuffle (cluster = self ._prepare_cluster_with_vnodes (), keyspace = 'keyspace' , routing_key = 'routing_key' )
811+ self ._assert_shuffle (cluster = self ._prepare_cluster_with_tablets (), keyspace = 'keyspace' , routing_key = 'routing_key' )
807812
808813 def test_no_shuffle_if_given_no_keyspace (self ):
809814 """
@@ -814,7 +819,8 @@ def test_no_shuffle_if_given_no_keyspace(self):
814819
815820 @test_category policy
816821 """
817- self ._assert_shuffle (keyspace = None , routing_key = 'routing_key' )
822+ self ._assert_shuffle (cluster = self ._prepare_cluster_with_vnodes (), keyspace = None , routing_key = 'routing_key' )
823+ self ._assert_shuffle (cluster = self ._prepare_cluster_with_tablets (), keyspace = None , routing_key = 'routing_key' )
818824
819825 def test_no_shuffle_if_given_no_routing_key (self ):
820826 """
@@ -825,27 +831,47 @@ def test_no_shuffle_if_given_no_routing_key(self):
825831
826832 @test_category policy
827833 """
828- self ._assert_shuffle (keyspace = 'keyspace' , routing_key = None )
834+ self ._assert_shuffle (cluster = self ._prepare_cluster_with_vnodes (), keyspace = 'keyspace' , routing_key = None )
835+ self ._assert_shuffle (cluster = self ._prepare_cluster_with_tablets (), keyspace = 'keyspace' , routing_key = None )
829836
830- @patch ('cassandra.policies.shuffle' )
831- def _assert_shuffle (self , patched_shuffle , keyspace , routing_key ):
837+ def _prepare_cluster_with_vnodes (self ):
832838 hosts = [Host (DefaultEndPoint (str (i )), SimpleConvictionPolicy ) for i in range (4 )]
833839 for host in hosts :
834840 host .set_up ()
841+ cluster = Mock (spec = Cluster )
842+ cluster .metadata = Mock (spec = Metadata )
843+ cluster .metadata ._tablets = Mock (spec = Tablets )
844+ cluster .metadata .all_hosts .return_value = hosts
845+ cluster .metadata .get_replicas .return_value = hosts [2 :]
846+ cluster .metadata ._tablets .table_has_tablets .return_value = False
847+ return cluster
835848
849+ def _prepare_cluster_with_tablets (self ):
850+ hosts = [Host (DefaultEndPoint (str (i )), SimpleConvictionPolicy ) for i in range (4 )]
851+ for host in hosts :
852+ host .set_up ()
836853 cluster = Mock (spec = Cluster )
837854 cluster .metadata = Mock (spec = Metadata )
838- cluster .control_connection ._tablets_routing_v1 = False
839- replicas = hosts [2 :]
840- cluster .metadata .get_replicas .return_value = replicas
855+ cluster .metadata ._tablets = Mock (spec = Tablets )
856+ cluster .metadata .all_hosts .return_value = hosts
857+ cluster .metadata .get_replicas .return_value = hosts [2 :]
858+ cluster .metadata ._tablets .table_has_tablets .return_value = True
859+ cluster .metadata ._tablets .get_tablet_for_key .return_value = Tablet (replicas = [(h .host_id , 0 ) for h in hosts [2 :]])
860+ return cluster
841861
862+ @patch ('cassandra.policies.shuffle' )
863+ def _assert_shuffle (self , patched_shuffle , cluster , keyspace , routing_key ):
864+ hosts = cluster .metadata .all_hosts ()
865+ replicas = cluster .metadata .get_replicas ()
842866 child_policy = Mock ()
843867 child_policy .make_query_plan .return_value = hosts
844868 child_policy .distance .return_value = HostDistance .LOCAL
845869
846870 policy = TokenAwarePolicy (child_policy , shuffle_replicas = True )
847871 policy .populate (cluster , hosts )
848872
873+ is_tablets = cluster .metadata ._tablets .table_has_tablets ()
874+
849875 cluster .metadata .get_replicas .reset_mock ()
850876 child_policy .make_query_plan .reset_mock ()
851877 query = Statement (routing_key = routing_key )
@@ -858,7 +884,11 @@ def _assert_shuffle(self, patched_shuffle, keyspace, routing_key):
858884 else :
859885 assert set (replicas ) == set (qplan [:2 ])
860886 assert hosts [:2 ] == qplan [2 :]
861- child_policy .make_query_plan .assert_called_once_with (keyspace , query )
887+ if is_tablets :
888+ child_policy .make_query_plan .assert_called_with (keyspace , query )
889+ assert child_policy .make_query_plan .call_count == 2
890+ else :
891+ child_policy .make_query_plan .assert_called_once_with (keyspace , query )
862892 assert patched_shuffle .call_count == 1
863893
864894
@@ -1538,7 +1568,6 @@ def test_query_plan_deferred_to_child(self):
15381568
15391569 def test_wrap_token_aware (self ):
15401570 cluster = Mock (spec = Cluster )
1541- cluster .control_connection ._tablets_routing_v1 = False
15421571 hosts = [Host (DefaultEndPoint ("127.0.0.{}" .format (i )), SimpleConvictionPolicy ) for i in range (1 , 6 )]
15431572 for host in hosts :
15441573 host .set_up ()
@@ -1547,6 +1576,8 @@ def get_replicas(keyspace, packed_key):
15471576 return hosts [:2 ]
15481577
15491578 cluster .metadata .get_replicas .side_effect = get_replicas
1579+ cluster .metadata ._tablets = Mock (spec = Tablets )
1580+ cluster .metadata ._tablets .table_has_tablets .return_value = []
15501581
15511582 child_policy = TokenAwarePolicy (RoundRobinPolicy ())
15521583
0 commit comments