diff --git a/.changes/frontend-lookup-retry.fixed.md b/.changes/frontend-lookup-retry.fixed.md new file mode 100644 index 000000000000..f8e7e988d2ed --- /dev/null +++ b/.changes/frontend-lookup-retry.fixed.md @@ -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). diff --git a/doc/build_apps/kv/kv_how_to.rst b/doc/build_apps/kv/kv_how_to.rst index 287a70b2c2d8..af844210a5aa 100644 --- a/doc/build_apps/kv/kv_how_to.rst +++ b/doc/build_apps/kv/kv_how_to.rst @@ -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 ` 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 ` 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. diff --git a/src/node/rpc/frontend.h b/src/node/rpc/frontend.h index ac39cbde73fa..cfb70865f3b0 100644 --- a/src/node/rpc/frontend.h +++ b/src/node/rpc/frontend.h @@ -965,14 +965,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; diff --git a/src/node/rpc/test/frontend_test.cpp b/src/node/rpc/test/frontend_test.cpp index 6f257755750f..70355a82a626 100644 --- a/src/node/rpc/test/frontend_test.cpp +++ b/src/node/rpc/test/frontend_test.cpp @@ -1622,6 +1622,111 @@ TEST_CASE("Retry on conflict") } } +class CompactingEndpointRegistry : public UserEndpointRegistry +{ + using Values = ccf::kv::Map; + 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(response)["error"]["code"] == + ccf::errors::TransactionCommitAttemptsExceedLimit); + CHECK(registry.lookup_count == max_attempts); + CHECK(registry.execution_count == 0); + } +} + class TestCommitHistory : public ccf::NullTxHistory { public: