Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
MBkkt committed Aug 10, 2023
1 parent f00490a commit 7601f8c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
7 changes: 1 addition & 6 deletions core/index/index_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -802,9 +802,7 @@ bool IndexWriter::FlushRequired(const segment_writer& segment) const noexcept {
const auto docs = segment.buffered_docs();
const auto memory = segment.memory_active();

return (docs_max != 0 && docs_max <= docs) || // too many docs
(memory_max != 0 && memory_max <= memory) || // too much memory
doc_limits::eof(docs); // segment is full
return memory_max <= memory || docs_max <= docs;
}

void IndexWriter::FlushContext::Emplace(ActiveSegmentContext&& active) {
Expand Down Expand Up @@ -1762,7 +1760,6 @@ IndexWriter::ActiveSegmentContext IndexWriter::GetSegmentContext() try {
// FlushContext::context_mutex_ to return their segment_context
if (const auto segment_count_max =
segment_limits_.segment_count_max.load(std::memory_order_relaxed);
segment_count_max != 0 && // '<' to account for +1 reservation
segment_count_max < segments_active) {
segments_active_.fetch_sub(1, std::memory_order_relaxed);
return {};
Expand Down Expand Up @@ -1792,9 +1789,7 @@ IndexWriter::ActiveSegmentContext IndexWriter::GetSegmentContext() try {

// recreate writer if it reserved more memory than allowed by current limits
if (auto segment_memory_max = segment_limits_.segment_memory_max.load();
segment_memory_max != 0 &&
segment_memory_max < segment_ctx->writer_->memory_reserved()) {
segment_ctx->writer_.reset(); // reset before create new
segment_ctx->writer_ = segment_writer::make(segment_ctx->dir_, options);
}

Expand Down
37 changes: 23 additions & 14 deletions core/index/index_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,17 @@ struct SegmentOptions {
// 0 == unlimited
size_t segment_count_max{0};

// Flush the segment to the repository after its total document
// count (live + masked) grows beyond this byte limit, in-flight
// documents will still be written to the segment before flush
// 0 == unlimited
size_t segment_docs_max{0};

// Flush the segment to the repository after its in-memory size
// grows beyond this byte limit, in-flight documents will still be
// written to the segment before flush
// 0 == unlimited
size_t segment_memory_max{0};

// Flush the segment to the repository after its total document
// count (live + masked) grows beyond this byte limit, in-flight
// documents will still be written to the segment before flush
// 0 == unlimited
uint32_t segment_docs_max{0};
};

// Progress report callback types for commits.
Expand Down Expand Up @@ -733,22 +733,31 @@ class IndexWriter : private util::noncopyable {

private:
struct SegmentLimits {
private:
// TODO(MBkkt) Change zero meaning
static constexpr auto kSizeMax = std::numeric_limits<size_t>::max();
static constexpr auto kDocsMax = std::numeric_limits<uint32_t>::max() - 2;
static auto ZeroMax(auto value, auto max) noexcept {
return std::min(value - 1, max - 1) + 1;
}

public:
// see segment_options::max_segment_count
std::atomic_size_t segment_count_max;
// see segment_options::max_segment_docs
std::atomic_size_t segment_docs_max;
// see segment_options::max_segment_memory
std::atomic_size_t segment_memory_max;
// see segment_options::max_segment_docs
std::atomic_uint32_t segment_docs_max;

explicit SegmentLimits(const SegmentOptions& opts) noexcept
: segment_count_max(opts.segment_count_max),
segment_docs_max(opts.segment_docs_max),
segment_memory_max(opts.segment_memory_max) {}
: segment_count_max{ZeroMax(opts.segment_count_max, kSizeMax)},
segment_memory_max{ZeroMax(opts.segment_memory_max, kSizeMax)},
segment_docs_max{ZeroMax(opts.segment_docs_max, kDocsMax)} {}

SegmentLimits& operator=(const SegmentOptions& opts) noexcept {
segment_count_max.store(opts.segment_count_max);
segment_docs_max.store(opts.segment_docs_max);
segment_memory_max.store(opts.segment_memory_max);
segment_count_max.store(ZeroMax(opts.segment_count_max, kSizeMax));
segment_memory_max.store(ZeroMax(opts.segment_memory_max, kSizeMax));
segment_docs_max.store(ZeroMax(opts.segment_docs_max, kDocsMax));
return *this;
}
};
Expand Down

0 comments on commit 7601f8c

Please sign in to comment.