Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/paimon/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ set(PAIMON_CORE_SRCS
core/realtime/realtime_store_read_pipeline.cpp
core/realtime/realtime_primary_key_writer.cpp
core/realtime/realtime_utils.cpp
core/realtime/realtime_writer.cpp
core/postpone/postpone_bucket_writer.cpp
core/schema/arrow_schema_validator.cpp
core/schema/schema_manager.cpp
Expand Down
19 changes: 6 additions & 13 deletions src/paimon/core/operation/key_value_file_store_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <optional>
#include <vector>

#include "arrow/c/bridge.h"
#include "paimon/common/data/binary_row.h"
#include "paimon/common/table/special_fields.h"
#include "paimon/core/compact/noop_compact_manager.h"
Expand All @@ -38,6 +37,7 @@
#include "paimon/core/realtime/realtime_context_impl.h"
#include "paimon/core/realtime/realtime_primary_key_writer.h"
#include "paimon/core/realtime/realtime_schema_layout.h"
#include "paimon/core/realtime/realtime_writer.h"
#include "paimon/core/schema/table_schema.h"
#include "paimon/core/utils/file_store_path_factory.h"
#include "paimon/core/utils/primary_key_table_utils.h"
Expand Down Expand Up @@ -139,20 +139,13 @@ Result<std::shared_ptr<BatchWriter>> KeyValueFileStoreWrite::CreateWriter(
if (!realtime_schema_layout_) {
return Status::Invalid("PK realtime schema layout is null");
}
auto c_write_schema = std::make_unique<ArrowSchema>();
PAIMON_RETURN_NOT_OK_FROM_ARROW(arrow::ExportSchema(
*realtime_schema_layout_->StoreWriteSchema(), c_write_schema.get()));
RealtimeStoreCreateRequest request{std::move(c_write_schema), options_.ToMap(), pool_,
RealtimeStoreMode::PRIMARY_KEY,
options_.GetRealtimeStoreStatisticsMode()};
if (options_.RealtimeSpillEnabled()) {
request.temp_directory = io_manager_ ? io_manager_->GetTempDir() : "";
request.file_system = options_.GetFileSystem();
}
PAIMON_ASSIGN_OR_RAISE(
RealtimeStoreState store_state,
realtime_context_impl->GetOrCreateRealtimeStore(
std::move(request), RealtimePartitionBucket(partition_map, bucket)));
RealtimeWriter::GetOrCreateStore(
realtime_context_impl, RealtimePartitionBucket(partition_map, bucket),
realtime_schema_layout_->StoreWriteSchema(), RealtimeStoreMode::PRIMARY_KEY,
options_, io_manager_ ? io_manager_->GetTempDir() : "",
/*deduplicate_key_fields=*/{}, pool_));
realtime_store_state = std::move(store_state);
compact_manager = std::make_shared<NoopCompactManager>();
} else {
Expand Down
92 changes: 11 additions & 81 deletions src/paimon/core/realtime/realtime_append_only_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,12 @@

#include "paimon/core/realtime/realtime_append_only_writer.h"

#include <algorithm>
#include <cstdint>
#include <iterator>
#include <limits>
#include <optional>
#include <utility>
#include <vector>

#include "arrow/api.h"
#include "arrow/c/bridge.h"
#include "arrow/c/helpers.h"
#include "paimon/common/reader/concat_batch_reader.h"
#include "paimon/common/table/special_fields.h"
#include "paimon/common/types/row_kind.h"
Expand Down Expand Up @@ -63,19 +58,11 @@ Result<std::shared_ptr<RealtimeAppendOnlyWriter>> RealtimeAppendOnlyWriter::Crea
}
PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<RealtimeContextImpl> realtime_context_impl,
RealtimeContextImpl::Cast(realtime_context));
auto write_schema = std::make_unique<ArrowSchema>();
PAIMON_RETURN_NOT_OK_FROM_ARROW(
arrow::ExportSchema(*schema_layout->StoreWriteSchema(), write_schema.get()));
RealtimeStoreCreateRequest request{std::move(write_schema), options.ToMap(), memory_pool,
RealtimeStoreMode::APPEND_ONLY,
options.GetRealtimeStoreStatisticsMode()};
if (options.RealtimeSpillEnabled()) {
request.temp_directory = temp_directory;
request.file_system = options.GetFileSystem();
}
PAIMON_ASSIGN_OR_RAISE(RealtimeStoreState store_state,
realtime_context_impl->GetOrCreateRealtimeStore(
std::move(request), RealtimePartitionBucket(partition, bucket)));
PAIMON_ASSIGN_OR_RAISE(
RealtimeStoreState store_state,
GetOrCreateStore(realtime_context_impl, RealtimePartitionBucket(partition, bucket),
schema_layout->StoreWriteSchema(), RealtimeStoreMode::APPEND_ONLY, options,
temp_directory, /*deduplicate_key_fields=*/{}, memory_pool));
return std::shared_ptr<RealtimeAppendOnlyWriter>(new RealtimeAppendOnlyWriter(
store_state.store, file_writer, schema_layout, store_state.initial_offset, memory_pool));
}
Expand All @@ -85,11 +72,10 @@ RealtimeAppendOnlyWriter::RealtimeAppendOnlyWriter(
const std::shared_ptr<AppendOnlyWriter>& file_writer,
const std::shared_ptr<RealtimeSchemaLayout>& schema_layout, int64_t next_offset,
const std::shared_ptr<MemoryPool>& memory_pool)
: arrow_pool_(GetArrowPool(memory_pool)),
realtime_store_(realtime_store),
: RealtimeWriter(realtime_store, next_offset, "append"),
arrow_pool_(GetArrowPool(memory_pool)),
file_writer_(file_writer),
schema_layout_(schema_layout),
next_offset_(next_offset) {}
schema_layout_(schema_layout) {}

Status RealtimeAppendOnlyWriter::Write(std::unique_ptr<RecordBatch>&& batch) {
if (!batch || !batch->GetData()) {
Expand Down Expand Up @@ -120,46 +106,12 @@ Status RealtimeAppendOnlyWriter::Write(std::unique_ptr<RecordBatch>&& batch) {
return Status::OK();
}

// TODO(xinyu.lxy): Refactor the shared seal logic with RealtimePrimaryKeyWriter.
Status RealtimeAppendOnlyWriter::SealCurrentSegment(bool allow_spill) {
auto* two_phase = dynamic_cast<TwoPhaseSealableRealtimeStore*>(realtime_store_.get());
PAIMON_ASSIGN_OR_RAISE(std::optional<std::shared_ptr<RealtimeSegmentHandle>> segment,
!allow_spill && two_phase ? two_phase->FreezeBuildingSegment()
: realtime_store_->SealForCommit());
std::lock_guard<std::mutex> lock(realtime_store_mutex_);
if (!segment) {
return Status::OK();
}
if (!segment.value()) {
return Status::Invalid("append real-time store sealed a null segment");
}
has_building_data_ = next_offset_ > segment.value()->GetOffsetRange().end;
sealed_segments_.push_back(std::move(segment.value()));
return Status::OK();
}

Status RealtimeAppendOnlyWriter::Seal() {
std::lock_guard<std::mutex> lock(prepare_mutex_);
return SealCurrentSegment(/*allow_spill=*/true);
}

Result<CommitIncrement> RealtimeAppendOnlyWriter::PrepareCommit(bool wait_compaction) {
std::lock_guard<std::mutex> lock(prepare_mutex_);
PAIMON_RETURN_NOT_OK(SealCurrentSegment(/*allow_spill=*/false));
std::vector<std::shared_ptr<RealtimeSegmentHandle>> segments;
{
std::lock_guard<std::mutex> realtime_store_lock(realtime_store_mutex_);
segments.swap(sealed_segments_);
}
Result<CommitIncrement> RealtimeAppendOnlyWriter::PrepareCommitImpl(
const std::vector<std::shared_ptr<RealtimeSegmentHandle>>& segments, bool wait_compaction) {
for (const std::shared_ptr<RealtimeSegmentHandle>& segment : segments) {
PAIMON_RETURN_NOT_OK(FlushSegment(segment));
}
PAIMON_ASSIGN_OR_RAISE(CommitIncrement increment, file_writer_->PrepareCommit(wait_compaction));
if (!segments.empty()) {
increment.SetRealtimeOffsetRange(OffsetRange(segments.front()->GetOffsetRange().begin,
segments.back()->GetOffsetRange().end));
}
return increment;
return file_writer_->PrepareCommit(wait_compaction);
}

Status RealtimeAppendOnlyWriter::FlushSegment(
Expand Down Expand Up @@ -238,28 +190,6 @@ Status RealtimeAppendOnlyWriter::Close() {
return file_writer_->Close();
}

bool RealtimeAppendOnlyWriter::HasUnpreparedRealtimeData() const {
std::lock_guard<std::mutex> lock(realtime_store_mutex_);
return has_building_data_ || !sealed_segments_.empty();
}

void RealtimeAppendOnlyWriter::ReleaseCommittedRealtimeSegments(int64_t committed_end_offset) {
std::vector<std::shared_ptr<RealtimeSegmentHandle>> retired_segments;
{
std::lock_guard<std::mutex> prepare_lock(prepare_mutex_);
std::lock_guard<std::mutex> store_lock(realtime_store_mutex_);
auto retained_end = std::stable_partition(
sealed_segments_.begin(), sealed_segments_.end(),
[committed_end_offset](const std::shared_ptr<RealtimeSegmentHandle>& segment) {
return segment->GetOffsetRange().end > committed_end_offset;
});
retired_segments.assign(std::make_move_iterator(retained_end),
std::make_move_iterator(sealed_segments_.end()));
sealed_segments_.erase(retained_end, sealed_segments_.end());
}
// Release Arrow buffers and spill files without holding the writer locks.
}

std::shared_ptr<Metrics> RealtimeAppendOnlyWriter::GetMetrics() const {
return file_writer_->GetMetrics();
}
Expand Down
22 changes: 5 additions & 17 deletions src/paimon/core/realtime/realtime_append_only_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@
#include <cstdint>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <vector>

#include "paimon/core/realtime/realtime_schema_layout.h"
#include "paimon/core/utils/batch_writer.h"
#include "paimon/core/realtime/realtime_writer.h"
#include "paimon/realtime/realtime_store.h"

namespace arrow {
Expand All @@ -42,7 +41,7 @@ class CoreOptions;
class MemoryPool;
class RealtimeContext;

class RealtimeAppendOnlyWriter : public BatchWriter {
class RealtimeAppendOnlyWriter : public RealtimeWriter {
public:
static Result<std::shared_ptr<RealtimeAppendOnlyWriter>> Create(
const std::map<std::string, std::string>& partition, int32_t bucket,
Expand All @@ -53,10 +52,6 @@ class RealtimeAppendOnlyWriter : public BatchWriter {

Status Write(std::unique_ptr<RecordBatch>&& batch) override;

Status Seal() override;

Result<CommitIncrement> PrepareCommit(bool wait_compaction) override;

Status Compact(bool full_compaction) override;

uint64_t GetMemoryUsage() const override;
Expand All @@ -69,9 +64,6 @@ class RealtimeAppendOnlyWriter : public BatchWriter {

Status Close() override;

bool HasUnpreparedRealtimeData() const override;
void ReleaseCommittedRealtimeSegments(int64_t committed_end_offset) override;

std::shared_ptr<Metrics> GetMetrics() const override;

private:
Expand All @@ -81,17 +73,13 @@ class RealtimeAppendOnlyWriter : public BatchWriter {
int64_t next_offset, const std::shared_ptr<MemoryPool>& memory_pool);

Status FlushSegment(const std::shared_ptr<RealtimeSegmentHandle>& segment);
Status SealCurrentSegment(bool allow_spill);
Result<CommitIncrement> PrepareCommitImpl(
const std::vector<std::shared_ptr<RealtimeSegmentHandle>>& segments,
bool wait_compaction) override;

std::shared_ptr<arrow::MemoryPool> arrow_pool_;
std::shared_ptr<RealtimeStore> realtime_store_;
std::shared_ptr<AppendOnlyWriter> file_writer_;
std::shared_ptr<RealtimeSchemaLayout> schema_layout_;
std::vector<std::shared_ptr<RealtimeSegmentHandle>> sealed_segments_;
int64_t next_offset_;
bool has_building_data_ = false;
mutable std::mutex realtime_store_mutex_;
std::mutex prepare_mutex_;
};

} // namespace paimon
Loading
Loading