Skip to content

Commit a3a22cc

Browse files
committed
Resolved some comments
* Renamed pub/sub section to System events, removed paragraph about pub/sub, moved the event definition to Event watchers section * Removed code-block with empty table for the event values * Fixed some refs and links
1 parent 0d00070 commit a3a22cc

File tree

9 files changed

+223
-240
lines changed

9 files changed

+223
-240
lines changed

doc/reference/reference_lua/box.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ with ``box``, with no arguments. The ``box`` module contains:
3434
box_space
3535
box_stat
3636
box_tuple
37-
box_watchers
3837

3938
box_txn_management
4039
box_sql
40+
box_events
4141
box_once
4242
box_snapshot
4343

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
.. _box-watchers:
2+
3+
Event watchers
4+
==============
5+
6+
The ``box`` module contains some features related to event subscriptions, also known as :term:`watchers <watcher>`.
7+
The subscriptions are used to inform a client about server-side :term:`events <event>`.
8+
Each event subscription is defined by a certain key.
9+
10+
.. glossary::
11+
12+
Event
13+
14+
An event is a state change or a system update that triggers the action of other systems.
15+
To read more about the built-in events in Tarantool,
16+
check the :doc:`system events </reference/reference_lua/box_events/system_events>` section.
17+
18+
State
19+
A state is an internally stored key-value pair.
20+
The key is a string.
21+
The value is an arbitrary type that can be encoded as MsgPack.
22+
To update a state, use the ``box.broadcast()`` function.
23+
24+
Watcher
25+
A watcher is a :doc:`callback </book/box/triggers>` that is invoked when a state change occurs.
26+
To register a local watcher, use the ``box.watch()`` function.
27+
To create a remote watcher, use the ``watch()`` function from the ``net.box`` module.
28+
Note that it is possible to register more than one watcher for the same key.
29+
30+
How the watcher works
31+
---------------------
32+
33+
First, you register a watcher.
34+
After that, the watcher callback is invoked for the first time.
35+
In this case, the callback is triggered whether or not the key has already been broadcast.
36+
All subsequent invocations are triggered with the :doc:`box.broadcast() </reference/reference_lua/box_events/broadcast>`
37+
called on the remote host.
38+
If a watcher is subscribed for a key that has not been broadcast yet, the callback is triggered only once,
39+
after the registration of the watcher.
40+
41+
The watcher callback takes two arguments.
42+
The first argument is a name of the key for which it was registered.
43+
The second one contains current key data.
44+
The callback is always invoked in a new fiber. It means that is is okay to yield in it.
45+
A watcher callback is never executed in parallel with itself.
46+
If the key is updated while the watcher callback is running, the callback will be invoked again with the new
47+
value as soon as it returns.
48+
49+
``box.watch`` and ``box.broadcast`` functions can be used before :doc:`box.cfg </reference/reference_lua/box_cfg>`.
50+
51+
Below is a list of all functions and members related to watchers or events.
52+
53+
.. container:: table
54+
55+
.. rst-class:: left-align-column-1
56+
.. rst-class:: left-align-column-2
57+
58+
.. list-table::
59+
:widths: 25 75
60+
:header-rows: 1
61+
62+
* - Name
63+
- Use
64+
65+
* - :doc:`./box_events/watch`
66+
- Create a local watcher.
67+
68+
* - :ref:`conn:watch() <conn-watch>`
69+
- Create a watcher for the remote host.
70+
71+
* - :doc:`./box_events/broadcast`
72+
- Update a state.
73+
74+
* - :ref:`˜Built-in events <system-events>`
75+
- Predefined events in Tarantool
76+
77+
.. toctree::
78+
:hidden:
79+
80+
box_events/watch
81+
box_events/broadcast
82+
box_events/system_events
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
.. _system-events:
2+
3+
System events
4+
=============
5+
6+
Predefined events have a special naming schema -- theirs names always start with the reserved ``box.`` prefix.
7+
It means that you cannot create new events with it.
8+
9+
The system processes the following events:
10+
11+
* ``box.id``
12+
* ``box.status``
13+
* ``box.election``
14+
* ``box.schema``
15+
16+
In response to each event, the server sends back certain ``IPROTO`` fields.
17+
18+
The events are available from the beginning as non-:ref:`MP_NIL <box_protocol-notation>`.
19+
If a watcher subscribes to a system event before it has been broadcast,
20+
it receives an empty table for the event value.
21+
22+
box.id
23+
~~~~~~
24+
25+
Contains :ref:`identification <box_info_info>` of the instance.
26+
Value changes are rare.
27+
28+
* ``id``: the numeric instance ID is unknown before the registration.
29+
For anonymous replicas, the value is ``0`` until they are officially registered.
30+
31+
* ``instance_uuid``: the UUID of the instance never changes after the first
32+
:doc:`box.cfg </reference/reference_lua/box_cfg>`.
33+
The value is unknown before the ``box.cfg`` call.
34+
35+
* ``replicaset_uuid``: the value is unknown until the instance joins a replicaset or boots a new one.
36+
The events start working before that -- right with the start of the listen.
37+
38+
.. code-block:: lua
39+
40+
-- box.id value
41+
{
42+
MP_STR “id”: MP_UINT; box.info.id,
43+
MP_STR “instance_uuid”: MP_UUID; box.info.uuid,
44+
MP_STR “replicaset_uuid”: MP_UUID box.info.cluster.uuid,
45+
}
46+
47+
box.status
48+
~~~~~~~~~~
49+
50+
Contains generic blob about the instance status.
51+
52+
* ``is_ro``: :ref:`indicates the read-only mode <box_introspection-box_info>` or the ``orphan`` status.
53+
* ``is_ro_cfg``: indicates the :ref:`read_only <cfg_basic-read_only>` mode for the instance.
54+
* ``status``: shows the status of an instance.
55+
56+
.. code-block:: lua
57+
58+
{
59+
MP_STR “is_ro”: MP_BOOL box.info.ro,
60+
MP_STR “is_ro_cfg”: MP_BOOL box.cfg.read_only,
61+
MP_STR “status”: MP_STR box.info.status,
62+
}
63+
64+
box.election
65+
~~~~~~~~~~~~
66+
67+
Contains fields of the :doc:`box.info.election </reference/reference_lua/box_info/election>`
68+
that are necessary to find out the most recent writable leader.
69+
70+
* ``term``: shows the current election term.
71+
* ``role``: indicates the election state of the node -- ``leader``, ``follower``, or ``candidate``.
72+
* ``is_ro``: :ref:`indicates the read-only mode <box_introspection-box_info>` or the ``orphan`` status.
73+
* ``leader``: shows the leader node ID in the current term.
74+
75+
.. code-block:: lua
76+
77+
{
78+
MP_STR “term”: MP_UINT box.info.election.term,
79+
MP_STR “role”: MP_STR box.info.election.state,
80+
MP_STR “is_ro”: MP_BOOL box.info.ro,
81+
MP_STR “leader”: MP_UINT box.info.election.leader,
82+
}
83+
84+
box.schema
85+
~~~~~~~~~~
86+
87+
Contains schema-related data.
88+
89+
* ``version``: shows the schema version.
90+
91+
.. code-block:: lua
92+
93+
{
94+
MP_STR “version”: MP_UINT schema_version,
95+
}
96+
97+
Usage example
98+
-------------
99+
100+
.. code-block:: lua
101+
102+
conn = net.box.connect(URI)
103+
-- Subscribe to updates of key 'box.id'
104+
w = conn:watch('box.id', function(key, value)
105+
assert(key == 'box.id')
106+
-- do something with value
107+
end)
108+
-- or to updates of key 'box.status'
109+
w = conn:watch('box.status', function(key, value)
110+
assert(key == 'box.status')
111+
-- do something with value
112+
end)
113+
-- or to updates of key 'box.election'
114+
w = conn:watch('box.election', function(key, value)
115+
assert(key == 'box.election')
116+
-- do something with value
117+
end)
118+
-- or to updates of key 'box.schema'
119+
w = conn:watch('box.schema', function(key, value)
120+
assert(key == 'box.schema')
121+
-- do something with value
122+
end)
123+
-- Unregister the watcher when it's no longer needed.
124+
w:unregister()
125+
126+

doc/reference/reference_lua/box_watchers/watch.rst renamed to doc/reference/reference_lua/box_events/watch.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ box.watch()
1515

1616
To read more about watchers, see the `Functions for watchers <box-watchers>` section.
1717

18+
.. note::
19+
20+
Keep in mind that garbage collection of a watcher handle doesn't lead to the watcher's destruction.
21+
In this case, the watcher remains registered.
22+
It is okay to discard the result of ``watch`` function if the watcher will never be unregistered.
23+
1824
**Example:**
1925

2026
.. code-block:: lua

doc/reference/reference_lua/box_watchers.rst

Lines changed: 0 additions & 71 deletions
This file was deleted.

doc/reference/reference_lua/index.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ This reference covers Tarantool's built-in Lua modules.
5656
uri
5757
xlog
5858
yaml
59-
pub-sub
6059
other
6160
errcodes
6261
debug_facilities

doc/reference/reference_lua/net_box.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ Below is a list of all ``net.box`` functions.
547547

548548
To read more about watchers, see the `Functions for watchers <box-watchers>` section.
549549

550-
The method has the same syntax as the :doc:`box.watch() </reference/reference_lua/box_watchers/broadcast>`
550+
The method has the same syntax as the :doc:`box.watch() </reference/reference_lua/box_events/broadcast>`
551551
function, which is used for subscribing to events locally.
552552

553553
Watchers survive reconnection (see the ``reconnect_after`` connection :ref:`option <net_box-new>`).
@@ -558,6 +558,12 @@ Below is a list of all ``net.box`` functions.
558558
connection ``peer_protocol_features``.
559559
For details, check the :ref:`net.box features table <net_box-new>`.
560560

561+
.. note::
562+
563+
Keep in mind that garbage collection of a watcher handle doesn't lead to the watcher's destruction.
564+
In this case, the watcher remains registered.
565+
It is okay to discard the result of ``watch`` function if the watcher will never be unregistered.
566+
561567
**Example:**
562568

563569
Server:
@@ -575,7 +581,7 @@ Below is a list of all ``net.box`` functions.
575581
-- Subscribe to updates of the 'foo' key.
576582
w = conn:watch('foo', function(key, value)
577583
assert(key == 'foo')
578-
-- do something with value
584+
-- do something with value
579585
end)
580586
-- Unregister the watcher if it is no longer needed.
581587
w:unregister()

0 commit comments

Comments
 (0)