Skip to content

Commit 472461c

Browse files
mykauldkropachev
authored andcommitted
(improvement)TokenAwarePolicy::make_query_plan(): remove redundant check if a table is using tablets
table_has_tablets() performs the same self._tablets.get((keyspace, table) that get_tablet_for_key() does which is a called a line later does, so it's redundant. Removed the former. Note - perhaps table_has_tablets() is not needed and can be removed? Unsure, it's unclear if it's part of the API or not. It's now completely unused across the code. Adjusted unit tests as well. Signed-off-by: Yaniv Kaul <yaniv.kaul@scylladb.com>
1 parent a0cde2e commit 472461c

2 files changed

Lines changed: 12 additions & 14 deletions

File tree

cassandra/policies.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -503,15 +503,14 @@ def make_query_plan(self, working_keyspace=None, query=None):
503503
return
504504

505505
replicas = []
506-
if self._cluster_metadata._tablets.table_has_tablets(keyspace, query.table):
507-
tablet = self._cluster_metadata._tablets.get_tablet_for_key(
506+
tablet = self._cluster_metadata._tablets.get_tablet_for_key(
508507
keyspace, query.table, self._cluster_metadata.token_map.token_class.from_key(query.routing_key))
509508

510-
if tablet is not None:
511-
replicas_mapped = set(map(lambda r: r[0], tablet.replicas))
512-
child_plan = child.make_query_plan(keyspace, query)
509+
if tablet is not None:
510+
replicas_mapped = set(map(lambda r: r[0], tablet.replicas))
511+
child_plan = child.make_query_plan(keyspace, query)
513512

514-
replicas = [host for host in child_plan if host.host_id in replicas_mapped]
513+
replicas = [host for host in child_plan if host.host_id in replicas_mapped]
515514
else:
516515
replicas = self._cluster_metadata.get_replicas(keyspace, query.routing_key)
517516

tests/unit/test_policies.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ def test_wrap_round_robin(self):
576576
cluster = Mock(spec=Cluster)
577577
cluster.metadata = Mock(spec=Metadata)
578578
cluster.metadata._tablets = Mock(spec=Tablets)
579-
cluster.metadata._tablets.table_has_tablets.return_value = []
579+
cluster.metadata._tablets.get_tablet_for_key.return_value = None
580580
hosts = [Host(DefaultEndPoint(str(i)), SimpleConvictionPolicy, host_id=uuid.uuid4()) for i in range(4)]
581581
for host in hosts:
582582
host.set_up()
@@ -609,7 +609,7 @@ def test_wrap_dc_aware(self):
609609
cluster = Mock(spec=Cluster)
610610
cluster.metadata = Mock(spec=Metadata)
611611
cluster.metadata._tablets = Mock(spec=Tablets)
612-
cluster.metadata._tablets.table_has_tablets.return_value = []
612+
cluster.metadata._tablets.get_tablet_for_key.return_value = None
613613
hosts = [Host(DefaultEndPoint(str(i)), SimpleConvictionPolicy, host_id=uuid.uuid4()) for i in range(4)]
614614
for host in hosts:
615615
host.set_up()
@@ -658,7 +658,7 @@ def test_wrap_rack_aware(self):
658658
cluster = Mock(spec=Cluster)
659659
cluster.metadata = Mock(spec=Metadata)
660660
cluster.metadata._tablets = Mock(spec=Tablets)
661-
cluster.metadata._tablets.table_has_tablets.return_value = []
661+
cluster.metadata._tablets.get_tablet_for_key.return_value = None
662662
hosts = [Host(DefaultEndPoint(str(i)), SimpleConvictionPolicy, host_id=uuid.uuid4()) for i in range(8)]
663663
for host in hosts:
664664
host.set_up()
@@ -803,7 +803,7 @@ def test_statement_keyspace(self):
803803
cluster.metadata._tablets = Mock(spec=Tablets)
804804
replicas = hosts[2:]
805805
cluster.metadata.get_replicas.return_value = replicas
806-
cluster.metadata._tablets.table_has_tablets.return_value = []
806+
cluster.metadata._tablets.get_tablet_for_key.return_value = None
807807

808808
child_policy = Mock()
809809
child_policy.make_query_plan.return_value = hosts
@@ -896,7 +896,7 @@ def _prepare_cluster_with_vnodes(self):
896896
cluster.metadata._tablets = Mock(spec=Tablets)
897897
cluster.metadata.all_hosts.return_value = hosts
898898
cluster.metadata.get_replicas.return_value = hosts[2:]
899-
cluster.metadata._tablets.table_has_tablets.return_value = False
899+
cluster.metadata._tablets.get_tablet_for_key.return_value = None
900900
return cluster
901901

902902
def _prepare_cluster_with_tablets(self):
@@ -908,7 +908,6 @@ def _prepare_cluster_with_tablets(self):
908908
cluster.metadata._tablets = Mock(spec=Tablets)
909909
cluster.metadata.all_hosts.return_value = hosts
910910
cluster.metadata.get_replicas.return_value = hosts[2:]
911-
cluster.metadata._tablets.table_has_tablets.return_value = True
912911
cluster.metadata._tablets.get_tablet_for_key.return_value = Tablet(replicas=[(h.host_id, 0) for h in hosts[2:]])
913912
return cluster
914913

@@ -923,7 +922,7 @@ def _assert_shuffle(self, patched_shuffle, cluster, keyspace, routing_key):
923922
policy = TokenAwarePolicy(child_policy, shuffle_replicas=True)
924923
policy.populate(cluster, hosts)
925924

926-
is_tablets = cluster.metadata._tablets.table_has_tablets()
925+
is_tablets = cluster.metadata._tablets.get_tablet_for_key() is not None
927926

928927
cluster.metadata.get_replicas.reset_mock()
929928
child_policy.make_query_plan.reset_mock()
@@ -1630,7 +1629,7 @@ def get_replicas(keyspace, packed_key):
16301629

16311630
cluster.metadata.get_replicas.side_effect = get_replicas
16321631
cluster.metadata._tablets = Mock(spec=Tablets)
1633-
cluster.metadata._tablets.table_has_tablets.return_value = []
1632+
cluster.metadata._tablets.get_tablet_for_key.return_value = None
16341633

16351634
child_policy = TokenAwarePolicy(RoundRobinPolicy())
16361635

0 commit comments

Comments
 (0)