From cf5ec190b40e73e11a69ad86972fc614079abba6 Mon Sep 17 00:00:00 2001 From: Konstantin Osipov <kostja@tarantool.org> Date: Thu, 19 May 2016 22:21:59 +0300 Subject: [PATCH] phia: fix a crashing box-tap/cfg.test.lua * passing env to phia_getstring() does not work any more --- src/box/lua/phia.c | 157 ----------------------------------------- src/box/phia.c | 137 +++-------------------------------- src/box/phia.h | 3 + src/box/phia_engine.cc | 26 +++---- src/box/phia_engine.h | 1 - src/box/phia_index.cc | 17 +++-- src/box/phia_space.cc | 15 ++-- 7 files changed, 37 insertions(+), 319 deletions(-) delete mode 100644 src/box/lua/phia.c diff --git a/src/box/lua/phia.c b/src/box/lua/phia.c deleted file mode 100644 index dae3204e5a..0000000000 --- a/src/box/lua/phia.c +++ /dev/null @@ -1,157 +0,0 @@ -/* - * Copyright 2010-2015, Tarantool AUTHORS, please see AUTHORS file. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * 1. Redistributions of source code must retain the above - * copyright notice, this list of conditions and the - * following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL - * <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF - * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include "phia.h" - -#include <lua.h> -#include <lauxlib.h> -#include <lualib.h> - -#include "lua/utils.h" - -#if 0 -/** - * When user invokes box.phia(), return a table of key/value - * pairs containing the current info. - */ -static int -lbox_phia_call(struct lua_State *L) -{ - lua_newtable(L); - phia_info(lbox_phia_cb, (void*)L); - return 1; -} - -#if 0 -static int -lbox_phia_index(struct lua_State *L) -{ - lbox_phia_call(L); - //lua_pushvalue(L, -2); - lua_gettable(L, -2); - return 1; -} -#endif - -/** Initialize box.phia package. */ -void -box_lua_phia_init(struct lua_State *L) -{ - /* - static const struct luaL_reg phialib [] = { - {NULL, NULL} - }; - */ - - //luaL_register_module(L, "box.phia", phialib); - lua_register(L, "box.phia", lbox_phia_call); - - //lbox_phia_call(L); - - /* - lua_newtable(L); - - lua_pushstring(L, "__index"); - lua_pushcfunction(L, lbox_phia_index); - lua_settable(L, -3); - - lua_pushstring(L, "__serialize"); - lua_pushcfunction(L, lbox_phia_call); - lua_settable(L, -3); - */ - - //lua_setmetatable(L, -2); - lua_pop(L, 1); -} -#endif - -#if 0 -static void -fill_stat_item(struct lua_State *L, int rps, int64_t total) -{ - lua_pushstring(L, "rps"); - lua_pushnumber(L, rps); - lua_settable(L, -3); - - lua_pushstring(L, "total"); - lua_pushnumber(L, total); - lua_settable(L, -3); -} - -static int -set_stat_item(const char *name, int rps, int64_t total, void *cb_ctx) -{ - struct lua_State *L = (struct lua_State *) cb_ctx; - - lua_pushstring(L, name); - lua_newtable(L); - - fill_stat_item(L, rps, total); - - lua_settable(L, -3); - - return 0; -} - -/** - * A stat_foreach() callback used to handle access to e.g. - * box.stats.DELETE. - */ -static int -seek_stat_item(const char *name, int rps, int64_t total, void *cb_ctx) -{ - struct lua_State *L = (struct lua_State *) cb_ctx; - if (strcmp(name, lua_tostring(L, -1)) != 0) - return 0; - - lua_newtable(L); - fill_stat_item(L, rps, total); - - return 1; -} -#endif - - -void -box_lua_phia_init(struct lua_State *L) -{ - static const struct luaL_reg phialib [] = { - {NULL, NULL} - }; - - luaL_register_module(L, "box.phia", phialib); - - lua_newtable(L); - luaL_register(L, NULL, lbox_phia_meta); - lua_setmetatable(L, -2); - - lua_pop(L, 1); /* phia module */ -} diff --git a/src/box/phia.c b/src/box/phia.c index 12c5e461dc..e5f38d4be2 100644 --- a/src/box/phia.c +++ b/src/box/phia.c @@ -14011,6 +14011,15 @@ phia_env_get_scheduler(struct phia_env *env) return &env->scheduler; } +const char * +phia_env_get_error(struct phia_env *env) +{ + static char error[128]; + error[0] = 0; + (void) sr_errorcopy(&env->error, error, sizeof(error)); + return error; +} + static struct phia_document * phia_document_new(struct phia_env*, struct phia_index *, const struct sv*); @@ -14265,30 +14274,6 @@ se_confv_offline(struct srconf *c, struct srconfstmt *s) return se_confv(c, s); } -static inline int -se_confphia_error(struct srconf *c, struct srconfstmt *s) -{ - struct phia_env *e = s->ptr; - char *errorp; - char error[128]; - error[0] = 0; - int len = sr_errorcopy(&e->error, error, sizeof(error)); - if (likely(len == 0)) - errorp = NULL; - else - errorp = error; - struct srconf conf = { - .key = c->key, - .flags = c->flags, - .type = c->type, - .function = NULL, - .value = errorp, - .ptr = NULL, - .next = NULL - }; - return se_confv(&conf, s); -} - static inline struct srconf* se_confphia(struct phia_env *e, struct seconfrt *rt, struct srconf **pc) { @@ -14297,7 +14282,6 @@ se_confphia(struct phia_env *e, struct seconfrt *rt, struct srconf **pc) sr_C(&p, pc, se_confv, "version", SS_STRING, rt->version, SR_RO, NULL); sr_C(&p, pc, se_confv, "version_storage", SS_STRING, rt->version_storage, SR_RO, NULL); sr_C(&p, pc, se_confv, "build", SS_STRING, rt->build, SR_RO, NULL); - sr_C(&p, pc, se_confphia_error, "error", SS_STRING, NULL, SR_RO, NULL); sr_c(&p, pc, se_confv_offline, "path", SS_STRINGPTR, &e->conf.path); sr_c(&p, pc, se_confv_offline, "path_create", SS_U32, &e->conf.path_create); sr_c(&p, pc, se_confv_offline, "recover", SS_U32, &e->conf.recover); @@ -14492,109 +14476,6 @@ se_confdb_get(struct srconf *c, struct srconfstmt *s) return 0; } -static inline int -se_confdb_upsert(struct srconf *c, struct srconfstmt *s) -{ - if (s->op != SR_WRITE) - return se_confv(c, s); - struct phia_index *db = c->ptr; - if (unlikely(phia_index_active(db))) { - sr_error(s->r->e, "write to %s is offline-only", s->path); - return -1; - } - /* set upsert function */ - return 0; -} - -static inline int -se_confdb_upsertarg(struct srconf *c, struct srconfstmt *s) -{ - if (s->op != SR_WRITE) - return se_confv(c, s); - struct phia_index *db = c->ptr; - if (unlikely(phia_index_active(db))) { - sr_error(s->r->e, "write to %s is offline-only", s->path); - return -1; - } - return 0; -} - -static inline int -se_confdb_status(struct srconf *c, struct srconfstmt *s) -{ - struct phia_index *db = c->value; - char *status = sr_statusof(&db->index->status); - struct srconf conf = { - .key = c->key, - .flags = c->flags, - .type = c->type, - .function = NULL, - .value = status, - .ptr = NULL, - .next = NULL - }; - return se_confv(&conf, s); -} - -static inline int -se_confdb_scheme(struct srconf *c ssunused, struct srconfstmt *s) -{ - /* set(scheme, field) */ - struct phia_index *db = c->ptr; - struct phia_env *e = db->env; - if (s->op != SR_WRITE) { - sr_error(&e->error, "%s", "bad operation"); - return -1; - } - if (unlikely(phia_index_active(db))) { - sr_error(s->r->e, "write to %s is offline-only", s->path); - return -1; - } - if (unlikely(db->scheme->scheme.fields_count == 8)) { - sr_error(s->r->e, "%s", "fields number limit reached"); - return -1; - } - char *name = s->value; - struct sffield *field = sf_schemefind(&db->scheme->scheme, name); - if (unlikely(field)) { - sr_error(&e->error, "field '%s' is already set", name); - return -1; - } - /* create new field */ - field = sf_fieldnew(&e->a, name); - if (unlikely(field == NULL)) - return sr_oom(&e->error); - int rc; - rc = sf_fieldoptions(field, &e->a, "string"); - if (unlikely(rc == -1)) { - sf_fieldfree(field, &e->a); - return sr_oom(&e->error); - } - rc = sf_schemeadd(&db->scheme->scheme, &e->a, field); - if (unlikely(rc == -1)) { - sf_fieldfree(field, &e->a); - return sr_oom(&e->error); - } - return 0; -} - -static inline int -se_confdb_field(struct srconf *c, struct srconfstmt *s) -{ - struct phia_index *db = c->ptr; - struct phia_env *e = db->env; - if (s->op != SR_WRITE) - return se_confv(c, s); - if (unlikely(phia_index_active(db))) { - sr_error(s->r->e, "write to %s is offline-only", s->path); - return -1; - } - char *path = s->value; - /* update key-part path */ - struct sffield *field = sf_schemefind(&db->scheme->scheme, c->key); - assert(field != NULL); - return sf_fieldoptions(field, &e->a, path); -} static inline struct srconf* se_confdb(struct phia_env *e, struct seconfrt *rt ssunused, struct srconf **pc) diff --git a/src/box/phia.h b/src/box/phia.h index 5637bf69b9..dc77ce61cf 100644 --- a/src/box/phia.h +++ b/src/box/phia.h @@ -57,6 +57,9 @@ phia_env_open(struct phia_env *e); int phia_env_delete(struct phia_env *e); +const char * +phia_env_get_error(struct phia_env *e); + struct phia_tx * phia_begin(struct phia_env *e); diff --git a/src/box/phia_engine.cc b/src/box/phia_engine.cc index 51b9d740ad..b84416a82d 100644 --- a/src/box/phia_engine.cc +++ b/src/box/phia_engine.cc @@ -212,14 +212,6 @@ phia_workers_stop(void) free(worker_pool); } -void phia_error(struct phia_env *env) -{ - char *error = (char *)phia_getstring(env, "phia.error", NULL); - char msg[512]; - snprintf(msg, sizeof(msg), "%s", error); - tnt_raise(ClientError, ER_PHIA, msg); -} - int phia_info(const char *name, phia_info_f cb, void *arg) { PhiaEngine *e = (PhiaEngine *)engine_find("phia"); @@ -365,7 +357,7 @@ PhiaEngine::init() worker_pool_size = cfg_geti("phia.threads"); int rc = phia_env_open(env); if (rc == -1) - phia_error(env); + tnt_raise(ClientError, ER_PHIA, phia_env_get_error(env)); } void @@ -376,7 +368,7 @@ PhiaEngine::endRecovery() /* complete two-phase recovery */ int rc = phia_env_open(env); if (rc == -1) - phia_error(env); + tnt_raise(ClientError, ER_PHIA, phia_env_get_error(env)); recovery_complete = 1; } @@ -429,7 +421,7 @@ join_send_space(struct space *sp, void *data) /* send database */ struct phia_cursor *cursor = phia_cursor(pk->db); if (cursor == NULL) - phia_error(env); + tnt_raise(ClientError, ER_PHIA, phia_env_get_error(env)); auto cursor_guard = make_scoped_guard([=]{ phia_cursor_delete(cursor); }); @@ -487,11 +479,11 @@ PhiaEngine::dropIndex(Index *index) /* schedule asynchronous drop */ int rc = phia_index_drop(i->db); if (rc == -1) - phia_error(env); + tnt_raise(ClientError, ER_PHIA, phia_env_get_error(env)); /* unref db object */ rc = phia_index_delete(i->db); if (rc == -1) - phia_error(env); + tnt_raise(ClientError, ER_PHIA, phia_env_get_error(env)); i->db = NULL; i->env = NULL; } @@ -548,7 +540,7 @@ PhiaEngine::begin(struct txn *txn) assert(txn->engine_tx == NULL); txn->engine_tx = phia_begin(env); if (txn->engine_tx == NULL) - phia_error(env); + tnt_raise(ClientError, ER_PHIA, phia_env_get_error(env)); } void @@ -576,7 +568,7 @@ PhiaEngine::prepare(struct txn *txn) tnt_raise(ClientError, ER_TRANSACTION_CONFLICT); break; case -1: - phia_error(env); + tnt_raise(ClientError, ER_PHIA, phia_env_get_error(env)); break; } } @@ -625,7 +617,7 @@ PhiaEngine::beginWalRecovery() { int rc = phia_env_open(env); if (rc == -1) - phia_error(env); + tnt_raise(ClientError, ER_PHIA, phia_env_get_error(env)); } int @@ -638,7 +630,7 @@ PhiaEngine::beginCheckpoint() int rc = phia_checkpoint(env); if (rc == -1) - phia_error(env); + tnt_raise(ClientError, ER_PHIA, phia_env_get_error(env)); return 0; } diff --git a/src/box/phia_engine.h b/src/box/phia_engine.h index a7d10b853d..a6fdceca22 100644 --- a/src/box/phia_engine.h +++ b/src/box/phia_engine.h @@ -63,7 +63,6 @@ extern "C" { typedef void (*phia_info_f)(const char*, const char*, void*); int phia_info(const char*, phia_info_f, void*); } -void phia_error(struct phia_env *); void phia_workers_start(struct phia_env *); struct phia_document; diff --git a/src/box/phia_index.cc b/src/box/phia_index.cc index f655ae60c5..c067286a18 100644 --- a/src/box/phia_index.cc +++ b/src/box/phia_index.cc @@ -52,7 +52,7 @@ PhiaIndex::createDocument(const char *key, const char **keyend) assert(key_def->part_count <= 8); struct phia_document *obj = phia_document(db); if (obj == NULL) - phia_error(env); + tnt_raise(ClientError, ER_PHIA, phia_env_get_error(env)); phia_setstring(obj, "arg", fiber(), 0); if (key == NULL) return obj; @@ -72,7 +72,8 @@ PhiaIndex::createDocument(const char *key, const char **keyend) if (partsize == 0) part = ""; if (phia_setstring(obj, partname, part, partsize) == -1) - phia_error(env); + tnt_raise(ClientError, ER_PHIA, + phia_env_get_error(env)); i++; } if (keyend) { @@ -93,14 +94,14 @@ PhiaIndex::PhiaIndex(struct key_def *key_def_arg) /* create database */ db = phia_index_new(env, key_def); if (db == NULL) - phia_error(env); + tnt_raise(ClientError, ER_PHIA, phia_env_get_error(env)); /* start two-phase recovery for a space: * a. created after snapshot recovery * b. created during log recovery */ rc = phia_index_open(db); if (rc == -1) - phia_error(env); + tnt_raise(ClientError, ER_PHIA, phia_env_get_error(env)); format = space->format; tuple_format_ref(format, 1); } @@ -118,10 +119,8 @@ PhiaIndex::~PhiaIndex() if (rc == -1) goto error; error:; - char *error = (char *)phia_getstring(env, "phia.error", 0); say_info("phia space %" PRIu32 " close error: %s", - key_def->space_id, error); - free(error); + key_def->space_id, phia_env_get_error(env)); } size_t @@ -155,7 +154,7 @@ PhiaIndex::findByKey(const char *key, uint32_t part_count = 0) const rc = phia_open(obj); if (rc == -1) { phia_destroy(obj); - phia_error(env); + tnt_raise(ClientError, ER_PHIA, phia_env_get_error(env)); } struct phia_document *result; if (transaction == NULL) { @@ -334,7 +333,7 @@ PhiaIndex::initIterator(struct iterator *ptr, } it->cursor = phia_cursor(db); if (it->cursor == NULL) - phia_error(env); + tnt_raise(ClientError, ER_PHIA, phia_env_get_error(env)); /* Position first key here, since key pointer might be * unavailable from lua. * diff --git a/src/box/phia_space.cc b/src/box/phia_space.cc index 14a6e6f114..c196546da5 100644 --- a/src/box/phia_space.cc +++ b/src/box/phia_space.cc @@ -73,14 +73,15 @@ PhiaSpace::applySnapshotRow(struct space *space, struct request *request) struct phia_tx *tx = phia_begin(index->env); if (tx == NULL) { phia_destroy(obj); - phia_error(index->env); + tnt_raise(ClientError, ER_PHIA, phia_env_get_error(index->env)); } int64_t signature = request->header->lsn; phia_tx_set_lsn(tx, signature); if (phia_replace(tx, obj) != 0) - phia_error(index->env); /* obj destroyed by phia_replace() */ + tnt_raise(ClientError, ER_PHIA, phia_env_get_error(index->env)); + /* obj destroyed by phia_replace() */ int rc = phia_commit(tx); switch (rc) { @@ -94,7 +95,7 @@ PhiaSpace::applySnapshotRow(struct space *space, struct request *request) tnt_raise(ClientError, ER_TRANSACTION_CONFLICT); return; case -1: - phia_error(index->env); + tnt_raise(ClientError, ER_PHIA, phia_env_get_error(index->env)); return; default: assert(0); @@ -143,7 +144,7 @@ PhiaSpace::executeReplace(struct txn*, int rc; rc = phia_replace(tx, obj); if (rc == -1) - phia_error(index->env); + tnt_raise(ClientError, ER_PHIA, phia_env_get_error(index->env)); return NULL; } @@ -162,7 +163,7 @@ PhiaSpace::executeDelete(struct txn*, struct space *space, struct phia_tx *tx = (struct phia_tx *)(in_txn()->engine_tx); int rc = phia_delete(tx, obj); if (rc == -1) - phia_error(index->env); + tnt_raise(ClientError, ER_PHIA, phia_env_get_error(index->env)); return NULL; } @@ -208,7 +209,7 @@ PhiaSpace::executeUpdate(struct txn*, struct space *space, int rc; rc = phia_replace(tx, obj); if (rc == -1) - phia_error(index->env); + tnt_raise(ClientError, ER_PHIA, phia_env_get_error(index->env)); return NULL; } @@ -440,5 +441,5 @@ PhiaSpace::executeUpsert(struct txn*, struct space *space, int rc = phia_upsert(tx, obj); free(value); if (rc == -1) - phia_error(index->env); + tnt_raise(ClientError, ER_PHIA, phia_env_get_error(index->env)); } -- GitLab