Coverage Report

Created: 2026-10-08 12:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/load/channel/load_channel.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <atomic>
21
#include <cstdint>
22
#include <memory>
23
#include <mutex>
24
#include <ostream>
25
#include <string>
26
#include <unordered_map>
27
#include <unordered_set>
28
#include <utility>
29
30
#include "common/status.h"
31
#include "runtime/runtime_profile.h"
32
#include "runtime/workload_group/workload_group_fwd.h"
33
#include "runtime/workload_management/resource_context.h"
34
#include "util/uid_util.h"
35
36
namespace doris {
37
38
class EosCompletion;
39
class PTabletWriterOpenRequest;
40
class PTabletWriterAddBlockRequest;
41
class PTabletWriterAddBlockResult;
42
class OpenPartitionRequest;
43
class BaseTabletsChannel;
44
45
// A LoadChannel manages tablets channels for all indexes
46
// corresponding to a certain load job
47
class LoadChannel {
48
public:
49
    LoadChannel(const UniqueId& load_id, int64_t timeout_s, bool is_high_priority,
50
                std::string sender_ip, int64_t backend_id, bool enable_profile, int64_t wg_id);
51
    ~LoadChannel();
52
53
    // open a new load channel if not exist
54
    Status open(const PTabletWriterOpenRequest& request);
55
56
    // This batch must belong to one index in one transaction. For hang_wait EOS,
57
    // return the sender-arrival barrier in eos_completion. Final flush/commit
58
    // stays synchronous on the last sender. The caller must register its RPC
59
    // completion only after all synchronous request/response access has ended.
60
    Status add_batch(const PTabletWriterAddBlockRequest& request,
61
                     PTabletWriterAddBlockResult* response,
62
                     std::shared_ptr<EosCompletion>* eos_completion);
63
64
    // return true if this load channel has been opened and all tablets channels are closed then.
65
    bool is_finished();
66
67
    Status cancel();
68
69
4
    time_t last_updated_time() const { return _last_updated_time.load(); }
70
71
6
    const UniqueId& load_id() const { return _load_id; }
72
73
4
    int64_t timeout() const { return _timeout_s; }
74
75
2
    bool is_high_priority() const { return _is_high_priority; }
76
77
2
    bool is_cancelled() const { return _cancelled.load(); }
78
79
0
    WorkloadGroupPtr workload_group() const { return _resource_ctx->workload_group(); }
80
81
0
    RuntimeProfile::Counter* get_mgr_add_batch_timer() { return _mgr_add_batch_timer; }
82
0
    RuntimeProfile::Counter* get_handle_mem_limit_timer() { return _handle_mem_limit_timer; }
83
84
protected:
85
    Status _get_tablets_channel(std::shared_ptr<BaseTabletsChannel>& channel, bool& is_finished,
86
                                int64_t index_id);
87
88
    Status _handle_eos(BaseTabletsChannel* channel, const PTabletWriterAddBlockRequest& request,
89
                       PTabletWriterAddBlockResult* response);
90
91
    void _init_profile();
92
    // thread safety
93
    void _report_profile(PTabletWriterAddBlockResult* response);
94
95
private:
96
    UniqueId _load_id;
97
    int64_t _txn_id = 0;
98
99
    std::mutex _profile_serialize_lock;
100
    std::unique_ptr<RuntimeProfile> _profile;
101
    RuntimeProfile* _self_profile = nullptr;
102
    RuntimeProfile::Counter* _add_batch_number_counter = nullptr;
103
    RuntimeProfile::Counter* _add_batch_timer = nullptr;
104
    RuntimeProfile::Counter* _add_batch_times = nullptr;
105
    RuntimeProfile::Counter* _mgr_add_batch_timer = nullptr;
106
    RuntimeProfile::Counter* _handle_mem_limit_timer = nullptr;
107
    RuntimeProfile::Counter* _handle_eos_timer = nullptr;
108
109
    // lock protect the tablets channel map
110
    std::mutex _lock;
111
    // index id -> tablets channel
112
    std::unordered_map<int64_t, std::shared_ptr<BaseTabletsChannel>> _tablets_channels;
113
    // index id -> (received rows, filtered rows)
114
    std::unordered_map<int64_t, std::pair<size_t, size_t>> _tablets_channels_rows;
115
    std::mutex _tablets_channels_lock;
116
    // This is to save finished channels id, to handle the retry request.
117
    std::unordered_set<int64_t> _finished_channel_ids;
118
    // set to true if at least one tablets channel has been opened
119
    bool _opened = false;
120
    std::atomic<bool> _cancelled {false};
121
122
    std::shared_ptr<ResourceContext> _resource_ctx;
123
124
    std::atomic<time_t> _last_updated_time;
125
126
    // the timeout of this load job.
127
    // Timed out channels will be periodically deleted by LoadChannelMgr.
128
    int64_t _timeout_s;
129
130
    // true if this is a high priority load task
131
    bool _is_high_priority = false;
132
133
    // the ip where tablet sink locate
134
    std::string _sender_ip;
135
136
    int64_t _backend_id;
137
138
    bool _enable_profile;
139
};
140
141
2
inline std::ostream& operator<<(std::ostream& os, LoadChannel& load_channel) {
142
2
    os << "LoadChannel(id=" << load_channel.load_id()
143
2
       << ", last_update_time=" << static_cast<uint64_t>(load_channel.last_updated_time())
144
2
       << ", is high priority: " << load_channel.is_high_priority() << ")";
145
2
    return os;
146
2
}
147
148
} // namespace doris