NetworkedApplications: versioned KV domain with idempotent outcomes
This commit is contained in:
1 parent
a2a0e1a4c3
commit
3253b8b4d4
4 files changed
+477
-1
No files matched your search
@@ -9,12 +9,16 @@ set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
|
||||
find_package(Void CONFIG REQUIRED COMPONENTS Runtime)
|
||||
|
||||
add_library(networked_kv STATIC src/application.cpp)
|
||||
add_library(networked_kv STATIC src/application.cpp src/memory_store.cpp)
|
||||
target_include_directories(networked_kv PUBLIC include)
|
||||
target_link_libraries(networked_kv PUBLIC Void::Runtime)
|
||||
|
||||
add_executable(networked_kv_test tests/application_test.cpp)
|
||||
target_link_libraries(networked_kv_test PRIVATE networked_kv)
|
||||
|
||||
add_executable(networked_kv_domain_test tests/domain_test.cpp)
|
||||
target_link_libraries(networked_kv_domain_test PRIVATE networked_kv)
|
||||
|
||||
enable_testing()
|
||||
add_test(NAME networked_kv_lifecycle COMMAND networked_kv_test)
|
||||
add_test(NAME networked_kv_domain COMMAND networked_kv_domain_test)
|
||||
@@ -0,0 +1,149 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Functional/Result.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace NetworkedKv
|
||||
{
|
||||
using Version = std::uint64_t;
|
||||
|
||||
struct Limits {
|
||||
std::size_t max_key_bytes{512};
|
||||
std::size_t max_value_bytes{1024ULL * 1024ULL};
|
||||
std::size_t max_request_id_bytes{128};
|
||||
std::size_t max_scan_page{1000};
|
||||
std::size_t max_operation_records{1024};
|
||||
};
|
||||
|
||||
struct Entry {
|
||||
std::string value;
|
||||
Version version{1};
|
||||
};
|
||||
|
||||
enum class PreconditionKind : std::uint8_t { Absent, Version };
|
||||
|
||||
/// Explicit absent/version precondition for a mutation.
|
||||
struct Precondition {
|
||||
PreconditionKind kind{PreconditionKind::Absent};
|
||||
Version version{0};
|
||||
|
||||
[[nodiscard]] static Precondition absent() noexcept
|
||||
{
|
||||
return {};
|
||||
}
|
||||
[[nodiscard]] static Precondition at_version(Version value) noexcept
|
||||
{
|
||||
return {PreconditionKind::Version, value};
|
||||
}
|
||||
};
|
||||
|
||||
/// Idempotency key for a mutation. Replaying the same id returns the
|
||||
/// recorded outcome instead of applying the mutation twice.
|
||||
struct RequestId {
|
||||
std::string value;
|
||||
};
|
||||
|
||||
enum class WriteOutcome : std::uint8_t {
|
||||
Applied,
|
||||
Conflict,
|
||||
NotFound,
|
||||
/// The backend could not prove whether the write committed.
|
||||
Unknown
|
||||
};
|
||||
|
||||
struct WriteResult {
|
||||
WriteOutcome outcome{WriteOutcome::Unknown};
|
||||
Version version{0};
|
||||
};
|
||||
|
||||
struct ScanItem {
|
||||
std::string key;
|
||||
Entry entry;
|
||||
};
|
||||
|
||||
struct ScanPage {
|
||||
std::vector<ScanItem> items;
|
||||
/// Opaque cursor for the next page; empty when the scan is done.
|
||||
std::optional<std::string> next_cursor;
|
||||
};
|
||||
|
||||
/// Versioned key-value domain contract shared by storage backends.
|
||||
class KvStore
|
||||
{
|
||||
public:
|
||||
|
||||
KvStore() = default;
|
||||
KvStore(const KvStore&) = delete;
|
||||
KvStore& operator=(const KvStore&) = delete;
|
||||
virtual ~KvStore() = default;
|
||||
|
||||
[[nodiscard]] virtual Void::Result::Result<std::optional<Entry>>
|
||||
get(std::string_view key) const = 0;
|
||||
|
||||
[[nodiscard]] virtual Void::Result::Result<WriteResult>
|
||||
put(std::string_view key, std::string value,
|
||||
Precondition precondition, RequestId request) = 0;
|
||||
|
||||
[[nodiscard]] virtual Void::Result::Result<WriteResult>
|
||||
erase(std::string_view key, Precondition precondition,
|
||||
RequestId request) = 0;
|
||||
|
||||
[[nodiscard]] virtual Void::Result::Result<ScanPage>
|
||||
scan(std::optional<std::string_view> cursor,
|
||||
std::size_t limit = 0) const = 0;
|
||||
};
|
||||
|
||||
/// Bounded in-memory reference backend. Durable backends implement the
|
||||
/// same contract without changing the domain semantics.
|
||||
class MemoryKvStore final : public KvStore
|
||||
{
|
||||
public:
|
||||
|
||||
explicit MemoryKvStore(Limits limits = {});
|
||||
|
||||
[[nodiscard]] Void::Result::Result<std::optional<Entry>>
|
||||
get(std::string_view key) const override;
|
||||
|
||||
[[nodiscard]] Void::Result::Result<WriteResult>
|
||||
put(std::string_view key, std::string value,
|
||||
Precondition precondition, RequestId request) override;
|
||||
|
||||
[[nodiscard]] Void::Result::Result<WriteResult>
|
||||
erase(std::string_view key, Precondition precondition,
|
||||
RequestId request) override;
|
||||
|
||||
[[nodiscard]] Void::Result::Result<ScanPage>
|
||||
scan(std::optional<std::string_view> cursor,
|
||||
std::size_t limit = 0) const override;
|
||||
|
||||
[[nodiscard]] const Limits& limits() const noexcept
|
||||
{
|
||||
return m_limits;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void>
|
||||
validate_key(std::string_view key) const;
|
||||
[[nodiscard]] Void::Result::Result<void>
|
||||
validate_request(const RequestId& request) const;
|
||||
[[nodiscard]] std::optional<WriteResult>
|
||||
replay(const RequestId& request) const;
|
||||
void remember(const RequestId& request, const WriteResult& result);
|
||||
|
||||
mutable std::mutex m_mutex;
|
||||
Limits m_limits;
|
||||
std::map<std::string, Entry, std::less<>> m_entries;
|
||||
std::map<std::string, WriteResult, std::less<>> m_operations;
|
||||
std::vector<std::string> m_operation_order;
|
||||
};
|
||||
} // namespace NetworkedKv
|
||||
@@ -0,0 +1,193 @@
|
||||
#include "networked_kv/domain.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
namespace NetworkedKv
|
||||
{
|
||||
namespace
|
||||
{
|
||||
[[nodiscard]] Void::Result::Error invalid(std::string message)
|
||||
{
|
||||
return Void::Result::Error{Void::Result::ErrorKind::invalid_argument,
|
||||
std::move(message)};
|
||||
}
|
||||
|
||||
[[nodiscard]] bool matches(const std::optional<Entry>& current,
|
||||
const Precondition& precondition) noexcept
|
||||
{
|
||||
if (precondition.kind == PreconditionKind::Absent) {
|
||||
return !current.has_value();
|
||||
}
|
||||
return current.has_value() &&
|
||||
current->version == precondition.version;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
MemoryKvStore::MemoryKvStore(Limits limits)
|
||||
: m_limits(limits)
|
||||
{
|
||||
}
|
||||
|
||||
Void::Result::Result<void>
|
||||
MemoryKvStore::validate_key(std::string_view key) const
|
||||
{
|
||||
if (key.empty()) {
|
||||
return std::unexpected{invalid("KvStore: key must not be empty")};
|
||||
}
|
||||
if (key.size() > m_limits.max_key_bytes) {
|
||||
return std::unexpected{
|
||||
invalid("KvStore: key exceeds max_key_bytes")};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Void::Result::Result<void>
|
||||
MemoryKvStore::validate_request(const RequestId& request) const
|
||||
{
|
||||
if (request.value.empty()) {
|
||||
return std::unexpected{
|
||||
invalid("KvStore: request id must not be empty")};
|
||||
}
|
||||
if (request.value.size() > m_limits.max_request_id_bytes) {
|
||||
return std::unexpected{
|
||||
invalid("KvStore: request id exceeds max_request_id_bytes")};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
std::optional<WriteResult>
|
||||
MemoryKvStore::replay(const RequestId& request) const
|
||||
{
|
||||
const auto found = m_operations.find(request.value);
|
||||
if (found == m_operations.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return found->second;
|
||||
}
|
||||
|
||||
void MemoryKvStore::remember(const RequestId& request,
|
||||
const WriteResult& result)
|
||||
{
|
||||
if (m_limits.max_operation_records == 0) {
|
||||
return;
|
||||
}
|
||||
if (m_operations.size() >= m_limits.max_operation_records &&
|
||||
!m_operation_order.empty()) {
|
||||
m_operations.erase(m_operation_order.front());
|
||||
m_operation_order.erase(m_operation_order.begin());
|
||||
}
|
||||
m_operations.emplace(request.value, result);
|
||||
m_operation_order.push_back(request.value);
|
||||
}
|
||||
|
||||
Void::Result::Result<std::optional<Entry>>
|
||||
MemoryKvStore::get(std::string_view key) const
|
||||
{
|
||||
if (auto valid = validate_key(key); !valid) {
|
||||
return std::unexpected{std::move(valid).error()};
|
||||
}
|
||||
std::lock_guard lock{m_mutex};
|
||||
const auto found = m_entries.find(key);
|
||||
if (found == m_entries.end()) {
|
||||
return std::optional<Entry>{};
|
||||
}
|
||||
return std::optional<Entry>{found->second};
|
||||
}
|
||||
|
||||
Void::Result::Result<WriteResult>
|
||||
MemoryKvStore::put(std::string_view key, std::string value,
|
||||
Precondition precondition, RequestId request)
|
||||
{
|
||||
if (auto valid = validate_key(key); !valid) {
|
||||
return std::unexpected{std::move(valid).error()};
|
||||
}
|
||||
if (auto valid = validate_request(request); !valid) {
|
||||
return std::unexpected{std::move(valid).error()};
|
||||
}
|
||||
if (value.size() > m_limits.max_value_bytes) {
|
||||
return std::unexpected{
|
||||
invalid("KvStore: value exceeds max_value_bytes")};
|
||||
}
|
||||
|
||||
std::lock_guard lock{m_mutex};
|
||||
if (const auto replayed = replay(request)) {
|
||||
return *replayed;
|
||||
}
|
||||
|
||||
const auto found = m_entries.find(key);
|
||||
const std::optional<Entry> current =
|
||||
found == m_entries.end() ? std::nullopt
|
||||
: std::optional<Entry>{found->second};
|
||||
if (!matches(current, precondition)) {
|
||||
WriteResult result{WriteOutcome::Conflict,
|
||||
current ? current->version : 0};
|
||||
remember(request, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
const Version version = current ? current->version + 1 : 1;
|
||||
m_entries.insert_or_assign(
|
||||
std::string{key}, Entry{std::move(value), version});
|
||||
WriteResult result{WriteOutcome::Applied, version};
|
||||
remember(request, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
Void::Result::Result<WriteResult>
|
||||
MemoryKvStore::erase(std::string_view key, Precondition precondition,
|
||||
RequestId request)
|
||||
{
|
||||
if (auto valid = validate_key(key); !valid) {
|
||||
return std::unexpected{std::move(valid).error()};
|
||||
}
|
||||
if (auto valid = validate_request(request); !valid) {
|
||||
return std::unexpected{std::move(valid).error()};
|
||||
}
|
||||
|
||||
std::lock_guard lock{m_mutex};
|
||||
if (const auto replayed = replay(request)) {
|
||||
return *replayed;
|
||||
}
|
||||
|
||||
const auto found = m_entries.find(key);
|
||||
if (found == m_entries.end()) {
|
||||
WriteResult result{WriteOutcome::NotFound, 0};
|
||||
remember(request, result);
|
||||
return result;
|
||||
}
|
||||
const std::optional<Entry> current = found->second;
|
||||
if (!matches(current, precondition)) {
|
||||
WriteResult result{WriteOutcome::Conflict, current->version};
|
||||
remember(request, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
m_entries.erase(found);
|
||||
WriteResult result{WriteOutcome::Applied, 0};
|
||||
remember(request, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
Void::Result::Result<ScanPage>
|
||||
MemoryKvStore::scan(std::optional<std::string_view> cursor,
|
||||
std::size_t limit) const
|
||||
{
|
||||
const std::size_t page =
|
||||
std::min(limit == 0 ? m_limits.max_scan_page : limit,
|
||||
m_limits.max_scan_page);
|
||||
|
||||
std::lock_guard lock{m_mutex};
|
||||
ScanPage out;
|
||||
auto it = cursor ? m_entries.upper_bound(*cursor) : m_entries.begin();
|
||||
while (it != m_entries.end() && out.items.size() < page) {
|
||||
out.items.push_back(ScanItem{it->first, it->second});
|
||||
++it;
|
||||
}
|
||||
if (it != m_entries.end() && !out.items.empty()) {
|
||||
out.next_cursor = out.items.back().key;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
} // namespace NetworkedKv
|
||||
@@ -0,0 +1,130 @@
|
||||
#include "networked_kv/domain.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
using namespace NetworkedKv;
|
||||
|
||||
void versioned_lifecycle()
|
||||
{
|
||||
MemoryKvStore store;
|
||||
auto created = store.put("a", "one", Precondition::absent(), {"r1"});
|
||||
assert(created.has_value());
|
||||
assert(created->outcome == WriteOutcome::Applied);
|
||||
assert(created->version == 1);
|
||||
|
||||
auto got = store.get("a");
|
||||
assert(got.has_value() && got->has_value());
|
||||
assert((*got)->value == "one");
|
||||
|
||||
// Absent precondition no longer holds.
|
||||
auto conflict = store.put("a", "two", Precondition::absent(), {"r2"});
|
||||
assert(conflict.has_value());
|
||||
assert(conflict->outcome == WriteOutcome::Conflict);
|
||||
|
||||
auto updated =
|
||||
store.put("a", "two", Precondition::at_version(1), {"r3"});
|
||||
assert(updated.has_value());
|
||||
assert(updated->outcome == WriteOutcome::Applied);
|
||||
assert(updated->version == 2);
|
||||
|
||||
// Replaying the same request id returns the recorded outcome without
|
||||
// applying the mutation again.
|
||||
auto replay =
|
||||
store.put("a", "ignored", Precondition::at_version(2), {"r3"});
|
||||
assert(replay.has_value());
|
||||
assert(replay->outcome == WriteOutcome::Applied);
|
||||
assert(replay->version == 2);
|
||||
auto current = store.get("a");
|
||||
assert(current.has_value() && current->has_value());
|
||||
assert((*current)->value == "two");
|
||||
assert((*current)->version == 2);
|
||||
|
||||
auto stale = store.erase("a", Precondition::at_version(1), {"r4"});
|
||||
assert(stale.has_value());
|
||||
assert(stale->outcome == WriteOutcome::Conflict);
|
||||
|
||||
auto erased = store.erase("a", Precondition::at_version(2), {"r5"});
|
||||
assert(erased.has_value());
|
||||
assert(erased->outcome == WriteOutcome::Applied);
|
||||
assert(!store.get("a")->has_value());
|
||||
|
||||
auto missing = store.erase("missing", Precondition::absent(), {"r6"});
|
||||
assert(missing.has_value());
|
||||
assert(missing->outcome == WriteOutcome::NotFound);
|
||||
}
|
||||
|
||||
void input_validation()
|
||||
{
|
||||
MemoryKvStore store;
|
||||
assert(!store.put("", "x", Precondition::absent(), {"r"}).has_value());
|
||||
assert(!store.put("k", "x", Precondition::absent(), {""}).has_value());
|
||||
|
||||
MemoryKvStore small{Limits{.max_key_bytes = 2, .max_value_bytes = 2}};
|
||||
assert(!small.put("key", "x", Precondition::absent(), {"r"}).has_value());
|
||||
assert(!small.put("k", "long", Precondition::absent(), {"r"}).has_value());
|
||||
}
|
||||
|
||||
void bounded_scan()
|
||||
{
|
||||
MemoryKvStore store{Limits{.max_scan_page = 2}};
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
const std::string key(1, static_cast<char>('a' + i));
|
||||
auto written = store.put(key, "v", Precondition::absent(),
|
||||
{"s" + std::to_string(i)});
|
||||
assert(written.has_value());
|
||||
}
|
||||
|
||||
std::vector<std::string> keys;
|
||||
std::optional<std::string> cursor;
|
||||
for (;;) {
|
||||
auto page = store.scan(
|
||||
cursor ? std::optional<std::string_view>{*cursor} : std::nullopt);
|
||||
assert(page.has_value());
|
||||
assert(page->items.size() <= 2);
|
||||
for (const auto& item : page->items) {
|
||||
keys.push_back(item.key);
|
||||
}
|
||||
if (!page->next_cursor) {
|
||||
break;
|
||||
}
|
||||
cursor = page->next_cursor;
|
||||
}
|
||||
assert(keys.size() == 5);
|
||||
assert(keys.front() == "a");
|
||||
assert(keys.back() == "e");
|
||||
}
|
||||
|
||||
void bounded_operation_records()
|
||||
{
|
||||
MemoryKvStore store{Limits{.max_operation_records = 2}};
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
const auto precondition = i == 0 ? Precondition::absent()
|
||||
: Precondition::at_version(
|
||||
static_cast<Version>(i));
|
||||
auto written = store.put("k", "v" + std::to_string(i), precondition,
|
||||
{"b" + std::to_string(i)});
|
||||
assert(written.has_value());
|
||||
assert(written->outcome == WriteOutcome::Applied);
|
||||
}
|
||||
// The store keeps working after evicting old operation records.
|
||||
auto got = store.get("k");
|
||||
assert(got.has_value() && got->has_value());
|
||||
assert((*got)->version == 5);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
versioned_lifecycle();
|
||||
input_validation();
|
||||
bounded_scan();
|
||||
bounded_operation_records();
|
||||
std::puts("networked-kv domain ok");
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user