Skip to content

Commit c569402

Browse files
committed
Invalidate tablets when table is dropped via schema event
1 parent e9773cd commit c569402

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

cassandra/metadata.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ def _drop_table(self, keyspace, table):
225225
except KeyError:
226226
# can happen if keyspace disappears while processing async event
227227
pass
228+
self._table_removed(keyspace, table)
228229

229230
def _update_type(self, type_meta):
230231
try:

tests/integration/standard/test_tablets.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,23 @@ def drop_ks(_):
218218

219219
self.run_tablets_invalidation_test(drop_ks)
220220

221+
def test_tablets_invalidation_drop_table(self):
222+
"""Dropping a table invalidates its tablet metadata via the schema change event."""
223+
224+
def drop_table(_):
225+
# Drop table to trigger tablets invalidation
226+
self.session.execute("DROP TABLE test1.table1")
227+
228+
try:
229+
self.run_tablets_invalidation_test(drop_table)
230+
finally:
231+
# Recreate the table for other tests
232+
self.session.execute(
233+
"""
234+
CREATE TABLE test1.table1 (pk int, ck int, v int, PRIMARY KEY (pk, ck));
235+
""")
236+
self.create_data(self.session)
237+
221238
@pytest.mark.last
222239
def test_tablets_invalidation_decommission_non_cc_node(self):
223240
def decommission_non_cc_node(rec):

tests/unit/test_metadata.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from cassandra.policies import SimpleConvictionPolicy
3737
from cassandra.pool import Host
3838
from cassandra.protocol import QueryMessage
39+
from cassandra.tablets import Tablet
3940
from tests.util import assertCountEqual
4041
import pytest
4142

@@ -441,6 +442,32 @@ def test_bytes_tokens(self):
441442
self._get_replicas(BytesToken)
442443

443444

445+
class DropTableMetadataTest(unittest.TestCase):
446+
"""Metadata._drop_table should invalidate tablets for the dropped table."""
447+
448+
def setUp(self):
449+
"""Set up metadata containing a table with a tablet record."""
450+
self.metadata = Metadata()
451+
keyspace = KeyspaceMetadata("ks", True, "NetworkTopologyStrategy", {"dc1": "1"})
452+
keyspace.tables["tb"] = TableMetadata("ks", "tb")
453+
self.metadata.keyspaces["ks"] = keyspace
454+
self.metadata._tablets.add_tablet("ks", "tb", Tablet(0, 100, [("host1", 0)]))
455+
456+
def test_drop_table_invalidates_tablets(self):
457+
"""Dropping a known table removes its tablet and table metadata."""
458+
self.metadata._drop_table("ks", "tb")
459+
460+
assert self.metadata._tablets.table_has_tablets("ks", "tb") is False
461+
assert "tb" not in self.metadata.keyspaces["ks"].tables
462+
463+
def test_drop_table_invalidates_tablets_for_unknown_keyspace(self):
464+
"""Dropping a table in an unknown keyspace still removes its tablet metadata."""
465+
self.metadata._tablets.add_tablet("unknown", "tb", Tablet(0, 100, [("host1", 0)]))
466+
self.metadata._drop_table("unknown", "tb")
467+
468+
assert self.metadata._tablets.table_has_tablets("unknown", "tb") is False
469+
470+
444471
class Murmur3TokensTest(unittest.TestCase):
445472

446473
def test_murmur3_init(self):

0 commit comments

Comments
 (0)