|
| 1 | +.. _vshard-pubsub: |
| 2 | + |
| 3 | +Pub/sub system and its events |
| 4 | +============================= |
| 5 | + |
| 6 | +Pub/sub is a notification system for Tarantool events. |
| 7 | +It is related to one-time subscriptions. |
| 8 | + |
| 9 | +Events that the system will process: |
| 10 | + |
| 11 | +box.id |
| 12 | +box.status |
| 13 | +box.election |
| 14 | +box.schema |
| 15 | + |
| 16 | +As a reaction to each event, the server sends back specific IPROTO fields. |
| 17 | + |
| 18 | +Built-in events for pub/sub |
| 19 | +--------------------------- |
| 20 | + |
| 21 | +The important purpose of the built-in events is to learn who is the |
| 22 | +master, unless it is defined in an application specific way. Knowing who |
| 23 | +is the master is necessary to send changes to a correct instance, and |
| 24 | +probably make reads of the most actual data if it is important. Also |
| 25 | +defined more built-in events for other mutable properties like leader |
| 26 | +state change, his election role and election term, schema version change |
| 27 | +and instance state. |
| 28 | + |
| 29 | +Built-in events have a special naming schema - their name always starts |
| 30 | +with box.. The prefix is reserved for built-in events. Creating new events |
| 31 | +with this prefix is banned. Below is a list of all the events + their names |
| 32 | +and values: |
| 33 | + |
| 34 | +.. container:: table |
| 35 | + |
| 36 | + .. list-table:: |
| 37 | + :widths: 20 40 40 |
| 38 | + |
| 39 | + * - Built-in event |
| 40 | + - Description |
| 41 | + - Value |
| 42 | + |
| 43 | + * - box.id |
| 44 | + - Identification of the instance. Changes are extra rare. Some |
| 45 | + values never change or change only once. For example, instance UUID never |
| 46 | + changes after the first box.cfg. But is not known before box.cfg is called. |
| 47 | + Replicaset UUID is unknown until the instance joins to a replicaset or |
| 48 | + bootsa new one, but the events are supposed to start working before that - |
| 49 | + right at listen launch. Instance numeric ID is known only after |
| 50 | + registration. On anonymous replicas is 0 until they are registered officially. |
| 51 | + - .. code-block:: lua |
| 52 | + |
| 53 | + { |
| 54 | + MP_STR “id”: MP_UINT; box.info.id, |
| 55 | + MP_STR “instance_uuid”: MP_UUID; box.info.uuid, |
| 56 | + MP_STR “replicaset_uuid”: MP_UUID box.info.cluster.uuid, |
| 57 | + } |
| 58 | + |
| 59 | + * - box.status |
| 60 | + - Generic blob about instance status. It is most commonly used |
| 61 | + and not frequently changed config options and box.info fields.] |
| 62 | + - .. code-block:: lua |
| 63 | + |
| 64 | + { |
| 65 | + MP_STR “is_ro”: MP_BOOL box.info.ro, |
| 66 | + MP_STR “is_ro_cfg”: MP_BOOL box.cfg.read_only, |
| 67 | + MP_STR “status”: MP_STR box.info.status, |
| 68 | + } |
| 69 | + |
| 70 | + * - box.election |
| 71 | + - All the needed parts of box.info.election needed to find who is the most recent writable leader. |
| 72 | + - .. code-block:: lua |
| 73 | + |
| 74 | + { |
| 75 | + MP_STR “term”: MP_UINT box.info.election.term, |
| 76 | + MP_STR “role”: MP_STR box.info.election.state, |
| 77 | + MP_STR “is_ro”: MP_BOOL box.info.ro, |
| 78 | + MP_STR “leader”: MP_UINT box.info.election.leader, |
| 79 | + } |
| 80 | + |
| 81 | + * - box.schema |
| 82 | + - Schema-related data. Currently it is only version. |
| 83 | + - .. code-block:: lua |
| 84 | + |
| 85 | + { |
| 86 | + MP_STR “version”: MP_UINT schema_version, |
| 87 | + } |
| 88 | + |
| 89 | +Built-in events can't be override. Meaning, users can't be able to call |
| 90 | +box.broadcast(‘box.id’, any_data) etc. |
| 91 | + |
| 92 | +The events are available from the very beginning as not MP_NIL. It's |
| 93 | +necessary for supported local subscriptions. Otherwise, there is no way to detect |
| 94 | +whether an event is even supported at all by this Tarantool version. If |
| 95 | +events are broadcast before box.cfg{}, then the following values will |
| 96 | +available: |
| 97 | +box.id = {} |
| 98 | +box.schema = {} |
| 99 | +box.status = {} |
| 100 | +box.election = {} |
| 101 | + |
| 102 | +This way, the users will be able to distinguish an event being not supported |
| 103 | +at all from ``box.cfg{}`` being not called yet. Otherwise they would need to |
| 104 | +parse ``_TARANTOOL`` version string locally and peer_version in net.box. |
| 105 | + |
| 106 | +Usage example |
| 107 | +------------- |
| 108 | + |
| 109 | +.. code-block:: lua |
| 110 | +
|
| 111 | + conn = net.box.connect(URI) |
| 112 | + -- Subscribe to updates of key 'box.id' |
| 113 | + w = conn:watch('box.id', function(key, value) |
| 114 | + assert(key == 'box.id') |
| 115 | + -- do something with value |
| 116 | + end) |
| 117 | + -- or to updates of key 'box.status' |
| 118 | + w = conn:watch('box.status', function(key, value) |
| 119 | + assert(key == 'box.status') |
| 120 | + -- do something with value |
| 121 | + end) |
| 122 | + -- or to updates of key 'box.election' |
| 123 | + w = conn:watch('box.election', function(key, value) |
| 124 | + assert(key == 'box.election') |
| 125 | + -- do something with value |
| 126 | + end) |
| 127 | + -- or to updates of key 'box.schema' |
| 128 | + w = conn:watch('box.schema', function(key, value) |
| 129 | + assert(key == 'box.schema') |
| 130 | + -- do something with value |
| 131 | + end) |
| 132 | + -- Unregister the watcher when it's no longer needed. |
| 133 | + w:unregister() |
| 134 | +
|
| 135 | +
|
0 commit comments