From 839fcdadcc2a4fa48eb1df01de0f09c8faf85319 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Tue, 24 Nov 2026 14:15:07 +0300 Subject: [PATCH] Client: Check CAS writes for multiple stripes after slicing them 3bc5a186 ("Execute a forced sync before CAS if inode range has dirty data") moved the "atomic writes to multiple stripes are unsupported" check above slice_rw(). op->parts is still empty there, so the check never fires: a CAS write or delete that crosses a stripe boundary sends only its first part and reports success with the length of that part, while the rest of the range stays unchanged. Do the check after slice_rw() again, as before 3bc5a186. --- src/client/cluster_client.cpp | 16 ++++---- src/test/test_cluster_client.cpp | 66 +++++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 10 deletions(-) diff --git a/src/client/cluster_client.cpp b/src/client/cluster_client.cpp index c5eb1d8c..a67dca19 100644 --- a/src/client/cluster_client.cpp +++ b/src/client/cluster_client.cpp @@ -925,14 +925,6 @@ void cluster_client_t::execute_cas(cluster_op_t *op, bool nosync) cb(op); return; } - if ((op->opcode == OSD_OP_WRITE || op->opcode == OSD_OP_DELETE) && op->version && op->parts.size() > 1) - { - // Atomic writes to multiple stripes are unsupported - op->retval = -EINVAL; - auto cb = std::move(op->callback); - cb(op); - return; - } if (!(op->flags & OP_IMMEDIATE_COMMIT) && !nosync && wb->has_dirty(op->inode, op->offset, op->len)) { @@ -961,6 +953,14 @@ void cluster_client_t::execute_cas(cluster_op_t *op, bool nosync) cb(op); return; } + if ((op->opcode == OSD_OP_WRITE || op->opcode == OSD_OP_DELETE) && op->version && op->parts.size() > 1) + { + // Atomic writes to multiple stripes are unsupported + op->retval = -EINVAL; + auto cb = std::move(op->callback); + cb(op); + return; + } op->needs_reslice = false; int res = try_send(op, 0, [this, op](osd_op_t *part) { diff --git a/src/test/test_cluster_client.cpp b/src/test/test_cluster_client.cpp index 7a941b18..8b10f347 100644 --- a/src/test/test_cluster_client.cpp +++ b/src/test/test_cluster_client.cpp @@ -23,7 +23,7 @@ public: static void test_vault(); }; -void configure_single_pg_pool(etcd_state_client_mock_t *mock) +void configure_single_pg_pool(etcd_state_client_mock_t *mock, const char *immediate_commit = "none") { mock->set("/vitastor/config/pools", json11::Json::object { { "1", json11::Json::object { @@ -33,7 +33,7 @@ void configure_single_pg_pool(etcd_state_client_mock_t *mock) { "pg_minsize", 1 }, { "pg_count", 1 }, { "failure_domain", "osd" }, - { "immediate_commit", "none" }, + { "immediate_commit", immediate_commit }, } } }); mock->set("/vitastor/pg/config", json11::Json::object { @@ -1343,6 +1343,67 @@ void test_sync_fail_dirty_state_loss() } +// CAS write/delete over two objects must fail with -EINVAL before anything is +// sent: the OSD checks the version of one object only +void test_cas_cross_object() +{ + json11::Json config; + timerfd_manager_t *tfd = new timerfd_manager_t([](int fd, bool wr, std::function callback){}); + etcd_mock_t etcd_store; + etcd_state_client_mock_t *mock = new etcd_state_client_mock_t(etcd_store); + mock->pause(); + cluster_client_t *cli = new cluster_client_t(NULL, tfd, config, std::unique_ptr(mock)); + configure_single_pg_pool(mock, "all"); + mock->resume(); + pretend_connected(cli, 1); + cluster_client_test_t::continue_ops(cli); + + const uint64_t obj_size = 128*1024; // default block_size + struct { uint64_t opcode, offset, len; int64_t expected; } cases[] = { + { OSD_OP_WRITE, obj_size-0x1000, 0x2000, -EINVAL }, + { OSD_OP_DELETE, obj_size-0x1000, 0x2000, -EINVAL }, + // ends exactly at the object boundary: one part, must be sent whole + { OSD_OP_WRITE, obj_size-0x2000, 0x2000, 0x2000 }, + }; + int fails = 0; + for (auto & c: cases) + { + const char *name = c.opcode == OSD_OP_WRITE ? "write" : "delete"; + int64_t r = -1000; + cluster_op_t *op = new cluster_op_t(); + op->opcode = c.opcode; + op->inode = 0x1000000000001; + op->offset = c.offset; + op->len = c.len; + op->version = 2; + if (c.opcode == OSD_OP_WRITE) + { + op->iov.push_back(malloc_or_die(c.len), c.len); + memset(op->iov.buf[0].iov_base, 0x5a, c.len); + } + op->callback = [&r](cluster_op_t *op) { r = op->retval; }; + cli->execute(op); + osd_client_t *cl = cli->msgr->osd_peers.at(1); + for (auto it = cl->sent_ops.begin(); r == -1000 && it != cl->sent_ops.end(); it = cl->sent_ops.begin()) + { + osd_op_t *part = it->second; + printf("CAS %s %jx+%jx: sent %jx+%x to OSD 1\n", name, c.offset, c.len, part->req.rw.offset, part->req.rw.len); + pretend_op_completed(cli, part, 0); + } + printf("CAS %s %jx+%jx version 2: retval %jd, expected %jd\n", name, c.offset, c.len, r, c.expected); + if (r != c.expected || cl->sent_ops.size()) + fails++; + if (c.opcode == OSD_OP_WRITE) + free(op->iov.buf[0].iov_base); + delete op; + } + assert(!fails); + + delete cli; + delete tfd; + printf("[ok] CAS over two objects test\n"); +} + int main(int narg, char *args[]) { test1(); @@ -1355,6 +1416,7 @@ int main(int narg, char *args[]) test_msgr_decrypt_chain(); test_primary_change_flush_loss(); test_sync_fail_dirty_state_loss(); + test_cas_cross_object(); cluster_client_test_t::test_vault(); test_pg_config_disappears(); test_sync_released_during_continue_ops(); -- 2.47.3