Skip to content

Commit 0e0b80c

Browse files
[Refactor] Move partition chunk writer into connector common (#76136)
Signed-off-by: alvin-phoenix-ai <alvin.zhao@phoenixdata.ai>
1 parent c7b961c commit 0e0b80c

36 files changed

Lines changed: 360 additions & 237 deletions

be/AGENTS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ Connector contracts, DataSource, DataSourceProvider default mechanics, and gener
280280
Reusable connector sink/file-writer helpers above connector contracts, including sink commit/profile types and shared writer-side utilities, without concrete connector registry composition, service, HTTP, or full Exec coupling.
281281
- Targets: `ConnectorCommon`
282282
- Allowed internal include prefixes: `connector/common/`, `connector_primitive/`, `compute_env/`, `cache/`, `storage_primitive/`, `exec_primitive/`, `exprs/`, `runtime/`, `platform/`, `formats/`, `fs/`, `io/`, `column/`, `types/`, `common/`, `base/`, `gutil/`, `gen_cpp/`
283-
- Allowed target deps: `ConnectorPrimitive`, `ComputeEnv`, `Cache`, `StoragePrimitive`, `ExecPrimitive`, `Expr`, `Runtime`, `Platform`, `FormatCore`, `FileSystem`, `IO`, `ColumnSortCore`, `ChunkCore`, `ColumnCore`, `Types`, `Common`, `Base`, `Gutil`, `StarRocksGen`
283+
- Allowed target deps: `ComputeEnv`, `ConnectorPrimitive`, `Cache`, `StoragePrimitive`, `ExecPrimitive`, `Expr`, `Runtime`, `Platform`, `FormatCore`, `FileSystem`, `IO`, `ColumnSortCore`, `ChunkCore`, `ColumnCore`, `Types`, `Common`, `Base`, `Gutil`, `StarRocksGen`
284284
- Core tests: `connector_common_test`
285285
- Remediation: Keep ConnectorCommon limited to reusable connector sink/file-writer helpers expressed through connector contracts and compute/runtime lower layers; move concrete connectors, registry wiring, service, HTTP, storage-engine-specific behavior, and full Exec integration upward.
286286

be/module_boundary_manifest.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@
787787
],
788788
"forbidden_include_prefixes": ["connector/", "exec/", "storage/", "service/", "http/", "cache/", "util/"],
789789
"forbidden_includes": [
790-
"connector/partitioned_connector_chunk_sink.h",
790+
"connector/common/partitioned_connector_chunk_sink.h",
791791
"connector/connector_registry.h",
792792
"exec/exec_env.h"
793793
],
@@ -827,9 +827,9 @@
827827
"connector/connector_registry.h",
828828
"exec/exec_env.h"
829829
],
830-
"allowed_target_deps": ["ConnectorPrimitive", "ComputeEnv", "Cache", "StoragePrimitive", "ExecPrimitive", "Expr", "Runtime", "Platform", "FormatCore", "FileSystem", "IO", "ColumnSortCore", "ChunkCore", "ColumnCore", "Types", "Common", "Base", "Gutil", "StarRocksGen"],
830+
"allowed_target_deps": ["ComputeEnv", "ConnectorPrimitive", "Cache", "StoragePrimitive", "ExecPrimitive", "Expr", "Runtime", "Platform", "FormatCore", "FileSystem", "IO", "ColumnSortCore", "ChunkCore", "ColumnCore", "Types", "Common", "Base", "Gutil", "StarRocksGen"],
831831
"allowed_test_targets": ["connector_common_test"],
832-
"allowed_test_link_deps": ["ConnectorCommon", "ConnectorPrimitive", "ComputeEnv", "Cache", "StoragePrimitive", "ExecPrimitive", "Expr", "Runtime", "Platform", "FormatCore", "FileSystem", "IO", "ColumnSortCore", "ChunkCore", "ColumnCore", "Types", "Common", "Base", "Gutil", "StarRocksGen"],
832+
"allowed_test_link_deps": ["ConnectorCommon", "ComputeEnv", "ConnectorPrimitive", "Cache", "StoragePrimitive", "ExecPrimitive", "Expr", "Runtime", "Platform", "FormatCore", "FileSystem", "IO", "ColumnSortCore", "ChunkCore", "ColumnCore", "Types", "Common", "Base", "Gutil", "StarRocksGen"],
833833
"remediation": "Keep ConnectorCommon limited to reusable connector sink/file-writer helpers expressed through connector contracts and compute/runtime lower layers; move concrete connectors, registry wiring, service, HTTP, storage-engine-specific behavior, and full Exec integration upward."
834834
},
835835
{

be/src/connector/CMakeLists.txt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,27 @@ set(LIBRARY_OUTPUT_PATH "${BUILD_DIR}/src/connector")
1616

1717
ADD_BE_LIB(ConnectorCommon
1818
common/connector_sink_commit.cpp
19+
common/connector_sink_executor.cpp
20+
common/hive_partition_utils.cpp
21+
common/partition_chunk_writer.cpp
22+
common/partition_chunk_writer_memory_manager.cpp
23+
common/partitioned_connector_chunk_sink.cpp
24+
common/spill_partition_chunk_writer.cpp
1925
common/utils.cpp
2026
)
2127

2228
target_link_libraries(ConnectorCommon PUBLIC
29+
ComputeEnv
30+
ConnectorPrimitive
31+
Runtime
32+
Platform
2333
FormatCore
34+
FileSystem
35+
IO
36+
ColumnSortCore
37+
ChunkCore
38+
ColumnCore
39+
Types
2440
Common
2541
Base
2642
Gutil
@@ -34,15 +50,10 @@ ADD_BE_LIB(Connector
3450
file_connector.cpp
3551
file_scan_metrics.cpp
3652
iceberg_connector.cpp
37-
partitioned_connector_chunk_sink.cpp
3853
file_chunk_sink.cpp
3954
hive_chunk_sink.cpp
4055
iceberg_chunk_sink.cpp
41-
hive_utils.cpp
4256
iceberg_utils.cpp
43-
partition_chunk_writer.cpp
44-
partition_chunk_writer_memory_manager.cpp
45-
connector_sink_executor.cpp
4657
deletion_vector/deletion_vector.cpp
4758
iceberg_delete_sink.cpp
4859
iceberg_row_delta_sink.cpp

be/src/connector/connector_sink_executor.cpp renamed to be/src/connector/common/connector_sink_executor.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "connector/connector_sink_executor.h"
15+
#include "connector/common/connector_sink_executor.h"
1616

1717
#include "column/chunk.h"
1818
#include "common/config_lake_fwd.h"
1919
#include "common/config_storage_fwd.h"
2020
#include "common/status.h"
2121
#include "common/system/cpu_info.h"
2222
#include "compute_env/load_spill/load_chunk_spiller.h"
23-
#include "connector/partition_chunk_writer.h"
2423
#include "platform/store_path.h"
2524
#include "runtime/current_thread.h"
2625

@@ -65,7 +64,7 @@ void ChunkSpillTask::run() {
6564

6665
void MergeBlockTask::run() {
6766
SCOPED_THREAD_LOCAL_MEM_TRACKER_SETTER(_mem_tracker);
68-
auto st = _writer->merge_blocks();
67+
auto st = _merge_func();
6968
if (_cb) {
7069
_cb(st);
7170
}

be/src/connector/connector_sink_executor.h renamed to be/src/connector/common/connector_sink_executor.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
#include <fmt/format.h>
1818

19+
#include <functional>
1920
#include <map>
21+
#include <memory>
22+
#include <string>
2023
#include <utility>
2124

2225
#include "column/chunk.h"
@@ -30,8 +33,6 @@ class MemTracker;
3033

3134
namespace starrocks::connector {
3235

33-
class SpillPartitionChunkWriter;
34-
3536
class ConnectorSinkExecutor {
3637
public:
3738
ConnectorSinkExecutor(std::string executor_name) : _executor_name(std::move(executor_name)) {}
@@ -92,13 +93,13 @@ class ChunkSpillTask final : public Runnable {
9293

9394
class MergeBlockTask : public Runnable {
9495
public:
95-
MergeBlockTask(SpillPartitionChunkWriter* writer, MemTracker* mem_tracker, std::function<void(const Status&)> cb)
96-
: _writer(writer), _mem_tracker(mem_tracker), _cb(std::move(cb)) {}
96+
MergeBlockTask(std::function<Status()> merge_func, MemTracker* mem_tracker, std::function<void(const Status&)> cb)
97+
: _merge_func(std::move(merge_func)), _mem_tracker(mem_tracker), _cb(std::move(cb)) {}
9798

9899
void run() override;
99100

100101
private:
101-
SpillPartitionChunkWriter* _writer;
102+
std::function<Status()> _merge_func;
102103
MemTracker* _mem_tracker;
103104
std::function<void(const Status&)> _cb;
104105
};

be/src/connector/hive_utils.cpp renamed to be/src/connector/common/hive_partition_utils.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "connector/hive_utils.h"
15+
#include "connector/common/hive_partition_utils.h"
1616

1717
#include <cmath>
1818
#include <sstream>
@@ -26,7 +26,7 @@
2626

2727
namespace starrocks::connector {
2828

29-
StatusOr<std::string> HiveUtils::make_partition_name(
29+
StatusOr<std::string> HivePartitionUtils::make_partition_name(
3030
const std::vector<std::string>& column_names,
3131
const std::vector<std::unique_ptr<ColumnEvaluator>>& column_evaluators, Chunk* chunk,
3232
bool support_null_partition) {
@@ -45,7 +45,7 @@ StatusOr<std::string> HiveUtils::make_partition_name(
4545
}
4646

4747
template <typename T>
48-
StatusOr<std::string> HiveUtils::format_decimal_value(T value, int scale) {
48+
StatusOr<std::string> HivePartitionUtils::format_decimal_value(T value, int scale) {
4949
if (scale < 0) {
5050
return Status::InvalidArgument("scale must be non-negative");
5151
}
@@ -68,12 +68,13 @@ StatusOr<std::string> HiveUtils::format_decimal_value(T value, int scale) {
6868
return res;
6969
}
7070

71-
template StatusOr<std::string> HiveUtils::format_decimal_value<int32_t>(int32_t value, int scale);
72-
template StatusOr<std::string> HiveUtils::format_decimal_value<int64_t>(int64_t value, int scale);
73-
template StatusOr<std::string> HiveUtils::format_decimal_value<int128_t>(int128_t value, int scale);
71+
template StatusOr<std::string> HivePartitionUtils::format_decimal_value<int32_t>(int32_t value, int scale);
72+
template StatusOr<std::string> HivePartitionUtils::format_decimal_value<int64_t>(int64_t value, int scale);
73+
template StatusOr<std::string> HivePartitionUtils::format_decimal_value<int128_t>(int128_t value, int scale);
7474

7575
// TODO(letian-jiang): translate org.apache.hadoop.hive.common.FileUtils#makePartName
76-
StatusOr<std::string> HiveUtils::column_value(const TypeDescriptor& type_desc, const ColumnPtr& column, int i) {
76+
StatusOr<std::string> HivePartitionUtils::column_value(const TypeDescriptor& type_desc, const ColumnPtr& column,
77+
int i) {
7778
DCHECK(i < column->size() && i >= 0);
7879
auto datum = column->get(i);
7980
if (datum.is_null()) {

be/src/connector/hive_utils.h renamed to be/src/connector/common/hive_partition_utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
namespace starrocks::connector {
2626

27-
class HiveUtils {
27+
class HivePartitionUtils {
2828
public:
2929
static StatusOr<std::string> make_partition_name(
3030
const std::vector<std::string>& column_names,
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright 2021-present StarRocks, Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "connector/common/partition_chunk_writer.h"
16+
17+
#include <algorithm>
18+
19+
#include "base/failpoint/fail_point.h"
20+
#include "column/chunk.h"
21+
#include "formats/file_writer.h"
22+
#include "formats/io/async_flush_stream_poller.h"
23+
24+
namespace starrocks::connector {
25+
26+
DEFINE_FAIL_POINT(parquet_chunk_writer_init_failed);
27+
28+
PartitionChunkWriter::PartitionChunkWriter(std::string partition, std::vector<int8_t> partition_field_null_list,
29+
const std::shared_ptr<PartitionChunkWriterContext>& ctx)
30+
: _partition(std::move(partition)),
31+
_partition_field_null_list(std::move(partition_field_null_list)),
32+
_file_writer_factory(ctx->file_writer_factory),
33+
_location_provider(ctx->location_provider),
34+
_max_file_size(ctx->max_file_size),
35+
_is_default_partition(ctx->is_default_partition) {
36+
_commit_extra_data.resize(_partition_field_null_list.size(), '0');
37+
std::transform(_partition_field_null_list.begin(), _partition_field_null_list.end(), _commit_extra_data.begin(),
38+
[](int8_t b) { return b + '0'; });
39+
}
40+
41+
Status PartitionChunkWriter::create_file_writer_if_needed() {
42+
if (!_file_writer) {
43+
std::string path = _is_default_partition ? _location_provider->get() : _location_provider->get(_partition);
44+
ASSIGN_OR_RETURN(auto new_writer_and_stream, _file_writer_factory->create(path));
45+
_file_writer = std::move(new_writer_and_stream.writer);
46+
_out_stream = std::move(new_writer_and_stream.stream);
47+
RETURN_IF_ERROR(_file_writer->init());
48+
49+
FAIL_POINT_TRIGGER_EXECUTE(parquet_chunk_writer_init_failed,
50+
{ return Status::InternalError("Create file writer failed due to fail point"); });
51+
_io_poller->enqueue(_out_stream);
52+
}
53+
return Status::OK();
54+
}
55+
56+
Status PartitionChunkWriter::commit_file() {
57+
if (!_file_writer) {
58+
return Status::OK();
59+
}
60+
SCOPED_TIMER(_sink_profile ? _sink_profile->commit_file_timer : nullptr);
61+
auto file_result = _file_writer->close();
62+
const auto io_status = file_result.io_status;
63+
const auto file_size = file_result.file_statistics.file_size;
64+
CommitResult result{.file_result = std::move(file_result)};
65+
result.set_partition_null_fingerprint(_commit_extra_data);
66+
_commit_callback(result);
67+
_file_writer = nullptr;
68+
VLOG(3) << "commit to remote file, filename: " << _out_stream->filename() << ", size: " << file_size;
69+
_out_stream = nullptr;
70+
return io_status;
71+
}
72+
73+
Status BufferPartitionChunkWriter::init() {
74+
RETURN_IF_ERROR(create_file_writer_if_needed());
75+
return Status::OK();
76+
}
77+
78+
Status BufferPartitionChunkWriter::write(const ChunkPtr& chunk) {
79+
if (_file_writer && _file_writer->get_written_bytes() >= _max_file_size) {
80+
RETURN_IF_ERROR(commit_file());
81+
}
82+
RETURN_IF_ERROR(create_file_writer_if_needed());
83+
SCOPED_TIMER(_sink_profile ? _sink_profile->write_file_timer : nullptr);
84+
return _file_writer->write(chunk.get());
85+
}
86+
87+
Status BufferPartitionChunkWriter::flush() {
88+
return commit_file();
89+
}
90+
91+
Status BufferPartitionChunkWriter::wait_flush() {
92+
return Status::OK();
93+
}
94+
95+
Status BufferPartitionChunkWriter::finish() {
96+
return commit_file();
97+
}
98+
99+
} // namespace starrocks::connector

be/src/connector/partition_chunk_writer.h renamed to be/src/connector/common/partition_chunk_writer.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,19 @@
1414

1515
#pragma once
1616

17+
#include <atomic>
18+
#include <cstdint>
19+
#include <list>
20+
#include <memory>
21+
#include <string>
22+
#include <unordered_map>
1723
#include <utility>
24+
#include <vector>
1825

1926
#include "base/uid_util.h"
2027
#include "column/sorting/sorting.h"
2128
#include "column/vectorized_fwd.h"
2229
#include "common/thread/threadpool.h"
23-
#include "compute_env/load_spill/load_chunk_spiller.h"
2430
#include "connector/common/connector_sink_commit.h"
2531
#include "connector/common/connector_sink_profile.h"
2632
#include "connector/common/utils.h"
@@ -30,11 +36,10 @@
3036

3137
namespace starrocks {
3238

39+
class RuntimeState;
3340
class TupleDescriptor;
34-
35-
namespace pipeline {
36-
class FragmentContext;
37-
} // namespace pipeline
41+
class LoadChunkSpiller;
42+
class LoadSpillBlockManager;
3843

3944
} // namespace starrocks
4045

@@ -62,7 +67,7 @@ struct BufferPartitionChunkWriterContext : PartitionChunkWriterContext {};
6267

6368
struct SpillPartitionChunkWriterContext : PartitionChunkWriterContext {
6469
std::shared_ptr<FileSystem> fs;
65-
pipeline::FragmentContext* fragment_context = nullptr;
70+
RuntimeState* state = nullptr;
6671
ConnectorSinkSpillExecutor* spill_executor = nullptr;
6772
TupleDescriptor* tuple_desc = nullptr;
6873
std::shared_ptr<std::vector<std::unique_ptr<ColumnEvaluator>>> column_evaluators;
@@ -215,7 +220,7 @@ class SpillPartitionChunkWriter final : public PartitionChunkWriter {
215220

216221
private:
217222
std::shared_ptr<FileSystem> _fs = nullptr;
218-
pipeline::FragmentContext* _fragment_context = nullptr;
223+
RuntimeState* _state = nullptr;
219224
TupleDescriptor* _tuple_desc = nullptr;
220225
std::shared_ptr<std::vector<std::unique_ptr<ColumnEvaluator>>> _column_evaluators;
221226
std::shared_ptr<SortOrdering> _sort_ordering;

be/src/connector/partition_chunk_writer_memory_manager.cpp renamed to be/src/connector/common/partition_chunk_writer_memory_manager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "connector/partition_chunk_writer_memory_manager.h"
15+
#include "connector/common/partition_chunk_writer_memory_manager.h"
1616

1717
#include "common/logging.h"
1818

0 commit comments

Comments
 (0)