Add Sentinel change notification handling logic
[ric-plt/sdl.git] / include / private / redis / asyncsentineldatabasediscovery.hpp
1 /*
2    Copyright (c) 2018-2019 Nokia.
3
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
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 */
16
17 #ifndef SHAREDDATALAYER_REDIS_ASYNCSENTINELDATABASEDISCOVERY_HPP_
18 #define SHAREDDATALAYER_REDIS_ASYNCSENTINELDATABASEDISCOVERY_HPP_
19
20 #include <functional>
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"
26
27 namespace shareddatalayer
28 {
29     namespace redis
30     {
31         class AsyncCommandDispatcher;
32         struct Contents;
33         class ContentsBuilder;
34         class Reply;
35     }
36
37     class Engine;
38
39     namespace redis
40     {
41         class AsyncSentinelDatabaseDiscovery: public AsyncDatabaseDiscovery
42         {
43         public:
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)>;
49
50             AsyncSentinelDatabaseDiscovery(const AsyncSentinelDatabaseDiscovery&) = delete;
51
52             AsyncSentinelDatabaseDiscovery& operator = (const AsyncSentinelDatabaseDiscovery&) = delete;
53
54             AsyncSentinelDatabaseDiscovery(std::shared_ptr<Engine> engine,
55                                            std::shared_ptr<Logger> logger);
56
57             AsyncSentinelDatabaseDiscovery(std::shared_ptr<Engine> engine,
58                                            std::shared_ptr<Logger> logger,
59                                            const AsyncCommandDispatcherCreator& asyncCommandDispatcherCreator,
60                                            std::shared_ptr<redis::ContentsBuilder> contentsBuilder);
61
62             ~AsyncSentinelDatabaseDiscovery() override;
63
64             void setStateChangedCb(const StateChangedCb& stateChangedCb) override;
65
66             void clearStateChangedCb() override;
67
68             void setConnected(bool state);
69         private:
70             std::shared_ptr<Engine> engine;
71             std::shared_ptr<Logger> logger;
72             StateChangedCb stateChangedCb;
73             DatabaseInfo databaseInfo;
74             std::shared_ptr<redis::AsyncCommandDispatcher> subscriber;
75             std::shared_ptr<redis::AsyncCommandDispatcher> dispatcher;
76             std::shared_ptr<redis::ContentsBuilder> contentsBuilder;
77             Timer subscribeRetryTimer;
78             Timer::Duration subscribeRetryTimerDuration;
79             Timer masterInquiryRetryTimer;
80             Timer::Duration masterInquiryRetryTimerDuration;
81
82             void subscribeNotifications();
83
84             void subscribeAck(const std::error_code& error, const Reply& reply);
85
86             void sendMasterInquiry();
87
88             void masterInquiryAck(const std::error_code& error, const Reply& reply);
89         };
90     }
91 }
92
93 #endif