Files
scylladb/sstables/exceptions.hh
Botond Dénes 62b1f1a581 sstables/exceptions: introduce parse_assert()
To replace SCYLLA_ASSERT on the read/parse path. SSTables can get
corrupt for various reasons, some outside of the database's control. A
bad SSTable should not bring down the database, the parsing should
simply be aborted, with as much information printed as possible for the
investigation of the nature of the corruption.
The newly introduced parse_assert() uses on_internal_error() under the
hood, which prints a backtrace and optionally allows for aborting when
on the error, to generate a coredump.

(cherry picked from commit 27e26ed93f)
2025-07-03 09:53:50 +03:00

75 lines
2.6 KiB
C++

/*
* Copyright (C) 2015-present ScyllaDB
*
*/
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
*/
#pragma once
#include <concepts>
#include <seastar/core/format.hh>
#include "seastarx.hh"
namespace sstables {
class malformed_sstable_exception : public std::exception {
sstring _msg;
public:
malformed_sstable_exception(sstring msg, sstring filename)
: malformed_sstable_exception{format("{} in sstable {}", msg, filename)}
{}
malformed_sstable_exception(sstring s) : _msg(s) {}
const char *what() const noexcept {
return _msg.c_str();
}
};
[[noreturn]] void on_parse_error(sstring message, std::optional<sstring> filename);
// Use this instead of SCYLLA_ASSERT() or assert() in code that is used while parsing SSTables.
// SSTables can be corrupted either by ScyllaDB itself or by a freak accident like cosmic background
// radiation hitting the disk the wrong way. Either way a corrupt SSTable should not bring down the
// whole server. This method will call on_internal_error() if the condition is false.
// The exception will include a complete backtrace, so no need to add call-site identifiers to the message.
inline void parse_assert(bool condition, std::optional<sstring> filename = {}, const char* message = nullptr) {
if (!condition) [[unlikely]] {
on_parse_error(message, filename);
}
}
struct bufsize_mismatch_exception : malformed_sstable_exception {
bufsize_mismatch_exception(size_t size, size_t expected) :
malformed_sstable_exception(format("Buffer improperly sized to hold requested data. Got: {:d}. Expected: {:d}", size, expected))
{}
};
class compaction_job_exception : public std::exception {
sstring _msg;
public:
compaction_job_exception(sstring msg) noexcept : _msg(std::move(msg)) {}
const char *what() const noexcept {
return _msg.c_str();
}
};
// Indicates that compaction was stopped via an external event,
// E.g. shutdown or api call.
class compaction_stopped_exception : public compaction_job_exception {
public:
compaction_stopped_exception(sstring ks, sstring cf, sstring reason)
: compaction_job_exception(format("Compaction for {}/{} was stopped due to: {}", ks, cf, reason)) {}
};
// Indicates that compaction hit an unrecoverable error
// and should be aborted.
class compaction_aborted_exception : public compaction_job_exception {
public:
compaction_aborted_exception(sstring ks, sstring cf, sstring reason)
: compaction_job_exception(format("Compaction for {}/{} was aborted due to: {}", ks, cf, reason)) {}
};
}