Coverage Report

Created: 2026-08-06 08:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/sink/writer/vwal_writer.cpp
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
#include "exec/sink/writer/vwal_writer.h"
19
20
#include <gen_cpp/FrontendService.h>
21
#include <gen_cpp/data.pb.h>
22
23
#include <sstream>
24
25
#include "io/fs/encrypted_fs_factory.h"
26
#include "io/fs/local_file_system.h"
27
#include "util/debug_points.h"
28
29
namespace doris {
30
31
VWalWriter::VWalWriter(int64_t db_id, int64_t tb_id, int64_t wal_id,
32
                       const std::string& import_label, WalManager* wal_manager,
33
                       std::vector<TSlotDescriptor>& slot_desc, int be_exe_version)
34
128
        : _db_id(db_id),
35
128
          _tb_id(tb_id),
36
128
          _wal_id(wal_id),
37
128
          _label(import_label),
38
128
          _wal_manager(wal_manager),
39
128
          _slot_descs(slot_desc),
40
128
          _be_exe_version(be_exe_version) {}
41
42
128
VWalWriter::~VWalWriter() {}
43
44
128
Status VWalWriter::init() {
45
128
    io::FileSystemSPtr wal_fs = io::global_local_filesystem();
46
128
#ifndef BE_TEST
47
128
    if (config::group_commit_wait_replay_wal_finish) {
48
0
        std::shared_ptr<std::mutex> lock = std::make_shared<std::mutex>();
49
0
        std::shared_ptr<std::condition_variable> cv = std::make_shared<std::condition_variable>();
50
0
        auto add_st = _wal_manager->add_wal_cv_map(_wal_id, lock, cv);
51
0
        if (!add_st.ok()) {
52
0
            LOG(WARNING) << "fail to add wal_id " << _wal_id << " to wal_cv_map";
53
0
        }
54
0
    }
55
128
    RETURN_IF_ERROR(determine_wal_fs(_db_id, _tb_id, wal_fs));
56
128
#endif
57
128
    RETURN_IF_ERROR(_create_wal_writer(_wal_id, wal_fs, _wal_writer));
58
128
    _wal_manager->add_wal_queue(_tb_id, _wal_id);
59
128
    std::stringstream ss;
60
440
    for (auto slot_desc : _slot_descs) {
61
440
        if (slot_desc.col_unique_id < 0) {
62
0
            continue;
63
0
        }
64
440
        ss << std::to_string(slot_desc.col_unique_id) << ",";
65
440
    }
66
128
    std::string col_ids = ss.str().substr(0, ss.str().size() - 1);
67
128
    RETURN_IF_ERROR(_wal_writer->append_header(col_ids));
68
128
    return Status::OK();
69
128
}
70
71
988
Status VWalWriter::write_wal(Block* block) {
72
988
    DBUG_EXECUTE_IF("VWalWriter.write_wal.fail",
73
988
                    { return Status::InternalError("Failed to write wal!"); });
74
988
    PBlock pblock;
75
988
    size_t uncompressed_bytes = 0, compressed_bytes = 0;
76
988
    int64_t compressed_time = 0;
77
988
    RETURN_IF_ERROR(block->serialize(_be_exe_version, &pblock, &uncompressed_bytes,
78
988
                                     &compressed_bytes, &compressed_time,
79
988
                                     segment_v2::CompressionTypePB::NO_COMPRESSION));
80
988
    RETURN_IF_ERROR(_wal_writer->append_blocks(std::vector<PBlock*> {&pblock}));
81
988
    return Status::OK();
82
988
}
83
84
128
Status VWalWriter::close() {
85
128
    if (config::group_commit_wait_replay_wal_finish) {
86
0
        std::string wal_path;
87
0
        RETURN_IF_ERROR(_wal_manager->get_wal_path(_wal_id, wal_path));
88
0
        LOG(INFO) << "close file " << wal_path;
89
0
        RETURN_IF_ERROR(_wal_manager->add_recover_wal(_db_id, _tb_id, _wal_id, wal_path));
90
0
        RETURN_IF_ERROR(_wal_manager->wait_replay_wal_finish(_wal_id));
91
0
    }
92
128
    if (_wal_writer != nullptr) {
93
128
        RETURN_IF_ERROR(_wal_writer->finalize());
94
128
    }
95
128
    return Status::OK();
96
128
}
97
98
Status VWalWriter::_create_wal_writer(int64_t wal_id, const io::FileSystemSPtr& fs,
99
128
                                      std::shared_ptr<WalWriter>& wal_writer) {
100
128
    std::string wal_path;
101
128
    RETURN_IF_ERROR(_wal_manager->get_wal_path(wal_id, wal_path));
102
128
    wal_writer = std::make_shared<WalWriter>(wal_path);
103
128
    RETURN_IF_ERROR(wal_writer->init(fs));
104
128
    return Status::OK();
105
128
}
106
} // namespace doris