Coverage Report

Created: 2025-06-18 18:37

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