Skip to content

Commit 325d2dd

Browse files
zjw1111Copilot
andcommitted
fix review
Co-authored-by: Copilot <copilot@github.com>
1 parent d8898c0 commit 325d2dd

7 files changed

Lines changed: 99 additions & 118 deletions

File tree

src/paimon/core/mergetree/merge_tree_writer_test.cpp

Lines changed: 57 additions & 98 deletions
Large diffs are not rendered by default.

src/paimon/core/mergetree/write_buffer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ template <typename T>
4646
class MergeFunctionWrapper;
4747

4848
/// WriteBuffer manages the batch buffer for MergeTreeWriter.
49-
/// It delegates to a SortBuffer implementation (BinaryInMemorySortBuffer or
50-
/// BinaryExternalSortBuffer) based on the spillable configuration.
49+
/// It delegates to a SortBuffer implementation (InMemorySortBuffer or ExternalSortBuffer) based on
50+
/// the spillable configuration.
5151
class WriteBuffer {
5252
public:
5353
static Result<std::unique_ptr<WriteBuffer>> Create(

src/paimon/core/operation/abstract_file_store_write.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "fmt/format.h"
2525
#include "paimon/common/data/binary_row.h"
2626
#include "paimon/common/metrics/metrics_impl.h"
27-
#include "paimon/core/memory/writer_memory_manager.h"
2827
#include "paimon/core/operation/file_store_scan.h"
2928
#include "paimon/core/operation/file_system_write_restore.h"
3029
#include "paimon/core/operation/metrics/compaction_metrics.h"
@@ -83,7 +82,7 @@ AbstractFileStoreWrite::AbstractFileStoreWrite(
8382
metrics_(std::make_shared<MetricsImpl>()),
8483
logger_(Logger::GetLogger("AbstractFileStoreWrite")) {
8584
writer_memory_manager_ =
86-
std::make_shared<WriterMemoryManager>(static_cast<uint64_t>(options.GetWriteBufferSize()));
85+
std::make_unique<WriterMemoryManager>(static_cast<uint64_t>(options.GetWriteBufferSize()));
8786
cache_manager_ = std::make_shared<CacheManager>(options.GetLookupCacheMaxMemory(),
8887
options.GetLookupCacheHighPrioPoolRatio());
8988
}

src/paimon/core/operation/abstract_file_store_write.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "paimon/common/io/cache/cache_manager.h"
3030
#include "paimon/core/core_options.h"
3131
#include "paimon/core/deletionvectors/bucketed_dv_maintainer.h"
32+
#include "paimon/core/memory/writer_memory_manager.h"
3233
#include "paimon/file_store_write.h"
3334
#include "paimon/logging.h"
3435
#include "paimon/metrics.h"
@@ -61,7 +62,6 @@ class MemoryPool;
6162
class RecordBatch;
6263
class RestoreFiles;
6364
class IOManager;
64-
class WriterMemoryManager;
6565

6666
class AbstractFileStoreWrite : public FileStoreWrite {
6767
public:
@@ -132,7 +132,7 @@ class AbstractFileStoreWrite : public FileStoreWrite {
132132
std::shared_ptr<BucketedDvMaintainer::Factory> dv_maintainer_factory_;
133133
std::shared_ptr<IOManager> io_manager_;
134134
std::shared_ptr<CacheManager> cache_manager_;
135-
std::shared_ptr<WriterMemoryManager> writer_memory_manager_;
135+
std::unique_ptr<WriterMemoryManager> writer_memory_manager_;
136136

137137
CoreOptions options_;
138138
std::shared_ptr<Executor> compact_executor_;

src/paimon/core/operation/key_value_file_store_write.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ Result<std::shared_ptr<BatchWriter>> KeyValueFileStoreWrite::CreateWriter(
117117
restore_max_seq_number, trimmed_primary_keys, data_file_path_factory, key_comparator_,
118118
user_defined_seq_comparator_, merge_function_wrapper_, table_schema_->Id(), schema_,
119119
options_, compact_manager, io_manager_, pool_));
120-
return std::shared_ptr<BatchWriter>(writer);
120+
return writer;
121121
}
122122

123123
Status KeyValueFileStoreWrite::Close() {

src/paimon/core/operation/key_value_file_store_write_test.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,7 @@ TEST_F(KeyValueFileStoreWriteTest,
218218
ASSERT_EQ(commit_messages.size(), 1);
219219
}
220220

221-
TEST_F(KeyValueFileStoreWriteTest,
222-
TestWriteShouldSpillLargestWriterWhenGlobalMemoryLimitIsExceeded) {
221+
TEST_F(KeyValueFileStoreWriteTest, TestSpillSimple) {
223222
auto fields = {arrow::field("f0", arrow::utf8(), /*nullable=*/false)};
224223
arrow::Schema typed_schema(fields);
225224
::ArrowSchema schema;
@@ -241,17 +240,38 @@ TEST_F(KeyValueFileStoreWriteTest,
241240

242241
ASSERT_OK_AND_ASSIGN(std::unique_ptr<WriteContext> write_context, context_builder.Finish());
243242
ASSERT_OK_AND_ASSIGN(auto file_store_write, FileStoreWrite::Create(std::move(write_context)));
243+
auto key_value_file_store_write = dynamic_cast<KeyValueFileStoreWrite*>(file_store_write.get());
244+
auto get_writer = [&](int32_t bucket) -> std::shared_ptr<paimon::BatchWriter> {
245+
auto partition_iter = key_value_file_store_write->writers_.find(BinaryRow::EmptyRow());
246+
if (partition_iter != key_value_file_store_write->writers_.end()) {
247+
auto& buckets = partition_iter->second;
248+
auto bucket_iter = buckets.find(bucket);
249+
if (PAIMON_LIKELY(bucket_iter != buckets.end())) {
250+
return bucket_iter->second.writer;
251+
}
252+
}
253+
assert(false);
254+
return nullptr;
255+
};
244256

257+
// write bucket 0, not trigger spill
245258
ASSERT_OK(WriteSingleStringRow(file_store_write.get(), /*bucket=*/0, std::string(48, 'a')));
246259
ASSERT_EQ(TestHelper::CountChannelFiles(dir->GetFileSystem(), dir->Str()), 0);
260+
ASSERT_GT(get_writer(0)->GetMemoryUsage(), 0);
247261

248-
ASSERT_OK(WriteSingleStringRow(file_store_write.get(), /*bucket=*/1, std::string(48, 'b')));
262+
// write bucket 1, spill bucket 0 (pick largest writer)
263+
ASSERT_OK(WriteSingleStringRow(file_store_write.get(), /*bucket=*/1, std::string(32, 'b')));
249264
ASSERT_GT(TestHelper::CountChannelFiles(dir->GetFileSystem(), dir->Str()), 0);
265+
ASSERT_EQ(get_writer(0)->GetMemoryUsage(), 0);
266+
ASSERT_GT(get_writer(1)->GetMemoryUsage(), 0);
250267

268+
// prepare commit, clean all spill files and memory buffers
251269
ASSERT_OK_AND_ASSIGN(auto commit_messages,
252270
file_store_write->PrepareCommit(/*wait_compaction=*/true));
253271
ASSERT_EQ(commit_messages.size(), 2);
254272
ASSERT_EQ(TestHelper::CountChannelFiles(dir->GetFileSystem(), dir->Str()), 0);
273+
ASSERT_EQ(get_writer(0)->GetMemoryUsage(), 0);
274+
ASSERT_EQ(get_writer(1)->GetMemoryUsage(), 0);
255275
}
256276

257277
} // namespace paimon::test

src/paimon/testing/utils/test_helper.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -381,18 +381,21 @@ class TestHelper {
381381

382382
static int64_t CountChannelFiles(const std::shared_ptr<FileSystem>& file_system,
383383
const std::string& tmp_path) {
384-
std::vector<std::unique_ptr<BasicFileStatus>> file_statuses;
385-
EXPECT_OK(file_system->ListDir(tmp_path, &file_statuses));
384+
std::vector<std::unique_ptr<BasicFileStatus>> dir_statuses;
385+
EXPECT_OK(file_system->ListDir(tmp_path, &dir_statuses));
386386

387387
int64_t channel_file_count = 0;
388-
for (const auto& file_status : file_statuses) {
389-
const std::string path = file_status->GetPath();
390-
if (file_status->IsDir() && path.find("paimon-io-") != std::string::npos) {
391-
channel_file_count += CountChannelFiles(file_system, path);
392-
continue;
393-
}
394-
if (StringUtils::EndsWith(path, ".channel")) {
395-
++channel_file_count;
388+
for (const auto& dir_status : dir_statuses) {
389+
const std::string dir_path = dir_status->GetPath();
390+
if (dir_status->IsDir() && dir_path.find("paimon-io-") != std::string::npos) {
391+
std::vector<std::unique_ptr<BasicFileStatus>> file_statuses;
392+
EXPECT_OK(file_system->ListDir(dir_path, &file_statuses));
393+
394+
for (const auto& file_status : file_statuses) {
395+
if (StringUtils::EndsWith(file_status->GetPath(), ".channel")) {
396+
++channel_file_count;
397+
}
398+
}
396399
}
397400
}
398401
return channel_file_count;

0 commit comments

Comments
 (0)