• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

llnl / dftracer-utils / 28693295402

04 Jul 2026 03:17AM UTC coverage: 52.408% (+0.1%) from 52.278%
28693295402

push

github

hariharan-devarajan
feat: silence noisy warnings on aarch64

37318 of 92666 branches covered (40.27%)

Branch coverage included in aggregate %.

33462 of 42389 relevant lines covered (78.94%)

20557.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

62.32
/src/dftracer/utils/core/rocksdb/database.cpp
1
#include <dftracer/utils/core/common/constants.h>
2
#include <dftracer/utils/core/common/error.h>
3
#include <dftracer/utils/core/common/filesystem.h>
4
#include <dftracer/utils/core/env.h>
5
#include <dftracer/utils/core/rocksdb/database.h>
6
#include <dftracer/utils/core/rocksdb/filesystem.h>
7
#include <rocksdb/slice.h>
8
#include <rocksdb/table.h>
9

10
#include <algorithm>
11
#include <atomic>
12
#include <cstdlib>
13
#include <stdexcept>
14
#include <utility>
15

16
namespace dftracer::utils::rocksdb {
17

18
namespace {
19

20
std::atomic<bool>& process_exiting_flag() {
11,226✔
21
    static std::atomic<bool> flag{false};
22
    return flag;
11,226✔
23
}
24

25
const ::rocksdb::ReadOptions& read_options() {
22,151✔
26
    static const ::rocksdb::ReadOptions options;
22,151!
27
    return options;
22,146✔
28
}
29

30
const ::rocksdb::WriteOptions& write_options() {
4,058✔
31
    static const auto options = [] {
2,222!
32
        ::rocksdb::WriteOptions wo;
190✔
33
        wo.disableWAL = true;
190✔
34
        return wo;
190✔
35
    }();
2,026✔
36
    return options;
4,060✔
37
}
38

39
void cleanup_failed_open(::rocksdb::DB*& db,
×
40
                         std::vector<::rocksdb::ColumnFamilyHandle*>& handles) {
41
    if (db != nullptr) {
×
42
        for (auto* handle : handles) {
×
43
            if (handle != nullptr) {
×
44
                db->DestroyColumnFamilyHandle(handle);
×
45
            }
46
        }
47
        static_cast<void>(db->Close());
×
48
        delete db;
×
49
        db = nullptr;
×
50
    }
51
    handles.clear();
×
52
}
×
53

54
}  // namespace
55

56
void mark_process_exiting_for_rocksdb() {
6✔
57
    process_exiting_flag().store(true, std::memory_order_relaxed);
6✔
58
}
6✔
59

60
RocksDatabase::RocksDatabase() = default;
28,396✔
61

62
RocksDatabase::RocksDatabase(const std::string& db_path, OpenMode open_mode) {
15✔
63
    open(db_path, open_mode);
6!
64
}
9✔
65

66
RocksDatabase::~RocksDatabase() { close(); }
16,980!
67

68
RocksDatabase::RocksDatabase(RocksDatabase&& other) noexcept
×
69
    : db_path_(std::move(other.db_path_)),
×
70
      open_mode_(other.open_mode_),
×
71
      file_system_(std::move(other.file_system_)),
×
72
      env_(std::move(other.env_)),
×
73
      db_(std::exchange(other.db_, nullptr)),
×
74
      column_families_(std::move(other.column_families_)) {}
×
75

76
RocksDatabase& RocksDatabase::operator=(RocksDatabase&& other) noexcept {
×
77
    if (this != &other) {
×
78
        close();
×
79
        db_path_ = std::move(other.db_path_);
×
80
        open_mode_ = other.open_mode_;
×
81
        file_system_ = std::move(other.file_system_);
×
82
        env_ = std::move(other.env_);
×
83
        db_ = std::exchange(other.db_, nullptr);
×
84
        column_families_ = std::move(other.column_families_);
×
85
    }
86
    return *this;
×
87
}
88

89
const decltype(cf::ALL)& RocksDatabase::default_column_families() {
2,770✔
90
    return cf::ALL;
2,770✔
91
}
92

93
::rocksdb::Options RocksDatabase::default_options() {
11,736✔
94
    ::rocksdb::Options options;
11,736✔
95
    options.create_if_missing = true;
11,736✔
96
    options.create_missing_column_families = true;
11,736✔
97
    options.allow_concurrent_memtable_write = true;
11,736✔
98
    options.enable_pipelined_write = true;
11,736✔
99
    options.max_open_files = Env::rocksdb_max_open_files();
11,736!
100
    options.max_background_jobs = 8;
11,736✔
101
    options.max_subcompactions = 8;
11,736✔
102
    options.write_buffer_size = 256 * 1024 * 1024;
11,736✔
103
    options.max_write_buffer_number = 4;
11,736✔
104
    return options;
11,736✔
105
}
5,952!
106

107
::rocksdb::ColumnFamilyOptions RocksDatabase::default_column_family_options() {
11,736✔
108
    ::rocksdb::ColumnFamilyOptions options;
11,736!
109

110
    ::rocksdb::BlockBasedTableOptions bbt;
11,736✔
111
    bbt.block_size = 32 * 1024;
11,736✔
112
    bbt.format_version = 5;
11,736✔
113
    bbt.index_block_restart_interval = 16;
11,736✔
114
    options.table_factory.reset(::rocksdb::NewBlockBasedTableFactory(bbt));
11,736!
115

116
#ifdef DFTRACER_UTILS_ENABLE_ZSTD
117
    options.compression = ::rocksdb::kZSTD;
118
    options.compression_opts.level = constants::rocksdb::ZSTD_COMPRESSION_LEVEL;
119
    options.compression_opts.max_dict_bytes =
120
        constants::rocksdb::ZSTD_MAX_DICT_BYTES;
121
    options.compression_opts.zstd_max_train_bytes =
122
        constants::rocksdb::ZSTD_MAX_TRAIN_BYTES;
123
    options.compression_opts.enabled = true;
124
    options.bottommost_compression = ::rocksdb::kZSTD;
125
    options.bottommost_compression_opts.level =
126
        constants::rocksdb::ZSTD_COMPRESSION_LEVEL;
127
    options.bottommost_compression_opts.max_dict_bytes =
128
        constants::rocksdb::ZSTD_MAX_DICT_BYTES;
129
    options.bottommost_compression_opts.zstd_max_train_bytes =
130
        constants::rocksdb::ZSTD_MAX_TRAIN_BYTES;
131
    options.bottommost_compression_opts.enabled = true;
132
#elif defined(DFTRACER_UTILS_ENABLE_LZ4)
133
    options.compression = ::rocksdb::kLZ4Compression;
134
    options.bottommost_compression = ::rocksdb::kZlibCompression;
135
#else
136
    options.compression = ::rocksdb::kZlibCompression;
11,736✔
137
    options.bottommost_compression = ::rocksdb::kZlibCompression;
11,736✔
138
#endif
139
    return options;
17,519✔
140
}
11,736!
141

142
bool RocksDatabase::open(const std::string& db_path, OpenMode open_mode) {
11,262✔
143
    close();
11,262!
144
    db_path_ = db_path;
11,264!
145
    open_mode_ = open_mode;
11,263✔
146

147
    std::error_code ec;
11,263✔
148
    if (open_mode_ == OpenMode::ReadWrite) {
11,264✔
149
        fs::create_directories(fs::path(db_path_), ec);
1,698!
150
    }
838✔
151

152
    auto db_options = default_options();
11,263!
153
    if (open_mode_ == OpenMode::ReadOnly) {
11,264✔
154
        db_options.create_if_missing = false;
9,588✔
155
        db_options.create_missing_column_families = false;
9,588✔
156
    }
4,878✔
157
    file_system_ = make_dftracer_file_system();
11,264!
158
    env_ = make_dftracer_env(file_system_);
11,263!
159
    db_options.env = env_.get();
11,262✔
160
    auto cf_options = default_column_family_options();
11,264!
161

162
    std::vector<std::string> column_family_names;
11,264✔
163
    auto list_status = ::rocksdb::DB::ListColumnFamilies(db_options, db_path_,
11,264!
164
                                                         &column_family_names);
5,548!
165
    if (!list_status.ok()) {
11,264!
166
        if (open_mode_ == OpenMode::ReadOnly) {
1,136✔
167
            throw DFTUtilsException(
66!
168
                ErrorCode::IO, "Failed to list RocksDB column families at '" +
22!
169
                                   db_path_ + "': " + list_status.ToString());
66!
170
        }
171
        column_family_names.reserve(default_column_families().size());
1,092!
172
        for (auto name : default_column_families()) {
29,488✔
173
            column_family_names.emplace_back(name);
28,398!
174
        }
175
    } else {
547✔
176
        if (open_mode_ == OpenMode::ReadWrite) {
10,126✔
177
            for (const auto& name : default_column_families()) {
15,707✔
178
                if (std::find(column_family_names.begin(),
22,693!
179
                              column_family_names.end(),
7,566✔
180
                              name) == column_family_names.end()) {
22,684!
181
                    column_family_names.emplace_back(name);
×
182
                }
183
            }
184
        }
291✔
185
    }
186

187
    std::vector<::rocksdb::ColumnFamilyDescriptor> descriptors;
11,219✔
188
    descriptors.reserve(column_family_names.size());
11,220!
189
    for (const auto& name : column_family_names) {
302,905✔
190
        auto opts = cf_options;
291,691!
191
        if (cf_options_override_) {
291,691✔
192
            cf_options_override_(name, opts);
289,926!
193
        }
147,160✔
194
        descriptors.emplace_back(name, opts);
291,684!
195
    }
291,701✔
196

197
    std::vector<::rocksdb::ColumnFamilyHandle*> handles;
11,221✔
198
    ::rocksdb::Status status;
11,220!
199
    if (open_mode_ == OpenMode::ReadOnly) {
11,220✔
200
        status = ::rocksdb::DB::OpenForReadOnly(
9,544!
201
            db_options, db_path_, descriptors, &handles, &db_, false);
9,544!
202
    } else {
4,856✔
203
        status = ::rocksdb::DB::Open(db_options, db_path_, descriptors,
3,351!
204
                                     &handles, &db_);
1,676✔
205
    }
206
    if (!status.ok()) {
11,216!
207
        cleanup_failed_open(db_, handles);
×
208
        throw DFTUtilsException(ErrorCode::IO, "Failed to open RocksDB at '" +
×
209
                                                   db_path_ +
×
210
                                                   "': " + status.ToString());
×
211
    }
212

213
    column_families_.clear();
11,217✔
214
    for (std::size_t i = 0; i < descriptors.size(); ++i) {
302,909✔
215
        column_families_.emplace(descriptors[i].name, handles[i]);
291,692!
216
    }
148,044✔
217

218
    return true;
5,526✔
219
}
11,348✔
220

221
void RocksDatabase::close() {
22,528✔
222
    if (db_ == nullptr) {
22,528✔
223
        column_families_.clear();
11,308✔
224
        return;
11,308✔
225
    }
226

227
    if (process_exiting_flag().load(std::memory_order_relaxed)) {
11,220!
228
        db_ = nullptr;
×
229
        column_families_.clear();
×
230
        env_.reset();
×
231
        file_system_.reset();
×
232
        db_path_.clear();
×
233
        return;
×
234
    }
235

236
    for (auto& entry : column_families_) {
302,940✔
237
        if (entry.second != nullptr) {
291,720✔
238
            db_->DestroyColumnFamilyHandle(entry.second);
291,720!
239
            entry.second = nullptr;
291,720✔
240
        }
148,044✔
241
    }
242
    column_families_.clear();
11,220✔
243

244
    auto* db = db_;
11,220✔
245
    db_ = nullptr;
11,220✔
246
    static_cast<void>(db->Close());
11,220✔
247
    delete db;
11,220✔
248
    env_.reset();
11,220✔
249
    file_system_.reset();
11,220✔
250
    db_path_.clear();
11,220✔
251
}
11,432✔
252

253
bool RocksDatabase::is_open() const noexcept { return db_ != nullptr; }
36✔
254

255
bool RocksDatabase::is_read_only() const noexcept {
1,892✔
256
    return open_mode_ == OpenMode::ReadOnly;
1,892✔
257
}
258

259
const std::string& RocksDatabase::path() const noexcept { return db_path_; }
×
260

261
::rocksdb::DB* RocksDatabase::get() const noexcept { return db_; }
×
262

263
::rocksdb::ColumnFamilyHandle* RocksDatabase::column_family_handle(
75,023✔
264
    std::string_view column_family) const {
265
    const auto name = column_family.empty() ? std::string(cf::DEFAULT)
75,023!
266
                                            : std::string(column_family);
112,498!
267
    const auto it = column_families_.find(name);
75,003✔
268
    if (it == column_families_.end() || it->second == nullptr) {
74,994!
269
        throw DFTUtilsException(ErrorCode::INVALID_ARGUMENT,
×
270
                                "Unknown RocksDB column family: " + name);
×
271
    }
272
    return it->second;
112,428✔
273
}
74,951✔
274

275
::rocksdb::Status RocksDatabase::put(std::string_view key,
1,436✔
276
                                     std::string_view value,
277
                                     std::string_view column_family) {
278
    return db_->Put(write_options(), column_family_handle(column_family),
2,154✔
279
                    ::rocksdb::Slice(key.data(), key.size()),
1,436✔
280
                    ::rocksdb::Slice(value.data(), value.size()));
2,872!
281
}
282

283
::rocksdb::Status RocksDatabase::get(std::string_view key, std::string* value,
12,618✔
284
                                     std::string_view column_family) const {
285
    return db_->Get(read_options(), column_family_handle(column_family),
18,927✔
286
                    ::rocksdb::Slice(key.data(), key.size()), value);
18,918!
287
}
288

289
::rocksdb::Status RocksDatabase::merge(std::string_view key,
×
290
                                       std::string_view value,
291
                                       std::string_view column_family) {
292
    return db_->Merge(write_options(), column_family_handle(column_family),
×
293
                      ::rocksdb::Slice(key.data(), key.size()),
×
294
                      ::rocksdb::Slice(value.data(), value.size()));
×
295
}
296

297
void RocksDatabase::set_cf_options_override(CfOptionsOverride override) {
11,191✔
298
    cf_options_override_ = std::move(override);
11,191✔
299
}
11,196✔
300

301
::rocksdb::Status RocksDatabase::merge(Batch& batch,
1,762✔
302
                                       std::string_view column_family,
303
                                       std::string_view key,
304
                                       std::string_view value) {
305
    return batch.Merge(column_family_handle(column_family),
2,646✔
306
                       ::rocksdb::Slice(key.data(), key.size()),
1,759✔
307
                       ::rocksdb::Slice(value.data(), value.size()));
3,527!
308
}
309

310
::rocksdb::Status RocksDatabase::del(std::string_view key,
×
311
                                     std::string_view column_family) {
312
    return db_->Delete(write_options(), column_family_handle(column_family),
×
313
                       ::rocksdb::Slice(key.data(), key.size()));
×
314
}
315

316
::rocksdb::Status RocksDatabase::delete_range(std::string_view begin_key,
×
317
                                              std::string_view end_key,
318
                                              std::string_view column_family) {
319
    return db_->DeleteRange(
×
320
        write_options(), column_family_handle(column_family),
321
        ::rocksdb::Slice(begin_key.data(), begin_key.size()),
×
322
        ::rocksdb::Slice(end_key.data(), end_key.size()));
×
323
}
324

325
::rocksdb::Status RocksDatabase::put(Batch& batch,
49,071✔
326
                                     std::string_view column_family,
327
                                     std::string_view key,
328
                                     std::string_view value) {
329
    return batch.Put(column_family_handle(column_family),
73,596✔
330
                     ::rocksdb::Slice(key.data(), key.size()),
49,060✔
331
                     ::rocksdb::Slice(value.data(), value.size()));
98,191!
332
}
333

334
::rocksdb::Status RocksDatabase::del(Batch& batch,
120✔
335
                                     std::string_view column_family,
336
                                     std::string_view key) {
337
    return batch.Delete(column_family_handle(column_family),
180✔
338
                        ::rocksdb::Slice(key.data(), key.size()));
180!
339
}
340

341
RocksDatabase::Batch RocksDatabase::begin_batch() const { return Batch(); }
2,627✔
342

343
::rocksdb::Status RocksDatabase::commit_batch(Batch& batch) {
2,626✔
344
    return db_->Write(write_options(), &batch);
2,626✔
345
}
346

347
std::unique_ptr<::rocksdb::Iterator> RocksDatabase::new_iterator(
9,534✔
348
    std::string_view column_family) const {
349
    return std::unique_ptr<::rocksdb::Iterator>(
4,773✔
350
        db_->NewIterator(read_options(), column_family_handle(column_family)));
9,534✔
351
}
352

353
::rocksdb::Status RocksDatabase::compact(std::string_view column_family) {
48✔
354
    ::rocksdb::CompactRangeOptions opts;
48✔
355
    opts.max_subcompactions = 8;
48✔
356
    return db_->CompactRange(opts, column_family_handle(column_family), nullptr,
48✔
357
                             nullptr);
48!
358
}
359

360
::rocksdb::Status RocksDatabase::ingest_external_files(
470✔
361
    std::string_view column_family,
362
    const std::vector<std::string>& external_files, bool ingest_behind) {
363
    if (external_files.empty()) {
470✔
364
        return ::rocksdb::Status::OK();
26✔
365
    }
366
    ::rocksdb::IngestExternalFileOptions opts;
444✔
367
    opts.move_files = false;
444✔
368
    opts.snapshot_consistency = true;
444✔
369
    opts.allow_global_seqno = true;
444✔
370
    opts.allow_blocking_flush = true;
444✔
371
    opts.ingest_behind = ingest_behind;
444✔
372
    return db_->IngestExternalFile(column_family_handle(column_family),
666✔
373
                                   external_files, opts);
444!
374
}
235✔
375

376
}  // namespace dftracer::utils::rocksdb
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc