cluck 1.0.1
The cluster lock service.
cluckd.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 "computer.h"
23#include "interrupt.h"
24#include "message_cache.h"
25#include "ticket.h"
26#include "timer.h"
27
28
29// eventdispatcher
30//
31#include <eventdispatcher/message.h>
32
33
42namespace cluck_daemon
43{
44
45
46
47class cluckd
48{
49public:
50 typedef std::shared_ptr<cluckd> pointer_t;
51
52 cluckd(int argc, char * argv[]);
53 cluckd(cluckd const & rhs) = delete;
54 virtual ~cluckd();
55
56 cluckd & operator = (cluckd const & rhs) = delete;
57
58 void add_connections();
59 void set_my_ip_address(addr::addr const & a);
60 void run();
61
62 int get_computer_count() const;
63 std::string const & get_server_name() const;
64 bool is_daemon_ready() const;
65 computer::pointer_t is_leader(std::string id = std::string()) const;
68 void cleanup();
69 ticket::ticket_id_t get_last_ticket(std::string const & lock_name);
70 void set_ticket(std::string const & object_name, std::string const & key, ticket::pointer_t ticket);
71 void lock_exiting(ed::message & msg);
72 ticket::key_map_t const get_entering_tickets(std::string const & lock_name);
73 std::string serialized_tickets();
74 ticket::pointer_t find_first_lock(std::string const & lock_name);
75 void stop(bool quitting);
76 std::string ticket_list() const;
77 void send_lock_started(ed::message const * msg);
78 void election_status();
79
80 // messages received by the messenger which then calls the cluckd functions
81 // however, the messenger accesses all of them to setup the dispatcher
82 //
83 void msg_absolutely(ed::message & msg);
84 void msg_activate_lock(ed::message & msg);
85 void msg_add_ticket(ed::message & msg);
86 void msg_clock_stable(ed::message & msg);
87 void msg_cluster_down(ed::message & msg);
88 void msg_cluster_up(ed::message & msg);
89 void msg_drop_ticket(ed::message & msg);
90 void msg_get_max_ticket(ed::message & msg);
91 void msg_info(ed::message & msg);
92 void msg_list_tickets(ed::message & msg);
93 void msg_lock(ed::message & msg);
94 void msg_lock_activated(ed::message & msg);
95 void msg_lock_entered(ed::message & msg);
96 void msg_lock_entering(ed::message & msg);
97 void msg_lock_exiting(ed::message & msg);
98 void msg_lock_failed(ed::message & msg);
99 void msg_lock_leaders(ed::message & msg);
100 void msg_lock_started(ed::message & msg);
101 void msg_lock_status(ed::message & msg);
102 void msg_lock_tickets(ed::message & msg);
103 void msg_max_ticket(ed::message & msg);
104 void msg_server_gone(ed::message & msg);
105 void msg_status(ed::message & msg);
106 void msg_ticket_added(ed::message & msg);
107 void msg_ticket_ready(ed::message & msg);
108 void msg_unlock(ed::message & msg);
109
110private:
111 bool get_parameters(
112 ed::message const & message
113 , std::string * object_name
114 , ed::dispatcher_match::tag_t * tag
115 , pid_t * client_pid
116 , cluck::timeout_t * timeout
117 , std::string * key
118 , std::string * source);
119 void activate_first_lock(std::string const & object_name);
120 void check_lock_status();
121 void synchronize_leaders();
122 void forward_message_to_leader(ed::message & message);
123
124 advgetopt::getopt f_opts;
125
127 std::string f_server_name = std::string();
128 ed::communicator::pointer_t f_communicator = ed::communicator::pointer_t();
132 std::size_t f_neighbors_count = 0;
133 std::size_t f_neighbors_quorum = 0;
134 std::string f_my_id = std::string();
135 addr::addr f_my_ip_address = addr::addr();
136 bool f_lock_status = false; // not ready
137 bool f_stable_clock = false;
138 computer::map_t f_computers = computer::map_t(); // key is the computer name
144 snapdev::timespec_ex f_election_date = snapdev::timespec_ex();
146 mutable time_t f_pace_lockstarted = 0;
147};
148
149
150
151} // namespace cluck_daemon
152// vim: ts=4 sw=4 et
Class handling intercomputer locking.
Definition cluckd.h:48
void msg_get_max_ticket(ed::message &msg)
Search for the largest ticket.
Definition cluckd.cpp:2671
bool is_daemon_ready() const
Check whether the cluck daemon is ready to process lock requests.
Definition cluckd.cpp:478
timer::pointer_t f_timer
Definition cluckd.h:131
void add_connections()
Finish the cluck daemon initialization.
Definition cluckd.cpp:381
void msg_lock_status(ed::message &msg)
A service asked about the lock status.
Definition cluckd.cpp:3801
void msg_lock_entered(ed::message &msg)
Tell the specified ticket LOCK_ENTERED was received.
Definition cluckd.cpp:3135
std::string const & get_server_name() const
Get the name of the server we are running on.
Definition cluckd.cpp:461
time_t f_pace_lockstarted
Definition cluckd.h:146
void run()
Run the cluck daemon.
Definition cluckd.cpp:421
void cleanup()
Clean timed out entries if any.
Definition cluckd.cpp:1688
void lock_exiting(ed::message &msg)
Used to simulate a LOCK_EXITING message.
Definition cluckd.cpp:1960
computer::pointer_t get_leader_b() const
Get pointer to leader B.
Definition cluckd.cpp:748
void msg_max_ticket(ed::message &msg)
Got the largest ticket from another leader.
Definition cluckd.cpp:3978
void msg_add_ticket(ed::message &msg)
Add a ticket from another cluckd.
Definition cluckd.cpp:2254
ed::communicator::pointer_t f_communicator
Definition cluckd.h:128
void msg_ticket_ready(ed::message &msg)
Let other leaders know that the ticket is ready.
Definition cluckd.cpp:4199
void msg_cluster_down(ed::message &msg)
The communicatord lost too many connections.
Definition cluckd.cpp:2492
ticket::ticket_id_t get_last_ticket(std::string const &lock_name)
Determine the last ticket defined in this cluck daemon.
Definition cluckd.cpp:1871
void msg_absolutely(ed::message &msg)
Lock the resource after confirmation that client is alive.
Definition cluckd.cpp:2109
ticket::object_map_t f_entering_tickets
Definition cluckd.h:142
std::string ticket_list() const
Generate the output for "cluck-status --list".
Definition cluckd.cpp:917
void msg_activate_lock(ed::message &msg)
Acknowledge the ACTIVATE_LOCK with what we think is our first lock.
Definition cluckd.cpp:2193
std::size_t f_neighbors_count
Definition cluckd.h:132
snapdev::timespec_ex f_election_date
Definition cluckd.h:144
void set_my_ip_address(addr::addr const &a)
Definition cluckd.cpp:407
addr::addr f_my_ip_address
Definition cluckd.h:135
void msg_unlock(ed::message &msg)
Unlock the resource.
Definition cluckd.cpp:4231
computer::map_t f_computers
Definition cluckd.h:138
void msg_info(ed::message &msg)
Return a JSON with the state of this cluckd object.
Definition cluckd.cpp:790
ticket::object_map_t f_tickets
Definition cluckd.h:143
void msg_lock_started(ed::message &msg)
Called whenever a cluck computer is acknowledging itself.
Definition cluckd.cpp:3655
void stop(bool quitting)
Called whenever we receive the STOP command or equivalent.
Definition cluckd.cpp:1284
std::string f_server_name
Definition cluckd.h:127
void synchronize_leaders()
Synchronize leaders.
Definition cluckd.cpp:1419
std::shared_ptr< cluckd > pointer_t
Definition cluckd.h:50
void msg_lock_leaders(ed::message &msg)
The list of leaders.
Definition cluckd.cpp:3584
void election_status()
Check the status of the election.
Definition cluckd.cpp:972
void msg_lock_activated(ed::message &msg)
Acknowledgement of the lock to activate.
Definition cluckd.cpp:3098
void forward_message_to_leader(ed::message &message)
Forward a user message to a leader.
Definition cluckd.cpp:1648
virtual ~cluckd()
Do some clean ups.
Definition cluckd.cpp:366
ticket::serial_t f_ticket_serial
Definition cluckd.h:145
computer::vector_t f_leaders
Definition cluckd.h:139
std::string serialized_tickets()
Definition cluckd.cpp:1970
void send_lock_started(ed::message const *msg)
Definition cluckd.cpp:1225
void msg_ticket_added(ed::message &msg)
Acknowledgement that the ticket was properly added.
Definition cluckd.cpp:4134
int get_computer_count() const
Return the number of known computers running cluckd.
Definition cluckd.cpp:445
messenger::pointer_t f_messenger
Definition cluckd.h:129
void msg_lock_entering(ed::message &msg)
Create an entering ticket.
Definition cluckd.cpp:3176
ticket::key_map_t const get_entering_tickets(std::string const &lock_name)
Get a reference to the list of entering tickets.
Definition cluckd.cpp:1930
void msg_list_tickets(ed::message &msg)
Reply to the LIST_TICKETS message with the TICKET_LIST.
Definition cluckd.cpp:2707
std::string f_my_id
Definition cluckd.h:134
void msg_lock_exiting(ed::message &msg)
Exit a ticket.
Definition cluckd.cpp:3353
void msg_server_gone(ed::message &msg)
Called whenever a remote connection is disconnected.
Definition cluckd.cpp:4021
void msg_lock(ed::message &msg)
Lock the named resource.
Definition cluckd.cpp:2753
void msg_clock_stable(ed::message &msg)
Message telling us whether the clock is stable.
Definition cluckd.cpp:2472
computer::pointer_t is_leader(std::string id=std::string()) const
Search for a leader.
Definition cluckd.cpp:658
cluck::timeout_t f_start_time
Definition cluckd.h:126
void activate_first_lock(std::string const &object_name)
Make sure the very first ticket is marked as LOCKED.
Definition cluckd.cpp:1328
cluckd(cluckd const &rhs)=delete
advgetopt::getopt f_opts
Definition cluckd.h:124
void set_ticket(std::string const &object_name, std::string const &key, ticket::pointer_t ticket)
Set the ticket.
Definition cluckd.cpp:1910
void msg_cluster_up(ed::message &msg)
Cluster is ready, send the LOCK_STARTED message.
Definition cluckd.cpp:2521
void msg_drop_ticket(ed::message &msg)
One of the cluckd processes asked for a ticket to be dropped.
Definition cluckd.cpp:2581
bool get_parameters(ed::message const &message, std::string *object_name, ed::dispatcher_match::tag_t *tag, pid_t *client_pid, cluck::timeout_t *timeout, std::string *key, std::string *source)
Try to get a set of parameters.
Definition cluckd.cpp:2017
void msg_lock_failed(ed::message &msg)
Acknowledge a lock failure.
Definition cluckd.cpp:3468
interrupt::pointer_t f_interrupt
Definition cluckd.h:130
void msg_lock_tickets(ed::message &msg)
Another cluckd is sending us its list of tickets.
Definition cluckd.cpp:3831
void msg_status(ed::message &msg)
With the STATUS message we know of new communicatord services.
Definition cluckd.cpp:4097
computer::pointer_t get_leader_a() const
Get pointer to leader A.
Definition cluckd.cpp:701
std::size_t f_neighbors_quorum
Definition cluckd.h:133
cluckd & operator=(cluckd const &rhs)=delete
message_cache::list_t f_message_cache
Definition cluckd.h:141
ticket::pointer_t find_first_lock(std::string const &lock_name)
Definition cluckd.cpp:1343
std::vector< pointer_t > vector_t
Definition computer.h:51
std::map< std::string, pointer_t > map_t
Definition computer.h:50
std::shared_ptr< computer > pointer_t
Definition computer.h:49
std::shared_ptr< interrupt > pointer_t
Definition interrupt.h:44
std::shared_ptr< messenger > pointer_t
Definition messenger.h:38
Handle the ticket messages.
Definition ticket.h:43
std::map< std::string, key_map_t > object_map_t
Definition ticket.h:48
std::uint32_t ticket_id_t
Definition ticket.h:50
std::shared_ptr< ticket > pointer_t
Definition ticket.h:45
std::int32_t serial_t
Definition ticket.h:49
std::map< std::string, pointer_t > key_map_t
Definition ticket.h:47
std::shared_ptr< ed::timer > pointer_t
Definition timer.h:38
snapdev::timespec_ex timeout_t
A timeout delay.
Definition cluck.h:80
std::list< message_cache > list_t

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.