Skip to content

Commit b9550f1

Browse files
CuriousGeorgiylocker
authored andcommitted
box: support space and index names in IPROTO requests
Add support for accepting IPROTO requests with space or index name instead of identifier (name is preferred over identifier to disambiguate missing identifiers from zero identifiers): mark space identifier request key as present upon encountering space name, and delay resolution of identifier until request gets to transaction thread. Add support for sending DML requests from net.box connection objects with disabled schema fetching by manually specifying space or index name or identifier: when schema fetching is disabled, the space and index tables of connections return wrapper tables that store necessary context (space or index name or identifier, determined by type, connection object and space for indexes) for performing requests. The space and index tables cache the wrapper table they return. Closes #8146 @TarantoolBot document Title: Space and index name in IPROTO requests Refer to design document for details: https://www.notion.so/tarantool/Schemafull-IPROTO-cc315ad6bdd641dea66ad854992d8cbf?pvs=4#f4d4b3fa2b3646f1949319866428b6c0
1 parent bf086dc commit b9550f1

17 files changed

+560
-69
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## feature/box
2+
3+
* Added support for accepting IPROTO requests with a space or index name instead
4+
of an identifier (gh-8146).

src/box/iproto.cc

+42-2
Original file line numberDiff line numberDiff line change
@@ -1666,8 +1666,8 @@ iproto_msg_decode(struct iproto_msg *msg, struct cmsg_hop **route)
16661666
assert(type < sizeof(iproto_thread->dml_route) /
16671667
sizeof(*iproto_thread->dml_route));
16681668
*route = iproto_thread->dml_route[type];
1669-
if (xrow_decode_dml(&msg->header, &msg->dml,
1670-
dml_request_key_map(type)))
1669+
if (xrow_decode_dml_iproto(&msg->header, &msg->dml,
1670+
dml_request_key_map(type)) != 0)
16711671
return -1;
16721672
/*
16731673
* In contrast to replication requests, for a client request
@@ -2143,6 +2143,42 @@ tx_process_rollback(struct cmsg *m)
21432143
tx_end_msg(msg, &header);
21442144
}
21452145

2146+
/*
2147+
* In case the request does not contain a space or identifier but contains a
2148+
* corresponding name, tries to resolve the name.
2149+
*/
2150+
static int
2151+
tx_resolve_space_and_index_name(struct request *dml)
2152+
{
2153+
struct space *space = NULL;
2154+
if (dml->space_name != NULL) {
2155+
space = space_by_name(dml->space_name, dml->space_name_len);
2156+
if (space == NULL) {
2157+
diag_set(ClientError, ER_NO_SUCH_SPACE,
2158+
tt_cstr(dml->space_name, dml->space_name_len));
2159+
return -1;
2160+
}
2161+
dml->space_id = space->def->id;
2162+
}
2163+
if ((dml->type == IPROTO_SELECT || dml->type == IPROTO_UPDATE ||
2164+
dml->type == IPROTO_DELETE) && dml->index_name != NULL) {
2165+
if (space == NULL)
2166+
space = space_cache_find(dml->space_id);
2167+
if (space == NULL)
2168+
return -1;
2169+
struct index *idx = space_index_by_name(space, dml->index_name,
2170+
dml->index_name_len);
2171+
if (idx == NULL) {
2172+
diag_set(ClientError, ER_NO_SUCH_INDEX_NAME,
2173+
tt_cstr(dml->index_name, dml->index_name_len),
2174+
space->def->name);
2175+
return -1;
2176+
}
2177+
dml->index_id = idx->dense_id;
2178+
}
2179+
return 0;
2180+
}
2181+
21462182
static void
21472183
tx_process1(struct cmsg *m)
21482184
{
@@ -2154,6 +2190,8 @@ tx_process1(struct cmsg *m)
21542190
struct obuf_svp svp;
21552191
struct obuf *out;
21562192
tx_inject_delay();
2193+
if (tx_resolve_space_and_index_name(&msg->dml) != 0)
2194+
goto error;
21572195
if (box_process1(&msg->dml, &tuple) != 0)
21582196
goto error;
21592197
out = msg->connection->tx.p_obuf;
@@ -2190,6 +2228,8 @@ tx_process_select(struct cmsg *m)
21902228
goto error;
21912229

21922230
tx_inject_delay();
2231+
if (tx_resolve_space_and_index_name(&msg->dml) != 0)
2232+
goto error;
21932233
packed_pos = req->after_position;
21942234
packed_pos_end = req->after_position_end;
21952235
if (packed_pos != NULL) {

src/box/iproto_constants.c

+2
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ const unsigned char iproto_key_type[iproto_key_MAX] =
154154
/* 0x5b */ MP_STR, /* IPROTO_AUTH_TYPE */
155155
/* 0x5c */ MP_STR, /* IPROTO_REPLICASET_NAME */
156156
/* 0x5d */ MP_STR, /* IPROTO_INSTANCE_NAME */
157+
/* 0x5e */ MP_STR, /* IPROTO_SPACE_NAME */
158+
/* 0x5f */ MP_STR, /* IPROTO_INDEX_NAME */
157159
/* }}} */
158160
};
159161

src/box/iproto_constants.h

+12
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,18 @@ extern const size_t iproto_flag_constants_size;
195195
_(IPROTO_AUTH_TYPE, 0x5b) \
196196
_(IPROTO_REPLICASET_NAME, 0x5c) \
197197
_(IPROTO_INSTANCE_NAME, 0x5d) \
198+
/**
199+
* Space name used instead of identifier (IPROTO_SPACE_ID) in DML
200+
* requests. Preferred when identifier is present (i.e., the identifier
201+
* is ignored).
202+
*/ \
203+
_(IPROTO_SPACE_NAME, 0x5e) \
204+
/**
205+
* Index name used instead of identifier (IPROTO_INDEX_ID) in
206+
* IPROTO_SELECT, IPROTO_UPDATE, and IPROTO_DELETE requests. Preferred
207+
* when identifier is present (i.e., the identifier is ignored).
208+
*/ \
209+
_(IPROTO_INDEX_NAME, 0x5f) \
198210

199211
ENUM(iproto_key, IPROTO_KEYS);
200212
/**

src/box/iproto_features.c

+2
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,6 @@ iproto_features_init(void)
7575
IPROTO_FEATURE_WATCHERS);
7676
iproto_features_set(&IPROTO_CURRENT_FEATURES,
7777
IPROTO_FEATURE_PAGINATION);
78+
iproto_features_set(&IPROTO_CURRENT_FEATURES,
79+
IPROTO_FEATURE_SPACE_AND_INDEX_NAMES);
7880
}

src/box/iproto_features.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ extern "C" {
5050
* request fields and IPROTO_POSITION response field.
5151
*/ \
5252
_(IPROTO_FEATURE_PAGINATION, 4) \
53+
/**
54+
* Using space [index] names instead of identifiers support:
55+
* IPROTO_SPACE_NAME and IPROTO_INDEX_NAME fields in IPROTO_SELECT,
56+
* IPROTO_UPDATE and IPROTO_DELETE request body;
57+
* IPROTO_SPACE_NAME field in IPROTO_INSERT, IPROTO_REPLACE,
58+
* IPROTO_UPDATE and IPROTO_UPSERT request body.
59+
*/ \
60+
_(IPROTO_FEATURE_SPACE_AND_INDEX_NAMES, 5) \
5361

5462
ENUM(iproto_feature_id, IPROTO_FEATURES);
5563

@@ -72,7 +80,7 @@ struct iproto_features {
7280
* `box.iproto.protocol_version` needs to be updated correspondingly.
7381
*/
7482
enum {
75-
IPROTO_CURRENT_VERSION = 4,
83+
IPROTO_CURRENT_VERSION = 5,
7684
};
7785

7886
/**

src/box/lua/net_box.c

+49-33
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ enum {
8080
/**
8181
* IPROTO protocol version supported by the netbox connector.
8282
*/
83-
NETBOX_IPROTO_VERSION = 4,
83+
NETBOX_IPROTO_VERSION = 5,
8484
};
8585

8686
/**
@@ -781,6 +781,44 @@ netbox_encode_eval(lua_State *L, int idx, struct mpstream *stream,
781781
netbox_end_encode(stream, svp);
782782
}
783783

784+
/*
785+
* Depending on the type of the argument (see also net.box space metatable)
786+
* encode either a space identifier or a space name.
787+
*/
788+
static void
789+
netbox_encode_space_id_or_name(lua_State *L, int idx, struct mpstream *stream)
790+
{
791+
if (lua_type(L, idx) == LUA_TNUMBER) {
792+
uint32_t space_id = lua_tonumber(L, idx);
793+
mpstream_encode_uint(stream, IPROTO_SPACE_ID);
794+
mpstream_encode_uint(stream, space_id);
795+
} else {
796+
size_t len;
797+
const char *space_name = lua_tolstring(L, idx, &len);
798+
mpstream_encode_uint(stream, IPROTO_SPACE_NAME);
799+
mpstream_encode_strn(stream, space_name, len);
800+
}
801+
}
802+
803+
/*
804+
* Depending on the type of the argument (see also net.box index metatable)
805+
* encode either a index identifier or an index name.
806+
*/
807+
static void
808+
netbox_encode_index_id_or_name(lua_State *L, int idx, struct mpstream *stream)
809+
{
810+
if (lua_type(L, idx) == LUA_TNUMBER) {
811+
uint32_t space_id = lua_tonumber(L, idx);
812+
mpstream_encode_uint(stream, IPROTO_INDEX_ID);
813+
mpstream_encode_uint(stream, space_id);
814+
} else {
815+
size_t len;
816+
const char *space_name = lua_tolstring(L, idx, &len);
817+
mpstream_encode_uint(stream, IPROTO_INDEX_NAME);
818+
mpstream_encode_strn(stream, space_name, len);
819+
}
820+
}
821+
784822
/* Encode select request. */
785823
static void
786824
netbox_encode_select(lua_State *L, int idx, struct mpstream *stream,
@@ -801,19 +839,13 @@ netbox_encode_select(lua_State *L, int idx, struct mpstream *stream,
801839
if (fetch_pos)
802840
map_size++;
803841
mpstream_encode_map(stream, map_size);
804-
uint32_t space_id = lua_tonumber(L, idx);
805-
uint32_t index_id = lua_tonumber(L, idx + 1);
806842
int iterator = lua_tointeger(L, idx + 2);
807843
uint32_t offset = lua_tonumber(L, idx + 3);
808844
uint32_t limit = lua_tonumber(L, idx + 4);
809845

810-
/* encode space_id */
811-
mpstream_encode_uint(stream, IPROTO_SPACE_ID);
812-
mpstream_encode_uint(stream, space_id);
846+
netbox_encode_space_id_or_name(L, idx, stream);
813847

814-
/* encode index_id */
815-
mpstream_encode_uint(stream, IPROTO_INDEX_ID);
816-
mpstream_encode_uint(stream, index_id);
848+
netbox_encode_index_id_or_name(L, idx + 1, stream);
817849

818850
/* encode iterator */
819851
mpstream_encode_uint(stream, IPROTO_ITERATOR);
@@ -865,10 +897,7 @@ netbox_encode_insert_or_replace(lua_State *L, int idx, struct mpstream *stream,
865897

866898
mpstream_encode_map(stream, 2);
867899

868-
/* encode space_id */
869-
uint32_t space_id = lua_tonumber(L, idx);
870-
mpstream_encode_uint(stream, IPROTO_SPACE_ID);
871-
mpstream_encode_uint(stream, space_id);
900+
netbox_encode_space_id_or_name(L, idx, stream);
872901

873902
/* encode args */
874903
mpstream_encode_uint(stream, IPROTO_TUPLE);
@@ -903,15 +932,9 @@ netbox_encode_delete(lua_State *L, int idx, struct mpstream *stream,
903932

904933
mpstream_encode_map(stream, 3);
905934

906-
/* encode space_id */
907-
uint32_t space_id = lua_tonumber(L, idx);
908-
mpstream_encode_uint(stream, IPROTO_SPACE_ID);
909-
mpstream_encode_uint(stream, space_id);
935+
netbox_encode_space_id_or_name(L, idx, stream);
910936

911-
/* encode space_id */
912-
uint32_t index_id = lua_tonumber(L, idx + 1);
913-
mpstream_encode_uint(stream, IPROTO_INDEX_ID);
914-
mpstream_encode_uint(stream, index_id);
937+
netbox_encode_index_id_or_name(L, idx + 1, stream);
915938

916939
/* encode key */
917940
mpstream_encode_uint(stream, IPROTO_KEY);
@@ -930,15 +953,9 @@ netbox_encode_update(lua_State *L, int idx, struct mpstream *stream,
930953

931954
mpstream_encode_map(stream, 5);
932955

933-
/* encode space_id */
934-
uint32_t space_id = lua_tonumber(L, idx);
935-
mpstream_encode_uint(stream, IPROTO_SPACE_ID);
936-
mpstream_encode_uint(stream, space_id);
956+
netbox_encode_space_id_or_name(L, idx, stream);
937957

938-
/* encode index_id */
939-
uint32_t index_id = lua_tonumber(L, idx + 1);
940-
mpstream_encode_uint(stream, IPROTO_INDEX_ID);
941-
mpstream_encode_uint(stream, index_id);
958+
netbox_encode_index_id_or_name(L, idx + 1, stream);
942959

943960
/* encode index_id */
944961
mpstream_encode_uint(stream, IPROTO_INDEX_BASE);
@@ -965,10 +982,7 @@ netbox_encode_upsert(lua_State *L, int idx, struct mpstream *stream,
965982

966983
mpstream_encode_map(stream, 4);
967984

968-
/* encode space_id */
969-
uint32_t space_id = lua_tonumber(L, idx);
970-
mpstream_encode_uint(stream, IPROTO_SPACE_ID);
971-
mpstream_encode_uint(stream, space_id);
985+
netbox_encode_space_id_or_name(L, idx, stream);
972986

973987
/* encode index_base */
974988
mpstream_encode_uint(stream, IPROTO_INDEX_BASE);
@@ -2994,6 +3008,8 @@ luaopen_net_box(struct lua_State *L)
29943008
IPROTO_FEATURE_WATCHERS);
29953009
iproto_features_set(&NETBOX_IPROTO_FEATURES,
29963010
IPROTO_FEATURE_PAGINATION);
3011+
iproto_features_set(&NETBOX_IPROTO_FEATURES,
3012+
IPROTO_FEATURE_SPACE_AND_INDEX_NAMES);
29973013

29983014
lua_pushcfunction(L, luaT_netbox_request_iterator_next);
29993015
luaT_netbox_request_iterator_next_ref = luaL_ref(L, LUA_REGISTRYINDEX);

0 commit comments

Comments
 (0)