Coverage Report

Created: 2025-04-24 14:47

/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/partial_update_info.h"
34
#include "olap/tablet.h"
35
#include "olap/tablet_schema.h"
36
#include "vec/common/custom_allocator.h"
37
38
namespace doris {
39
40
class FlushToken;
41
class MemTable;
42
class StorageEngine;
43
class TupleDescriptor;
44
class SlotDescriptor;
45
class OlapTableSchemaParam;
46
class RowsetWriter;
47
struct FlushStatistic;
48
class WorkloadGroup;
49
50
namespace vectorized {
51
class Block;
52
} // namespace vectorized
53
54
// Writer for a particular (load, index, tablet).
55
// This class is NOT thread-safe, external synchronization is required.
56
class MemTableWriter {
57
public:
58
    MemTableWriter(const WriteRequest& req);
59
60
    ~MemTableWriter();
61
62
    Status init(std::shared_ptr<RowsetWriter> rowset_writer, TabletSchemaSPtr tablet_schema,
63
                std::shared_ptr<PartialUpdateInfo> partial_update_info,
64
                std::shared_ptr<WorkloadGroup> wg_sptr, bool unique_key_mow = false);
65
66
    Status write(const vectorized::Block* block, const DorisVector<uint32_t>& row_idxs);
67
68
    // flush the last memtable to flush queue, must call it before close_wait()
69
    Status close();
70
    // wait for all memtables to be flushed, update profiles if provided.
71
    // mem_consumption() should be 0 after this function returns.
72
15
    Status close_wait(RuntimeProfile* profile = nullptr) {
73
15
        RETURN_IF_ERROR(_do_close_wait());
74
15
        if (profile != nullptr) {
75
15
            _update_profile(profile);
76
15
        }
77
15
        return Status::OK();
78
15
    }
79
80
    // abandon current memtable and wait for all pending-flushing memtables to be destructed.
81
    // mem_consumption() should be 0 after this function returns.
82
    Status cancel();
83
    Status cancel_with_status(const Status& st);
84
85
    int64_t mem_consumption(MemType mem);
86
    int64_t active_memtable_mem_consumption();
87
88
    // Submit current memtable to flush queue, and return without waiting.
89
    // This is currently for reducing mem consumption of this memtable writer.
90
    Status flush_async();
91
92
    // Wait all memtable in flush queue to be flushed
93
    Status wait_flush();
94
95
0
    int64_t tablet_id() const { return _req.tablet_id; }
96
97
0
    int64_t total_received_rows() const { return _total_received_rows; }
98
99
    const FlushStatistic& get_flush_token_stats();
100
101
    uint64_t flush_running_count() const;
102
103
0
    uint64_t workload_group_id() const {
104
0
        auto wg = _resource_ctx->workload_group();
105
0
        if (wg != nullptr) {
106
0
            return wg->id();
107
0
        }
108
0
        return 0;
109
0
    }
110
111
private:
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