2 Copyright (c) 2018-2019 Nokia.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
17 #ifndef SHAREDDATALAYER_REDIS_ASYNCSENTINELDATABASEDISCOVERY_HPP_
18 #define SHAREDDATALAYER_REDIS_ASYNCSENTINELDATABASEDISCOVERY_HPP_
21 #include <system_error>
22 #include "private/redis/asyncdatabasediscovery.hpp"
23 #include "private/redis/databaseinfo.hpp"
24 #include "private/logger.hpp"
25 #include "private/timer.hpp"
27 namespace shareddatalayer
31 class AsyncCommandDispatcher;
33 class ContentsBuilder;
41 class AsyncSentinelDatabaseDiscovery: public AsyncDatabaseDiscovery
44 using AsyncCommandDispatcherCreator = std::function<std::shared_ptr<redis::AsyncCommandDispatcher>(Engine& engine,
45 const redis::DatabaseInfo& databaseInfo,
46 std::shared_ptr<redis::ContentsBuilder> contentsBuilder,
47 std::shared_ptr<Logger> logger,
48 bool usePermanentCommandCallbacks)>;
50 AsyncSentinelDatabaseDiscovery(const AsyncSentinelDatabaseDiscovery&) = delete;
52 AsyncSentinelDatabaseDiscovery& operator = (const AsyncSentinelDatabaseDiscovery&) = delete;
54 AsyncSentinelDatabaseDiscovery(std::shared_ptr<Engine> engine,
55 std::shared_ptr<Logger> logger,
56 const HostAndPort& sentinelAddress,
57 const std::string& sentinelMasterName);
59 AsyncSentinelDatabaseDiscovery(std::shared_ptr<Engine> engine,
60 std::shared_ptr<Logger> logger,
61 const HostAndPort& sentinelAddress,
62 const std::string& sentinelMasterName,
63 const AsyncCommandDispatcherCreator& asyncCommandDispatcherCreator,
64 std::shared_ptr<redis::ContentsBuilder> contentsBuilder);
66 ~AsyncSentinelDatabaseDiscovery() override;
68 void setStateChangedCb(const StateChangedCb& stateChangedCb) override;
70 void clearStateChangedCb() override;
72 void setConnected(bool state);
74 std::shared_ptr<Engine> engine;
75 std::shared_ptr<Logger> logger;
76 StateChangedCb stateChangedCb;
77 DatabaseInfo databaseInfo;
78 std::string sentinelMasterName;
79 std::shared_ptr<redis::AsyncCommandDispatcher> subscriber;
80 std::shared_ptr<redis::AsyncCommandDispatcher> dispatcher;
81 std::shared_ptr<redis::ContentsBuilder> contentsBuilder;
82 Timer subscribeRetryTimer;
83 Timer::Duration subscribeRetryTimerDuration;
84 Timer masterInquiryRetryTimer;
85 Timer::Duration masterInquiryRetryTimerDuration;
87 void subscribeNotifications();
89 void subscribeAck(const std::error_code& error, const Reply& reply);
91 void sendMasterInquiry();
93 void masterInquiryAck(const std::error_code& error, const Reply& reply);