diff --git a/src/blockstore/v1/impl.h b/src/blockstore/v1/impl.h index 160624b9..6fa7b6d5 100644 --- a/src/blockstore/v1/impl.h +++ b/src/blockstore/v1/impl.h @@ -176,6 +176,7 @@ class blockstore_impl_t: public blockstore_i std::set own_ops; std::vector unsynced_big_writes, unsynced_small_writes; int unsynced_big_write_count = 0, unstable_unsynced = 0; + uint64_t journal_full_reported = 0; // Number of deletions which took over the durability of preceding unsynced writes but // aren't journaled themselves yet, see forget_unstable_before_delete() int unsynced_forgotten = 0; @@ -347,6 +348,7 @@ public: // Print diagnostics to stdout void dump_diagnostics(); + void report_journal_full(); // Get diagnostic string for an operation std::string get_op_diag(blockstore_op_t *op); diff --git a/src/blockstore/v1/journal.cpp b/src/blockstore/v1/journal.cpp index 845376b7..b6935234 100644 --- a/src/blockstore/v1/journal.cpp +++ b/src/blockstore/v1/journal.cpp @@ -129,6 +129,7 @@ int blockstore_journal_check_t::check_available(blockstore_op_t *op, int entries if (!bs->flusher->may_advance()) { give_up = true; + bs->report_journal_full(); } else { @@ -330,6 +331,33 @@ uint64_t journal_t::get_trim_pos() return used_start; } +// Clients retry refused writes, so report it at most once in 10 seconds +void blockstore_impl_t::report_journal_full() +{ + timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); + if (journal_full_reported && now.tv_sec < journal_full_reported+10) + return; + journal_full_reported = now.tv_sec; + // The oldest entry still in use is what keeps the journal from being trimmed + auto used_it = journal.used_sectors.lower_bound(journal.used_start); + if (used_it == journal.used_sectors.end()) + used_it = journal.used_sectors.begin(); + auto oldest = dirty_db.end(); + if (used_it != journal.used_sectors.end()) + { + for (oldest = dirty_db.begin(); oldest != dirty_db.end() && oldest->second.journal_sector != used_it->first; oldest++) {} + } + bool found = oldest != dirty_db.end(); + printf( + "Journal is full and nothing in it can be flushed, refusing writes with EAGAIN" + " (used_start=%08jx next_free=%08jx, %zu objects with unstable writes, oldest entry %jx:%jx v%ju %s)\n", + journal.used_start, journal.next_free, unstable_writes.size(), + found ? oldest->first.oid.inode : 0, found ? oldest->first.oid.stripe : 0, found ? oldest->first.version : 0, + !found ? "not found" : (IS_STABLE(oldest->second.state) ? "stable" : "unstable") + ); +} + void journal_t::dump_diagnostics() { auto journal_used_it = used_sectors.lower_bound(used_start); diff --git a/src/test/test_blockstore_v1.cpp b/src/test/test_blockstore_v1.cpp index 8969ed05..751303a4 100644 --- a/src/test/test_blockstore_v1.cpp +++ b/src/test/test_blockstore_v1.cpp @@ -3357,6 +3357,136 @@ static void test_read_error_is_not_edom(const char *csum_block_size) free(read_op.buf); } +// A write over a deletion that the flusher is flushing used to queue the deletion once more. +// That stale queue entry hid the flush of the new write, the write pinned the journal for good, +// and once the journal wrapped every write was refused with EAGAIN until a restart +static void test_write_over_flushing_delete_keeps_journal_free() +{ + printf("\n-- test_write_over_flushing_delete_keeps_journal_free\n"); + + bs_test_t test; + test.default_cfg(); + // 1 MB journal, so that a pinned entry blocks it after a few hundred writes + test.config["journal_offset"] = std::to_string(31*1024*1024); + test.config["log_level"] = "0"; + // One flusher, so that nothing else picks the deletion from the queue while it is busy + test.config["max_flusher_count"] = "1"; + test.init(); + printf("blockstore initialized\n"); + + object_id oid = { .inode = 1, .stripe = 0 }; + const uint32_t bs_size = 128*1024; + uint8_t *buf = (uint8_t*)memalign_or_die(MEM_ALIGNMENT, bs_size); + memset(buf, 0xAA, bs_size); + + printf("write v1 (big) and flush it, so that the deletion has metadata to clear\n"); + blockstore_op_t op; + op.opcode = BS_OP_WRITE_STABLE; + op.oid = oid; + op.version = 1; + op.offset = 0; + op.len = bs_size; + op.buf = buf; + test.exec_op(&op); + assert(op.retval == (int)op.len); + test.force_compaction(); + assert(!test.find_dirty_entry(oid, 1)); + + printf("delete v2\n"); + op.opcode = BS_OP_DELETE; + op.version = 2; + op.len = 0; + op.buf = NULL; + test.exec_op(&op); + assert(op.retval == 0); + + // Hold the flusher's metadata write of the deletion, so that it stays in the middle of the flush + const uint64_t journal_offset = parse_size(test.config["journal_offset"]); + ring_data_t *held = NULL; + test.sqe_handler = [&](io_uring_sqe *sqe) + { + if (!held && sqe->fd == MOCK_DATA_FD && sqe->off < journal_offset && + (sqe->opcode == IORING_OP_WRITEV || sqe->opcode == IORING_OP_WRITE)) + { + bool ok = test.data_disk->submit(sqe); + assert(ok); + held = (ring_data_t*)sqe->user_data; + return true; + } + return false; + }; + test.flusher()->request_trim(); + for (int i = 0; i < 100 && !held; i++) + test.ringloop->loop(); + assert(held); + + printf("write v1 (small) over the deletion while it is being flushed\n"); + blockstore_op_t wr; + bool wr_done = false; + wr.opcode = BS_OP_WRITE_STABLE; + wr.oid = oid; + wr.version = 1; + wr.offset = 0; + wr.len = 4096; + wr.buf = buf; + wr.callback = [&](blockstore_op_t *op) { wr_done = true; }; + test.bs->enqueue_op(&wr); + for (int i = 0; i < 20 && !wr_done; i++) + test.ringloop->loop(); + assert(!wr_done); + + // Hold the journal trim that follows the flush, so that the write over the deletion + // becomes stable before the flusher looks at its queue again + printf("finish the flush of the deletion, holding the journal trim after it\n"); + ring_data_t *held_trim = NULL; + test.sqe_handler = [&](io_uring_sqe *sqe) + { + if (!held_trim && sqe->fd == MOCK_DATA_FD && sqe->off == journal_offset && + (sqe->opcode == IORING_OP_WRITEV || sqe->opcode == IORING_OP_WRITE)) + { + bool ok = test.data_disk->submit(sqe); + assert(ok); + held_trim = (ring_data_t*)sqe->user_data; + return true; + } + return false; + }; + test.ringloop->mark_completed(held); + for (int i = 0; i < 1000 && !wr_done; i++) + test.ringloop->loop(); + assert(wr_done && wr.retval == (int)wr.len && wr.version == 1); + // Without it the flusher drops the stale entry before the write is stable, and nothing is tested + assert(held_trim); + test.sqe_handler = NULL; + test.ringloop->mark_completed(held_trim); + while (test.flusher()->is_active()) + test.ringloop->loop(); + test.flusher()->release_trim(); + + printf("write other objects until the journal wraps several times\n"); + int refused = 0; + for (int i = 0; i < 2000; i++) + { + op.opcode = BS_OP_WRITE_STABLE; + op.oid = { .inode = 2, .stripe = (uint64_t)(i % 16) * bs_size }; + op.version = 1 + i / 16; + op.offset = 0; + op.len = 4096; + op.buf = buf; + test.exec_op(&op); + if (op.retval == -EAGAIN) + refused++; + else + assert(op.retval == (int)op.len); + } + printf("%d writes refused with EAGAIN, v1 over the deletion is %s\n", refused, + test.find_dirty_entry(oid, 1) ? "still not flushed" : "flushed"); + assert(!refused); + assert(!test.find_dirty_entry(oid, 1)); + + free(buf); +} + int main(int narg, char *args[]) { test_preserve_corruption(); @@ -3400,5 +3530,6 @@ int main(int narg, char *args[]) test_small_write_waits_for_its_own_big_write_imm(); test_writes_over_delete_released_imm(); test_tombstone_unsupported(); + test_write_over_flushing_delete_keeps_journal_free(); return 0; }