Coverage Report

Created: 2026-04-14 13:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/cloud/delete_bitmap_file_reader.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 "cloud/delete_bitmap_file_reader.h"
19
20
#include "cloud/delete_bitmap_file_writer.h"
21
#include "common/status.h"
22
#include "io/fs/file_reader.h"
23
#include "io/fs/packed_file_reader.h"
24
#include "util/coding.h"
25
26
namespace doris {
27
28
DeleteBitmapFileReader::DeleteBitmapFileReader(int64_t tablet_id, const std::string& rowset_id,
29
                                               std::optional<StorageResource>& storage_resource)
30
1
        : _tablet_id(tablet_id), _rowset_id(rowset_id), _storage_resource(storage_resource) {}
31
32
DeleteBitmapFileReader::DeleteBitmapFileReader(int64_t tablet_id, const std::string& rowset_id,
33
                                               std::optional<StorageResource>& storage_resource,
34
                                               const PackedSliceLocationPB& packed_location)
35
0
        : _tablet_id(tablet_id),
36
0
          _rowset_id(rowset_id),
37
0
          _storage_resource(storage_resource),
38
0
          _is_packed(true),
39
0
          _packed_offset(packed_location.offset()),
40
0
          _packed_size(packed_location.size()),
41
0
          _packed_file_path(packed_location.packed_file_path()),
42
0
          _packed_file_size(packed_location.has_packed_file_size()
43
0
                                    ? packed_location.packed_file_size()
44
0
                                    : -1) {}
45
46
1
DeleteBitmapFileReader::~DeleteBitmapFileReader() = default;
47
48
1
Status DeleteBitmapFileReader::init() {
49
1
#ifdef BE_TEST
50
1
    _path = "./log/" + _rowset_id + "_delete_bitmap.db";
51
1
    bool exists = false;
52
1
    RETURN_IF_ERROR(io::global_local_filesystem()->exists(_path, &exists));
53
1
    if (!exists) {
54
0
        return Status::NotFound("{} doesn't exist", _path);
55
0
    }
56
1
    RETURN_IF_ERROR(io::global_local_filesystem()->open_file(_path, &_file_reader));
57
1
    return Status::OK();
58
0
#endif
59
0
    if (!_storage_resource) {
60
0
        return Status::InternalError("invalid storage resource for tablet_id={}", _tablet_id);
61
0
    }
62
63
0
    if (_is_packed) {
64
        // Read from packed file
65
0
        io::FileReaderSPtr inner_reader;
66
0
        io::FileReaderOptions opts;
67
0
        if (_packed_file_size > 0) {
68
0
            opts.file_size = _packed_file_size;
69
0
        }
70
0
        opts.cache_type = io::FileCachePolicy::NO_CACHE;
71
0
        RETURN_IF_ERROR(_storage_resource->fs->open_file(io::Path(_packed_file_path), &inner_reader,
72
0
                                                         &opts));
73
74
0
        _path = _storage_resource->remote_delete_bitmap_path(_tablet_id, _rowset_id);
75
0
        _file_reader = std::make_shared<io::PackedFileReader>(
76
0
                std::move(inner_reader), io::Path(_path), _packed_offset, _packed_size);
77
0
    } else {
78
        // Read from standalone file
79
0
        _path = _storage_resource->remote_delete_bitmap_path(_tablet_id, _rowset_id);
80
0
        io::FileReaderOptions opts;
81
0
        RETURN_IF_ERROR(_storage_resource->fs->open_file(_path, &_file_reader, &opts));
82
0
    }
83
0
    return Status::OK();
84
0
}
85
86
1
Status DeleteBitmapFileReader::close() {
87
1
    if (_file_reader) {
88
1
        return _file_reader->close();
89
1
    }
90
0
    return Status::OK();
91
1
}
92
93
1
Status DeleteBitmapFileReader::read(DeleteBitmapPB& delete_bitmap) {
94
1
    size_t bytes_read = 0;
95
1
    size_t offset = 0;
96
    // 0. read magic number
97
1
    {
98
1
        uint8_t magic_buf[DeleteBitmapFileWriter::MAGIC_SIZE];
99
1
        RETURN_IF_ERROR(_file_reader->read_at(
100
1
                offset, {magic_buf, DeleteBitmapFileWriter::MAGIC_SIZE}, &bytes_read));
101
1
        offset += DeleteBitmapFileWriter::MAGIC_SIZE;
102
1
        if (bytes_read != DeleteBitmapFileWriter::MAGIC_SIZE ||
103
1
            memcmp(magic_buf, DeleteBitmapFileWriter::DELETE_BITMAP_MAGIC,
104
1
                   DeleteBitmapFileWriter::MAGIC_SIZE) != 0) {
105
0
            return Status::InternalError(
106
0
                    "read delete bitmap failed from {} because magic is not "
107
0
                    "matched",
108
0
                    _path);
109
0
        }
110
1
    }
111
    // 1. read delete bitmap proto length
112
1
    size_t delete_bitmap_len = 0;
113
1
    {
114
1
        uint8_t len_buf[DeleteBitmapFileWriter::LENGTH_SIZE];
115
1
        RETURN_IF_ERROR(_file_reader->read_at(
116
1
                offset, {len_buf, DeleteBitmapFileWriter::LENGTH_SIZE}, &bytes_read));
117
1
        offset += DeleteBitmapFileWriter::LENGTH_SIZE;
118
1
        delete_bitmap_len = decode_fixed64_le(len_buf);
119
1
        if (delete_bitmap_len == 0) {
120
0
            return Status::InternalError("read delete bitmap failed from {} because length is 0",
121
0
                                         _path);
122
0
        }
123
1
        if (offset == _file_reader->size()) {
124
0
            LOG(WARNING) << "read delete bitmap failed because reach end of file=" << _path
125
0
                         << ", file size=" << _file_reader->size() << ", offset=" << offset
126
0
                         << ", delete_bitmap_len=" << delete_bitmap_len;
127
0
            return Status::InternalError(
128
0
                    "read delete bitmap failed from {} because reach end of file", _path);
129
0
        }
130
1
    }
131
    // 2. read delete bitmap
132
1
    std::string delete_bitmap_buf;
133
1
    {
134
1
        delete_bitmap_buf.resize(delete_bitmap_len);
135
1
        RETURN_IF_ERROR(_file_reader->read_at(
136
1
                offset, {delete_bitmap_buf.c_str(), delete_bitmap_len}, &bytes_read));
137
1
        offset += delete_bitmap_len;
138
1
        if (!delete_bitmap.ParseFromString(delete_bitmap_buf)) {
139
0
            LOG(WARNING) << "deserialize delete bitmap failed from file=" << _path
140
0
                         << ", file size=" << _file_reader->size()
141
0
                         << ", delete_bitmap_len=" << delete_bitmap_len
142
0
                         << ", bytes_read=" << bytes_read;
143
0
            return Status::InternalError("deserialize delete bitmap failed from {}", _path);
144
0
        }
145
1
    }
146
    // 3. checksum
147
1
    uint8_t checksum_len_buf[DeleteBitmapFileWriter::CHECKSUM_SIZE];
148
1
    RETURN_IF_ERROR(_file_reader->read_at(
149
1
            offset, {checksum_len_buf, DeleteBitmapFileWriter::CHECKSUM_SIZE}, &bytes_read));
150
1
    offset += DeleteBitmapFileWriter::CHECKSUM_SIZE;
151
1
    uint32_t checksum = decode_fixed32_le(checksum_len_buf);
152
1
    uint32_t computed_checksum = crc32c::Crc32c(delete_bitmap_buf.data(), delete_bitmap_len);
153
1
    if (computed_checksum != checksum) {
154
0
        return Status::InternalError("delete bitmap checksum failed from file=" + _path +
155
0
                                     ", computed checksum=" + std::to_string(computed_checksum) +
156
0
                                     ", expected=" + std::to_string(checksum));
157
0
    }
158
1
    return Status::OK();
159
1
}
160
161
} // namespace doris