libfoedus-core
FOEDUS Core Library
|
Receives an arbitrary number of sorted buffers and emits one fully sorted stream of logs. More...
Receives an arbitrary number of sorted buffers and emits one fully sorted stream of logs.
Definition at line 77 of file merge_sort.hpp.
#include <merge_sort.hpp>
Classes | |
struct | AdjustComparatorMasstree |
Used in batch_sort_adjust_sort if the storage is a masstree storage. More... | |
struct | GroupifyResult |
Represents a group of consecutive logs in the current batch. More... | |
struct | InputStatus |
Current status of each input. More... | |
struct | PositionEntry |
Provides additional information for each entry we are sorting. More... | |
struct | SortEntry |
Entries we actually sort. More... | |
Public Types | |
enum | Constants { kMaxMergedPosition = 1 << 23, kLogChunk = 1 << 10, kDefaultChunkBatch = 1 << 8, kMaxLevels = 32, kWindowChunkReserveBytes = 1 << 16 } |
typedef uint32_t | MergedPosition |
Position in MergeSort's buffer. More... | |
typedef uint16_t | InputIndex |
Index in input streams. More... | |
Public Member Functions | |
MergeSort (storage::StorageId id, storage::StorageType type, Epoch base_epoch, SortedBuffer *const *inputs, uint16_t inputs_count, uint16_t max_original_pages, memory::AlignedMemory *const work_memory, uint16_t chunk_batch_size=kDefaultChunkBatch) | |
ErrorStack | next_batch () |
Executes merge-sort on several thousands of logs and provides the result as a batch. More... | |
uint32_t | fetch_logs (uint32_t sort_pos, uint32_t count, log::RecordLogType const **out) const |
To reduce L1 cache miss stall, we prefetch some number of position entries and the pointed log entries in parallel. More... | |
GroupifyResult | groupify (uint32_t begin, uint32_t limit=0) const __attribute__((always_inline)) |
Find a group of consecutive logs from the given position that have either a common log type or a common key. More... | |
void | assert_sorted () |
For debug/test only. More... | |
ErrorStack | initialize_once () override |
ErrorStack | uninitialize_once () override |
bool | is_no_merging () const __attribute__((always_inline)) |
bool | is_ended_all () const |
storage::StorageId | get_storage_id () const __attribute__((always_inline)) |
Epoch | get_base_epoch () const __attribute__((always_inline)) |
MergedPosition | get_current_count () const __attribute__((always_inline)) |
InputIndex | get_inputs_count () const __attribute__((always_inline)) |
const PositionEntry * | get_position_entries () const __attribute__((always_inline)) |
log::LogCode | get_log_type_from_sort_position (uint32_t sort_pos) const __attribute__((always_inline)) |
const SortEntry * | get_sort_entries () const __attribute__((always_inline)) |
const log::RecordLogType * | resolve_merged_position (MergedPosition pos) const __attribute__((always_inline)) |
const log::RecordLogType * | resolve_sort_position (uint32_t sort_pos) const __attribute__((always_inline)) |
void | change_log_type_at (uint32_t sort_pos, log::LogCode before, log::LogCode after) |
used to switch between two compatible log types. More... | |
storage::Page * | get_original_pages () const __attribute__((always_inline)) |
bool | are_all_single_layer_logs () const __attribute__((always_inline)) |
uint16_t | get_longest_key_length () const __attribute__((always_inline)) |
![]() | |
DefaultInitializable () | |
virtual | ~DefaultInitializable () |
DefaultInitializable (const DefaultInitializable &)=delete | |
DefaultInitializable & | operator= (const DefaultInitializable &)=delete |
ErrorStack | initialize () override final |
Typical implementation of Initializable::initialize() that provides initialize-once semantics. More... | |
ErrorStack | uninitialize () override final |
Typical implementation of Initializable::uninitialize() that provides uninitialize-once semantics. More... | |
bool | is_initialized () const override final |
Returns whether the object has been already initialized or not. More... | |
![]() | |
virtual | ~Initializable () |
typedef uint16_t foedus::snapshot::MergeSort::InputIndex |
Index in input streams.
Definition at line 85 of file merge_sort.hpp.
typedef uint32_t foedus::snapshot::MergeSort::MergedPosition |
Position in MergeSort's buffer.
We never sort more than 2^23 entries at once because we merge by chunks.
Definition at line 83 of file merge_sort.hpp.
Definition at line 87 of file merge_sort.hpp.
foedus::snapshot::MergeSort::MergeSort | ( | storage::StorageId | id, |
storage::StorageType | type, | ||
Epoch | base_epoch, | ||
SortedBuffer *const * | inputs, | ||
uint16_t | inputs_count, | ||
uint16_t | max_original_pages, | ||
memory::AlignedMemory *const | work_memory, | ||
uint16_t | chunk_batch_size = kDefaultChunkBatch |
||
) |
Definition at line 63 of file merge_sort.cpp.
References ASSERT_ND.
|
inline |
Definition at line 411 of file merge_sort.hpp.
void foedus::snapshot::MergeSort::assert_sorted | ( | ) |
For debug/test only.
Checks if sort_entries_ are indeed fully sorted. This method is wiped out in release mode.
Definition at line 695 of file merge_sort.cpp.
References ASSERT_ND, foedus::storage::hash::HashCommonLogType::assert_type(), foedus::snapshot::MergeSort::SortEntry::data_, foedus::xct::XctId::get_epoch(), foedus::xct::XctId::get_ordinal(), foedus::snapshot::MergeSort::SortEntry::get_position(), foedus::log::BaseLogType::header_, foedus::snapshot::MergeSort::PositionEntry::input_index_, foedus::storage::kArrayStorage, foedus::storage::kHashStorage, foedus::storage::kMasstreeStorage, foedus::snapshot::MergeSort::SortEntry::set(), foedus::Epoch::subtract(), and foedus::log::LogHeader::xct_id_.
|
inline |
used to switch between two compatible log types.
It's a special-purpose method, use it with caution.
Definition at line 393 of file merge_sort.hpp.
References ASSERT_ND, foedus::snapshot::MergeSort::PositionEntry::get_log_type(), get_log_type_from_sort_position(), foedus::snapshot::MergeSort::SortEntry::get_position(), foedus::log::LogHeader::get_type(), foedus::log::BaseLogType::header_, foedus::snapshot::MergeSort::PositionEntry::log_type_, foedus::log::LogHeader::log_type_code_, and resolve_merged_position().
uint32_t foedus::snapshot::MergeSort::fetch_logs | ( | uint32_t | sort_pos, |
uint32_t | count, | ||
log::RecordLogType const ** | out | ||
) | const |
To reduce L1 cache miss stall, we prefetch some number of position entries and the pointed log entries in parallel.
[in] | sort_pos | we prefetch from this sort_entries_ -> position_entries_ -> logs |
[in] | count | we prefetch upto sort_entries_[sort_pos + count - 1]. Should be at least 2 or 4 to make parallel prefetch effective. |
[out] | out | Fetched logs. |
Definition at line 273 of file merge_sort.cpp.
References ASSERT_ND, foedus::snapshot::MergeSort::InputStatus::from_compact_pos(), foedus::snapshot::MergeSort::SortEntry::get_position(), foedus::snapshot::MergeSort::PositionEntry::input_index_, is_no_merging(), and foedus::assorted::prefetch_cacheline().
|
inline |
Definition at line 368 of file merge_sort.hpp.
|
inline |
Definition at line 369 of file merge_sort.hpp.
Referenced by foedus::storage::array::ArrayComposeContext::execute(), foedus::storage::hash::HashComposeContext::execute(), and foedus::storage::masstree::MasstreeComposeContext::execute().
|
inline |
Definition at line 370 of file merge_sort.hpp.
|
inline |
Definition at line 374 of file merge_sort.hpp.
References foedus::snapshot::MergeSort::PositionEntry::get_log_type(), and foedus::snapshot::MergeSort::SortEntry::get_position().
Referenced by change_log_type_at().
|
inline |
Definition at line 412 of file merge_sort.hpp.
|
inline |
Definition at line 410 of file merge_sort.hpp.
|
inline |
Definition at line 371 of file merge_sort.hpp.
|
inline |
Definition at line 378 of file merge_sort.hpp.
Referenced by foedus::storage::array::ArrayComposeContext::execute(), and foedus::storage::hash::HashComposeContext::execute().
|
inline |
Definition at line 367 of file merge_sort.hpp.
|
inline |
Find a group of consecutive logs from the given position that have either a common log type or a common key.
[in] | begin | starting position in the sorted array. |
[in] | limit | maximum number of logs to check. If not specified, all logs in the batch. |
Some composer uses this optional method to batch-process the logs. In an ideal case, there always is a big group, and composer calls this method per hundreds of logs. But, it's also quite possible that there isn't a good group. Hence, these methods are called very frequently (potentially for each log). Worth explicit inlining.
Definition at line 534 of file merge_sort.hpp.
References ASSERT_ND, foedus::snapshot::MergeSort::GroupifyResult::count_, foedus::snapshot::MergeSort::SortEntry::get_position(), foedus::snapshot::MergeSort::GroupifyResult::has_common_key_, foedus::snapshot::MergeSort::GroupifyResult::has_common_log_code_, foedus::log::kLogCodeInvalid, foedus::snapshot::MergeSort::GroupifyResult::log_code_, foedus::snapshot::MergeSort::PositionEntry::log_type_, and UNLIKELY.
Referenced by foedus::storage::masstree::MasstreeComposeContext::execute().
|
overridevirtual |
Implements foedus::DefaultInitializable.
Definition at line 93 of file merge_sort.cpp.
References foedus::snapshot::SortedBuffer::assert_checks(), foedus::snapshot::MergeSort::InputStatus::assert_consistent(), ASSERT_ND, foedus::memory::AlignedMemory::assure_capacity(), foedus::snapshot::MergeSort::InputStatus::chunk_relative_pos_, foedus::snapshot::MergeSort::InputStatus::cur_relative_pos_, foedus::snapshot::MergeSort::InputStatus::end_absolute_pos_, foedus::memory::AlignedMemory::get_block(), foedus::snapshot::SortedBuffer::get_buffer(), foedus::snapshot::SortedBuffer::get_buffer_size(), foedus::snapshot::SortedBuffer::get_cur_block_abosulte_begin(), foedus::snapshot::SortedBuffer::get_cur_block_abosulte_end(), foedus::snapshot::SortedBuffer::get_offset(), foedus::memory::AlignedMemory::get_size(), kLogChunk, foedus::storage::kPageSize, foedus::kRetOk, foedus::snapshot::MergeSort::InputStatus::previous_chunk_relative_pos_, foedus::fs::status(), foedus::snapshot::MergeSort::InputStatus::window_, foedus::snapshot::MergeSort::InputStatus::window_offset_, foedus::snapshot::MergeSort::InputStatus::window_size_, and WRAP_ERROR_CODE.
|
inline |
Definition at line 358 of file merge_sort.hpp.
Referenced by foedus::storage::array::ArrayComposeContext::execute(), foedus::storage::hash::HashComposeContext::execute(), foedus::storage::masstree::MasstreeComposeContext::execute(), and next_batch().
|
inline |
Definition at line 356 of file merge_sort.hpp.
Referenced by fetch_logs(), and next_batch().
ErrorStack foedus::snapshot::MergeSort::next_batch | ( | ) |
Executes merge-sort on several thousands of logs and provides the result as a batch.
Composer repeatedly invokes this method until it returns false. For each invokation (batch), composer consumes the entries in sorted order, in other words sort_entries_[0] to sort_entries_[current_count_ - 1], which points to position_entries_. This level of indirection might cause more L1 cache misses (only if we could fit everything into 16 bytes!), so we do prefetching to ameriolate it.
Definition at line 143 of file merge_sort.cpp.
References ASSERT_ND, CHECK_ERROR, is_ended_all(), foedus::DefaultInitializable::is_initialized(), is_no_merging(), and foedus::kRetOk.
Referenced by foedus::storage::array::ArrayComposeContext::execute(), foedus::storage::hash::HashComposeContext::execute(), and foedus::storage::masstree::MasstreeComposeContext::execute().
|
inline |
Definition at line 379 of file merge_sort.hpp.
References ASSERT_ND, foedus::snapshot::MergeSort::InputStatus::from_compact_pos(), foedus::snapshot::MergeSort::PositionEntry::input_index_, and foedus::snapshot::MergeSort::PositionEntry::input_position_.
Referenced by change_log_type_at(), and resolve_sort_position().
|
inline |
Definition at line 384 of file merge_sort.hpp.
References ASSERT_ND, foedus::snapshot::MergeSort::SortEntry::get_position(), and resolve_merged_position().
Referenced by foedus::storage::array::ArrayComposeContext::execute(), and foedus::storage::masstree::MasstreeComposeContext::execute().
|
inlineoverridevirtual |
Implements foedus::DefaultInitializable.
Definition at line 354 of file merge_sort.hpp.
References foedus::kRetOk.