Skip to content

Commit 8848b21

Browse files
authored
fix: close input/output streams when remote lookup file read/write failed (alibaba#380)
1 parent 02f68c8 commit 8848b21

10 files changed

Lines changed: 30 additions & 20 deletions

cmake_modules/CorrosionFetch.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# targets. Used to bring in third_party/tantivy_ffi for the tantivy-fulltext
1717
# global index (see docs/dev/tantivy_fts_migration_plan.md).
1818
#
19-
# Pinned to v0.5.0 (stable release). Requires CMake >= 3.22.
19+
# Pinned to v0.5.2 (stable release). Requires CMake >= 3.22.
2020

2121
include(FetchContent)
2222

src/paimon/core/mergetree/compact/lookup_merge_tree_compact_rewriter.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
#pragma once
18+
1819
#include "arrow/api.h"
1920
#include "paimon/core/core_options.h"
2021
#include "paimon/core/io/data_file_meta.h"
@@ -24,7 +25,9 @@
2425
#include "paimon/core/mergetree/lookup/remote_lookup_file_manager.h"
2526
#include "paimon/core/mergetree/lookup_levels.h"
2627
#include "paimon/core/schema/table_schema.h"
28+
2729
namespace paimon {
30+
2831
/// A `MergeTreeCompactRewriter` which produces changelog files by lookup for the compaction
2932
/// involving level 0 files.
3033
template <typename T>
@@ -86,7 +89,6 @@ class LookupMergeTreeCompactRewriter : public ChangelogMergeTreeRewriter {
8689
Result<std::vector<std::shared_ptr<DataFileMeta>>> NotifyRewriteCompactAfter(
8790
const std::vector<std::shared_ptr<DataFileMeta>>& files) override;
8891

89-
private:
9092
std::unique_ptr<LookupLevels<T>> lookup_levels_;
9193
std::shared_ptr<BucketedDvMaintainer> dv_maintainer_;
9294
std::shared_ptr<RemoteLookupFileManager> remote_lookup_file_manager_;

src/paimon/core/mergetree/compact/lookup_merge_tree_compact_rewriter_test.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@
5353
#include "paimon/testing/utils/io_exception_helper.h"
5454
#include "paimon/testing/utils/read_result_collector.h"
5555
#include "paimon/testing/utils/testharness.h"
56+
5657
namespace paimon::test {
58+
5759
class LookupMergeTreeCompactRewriterTest : public ::testing::TestWithParam<std::string> {
5860
public:
5961
void SetUp() override {

src/paimon/core/mergetree/lookup/remote_lookup_file_manager.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717

1818
#include "fmt/format.h"
1919
#include "paimon/common/utils/path_util.h"
20+
#include "paimon/common/utils/scope_guard.h"
2021
#include "paimon/core/mergetree/lookup/file_position.h"
2122
#include "paimon/core/mergetree/lookup/positioned_key_value.h"
23+
2224
namespace paimon {
2325

2426
RemoteLookupFileManager::RemoteLookupFileManager(
@@ -103,6 +105,15 @@ Status RemoteLookupFileManager::CopyRemoteToLocal(const std::string& remote_path
103105
Status RemoteLookupFileManager::CopyFromInputToOutput(
104106
std::unique_ptr<InputStream>&& input_stream,
105107
std::unique_ptr<OutputStream>&& output_stream) const {
108+
ScopeGuard input_close_guard([&input_stream]() -> void {
109+
Status s = input_stream->Close();
110+
(void)s;
111+
});
112+
ScopeGuard output_close_guard([&output_stream]() -> void {
113+
Status s = output_stream->Close();
114+
(void)s;
115+
});
116+
106117
auto buffer = std::make_shared<Bytes>(kBufferSize, pool_.get());
107118
PAIMON_ASSIGN_OR_RAISE(int64_t total_length, input_stream->Length());
108119
PAIMON_RETURN_NOT_OK(ValidateValueNonNegative(total_length, "input stream length"));
@@ -128,8 +139,12 @@ Status RemoteLookupFileManager::CopyFromInputToOutput(
128139
write_size += current_read_size;
129140
}
130141
PAIMON_RETURN_NOT_OK(output_stream->Flush());
131-
PAIMON_RETURN_NOT_OK(output_stream->Close());
132-
PAIMON_RETURN_NOT_OK(input_stream->Close());
142+
Status output_close_status = output_stream->Close();
143+
output_close_guard.Release();
144+
PAIMON_RETURN_NOT_OK(output_close_status);
145+
Status input_close_status = input_stream->Close();
146+
input_close_guard.Release();
147+
PAIMON_RETURN_NOT_OK(input_close_status);
133148
return Status::OK();
134149
}
135150
template Result<std::shared_ptr<DataFileMeta>>

src/paimon/core/mergetree/lookup/remote_lookup_file_manager.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ class RemoteLookupFileManager {
5050

5151
static constexpr uint64_t kBufferSize = 1024 * 1024;
5252

53-
private:
5453
int32_t level_threshold_;
5554
std::shared_ptr<MemoryPool> pool_;
5655
std::shared_ptr<DataFilePathFactory> path_factory_;

src/paimon/global_index/lucene/lucene_global_index_reader.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,10 @@ std::shared_ptr<GlobalIndexResult> LuceneGlobalIndexReader::SearchWithNoLimit(
208208

209209
Result<std::shared_ptr<GlobalIndexResult>> LuceneGlobalIndexReader::VisitFullTextSearch(
210210
const std::shared_ptr<FullTextSearch>& full_text_search) {
211-
if (full_text_search && full_text_search->min_score.has_value()) {
211+
if (!full_text_search) {
212+
return Status::Invalid("VisitFullTextSearch: null FullTextSearch pointer");
213+
}
214+
if (full_text_search->min_score.has_value()) {
212215
// The lucene backend does not support min_score pushdown. Fail loudly
213216
// instead of silently ignoring the threshold and returning unfiltered
214217
// results, which would be a correctness bug for the caller.

src/paimon/global_index/tantivy/tantivy_streaming_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ struct WriteResult {
9393
class StreamingTestFixture : public ::testing::Test {
9494
public:
9595
WriteResult BuildArchive(std::size_t n_docs,
96-
const std::string& text_template = "apple banana cherry {}") {
96+
const std::string& text_template = "apple banana cherry %zu") {
9797
auto root_dir = paimon::test::UniqueTestDirectory::Create();
9898
EXPECT_TRUE(root_dir);
9999
std::string root = root_dir->Str();
@@ -239,7 +239,7 @@ TEST(ParseArchiveHeaderFuzz, PayloadLenNegative) {
239239

240240
TEST_F(StreamingTestFixture, ConcurrentQueryOnSameReader) {
241241
// 50 docs containing "apple" in every one (all should match)
242-
auto wr = BuildArchive(50, "apple banana {}");
242+
auto wr = BuildArchive(50, "apple banana %zu");
243243
auto reader = OpenReader(wr.root_dir, wr.meta);
244244

245245
auto fts = BuildMatchAll("apple");

src/paimon/global_index/tantivy/tantivy_writer_test.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,6 @@ std::vector<PackedEntry> ParsePacked(const std::vector<uint8_t>& bytes) {
134134

135135
class TantivyGlobalIndexWriterTest : public ::testing::Test {
136136
public:
137-
std::unique_ptr<::ArrowSchema> CreateArrowSchema(
138-
const std::shared_ptr<arrow::DataType>& data_type) const {
139-
auto c_schema = std::make_unique<::ArrowSchema>();
140-
EXPECT_TRUE(arrow::ExportType(*data_type, c_schema.get()).ok());
141-
return c_schema;
142-
}
143-
144137
Result<std::vector<GlobalIndexIOMeta>> WriteIndex(
145138
const std::string& root, const std::shared_ptr<arrow::DataType>& data_type,
146139
const std::map<std::string, std::string>& options,

test/inte/append_compaction_inte_test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ class AppendCompactionInteTest : public testing::Test,
117117
auto partition = BinaryRowGenerator::GenerateRow({10}, pool_.get());
118118
int32_t bucket = 1;
119119
auto abstract_write = dynamic_cast<AbstractFileStoreWrite*>(helper->write_.get());
120+
ASSERT_NE(abstract_write, nullptr);
120121
ASSERT_OK_AND_ASSIGN(auto restore_files,
121122
abstract_write->ScanExistingFileMetas(partition, bucket));
122123
ASSERT_OK_AND_ASSIGN(

test/inte/write_and_read_inte_test.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@
4747
#include "paimon/testing/utils/test_helper.h"
4848
#include "paimon/testing/utils/testharness.h"
4949

50-
namespace paimon {
51-
class DataSplit;
52-
class RecordBatch;
53-
} // namespace paimon
54-
5550
namespace paimon::test {
5651
// This is a sdk end-to-end test demo that supports write, commit, scan, and read operations.
5752
class WriteAndReadInteTest

0 commit comments

Comments
 (0)