-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathservice_cluster_util.cpp
105 lines (82 loc) · 2.81 KB
/
service_cluster_util.cpp
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
#include "service_common.h"
uint8_t topic::replication_factor() const noexcept {
return cluster.rf_;
}
bool cluster_node::is_replica_for(const topic_partition *p) const TANK_NOEXCEPT_IF_NORUNTIME_CHECKS {
// this works because we always std::sort() whenever replica_for is dirty
// this is sorted by idx
const auto all = replica_for.data();
TANK_EXPECT(std::is_sorted(replica_for.begin(), replica_for.end()));
for (int32_t top = static_cast<int32_t>(replica_for.size()) - 1, btm = 0; btm <= top;) {
const auto mid = (btm + top) / 2;
const auto at_mid = all[mid];
if (p < at_mid) {
top = mid - 1;
} else if (p > at_mid) {
btm = mid + 1;
} else {
return true;
}
}
return false;
}
bool topic_partition::require_leader() const noexcept {
const auto topic = owner;
if (not topic->enabled) {
return false;
}
if (not enabled()) {
return false;
}
if (0 == topic->cluster.rf_) {
return false;
}
return true;
}
// Recall that this is the number of _copies_ required
// not the number of replicas other than the leader that need to replicate from that node
//
// i.e this is the replication factor
uint16_t topic_partition::required_replicas() const noexcept {
const auto topic = owner;
if (not topic->enabled) {
return 0;
}
if (not enabled()) {
return 0;
}
return topic->cluster.rf_;
}
bool Service::should_manage_topic_partition_replicas(const topic *t) const noexcept {
if (!cluster_aware() || !cluster_state.leader_self() || !t->enabled || !t->partitions_ || t->partitions_->empty()) {
return false;
}
return true;
}
bool Service::is_valid_topic_name(const str_view8 n) noexcept {
if (not n or n.size() > 64) {
return false;
}
bool any{false};
for (const auto c : n) {
switch (c) {
case 'a' ... 'z':
case 'A' ... 'Z':
case '0' ... '9':
any = true;
break;
case '-':
case '_':
break;
default:
return false;
}
}
if (const auto c = n.front(); c == '-' or c == '_') {
return false;
}
if (const auto c = n.back(); c == '-' or c == '_') {
return false;
}
return any;
}