RPC: Wire version negotiation and envelope migration (S-RPC-05)
This commit is contained in:
1 parent
2c026ea30c
commit
9ab482dc5d
3 files changed
+144
No files matched your search
@@ -0,0 +1,45 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Void/Functional/Result.hpp>
|
||||||
|
#include <Void/RPC/Codec.hpp>
|
||||||
|
#include <Void/RPC/Types.hpp>
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
namespace Void::RPC
|
||||||
|
{
|
||||||
|
struct WireVersion {
|
||||||
|
std::uint16_t major{kWireMajorV1};
|
||||||
|
std::uint16_t minor{kWireMinorV1};
|
||||||
|
|
||||||
|
[[nodiscard]] bool operator==(const WireVersion&) const noexcept = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline constexpr WireVersion kCurrentWireVersion{kWireMajorV1,
|
||||||
|
kWireMinorV1};
|
||||||
|
|
||||||
|
enum class WireVersionCompatibility : std::uint8_t {
|
||||||
|
Same,
|
||||||
|
/// The peer speaks a newer minor: unknown optional extensions are
|
||||||
|
/// length-skipped, so the exchange stays compatible.
|
||||||
|
PeerNewerMinor,
|
||||||
|
/// The peer speaks an older minor: omit fields it cannot know.
|
||||||
|
PeerOlderMinor,
|
||||||
|
/// Different major: the wire formats are not interoperable.
|
||||||
|
IncompatibleMajor
|
||||||
|
};
|
||||||
|
|
||||||
|
[[nodiscard]] WireVersionCompatibility
|
||||||
|
negotiate_wire_version(WireVersion peer,
|
||||||
|
WireVersion local = kCurrentWireVersion) noexcept;
|
||||||
|
|
||||||
|
/// Migrate a decoded envelope to the local wire version.
|
||||||
|
///
|
||||||
|
/// Compatible minors are accepted (the envelope's fields are already
|
||||||
|
/// interpreted by the decoder); a different major fails with
|
||||||
|
/// RpcStatusCode::IncompatibleVersion. The returned envelope reports the
|
||||||
|
/// local version so later stages never see a foreign minor.
|
||||||
|
[[nodiscard]] Void::Result::Result<WireEnvelope>
|
||||||
|
migrate_envelope(WireEnvelope envelope,
|
||||||
|
WireVersion local = kCurrentWireVersion);
|
||||||
|
} // namespace Void::RPC
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
#include <Void/RPC/Version.hpp>
|
||||||
|
|
||||||
|
namespace Void::RPC
|
||||||
|
{
|
||||||
|
WireVersionCompatibility
|
||||||
|
negotiate_wire_version(WireVersion peer, WireVersion local) noexcept
|
||||||
|
{
|
||||||
|
if (peer.major != local.major) {
|
||||||
|
return WireVersionCompatibility::IncompatibleMajor;
|
||||||
|
}
|
||||||
|
if (peer.minor == local.minor) {
|
||||||
|
return WireVersionCompatibility::Same;
|
||||||
|
}
|
||||||
|
return peer.minor > local.minor
|
||||||
|
? WireVersionCompatibility::PeerNewerMinor
|
||||||
|
: WireVersionCompatibility::PeerOlderMinor;
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Result::Result<WireEnvelope>
|
||||||
|
migrate_envelope(WireEnvelope envelope, WireVersion local)
|
||||||
|
{
|
||||||
|
const WireVersion peer{envelope.major, envelope.minor};
|
||||||
|
if (negotiate_wire_version(peer, local) ==
|
||||||
|
WireVersionCompatibility::IncompatibleMajor) {
|
||||||
|
return Void::Result::fail(make_rpc_error(
|
||||||
|
RpcStatusCode::IncompatibleVersion,
|
||||||
|
"RPC: wire major version is not interoperable"));
|
||||||
|
}
|
||||||
|
// Minor differences are forward/backward compatible; normalize so
|
||||||
|
// later stages always see the local version.
|
||||||
|
envelope.major = local.major;
|
||||||
|
envelope.minor = local.minor;
|
||||||
|
return envelope;
|
||||||
|
}
|
||||||
|
} // namespace Void::RPC
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
#include <Void/RPC/Version.hpp>
|
||||||
|
#include <Void/Testing/Assert.hpp>
|
||||||
|
#include <Void/Testing/Test.hpp>
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
using namespace Void::RPC;
|
||||||
|
using Compatibility = WireVersionCompatibility;
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
Void::Testing::Test rpc_wire_version_negotiation()
|
||||||
|
{
|
||||||
|
Void::Testing::expect(negotiate_wire_version({1, 0}) ==
|
||||||
|
Compatibility::Same);
|
||||||
|
Void::Testing::expect(negotiate_wire_version({1, 1}) ==
|
||||||
|
Compatibility::PeerNewerMinor);
|
||||||
|
Void::Testing::expect(negotiate_wire_version({1, 0}, {1, 2}) ==
|
||||||
|
Compatibility::PeerOlderMinor);
|
||||||
|
Void::Testing::expect(negotiate_wire_version({2, 0}) ==
|
||||||
|
Compatibility::IncompatibleMajor);
|
||||||
|
Void::Testing::expect(negotiate_wire_version({0, 9}) ==
|
||||||
|
Compatibility::IncompatibleMajor);
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Testing::Test rpc_wire_envelope_migration()
|
||||||
|
{
|
||||||
|
// A newer minor round-trips through the codec and is normalized.
|
||||||
|
WireEnvelope newer;
|
||||||
|
newer.minor = 5;
|
||||||
|
newer.kind = FrameKind::UnaryRequest;
|
||||||
|
newer.payload = {std::byte{0x42}};
|
||||||
|
auto encoded = encode_envelope(newer);
|
||||||
|
Void::Testing::require(encoded.has_value());
|
||||||
|
auto decoded = decode_envelope(*encoded);
|
||||||
|
Void::Testing::require(decoded.has_value());
|
||||||
|
Void::Testing::expect_eq(decoded->minor, std::uint16_t{5});
|
||||||
|
|
||||||
|
auto migrated = migrate_envelope(*decoded);
|
||||||
|
Void::Testing::require(migrated.has_value());
|
||||||
|
Void::Testing::expect_eq(migrated->major, kWireMajorV1);
|
||||||
|
Void::Testing::expect_eq(migrated->minor, kWireMinorV1);
|
||||||
|
Void::Testing::expect(migrated->kind == FrameKind::UnaryRequest);
|
||||||
|
Void::Testing::expect_eq(migrated->payload.size(), std::size_t{1});
|
||||||
|
|
||||||
|
// An older minor is also accepted.
|
||||||
|
WireEnvelope older;
|
||||||
|
older.major = 1;
|
||||||
|
older.minor = 0;
|
||||||
|
auto older_migrated = migrate_envelope(older, {1, 3});
|
||||||
|
Void::Testing::require(older_migrated.has_value());
|
||||||
|
Void::Testing::expect_eq(older_migrated->minor, std::uint16_t{3});
|
||||||
|
|
||||||
|
// A different major is rejected with IncompatibleVersion.
|
||||||
|
WireEnvelope incompatible;
|
||||||
|
incompatible.major = 2;
|
||||||
|
auto rejected = migrate_envelope(incompatible);
|
||||||
|
Void::Testing::require(!rejected.has_value());
|
||||||
|
Void::Testing::expect_eq(
|
||||||
|
rejected.error().code().value,
|
||||||
|
static_cast<std::int32_t>(RpcStatusCode::IncompatibleVersion));
|
||||||
|
}
|
||||||
Reference in new issue
Block a user