seqeron 0.18.0
Loading...
Searching...
No Matches
Snapshot.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <array>
5#include <cstddef>
6#include <cstdint>
7#include <cstring>
8#include <optional>
9#include <string>
10#include <vector>
11
12#if defined(__x86_64__)
13#include <nmmintrin.h>
14#elif defined(__aarch64__)
15#include <arm_acle.h>
16#if defined(__linux__)
17#include <asm/hwcap.h>
18#include <sys/auxv.h>
19#endif
20#endif
21
22// The snapshot format (doc/snapshot.md §2, §6): a sequence of records, the façade's header first, and the
23// CRC-32C over their bytes in order. The Java twins are protocol/SnapshotFormat and SnapshotHeader; keep them in
24// step.
25
27
28// The most bytes one record holds: what its uint16 length prefix in the file can say.
29inline constexpr std::uint16_t MAX_SNAPSHOT_RECORD_LENGTH = 65535;
30
31namespace detail {
32
33inline constexpr std::array<std::uint32_t, 256> CRC32C_TABLE = [] {
34 std::array<std::uint32_t, 256> table{};
35 for (std::uint32_t i = 0; i < 256; ++i)
36 {
37 std::uint32_t c = i;
38 for (int bit = 0; bit < 8; ++bit)
39 {
40 c = (c >> 1) ^ ((c & 1U) != 0 ? 0x82F63B78U : 0U);
41 }
42 table[i] = c;
43 }
44 return table;
45}();
46
47inline void putLe(std::uint8_t* out, std::uint64_t value, const std::size_t bytes)
48{
49 for (std::size_t i = 0; i < bytes; ++i)
50 {
51 out[i] = static_cast<std::uint8_t>(value);
52 value >>= 8;
53 }
54}
55
56inline std::uint64_t getLe(const std::uint8_t* in, const std::size_t bytes)
57{
58 std::uint64_t value = 0;
59 for (std::size_t i = bytes; i > 0; --i)
60 {
61 value = (value << 8) | in[i - 1];
62 }
63 return value;
64}
65
75inline std::uint32_t crc32cTable(const std::uint8_t* data, const std::size_t length, const std::uint32_t crc)
76{
77 std::uint32_t c = ~crc;
78 for (std::size_t i = 0; i < length; ++i)
79 {
80 c = CRC32C_TABLE[(c ^ data[i]) & 0xFFU] ^ (c >> 8);
81 }
82 return ~c;
83}
84
85// The CRC-32C instruction, compiled for its own function by a target attribute so a consumer needs no compiler
86// flag, and called only once hasCrc32cInstruction() has said the CPU has it.
87#if defined(__x86_64__)
88
89inline bool hasCrc32cInstruction()
90{
91 return __builtin_cpu_supports("sse4.2");
92}
93
102__attribute__((target("sse4.2"))) inline std::uint32_t crc32cHardware(const std::uint8_t* data, std::size_t length,
103 const std::uint32_t crc)
104{
105 std::uint64_t c = ~crc;
106 for (; length >= sizeof(std::uint64_t); length -= sizeof(std::uint64_t), data += sizeof(std::uint64_t))
107 {
108 std::uint64_t word;
109 std::memcpy(&word, data, sizeof(word));
110 c = _mm_crc32_u64(c, word);
111 }
112 auto c32 = static_cast<std::uint32_t>(c);
113 for (; length > 0; --length, ++data)
114 {
115 c32 = _mm_crc32_u8(c32, *data);
116 }
117 return ~c32;
118}
119
120#elif defined(__aarch64__)
121
122inline bool hasCrc32cInstruction()
123{
124#if defined(__ARM_FEATURE_CRC32) || defined(__APPLE__)
125 return true;
126#elif defined(__linux__)
127 return (getauxval(AT_HWCAP) & HWCAP_CRC32) != 0;
128#else
129 return false;
130#endif
131}
132
141__attribute__((target("+crc"))) inline std::uint32_t crc32cHardware(const std::uint8_t* data, std::size_t length,
142 const std::uint32_t crc)
143{
144 std::uint32_t c = ~crc;
145 for (; length >= sizeof(std::uint64_t); length -= sizeof(std::uint64_t), data += sizeof(std::uint64_t))
146 {
147 std::uint64_t word;
148 std::memcpy(&word, data, sizeof(word));
149 c = __crc32cd(c, word);
150 }
151 for (; length > 0; --length, ++data)
152 {
153 c = __crc32cb(c, *data);
154 }
155 return ~c;
156}
157
158#else
159
160inline bool hasCrc32cInstruction()
161{
162 return false;
163}
164
165inline std::uint32_t crc32cHardware(const std::uint8_t* data, const std::size_t length, const std::uint32_t crc)
166{
167 return crc32cTable(data, length, crc);
168}
169
170#endif
171
172} // namespace detail
173
183inline std::uint32_t crc32c(const std::uint8_t* data, const std::size_t length, const std::uint32_t crc = 0)
184{
185 static const bool hardware = detail::hasCrc32cInstruction();
186 return hardware ? detail::crc32cHardware(data, length, crc) : detail::crc32cTable(data, length, crc);
187}
188
189// One active group, in a snapshot header.
191{
192 std::int32_t groupId;
193 std::string groupName;
194
195 bool operator==(const SnapshotGroup&) const = default;
196};
197
198// One instance's list row, in a gateway's snapshot header.
200{
201 std::int32_t gatewayId;
202 std::uint8_t preferenceRank;
203 std::string gatewayName;
204
205 bool operator==(const SnapshotGatewayRow&) const = default;
206};
207
208// A gateway's continuation of the snapshot header.
210{
211 std::int32_t gatewaySourceId;
212 std::int32_t activeGatewayId;
213 std::int32_t highestConnectionId;
214 std::vector<SnapshotGatewayRow> rows;
215
216 bool operator==(const SnapshotGatewayState&) const = default;
217};
218
225{
226 static constexpr std::uint16_t VERSION = 2;
227 static constexpr std::int32_t NO_GATEWAY = -1;
228 static constexpr std::size_t FIXED_LENGTH = 19;
229 static constexpr std::size_t NAME_LENGTH = 32;
230 static constexpr std::size_t GROUP_LENGTH = 1 + NAME_LENGTH;
231 static constexpr std::size_t GATEWAY_FIXED_LENGTH = 14;
232 static constexpr std::size_t GATEWAY_ROW_LENGTH = 5 + NAME_LENGTH;
233
234 std::int64_t leadershipTermId;
235 std::int32_t leaderMemberId;
236 std::vector<SnapshotGroup> groups;
237 std::optional<SnapshotGatewayState> gateway;
238
239 bool operator==(const SnapshotHeader&) const = default;
240
241 // Bytes encode writes; the value of headerLength.
242 std::size_t encodedLength() const
243 {
244 const std::size_t applicationLength = FIXED_LENGTH + GROUP_LENGTH * groups.size();
245 return gateway ? applicationLength + GATEWAY_FIXED_LENGTH + GATEWAY_ROW_LENGTH * gateway->rows.size()
246 : applicationLength;
247 }
248
255 std::size_t encode(std::uint8_t* out) const
256 {
257 const std::size_t length = encodedLength();
258 detail::putLe(out, VERSION, 2);
259 detail::putLe(out + 2, length, 4);
260 detail::putLe(out + 6, static_cast<std::uint64_t>(leadershipTermId), 8);
261 detail::putLe(out + 14, static_cast<std::uint32_t>(leaderMemberId), 4);
262 out[18] = static_cast<std::uint8_t>(groups.size());
263 std::uint8_t* group = out + FIXED_LENGTH;
264 for (const SnapshotGroup& each : groups)
265 {
266 group[0] = static_cast<std::uint8_t>(each.groupId);
267 putName(group + 1, each.groupName);
268 group += GROUP_LENGTH;
269 }
270 if (gateway)
271 {
272 detail::putLe(group, static_cast<std::uint32_t>(gateway->gatewaySourceId), 4);
273 detail::putLe(group + 4, static_cast<std::uint32_t>(gateway->activeGatewayId), 4);
274 detail::putLe(group + 8, static_cast<std::uint32_t>(gateway->highestConnectionId), 4);
275 detail::putLe(group + 12, gateway->rows.size(), 2);
276 std::uint8_t* row = group + GATEWAY_FIXED_LENGTH;
277 for (const SnapshotGatewayRow& each : gateway->rows)
278 {
279 detail::putLe(row, static_cast<std::uint32_t>(each.gatewayId), 4);
280 row[4] = each.preferenceRank;
281 putName(row + 5, each.gatewayName);
282 row += GATEWAY_ROW_LENGTH;
283 }
284 }
285 return length;
286 }
287
295 static std::int32_t version(const std::uint8_t* in, const std::size_t length)
296 {
297 return length < 2 ? -1 : static_cast<std::int32_t>(detail::getLe(in, 2));
298 }
299
307 static std::optional<SnapshotHeader> decode(const std::uint8_t* in, const std::size_t length)
308 {
309 if (length < FIXED_LENGTH || version(in, length) != VERSION)
310 {
311 return std::nullopt;
312 }
313 const std::uint64_t headerLength = detail::getLe(in + 2, 4);
314 const std::size_t groupCount = in[18];
315 const std::size_t applicationLength = FIXED_LENGTH + GROUP_LENGTH * groupCount;
316 if (headerLength > length || headerLength < applicationLength)
317 {
318 return std::nullopt;
319 }
320 SnapshotHeader header{ static_cast<std::int64_t>(detail::getLe(in + 6, 8)),
321 static_cast<std::int32_t>(detail::getLe(in + 14, 4)),
322 {},
323 std::nullopt };
324 const std::uint8_t* group = in + FIXED_LENGTH;
325 for (std::size_t i = 0; i < groupCount; ++i)
326 {
327 header.groups.push_back(SnapshotGroup{ group[0], getName(group + 1) });
328 group += GROUP_LENGTH;
329 }
330 if (headerLength == applicationLength)
331 {
332 return header;
333 }
334 if (headerLength < applicationLength + GATEWAY_FIXED_LENGTH)
335 {
336 return std::nullopt;
337 }
338 const std::size_t rowCount = detail::getLe(group + 12, 2);
339 if (headerLength != applicationLength + GATEWAY_FIXED_LENGTH + GATEWAY_ROW_LENGTH * rowCount)
340 {
341 return std::nullopt;
342 }
343 SnapshotGatewayState state{ static_cast<std::int32_t>(detail::getLe(group, 4)),
344 static_cast<std::int32_t>(detail::getLe(group + 4, 4)),
345 static_cast<std::int32_t>(detail::getLe(group + 8, 4)),
346 {} };
347 const std::uint8_t* row = group + GATEWAY_FIXED_LENGTH;
348 for (std::size_t i = 0; i < rowCount; ++i)
349 {
350 state.rows.push_back(
351 SnapshotGatewayRow{ static_cast<std::int32_t>(detail::getLe(row, 4)), row[4], getName(row + 5) });
352 row += GATEWAY_ROW_LENGTH;
353 }
354 header.gateway = std::move(state);
355 return header;
356 }
357
358 private:
359 static void putName(std::uint8_t* out, const std::string& name)
360 {
361 const std::size_t nameLength = std::min(name.size(), NAME_LENGTH);
362 std::copy_n(name.data(), nameLength, out);
363 std::fill_n(out + nameLength, NAME_LENGTH - nameLength, std::uint8_t{ 0 });
364 }
365
366 static std::string getName(const std::uint8_t* in)
367 {
368 const auto* name = reinterpret_cast<const char*>(in);
369 return std::string(name, std::find(name, name + NAME_LENGTH, '\0') - name);
370 }
371};
372
373} // namespace org::limitless::seqeron::protocol
Definition ProxyProtocol.hpp:8
std::uint32_t crc32c(const std::uint8_t *data, const std::size_t length, const std::uint32_t crc=0)
Computes the CRC-32C of a span of bytes, as SnapshotEnd::crc32c carries it, or continues one: with th...
Definition Snapshot.hpp:183
constexpr std::uint16_t MAX_SNAPSHOT_RECORD_LENGTH
Definition Snapshot.hpp:29
std::string gatewayName
at most 32 US-ASCII characters
Definition Snapshot.hpp:203
bool operator==(const SnapshotGatewayRow &) const =default
std::int32_t gatewayId
Definition Snapshot.hpp:201
std::uint8_t preferenceRank
Definition Snapshot.hpp:202
std::vector< SnapshotGatewayRow > rows
every instance of the gateway, in list order
Definition Snapshot.hpp:214
std::int32_t gatewaySourceId
Definition Snapshot.hpp:211
bool operator==(const SnapshotGatewayState &) const =default
std::int32_t activeGatewayId
the current GatewayActive's, or SnapshotHeader::NO_GATEWAY
Definition Snapshot.hpp:212
std::int32_t highestConnectionId
the highest in the source's history
Definition Snapshot.hpp:213
std::int32_t groupId
1 to MAX_GROUPS
Definition Snapshot.hpp:192
bool operator==(const SnapshotGroup &) const =default
std::string groupName
at most 32 US-ASCII characters
Definition Snapshot.hpp:193
The header a façade writes ahead of the application's body: what it derives from frames before the ro...
Definition Snapshot.hpp:225
std::int64_t leadershipTermId
Definition Snapshot.hpp:234
static constexpr std::uint16_t VERSION
Definition Snapshot.hpp:226
static std::int32_t version(const std::uint8_t *in, const std::size_t length)
Reads the headerVersion a snapshot starts with.
Definition Snapshot.hpp:295
std::vector< SnapshotGroup > groups
the active groups before the cut, in ascending groupId
Definition Snapshot.hpp:236
bool operator==(const SnapshotHeader &) const =default
static constexpr std::size_t GATEWAY_ROW_LENGTH
Definition Snapshot.hpp:232
std::optional< SnapshotGatewayState > gateway
empty for an application's header
Definition Snapshot.hpp:237
static constexpr std::size_t FIXED_LENGTH
Definition Snapshot.hpp:228
std::int32_t leaderMemberId
Definition Snapshot.hpp:235
std::size_t encode(std::uint8_t *out) const
Writes the header.
Definition Snapshot.hpp:255
static constexpr std::size_t GROUP_LENGTH
Definition Snapshot.hpp:230
std::size_t encodedLength() const
Definition Snapshot.hpp:242
static std::optional< SnapshotHeader > decode(const std::uint8_t *in, const std::size_t length)
Reads a header written by encode.
Definition Snapshot.hpp:307
static constexpr std::size_t NAME_LENGTH
Definition Snapshot.hpp:229
static constexpr std::size_t GATEWAY_FIXED_LENGTH
Definition Snapshot.hpp:231
static constexpr std::int32_t NO_GATEWAY
Definition Snapshot.hpp:227