// Copy the extent because we might be stuck writing for a while
ByteVector extent_copy(dirty_extent, dirty_extent_end);
+ // Mark the extent clean before releasing the lock: a modification
+ // during the write window sets the flag again, so the extent is
+ // flushed on a later pass instead of being marked clean while the
+ // on-disk copy predates the modification. The flag is not
+ // restored if the write fails: the on-disk copy stays stale
+ // until the next update dirties the extent, which is the same
+ // best-effort loss accepted at shutdown, and avoids retrying a
+ // persistently failing write (e.g. ENOSPC) on every pass.
+ m_extent_metadata.at(extent_index).m_dirty = false;
+
// Release the lock
lock.unlock();
bees_fsync(m_fd);
}
- // Mark extent clean if write was successful
- lock.lock();
- m_extent_metadata.at(extent_index).m_dirty = false;
-
wrote_extent = true;
});
if (flush_dirty_extent(extent_index)) {
++wrote_extents;
++m_writeback_extent_count;
- if (slowly) {
- if (m_stop_requested) {
- slowly = false;
- continue;
- }
- BEESNOTE("flush rate limited after extent #" << extent_index << " of " << m_extents << " extents");
- chrono::duration<double> sleep_time(m_flush_rate_limit.sleep_time(BLOCK_SIZE_HASHTAB_EXTENT));
- unique_lock<mutex> lock(m_stop_mutex);
- m_stop_condvar.wait_for(lock, sleep_time);
+ }
+ // Rate limit the attempt whether or not it succeeded, so a
+ // persistently failing write does not turn a pass into a
+ // burst of failures and logged exceptions.
+ if (slowly) {
+ if (m_stop_requested) {
+ slowly = false;
+ continue;
}
+ BEESNOTE("flush rate limited after extent #" << extent_index << " of " << m_extents << " extents");
+ chrono::duration<double> sleep_time(m_flush_rate_limit.sleep_time(BLOCK_SIZE_HASHTAB_EXTENT));
+ unique_lock<mutex> lock(m_stop_mutex);
+ m_stop_condvar.wait_for(lock, sleep_time);
}
}
BEESLOGINFO("Flushed " << wrote_extents << " of " << m_extents << " hash table extents");