-
Notifications
You must be signed in to change notification settings - Fork 70
/
common.h
123 lines (106 loc) · 4.63 KB
/
common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#pragma once
#include <switch.h>
#include <ext/martinus/robin_hood.h>
#include <cassert>
#define TANK_RUNTIME_CHECKS 1
#ifdef TANK_RUNTIME_CHECKS
//#define TANK_EXPECT(...) EXPECT(__VA_ARGS__)
#define TANK_EXPECT(...) \
do { \
assert(__VA_ARGS__); \
} while (0)
#define TANK_NOEXCEPT_IF_NORUNTIME_CHECKS
#else
#define TANK_EXPECT(...) \
do { \
} while (0)
#define TANK_NOEXCEPT_IF_NORUNTIME_CHECKS noexcept
#define TANK_THROW_SWITCH_EXCEPTIONS 1
#endif
#define MAKE_TANK_RELEASE(major, minor) ((major)*100 + (minor))
#define TANK_VERSION (MAKE_TANK_RELEASE(4, 0))
// 2022-08-18: PARTITION_PROVIDER
// This is tricky, but very important for certain applications. If enabled, consume requests may include flags, and one such flag
// may be used to specify that, in cluster-aware setups, the closest to the client replica for a topic partition consume request(among the ones in its ISR)
// should be used instead of the leader.
//
// This is not sufficiently tested yet
#define TANK_SUPPORT_CONSUME_FLAGS 1
namespace TankFlags {
enum class BundleMsgFlags : uint8_t {
HaveKey = 1,
UseLastSpecifiedTS = 2,
SeqNumPrevPlusOne = 4
};
}
namespace TANK_Limits {
static constexpr const std::size_t max_topic_partitions{65530};
static constexpr const std::size_t max_topic_name_len{64};
} // namespace TANK_Limits
enum class ConsumeFlags : uint8_t {
// if set, and there is a TANK instance running locally(i.e the endpoint IP4 address
// matches the client's IP4 address, and that instance is in the ISR of the partition, then that instance will be used("provider")
prefer_local_node = 1u << 0,
};
enum class TankAPIMsgType : uint8_t {
Produce = 0x1,
Consume = 0x2,
Ping = 0x3,
__obsolete_RegReplica = 0x4,
// Same as Produce, except that the seq.num of the first msg of the first bundle is going to
// be explicitly encoded in the request.
//
// This going to be used by tank-cli mirroring functionality (and maybe for replication), where
// for example older segments may have been deleted and we are expected to consume from a node and produce to another, except
// that if the other is starting off with no data, it will logically assign (1) as the first message of the bundle, not
// whatever the origin had when we consumed from it.
//
// For example, if the earliest available segment for a partition in origin has a baseSeqNum (1000) (i.e not 1), and we consume from it
// in order to mirror to another node, and that node has no data for that partition, it will create a new segment with baseSeqNum(0), not 1.
ProduceWithSeqnum = 0x5,
DiscoverPartitions = 0x6,
CreateTopic = 0x7,
ReloadConf = 0x8,
ConsumePeer = 0x9,
Status = 10,
DiscoverTopics = 0xb,
DiscoverTopology = 0xc,
};
namespace TANKUtil {
template <typename T>
inline T minimum(const T &t) noexcept {
return t;
}
template <typename T, typename... P>
inline auto minimum(const T &t, const P &...p) noexcept {
using res_type = std::common_type_t<T, P...>;
return std::min(res_type(t), res_type(minimum(p...)));
}
// TODO: figure out a better name
namespace produce_request_acks {
// Require ack from all nodes in ISR
inline uint8_t ISR() noexcept {
return 0;
}
// Require ack from (all nodes in ISR / 2 + 1)
inline uint8_t ISR_quorum() noexcept {
return 255;
}
inline uint8_t value(const uint8_t v) {
TANK_EXPECT(v != ISR());
TANK_EXPECT(v != ISR_quorum());
return v;
}
} // namespace produce_request_acks
// we need this to guard agains race condition which stem from our deferred _now updates
inline constexpr uint32_t time32_delta(const time32_t start, const time32_t end) {
return end >= start ? end - start : 0;
}
inline void safe_close(int fd) {
TANK_EXPECT(fd > 2);
close(fd);
}
// saniy check
static_assert(time32_delta(0, 10) == 10);
static_assert(time32_delta(11, 10) == 0);
} // namespace TANKUtil