diff --git a/src/blockstore/v1/write.cpp b/src/blockstore/v1/write.cpp index c00c75ec..23e30ba7 100644 --- a/src/blockstore/v1/write.cpp +++ b/src/blockstore/v1/write.cpp @@ -842,9 +842,10 @@ resume_4: } if (imm && (dirty_it->second.state & BS_ST_TYPE_MASK) == BS_ST_BIG_WRITE) { - // Unblock small writes + // Unblock small writes. The ones after the next big write wait for that one dirty_it++; - while (dirty_it != dirty_db.end() && dirty_it->first.oid == op->oid) + while (dirty_it != dirty_db.end() && dirty_it->first.oid == op->oid && + !IS_BIG_WRITE(dirty_it->second.state)) { if ((dirty_it->second.state & BS_ST_WORKFLOW_MASK) == BS_ST_WAIT_BIG) { diff --git a/src/test/test_blockstore_v1.cpp b/src/test/test_blockstore_v1.cpp index 1edddb9f..5dd7c60e 100644 --- a/src/test/test_blockstore_v1.cpp +++ b/src/test/test_blockstore_v1.cpp @@ -3127,6 +3127,83 @@ static void test_small_write_waits_for_its_own_big_write() free(buf); } +// The same with immediate_commit=all: the ack of an EARLIER big write must not let go a small +// write which waits for a later big write that is still in flight +static void test_small_write_waits_for_its_own_big_write_imm() +{ + printf("\n-- test_small_write_waits_for_its_own_big_write_imm\n"); + + bs_test_t test; + test.default_cfg(); + test.init(); + printf("blockstore initialized\n"); + + object_id oid = { .inode = 1, .stripe = 0 }; + const uint32_t bs_size = 128*1024; + uint8_t *buf1 = (uint8_t*)memalign_or_die(MEM_ALIGNMENT, bs_size); + uint8_t *buf2 = (uint8_t*)memalign_or_die(MEM_ALIGNMENT, bs_size); + uint8_t *buf3 = (uint8_t*)memalign_or_die(MEM_ALIGNMENT, 4096); + memset(buf1, 0xAA, bs_size); + memset(buf2, 0xBB, bs_size); + memset(buf3, 0xCC, 4096); + + // Let the data write of v1 through and swallow the data write of v2, so that v2 stays in flight + int big_data_writes = 0; + test.sqe_handler = [&](io_uring_sqe *sqe) + { + if (sqe->fd != MOCK_DATA_FD || sqe->opcode != IORING_OP_WRITEV || sqe->off < test.dsk().data_offset) + return false; + return (++big_data_writes) == 2; + }; + + printf("write v1 (big), v2 (big) and v3 (small) at once\n"); + blockstore_op_t op[3]; + bool done[3] = {}; + uint8_t *bufs[3] = { buf1, buf2, buf3 }; + for (int i = 0; i < 3; i++) + { + op[i].opcode = BS_OP_WRITE; + op[i].oid = oid; + op[i].version = i+1; + op[i].offset = 0; + op[i].len = i < 2 ? bs_size : 4096; + op[i].buf = bufs[i]; + op[i].callback = [&done, i](blockstore_op_t *op) { done[i] = true; }; + test.bs->enqueue_op(&op[i]); + } + for (int i = 0; i < 50; i++) + test.ringloop->loop(); + assert(big_data_writes == 2); + assert(done[0] && op[0].retval == (int)bs_size); + assert(!done[1]); + printf("v1 is done, v2 is in flight - v3 has to wait for v2\n"); + assert(!done[2]); + + printf("restarting as if the process had died\n"); + test.sqe_handler = NULL; + test.destroy_bs(); + test.ringloop->reset(); + test.init(); + + printf("the object must come back as v1 - not as v3 written over v1\n"); + blockstore_op_t rd; + rd.opcode = BS_OP_READ; + rd.oid = oid; + rd.version = UINT64_MAX; + rd.offset = 0; + rd.len = bs_size; + rd.buf = (uint8_t*)memalign_or_die(MEM_ALIGNMENT, rd.len); + test.exec_op(&rd); + assert(rd.retval == (int)rd.len); + printf("got v%ju\n", rd.version); + assert(rd.version == 1 && memcheck(rd.buf, 0xAA, bs_size)); + + free(rd.buf); + free(buf3); + free(buf2); + free(buf1); +} + // Tombstones are a heap-store (meta_format 3) feature, the v1 store must refuse them static void test_tombstone_unsupported() { @@ -3183,6 +3260,7 @@ int main(int narg, char *args[]) test_sync_waits_for_delete_taking_over_unsynced(); test_zero_write_over_rollback_to_zero(); test_small_write_waits_for_its_own_big_write(); + test_small_write_waits_for_its_own_big_write_imm(); test_tombstone_unsupported(); return 0; }