Skip to content
Merged
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/frontend-lookup-retry.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed request retries when `CompactedVersionConflict` is thrown during KV-backed endpoint lookup.
See [transaction semantics](doc/build_apps/kv/kv_how_to.rst#transaction-semantics).
5 changes: 4 additions & 1 deletion doc/build_apps/kv/kv_how_to.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ A reference to a new :cpp:class:`ccf::kv::Tx` is passed to each endpoint handler

Each :cpp:class:`ccf::kv::Tx` gets a consistent, opaque view of the KV, including the values which have been left by previous writes. Any writes produced by this transaction will be visible to all future transactions.

When the endpoint handler indicates that its :cpp:class:`ccf::kv::Tx` should be applied (see :ref:`this section <build_apps/kv/kv_how_to:Applying and reverting writes>` for details), the executing node attempts to apply the changes to its local KV. If this produces conflicts with concurrently executing transactions, it will be automatically re-executed. Once the transaction is applied successfully, it is automatically replicated to other nodes and will, if the network is healthy, eventually be committed.
When the endpoint handler indicates that its :cpp:class:`ccf::kv::Tx` should be applied (see :ref:`this section <build_apps/kv/kv_how_to:Applying and reverting writes>` for details), the executing node attempts to apply the changes to its local KV.
If this produces conflicts with concurrently executing transactions, it will be automatically re-executed.
Conflicts with KV compaction during KV-backed endpoint lookup or handler execution also cause the request to be retried with a fresh transaction, including endpoint lookup.
Once the transaction is applied successfully, it is automatically replicated to other nodes and will, if the network is healthy, eventually be committed.

For each :cpp:type:`ccf::kv::Map` that a transaction wants to write to or read from, a :cpp:class:`ccf::kv::MapHandle` must first be acquired. These are acquired from the :cpp:func:`ccf::kv::Tx::rw` (`read-write`) method. These may be acquired either by name (in which case the desired type must be explicitly specified as a template parameter), or by using a :cpp:type:`ccf::kv::Map` instance which defines both the map's name and key-value types.

Expand Down
12 changes: 6 additions & 6 deletions src/node/rpc/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -954,14 +954,14 @@ namespace ccf

++attempts;

endpoint = find_endpoint(ctx, *tx_p);
if (endpoint == nullptr)
{
return;
}

try
{
endpoint = find_endpoint(ctx, *tx_p);
if (endpoint == nullptr)
{
return;
}

if (!check_uri_allowed(ctx, endpoint))
{
return;
Expand Down
105 changes: 105 additions & 0 deletions src/node/rpc/test/frontend_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1798,6 +1798,111 @@ TEST_CASE("Retry on conflict")
}
}

class CompactingEndpointRegistry : public UserEndpointRegistry
{
using Values = ccf::kv::Map<size_t, size_t>;
ccf::kv::Store& tables;
const Values snapshot{"public:lookup_snapshot"};
const Values metadata{"public:lookup_metadata"};
size_t conflicts;

public:
size_t lookup_count = 0;
size_t execution_count = 0;

CompactingEndpointRegistry(
ccf::AbstractNodeContext& context,
ccf::kv::Store& tables_,
size_t conflicts_) :
UserEndpointRegistry(context),
tables(tables_),
conflicts(conflicts_)
{
auto tx = tables.create_tx();
tx.rw(snapshot)->put(0, 0);
tx.rw(metadata)->put(0, 0);
REQUIRE(tx.commit() == ccf::kv::CommitResult::SUCCESS);

make_endpoint(
"/lookup",
HTTP_POST,
[this](auto& ctx) {
++execution_count;
ctx.rpc_ctx->set_response_status(HTTP_STATUS_OK);
ctx.rpc_ctx->set_response_body(
std::to_string(ctx.tx.ro(metadata)->get(0).value()));
},
no_auth_required)
.install();
}

ccf::endpoints::EndpointDefinitionPtr find_endpoint(
ccf::kv::Tx& tx, ccf::RpcContext& ctx) override
{
++lookup_count;
// Pin the request's snapshot before compaction races metadata acquisition.
tx.ro(snapshot)->get(0);
if (lookup_count <= conflicts)
{
auto update = tables.create_tx();
update.rw(metadata)->put(0, lookup_count);
REQUIRE(update.commit() == ccf::kv::CommitResult::SUCCESS);
tables.compact(tables.current_version());
ctx.set_response_header("test-lookup-conflicted", "true");
}

// Acquiring this handle throws a real CompactedVersionConflict if the
// request's snapshot has just been compacted away.
tx.ro(metadata)->get(0);
return UserEndpointRegistry::find_endpoint(tx, ctx);
}
};

TEST_CASE("Retry endpoint lookup on compaction conflict")
{
constexpr size_t max_attempts = 30;
size_t conflicts = 1;
SUBCASE("Lookup succeeds on a fresh transaction") {}
SUBCASE("Lookup succeeds on the final attempt")
{
conflicts = max_attempts - 1;
}
SUBCASE("Lookup exhausts the retry limit")
{
conflicts = max_attempts;
}

NetworkState network;
prepare_callers(network);
ccf::StubNodeContext context;
CompactingEndpointRegistry registry(context, *network.tables, conflicts);
RpcFrontend frontend(*network.tables, registry, context);
frontend.open();

auto rpc_ctx = ccf::make_rpc_context(
anonymous_session, create_simple_request("/lookup").build_request());
frontend.process(rpc_ctx);
CHECK_FALSE(rpc_ctx->response_is_pending);
const auto response = parse_response(rpc_ctx->serialise_response());
CHECK_FALSE(response.headers.contains("test-lookup-conflicted"));
if (conflicts < max_attempts)
{
CHECK(response.status == HTTP_STATUS_OK);
CHECK(parse_response_body(response) == std::to_string(conflicts));
CHECK(registry.lookup_count == conflicts + 1);
CHECK(registry.execution_count == 1);
}
else
{
CHECK(response.status == HTTP_STATUS_SERVICE_UNAVAILABLE);
CHECK(
parse_response_body<nlohmann::json>(response)["error"]["code"] ==
ccf::errors::TransactionCommitAttemptsExceedLimit);
CHECK(registry.lookup_count == max_attempts);
CHECK(registry.execution_count == 0);
}
}

class TestCommitHistory : public ccf::NullTxHistory
{
public:
Expand Down