Skip to content
Closed
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
2 changes: 2 additions & 0 deletions .changes/cft-trace-lock-hold.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Consensus protocol trace events exported under the `ccf.cft` Fluentd tag now include a `lock_hold` field, numbering the hold of the node's consensus lock in which the event was emitted.
Collectors need no configuration change.
47 changes: 23 additions & 24 deletions src/consensus/aft/cft.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the Apache 2.0 License.
#pragma once

#include "ccf/ds/locking.h"
#include "ccf/reconfiguration_type.h"
#include "ccf/tx_id.h"
#include "ccf/tx_status.h"
Expand Down Expand Up @@ -231,7 +230,7 @@ namespace aft
// is not called from Store::commit() under the KV version lock, so it
// can be read under the lock rather than made atomic. std::optional<
// NodeId> could not be made atomic in any case.
std::lock_guard<ccf::ds::Mutex> guard(state->lock);
std::lock_guard<ccf::tracing::TracedMutex> guard(state->lock);
return leader_id;
}

Expand All @@ -253,7 +252,7 @@ namespace aft

bool can_replicate() override
{
std::unique_lock<ccf::ds::Mutex> guard(state->lock);
std::unique_lock<ccf::tracing::TracedMutex> guard(state->lock);
return can_replicate_unsafe();
}

Expand All @@ -271,13 +270,13 @@ namespace aft
{
return false;
}
std::unique_lock<ccf::ds::Mutex> guard(state->lock);
std::unique_lock<ccf::tracing::TracedMutex> guard(state->lock);
return state->last_idx - state->commit_idx >= max_uncommitted_tx_count;
}

Consensus::SignatureDisposition get_signature_disposition() override
{
std::unique_lock<ccf::ds::Mutex> guard(state->lock);
std::unique_lock<ccf::tracing::TracedMutex> guard(state->lock);
if (can_sign_unsafe())
{
if (should_sign)
Expand Down Expand Up @@ -423,7 +422,7 @@ namespace aft
{
// When receiving append entries as a follower, all security domains will
// be deserialised
std::lock_guard<ccf::ds::Mutex> guard(state->lock);
std::lock_guard<ccf::tracing::TracedMutex> guard(state->lock);
public_only = false;
}

Expand All @@ -437,7 +436,7 @@ namespace aft
"Can't force leadership if there is already a leader");
}

std::lock_guard<ccf::ds::Mutex> guard(state->lock);
std::lock_guard<ccf::tracing::TracedMutex> guard(state->lock);
state->current_view += starting_view_change;
become_leader();
}
Expand All @@ -456,7 +455,7 @@ namespace aft
"Can't force leadership if there is already a leader");
}

std::lock_guard<ccf::ds::Mutex> guard(state->lock);
std::lock_guard<ccf::tracing::TracedMutex> guard(state->lock);
state->current_view = term;
state->last_idx = index;
state->commit_idx = commit_idx_;
Expand All @@ -474,7 +473,7 @@ namespace aft
{
// This should only be called when the node resumes from a snapshot and
// before it has received any append entries.
std::lock_guard<ccf::ds::Mutex> guard(state->lock);
std::lock_guard<ccf::tracing::TracedMutex> guard(state->lock);

trace::init_as_backup(*state, index, term);

Expand All @@ -495,26 +494,26 @@ namespace aft

Index get_committed_seqno() override
{
std::lock_guard<ccf::ds::Mutex> guard(state->lock);
std::lock_guard<ccf::tracing::TracedMutex> guard(state->lock);
return get_commit_idx_unsafe();
}

Term get_view() override
{
std::lock_guard<ccf::ds::Mutex> guard(state->lock);
std::lock_guard<ccf::tracing::TracedMutex> guard(state->lock);
return state->current_view;
}

std::pair<Term, Index> get_committed_txid() override
{
std::lock_guard<ccf::ds::Mutex> guard(state->lock);
std::lock_guard<ccf::tracing::TracedMutex> guard(state->lock);
ccf::SeqNo commit_idx = get_commit_idx_unsafe();
return {get_term_internal(commit_idx), commit_idx};
}

Term get_view(Index idx) override
{
std::lock_guard<ccf::ds::Mutex> guard(state->lock);
std::lock_guard<ccf::tracing::TracedMutex> guard(state->lock);
return get_term_internal(idx);
}

Expand Down Expand Up @@ -610,14 +609,14 @@ namespace aft

Configuration::Nodes get_latest_configuration() override
{
std::lock_guard<ccf::ds::Mutex> guard(state->lock);
std::lock_guard<ccf::tracing::TracedMutex> guard(state->lock);
return get_latest_configuration_unsafe();
}

ccf::kv::ConsensusDetails get_details() override
{
ccf::kv::ConsensusDetails details;
std::lock_guard<ccf::ds::Mutex> guard(state->lock);
std::lock_guard<ccf::tracing::TracedMutex> guard(state->lock);
details.primary_id = leader_id;
details.current_view = state->current_view;
details.ticking = ticking;
Expand All @@ -642,7 +641,7 @@ namespace aft

bool replicate(const ccf::kv::BatchVector& entries, Term term) override
{
std::lock_guard<ccf::ds::Mutex> guard(state->lock);
std::lock_guard<ccf::tracing::TracedMutex> guard(state->lock);

if (state->leadership_state.load() != ccf::kv::LeadershipState::Leader)
{
Expand Down Expand Up @@ -849,7 +848,7 @@ namespace aft

void periodic(std::chrono::milliseconds elapsed) override
{
std::unique_lock<ccf::ds::Mutex> guard(state->lock);
std::unique_lock<ccf::tracing::TracedMutex> guard(state->lock);
trace::tick(*state);
timeout_elapsed += elapsed;

Expand Down Expand Up @@ -1107,7 +1106,7 @@ namespace aft
const uint8_t* data,
size_t size)
{
std::unique_lock<ccf::ds::Mutex> guard(state->lock);
std::unique_lock<ccf::tracing::TracedMutex> guard(state->lock);

CFT_DEBUG_FMT(
"Recv {} to {} from {}: {}.{} to {}.{} in term {}",
Expand Down Expand Up @@ -1536,7 +1535,7 @@ namespace aft
void recv_append_entries_response(
const ccf::NodeId& from, AppendEntriesResponse r)
{
std::lock_guard<ccf::ds::Mutex> guard(state->lock);
std::lock_guard<ccf::tracing::TracedMutex> guard(state->lock);

auto node = all_other_nodes.find(from);
if (node == all_other_nodes.end())
Expand Down Expand Up @@ -1791,7 +1790,7 @@ namespace aft

void recv_request_vote(const ccf::NodeId& from, RequestVote r)
{
std::lock_guard<ccf::ds::Mutex> guard(state->lock);
std::lock_guard<ccf::tracing::TracedMutex> guard(state->lock);

trace::recv_request_vote(*state, r, from.value());

Expand All @@ -1800,7 +1799,7 @@ namespace aft

void recv_request_pre_vote(const ccf::NodeId& from, RequestPreVote r)
{
std::lock_guard<ccf::ds::Mutex> guard(state->lock);
std::lock_guard<ccf::tracing::TracedMutex> guard(state->lock);

trace::recv_request_vote(*state, r, from.value());

Expand Down Expand Up @@ -1857,7 +1856,7 @@ namespace aft
RequestVoteResponse r,
ElectionType election_type)
{
std::lock_guard<ccf::ds::Mutex> guard(state->lock);
std::lock_guard<ccf::tracing::TracedMutex> guard(state->lock);

trace::recv_request_vote_response(*state, r, from.value());

Expand Down Expand Up @@ -1980,7 +1979,7 @@ namespace aft
void recv_propose_request_vote(
const ccf::NodeId& from, ProposeRequestVote r)
{
std::lock_guard<ccf::ds::Mutex> guard(state->lock);
std::lock_guard<ccf::tracing::TracedMutex> guard(state->lock);

trace::recv_propose_request_vote(*state, r, from.value());

Expand Down Expand Up @@ -2730,7 +2729,7 @@ namespace aft
public:
void nominate_successor() override
{
std::lock_guard<ccf::ds::Mutex> guard(state->lock);
std::lock_guard<ccf::tracing::TracedMutex> guard(state->lock);
trace::nomination(*state);
nominate_successor_unsafe();
}
Expand Down
4 changes: 2 additions & 2 deletions src/consensus/aft/impl/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
#pragma once

#include "ccf/crypto/verifier.h"
#include "ccf/ds/locking.h"
#include "ccf/tx_status.h"
#include "consensus/aft/cft_types.h"
#include "ds/internal_logger.h"
#include "kv/kv_types.h"
#include "tracing/traced_mutex.h"

#include <atomic>
#include <deque>
Expand Down Expand Up @@ -172,7 +172,7 @@ namespace aft

State() = default;

ccf::ds::Mutex lock;
ccf::tracing::TracedMutex lock;

ccf::NodeId node_id;
ccf::View current_view = 0;
Expand Down
46 changes: 37 additions & 9 deletions src/tracing/test/fluentd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,19 +467,26 @@ TEST_CASE("SPSC export: framing, producer isolation, drops and shutdown")
static_cast<ssize_t>(actual.size()));
CHECK(actual == buffer);
};
check_event([] { request_trace::empty(); }, {{"function", "empty"}});
check_event(
request_trace::empty_from_other_translation_unit, {{"function", "empty"}});
[] { request_trace::empty(); }, {{"function", "empty"}, {"lock_hold", 0}});
check_event(
request_trace::empty_from_other_translation_unit,
{{"function", "empty"}, {"lock_hold", 0}});
check_event(
[] { request_trace::single(request_trace::Nested{7, false}); },
{{"function", "single"}, {"value", {{"number", 7}, {"flag", false}}}});
{{"function", "single"},
{"lock_hold", 0},
{"value", {{"number", 7}, {"flag", false}}}});
check_event(
request_trace::single_from_other_translation_unit,
{{"function", "single"}, {"value", {{"number", 7}, {"flag", false}}}});
{{"function", "single"},
{"lock_hold", 0},
{"value", {{"number", 7}, {"flag", false}}}});
int evaluations = 0;
check_event(
[&] { request_trace::request("/app/log", ++evaluations, false); },
{{"function", "request"},
{"lock_hold", 0},
{"path", "/app/log"},
{"status", 1},
{"cached", false}});
Expand All @@ -497,10 +504,26 @@ TEST_CASE("SPSC export: framing, producer isolation, drops and shutdown")
ccf::tracing::emit("ccf.cft", "probe", request_trace::EncodingProbe{encoded});
CHECK_FALSE(encoded);
CHECK(ccf::tracing::event_buffer() == buffer_before_filtering);
check_event([] { request_trace::empty(); }, {{"function", "empty"}});
check_event(
[] { request_trace::empty(); }, {{"function", "empty"}, {"lock_hold", 0}});
CHECK(
nlohmann::json::from_msgpack(ccf::tracing::event_buffer())[2]["h_ts"] ==
sequence_before_filtering + 1);
// Each hold of a traced mutex is numbered in the events emitted in it
ccf::tracing::TracedMutex outer_mutex;
ccf::tracing::TracedMutex inner_mutex;
for (const auto hold : {1, 2})
{
std::lock_guard<ccf::tracing::TracedMutex> outer_guard(outer_mutex);
{
std::lock_guard<ccf::tracing::TracedMutex> inner_guard(inner_mutex);
}
check_event(
[] { request_trace::empty(); },
{{"function", "empty"}, {"lock_hold", hold}});
}
check_event(
[] { request_trace::empty(); }, {{"function", "empty"}, {"lock_hold", 0}});

for (const auto level :
{ccf::LoggerLevel::TRACE,
Expand Down Expand Up @@ -608,14 +631,16 @@ TEST_CASE("SPSC export: framing, producer isolation, drops and shutdown")
CHECK_NOTHROW(request_trace::single(large_value));
allocation_failure = 0;
CHECK(Sink::dropped_count() == before_encoding_failure + 1);
check_event([] { request_trace::empty(); }, {{"function", "empty"}});
check_event(
[] { request_trace::empty(); }, {{"function", "empty"}, {"lock_hold", 0}});

const std::string oversized_value(
ccf::tracing::SPSCQueue::MAX_RECORD_SIZE, 'x');
const auto before_oversized = Sink::dropped_count();
request_trace::single(oversized_value);
CHECK(Sink::dropped_count() == before_oversized + 1);
check_event([] { request_trace::empty(); }, {{"function", "empty"}});
check_event(
[] { request_trace::empty(); }, {{"function", "empty"}, {"lock_hold", 0}});

std::thread second([&] {
Sink::bind_producer(1);
Expand Down Expand Up @@ -646,7 +671,9 @@ TEST_CASE("SPSC export: framing, producer isolation, drops and shutdown")
static_cast<ssize_t>(received.size()));
CHECK(received == bytes);
CHECK(Sink::dropped_count() == drops_before_disconnect);
check_event([] { request_trace::empty(); }, {{"function", "empty"}});
check_event(
[] { request_trace::empty(); },
{{"function", "empty"}, {"lock_hold", 0}});
}

const auto drops_before_allocation_failures = Sink::dropped_count();
Expand All @@ -667,7 +694,8 @@ TEST_CASE("SPSC export: framing, producer isolation, drops and shutdown")
allocation_failure = 0;
}
CHECK(Sink::dropped_count() == drops_before_emission_failures + 2);
check_event([] { request_trace::empty(); }, {{"function", "empty"}});
check_event(
[] { request_trace::empty(); }, {{"function", "empty"}, {"lock_hold", 0}});

stalled = true;
std::array<uint8_t, 128> payload = {};
Expand Down
10 changes: 7 additions & 3 deletions src/tracing/trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "msgpack/fluentd_event_time.h"
#include "msgpack/serialization.h"
#include "tracing/fluentd_sink.h"
#include "tracing/traced_mutex.h"

#include <atomic>
#include <chrono>
Expand Down Expand Up @@ -90,15 +91,18 @@ namespace ccf::tracing
CCF_TRACE_ARGUMENT_FOR_JSON_NEXT(TYPE, MEMBER)

// DECLARE_TRACE_EVENT(event, tag, field) declares event(const auto& field).
// Declare in the caller's namespace. The wrapper adds the function field.
// Declare in the caller's namespace. The wrapper adds the function field and
// lock_hold, the TracedMutex hold the event is emitted in.
#define DECLARE_TRACE_EVENT(NAME, TAG, ...) \
inline void NAME(__VA_OPT__(_FOR_JSON_COUNT_NN(__VA_ARGS__)(POP1)( \
CCF_TRACE_PARAMETER, _, __VA_ARGS__))) \
{ \
ccf::tracing::emit( \
TAG, \
"function", \
#NAME __VA_OPT__(_FOR_JSON_COUNT_NN(__VA_ARGS__)(POP1)( \
CCF_TRACE_ARGUMENT, _, __VA_ARGS__))); \
#NAME, \
"lock_hold", \
ccf::tracing::lock_hold __VA_OPT__(_FOR_JSON_COUNT_NN(__VA_ARGS__)( \
POP1)(CCF_TRACE_ARGUMENT, _, __VA_ARGS__))); \
} \
REQUIRES_SEMICOLON_TERMINATION
Loading