Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent racey access to indexer state #6886

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/ccf/indexing/indexer_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "ccf/indexing/strategy.h"
#include "ccf/node_subsystem_interface.h"
#include "ccf/pal/locking.h"

#include <map>
#include <memory>
Expand All @@ -19,6 +20,7 @@ namespace ccf::indexing
class IndexingStrategies : public ccf::AbstractNodeSubSystem
{
protected:
ccf::pal::Mutex lock;
std::set<StrategyPtr> strategies;

public:
Expand All @@ -36,11 +38,13 @@ namespace ccf::indexing
throw std::logic_error("Tried to install null strategy");
}

std::lock_guard<ccf::pal::Mutex> guard(lock);
return strategies.insert(strategy).second;
}

void uninstall_strategy(const StrategyPtr& strategy)
{
std::lock_guard<ccf::pal::Mutex> guard(lock);
if (strategy == nullptr || strategies.find(strategy) == strategies.end())
{
throw std::logic_error("Strategy doesn't exist");
Expand All @@ -51,6 +55,7 @@ namespace ccf::indexing

nlohmann::json describe() const
{
std::lock_guard<ccf::pal::Mutex> guard(lock);
auto j = nlohmann::json::array();

for (const auto& strategy : strategies)
Expand Down
4 changes: 4 additions & 0 deletions include/ccf/indexing/strategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#pragma once

#include "ccf/kv/read_only_store.h"
#include "ccf/pal/locking.h"
#include "ccf/tx_id.h"

#include <optional>
Expand Down Expand Up @@ -68,6 +69,7 @@ namespace ccf::indexing
class LazyStrategy : public Base
{
protected:
ccf::pal::Mutex lock;
ccf::SeqNo max_requested_seqno = 0;

public:
Expand All @@ -78,6 +80,7 @@ namespace ccf::indexing
const auto base = Base::next_requested();
if (base.has_value())
{
std::lock_guard<ccf::pal::Mutex> guard(lock);
if (*base <= max_requested_seqno)
{
return base;
Expand All @@ -89,6 +92,7 @@ namespace ccf::indexing

void extend_index_to(ccf::TxID to_txid)
{
std::lock_guard<ccf::pal::Mutex> guard(lock);
if (to_txid.seqno > max_requested_seqno)
{
max_requested_seqno = to_txid.seqno;
Expand Down
3 changes: 3 additions & 0 deletions src/indexing/indexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ namespace ccf::indexing
update_commit(newly_committed);

std::optional<ccf::SeqNo> min_requested = std::nullopt;

std::lock_guard<ccf::pal::Mutex> guard(lock);

for (auto& strategy : strategies)
{
strategy->tick();
Expand Down
Loading