Skip to content

Commit

Permalink
utils: introduce concurrent_modification_error exception
Browse files Browse the repository at this point in the history
It is handy to have a base class for all instances of concurrent
modifications.
  • Loading branch information
ztlpn committed May 10, 2024
1 parent 30bbfb1 commit 627db4d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
13 changes: 4 additions & 9 deletions src/v/cluster/topic_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,17 @@ class topic_table {
// * partition::get_revision_id()
// * raft::group_configuration::revision_id()

class concurrent_modification_error final : public std::exception {
class concurrent_modification_error final
: public ::concurrent_modification_error {
public:
concurrent_modification_error(
model::revision_id initial_revision,
model::revision_id current_revision)
: _msg(ssx::sformat(
: ::concurrent_modification_error(ssx::sformat(
"Topic table was modified by concurrent fiber. "
"(initial_revision: "
"{}, current_revision: {}) ",
"(initial_revision: {}, current_revision: {}) ",
initial_revision,
current_revision)) {}

const char* what() const noexcept final { return _msg.c_str(); }

private:
ss::sstring _msg;
};

class in_progress_update {
Expand Down
2 changes: 1 addition & 1 deletion src/v/cluster/topics_frontend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ topics_frontend::partitions_with_lost_majority(
co_return errc::concurrent_modification_error;
}
co_return result;
} catch (const topic_table::concurrent_modification_error& e) {
} catch (const concurrent_modification_error& e) {
// state changed while generating the plan, force caller to retry;
vlog(
clusterlog.info,
Expand Down
33 changes: 33 additions & 0 deletions src/v/utils/exceptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2024 Redpanda Data, Inc.
*
* Use of this software is governed by the Business Source License
* included in the file licenses/BSL.md
*
* As of the Change Date specified in that file, in accordance with
* the Business Source License, use of this software will be governed
* by the Apache License, Version 2.0
*/
#pragma once

#include "base/seastarx.h"

#include <seastar/core/sstring.hh>

#include <stdexcept>

/// Some objects reference state that changes comparatively rarely (e.g.
/// topic_table state) across yield points and expect these references to remain
/// valid. In case these references are invalidated by a concurrent fiber, this
/// exception is thrown. This is a signal for the caller to restart the
/// computation with up-to-date state.
class concurrent_modification_error : public std::exception {
public:
explicit concurrent_modification_error(ss::sstring s)
: _msg(std::move(s)) {}

const char* what() const noexcept override { return _msg.c_str(); }

private:
ss::sstring _msg;
};
10 changes: 5 additions & 5 deletions src/v/utils/stable_iterator_adaptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@
#pragma once

#include "base/seastarx.h"
#include "utils/exceptions.h"

#include <seastar/util/noncopyable_function.hh>

#include <boost/iterator/iterator_adaptor.hpp>
#include <fmt/format.h>

#include <stdexcept>
#include <string_view>
#include <version>

class iterator_stability_violation : public std::runtime_error {
class iterator_stability_violation final
: public concurrent_modification_error {
public:
explicit iterator_stability_violation(const std::string& why)
: std::runtime_error(why){};
explicit iterator_stability_violation(ss::sstring why)
: concurrent_modification_error(std::move(why)){};
};

/*
Expand Down

0 comments on commit 627db4d

Please sign in to comment.