|
| 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 | +#include <pulsar/AutoClusterFailover.h> |
| 20 | +#include <pulsar/Client.h> |
| 21 | + |
| 22 | +#include <atomic> |
| 23 | +#include <memory> |
| 24 | + |
| 25 | +#include "ClientImpl.h" |
| 26 | +#include "ExecutorService.h" |
| 27 | +#include "LogUtils.h" |
| 28 | + |
| 29 | +DECLARE_LOG_OBJECT() |
| 30 | + |
| 31 | +namespace pulsar { |
| 32 | + |
| 33 | +class AutoClusterFailoverImpl : public std::enable_shared_from_this<AutoClusterFailoverImpl> { |
| 34 | + public: |
| 35 | + explicit AutoClusterFailoverImpl(AutoClusterFailover::Config&& config) : config_(std::move(config)) {} |
| 36 | + |
| 37 | + ~AutoClusterFailoverImpl() { running_->store(false, std::memory_order_release); } |
| 38 | + |
| 39 | + void initialize(const ClientImplPtr& client) { |
| 40 | + client_ = client; |
| 41 | + executor_ = client->getIOExecutorProvider()->get(); |
| 42 | + scheduleProbeAndUpdateServiceUrl(); |
| 43 | + } |
| 44 | + |
| 45 | + void scheduleProbeAndUpdateServiceUrl() { |
| 46 | + auto timer = executor_->createDeadlineTimer(); |
| 47 | + timer->expires_after(config_.checkInterval); |
| 48 | + timer->async_wait([this, weakSelf{weak_from_this()}, timer](auto error) { |
| 49 | + auto self = weakSelf.lock(); |
| 50 | + if (!self) { |
| 51 | + LOG_INFO("AutoClusterFailoverImpl has been destroyed, exiting timer callback"); |
| 52 | + return; |
| 53 | + } |
| 54 | + auto closed = !running_->load(std::memory_order_acquire); |
| 55 | + if (!error || closed) { |
| 56 | + LOG_INFO("AutoClusterFailover exited, timer error: " << error.message() |
| 57 | + << ", closed: " << closed); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + probeAndUpdateServiceUrl(); |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + void probeAndUpdateServiceUrl() { |
| 66 | + auto currentServiceInfo = client_->getServiceInfo(); |
| 67 | + if (currentServiceInfo == config_.primary) { |
| 68 | + // TODO: probe whether primary is down |
| 69 | + } else { |
| 70 | + // TODO: |
| 71 | + // 1. probe whether current (one secondary) is down |
| 72 | + // 2. if not, check whether primary is up and switch back if it is |
| 73 | + } |
| 74 | + scheduleProbeAndUpdateServiceUrl(); |
| 75 | + } |
| 76 | + |
| 77 | + private: |
| 78 | + const AutoClusterFailover::Config config_; |
| 79 | + ClientImplPtr client_; |
| 80 | + ExecutorServicePtr executor_; |
| 81 | + std::shared_ptr<std::atomic_bool> running_{std::make_shared<std::atomic_bool>(true)}; |
| 82 | +}; |
| 83 | + |
| 84 | +AutoClusterFailover::AutoClusterFailover(AutoClusterFailover::Config&& config) |
| 85 | + : impl_(std::make_shared<AutoClusterFailoverImpl>(std::move(config))) {} |
| 86 | + |
| 87 | +AutoClusterFailover::~AutoClusterFailover() {} |
| 88 | + |
| 89 | +void AutoClusterFailover::initialize(Client& client) { impl_->initialize(client.impl_); } |
| 90 | + |
| 91 | +} // namespace pulsar |
0 commit comments