Coverage Report

Created: 2026-03-12 14:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/load/memtable/memtable_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 <cstdint>
26
#include <memory>
27
#include <mutex>
28
#include <random>
29
#include <vector>
30
31
#include "common/status.h"
32
#include "core/custom_allocator.h"
33
#include "load/delta_writer/delta_writer_context.h"
34
#include "load/memtable/memtable.h"
35
#include "storage/partial_update_info.h"
36
#include "storage/tablet/tablet.h"
37
#include "storage/tablet/tablet_schema.h"
38
39
namespace doris {
40
41
class FlushToken;
42
class MemTable;
43
class StorageEngine;
44
class TupleDescriptor;
45
class SlotDescriptor;
46
class OlapTableSchemaParam;
47
class RowsetWriter;
48
struct FlushStatistic;
49
class WorkloadGroup;
50
51
class Block;
52
53
// Writer for a particular (load, index, tablet).
54
// This class is NOT thread-safe, external synchronization is required.
55
class MemTableWriter {
56
public:
57
    MemTableWriter(const WriteRequest& req);
58
59
    ~MemTableWriter();
60
61
    Status init(std::shared_ptr<RowsetWriter> rowset_writer, TabletSchemaSPtr tablet_schema,
62
                std::shared_ptr<PartialUpdateInfo> partial_update_info,
63
                std::shared_ptr<WorkloadGroup> wg_sptr, bool unique_key_mow = false);
64
65
    Status write(const Block* block, const DorisVector<uint32_t>& row_idxs);
66
67
    // flush the last memtable to flush queue, must call it before close_wait()
68
    Status close();
69
    // wait for all memtables to be flushed, update profiles if provided.
70
    // mem_consumption() should be 0 after this function returns.
71
5.10k
    Status close_wait(RuntimeProfile* profile = nullptr) {
72
5.10k
        RETURN_IF_ERROR(_do_close_wait());
73
5.10k
        if (profile != nullptr) {
74
5.10k
            _update_profile(profile);
75
5.10k
        }
76
5.10k
        return Status::OK();
77
5.10k
    }
78
79
    // abandon current memtable and wait for all pending-flushing memtables to be destructed.
80
    // mem_consumption() should be 0 after this function returns.
81
    Status cancel();
82
    Status cancel_with_status(const Status& st);
83
84
    int64_t mem_consumption(MemType mem);
85
    int64_t active_memtable_mem_consumption();
86
87
    // Submit current memtable to flush queue, and return without waiting.
88
    // This is currently for reducing mem consumption of this memtable writer.
89
    Status flush_async();
90
91
    // Wait all memtable in flush queue to be flushed
92
    Status wait_flush();
93
94
0
    int64_t tablet_id() const { return _req.tablet_id; }
95
96
2.96k
    int64_t total_received_rows() const { return _total_received_rows; }
97
98
    const FlushStatistic& get_flush_token_stats();
99
100
    uint64_t flush_running_count() const;
101
102
0
    uint64_t workload_group_id() const {
103
0
        auto wg = _resource_ctx->workload_group();
104
0
        if (wg != nullptr) {
105
0
            return wg->id();
106
0
        }
107
0
        return 0;
108
0
    }
109
110
private:
111
    Status _flush_memtable();
112
    // push a full memtable to flush executor
113
    Status _flush_memtable_async();
114
115
    void _reset_mem_table();
116
117
    Status _do_close_wait();
118
    void _update_profile(RuntimeProfile* profile);
119
120
    std::atomic<bool> _is_init = false;
121
    bool _is_cancelled = false;
122
    bool _is_closed = false;
123
    Status _cancel_status;
124
    WriteRequest _req;
125
    std::shared_ptr<RowsetWriter> _rowset_writer;
126
    std::shared_ptr<MemTable> _mem_table;
127
    TabletSchemaSPtr _tablet_schema;
128
    bool _unique_key_mow = false;
129
130
    // This variable is accessed from writer thread and token flush thread
131
    // use a shared ptr to avoid use after free problem.
132
    std::shared_ptr<FlushToken> _flush_token;
133
    // Save the not active memtable that is in flush queue or under flushing.
134
    std::vector<std::weak_ptr<MemTable>> _freezed_mem_tables;
135
    // The lock to protect _memtable and _freezed_mem_tables structure to avoid concurrency modification or read
136
    std::mutex _mem_table_ptr_lock;
137
    std::shared_ptr<ResourceContext> _resource_ctx;
138
139
    std::mutex _lock;
140
141
    // total rows num written by MemTableWriter
142
    std::atomic<int64_t> _total_received_rows = 0;
143
    int64_t _wait_flush_time_ns = 0;
144
    int64_t _close_wait_time_ns = 0;
145
    int64_t _segment_num = 0;
146
147
    MonotonicStopWatch _lock_watch;
148
149
    std::shared_ptr<PartialUpdateInfo> _partial_update_info;
150
};
151
152
} // namespace doris