Skip to content
Merged
105 changes: 105 additions & 0 deletions include/pulsar/AutoClusterFailover.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* 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 <chrono>

namespace pulsar {

class Client;
class AutoClusterFailoverImpl;

class PULSAR_PUBLIC AutoClusterFailover final : public ServiceInfoProvider {
public:
struct Config {
ServiceInfo primary;
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
};

/**
* Builder helps create an AutoClusterFailover configuration.
*
* Example:
* ServiceInfo primary{...};
* std::vector<ServiceInfo> secondaries{...};
* AutoClusterFailover provider = AutoClusterFailover::Builder(primary, secondaries)
* .withCheckInterval(std::chrono::seconds(30))
* .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 be unreachable before switching.
* - switchBackDelay: how long the primary must remain healthy before switching back.
*/
class Builder {
public:
Builder(ServiceInfo primary, std::vector<ServiceInfo> secondary) {
config_.primary = std::move(primary);
config_.secondary = std::move(secondary);
}
Comment thread
BewareMyPower marked this conversation as resolved.
Outdated

// Set how frequently probes run against the active cluster(s).
Builder& withCheckInterval(std::chrono::milliseconds interval) {
config_.checkInterval = interval;
return *this;
}

// Set how long the current cluster must be unreachable before attempting failover.
Builder& withFailoverDelay(std::chrono::milliseconds delay) {
config_.failoverDelay = delay;
return *this;
}

// Set how long the primary must remain healthy before switching back from a secondary.
Builder& withSwitchBackDelay(std::chrono::milliseconds delay) {
config_.switchBackDelay = delay;
return *this;
}

AutoClusterFailover build() { return AutoClusterFailover(std::move(config_)); }

private:
Config config_;
};

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
1 change: 1 addition & 0 deletions include/pulsar/ServiceInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace pulsar {
*/
class PULSAR_PUBLIC ServiceInfo final {
public:
ServiceInfo() = default; // only for storing in containers like std::vector, not for public use
Comment thread
BewareMyPower marked this conversation as resolved.
Outdated
ServiceInfo(std::string serviceUrl, AuthenticationPtr authentication = AuthFactory::Disabled(),
std::optional<std::string> tlsTrustCertsFilePath = std::nullopt);

Expand Down
Loading
Loading