Skip to content
Merged
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
99 changes: 99 additions & 0 deletions docs/source/user_guide/append_only_table.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,102 @@ The experimental real-time append deduplicate mode is an exception. With
rows. The table still has no primary key; deduplication is scoped to each
partition-bucket. See :doc:`realtime` for the required schema, options and
shared context.

Write Buffer
------------
By default, an append writer sends each ``RecordBatch`` straight to a data file
writer and reports no memory to the write memory manager. Writing to many
partitions or buckets at once can then use a lot of memory, because every
active ``(partition, bucket)`` writer keeps an open data file writer.

.. list-table::
:header-rows: 1

* - Option
- Default
- Description
* - ``write-buffer-for-append``
- ``false``
- Buffer written batches in memory until the next flush and report their
size to the write memory manager.
* - ``write-max-writers-to-spill``
- ``10``
- Number of active writers at which creating the next one, the 11th by
default, switches every writer to a spillable write buffer.
* - ``write-buffer-spillable``
- ``true``
- Whether a buffer enabled by ``write-buffer-for-append`` may spill.
* - ``write-buffer-size``
- ``256 mb``
- Estimated in-memory threshold shared by the write buffers of one
``FileStoreWrite``. Once reached, the largest buffer spills or flushes.
Each buffered batch counts the Arrow buffers it references up to its
last row, so columns sliced from larger columns keep more memory alive
than they count; copy them before writing. Memory held by data file
writers is not counted.
* - ``write-buffer-spill.max-disk-size``
- unlimited
- Disk budget for the spill files of one writer, checked before each spill,
so a single spill can go past it.

Spill files are temporary Arrow IPC files under the directory set with
``WriteContextBuilder::WithTempDirectory()``, written and read with the
threads set by ``WriteContextBuilder::SetWriteBufferSpillThreadNumber()``.
Without a temporary directory, a buffer never spills: under memory pressure it
writes data files instead. The spill directory is created under it when the
first writer that may spill is created, or when the writers switch. If that
fails, e.g. because the path is a regular file, the write logs the path and the
reason as a warning once, its buffers write data files instead of spilling and
its writers never switch; no rows are lost and no spill file is left. Failing to
write or close a spill file is an error instead, as described below.

When ``write-buffer-for-append = true``, each writer buffers its batches. Under
memory pressure, a spillable buffer moves them to a spill file; otherwise the
writer flushes them to data files. ``PrepareCommit()`` replays the spill files
and the in-memory batches, in write order, into data files and then deletes the
spill files.

Independently of ``write-buffer-for-append``, once a write has
``write-max-writers-to-spill`` active writers, creating the next one first
switches every active writer, and every writer created afterwards, to a
spillable write buffer. The switch requires a temporary directory whose spill
directory can be created, and overrides ``write-buffer-spillable = false``. A
writer that already has a buffer keeps its rows and starts spilling. As in
Paimon Java, a writer without one closes the data files it was writing, reads
their rows back into its new buffer ahead of later rows, so the bucket keeps its
write order, and deletes the files. The rows count toward the shared
``write-buffer-size`` as they are read back, so the largest buffers spill, or
flush data files once their spill files reach
``write-buffer-spill.max-disk-size``, before the switched writers together go
past it. If closing or reading them back fails, the files are deleted anyway and
the writer fails as described below.

The disk budget is checked by each writer before it spills: once the spill
files of a writer reach ``write-buffer-spill.max-disk-size``, the writer
flushes its buffered rows to data files instead, which releases the spill
files. A spill file that fails to be deleted keeps counting toward the budget
and is deleted again on the next flush or close.

The write buffer does not apply to:

- writes whose schema has BLOB fields written to dedicated ``.blob`` files,
including a write of selected columns that contains such a field. BLOB
fields stored inline through ``blob-descriptor-field`` or
``blob-view-field`` are buffered and can switch.
- real-time writes. See :doc:`realtime` for how real-time stores spill.

Closing a writer deletes its spill files and the data files it flushed that
were never returned by ``PrepareCommit()``, even when syncing its compaction
fails.

After an I/O error from ``Write()`` or ``PrepareCommit()``, close the
``FileStoreWrite`` and write the rows since the last commit again with a new
one. Within one writer:

- a failed replay discards its partial data files and keeps the buffered rows,
so the next flush of that writer does not duplicate them.
- once a writer accepted rows it can no longer commit exactly once, e.g. when
closing or reading back its data files fails during a switch, or a spill or
flush fails after a batch was accepted, every later ``Write()`` or
``PrepareCommit()`` that reaches it returns the error, instead of committing
without those rows or with them twice.
14 changes: 13 additions & 1 deletion include/paimon/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,9 @@ struct PAIMON_EXPORT Options {
static const char WRITE_BATCH_SIZE[];

/// "write-buffer-size" - Amount of data to build up in memory before converting to a sorted
/// on-disk file. The default value is 256 mb
/// on-disk file. For an append-only table with write buffers, the estimated memory shared by
/// the write buffers of one write before the largest one spills or flushes data files. The
/// default value is 256 mb
static const char WRITE_BUFFER_SIZE[];

/// "write-buffer-spillable" - Whether the write buffer can be spillable. Default value is true.
Expand All @@ -267,6 +269,16 @@ struct PAIMON_EXPORT Options {
/// works when the write buffer spill is enabled. Default value is unlimited.
static const char WRITE_BUFFER_SPILL_MAX_DISK_SIZE[];

/// "write-buffer-for-append" - This option only works for append-only tables. Whether the write
/// uses a write buffer to avoid out-of-memory errors. Default value is false.
static const char WRITE_BUFFER_FOR_APPEND[];

/// "write-max-writers-to-spill" - When writing to an append-only table, if the number of
/// active writers is greater than this option, all writers switch to spillable write buffers
/// to avoid out-of-memory errors, even with `write-buffer-spillable` = false. This requires a
/// temporary directory for spill files. Default value is 10.
static const char WRITE_MAX_WRITERS_TO_SPILL[];

/// "local-sort.max-num-file-handles" - The maximal fan-in for external merge sort. It limits
/// the number of file handles. If it is too small, may cause intermediate merging. But if it is
/// too large, it will cause too many files opened at the same time, consume memory and lead to
Expand Down
4 changes: 4 additions & 0 deletions include/paimon/file_store_write.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ class PAIMON_EXPORT FileStoreWrite {
/// @note A failure while writing or flushing a real-time batch invalidates the writer and its
/// `RealtimeContext`. Recreate both and let upstream replay input from the durable
/// recovery offset persisted in the snapshot; retrying the failed writer is unsupported.
/// @note For an append-only table with write buffers, an I/O error from `Write()`, e.g. while
/// spilling, may leave this writer unable to commit the rows it accepted exactly once,
/// and later calls then return the error. Close it and write the rows since the last
/// commit again with a new one.
virtual Status Write(std::unique_ptr<RecordBatch>&& batch) = 0;

/// Slices the current in-memory real-time data into a sealed segment so it can be reclaimed
Expand Down
2 changes: 2 additions & 0 deletions src/paimon/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ set(PAIMON_CORE_SRCS
core/append/append_file_compaction.cpp
core/append/append_file_compaction_planner.cpp
core/append/append_only_writer.cpp
core/append/append_write_buffer.cpp
core/append/bucketed_append_compact_manager.cpp
core/bucket/bucket_select_converter.cpp
core/append/append_compact_task.cpp
Expand Down Expand Up @@ -868,6 +869,7 @@ if(PAIMON_BUILD_TESTS)
SOURCES
core/append/append_file_compaction_planner_test.cpp
core/append/append_only_writer_test.cpp
core/append/append_write_buffer_test.cpp
core/append/append_compact_coordinator_test.cpp
core/append/append_file_index_write_test.cpp
core/append/bucketed_append_compact_manager_test.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,10 @@ class MapSharedShreddingReadPlanFactoryTest : public ::testing::Test {
const std::optional<std::vector<std::string>>& write_cols, int64_t max_sequence_number,
const std::shared_ptr<DataFilePathFactory>& path_factory,
const std::shared_ptr<CompactManager>& compact_manager) const {
return std::make_unique<AppendOnlyWriter>(core_options, schema_id, logical_schema,
write_cols, max_sequence_number, path_factory,
compact_manager, pool_);
return std::make_unique<AppendOnlyWriter>(
core_options, schema_id, logical_schema, write_cols, max_sequence_number, path_factory,
compact_manager, /*use_write_buffer=*/false, /*io_manager=*/nullptr,
/*enable_multi_thread_spill=*/false, /*files_reader_factory=*/nullptr, pool_);
}

private:
Expand Down
2 changes: 2 additions & 0 deletions src/paimon/common/defs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ const char Options::WRITE_BATCH_SIZE[] = "write.batch-size";
const char Options::WRITE_BUFFER_SIZE[] = "write-buffer-size";
const char Options::WRITE_BUFFER_SPILLABLE[] = "write-buffer-spillable";
const char Options::WRITE_BUFFER_SPILL_MAX_DISK_SIZE[] = "write-buffer-spill.max-disk-size";
const char Options::WRITE_BUFFER_FOR_APPEND[] = "write-buffer-for-append";
const char Options::WRITE_MAX_WRITERS_TO_SPILL[] = "write-max-writers-to-spill";
const char Options::LOCAL_SORT_MAX_NUM_FILE_HANDLES[] = "local-sort.max-num-file-handles";
const char Options::SPILL_COMPRESSION[] = "spill-compression";
const char Options::SPILL_COMPRESSION_ZSTD_LEVEL[] = "spill-compression.zstd-level";
Expand Down
1 change: 1 addition & 0 deletions src/paimon/core/append/append_compact_coordinator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ std::unique_ptr<AppendOnlyFileStoreWrite> CreateFileStoreWrite(
/*ignore_previous_files=*/true,
/*is_streaming_mode=*/false,
/*ignore_num_bucket_check=*/false,
/*enable_multi_thread_spill=*/false,
/*realtime_context=*/nullptr, executor, pool);
}

Expand Down
Loading
Loading