Skip to content

Commit 40259cc

Browse files
PIP-121: Implement AutoClusterFailover (#547)
1 parent b3d2b58 commit 40259cc

File tree

3 files changed

+752
-0
lines changed

3 files changed

+752
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
#ifndef PULSAR_AUTO_CLUSTER_FAILOVER_H_
20+
#define PULSAR_AUTO_CLUSTER_FAILOVER_H_
21+
22+
#include <pulsar/ServiceInfoProvider.h>
23+
24+
#include <chrono>
25+
#include <cstdint>
26+
#include <functional>
27+
#include <memory>
28+
#include <vector>
29+
30+
namespace pulsar {
31+
32+
class Client;
33+
class AutoClusterFailoverImpl;
34+
35+
class PULSAR_PUBLIC AutoClusterFailover final : public ServiceInfoProvider {
36+
public:
37+
struct Config {
38+
const ServiceInfo primary;
39+
const std::vector<ServiceInfo> secondary;
40+
std::chrono::milliseconds checkInterval{5000}; // 5 seconds
41+
uint32_t failoverThreshold{1};
42+
uint32_t switchBackThreshold{1};
43+
44+
Config(ServiceInfo primary, std::vector<ServiceInfo> secondary)
45+
: primary(std::move(primary)), secondary(std::move(secondary)) {}
46+
};
47+
48+
/**
49+
* Builder helps create an AutoClusterFailover configuration.
50+
*
51+
* Example:
52+
* ServiceInfo primary{...};
53+
* std::vector<ServiceInfo> secondaries{...};
54+
* AutoClusterFailover provider = AutoClusterFailover::Builder(primary, secondaries)
55+
* .withCheckInterval(std::chrono::seconds(5))
56+
* .withFailoverThreshold(3)
57+
* .withSwitchBackThreshold(3)
58+
* .build();
59+
*
60+
* Notes:
61+
* - primary: the preferred cluster to use when available.
62+
* - secondary: ordered list of fallback clusters.
63+
* - checkInterval: frequency of health probes.
64+
* - failoverThreshold: the number of consecutive failed probes required before switching away from
65+
* the current cluster.
66+
* - switchBackThreshold: the number of consecutive successful probes to the primary required before
67+
* switching back from a secondary while that secondary remains available. If the active secondary
68+
* becomes unavailable and the primary is available, the implementation may switch back to the
69+
* primary immediately, regardless of this threshold.
70+
*/
71+
class Builder {
72+
public:
73+
Builder(ServiceInfo primary, std::vector<ServiceInfo> secondary)
74+
: config_(std::move(primary), std::move(secondary)) {}
75+
76+
// Set how frequently probes run against the active cluster(s). Default: 5 seconds.
77+
Builder& withCheckInterval(std::chrono::milliseconds interval) {
78+
config_.checkInterval = interval;
79+
return *this;
80+
}
81+
82+
// Set the number of consecutive failed probes required before attempting failover. Default: 1.
83+
Builder& withFailoverThreshold(uint32_t threshold) {
84+
config_.failoverThreshold = threshold;
85+
return *this;
86+
}
87+
88+
// Set the number of consecutive successful primary probes required before switching back from a
89+
// healthy secondary. If the active secondary becomes unavailable and the primary is available,
90+
// the implementation may switch back immediately regardless of this threshold. Default: 1.
91+
Builder& withSwitchBackThreshold(uint32_t threshold) {
92+
config_.switchBackThreshold = threshold;
93+
return *this;
94+
}
95+
96+
AutoClusterFailover build() { return AutoClusterFailover(std::move(config_)); }
97+
98+
private:
99+
Config config_;
100+
};
101+
102+
explicit AutoClusterFailover(Config&& config);
103+
104+
~AutoClusterFailover() final;
105+
106+
ServiceInfo initialServiceInfo() final;
107+
108+
void initialize(std::function<void(ServiceInfo)> onServiceInfoUpdate) final;
109+
110+
private:
111+
std::shared_ptr<AutoClusterFailoverImpl> impl_;
112+
};
113+
114+
} // namespace pulsar
115+
116+
#endif

0 commit comments

Comments
 (0)