-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathAutoClusterFailover.h
More file actions
165 lines (142 loc) · 6.61 KB
/
Copy pathAutoClusterFailover.h
File metadata and controls
165 lines (142 loc) · 6.61 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#ifndef PULSAR_AUTO_CLUSTER_FAILOVER_H_
#define PULSAR_AUTO_CLUSTER_FAILOVER_H_
#include <pulsar/ServiceInfoProvider.h>
#include <algorithm>
#include <chrono>
#include <cstdint>
#include <functional>
#include <memory>
#include <vector>
namespace pulsar {
class Client;
class AutoClusterFailoverImpl;
class PULSAR_PUBLIC AutoClusterFailover final : public ServiceInfoProvider {
public:
struct Config {
const ServiceInfo primary;
const std::vector<ServiceInfo> secondary;
std::chrono::milliseconds checkInterval{30000}; // 30 seconds
std::chrono::milliseconds failoverDelay{30000}; // 30 seconds
std::chrono::milliseconds switchBackDelay{60000}; // 60 seconds
uint32_t failoverThreshold{1};
uint32_t switchBackThreshold{2};
Config(ServiceInfo primary, std::vector<ServiceInfo> secondary)
: primary(std::move(primary)), secondary(std::move(secondary)) {}
};
/**
* Builder helps create an AutoClusterFailover configuration.
*
* Example:
* ServiceInfo primary{...};
* std::vector<ServiceInfo> secondaries{...};
* AutoClusterFailover provider = AutoClusterFailover::Builder(primary, secondaries)
* .withCheckInterval(std::chrono::seconds(5))
* .withFailoverDelay(std::chrono::seconds(30))
* .withSwitchBackDelay(std::chrono::seconds(60))
* .build();
*
* Notes:
* - primary: the preferred cluster to use when available.
* - secondary: ordered list of fallback clusters.
* - checkInterval: frequency of health probes.
* - failoverDelay: how long the current cluster must remain unavailable before switching away.
* - switchBackDelay: how long the primary must remain healthy before switching back from a secondary.
* If the active secondary becomes unavailable and the primary is available, the implementation may
* switch back to the primary immediately, regardless of this delay.
*/
class Builder {
public:
Builder(ServiceInfo primary, std::vector<ServiceInfo> secondary)
: config_(std::move(primary), std::move(secondary)) {}
// Set how frequently probes run against the active cluster(s). Default: 30 seconds.
Builder& withCheckInterval(std::chrono::milliseconds interval) {
config_.checkInterval = interval;
updateThresholdsFromDelays();
return *this;
}
// Set how long the current cluster must remain unavailable before switching away. Default: 30
// seconds.
Builder& withFailoverDelay(std::chrono::milliseconds delay) {
failoverThresholdConfigured_ = false;
config_.failoverDelay = delay;
config_.failoverThreshold = calculateThreshold(config_.failoverDelay, config_.checkInterval);
return *this;
}
// Set how long the primary must remain healthy before switching back from a secondary. Default: 60
// seconds.
Builder& withSwitchBackDelay(std::chrono::milliseconds delay) {
switchBackThresholdConfigured_ = false;
config_.switchBackDelay = delay;
config_.switchBackThreshold = calculateThreshold(config_.switchBackDelay, config_.checkInterval);
return *this;
}
// Set the number of consecutive failed probes required before attempting failover.
Builder& withFailoverThreshold(uint32_t threshold) {
failoverThresholdConfigured_ = true;
config_.failoverThreshold = threshold;
config_.failoverDelay = config_.checkInterval * threshold;
return *this;
}
// Set the number of consecutive successful primary probes required before switching back from a
// healthy secondary. If the active secondary becomes unavailable and the primary is available,
// the implementation may switch back immediately regardless of this threshold.
Builder& withSwitchBackThreshold(uint32_t threshold) {
switchBackThresholdConfigured_ = true;
config_.switchBackThreshold = threshold;
config_.switchBackDelay = config_.checkInterval * threshold;
return *this;
}
AutoClusterFailover build() { return AutoClusterFailover(std::move(config_)); }
private:
static uint32_t calculateThreshold(std::chrono::milliseconds delay,
std::chrono::milliseconds interval) {
if (delay <= std::chrono::milliseconds::zero() || interval <= std::chrono::milliseconds::zero()) {
return 1;
}
return static_cast<uint32_t>(
std::max<int64_t>(1, (delay.count() + interval.count() - 1) / interval.count()));
}
void updateThresholdsFromDelays() {
if (failoverThresholdConfigured_) {
config_.failoverDelay = config_.checkInterval * config_.failoverThreshold;
} else {
config_.failoverThreshold = calculateThreshold(config_.failoverDelay, config_.checkInterval);
}
if (switchBackThresholdConfigured_) {
config_.switchBackDelay = config_.checkInterval * config_.switchBackThreshold;
} else {
config_.switchBackThreshold =
calculateThreshold(config_.switchBackDelay, config_.checkInterval);
}
}
Config config_;
bool failoverThresholdConfigured_{false};
bool switchBackThresholdConfigured_{false};
};
explicit AutoClusterFailover(Config&& config);
~AutoClusterFailover() final;
ServiceInfo initialServiceInfo() final;
void initialize(std::function<void(ServiceInfo)> onServiceInfoUpdate) final;
private:
std::shared_ptr<AutoClusterFailoverImpl> impl_;
};
} // namespace pulsar
#endif