client: Fix CAS writes and buffer cleanup with write-back and immediate_commit #196

Merged
vitalif merged 2 commits from koolkhel/vitastor:cas-writeback-immediate-commit into master 2026-11-26 16:05:53 +03:00
Contributor

Description

Two client fixes for the write-back cache on immediate_commit=all pools. The new test needs both.

  1. client: Flush the write-back cache before CAS writes with immediate_commit. execute_cas() forced a SYNC before a CAS write over dirty write-back data only when the pool's immediate_commit is not "all". So on an immediate_commit=all pool with client_enable_writeback, the CAS write went to the OSD first and succeeded against the old version. The older cached write then overwrote it at the next flush: the CAS write was lost although it had returned success. The fix adds the || enable_writeback term that the other four write-back checks in cluster_client.cpp already have.
  2. client: Delete written-back buffers with immediate_commit again. Since 2e223f03 a flush op carries its flush_id. So continue_rw() marks the flushed buffers as written before the flush callback runs, and the delete_flush() added by 21066a09 finds nothing. With immediate_commit no SYNC removes them later, so they stay for good. The leak 21066a09 fixed is back, and reads are answered from these buffers with version 0, so a CAS write retried after -EINTR never sees the real version.

Who is exposed: only clients with client_enable_writeback on immediate_commit=all pools. The KV database mixes CAS block writes with plain writes to the same blocks (clear_block() with version 0), so (1) can lose a block update there. Block clients (QEMU, NBD, ublk) send no CAS writes, but (2) keeps their written-back buffers forever.

Found: seen in a test run. test_cas failed with "CAS write data lost" when run with the client write-back cache on an immediate_commit=all pool. With the first fix alone it failed with "CAS write failed to invalidate the write-back cache and read returned version 0", which led to the second fix.

Tested:

  • IMMEDIATE_COMMIT=1 ./test_cas.sh is added to run_tests.sh, and test.yml is regenerated with tests-to-yaml.pl. It fails on master, fails with only the first commit, and passes with both.
  • ./test_cas.sh, IMMEDIATE_COMMIT=1 ./test_kv_stress.sh and test_cluster_client pass.
  • A buffered fio randwrite with verify (no -direct) on an IMMEDIATE_COMMIT=1 stand passes.

Contributor Name

Yury Luneff (MIND Software LLC, https://mindsw.io)

CLA

### Description Two client fixes for the write-back cache on immediate_commit=all pools. The new test needs both. 1. **client: Flush the write-back cache before CAS writes with immediate_commit.** `execute_cas()` forced a SYNC before a CAS write over dirty write-back data only when the pool's immediate_commit is not "all". So on an immediate_commit=all pool with `client_enable_writeback`, the CAS write went to the OSD first and succeeded against the old version. The older cached write then overwrote it at the next flush: the CAS write was lost although it had returned success. The fix adds the `|| enable_writeback` term that the other four write-back checks in cluster_client.cpp already have. 2. **client: Delete written-back buffers with immediate_commit again.** Since 2e223f03 a flush op carries its `flush_id`. So `continue_rw()` marks the flushed buffers as written before the flush callback runs, and the `delete_flush()` added by 21066a09 finds nothing. With immediate_commit no SYNC removes them later, so they stay for good. The leak 21066a09 fixed is back, and reads are answered from these buffers with version 0, so a CAS write retried after -EINTR never sees the real version. Who is exposed: only clients with `client_enable_writeback` on immediate_commit=all pools. The KV database mixes CAS block writes with plain writes to the same blocks (`clear_block()` with version 0), so (1) can lose a block update there. Block clients (QEMU, NBD, ublk) send no CAS writes, but (2) keeps their written-back buffers forever. Found: seen in a test run. test_cas failed with "CAS write data lost" when run with the client write-back cache on an immediate_commit=all pool. With the first fix alone it failed with "CAS write failed to invalidate the write-back cache and read returned version 0", which led to the second fix. Tested: - `IMMEDIATE_COMMIT=1 ./test_cas.sh` is added to run_tests.sh, and test.yml is regenerated with tests-to-yaml.pl. It fails on master, fails with only the first commit, and passes with both. - `./test_cas.sh`, `IMMEDIATE_COMMIT=1 ./test_kv_stress.sh` and test_cluster_client pass. - A buffered fio randwrite with verify (no `-direct`) on an IMMEDIATE_COMMIT=1 stand passes. ### Contributor Name Yury Luneff (MIND Software LLC, https://mindsw.io) ### CLA - [x] I accept Vitastor CLA agreement: https://git.yourcmc.ru/vitalif/vitastor/src/branch/master/CLA-en.md
koolkhel added 2 commits 2026-11-26 15:43:57 +03:00
execute_cas() runs a SYNC before a CAS write over dirty write-back data
only when the pool's immediate_commit is not "all". But the write-back
cache also holds writes for immediate_commit=all pools, so there the CAS
write went to the OSD first, succeeded against the old version, and the
older cached write overwrote it at the next flush. Check enable_writeback
here like the other write-back checks in cluster_client.cpp do.
21066a09 deletes buffers in the flush callback when immediate_commit is
on, because no SYNC ever removes them then. Since 2e223f03 a flush op
carries its flush_id, so continue_rw() marks its buffers as written,
with flush_id 0, before the callback runs, and delete_flush() finds
nothing. The buffers then stay forever: memory grows with every range
written, and reads are answered from them with version 0, so a CAS
write retried after -EINTR never sees the real version. Skip
mark_flush_written() for immediate_commit ops, as handle_op_part()
already does for mark_flush_written_to().

Also run test_cas with IMMEDIATE_COMMIT=1. It fails without the
previous commit ("CAS write data lost") and without this one ("read
returned version 0").
vitalif merged commit 6601ad9265 into master 2026-11-26 16:05:53 +03:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: vitalif/vitastor#196
No description provided.