From 5647d24667a48ca3e9242c33fca86ab73b580652 Mon Sep 17 00:00:00 2001 From: Eugine Blikh Date: Tue, 16 Jun 2020 21:49:31 +0300 Subject: [PATCH] Fix select() by space_no and index_name When a space exists, but a client side schema does not know about it at the moment (say, right after connection), and when a user calls select() with a space id (number) and name of an index (string), the problem appears: the client raises the following error: | TarantoolParsingException: Failed to parse schema (index) However it should successfully perform the request. The problem is that when there is no record for a space in client's schema, it is not possible to save a record for an index of this space. The idea of the fix is to verify whether we know about a space even when a numeric ID is already provided. If a client has no record about the space, it fetches a schema and verify whether the space appears. If so, there is no more problem described above. Otherwise the client raises an error that the space with given ID does not exist. While we're here, ensure that return values of tarantool_schema_*() are checked against -1, not Zend's FAILURE macro (which is only guaranteed to be less than zero) in the modified code. Also ensure that the FAILURE macro is returned from the get_spaceno_by_name() function, not -1. Closes #42 @Totktonada: polish code, polish test, write the description. --- src/tarantool.c | 51 +++++++++++++++++++++++++++++------------- src/tarantool_proto.h | 2 ++ src/tarantool_schema.c | 19 ++++++++++++++++ src/tarantool_schema.h | 2 ++ test/DMLTest.php | 37 ++++++++++++++++++++++++++++++ 5 files changed, 96 insertions(+), 15 deletions(-) diff --git a/src/tarantool.c b/src/tarantool.c index c3ab7a4..5f95845 100644 --- a/src/tarantool.c +++ b/src/tarantool.c @@ -631,21 +631,30 @@ int convert_iterator(zval *iter, int all) { } int get_spaceno_by_name(tarantool_connection *obj, zval *name) { - if (Z_TYPE_P(name) == IS_LONG) - return Z_LVAL_P(name); - if (Z_TYPE_P(name) != IS_STRING) { + if (Z_TYPE_P(name) != IS_STRING && Z_TYPE_P(name) != IS_LONG) { tarantool_throw_exception("Space ID must be String or Long"); return FAILURE; } - int32_t space_no = tarantool_schema_get_sid_by_string(obj->schema, - Z_STRVAL_P(name), Z_STRLEN_P(name)); - if (space_no != FAILURE) - return space_no; - + int32_t space_no; tarantool_tp_update(obj->tps); - tp_select(obj->tps, SPACE_SPACE, INDEX_SPACE_NAME, 0, 4096); - tp_key(obj->tps, 1); - tp_encode_str(obj->tps, Z_STRVAL_P(name), Z_STRLEN_P(name)); + if (Z_TYPE_P(name) == IS_LONG) { + space_no = tarantool_schema_get_sid_by_number(obj->schema, + Z_LVAL_P(name)); + if (space_no != -1) + return space_no; + tp_select(obj->tps, SPACE_SPACE, INDEX_SPACE_NO, 0, 4096); + tp_key(obj->tps, 1); + tp_encode_uint(obj->tps, Z_LVAL_P(name)); + } else { + space_no = tarantool_schema_get_sid_by_string(obj->schema, + Z_STRVAL_P(name), + Z_STRLEN_P(name)); + if (space_no != -1) + return space_no; + tp_select(obj->tps, SPACE_SPACE, INDEX_SPACE_NAME, 0, 4096); + tp_key(obj->tps, 1); + tp_encode_str(obj->tps, Z_STRVAL_P(name), Z_STRLEN_P(name)); + } tp_reqid(obj->tps, TARANTOOL_G(sync_counter)++); obj->value->len = tp_used(obj->tps); @@ -679,10 +688,22 @@ int get_spaceno_by_name(tarantool_connection *obj, zval *name) { tarantool_throw_parsingexception("schema (space)"); return FAILURE; } - space_no = tarantool_schema_get_sid_by_string(obj->schema, - Z_STRVAL_P(name), Z_STRLEN_P(name)); - if (space_no == FAILURE) - THROW_EXC("No space '%s' defined", Z_STRVAL_P(name)); + + if (Z_TYPE_P(name) == IS_LONG) { + space_no = tarantool_schema_get_sid_by_number(obj->schema, + Z_LVAL_P(name)); + if (space_no == -1) { + THROW_EXC("No space %d defined", Z_LVAL_P(name)); + } + } else { + space_no = tarantool_schema_get_sid_by_string(obj->schema, + Z_STRVAL_P(name), + Z_STRLEN_P(name)); + if (space_no == -1) { + THROW_EXC("No space '%s' defined", Z_STRVAL_P(name)); + } + } + return space_no; } diff --git a/src/tarantool_proto.h b/src/tarantool_proto.h index 658d0fb..49f0432 100644 --- a/src/tarantool_proto.h +++ b/src/tarantool_proto.h @@ -12,6 +12,8 @@ #define SPACE_SPACE 281 #define SPACE_INDEX 289 +#define INDEX_SPACE_NO 0 +#define INDEX_INDEX_NO 0 #define INDEX_SPACE_NAME 2 #define INDEX_INDEX_NAME 2 diff --git a/src/tarantool_schema.c b/src/tarantool_schema.c index 01d7e6f..70d434c 100644 --- a/src/tarantool_schema.c +++ b/src/tarantool_schema.c @@ -557,6 +557,25 @@ tarantool_schema_get_sid_by_string( return space->space_number; } +int32_t +tarantool_schema_get_sid_by_number( + struct tarantool_schema *schema_obj, + uint32_t sid +) { + struct mh_schema_space_t *schema = schema_obj->space_hash; + struct schema_key space_key = { + (void *)&sid, + sizeof(uint32_t), + sid, /* ignored */ + }; + mh_int_t space_slot = mh_schema_space_find(schema, &space_key, NULL); + if (space_slot == mh_end(schema)) + return -1; + const struct schema_space_value *space = + *mh_schema_space_node(schema, space_slot); + return space->space_number; +} + int32_t tarantool_schema_get_iid_by_string( struct tarantool_schema *schema_obj, uint32_t sid, diff --git a/src/tarantool_schema.h b/src/tarantool_schema.h index 537738e..219775a 100644 --- a/src/tarantool_schema.h +++ b/src/tarantool_schema.h @@ -56,6 +56,8 @@ int32_t tarantool_schema_get_sid_by_string(struct tarantool_schema *, const char *, uint32_t); int32_t +tarantool_schema_get_sid_by_number(struct tarantool_schema *, uint32_t); +int32_t tarantool_schema_get_iid_by_string(struct tarantool_schema *, uint32_t, const char *, uint32_t); int32_t diff --git a/test/DMLTest.php b/test/DMLTest.php index 1ecff82..7b3bdcf 100644 --- a/test/DMLTest.php +++ b/test/DMLTest.php @@ -468,4 +468,41 @@ public function test_18_02_delete_loop() { gc_collect_cycles(); gc_collect_cycles(); } + + public function test_19_select_space_no_index_name_known() { + /* + * gh-42: 'Failed to parse schema (index)' at select() + * error when a space is passed as a number, but an index + * is passed as a name. + * + * The problem appears when a schema is not fetched yet, + * so let's test it on a new client instance. + */ + $port = TestHelpers::getTarantoolPort(); + $tarantool = new Tarantool('localhost', $port, 'test'); + + $vindex_id = 289; + $res = $tarantool->select($vindex_id, [$vindex_id], 'primary', 1); + $this->assertEquals($res[0][0], $vindex_id); + } + + public function test_20_select_space_no_index_name_unknown() { + /* + * Related to gh-42, see above. This case is to verify + * that the client gives an error in the case when unknown + * space id is passed (together with some index name). + * + * One of transient versions of gh-42 patch had a mistake + * on the error path that cause a segfault. This case + * triggers execution of the corresponding code. + */ + $port = TestHelpers::getTarantoolPort(); + $tarantool = new Tarantool('localhost', $port, 'test'); + + $this->expectException(TarantoolException::class); + $this->expectExceptionMessage('No space 1024 defined'); + + $unknown_space_id = 1024; + $tarantool->select($unknown_space_id, [], 'primary'); + } }