Skip to content

Commit

Permalink
Fix select() by space_no and index_name
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bigbes authored and Totktonada committed Jul 17, 2020
1 parent e67cb23 commit 5647d24
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 15 deletions.
51 changes: 36 additions & 15 deletions src/tarantool.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 2 additions & 0 deletions src/tarantool_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions src/tarantool_schema.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/tarantool_schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions test/DMLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

0 comments on commit 5647d24

Please sign in to comment.