cluck 1.0.1
The cluster lock service.
cluck.h
Go to the documentation of this file.
1// Copyright (c) 2016-2025 Made to Order Software Corp. All Rights Reserved
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15#pragma once
16
17// eventdispatcher
18//
19#include <eventdispatcher/connection_with_send_message.h>
20#include <eventdispatcher/dispatcher.h>
21#include <eventdispatcher/timer.h>
22
23
24// snapdev
25//
26#include <snapdev/callback_manager.h>
27#include <snapdev/timespec_ex.h>
28
29
30
31namespace cluck
32{
33
34
35
36enum class mode_t
37{
38 CLUCK_MODE_SIMPLE, // safe code executes without the need for additional asynchronous messages
39 CLUCK_MODE_EXTENDED // safe code requires further messages to be sent/received
40};
41
42
43enum class reason_t
44{
46 CLUCK_REASON_LOCAL_TIMEOUT, // process_timeout() was called
47 CLUCK_REASON_REMOTE_TIMEOUT, // FAILED_LOCK was received with a "timeout" error
48 CLUCK_REASON_DEADLOCK, // FAILED_LOCK was received with a "deadlock" error
49 CLUCK_REASON_TRANSMISSION_ERROR, // communicatord could not forward the message to a cluckd
50 CLUCK_REASON_INVALID, // someone did not like our message
51};
52
53
54enum class type_t
55{
56 CLUCK_TYPE_READ_WRITE, // wait N seconds after the last READ-ONLY before attempting the READ-WRITE lock
57 CLUCK_TYPE_READ_ONLY, // shared lock
58 CLUCK_TYPE_READ_WRITE_PRIORITY, // prevent further READ-ONLY until done with this lock
59};
60
61
70
71
80typedef snapdev::timespec_ex timeout_t;
81
84inline timeout_t CLUCK_MAXIMUM_TIMEOUT = timeout_t(7 * 24 * 60 * 60, 0); // no matter what limit all timeouts to this value (7 days)
86inline timeout_t CLUCK_LOCK_OBTENTION_MAXIMUM_TIMEOUT = timeout_t(60 * 60, 0); // by default limit obtention timeout to this value
90
91
92inline std::size_t CLUCK_MAXIMUM_ENTERING_LOCKS = 100;
93
94
100void set_unlock_timeout(timeout_t timeout);
101
102
103
119class cluck
120 : public ed::timer
121{
122public:
123 typedef std::shared_ptr<cluck> pointer_t;
124 typedef std::function<bool(cluck *)> callback_t;
125 typedef snapdev::callback_manager<callback_t> callback_manager_t;
126 typedef std::uint64_t serial_t;
127
128 cluck(
129 std::string const & object_name
130 , ed::connection_with_send_message::pointer_t connection
131 , ed::dispatcher::pointer_t dispatcher
133 virtual ~cluck() override;
134
135 callback_manager_t::callback_id_t
136 add_lock_obtained_callback(
137 callback_t func
138 , callback_manager_t::priority_t priority = callback_manager_t::DEFAULT_PRIORITY);
139 bool remove_lock_obtained_callback(
140 callback_manager_t::callback_id_t id);
141 callback_manager_t::callback_id_t
142 add_lock_failed_callback(
143 callback_t func
144 , callback_manager_t::priority_t priority = callback_manager_t::DEFAULT_PRIORITY);
145 bool remove_lock_failed_callback(
146 callback_manager_t::callback_id_t id);
147 callback_manager_t::callback_id_t
148 add_finally_callback(
149 callback_t func
150 , callback_manager_t::priority_t priority = callback_manager_t::DEFAULT_PRIORITY);
151 bool remove_finally_callback(
152 callback_manager_t::callback_id_t id);
153
159 void set_unlock_timeout(timeout_t timeout);
160
161 std::string const & get_object_name() const;
162 mode_t get_mode() const;
163 type_t get_type() const;
164 void set_type(type_t type);
165 reason_t get_reason() const;
166
167 bool lock();
168 void unlock();
169 timeout_t get_timeout_date() const;
170 bool is_locked() const;
171 bool is_busy() const;
172
173 // ed::connection implementation
174 //
175 virtual void process_timeout() override;
176
177protected:
178 virtual void lock_obtained();
179 virtual void lock_failed();
180 virtual void finally();
181
182private:
183 bool is_cluck_msg(ed::message & msg) const;
184 void msg_locked(ed::message & msg);
185 void msg_lock_failed(ed::message & msg);
186 void msg_transmission_report(ed::message & msg);
187 void msg_unlocked(ed::message & msg);
188 void msg_unlocking(ed::message & msg);
189 void set_reason(reason_t reason);
190 bool help(advgetopt::string_set_t & commands);
191
192 std::string f_object_name = std::string();
193 ed::dispatcher_match::tag_t const
194 f_tag = ed::dispatcher_match::DISPATCHER_MATCH_NO_TAG;
195 ed::connection_with_send_message::pointer_t
196 f_connection = ed::connection_with_send_message::pointer_t();
197 ed::dispatcher::pointer_t f_dispatcher = ed::dispatcher::pointer_t();
199 callback_manager_t f_lock_obtained_callbacks = callback_manager_t();
200 callback_manager_t f_lock_failed_callbacks = callback_manager_t();
201 callback_manager_t f_finally_callbacks = callback_manager_t();
202 timeout_t f_lock_obtention_timeout = CLUCK_DEFAULT_TIMEOUT;
203 timeout_t f_lock_duration_timeout = CLUCK_DEFAULT_TIMEOUT;
205 timeout_t f_lock_timeout_date = timeout_t();
206 timeout_t f_unlocked_timeout_date = timeout_t();
210 serial_t f_serial = serial_t();
211};
212
213
214
215} // namespace cluck
216// vim: ts=4 sw=4 et
std::shared_ptr< cluck > pointer_t
Definition cluck.h:123
snapdev::callback_manager< callback_t > callback_manager_t
Definition cluck.h:125
std::uint64_t serial_t
Definition cluck.h:126
std::function< bool(cluck *)> callback_t
Definition cluck.h:124
timeout_t CLUCK_UNLOCK_DEFAULT_TIMEOUT
Definition cluck.h:88
timeout_t CLUCK_LOCK_OBTENTION_MAXIMUM_TIMEOUT
Definition cluck.h:86
timeout_t get_unlock_timeout()
Definition cluck.cpp:182
void set_lock_duration_timeout(timeout_t timeout)
Definition cluck.cpp:166
timeout_t get_lock_obtention_timeout()
Definition cluck.cpp:136
state_t
Definition cluck.h:63
reason_t
Definition cluck.h:44
@ CLUCK_REASON_TRANSMISSION_ERROR
timeout_t CLUCK_DEFAULT_TIMEOUT
Definition cluck.h:82
mode_t
Definition cluck.h:37
snapdev::timespec_ex timeout_t
A timeout delay.
Definition cluck.h:80
timeout_t CLUCK_LOCK_OBTENTION_DEFAULT_TIMEOUT
Definition cluck.h:85
type_t
Definition cluck.h:55
@ CLUCK_TYPE_READ_WRITE_PRIORITY
timeout_t CLUCK_UNLOCK_MINIMUM_TIMEOUT
Definition cluck.h:89
timeout_t CLUCK_MINIMUM_TIMEOUT
Definition cluck.h:83
timeout_t get_lock_duration_timeout()
Definition cluck.cpp:159
void set_lock_obtention_timeout(timeout_t timeout)
Definition cluck.cpp:143
timeout_t CLUCK_MAXIMUM_TIMEOUT
Definition cluck.h:84
void set_unlock_timeout(timeout_t timeout)
Definition cluck.cpp:189
std::size_t CLUCK_MAXIMUM_ENTERING_LOCKS
Definition cluck.h:92
timeout_t CLUCK_LOCK_DURATION_DEFAULT_TIMEOUT
Definition cluck.h:87

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.