Coverage Report

Created: 2025-10-17 00:26

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