Skip to content

Commit d49f4dd

Browse files
Copilotmykaul
andcommitted
Refactor: simplify tablet check by removing redundant table_has_tablets call
Co-authored-by: mykaul <4655593+mykaul@users.noreply.github.com>
1 parent 85c6ee8 commit d49f4dd

2 files changed

Lines changed: 11 additions & 16 deletions

File tree

cassandra/policies.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -520,13 +520,12 @@ def make_query_plan(self, working_keyspace=None, query=None):
520520
child_plan = list(child.make_query_plan(keyspace, query))
521521

522522
replicas = []
523-
if self._cluster_metadata._tablets.table_has_tablets(keyspace, query.table):
524-
tablet = self._cluster_metadata._tablets.get_tablet_for_key(
523+
tablet = self._cluster_metadata._tablets.get_tablet_for_key(
525524
keyspace, query.table, self._cluster_metadata.token_map.token_class.from_key(query.routing_key))
526525

527-
if tablet is not None:
528-
replicas_mapped = set(map(lambda r: r[0], tablet.replicas))
529-
replicas = [host for host in child_plan if host.host_id in replicas_mapped]
526+
if tablet is not None:
527+
replicas_mapped = set(map(lambda r: r[0], tablet.replicas))
528+
replicas = [host for host in child_plan if host.host_id in replicas_mapped]
530529
else:
531530
replicas = self._cluster_metadata.get_replicas(keyspace, query.routing_key)
532531

tests/unit/test_policies.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ def test_wrap_round_robin(self):
584584
cluster = Mock(spec=Cluster)
585585
cluster.metadata = Mock(spec=Metadata)
586586
cluster.metadata._tablets = Mock(spec=Tablets)
587-
cluster.metadata._tablets.table_has_tablets.return_value = []
587+
cluster.metadata._tablets.get_tablet_for_key.return_value = None
588588
hosts = [Host(DefaultEndPoint(str(i)), SimpleConvictionPolicy) for i in range(4)]
589589
for host in hosts:
590590
host.set_up()
@@ -617,7 +617,7 @@ def test_wrap_dc_aware(self):
617617
cluster = Mock(spec=Cluster)
618618
cluster.metadata = Mock(spec=Metadata)
619619
cluster.metadata._tablets = Mock(spec=Tablets)
620-
cluster.metadata._tablets.table_has_tablets.return_value = []
620+
cluster.metadata._tablets.get_tablet_for_key.return_value = None
621621
hosts = [Host(DefaultEndPoint(str(i)), SimpleConvictionPolicy) for i in range(4)]
622622
for host in hosts:
623623
host.set_up()
@@ -666,7 +666,7 @@ def test_wrap_rack_aware(self):
666666
cluster = Mock(spec=Cluster)
667667
cluster.metadata = Mock(spec=Metadata)
668668
cluster.metadata._tablets = Mock(spec=Tablets)
669-
cluster.metadata._tablets.table_has_tablets.return_value = []
669+
cluster.metadata._tablets.get_tablet_for_key.return_value = None
670670
hosts = [Host(DefaultEndPoint(str(i)), SimpleConvictionPolicy) for i in range(8)]
671671
for host in hosts:
672672
host.set_up()
@@ -811,7 +811,7 @@ def test_statement_keyspace(self):
811811
cluster.metadata._tablets = Mock(spec=Tablets)
812812
replicas = hosts[2:]
813813
cluster.metadata.get_replicas.return_value = replicas
814-
cluster.metadata._tablets.table_has_tablets.return_value = []
814+
cluster.metadata._tablets.get_tablet_for_key.return_value = None
815815

816816
child_policy = Mock()
817817
child_policy.make_query_plan.return_value = hosts
@@ -904,7 +904,7 @@ def _prepare_cluster_with_vnodes(self):
904904
cluster.metadata._tablets = Mock(spec=Tablets)
905905
cluster.metadata.all_hosts.return_value = hosts
906906
cluster.metadata.get_replicas.return_value = hosts[2:]
907-
cluster.metadata._tablets.table_has_tablets.return_value = False
907+
cluster.metadata._tablets.get_tablet_for_key.return_value = None
908908
return cluster
909909

910910
def _prepare_cluster_with_tablets(self):
@@ -916,7 +916,6 @@ def _prepare_cluster_with_tablets(self):
916916
cluster.metadata._tablets = Mock(spec=Tablets)
917917
cluster.metadata.all_hosts.return_value = hosts
918918
cluster.metadata.get_replicas.return_value = hosts[2:]
919-
cluster.metadata._tablets.table_has_tablets.return_value = True
920919
cluster.metadata._tablets.get_tablet_for_key.return_value = Tablet(replicas=[(h.host_id, 0) for h in hosts[2:]])
921920
return cluster
922921

@@ -931,8 +930,6 @@ def _assert_shuffle(self, patched_shuffle, cluster, keyspace, routing_key):
931930
policy = TokenAwarePolicy(child_policy, shuffle_replicas=True)
932931
policy.populate(cluster, hosts)
933932

934-
is_tablets = cluster.metadata._tablets.table_has_tablets()
935-
936933
cluster.metadata.get_replicas.reset_mock()
937934
child_policy.make_query_plan.reset_mock()
938935
query = Statement(routing_key=routing_key)
@@ -964,7 +961,7 @@ def test_child_make_query_plan_called_once(self):
964961
cluster = Mock(spec=Cluster)
965962
cluster.metadata = Mock(spec=Metadata)
966963
cluster.metadata._tablets = Mock(spec=Tablets)
967-
cluster.metadata._tablets.table_has_tablets.return_value = False
964+
cluster.metadata._tablets.get_tablet_for_key.return_value = None # No tablets for this table
968965
replicas = hosts[2:]
969966
cluster.metadata.get_replicas.return_value = replicas
970967

@@ -996,7 +993,6 @@ def test_child_make_query_plan_called_once(self):
996993
child_policy.make_query_plan.assert_called_once_with(None, query)
997994

998995
# Test case 4: With tablets (should call once)
999-
cluster.metadata._tablets.table_has_tablets.return_value = True
1000996
tablet = Mock(spec=Tablet)
1001997
tablet.replicas = [(hosts[0].host_id, None), (hosts[1].host_id, None)]
1002998
cluster.metadata._tablets.get_tablet_for_key.return_value = tablet
@@ -1695,7 +1691,7 @@ def get_replicas(keyspace, packed_key):
16951691

16961692
cluster.metadata.get_replicas.side_effect = get_replicas
16971693
cluster.metadata._tablets = Mock(spec=Tablets)
1698-
cluster.metadata._tablets.table_has_tablets.return_value = []
1694+
cluster.metadata._tablets.get_tablet_for_key.return_value = None
16991695

17001696
child_policy = TokenAwarePolicy(RoundRobinPolicy())
17011697

0 commit comments

Comments
 (0)