Coverage Report

Created: 2025-11-20 23:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/olap/delta_writer.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 <gen_cpp/Types_types.h>
21
#include <gen_cpp/internal_service.pb.h>
22
#include <gen_cpp/types.pb.h>
23
24
#include <atomic>
25
#include <memory>
26
#include <mutex>
27
#include <shared_mutex>
28
#include <unordered_set>
29
#include <vector>
30
31
#include "common/status.h"
32
#include "olap/delta_writer_context.h"
33
#include "olap/memtable_writer.h"
34
#include "olap/olap_common.h"
35
#include "olap/rowset/rowset.h"
36
#include "olap/tablet.h"
37
#include "olap/tablet_meta.h"
38
#include "olap/tablet_schema.h"
39
#include "util/uid_util.h"
40
41
namespace doris {
42
43
class FlushToken;
44
class MemTable;
45
class StorageEngine;
46
class TupleDescriptor;
47
class SlotDescriptor;
48
class OlapTableSchemaParam;
49
class RowsetWriter;
50
51
namespace vectorized {
52
class Block;
53
} // namespace vectorized
54
55
class BaseRowsetBuilder;
56
class RowsetBuilder;
57
58
// Writer for a particular (load, index, tablet).
59
// This class is NOT thread-safe, external synchronization is required.
60
class BaseDeltaWriter {
61
public:
62
    BaseDeltaWriter(const WriteRequest& req, RuntimeProfile* profile, const UniqueId& load_id);
63
64
    virtual ~BaseDeltaWriter();
65
66
    virtual Status write(const vectorized::Block* block, const DorisVector<uint32_t>& row_idxs) = 0;
67
68
    // flush the last memtable to flush queue, must call it before build_rowset()
69
    virtual Status close() = 0;
70
    // wait for all memtables to be flushed.
71
    // mem_consumption() should be 0 after this function returns.
72
    virtual Status build_rowset();
73
    Status submit_calc_delete_bitmap_task();
74
    Status wait_calc_delete_bitmap();
75
76
    // abandon current memtable and wait for all pending-flushing memtables to be destructed.
77
    // mem_consumption() should be 0 after this function returns.
78
    Status cancel();
79
    virtual Status cancel_with_status(const Status& st);
80
81
    int64_t mem_consumption(MemType mem);
82
83
    // Wait all memtable in flush queue to be flushed
84
    Status wait_flush();
85
86
0
    int64_t partition_id() const { return _req.partition_id; }
87
88
0
    int64_t tablet_id() const { return _req.tablet_id; }
89
90
0
    int64_t txn_id() const { return _req.txn_id; }
91
92
0
    int64_t total_received_rows() const { return _memtable_writer->total_received_rows(); }
93
94
    int64_t num_rows_filtered() const;
95
96
    static void collect_tablet_load_rowset_num_info(
97
            BaseTablet* tablet,
98
            google::protobuf::RepeatedPtrField<PTabletLoadRowsetInfo>* tablet_infos);
99
100
    void set_tablet_load_rowset_num_info(
101
            google::protobuf::RepeatedPtrField<PTabletLoadRowsetInfo>* tablet_info);
102
103
protected:
104
    virtual void _init_profile(RuntimeProfile* profile);
105
106
    Status init();
107
108
    bool _is_init = false;
109
    bool _is_cancelled = false;
110
    WriteRequest _req;
111
    std::unique_ptr<BaseRowsetBuilder> _rowset_builder;
112
    std::shared_ptr<MemTableWriter> _memtable_writer;
113
114
    // total rows num written by DeltaWriter
115
    std::atomic<int64_t> _total_received_rows = 0;
116
117
    RuntimeProfile* _profile = nullptr;
118
    RuntimeProfile::Counter* _close_wait_timer = nullptr;
119
    RuntimeProfile::Counter* _wait_flush_limit_timer = nullptr;
120
121
    MonotonicStopWatch _lock_watch;
122
};
123
124
// `StorageEngine` mixin for `BaseDeltaWriter`
125
class DeltaWriter final : public BaseDeltaWriter {
126
public:
127
    DeltaWriter(StorageEngine& engine, const WriteRequest& req, RuntimeProfile* profile,
128
                const UniqueId& load_id);
129
130
    ~DeltaWriter() override;
131
132
    Status write(const vectorized::Block* block, const DorisVector<uint32_t>& row_idxs) override;
133
134
    Status close() override;
135
136
    Status cancel_with_status(const Status& st) override;
137
138
    Status build_rowset() override;
139
140
    Status commit_txn(const PSlaveTabletNodes& slave_tablet_nodes);
141
142
    bool check_slave_replicas_done(google::protobuf::Map<int64_t, PSuccessSlaveTabletNodeIds>*
143
                                           success_slave_tablet_node_ids);
144
145
    void add_finished_slave_replicas(google::protobuf::Map<int64_t, PSuccessSlaveTabletNodeIds>*
146
                                             success_slave_tablet_node_ids);
147
148
    void finish_slave_tablet_pull_rowset(int64_t node_id, bool is_succeed);
149
150
private:
151
    void _init_profile(RuntimeProfile* profile) override;
152
153
    void _request_slave_tablet_pull_rowset(const PNodeInfo& node_info);
154
155
    // Convert `_rowset_builder` from `BaseRowsetBuilder` to `RowsetBuilder`
156
    RowsetBuilder* rowset_builder();
157
158
    std::mutex _lock;
159
160
    StorageEngine& _engine;
161
    std::unordered_set<int64_t> _unfinished_slave_node;
162
    PSuccessSlaveTabletNodeIds _success_slave_node_ids;
163
    std::shared_mutex _slave_node_lock;
164
165
    RuntimeProfile::Counter* _commit_txn_timer = nullptr;
166
};
167
168
} // namespace doris