From 7427ec991c7e35f70a12d2cce229f8adbb8cfc5e Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 19 Sep 2018 16:36:01 -0700 Subject: [PATCH] ircd::db: Add closure on manual compaction arguments. --- include/ircd/db/column.h | 4 +-- include/ircd/db/database/database.h | 2 +- ircd/db.cc | 42 ++++++++++++++++++++++++----- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/include/ircd/db/column.h b/include/ircd/db/column.h index a11357664..c0b194309 100644 --- a/include/ircd/db/column.h +++ b/include/ircd/db/column.h @@ -59,8 +59,8 @@ namespace ircd::db // [SET] Other operations void setopt(column &, const string_view &key, const string_view &val); - void compact(column &, const std::pair &, const int &to_level = -1); - void compact(column &, const int &level = -1); + void compact(column &, const std::pair &, const compactor & = {}); + void compact(column &, const int &level = -1, const compactor & = {}); void sort(column &, const bool &blocking = false); } diff --git a/include/ircd/db/database/database.h b/include/ircd/db/database/database.h index 82bc60e96..74e7cb52c 100644 --- a/include/ircd/db/database/database.h +++ b/include/ircd/db/database/database.h @@ -40,7 +40,7 @@ namespace ircd::db void fdeletions(database &, const bool &enable, const bool &force = false); uint64_t checkpoint(database &); void check(database &); - void compact(database &); + void compact(database &, const compactor & = {}); void sort(database &, const bool &blocking = true); void flush(database &, const bool &sync = false); void sync(database &); diff --git a/ircd/db.cc b/ircd/db.cc index 006c5aaba..ee9f141d4 100644 --- a/ircd/db.cc +++ b/ircd/db.cc @@ -347,7 +347,8 @@ ircd::db::sort(database &d, } void -ircd::db::compact(database &d) +ircd::db::compact(database &d, + const compactor &cb) { static const std::pair range { @@ -357,8 +358,8 @@ ircd::db::compact(database &d) for(const auto &c : d.columns) { db::column column{*c}; - compact(column, range, -1); - compact(column, -1); + compact(column, range, cb); + compact(column, -1, cb); } } @@ -7350,7 +7351,8 @@ ircd::db::sort(column &column, void ircd::db::compact(column &column, - const int &level_) + const int &level_, + const compactor &cb) { database::column &c(column); database &d(*c.d); @@ -7382,6 +7384,19 @@ ircd::db::compact(column &column, ctx::interruption_point(); const ctx::uninterruptible::nothrow ui; const std::lock_guard lock{write_mutex}; + + // Save and restore the existing filter callback so we can allow our + // caller to use theirs. Note that this manual compaction should be + // exclusive for this column (no background compaction should be + // occurring, at least one relying on this filter). + auto their_filter(std::move(c.cfilter.user)); + const unwind unfilter{[&c, &their_filter] + { + c.cfilter.user = std::move(their_filter); + }}; + + c.cfilter.user = cb; + log::debug { log, "'%s':'%s' COMPACT level:%d files:%zu size:%zu", @@ -7402,7 +7417,7 @@ ircd::db::compact(column &column, void ircd::db::compact(column &column, const std::pair &range, - const int &to_level) + const compactor &cb) { database &d(column); database::column &c(column); @@ -7421,11 +7436,24 @@ ircd::db::compact(column &column, rocksdb::CompactRangeOptions opts; opts.change_level = true; - opts.target_level = to_level; + opts.target_level = -1; opts.allow_write_stall = true; const ctx::uninterruptible::nothrow ui; const std::lock_guard lock{write_mutex}; + + // Save and restore the existing filter callback so we can allow our + // caller to use theirs. Note that this manual compaction should be + // exclusive for this column (no background compaction should be + // occurring, at least one relying on this filter). + auto their_filter(std::move(c.cfilter.user)); + const unwind unfilter{[&c, &their_filter] + { + c.cfilter.user = std::move(their_filter); + }}; + + c.cfilter.user = cb; + log::debug { log, "'%s':'%s' @%lu COMPACT [%s, %s] to level %d", @@ -7434,7 +7462,7 @@ ircd::db::compact(column &column, sequence(d), range.first, range.second, - to_level + opts.target_level }; throw_on_error