-
Notifications
You must be signed in to change notification settings - Fork 43
Document the watchers and built-in events #2858
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ba2a698
Add new functions to module box
xuniq 7c49745
Update watch and broadcast methods
xuniq d14c852
Update pub/sub
xuniq 8ac6786
Proofread text and add links
xuniq cd26e31
Proofread text, update links
xuniq 6b07a34
Improve text
xuniq abc4741
Update after the review
xuniq 0d00070
Update after the review
xuniq a3a22cc
Resolved some comments
xuniq b357275
Update files after the review
xuniq f2b8dbb
Proofread
patiencedaur 7d721e3
Correct an article
patiencedaur 9611be4
Fix build errors
patiencedaur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
.. _box-watchers: | ||
|
||
Event watchers | ||
============== | ||
|
||
The ``box`` module contains some features related to event subscriptions, also known as :term:`watchers <watcher>`. | ||
The subscriptions are used to inform the client about server-side :term:`events <event>`. | ||
Each event subscription is defined by a certain key. | ||
|
||
.. glossary:: | ||
|
||
Event | ||
|
||
An event is a state change or a system update that triggers the action of other systems. | ||
To read more about built-in events in Tarantool, | ||
check the :doc:`system events </reference/reference_lua/box_events/system_events>` section. | ||
|
||
State | ||
A state is an internally stored key-value pair. | ||
The key is a string. | ||
The value is an arbitrary type that can be encoded as MsgPack. | ||
To update a state, use the ``box.broadcast()`` function. | ||
|
||
Watcher | ||
A watcher is a :doc:`callback </book/box/triggers>` that is invoked when a state change occurs. | ||
To register a local watcher, use the ``box.watch()`` function. | ||
To create a remote watcher, use the ``watch()`` function from the ``net.box`` module. | ||
Note that it is possible to register more than one watcher for the same key. | ||
|
||
How a watcher works | ||
------------------- | ||
|
||
First, you register a watcher. | ||
After that, the watcher callback is invoked for the first time. | ||
In this case, the callback is triggered whether or not the key has already been broadcast. | ||
All subsequent invocations are triggered with :doc:`box.broadcast() </reference/reference_lua/box_events/broadcast>` | ||
called on the remote host. | ||
If a watcher is subscribed for a key that has not been broadcast yet, the callback is triggered only once, | ||
after the registration of the watcher. | ||
|
||
The watcher callback takes two arguments. | ||
The first argument is the name of the key for which it was registered. | ||
The second one contains current key data. | ||
The callback is always invoked in a new fiber. It means that it is allowed to yield in it. | ||
A watcher callback is never executed in parallel with itself. | ||
If the key is updated while the watcher callback is running, the callback will be invoked again with the new | ||
value as soon as it returns. | ||
|
||
``box.watch`` and ``box.broadcast`` functions can be used before :doc:`box.cfg </reference/reference_lua/box_cfg>`. | ||
|
||
Below is a list of all functions and pages related to watchers or events. | ||
|
||
.. container:: table | ||
|
||
.. rst-class:: left-align-column-1 | ||
.. rst-class:: left-align-column-2 | ||
|
||
.. list-table:: | ||
:widths: 25 75 | ||
:header-rows: 1 | ||
|
||
* - Name | ||
- Use | ||
|
||
* - :doc:`./box_events/watch` | ||
- Create a local watcher. | ||
|
||
* - :ref:`conn:watch() <conn-watch>` | ||
- Create a watcher for the remote host. | ||
|
||
* - :doc:`./box_events/broadcast` | ||
- Update a state. | ||
|
||
* - :ref:`Built-in events <system-events>` | ||
- Predefined events in Tarantool | ||
|
||
.. toctree:: | ||
:hidden: | ||
|
||
box_events/watch | ||
box_events/broadcast | ||
box_events/system_events |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
.. _box-broadcast: | ||
|
||
================================================================================ | ||
box.broadcast() | ||
================================================================================ | ||
|
||
.. function:: box.broadcast(key, value) | ||
|
||
Update the value of a particular key and notify all key watchers of the update. | ||
|
||
:param string key: key name of the event to subscribe to | ||
:param value: any data that can be encoded in MsgPack | ||
|
||
:return: none | ||
|
||
**Possible errors:** | ||
|
||
* The value can't be encoded as MsgPack. | ||
* The key refers to a ``box.`` system event | ||
|
||
**Example:** | ||
|
||
.. code-block:: lua | ||
|
||
-- Broadcast value 42 for the 'foo' key. | ||
box.broadcast('foo', 42) | ||
|
||
|
121 changes: 121 additions & 0 deletions
121
doc/reference/reference_lua/box_events/system_events.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
.. _system-events: | ||
|
||
System events | ||
============= | ||
|
||
Predefined events have a special naming schema -- theirs names always start with the reserved ``box.`` prefix. | ||
It means that you cannot create new events with it. | ||
|
||
The system processes the following events: | ||
|
||
* ``box.id`` | ||
* ``box.status`` | ||
* ``box.election`` | ||
* ``box.schema`` | ||
|
||
In response to each event, the server sends back certain ``IPROTO`` fields. | ||
|
||
The events are available from the beginning as non-:ref:`MP_NIL <box_protocol-notation>`. | ||
If a watcher subscribes to a system event before it has been broadcast, | ||
it receives an empty table for the event value. | ||
|
||
The event is generated when there is a change in any of the values listed in the event. | ||
For example, see the parameters in the ``box.id`` event below -- ``id``, ``instance_uuid``, and ``replicaset_uuid``. | ||
Suppose the ``ìd`` value (``box.info.id``) has changed. | ||
This triggers the ``box.info`` event, which states that the value of ``box.info.id`` has changed, | ||
while ``box.info.uuid`` and ``box.info.cluster.uuid`` remain the same. | ||
|
||
box.id | ||
~~~~~~ | ||
|
||
Contains :ref:`identification <box_info_info>` of the instance. | ||
Value changes are rare. | ||
|
||
* ``id``: the numeric instance ID is unknown before the registration. | ||
For anonymous replicas, the value is ``0`` until they are officially registered. | ||
|
||
* ``instance_uuid``: the UUID of the instance never changes after the first | ||
:doc:`box.cfg </reference/reference_lua/box_cfg>`. | ||
The value is unknown before the ``box.cfg`` call. | ||
|
||
* ``replicaset_uuid``: the value is unknown until the instance joins a replicaset or boots a new one. | ||
|
||
.. code-block:: none | ||
|
||
-- box.id value | ||
{ | ||
MP_STR “id”: MP_UINT; box.info.id, | ||
MP_STR “instance_uuid”: MP_UUID; box.info.uuid, | ||
MP_STR “replicaset_uuid”: MP_UUID box.info.cluster.uuid, | ||
} | ||
|
||
box.status | ||
~~~~~~~~~~ | ||
|
||
Contains generic information about the instance status. | ||
|
||
* ``is_ro``: :ref:`indicates the read-only mode <box_introspection-box_info>` or the ``orphan`` status. | ||
* ``is_ro_cfg``: indicates the :ref:`read_only <cfg_basic-read_only>` mode for the instance. | ||
* ``status``: shows the status of an instance. | ||
|
||
.. code-block:: none | ||
|
||
{ | ||
MP_STR “is_ro”: MP_BOOL box.info.ro, | ||
MP_STR “is_ro_cfg”: MP_BOOL box.cfg.read_only, | ||
MP_STR “status”: MP_STR box.info.status, | ||
} | ||
|
||
box.election | ||
~~~~~~~~~~~~ | ||
|
||
Contains fields of :doc:`box.info.election </reference/reference_lua/box_info/election>` | ||
that are necessary to find out the most recent writable leader. | ||
|
||
* ``term``: shows the current election term. | ||
* ``role``: indicates the election state of the node -- ``leader``, ``follower``, or ``candidate``. | ||
* ``is_ro``: :ref:`indicates the read-only mode <box_introspection-box_info>` or the ``orphan`` status. | ||
* ``leader``: shows the leader node ID in the current term. | ||
|
||
.. code-block:: none | ||
|
||
{ | ||
MP_STR “term”: MP_UINT box.info.election.term, | ||
MP_STR “role”: MP_STR box.info.election.state, | ||
MP_STR “is_ro”: MP_BOOL box.info.ro, | ||
MP_STR “leader”: MP_UINT box.info.election.leader, | ||
} | ||
|
||
box.schema | ||
~~~~~~~~~~ | ||
|
||
Contains schema-related data. | ||
|
||
* ``version``: shows the schema version. | ||
|
||
.. code-block:: none | ||
|
||
{ | ||
MP_STR “version”: MP_UINT schema_version, | ||
} | ||
|
||
Usage example | ||
------------- | ||
|
||
.. code-block:: lua | ||
|
||
local conn = net.box.connect(URI) | ||
local log = require('log') | ||
-- Subscribe to updates of key 'box.id' | ||
local w = conn:watch('box.id', function(key, value) | ||
assert(key == 'box.id') | ||
log.info("The box.id value is '%s'", value) | ||
end) | ||
|
||
If you want to unregister the watcher when it's no longer needed, use the following command: | ||
|
||
.. code-block:: lua | ||
|
||
w:unregister() | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
.. _box-watch: | ||
|
||
================================================================================ | ||
box.watch() | ||
================================================================================ | ||
|
||
.. function:: box.watch(key, func) | ||
|
||
Subscribe to events broadcast by a local host. | ||
|
||
:param string key: key name of the event to subscribe to | ||
:param function func: callback to invoke when the key value is updated | ||
|
||
:return: a watcher handle. The handle consists of one method -- ``unregister()``, which unregisters the watcher. | ||
|
||
To read more about watchers, see the :ref:`Functions for watchers <box-watchers>` section. | ||
|
||
.. note:: | ||
|
||
Keep in mind that garbage collection of a watcher handle doesn't lead to the watcher's destruction. | ||
In this case, the watcher remains registered. | ||
It is okay to discard the result of ``watch`` function if the watcher will never be unregistered. | ||
|
||
**Example:** | ||
|
||
Server: | ||
|
||
.. code-block:: lua | ||
|
||
xuniq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
-- Broadcast value 42 for the 'foo' key. | ||
box.broadcast('foo', 42) | ||
|
||
Client: | ||
|
||
.. code-block:: lua | ||
|
||
conn = require('net.box').connect(URI) | ||
local log = require('log') | ||
-- Subscribe to updates of the 'foo' key. | ||
local w = box.watch('foo', function(key, value) | ||
assert(key == 'foo') | ||
log.info("The box.id value is '%d'", value) | ||
end) | ||
|
||
If you don't need the watcher anymore, you can unregister it using the command below: | ||
|
||
.. code-block:: lua | ||
|
||
w:unregister() | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.