Move handle_event code into _read.cpp, _write.cpp; move sync code into _sync.cpp

This commit is contained in:
Vitaliy Filippov
2019-11-10 02:27:27 +03:00
parent 8e634d5b74
commit 24f5d71595
6 changed files with 324 additions and 223 deletions
+27 -109
View File
@@ -114,120 +114,38 @@ int blockstore::dequeue_write(blockstore_operation *op)
return 1;
}
int blockstore::dequeue_sync(blockstore_operation *op)
void blockstore::handle_write_event(ring_data_t *data, blockstore_operation *op)
{
op->big_write_count = 0;
op->big_write_state = 0x10000;
op->sync_writes.swap(unsynced_writes);
unsynced_writes.clear();
auto it = op->sync_writes.begin();
while (it != op->sync_writes.end())
if (data->res < 0)
{
uint32_t state = dirty_db[*it].state;
if (IS_BIG_WRITE(state))
{
op->big_write_count++;
op->big_write_state = op->big_write_state < state ? op->big_write_state : state;
}
it++;
// write error
// FIXME: our state becomes corrupted after a write error. maybe do something better than just die
throw new std::runtime_error("write operation failed. in-memory state is corrupted. AAAAAAAaaaaaaaaa!!!111");
}
if (op->big_write_count == 0 || op->big_write_state == ST_D_META_WRITTEN)
op->pending_ops--;
if (op->min_used_journal_sector > 0)
{
// Just fsync the journal
BS_SUBMIT_GET_SQE(sqe, data);
io_uring_prep_fsync(sqe, journal.fd, 0);
data->op = op;
op->pending_ops = 1;
for (uint64_t s = op->min_used_journal_sector; s <= op->max_used_journal_sector; s++)
{
journal.sector_info[s-1].usage_count--;
}
op->min_used_journal_sector = op->max_used_journal_sector = 0;
}
else if (op->big_write_state == ST_D_WRITTEN)
if (op->pending_ops == 0)
{
// 1st step: fsync data
BS_SUBMIT_GET_SQE(sqe, data);
io_uring_prep_fsync(sqe, data_fd, 0);
data->op = op;
op->pending_ops = 1;
// Acknowledge write without sync
auto dirty_it = dirty_db.find((obj_ver_id){
.oid = op->oid,
.version = op->version,
});
dirty_it->second.state = (dirty_it->second.state == ST_J_SUBMITTED
? ST_J_WRITTEN : (dirty_it->second.state == ST_DEL_SUBMITTED ? ST_DEL_WRITTEN : ST_D_WRITTEN));
op->retval = op->len;
op->callback(op);
in_progress_ops.erase(op);
unsynced_writes.push_back((obj_ver_id){
.oid = op->oid,
.version = op->version,
});
}
else if (op->big_write_state == ST_D_SYNCED)
{
// 2nd step: Data device is synced, prepare & write journal entries
// Check space in the journal and journal memory buffers
int required = op->big_write_count, sectors_required = 1;
uint64_t next_pos = journal.next_free, next_sector = journal.cur_sector;
while (1)
{
int fits = (512 - journal.in_sector_pos) / sizeof(journal_entry_big_write);
required -= fits;
if (required <= 0)
break;
next_pos = (next_pos+512) < journal.len ? next_pos+512 : 512;
sectors_required++;
next_sector = ((next_sector + 1) % journal.sector_count);
if (journal.sector_info[next_sector].usage_count > 0)
{
// No memory buffer available. Wait for it.
op->wait_for = WAIT_JOURNAL_BUFFER;
return 0;
}
}
if (next_pos >= journal.used_start)
{
// No space in the journal. Wait for it.
op->wait_for = WAIT_JOURNAL;
op->wait_detail = next_pos;
return 0;
}
// Get SQEs. Don't bother about merging, submit each journal sector as a separate request
struct io_uring_sqe *sqe[sectors_required];
for (int i = 0; i < sectors_required; i++)
{
BS_SUBMIT_GET_SQE_DECL(sqe[i]);
}
// Prepare and submit journal entries
op->min_used_journal_sector = 1 + journal.cur_sector;
sectors_required = 0;
required = op->big_write_count;
it = op->sync_writes.begin();
while (1)
{
int fits = (512 - journal.in_sector_pos) / sizeof(journal_entry_big_write);
while (fits > 0 && required > 0)
{
journal_entry_big_write *je = (journal_entry_big_write*)(
journal.sector_buf + 512*journal.cur_sector + journal.in_sector_pos
);
*je = {
.crc32 = 0,
.magic = JOURNAL_MAGIC,
.type = JE_BIG_WRITE,
.size = sizeof(journal_entry_big_write),
.crc32_prev = journal.crc32_last,
.oid = it->oid,
.version = it->version,
.block = dirty_db[*it].location,
};
je->crc32 = je_crc32((journal_entry*)je);
journal.crc32_last = je->crc32;
journal.in_sector_pos += sizeof(journal_entry_big_write);
required--;
}
if (required <= 0)
break;
journal.sector_info[journal.cur_sector].usage_count++;
struct ring_data_t *data = ((ring_data_t*)sqe[sectors_required]->user_data);
data->iov = (struct iovec){ journal.sector_buf + 512*journal.cur_sector, 512 };
data->op = op;
io_uring_prep_writev(
sqe[sectors_required], journal.fd, &data->iov, 1, journal.offset + journal.sector_info[journal.cur_sector].offset
);
journal.cur_sector = ((journal.cur_sector + 1) % journal.sector_count);
journal.sector_info[journal.cur_sector].offset = journal.next_free;
journal.in_sector_pos = 0;
journal.next_free = (journal.next_free + 512) < journal.len ? journal.next_free + 512 : 512;
memset(journal.sector_buf + 512*journal.cur_sector, 0, 512);
sectors_required++;
}
op->pending_ops = sectors_required;
op->max_used_journal_sector = 1 + journal.cur_sector;
}
return 1;
}