Bug: Using limit in TWCS sorted table doesn't work
Status: Triage Needed
Priority: Normal
Created: 30/Nov/20 15:47

Cassandra version: 3.11.6, (checked on 3.11.9 as well). A 7 nodes cluster running on Kubernetes.

We have a table that uses to sort sessions by username and app_id. The table uses TWCS and saves the data for 60 days as follows: 
{code:java}
@cqlsh> DESCRIBE sorted_sessions_by_username;
CREATE TABLE securedtouch.sorted_sessions_by_username (
 app_id text,
 username text,
 start_timestamp bigint,
 session_id text,
 PRIMARY KEY ((app_id, username), start_timestamp, session_id)
) WITH CLUSTERING ORDER BY (start_timestamp DESC, session_id ASC)
 AND bloom_filter_fp_chance = 0.01
 AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
 AND comment = ''
 AND compaction = {'class': 'org.apache.cassandra.db.compaction.TimeWindowCompactionStrategy', 'compaction_window_size': '4', 'compaction_window_unit': 'DAYS', 'max_threshold': '32', 'min_threshold': '2'}
 AND compression = {'chunk_length_in_kb': '4', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
 AND crc_check_chance = 1.0
 AND dclocal_read_repair_chance = 0.0
 AND default_time_to_live = 5184000
 AND gc_grace_seconds = 10800
 AND max_index_interval = 2048
 AND memtable_flush_period_in_ms = 0
 AND min_index_interval = 128
 AND read_repair_chance = 0.0
 AND speculative_retry = '99PERCENTILE';
{code}
 

With tracing on, I'm going to insert a new app_id and username into the table:

 
{code:java}
@cqlsh> INSERT INTO sorted_sessions_by_username (app_id, username, start_timestamp, session_id) values ('bug_test_app_id', 'bug_test_username', 
123456, 'bug_test_session_id');{code}
 

 

And now I'll query for it like that:

 
{code:java}
@cqlsh> SELECT * FROM sorted_sessions_by_username WHERE app_id = 'bug_test_app_id' and username = 'bug_test_username' LIMIT 1;
app_id | username | start_timestamp | session_id
-----------------+-------------------+-----------------+---------------------
 bug_test_app_id | bug_test_username | 123456 | bug_test_session_id
(1 rows)
Tracing session: 4be251c0-3322-11eb-ba1d-cdd072748f27
activity | timestamp | source | source_elapsed | client
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------+------------+----------------+-----------
 Execute CQL3 query | 2020-11-30 15:39:55.996000 | 10.0.2.30 | 0 | 127.0.0.1
 Parsing SELECT * FROM sorted_sessions_by_username WHERE app_id = 'bug_test_app_id' and username = 'bug_test_username' LIMIT 1; [Native-Transport-Requests-7] | 2020-11-30 15:39:55.997000 | 10.0.2.30 | 678 | 127.0.0.1
 Preparing statement [Native-Transport-Requests-7] | 2020-11-30 15:39:55.997000 | 10.0.2.30 | 852 | 127.0.0.1
 reading data from /10.0.0.103 [Native-Transport-Requests-7] | 2020-11-30 15:39:55.997000 | 10.0.2.30 | 1126 | 127.0.0.1
 speculating read retry on /10.2.42.36 [Native-Transport-Requests-7] | 2020-11-30 15:39:55.997000 | 10.0.2.30 | 1272 | 127.0.0.1
 Sending READ message to cassandra-1.cassandra.default.svc.cluster.local/10.0.0.103 [MessagingService-Outgoing-cassandra-1.cassandra.default.svc.cluster.local/10.0.0.103-Small] | 2020-11-30 15:39:55.997000 | 10.0.2.30 | 1322 | 127.0.0.1
 Sending READ message to cassandra-4.cassandra.default.svc.cluster.local/10.2.42.36 [MessagingService-Outgoing-cassandra-4.cassandra.default.svc.cluster.local/10.2.42.36-Small] | 2020-11-30 15:39:55.997000 | 10.0.2.30 | 1418 | 127.0.0.1
 READ message received from /10.0.2.30 [MessagingService-Incoming-/10.0.2.30] | 2020-11-30 15:39:55.998000 | 10.0.0.103 | 75 | 127.0.0.1
 READ message received from /10.0.2.30 [MessagingService-Incoming-/10.0.2.30] | 2020-11-30 15:39:55.998000 | 10.2.42.36 | 52 | 127.0.0.1
 Executing single-partition query on sorted_sessions_by_username [ReadStage-6] | 2020-11-30 15:39:55.998000 | 10.0.0.103 | 399 | 127.0.0.1
 Executing single-partition query on sorted_sessions_by_username [ReadStage-4] | 2020-11-30 15:39:55.998000 | 10.2.42.36 | 290 | 127.0.0.1
 Acquiring sstable references [ReadStage-6] | 2020-11-30 15:39:55.998000 | 10.0.0.103 | 487 | 127.0.0.1
 Acquiring sstable references [ReadStage-4] | 2020-11-30 15:39:55.998000 | 10.2.42.36 | 399 | 127.0.0.1
 Bloom filter allows skipping sstable 16267 [ReadStage-6] | 2020-11-30 15:39:55.998000 | 10.0.0.103 | 584 | 127.0.0.1
 Bloom filter allows skipping sstable 12935 [ReadStage-4] | 2020-11-30 15:39:55.998000 | 10.2.42.36 | 502 | 127.0.0.1
 Bloom filter allows skipping sstable 16207 [ReadStage-6] | 2020-11-30 15:39:55.998000 | 10.0.0.103 | 617 | 127.0.0.1
 Bloom filter allows skipping sstable 12885 [ReadStage-4] | 2020-11-30 15:39:55.998000 | 10.2.42.36 | 545 | 127.0.0.1
 Bloom filter allows skipping sstable 15659 [ReadStage-6] | 2020-11-30 15:39:55.998000 | 10.0.0.103 | 639 | 127.0.0.1
 Bloom filter allows skipping sstable 12419 [ReadStage-4] | 2020-11-30 15:39:55.998000 | 10.2.42.36 | 567 | 127.0.0.1
 Bloom filter allows skipping sstable 15346 [ReadStage-6] | 2020-11-30 15:39:55.998001 | 10.0.0.103 | 659 | 127.0.0.1
 Bloom filter allows skipping sstable 12153 [ReadStage-4] | 2020-11-30 15:39:55.998001 | 10.2.42.36 | 584 | 127.0.0.1
 Bloom filter allows skipping sstable 15061 [ReadStage-6] | 2020-11-30 15:39:55.998001 | 10.0.0.103 | 681 | 127.0.0.1
 Bloom filter allows skipping sstable 11910 [ReadStage-4] | 2020-11-30 15:39:55.998001 | 10.2.42.36 | 600 | 127.0.0.1
 Bloom filter allows skipping sstable 14800 [ReadStage-6] | 2020-11-30 15:39:55.998001 | 10.0.0.103 | 703 | 127.0.0.1
 Bloom filter allows skipping sstable 11693 [ReadStage-4] | 2020-11-30 15:39:55.998001 | 10.2.42.36 | 614 | 127.0.0.1
 Bloom filter allows skipping sstable 14521 [ReadStage-6] | 2020-11-30 15:39:55.998001 | 10.0.0.103 | 718 | 127.0.0.1
 Bloom filter allows skipping sstable 11456 [ReadStage-4] | 2020-11-30 15:39:55.998001 | 10.2.42.36 | 630 | 127.0.0.1
 Bloom filter allows skipping sstable 14298 [ReadStage-6] | 2020-11-30 15:39:55.998001 | 10.0.0.103 | 733 | 127.0.0.1
 Bloom filter allows skipping sstable 11265 [ReadStage-4] | 2020-11-30 15:39:55.998001 | 10.2.42.36 | 645 | 127.0.0.1
 Bloom filter allows skipping sstable 14047 [ReadStage-6] | 2020-11-30 15:39:55.998001 | 10.0.0.103 | 749 | 127.0.0.1
 Bloom filter allows skipping sstable 11066 [ReadStage-4] | 2020-11-30 15:39:55.998001 | 10.2.42.36 | 660 | 127.0.0.1
 Bloom filter allows skipping sstable 13743 [ReadStage-6] | 2020-11-30 15:39:55.998001 | 10.0.0.103 | 772 | 127.0.0.1
 Bloom filter allows skipping sstable 10829 [ReadStage-4] | 2020-11-30 15:39:55.998001 | 10.2.42.36 | 677 | 127.0.0.1
 Bloom filter allows skipping sstable 13389 [ReadStage-6] | 2020-11-30 15:39:55.998001 | 10.0.0.103 | 793 | 127.0.0.1
 Bloom filter allows skipping sstable 13079 [ReadStage-6] | 2020-11-30 15:39:55.998001 | 10.0.0.103 | 816 | 127.0.0.1
 Bloom filter allows skipping sstable 12749 [ReadStage-6] | 2020-11-30 15:39:55.998002 | 10.0.0.103 | 839 | 127.0.0.1
 Bloom filter allows skipping sstable 12427 [ReadStage-6] | 2020-11-30 15:39:55.998002 | 10.0.0.103 | 858 | 127.0.0.1
 Bloom filter allows skipping sstable 12101 [ReadStage-6] | 2020-11-30 15:39:55.998002 | 10.0.0.103 | 872 | 127.0.0.1
 Bloom filter allows skipping sstable 11760 [ReadStage-6] | 2020-11-30 15:39:55.998002 | 10.0.0.103 | 886 | 127.0.0.1
 Skipped 0/16 non-slice-intersecting sstables, included 0 due to tombstones [ReadStage-6] | 2020-11-30 15:39:55.998002 | 10.0.0.103 | 900 | 127.0.0.1
 Merged data from memtables and 0 sstables [ReadStage-6] | 2020-11-30 15:39:55.998002 | 10.0.0.103 | 995 | 127.0.0.1
 Read 1 live rows and 0 tombstone cells [ReadStage-6] | 2020-11-30 15:39:55.999000 | 10.0.0.103 | 1042 | 127.0.0.1
 Bloom filter allows skipping sstable 10554 [ReadStage-4] | 2020-11-30 15:39:55.999000 | 10.2.42.36 | 793 | 127.0.0.1
 REQUEST_RESPONSE message received from /10.0.0.103 [MessagingService-Incoming-/10.0.0.103] | 2020-11-30 15:39:55.999000 | 10.0.2.30 | 3079 | 127.0.0.1
 Enqueuing response to /10.0.2.30 [ReadStage-6] | 2020-11-30 15:39:55.999000 | 10.0.0.103 | 1067 | 127.0.0.1
 Bloom filter allows skipping sstable 10313 [ReadStage-4] | 2020-11-30 15:39:55.999000 | 10.2.42.36 | 832 | 127.0.0.1
 Processing response from /10.0.0.103 [RequestResponseStage-6] | 2020-11-30 15:39:55.999000 | 10.0.2.30 | 3183 | 127.0.0.1
 Sending REQUEST_RESPONSE message to cassandra-2.cassandra.default.svc.cluster.local/10.0.2.30 [MessagingService-Outgoing-cassandra-2.cassandra.default.svc.cluster.local/10.0.2.30-Small] | 2020-11-30 15:39:55.999000 | 10.0.0.103 | 1201 | 127.0.0.1
 REQUEST_RESPONSE message received from /10.2.42.36 [MessagingService-Incoming-/10.2.42.36] | 2020-11-30 15:39:56 | 10.0.2.30 | 30 | 127.0.0.1
 Processing response from /10.2.42.36 [RequestResponseStage-6] | 2020-11-30 15:39:56 | 10.0.2.30 | 168 | 127.0.0.1
 Initiating read-repair [RequestResponseStage-6] | 2020-11-30 15:39:56 | 10.0.2.30 | 246 | 127.0.0.1
 Request complete | 2020-11-30 15:39:55.999417 | 10.0.2.30 | 3417 | 127.0.0.1
{code}
 

As you can see, the database checks for the existence in all of the sstables, although the only and the last result is found in the last one, or even worse, in the memtable!

I'd expect Cassandra to start searching the sstables in decreasing order until it finds the required "limit" results. 

I saw this ticket: https://issues.apache.org/jira/browse/CASSANDRA-8180+**+ and came to an understanding that this should have been fixed in Cassandra 3.4, but obviously, that's no the case