Session
Last updated
Was this helpful?
Last updated
Was this helpful?
The session
package (and related packages such as sessionctx
and sessionctx/variable
) are responsible for maintaining the state of both sessions and transactions.
New connections are first established in the server
package. After some initial protocol negotiation, the server
package session.CreateSession()
. This function then calls session.createSessionWithOpt()
(via CreateSessionWithOpt()
) which creates the session.
Sessions used for internal server operations are usually created in a different manner, with the sessionctx being retrieved from a pool of sessions maintained by domain
. For example:
Internal sessions will not show up in the output of SHOW PROCESSLIST
, and skip all privilege checks because they do not have a privilege manager handle attached.
System variables follow similar semantics to MySQL:
If a variable includes SESSION
scope, the value is copied to the session state when the session is created.
Any changes to the GLOBAL
value will not apply to any existing sessions.
For many variables, as well as a string value there is a typed field in sessionVars
. For example:
SessionVars.systems["tidb_skip_utf8_check"]
(string) maps to SessionVars.SkipUTF8Check
(bool).
In the above example, skipping the SetSession
function is useful because it prevents the CollationDatabase
from being overwritten when the session is initialized. This is only expected if the user issues a statement to change the CharsetDatabase
value.
In TiDB, changes to GLOBAL
scoped system variables are propagated to other TiDB servers in the cluster and persist across restarts. The notification event to other servers is sent via an etcd channel in the call domain.GetDomain(s).NotifyUpdateSysVarCache()
:
The decision to make an option such as tidb_general_log
instance scoped is because it references a file on the local filesystem. This may create issues when global, as the path may not be writable on each tidb-server in the cluster.
The session.CommitTxn()
function will handle the commit
, including retry (if permitted). There is also special handling for both pessimistic and optimistic transactions, as well as removing the key-value pairs which apply to temporary tables from the transaction buffer.
The state of the variables is stored in . The raw string values are stored in a map named . This string value is used for persistence in the mysql.global_variables
table.
The typed value is set when the SetSession
attached to the is called. For tidb_skip_utf8_check
this is as follows:
The SetSession
function can also be considered an Init
function, since it is called when the session is created and the values are copied from global scope. To disable SetSession
from being called on creation, skipInit
can be set to true
. For example with :
Because GLOBAL
scoped variables are propagated to other servers, TiDB also has a special concept of "instance-scoped variables". An instance scoped variable is actually a SESSION
scoped variable that has a GetSession
method which returns data that is specific to an instance. For example, :
As you can see by the Scope: Session
, instance-scoped variables are not natively handled by the sysvar framework, but are instead denoted by the GetSession()
function reading from a global location. The documentation for also notes it as "instance" scoped by convention.
The session
struct (s.txn
) is responsible for keeping modified key-value pairs in a until the transaction commits. A commit
statement only sets the session variable state that it is :
The function which is called as part of is responsible for committing the transaction: