cluck 1.0.1
The cluster lock service.
ticket.h
Go to the documentation of this file.
1// Copyright (c) 2016-2025 Made to Order Software Corp. All Rights Reserved
2//
3// https://snapwebsites.org/project/cluck
4// contact@m2osw.com
5//
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18#pragma once
19
20// self
21//
22#include "messenger.h"
23
24
25// cluck
26//
27#include <cluck/cluck.h>
28
29
30// fluid-settings
31//
32#include <fluid-settings/fluid_settings_connection.h>
33
34
35
36namespace cluck_daemon
37{
38
39
40
41class ticket
42 : public std::enable_shared_from_this<ticket>
43{
44public:
45 typedef std::shared_ptr<ticket> pointer_t;
46 typedef std::vector<pointer_t> vector_t;
47 typedef std::map<std::string, pointer_t> key_map_t; // sorted by key
48 typedef std::map<std::string, key_map_t> object_map_t; // sorted by object_name
49 typedef std::int32_t serial_t;
50 typedef std::uint32_t ticket_id_t;
51
52 static serial_t const NO_SERIAL = -1;
53 static ticket_id_t const NO_TICKET = 0;
54
55 ticket(
56 cluckd * c
58 , std::string const & lock_name
59 , ed::dispatcher_match::tag_t tag
60 , std::string const & entering_key
61 , cluck::timeout_t obtention_timeout
62 , cluck::timeout_t lock_duration
63 , std::string const & server_name
64 , std::string const & service_name);
65 ticket(ticket const &) = delete;
66 ticket & operator = (ticket const &) = delete;
67
68 // message handling
69 //
70 bool send_message_to_leaders(ed::message & msg);
71 void entering();
72 void entered();
73 void max_ticket(ticket_id_t new_max_ticket);
74 void add_ticket();
75 void ticket_added(key_map_t const & entering);
76 void remove_entering(std::string const & key);
77 void activate_lock();
78 void lock_activated();
79 void drop_ticket(); // this is called when we receive the UNLOCK event
80 void lock_failed(std::string const & reason);
82
83 // object handling
84 //
85 void set_owner(std::string const & owner);
86 std::string const & get_owner() const;
87 pid_t get_client_pid() const;
88 void set_serial(serial_t owner);
89 serial_t get_serial() const;
92 void set_ready();
93 void set_ticket_number(ticket_id_t number);
95 bool is_locked() const;
96 bool one_leader() const;
102 bool timed_out() const;
103 std::string const & get_object_name() const;
104 ed::dispatcher_match::tag_t get_tag() const;
105 std::string const & get_server_name() const;
106 std::string const & get_service_name() const;
107 std::string const & get_entering_key() const;
108 std::string const & get_ticket_key() const;
109 std::string serialize() const;
110 void unserialize(std::string const & data);
111
112private:
113 enum class lock_failure_t
114 {
115 LOCK_FAILURE_NONE, // no failure so far
116 LOCK_FAILURE_LOCK, // LOCK timed out
117 LOCK_FAILURE_UNLOCKING, // UNLOCKING timed out
118 };
119
120 // this is owned by a cluckd object so no need for a smart pointer
121 // (and it would create a parent/child loop)
122 //
123 cluckd * f_cluckd = nullptr;
124
125 // initialization
126 //
128 std::string f_object_name = std::string();
129 ed::dispatcher_match::tag_t f_tag = ed::dispatcher_match::DISPATCHER_MATCH_NO_TAG;
134 std::string f_server_name = std::string();
135 std::string f_service_name = std::string();
136 std::string f_owner = std::string();
138
139 // initialized, entering
140 //
141 std::string f_entering_key = std::string();
142 bool f_get_max_ticket = false;
143
144 // entered, adding ticket
145 //
147 bool f_added_ticket = false;
148 std::string f_ticket_key = std::string();
149
150 // ticket added, exiting
151 //
154
155 // exited, ticket ready
156 //
157 bool f_ticket_ready = false;
158
159 // locked
160 //
161 bool f_locked = false;
164
165 // the lock did not take (in most cases, this is because of a timeout)
166 //
168};
169
170
171
172} // namespace cluck_deamon
173// vim: ts=4 sw=4 et
Class handling intercomputer locking.
Definition cluckd.h:48
Handle messages from the communicatord.
Definition messenger.h:36
std::shared_ptr< messenger > pointer_t
Definition messenger.h:38
Handle the ticket messages.
Definition ticket.h:43
void ticket_added(key_map_t const &entering)
Called whenever a TICKET_ADDED is received.
Definition ticket.cpp:657
ticket & operator=(ticket const &)=delete
cluck::timeout_t f_unlock_duration
Definition ticket.h:133
key_map_t f_still_entering
Definition ticket.h:153
void set_owner(std::string const &owner)
Define whether this ticket is the owner of that lock.
Definition ticket.cpp:1073
void set_serial(serial_t owner)
Give the lock a serial number for some form of unicity.
Definition ticket.cpp:1147
std::string const & get_object_name() const
Retrieve the object name of this ticket.
Definition ticket.cpp:1476
void entering()
Enter the mode that lets us retrieve our ticket number.
Definition ticket.cpp:471
cluck::timeout_t get_unlock_duration() const
Get unlock duration.
Definition ticket.cpp:1213
void set_alive_timeout(cluck::timeout_t timeout)
Define a time when the ticket times out while waiting.
Definition ticket.cpp:1353
ticket(ticket const &)=delete
std::string const & get_service_name() const
Retrieve the service name of this ticket.
Definition ticket.cpp:1523
cluck::timeout_t f_lock_timeout_date
Definition ticket.h:162
static serial_t const NO_SERIAL
Definition ticket.h:52
bool timed_out() const
Check whether this ticket timed out.
Definition ticket.cpp:1463
cluck::timeout_t get_lock_timeout_date() const
Get the lock timeout date.
Definition ticket.cpp:1398
cluck::timeout_t f_obtention_timeout
Definition ticket.h:130
std::string const & get_owner() const
Return the name of this ticket's owner.
Definition ticket.cpp:1096
void entered()
Tell this entering that we received a LOCKENTERED message.
Definition ticket.cpp:521
cluckd * f_cluckd
Definition ticket.h:123
std::string const & get_entering_key() const
Retrieve a reference to the entering key of this ticket.
Definition ticket.cpp:1540
std::string f_ticket_key
Definition ticket.h:148
cluck::timeout_t get_obtention_timeout() const
Get the obtention timeout date.
Definition ticket.cpp:1319
static ticket_id_t const NO_TICKET
Definition ticket.h:53
messenger::pointer_t f_messenger
Definition ticket.h:127
cluck::timeout_t f_lock_duration
Definition ticket.h:132
std::string serialize() const
Serialize a ticket to send it over to another leader.
Definition ticket.cpp:1583
std::string f_owner
Definition ticket.h:136
ticket_id_t get_ticket_number() const
Return the ticket number of this ticket.
Definition ticket.cpp:1278
ed::dispatcher_match::tag_t get_tag() const
Retrieve the tag of this ticket.
Definition ticket.cpp:1489
void set_unlock_duration(cluck::timeout_t duration)
Change the unlock duration to the specified value.
Definition ticket.cpp:1188
bool f_added_ticket_quorum
Definition ticket.h:152
void activate_lock()
Check whether this ticket can be activated and do so if so.
Definition ticket.cpp:750
std::string f_object_name
Definition ticket.h:128
serial_t f_serial
Definition ticket.h:137
cluck::timeout_t f_unlocked_timeout_date
Definition ticket.h:163
ed::dispatcher_match::tag_t f_tag
Definition ticket.h:129
std::string f_server_name
Definition ticket.h:134
std::string const & get_server_name() const
Retrieve the server name of this ticket.
Definition ticket.cpp:1506
pid_t get_client_pid() const
Retrieve the client process identifier.
Definition ticket.cpp:1114
std::map< std::string, key_map_t > object_map_t
Definition ticket.h:48
lock_failure_t f_lock_failed
Definition ticket.h:167
std::uint32_t ticket_id_t
Definition ticket.h:50
bool send_message_to_leaders(ed::message &msg)
Send a message to the other two leaders.
Definition ticket.cpp:417
void drop_ticket()
We are done with the ticket.
Definition ticket.cpp:813
bool is_locked() const
Check whether this ticket is locked or not.
Definition ticket.cpp:1290
serial_t get_serial() const
Return the serial number of this ticket.
Definition ticket.cpp:1160
void unserialize(std::string const &data)
Unserialize a ticket string back to a ticket object.
Definition ticket.cpp:1659
void lock_failed(std::string const &reason)
Let the service that wanted this lock know that it failed.
Definition ticket.cpp:897
void lock_activated()
Check whether this ticket can be activated and do so if so.
Definition ticket.cpp:779
cluck::timeout_t get_current_timeout_date() const
Get the current lock timeout date.
Definition ticket.cpp:1428
std::string const & get_ticket_key() const
Retrieve a reference to the ticket key.
Definition ticket.cpp:1561
std::string f_service_name
Definition ticket.h:135
void max_ticket(ticket_id_t new_max_ticket)
Called whenever a MAX_TICKET is received.
Definition ticket.cpp:566
ticket_id_t f_our_ticket
Definition ticket.h:146
std::vector< pointer_t > vector_t
Definition ticket.h:46
void set_ticket_number(ticket_id_t number)
Set the ticket number.
Definition ticket.cpp:1246
std::shared_ptr< ticket > pointer_t
Definition ticket.h:45
void remove_entering(std::string const &key)
Call any time time an entering flag is reset.
Definition ticket.cpp:694
void add_ticket()
Send the ADD_TICKET message.
Definition ticket.cpp:601
void set_ready()
Mark the ticket as being ready.
Definition ticket.cpp:1227
bool one_leader() const
Check whether the system only has one leader.
Definition ticket.cpp:1305
std::int32_t serial_t
Definition ticket.h:49
cluck::timeout_t f_alive_timeout
Definition ticket.h:131
std::map< std::string, pointer_t > key_map_t
Definition ticket.h:47
cluck::timeout_t get_lock_duration() const
Retrieve the lock duration.
Definition ticket.cpp:1381
std::string f_entering_key
Definition ticket.h:141
snapdev::timespec_ex timeout_t
A timeout delay.
Definition cluck.h:80

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.