Skip to content

Commit 9012f0a

Browse files
authored
fix: release global index writer before reader to avoid mem issue when write index failed (alibaba#398)
1 parent 7b505be commit 9012f0a

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

src/paimon/core/global_index/global_index_write_task.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "paimon/common/table/special_fields.h"
2121
#include "paimon/common/types/data_field.h"
2222
#include "paimon/common/utils/arrow/status_utils.h"
23+
#include "paimon/common/utils/scope_guard.h"
2324
#include "paimon/core/core_options.h"
2425
#include "paimon/core/global_index/global_index_file_manager.h"
2526
#include "paimon/core/io/data_increment.h"
@@ -204,16 +205,21 @@ Result<std::shared_ptr<CommitMessage>> GlobalIndexWriteTask::WriteIndex(
204205
std::shared_ptr<GlobalIndexFileManager> index_file_manager,
205206
CreateGlobalIndexFileManager(table_path, table_schema, core_options, pool));
206207

208+
// create batch reader
209+
PAIMON_ASSIGN_OR_RAISE(
210+
std::unique_ptr<BatchReader> batch_reader,
211+
CreateBatchReader(table_path, field_name, indexed_split, core_options, pool));
212+
207213
// create global index writer
208214
PAIMON_ASSIGN_OR_RAISE(DataField field, table_schema->GetField(field_name));
209215
PAIMON_ASSIGN_OR_RAISE(
210216
std::shared_ptr<GlobalIndexWriter> global_index_writer,
211217
CreateGlobalIndexWriter(index_type, field, index_file_manager, core_options, pool));
212218

213-
// create batch reader
214-
PAIMON_ASSIGN_OR_RAISE(
215-
std::unique_ptr<BatchReader> batch_reader,
216-
CreateBatchReader(table_path, field_name, indexed_split, core_options, pool));
219+
ScopeGuard guard([&]() {
220+
global_index_writer.reset();
221+
batch_reader.reset();
222+
});
217223

218224
// read from data split and write to index writer
219225
PAIMON_ASSIGN_OR_RAISE(

test/inte/global_index_test.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,41 @@ TEST_P(GlobalIndexTest, TestWriteLuminaIndex) {
294294
ASSERT_TRUE(expected_commit_message->TEST_Equal(*index_commit_msg_impl));
295295
}
296296

297+
TEST_P(GlobalIndexTest, TestWriteLuminaIndexWithMismatchedDimension) {
298+
arrow::FieldVector fields = {arrow::field("f0", arrow::utf8()),
299+
arrow::field("f1", arrow::list(arrow::float32()))};
300+
auto schema = arrow::schema(fields);
301+
std::map<std::string, std::string> lumina_options = {{"lumina.index.dimension", "3"},
302+
{"lumina.index.type", "bruteforce"},
303+
{"lumina.distance.metric", "l2"},
304+
{"lumina.encoding.type", "rawf32"},
305+
{"lumina.search.parallel_number", "10"}};
306+
307+
std::map<std::string, std::string> options = {
308+
{Options::MANIFEST_FORMAT, "orc"}, {Options::FILE_FORMAT, file_format_},
309+
{Options::FILE_SYSTEM, "local"}, {Options::ROW_TRACKING_ENABLED, "true"},
310+
{Options::DATA_EVOLUTION_ENABLED, "true"}, {Options::READ_BATCH_SIZE, "1"}};
311+
312+
CreateTable(/*partition_keys=*/{}, schema, options);
313+
std::string table_path = PathUtil::JoinPath(dir_->Str(), "foo.db/bar");
314+
315+
std::vector<std::string> write_cols = schema->field_names();
316+
auto src_array = arrow::ipc::internal::json::ArrayFromJSON(arrow::struct_(fields), R"([
317+
["a", [0.0, 0.0, 0.0]],
318+
["b", [0.0, 0.0, 0.0, 0.0]]
319+
])")
320+
.ValueOrDie();
321+
322+
ASSERT_OK_AND_ASSIGN(auto commit_msgs, WriteArray(table_path, write_cols, src_array));
323+
ASSERT_OK(Commit(table_path, commit_msgs));
324+
325+
ASSERT_NOK_WITH_MSG(
326+
WriteIndex(table_path, /*partition_filters=*/{}, "f1", "lumina",
327+
/*options=*/lumina_options, Range(0, 1)),
328+
"invalid input array in LuminaIndexWriter, length of field array [1] multiplied "
329+
"dimension [3] must match length of field value array [4]");
330+
}
331+
297332
TEST_P(GlobalIndexTest, TestWriteIndex) {
298333
CreateTable();
299334
std::string table_path = PathUtil::JoinPath(dir_->Str(), "foo.db/bar");

0 commit comments

Comments
 (0)