Skip to content

Commit

Permalink
Revert "Implement "safe timestamp" reservation (#1050)"
Browse files Browse the repository at this point in the history
This reverts commit ec0862b.
  • Loading branch information
senderista committed Nov 8, 2021
1 parent 94c1bcb commit fcaacf1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 453 deletions.
105 changes: 4 additions & 101 deletions production/db/core/inc/db_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,13 @@ class server_t
// eligible for GC. GC cannot be started for any committed txn until the
// post-apply watermark has advanced to its commit_ts. The "post-GC"
// watermark represents a lower bound on the latest commit_ts whose txn log
// could have had GC reclaim all its resources. Finally, the "pre-truncate"
// watermark represents an (exclusive) upper bound on the timestamps whose
// metadata entries could have had their memory reclaimed (e.g., via
// zeroing, unmapping, or overwriting). Any timestamp whose metadata entry
// could potentially be dereferenced must be "reserved" via the "safe_ts"
// API to prevent the pre-truncate watermark from advancing past it and
// allowing its metadata entry to be reclaimed.
// could have had GC reclaim all its resources. The txn table cannot be
// safely truncated at any timestamp after the post-GC watermark.
//
// The pre-apply watermark must either be equal to the post-apply watermark or greater by 1.
//
// Schematically:
// commit timestamps of transactions whose metadata entries have been reclaimed
// < pre-truncate watermark
// <= commit timestamps of transactions completely garbage-collected
// commit timestamps of transactions completely garbage-collected
// <= post-GC watermark
// <= commit timestamps of transactions applied to shared view
// <= post-apply watermark
Expand All @@ -196,114 +189,24 @@ class server_t
pre_apply,
post_apply,
post_gc,
pre_truncate,
// This should always be last.
count
};

// An array of monotonically nondecreasing timestamps, or "watermarks", that
// represent the progress of system maintenance tasks with respect to txn
// timestamps. See `watermark_type_t` for a full explanation.
static inline std::array<std::atomic<gaia_txn_id_t>, common::get_enum_value(watermark_type_t::count)> s_watermarks{};

// A global array in which each session thread publishes a "safe timestamp"
// that it needs to protect from memory reclamation. The minimum of all
// published "safe timestamps" is an upper bound below which all txn
// metadata entries can be safely reclaimed.
//
// Each thread maintains 2 publication entries rather than 1, in order to
// tolerate validation failures. See the reserve_safe_ts() implementation
// for a full explanation.
//
// Before using the safe_ts API, a thread must first reserve an index in
// this array using reserve_safe_ts_index(). When a thread is no longer
// using the safe_ts API (e.g., at session exit), it should call
// release_safe_ts_index() to make this index available for use by other
// threads.
//
// The only clients of the safe_ts API are expected to be session threads,
// so we only allocate enough entries for the maximum allowed number of
// session threads.
static inline std::array<std::array<std::atomic<gaia_txn_id_t>, 2>, c_session_limit>
s_safe_ts_per_thread_entries{};

// The reserved status of each index into `s_safe_ts_per_thread_entries` is
// tracked in this bitmap. Before calling any safe_ts API functions, each
// thread must reserve an index by setting a cleared bit in this bitmap.
// When it is no longer using the safe_ts API (e.g., at session exit), each
// thread should clear the bit that it set.
static inline std::array<
std::atomic<uint64_t>, s_safe_ts_per_thread_entries.size() / memory_manager::c_uint64_bit_count>
s_safe_ts_reserved_indexes_bitmap{};

static constexpr size_t c_invalid_safe_ts_index{s_safe_ts_per_thread_entries.size()};

// The current thread's index in `s_safe_ts_per_thread_entries`.
thread_local static inline size_t s_safe_ts_index{c_invalid_safe_ts_index};

private:
// Returns the current value of the given watermark.
static inline gaia_txn_id_t get_watermark(watermark_type_t watermark)
{
return s_watermarks[common::get_enum_value(watermark)].load();
}

// Returns a reference to the array entry of the given watermark.
static inline std::atomic<gaia_txn_id_t>& get_watermark_entry(watermark_type_t watermark)
{
return s_watermarks[common::get_enum_value(watermark)];
}

// Atomically advances the given watermark to the given timestamp, if the
// given timestamp is larger than the watermark's current value. It thus
// guarantees that the watermark is monotonically nondecreasing in time.
//
// Returns true if the watermark was advanced to the given value, false
// otherwise.
static bool advance_watermark(watermark_type_t watermark, gaia_txn_id_t ts);

// Reserves an index for the current thread in
// `s_safe_ts_per_thread_entries`. The entries at this index are read-write
// for the current thread and read-only for all other threads.
//
// Returns true if an index was successfully reserved, false otherwise.
static bool reserve_safe_ts_index();

// Releases the current thread's index in `s_safe_ts_per_thread_entries`,
// allowing the entries at that index to be used by other threads.
//
// This method cannot fail or throw.
static void release_safe_ts_index();

// Reserves a "safe timestamp" that protects its own and all subsequent
// metadata entries from memory reclamation, by ensuring that the
// pre-truncate watermark can never advance past it until release_safe_ts()
// is called on that timestamp.
//
// Any previously reserved "safe timestamp" value for this thread will be
// atomically replaced by the new "safe timestamp" value.
//
// This operation can fail if post-publication validation fails. See the
// reserve_safe_ts() implementation for details.
//
// Returns true if the given timestamp was successfully reserved, false
// otherwise.
static bool reserve_safe_ts(gaia_txn_id_t safe_ts);

// Releases the current thread's "safe timestamp", allowing the pre-truncate
// watermark to advance past it, signaling to GC tasks that its metadata
// entry is eligible for reclamation.
//
// This method cannot fail or throw.
static void release_safe_ts();

// This method computes a "safe truncation timestamp", i.e., an (exclusive)
// upper bound below which all txn metadata entries can be safely reclaimed.
//
// The timestamp returned is guaranteed not to exceed any "safe timestamp"
// that was reserved before this method was called.
static gaia_txn_id_t get_safe_truncation_ts();

private:
// A list of data mappings that we manage together.
// The order of declarations must be the order of data_mapping_t::index_t values!
Expand Down Expand Up @@ -438,7 +341,7 @@ class server_t

static void gc_applied_txn_logs();

static void truncate_txn_table();
static void update_txn_table_safe_truncation_point();

static gaia_txn_id_t submit_txn(gaia_txn_id_t begin_ts, int log_fd);

Expand Down
Loading

0 comments on commit fcaacf1

Please sign in to comment.