From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: From: Vladislav Shpilevoy Subject: [PATCH v2 2/4] session: minimize number of session type resets Date: Tue, 11 Dec 2018 19:10:21 +0300 Message-Id: In-Reply-To: References: In-Reply-To: References: To: tarantool-patches@freelists.org Cc: vdavydov.dev@gmail.com List-ID: Strictly speaking, session type change after its creation is illegal. But. There are one case when it is ok and inevitable: 'console' and 'repl' sessions. 'Repl' session is just an interactive console from stdin run as console.start(). 'Console' is a session, created on console.connect(). 'Repl' can not be set right after fiber creation because struct session depends on box/, however src/lua/init.c, which starts 'repl', does not depend on it. 'Console' can not be set, because a user can call console.start() at any moment of his lua script. This patch removes other unnecessary session type changes and wraps 'console'/'repl' with session_set_type(). --- src/box/applier.cc | 5 ++++- src/box/lua/session.c | 5 +++-- src/box/session.cc | 6 +++--- src/box/session.h | 12 +++++++++--- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/box/applier.cc b/src/box/applier.cc index ff4af95e5..4b5b851e6 100644 --- a/src/box/applier.cc +++ b/src/box/applier.cc @@ -587,7 +587,10 @@ applier_f(va_list ap) * Set correct session type for use in on_replace() * triggers. */ - current_session()->type = SESSION_TYPE_APPLIER; + if (session_create_on_demand(SESSION_TYPE_APPLIER) == NULL) { + diag_log(); + return -1; + } /* Re-connect loop */ while (!fiber_is_cancelled()) { diff --git a/src/box/lua/session.c b/src/box/lua/session.c index f4944baea..51582f749 100644 --- a/src/box/lua/session.c +++ b/src/box/lua/session.c @@ -51,12 +51,13 @@ lbox_session_create(struct lua_State *L, enum session_type type) { struct session *session = fiber_get_session(fiber()); if (session == NULL) { - session = session_create_on_demand(); + session = session_create_on_demand(type); if (session == NULL) return luaT_error(L); session->meta.fd = lua_tointeger(L, 1); + } else { + session_set_type(session, type); } - session->type = type; lua_pushnumber(L, session->id); return 1; diff --git a/src/box/session.cc b/src/box/session.cc index 64714cdcf..36da9ce69 100644 --- a/src/box/session.cc +++ b/src/box/session.cc @@ -105,7 +105,7 @@ session_create(enum session_type type) session->id = sid_max(); memset(&session->meta, 0, sizeof(session->meta)); - session->type = type; + session_set_type(session, type); session->sql_flags = default_flags; session->sql_default_engine = SQL_STORAGE_ENGINE_MEMTX; @@ -127,12 +127,12 @@ session_create(enum session_type type) } struct session * -session_create_on_demand() +session_create_on_demand(enum session_type type) { assert(fiber_get_session(fiber()) == NULL); /* Create session on demand */ - struct session *s = session_create(SESSION_TYPE_BACKGROUND); + struct session *s = session_create(type); if (s == NULL) return NULL; s->fiber_on_stop = { diff --git a/src/box/session.h b/src/box/session.h index df1dcbc62..45f0899b1 100644 --- a/src/box/session.h +++ b/src/box/session.h @@ -140,6 +140,12 @@ struct session_vtab { extern struct session_vtab session_vtab_registry[]; +static inline void +session_set_type(struct session *session, enum session_type new_type) +{ + session->type = new_type; +} + /** * Find a session by id. */ @@ -200,7 +206,7 @@ extern struct credentials admin_credentials; * trigger to destroy it when this fiber ends. */ struct session * -session_create_on_demand(); +session_create_on_demand(enum session_type type); /* * When creating a new fiber, the database (box) @@ -217,7 +223,7 @@ current_session() { struct session *session = fiber_get_session(fiber()); if (session == NULL) { - session = session_create_on_demand(); + session = session_create_on_demand(SESSION_TYPE_BACKGROUND); if (session == NULL) diag_raise(); } @@ -236,7 +242,7 @@ effective_user() struct fiber *f = fiber(); struct credentials *u = f->storage.credentials; if (u == NULL) { - session_create_on_demand(); + session_create_on_demand(SESSION_TYPE_BACKGROUND); u = f->storage.credentials; } return u; -- 2.17.2 (Apple Git-113)