Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
To: tarantool-patches@freelists.org
Cc: vdavydov.dev@gmail.com
Subject: [PATCH v2 2/4] session: minimize number of session type resets
Date: Tue, 11 Dec 2018 19:10:21 +0300	[thread overview]
Message-ID: <b40fea90b0bf8b4cb56eaa3a1a6dbabf738bf744.1544544183.git.v.shpilevoy@tarantool.org> (raw)
In-Reply-To: <cover.1544544183.git.v.shpilevoy@tarantool.org>
In-Reply-To: <cover.1544544183.git.v.shpilevoy@tarantool.org>

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)

  parent reply	other threads:[~2018-12-11 16:10 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-11 16:10 [PATCH v2 0/4] Outdate binary session on disconnect Vladislav Shpilevoy
2018-12-11 16:10 ` [PATCH v2 1/4] console: forbid arbitrary session type setting Vladislav Shpilevoy
2018-12-21 11:17   ` Vladimir Davydov
2018-12-11 16:10 ` Vladislav Shpilevoy [this message]
2018-12-11 16:10 ` [PATCH v2 3/4] session: store vtab in struct session Vladislav Shpilevoy
2018-12-21 11:04   ` Vladimir Davydov
2018-12-21 11:23     ` [tarantool-patches] " Vladislav Shpilevoy
2018-12-11 16:10 ` [PATCH v2 4/4] session: outdate a session of a closed connection Vladislav Shpilevoy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b40fea90b0bf8b4cb56eaa3a1a6dbabf738bf744.1544544183.git.v.shpilevoy@tarantool.org \
    --to=v.shpilevoy@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=vdavydov.dev@gmail.com \
    --subject='Re: [PATCH v2 2/4] session: minimize number of session type resets' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox