Module membership
This module is a membership library for Tarantool based on a gossip
protocol.
This library builds a mesh from multiple Tarantool instances. The mesh monitors itself, helps members discover everyone else in the group and get notified about their status changes with low latency. It is built upon the ideas from Consul or, more precisely, the SWIM algorithm.
The membership module works over UDP protocol and can operate even
before the box.cfg initialization.
A member is represented by the table with the following fields:
uri(string) is a Uniform Resource Identifier.status(string) is a string that takes one of the values below.-
alive: a member that replies to ping-messages isaliveand well. -
suspect: if any member in the group cannot get a reply from any other member, the first member asks three otheralivemembers to send a ping-message to the member in question. If there is no response, the latter becomes asuspect. -
dead: asuspectbecomesdeadafter a timeout. -
left: a member gets theleftstatus after executing the leave() function.
-
incarnation(number) is a value incremented every time the instance becomes asuspect,dead, or updates its payload.payload(table) is auxiliary data that can be used by various modules.timestamp(number) is a value offiber.time64()which:- corresponds to the last update of
statusorincarnation; - is always local;
- does not depend on other members' clock setting.
- corresponds to the last update of
Below is an example of the table:
tarantool> membership.myself()---uri: localhost:33001status: aliveincarnation: 1payload:uuid: 2d00c500-2570-4019-bfcc-ab25e5096b73timestamp: 1522427330993752...
Below is a list of membership's common, encryption, subscription
functions, and options.
Name | Use |
|---|---|
Common functions | |
Initialize the | |
Get the member data structure of the current instance. | |
Get the member data structure for a given URI. | |
Obtain a table with all members known to the current instance. | |
Shorthand for | |
Add a member to the group. | |
Check if the member is in the group. | |
Discover members in LAN by sending a UDP broadcast message. | |
Update | |
Gracefully leave the group. | |
Check if encryption is enabled. | Encryption functions |
Set the key for low-level message encryption. | |
Retrieve the encryption key in use. | |
Subscription functions | |
Subscribe for the members table updates. | |
Remove the subscription. | Options |
Direct ping period. | |
ACK message wait time. | |
Anti-entropy synchronization period. | |
Timeout to mark a | |
Number of members to ping a |
membership
Common functions:
Initialize the membership module. This binds a UDP socket to
0.0.0.0:<port>, sets the advertise_uri parameter to
<advertise_host>:<port>, and incarnation to 1.
The init() function can be called several times, the old socket will
be closed and a new one opened.
If the advertise_uri changes during the next init(), the old URI is
considered DEAD. In order to leave the group gracefully, use the
leave() function.
Parameters:
-
advertise_host(string) — a hostname or IP address to advertise to other members -
port(number) — a UDP port to bind
Returns
true
Return type
boolean
raises : socket bind error
Returns
the member data structure of the current instance.
Return type
table
Parameters:
uri(string) — the given member'sadvertise_uri
Returns
the member data structure of the instance with the given URI.
Return type
table
Obtain all members known to the current instance.
Editing this table has no effect.
Returns
a table with URIs as keys and corresponding member data structures as values.
Return type
table
A shorthand for pairs(membership.members()).
Returns
Lua iterator
It can be used in the following way:
for uri, member in membership.pairs()-- do somethingend
Add a member with the given URI to the group and propagate this event to other members. Adding a member to a single instance is enough as everybody else in the group will receive the update with time. It does not matter who adds whom.
Parameters:
uri(string) — theadvertise_uriof the member to add
Returns
true or nil in case of an error
Return type
boolean
raises : parse error if the URI cannot be parsed
Send a message to a member to make sure it is in the group. If the
member is alive but not in the group, it is added. If it already is in
the group, nothing happens.
Parameters:
uri(string) — theadvertise_uriof the member to ping
Returns
true if the member responds within 0.2 seconds, otherwise
no response
Return type
boolean
raises
: ping was not sent if the hostname could not be resolved
Discover members in local network by sending a UDP broadcast message to
all networks discovered by a getifaddrs() C call.
Returns
true if broadcast was sent, false if getaddrinfo() fails.
Return type
boolean
Update myself().payload and disseminate it along with the member
status.
Increments incarnation.
Parameters:
-
key(string) — a key to set in payload table -
value— auxiliary data
Returns
true
Return type
boolean
Gracefully leave the membership group. The node will be marked with
the left status and no other members will ever try to reconnect it.
Returns
true
Return type
boolean
Returns
true if encryption is enabled, false otherwise.
Return type
boolean
Encryption functions:
Set the key used for low-level message encryption. The key is either
trimmed or padded automatically to be exactly 32 bytes. If the key
value is nil, the encryption is disabled.
The encryption is handled by the crypto.cipher.aes256.cbc Tarantool
module.
For proper communication, all members must be configured to use the same
encryption key. Otherwise, members report either dead or
non-decryptable in their status.
Parameters:
key(string) — encryption key
Returns
nil.
Retrieve the encryption key that is currently in use.
Returns
encryption key or nil if the encryption is disabled.
Return type
string
Subscription functions:
Subscribe for updates in the members table.
Returns
a fiber.cond object broadcasted whenever the members table
changes.
Return type
object
Remove subscription on cond obtained by the
subscribe() function.
The cond's validity is not checked.
Parameters:
cond— thefiber.condobject obtained from subscribe()
Returns
nil.
Below is a list of membership options. They can be set as follows:
options = require('membership.options')options.<option> = <value>
Period of sending direct pings. Denoted as T' in the SWIM protocol.
Time to wait for ACK message after a ping. If a member is late to reply, the indirect ping algorithm is invoked.
Period to perform the anti-entropy synchronization algorithm of the SWIM protocol.
Timeout to mark suspect members as dead.
Number of members to try pinging a suspect indirectly. Denoted as k
in the SWIM protocol.