MXS-1924: Enable WAL mode for sqlite

The mysqlauth SQLite database is now opened in WAL mode if possible. This
should prevent lockups of the database when the list of users is updated.

Also moved the starting of the SQLite transaction one level up to also
include the delete part in it. This should further reduce the effects of
updating users.
This commit is contained in:
Markus Mäkelä 2018-06-15 01:12:24 +03:00
parent 53177c30de
commit 856d513040
No known key found for this signature in database
GPG Key ID: 72D48FCE664F7B19
3 changed files with 33 additions and 17 deletions

View File

@ -776,8 +776,6 @@ int get_users_from_server(MYSQL *con, SERVER_REF *server, SERVICE *service, SERV
if (result)
{
start_sqlite_transaction(instance->handle);
MYSQL_ROW row;
while ((row = mysql_fetch_row(result)))
@ -804,8 +802,6 @@ int get_users_from_server(MYSQL *con, SERVER_REF *server, SERVICE *service, SERV
}
}
commit_sqlite_transaction(instance->handle);
mysql_free_result(result);
}
}
@ -874,6 +870,7 @@ static int get_users(SERV_LISTENER *listener, bool skip_local)
/** Delete the old users */
MYSQL_AUTH *instance = (MYSQL_AUTH*)listener->auth_instance;
start_sqlite_transaction(instance->handle);
delete_mysql_users(instance->handle);
SERVER_REF *server = service->dbref;
@ -921,6 +918,8 @@ static int get_users(SERV_LISTENER *listener, bool skip_local)
}
}
commit_sqlite_transaction(instance->handle);
MXS_FREE(dpwd);
if (no_active_servers)

View File

@ -129,25 +129,41 @@ static void get_database_path(SERV_LISTENER *port, char *dest, size_t size)
static bool open_instance_database(const char *path, sqlite3 **handle)
{
bool rval = true;
if (sqlite3_open_v2(path, handle, db_flags, NULL) != SQLITE_OK)
{
MXS_ERROR("Failed to open SQLite3 handle.");
return false;
rval = false;
}
char *err;
if (sqlite3_exec(*handle, users_create_sql, NULL, NULL, &err) != SQLITE_OK ||
sqlite3_exec(*handle, databases_create_sql, NULL, NULL, &err) != SQLITE_OK ||
sqlite3_exec(*handle, pragma_sql, NULL, NULL, &err) != SQLITE_OK)
else
{
MXS_ERROR("Failed to create database: %s", err);
sqlite3_free(err);
sqlite3_close_v2(*handle);
return false;
char *err;
if (sqlite3_exec(*handle, users_create_sql, NULL, NULL, &err) != SQLITE_OK ||
sqlite3_exec(*handle, databases_create_sql, NULL, NULL, &err) != SQLITE_OK)
{
MXS_ERROR("Failed to create database: %s", err);
sqlite3_free(err);
sqlite3_close_v2(*handle);
rval = false;
}
else if (sqlite3_exec(*handle, pragma_sql, NULL, NULL, &err) != SQLITE_OK)
{
sqlite3_free(err);
MXS_NOTICE("Could not open SQLite database in WAL mode, using in-memory mode");
if (sqlite3_exec(*handle, old_pragma_sql, NULL, NULL, &err) != SQLITE_OK)
{
MXS_ERROR("Failed to set in-memory mode: %s", err);
sqlite3_free(err);
sqlite3_close_v2(*handle);
rval = false;
}
}
}
return true;
return rval;
}
static bool open_client_database(const char *path, sqlite3 **handle)

View File

@ -58,7 +58,8 @@ static const char databases_create_sql[] =
"CREATE TABLE IF NOT EXISTS " MYSQLAUTH_DATABASES_TABLE_NAME "(db varchar(255))";
/** PRAGMA configuration options for SQLite */
static const char pragma_sql[] = "PRAGMA JOURNAL_MODE=MEMORY";
static const char pragma_sql[] = "PRAGMA journal_mode=WAL";
static const char old_pragma_sql[] = "PRAGMA journal_mode=MEMORY";
/** Query that checks if there's a grant for the user being authenticated */
static const char mysqlauth_validate_user_query[] =