Skip to content

Commit 9edc7c3

Browse files
committed
Update after the review
1 parent f541463 commit 9edc7c3

File tree

16 files changed

+324
-140
lines changed

16 files changed

+324
-140
lines changed

doc/dev_guide/internals/box_protocol.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ The binary protocol provides complete access to Tarantool functionality, includi
2020
asynchronously via the same connection
2121
* response format that supports zero-copy writes
2222

23+
.. note::
24+
25+
Since version :doc:`2.11.0 </release/2.11.0>`, you can use the :ref:`box.iproto <box_iproto>` submodule to access
26+
IPROTO constants and features from Lua. The submodule enables to :ref:`send arbitrary IPROTO packets <reference_lua-box_iproto_send>`
27+
over the session's socket and :ref:`override the behavior <reference_lua-box_iproto_override>` for all IPROTO
28+
request types (including the :ref:`unknown <box_iproto-unknown>` types).
29+
2330
.. toctree::
2431
:maxdepth: 1
2532

doc/dev_guide/internals/iproto/keys.rst

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ General
5252
- 0x00 |br| MP_UINT
5353
- Request type or response type
5454

55-
* - :ref:`IPROTO_UNKNOWN <internals-iproto-keys-unknown>`
56-
- -1 |br| MP_UINT
57-
- Unknown request type. The key is used only for IPROTO handler overriding.
58-
5955
* - :ref:`IPROTO_ERROR <internals-iproto-keys-error>`
6056
- 0x52 |br| :ref:`MP_ERROR <msgpack_ext-error>`
6157
- Error response
@@ -598,16 +594,6 @@ See requests and responses for :ref:`client-server communication <internals-requ
598594
:ref:`events and subscriptions <box-protocol-watchers>`,
599595
:ref:`streams and interactive transactions <internals-iproto-streams>`.
600596

601-
.. _internals-iproto-keys-unknown:
602-
603-
IPROTO_UNKNOWN
604-
~~~~~~~~~~~~~~
605-
606-
Code: -1.
607-
608-
Unknown request type. The constant is used for overriding the unknown request handler.
609-
Learn more: :ref:`box.iproto.override() <reference_lua-box_iproto_override>`.
610-
611597
.. _internals-iproto-keys-error:
612598

613599
IPROTO_ERROR

doc/dev_guide/internals/iproto/requests.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ Overview
3838
- 0x8XXX |br| MP_INT
3939
- Error response
4040

41+
* - :ref:`IPROTO_UNKNOWN <internals-iproto-keys-unknown>`
42+
- -1 |br| MP_UINT
43+
- Unknown request type. The constant is used only for IPROTO handler :ref:`overriding <reference_lua-box_iproto_override>`.
44+
4145
* - :ref:`IPROTO_SELECT <box_protocol-select>`
4246
- 0x01
4347
- :ref:`Select <box_space-select>` request
@@ -126,6 +130,18 @@ constants for return codes.
126130
To learn more about error responses,
127131
check the section :ref:`Request and response format <box_protocol-responses_error>`.
128132

133+
.. _internals-iproto-unknown:
134+
135+
IPROTO_UNKNOWN
136+
~~~~~~~~~~~~~~
137+
138+
Since :doc:`2.11.0 </release/2.11.0>`.
139+
140+
Code: -1.
141+
142+
Unknown request type. The constant is used for overriding an unknown IPROTO request handler.
143+
Learn more: :ref:`box.iproto.override() <reference_lua-box_iproto_override>` and :ref:`box_box_iproto_override <box_iproto_override>`.
144+
129145
.. _box_protocol-select:
130146

131147
IPROTO_SELECT

doc/dev_guide/reference_capi/box.rst

Lines changed: 107 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@
247247
248248
.. _box_box_iproto_send:
249249
250-
.. c:function:: int box_iproto_send(uint64_t sid, char *header, char *header_end, char *body, char *body_end)
250+
.. c:function:: int box_iproto_send(uint64_t sid, char *header, char *header_end[, char *body, char *body_end])
251251
252252
Since version :doc:`2.11.0 </release/2.11.0>`.
253253
Send an :ref:`IPROTO <internals-iproto-format>` packet over the session's socket with the given MsgPack header
@@ -258,7 +258,8 @@
258258
:param uint32_t sid: IPROTO session identifier (see :ref:`box_session_id() <box_box_session_id>`)
259259
:param char* header: a MsgPack-encoded header
260260
:param char* header_end: end of a header encoded as MsgPack
261-
:param char* body: a MsgPack-encoded body
261+
:param char* body: a MsgPack-encoded body. If the ``body`` and ``body_end`` parameters are omitted, the packet
262+
consists of the header only.
262263
:param char* body_end: end of a body encoded as MsgPack
263264
264265
:return: 0 on success
@@ -276,24 +277,62 @@
276277
277278
For details, see `src/box/errcode.h <https://github.com/tarantool/tarantool/blob/master/src/box/errcode.h>`__.
278279
280+
**Example**
281+
282+
.. code-block:: c
283+
284+
/* IPROTO constants are not exported to C.
285+
* That is, the user encodes them by himself.
286+
*/
287+
#define IPROTO_REQUEST_TYPE 0x00
288+
#define IPROTO_OK 0x00
289+
#define IPROTO_SYNC 0x01
290+
#define IPROTO_SCHEMA_VERSION 0x05
291+
#define IPROTO_DATA 0x30
292+
293+
char buf[256] = {};
294+
char *header = buf;
295+
char *header_end = header;
296+
header_end = mp_encode_map(header_end, 3);
297+
header_end = mp_encode_uint(header_end, IPROTO_REQUEST_TYPE);
298+
header_end = mp_encode_uint(header_end, IPROTO_OK);
299+
header_end = mp_encode_uint(header_end, IPROTO_SYNC);
300+
header_end = mp_encode_uint(header_end, 10);
301+
header_end = mp_encode_uint(header_end, IPROTO_SCHEMA_VERSION);
302+
header_end = mp_encode_uint(header_end, box_schema_version());
303+
304+
char *body = header_end;
305+
char *body_end = body;
306+
body_end = mp_encode_map(body_end, 1);
307+
body_end = mp_encode_uint(body_end, IPROTO_DATA);
308+
body_end = mp_encode_uint(body_end, 1);
309+
310+
/* The packet contains both the header and body. */
311+
box_iproto_send(box_session_id(), header, header_end, body, body_end);
312+
313+
/* The packet contains the header only. */
314+
box_iproto_send(box_session_id(), header, header_end, NULL, NULL);
315+
279316
.. _box_box_iproto_override:
280317
281318
.. c:function:: void box_iproto_override(uint32_t request_type, iproto_handler_t handler, iproto_handler_destroy_t destroy, void *ctx)
282319
283320
Since version :doc:`2.11.0 </release/2.11.0>`.
284-
Sets an IPROTO request handler with the provided context for the given request type.
321+
Set a new IPROTO request handler with the provided context for the given request type.
285322
The function yields.
286323
287-
Possible values:
324+
:param uint32_t request_type: IPROTO request type code (for example, ``IPROTO_SELECT``).
325+
For details, check :ref:`Client-server requests and responses <internals-requests_responses>`.
326+
327+
To override the handler of an unknown request type, use the :ref:`IPROTO_UNKNOWN <internals-iproto-keys-unknown>` type code.
288328
289-
* a type code from the :ref:`box.iproto.type <reference_lua-box_iproto_type>` (except
290-
``box.iproto.type.UNKNOWN``) -- override the existing request type.
329+
:param iproto_handler_t handler: IPROTO request handler. To reset the request handler, set the ``handler`` parameter to ``NULL``.
330+
See the full parameter description in the :ref:`Handler function <box_box_iproto_override-handler>` section.
291331
292-
* ``box.iproto.type.UNKNOWN`` -- override an unknown request type.
332+
:param iproto_handler_destroy_t destroy: IPROTO request handler destructor. The destructor is called when the
333+
corresponding handler is removed. See the full parameter description
334+
in the :ref:`Handler destructor function <box_box_iproto_override-destroy>` section.
293335
294-
:param uint32_t request_type: request type code from the ``iproto_type`` enumeration
295-
:param iproto_handler_t handler: IPROTO request handler
296-
:param iproto_handler_destroy_t destroy: IPROTO request handler destructor
297336
:param void* ctx: a context passed to the ``handler`` and ``destroy`` callbacks
298337
299338
:return: 0 on success
@@ -304,7 +343,64 @@ Possible values:
304343
305344
**Possible errors:**
306345
346+
If the Lua handler throws an exception, the behavior is similar to a remote procedure call.
347+
The following errors are returned to the client over IPROTO (see `src/lua/utils.h <https://github.com/tarantool/tarantool/blob/dec0e0221e183fa972efa65bb0fb658112f2196f/src/lua/utils.h#L366-L371>`__):
348+
307349
* :errcode:`ER_PROC_LUA` -- the exception is thrown, diagnostic is not set.
308350
* diagnostics from ``src/box/errcode.h`` -- the exception is thrown, diagnostic is set.
309351
310-
For details, see `src/box/errcode.h <https://github.com/tarantool/tarantool/blob/master/src/box/errcode.h>`__.
352+
For details, see `src/box/errcode.h <https://github.com/tarantool/tarantool/blob/master/src/box/errcode.h>`__.
353+
354+
.. _box_box_iproto_override-handler:
355+
356+
**Handler function**
357+
358+
The signature of a handler function (the ``handler`` parameter):
359+
360+
.. code-block:: c
361+
362+
enum iproto_handler_status {
363+
IPROTO_HANDLER_OK,
364+
IPROTO_HANDLER_ERROR,
365+
IPROTO_HANDLER_FALLBACK,
366+
}
367+
368+
typedef enum iproto_handler_status
369+
(*iproto_handler_t)(const char *header, const char *header_end,
370+
const char *body, const char *body_end, void *ctx);
371+
372+
where:
373+
374+
* ``header``(const char*) -- a MsgPack-encoded header
375+
* ``header_end``(const char*) -- end of a header encoded as MsgPack
376+
* ``body``(const char*) -- a MsgPack-encoded body
377+
* ``header_end``(const char*) -- end of a body encoded as MsgPack
378+
379+
The handler returns a status code. Possible statuses:
380+
381+
* ``IPROTO_REQUEST_HANDLER_OK`` -- success
382+
* ``IPROTO_REQUEST_HANDLER_ERROR`` -- error, diagnostic must be set by handler
383+
* ``IPROTO_REQUEST_HANDLER_FALLBACK`` -- fallback to the system handler
384+
385+
.. _box_box_iproto_override-destroy:
386+
387+
**Handler destructor function**
388+
389+
The signature of a destructor function (the ``destroy`` parameter):
390+
391+
.. code-block:: c
392+
393+
typedef void (*iproto_handler_destroy_t)(void *ctx);
394+
395+
where:
396+
397+
* ``ctx`` (void*): the context provided by ``box_iproto_override()`` function.
398+
399+
**Examples**
400+
401+
.. code-block:: c
402+
403+
box_iproto_override(1000, iproto_request_handler_с, NULL)
404+
box_iproto_override(IPROTO_SELECT, iproto_request_handler_с, (uintptr_t)23)
405+
box_iproto_override(IPROTO_SELECT, NULL, NULL)
406+
box_iproto_override(IPROTO_UNKNOWN, iproto_unknown_request_handler_с, &ctx)

doc/reference/reference_lua/box_iproto.rst

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ Submodule box.iproto
66
Since :doc:`2.11.0 </release/2.11.0>`.
77

88
The ``box.iproto`` submodule provides the ability to work with the network subsystem of Tarantool.
9-
It enables to extend the :ref:`binary protocol <box_protocol>` functionality from the Lua libraries.
9+
It enables to extend :ref:`IPROTO <box_protocol>` functionality from the Lua libraries.
1010
With this submodule, you can:
1111

1212
* :ref:`parse the unknown IPROTO requests types <reference_lua-box_iproto_override>`
1313
* :ref:`send arbitrary IPROTO packets <reference_lua-box_iproto_send>`
14-
* :ref:`override the behavior <reference_lua-box_iproto_override>` of the existing request types in binary protocol
14+
* :ref:`override the behavior <reference_lua-box_iproto_override>` of the existing request types in the binary protocol
1515

1616
The submodule exports all IPROTO :ref:`constants <internals-box_protocol>` and :ref:`features <internals-iproto-keys-features>` to Lua.
1717

@@ -20,7 +20,7 @@ The submodule exports all IPROTO :ref:`constants <internals-box_protocol>` and :
2020
IPROTO constants
2121
----------------
2222

23-
The IPROTO constants in the ``box.iproto`` namespace are written in the upper case without the ``IPROTO_`` prefix.
23+
IPROTO constants in the ``box.iproto`` namespace are written in uppercase letters without the ``IPROTO_`` prefix.
2424
The constants are divided into several groups:
2525

2626
* :ref:`key <reference_lua-box_iproto_key>` (:ref:`IPROTO_SYNC <internals-iproto-keys-sync>`, :ref:`IPROTO_REQUEST_TYPE <internals-iproto-keys-request_type>`)
@@ -30,7 +30,7 @@ The constants are divided into several groups:
3030
* :ref:`metadata key <reference_lua-box_iproto_metadata>` (:ref:`IPROTO_FIELD_IS_NULLABLE <internals-iproto-keys-sql-specific>`)
3131
* :ref:`RAFT key <reference_lua-box_iproto_raft>` (:ref:`IPROTO_TERM <internals-iproto-keys-term>`)
3232

33-
Each group is located in the corresponding subnamespace without prefix.
33+
Each group is located in the corresponding subnamespace without the prefix.
3434
For example:
3535

3636
.. code-block:: lua
@@ -39,9 +39,9 @@ For example:
3939
-- ...
4040
box.iproto.type.SELECT = 1
4141
-- ...
42-
box.iproto.flag.COMMIT = 0x01
42+
box.iproto.flag.COMMIT = 1
4343
-- ...
44-
box.iproto.ballot_key.VCLOCK = 0x02
44+
box.iproto.ballot_key.VCLOCK = 2
4545
-- ...
4646
box.iproto.metadata_key.IS_NULLABLE = 3
4747
-- ...
@@ -61,7 +61,7 @@ The submodule exports:
6161

6262
**Example**
6363

64-
The example converts the feature names from the ``box.iproto.protocol_features`` set into the code numbers:
64+
The example converts the feature names from the ``box.iproto.protocol_features`` set into codes:
6565

6666
.. code-block:: lua
6767
@@ -71,15 +71,26 @@ The example converts the feature names from the ``box.iproto.protocol_features``
7171
transactions = true,
7272
error_extension = true,
7373
watchers = true,
74+
pagination = true,
7475
}
7576
7677
-- Convert the feature names into codes
7778
features = {}
7879
for name in pairs(box.iproto.protocol_features) do
7980
table.insert(features, box.iproto.feature[name])
8081
end
81-
features -- [0, 1, 2, 3]
82+
features -- [0, 1, 2, 3, 4]
8283
84+
.. _box_iproto-unknown:
85+
86+
Handling the unknown IPROTO request types
87+
-----------------------------------------
88+
89+
Every IPROTO request has a static handler.
90+
That is, before version :doc:`2.11.0 </release/2.11.0>`, any unknown request raised an error.
91+
Since :doc:`2.11.0 </release/2.11.0>`, a new request type is introduced -- :ref:`IPROTO_UNKNOWN <internals-iproto-keys-unknown>`.
92+
This type is used to override the handlers of the unknown IPROTO requests types. For details, see
93+
:ref:`box.iproto.override() <reference_lua-box_iproto_override>` and :ref:`box_iproto_override <box_box_iproto_override>` functions.
8394

8495
.. _box_iproto-reference:
8596

@@ -94,19 +105,19 @@ The table lists all available functions and data of the submodule:
94105
.. rst-class:: left-align-column-2
95106

96107
.. list-table::
97-
:widths: 25 75
108+
:widths: 30 70
98109
:header-rows: 1
99110

100111
* - Name
101112
- Use
102113

103-
* - :doc:`./box_iproto/keys`
114+
* - :doc:`./box_iproto/key`
104115
- Request keys
105116

106-
* - :doc:`./box_iproto/request_types`
117+
* - :doc:`./box_iproto/request_type`
107118
- Request types
108119

109-
* - :doc:`./box_iproto/flags`
120+
* - :doc:`./box_iproto/flag`
110121
- Flags from the :ref:`IPROTO_FLAGS <box_protocol-flags>` key
111122

112123
* - :doc:`./box_iproto/ballot`
@@ -116,7 +127,7 @@ The table lists all available functions and data of the submodule:
116127
- Keys nested in the :ref:`IPROTO_METADATA <internals-iproto-keys-metadata>` key
117128

118129
* - :doc:`./box_iproto/raft`
119-
- Keys from the ``IPROTO_RAFT*`` requests
130+
- Keys from the ``IPROTO_RAFT_`` requests
120131

121132
* - :doc:`./box_iproto/protocol_version`
122133
- Current IPROTO protocol version
@@ -127,18 +138,17 @@ The table lists all available functions and data of the submodule:
127138
* - :doc:`./box_iproto/feature`
128139
- IPROTO protocol :ref:`features <internals-iproto-keys-features>`
129140

130-
* - :doc:`./box_iproto/override`
131-
- Set an IPROTO request handler callbacks for the given request type
141+
* - :doc:`./box_iproto/override`
142+
- Set a new IPROTO request handler callback for the given request type
132143

133-
* - :doc:`./box_iproto/send`
144+
* - :doc:`./box_iproto/send`
134145
- Send an IPROTO packet over the session's socket
135146

136147

137148
.. toctree::
138149
:hidden:
139150

140151
box_iproto/key
141-
box_iproto/request_key
142152
box_iproto/request_type
143153
box_iproto/flag
144154
box_iproto/ballot

doc/reference/reference_lua/box_iproto/ballot.rst

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ box.iproto.ballot_key
1010
The ``box.iproto.ballot_key`` namespace contains the keys from the :ref:`IPROTO_BALLOT <box_protocol-ballots>` requests.
1111
Learn more: :ref:`IPROTO_BALLOT keys <internals-iproto-keys-ballot>`.
1212

13-
**Example**
13+
**Example**
1414

15-
.. code-block:: lua
15+
.. code-block:: lua
16+
17+
tarantool> box.iproto.ballot_key.IS_RO_CFG
18+
---
19+
- 1
20+
...
21+
tarantool> box.iproto.ballot_key.VCLOCK
22+
---
23+
- 2
24+
...
1625
17-
box.iproto.ballot_key.IS_RO_CFG = 0x01
18-
-- ...
19-
box.iproto.ballot_key.VCLOCK = 0x02
20-
-- ...

0 commit comments

Comments
 (0)