Skip to content

Commit

Permalink
Verify database connection on interval (#9665)
Browse files Browse the repository at this point in the history
  • Loading branch information
nerzhul committed Apr 15, 2020
1 parent 62c62f3 commit 5c588f8
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 34 deletions.
38 changes: 22 additions & 16 deletions src/database/database-postgresql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,19 @@ void Database_PostgreSQL::connectToDatabase()
initStatements();
}

void Database_PostgreSQL::verifyDatabase()
void Database_PostgreSQL::pingDatabase()
{
if (PQstatus(m_conn) == CONNECTION_OK)
return;

PQreset(m_conn);
ping();
// Verify DB connection with ping
try {
ping();
} catch (const DatabaseException &e) {
// If ping failed, show the error and try reconnect
PQreset(m_conn);

errorstream << e.what() << std::endl
<< "Reconnecting to database " << m_connect_string << std::endl;
connectToDatabase();
}
}

void Database_PostgreSQL::ping()
Expand Down Expand Up @@ -151,7 +157,7 @@ void Database_PostgreSQL::createTableIfNotExists(const std::string &table_name,

void Database_PostgreSQL::beginSave()
{
verifyDatabase();
pingDatabase();
checkResults(PQexec(m_conn, "BEGIN;"));
}

Expand Down Expand Up @@ -227,7 +233,7 @@ bool MapDatabasePostgreSQL::saveBlock(const v3s16 &pos, const std::string &data)
return false;
}

verifyDatabase();
pingDatabase();

s32 x, y, z;
x = htonl(pos.X);
Expand All @@ -251,7 +257,7 @@ bool MapDatabasePostgreSQL::saveBlock(const v3s16 &pos, const std::string &data)

void MapDatabasePostgreSQL::loadBlock(const v3s16 &pos, std::string *block)
{
verifyDatabase();
pingDatabase();

s32 x, y, z;
x = htonl(pos.X);
Expand All @@ -275,7 +281,7 @@ void MapDatabasePostgreSQL::loadBlock(const v3s16 &pos, std::string *block)

bool MapDatabasePostgreSQL::deleteBlock(const v3s16 &pos)
{
verifyDatabase();
pingDatabase();

s32 x, y, z;
x = htonl(pos.X);
Expand All @@ -293,7 +299,7 @@ bool MapDatabasePostgreSQL::deleteBlock(const v3s16 &pos)

void MapDatabasePostgreSQL::listAllLoadableBlocks(std::vector<v3s16> &dst)
{
verifyDatabase();
pingDatabase();

PGresult *results = execPrepared("list_all_loadable_blocks", 0,
NULL, NULL, NULL, false, false);
Expand Down Expand Up @@ -435,7 +441,7 @@ void PlayerDatabasePostgreSQL::initStatements()

bool PlayerDatabasePostgreSQL::playerDataExists(const std::string &playername)
{
verifyDatabase();
pingDatabase();

const char *values[] = { playername.c_str() };
PGresult *results = execPrepared("load_player", 1, values, false);
Expand All @@ -451,7 +457,7 @@ void PlayerDatabasePostgreSQL::savePlayer(RemotePlayer *player)
if (!sao)
return;

verifyDatabase();
pingDatabase();

v3f pos = sao->getBasePosition();
std::string pitch = ftos(sao->getLookPitch());
Expand Down Expand Up @@ -535,7 +541,7 @@ void PlayerDatabasePostgreSQL::savePlayer(RemotePlayer *player)
bool PlayerDatabasePostgreSQL::loadPlayer(RemotePlayer *player, PlayerSAO *sao)
{
sanity_check(sao);
verifyDatabase();
pingDatabase();

const char *values[] = { player->getName() };
PGresult *results = execPrepared("load_player", 1, values, false, false);
Expand Down Expand Up @@ -610,7 +616,7 @@ bool PlayerDatabasePostgreSQL::removePlayer(const std::string &name)
if (!playerDataExists(name))
return false;

verifyDatabase();
pingDatabase();

const char *values[] = { name.c_str() };
execPrepared("remove_player", 1, values);
Expand All @@ -620,7 +626,7 @@ bool PlayerDatabasePostgreSQL::removePlayer(const std::string &name)

void PlayerDatabasePostgreSQL::listPlayers(std::vector<std::string> &res)
{
verifyDatabase();
pingDatabase();

PGresult *results = execPrepared("load_player_list", 0, NULL, false);

Expand Down
8 changes: 6 additions & 2 deletions src/database/database-postgresql.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ class Database_PostgreSQL: public Database
Database_PostgreSQL(const std::string &connect_string);
~Database_PostgreSQL();

virtual void pingDatabase();

void beginSave();
void endSave();

bool initialized() const;


protected:
// Conversion helpers
inline int pg_to_int(PGresult *res, int row, int col)
Expand Down Expand Up @@ -82,7 +83,6 @@ class Database_PostgreSQL: public Database
}

void createTableIfNotExists(const std::string &table_name, const std::string &definition);
void verifyDatabase();

// Database initialization
void connectToDatabase();
Expand Down Expand Up @@ -113,6 +113,8 @@ class MapDatabasePostgreSQL : private Database_PostgreSQL, public MapDatabase
MapDatabasePostgreSQL(const std::string &connect_string);
virtual ~MapDatabasePostgreSQL() = default;

virtual void pingDatabase() { Database_PostgreSQL::pingDatabase(); }

bool saveBlock(const v3s16 &pos, const std::string &data);
void loadBlock(const v3s16 &pos, std::string *block);
bool deleteBlock(const v3s16 &pos);
Expand All @@ -132,6 +134,8 @@ class PlayerDatabasePostgreSQL : private Database_PostgreSQL, public PlayerDatab
PlayerDatabasePostgreSQL(const std::string &connect_string);
virtual ~PlayerDatabasePostgreSQL() = default;

virtual void pingDatabase() { Database_PostgreSQL::pingDatabase(); }

void savePlayer(RemotePlayer *player);
bool loadPlayer(RemotePlayer *player, PlayerSAO *sao);
bool removePlayer(const std::string &name);
Expand Down
26 changes: 13 additions & 13 deletions src/database/database-sqlite3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ Database_SQLite3::Database_SQLite3(const std::string &savedir, const std::string

void Database_SQLite3::beginSave()
{
verifyDatabase();
pingDatabase();
SQLRES(sqlite3_step(m_stmt_begin), SQLITE_DONE,
"Failed to start SQLite3 transaction");
sqlite3_reset(m_stmt_begin);
}

void Database_SQLite3::endSave()
{
verifyDatabase();
pingDatabase();
SQLRES(sqlite3_step(m_stmt_end), SQLITE_DONE,
"Failed to commit SQLite3 transaction");
sqlite3_reset(m_stmt_end);
Expand Down Expand Up @@ -171,7 +171,7 @@ void Database_SQLite3::openDatabase()
"Failed to enable sqlite3 foreign key support");
}

void Database_SQLite3::verifyDatabase()
void Database_SQLite3::pingDatabase()
{
if (m_initialized) return;

Expand Down Expand Up @@ -247,7 +247,7 @@ inline void MapDatabaseSQLite3::bindPos(sqlite3_stmt *stmt, const v3s16 &pos, in

bool MapDatabaseSQLite3::deleteBlock(const v3s16 &pos)
{
verifyDatabase();
pingDatabase();

bindPos(m_stmt_delete, pos);

Expand All @@ -263,7 +263,7 @@ bool MapDatabaseSQLite3::deleteBlock(const v3s16 &pos)

bool MapDatabaseSQLite3::saveBlock(const v3s16 &pos, const std::string &data)
{
verifyDatabase();
pingDatabase();

#ifdef __ANDROID__
/**
Expand All @@ -290,7 +290,7 @@ bool MapDatabaseSQLite3::saveBlock(const v3s16 &pos, const std::string &data)

void MapDatabaseSQLite3::loadBlock(const v3s16 &pos, std::string *block)
{
verifyDatabase();
pingDatabase();

bindPos(m_stmt_read, pos);

Expand All @@ -311,7 +311,7 @@ void MapDatabaseSQLite3::loadBlock(const v3s16 &pos, std::string *block)

void MapDatabaseSQLite3::listAllLoadableBlocks(std::vector<v3s16> &dst)
{
verifyDatabase();
pingDatabase();

while (sqlite3_step(m_stmt_list) == SQLITE_ROW)
dst.push_back(getIntegerAsBlock(sqlite3_column_int64(m_stmt_list, 0)));
Expand Down Expand Up @@ -439,7 +439,7 @@ void PlayerDatabaseSQLite3::initStatements()

bool PlayerDatabaseSQLite3::playerDataExists(const std::string &name)
{
verifyDatabase();
pingDatabase();
str_to_sqlite(m_stmt_player_load, 1, name);
bool res = (sqlite3_step(m_stmt_player_load) == SQLITE_ROW);
sqlite3_reset(m_stmt_player_load);
Expand Down Expand Up @@ -536,7 +536,7 @@ void PlayerDatabaseSQLite3::savePlayer(RemotePlayer *player)

bool PlayerDatabaseSQLite3::loadPlayer(RemotePlayer *player, PlayerSAO *sao)
{
verifyDatabase();
pingDatabase();

str_to_sqlite(m_stmt_player_load, 1, player->getName());
if (sqlite3_step(m_stmt_player_load) != SQLITE_ROW) {
Expand Down Expand Up @@ -600,7 +600,7 @@ bool PlayerDatabaseSQLite3::removePlayer(const std::string &name)

void PlayerDatabaseSQLite3::listPlayers(std::vector<std::string> &res)
{
verifyDatabase();
pingDatabase();

while (sqlite3_step(m_stmt_player_list) == SQLITE_ROW)
res.push_back(sqlite_to_string(m_stmt_player_list, 0));
Expand Down Expand Up @@ -673,7 +673,7 @@ void AuthDatabaseSQLite3::initStatements()

bool AuthDatabaseSQLite3::getAuth(const std::string &name, AuthEntry &res)
{
verifyDatabase();
pingDatabase();
str_to_sqlite(m_stmt_read, 1, name);
if (sqlite3_step(m_stmt_read) != SQLITE_ROW) {
sqlite3_reset(m_stmt_read);
Expand Down Expand Up @@ -735,7 +735,7 @@ bool AuthDatabaseSQLite3::createAuth(AuthEntry &authEntry)

bool AuthDatabaseSQLite3::deleteAuth(const std::string &name)
{
verifyDatabase();
pingDatabase();

str_to_sqlite(m_stmt_delete, 1, name);
sqlite3_vrfy(sqlite3_step(m_stmt_delete), SQLITE_DONE);
Expand All @@ -749,7 +749,7 @@ bool AuthDatabaseSQLite3::deleteAuth(const std::string &name)

void AuthDatabaseSQLite3::listNames(std::vector<std::string> &res)
{
verifyDatabase();
pingDatabase();

while (sqlite3_step(m_stmt_list_names) == SQLITE_ROW) {
res.push_back(sqlite_to_string(m_stmt_list_names, 0));
Expand Down
12 changes: 9 additions & 3 deletions src/database/database-sqlite3.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ class Database_SQLite3 : public Database
void beginSave();
void endSave();

// Open and initialize the database if needed
virtual void pingDatabase();

bool initialized() const { return m_initialized; }
protected:
Database_SQLite3(const std::string &savedir, const std::string &dbname);

// Open and initialize the database if needed
void verifyDatabase();

// Convertors
inline void str_to_sqlite(sqlite3_stmt *s, int iCol, const std::string &str) const
{
Expand Down Expand Up @@ -146,6 +146,8 @@ class MapDatabaseSQLite3 : private Database_SQLite3, public MapDatabase
MapDatabaseSQLite3(const std::string &savedir);
virtual ~MapDatabaseSQLite3();

virtual void pingDatabase() { Database_SQLite3::pingDatabase(); }

bool saveBlock(const v3s16 &pos, const std::string &data);
void loadBlock(const v3s16 &pos, std::string *block);
bool deleteBlock(const v3s16 &pos);
Expand Down Expand Up @@ -173,6 +175,8 @@ class PlayerDatabaseSQLite3 : private Database_SQLite3, public PlayerDatabase
PlayerDatabaseSQLite3(const std::string &savedir);
virtual ~PlayerDatabaseSQLite3();

virtual void pingDatabase() { Database_SQLite3::pingDatabase(); }

void savePlayer(RemotePlayer *player);
bool loadPlayer(RemotePlayer *player, PlayerSAO *sao);
bool removePlayer(const std::string &name);
Expand Down Expand Up @@ -208,6 +212,8 @@ class AuthDatabaseSQLite3 : private Database_SQLite3, public AuthDatabase
AuthDatabaseSQLite3(const std::string &savedir);
virtual ~AuthDatabaseSQLite3();

virtual void pingDatabase() { Database_SQLite3::pingDatabase(); }

virtual bool getAuth(const std::string &name, AuthEntry &res);
virtual bool saveAuth(const AuthEntry &authEntry);
virtual bool createAuth(AuthEntry &authEntry);
Expand Down
4 changes: 4 additions & 0 deletions src/database/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Database
virtual void beginSave() = 0;
virtual void endSave() = 0;
virtual bool initialized() const { return true; }
virtual void pingDatabase() {}
};

class MapDatabase : public Database
Expand All @@ -57,6 +58,8 @@ class PlayerDatabase
public:
virtual ~PlayerDatabase() = default;

virtual void pingDatabase() {}

virtual void savePlayer(RemotePlayer *player) = 0;
virtual bool loadPlayer(RemotePlayer *player, PlayerSAO *sao) = 0;
virtual bool removePlayer(const std::string &name) = 0;
Expand All @@ -83,4 +86,5 @@ class AuthDatabase
virtual bool deleteAuth(const std::string &name) = 0;
virtual void listNames(std::vector<std::string> &res) = 0;
virtual void reload() = 0;
virtual void pingDatabase() {}
};
5 changes: 5 additions & 0 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1887,6 +1887,11 @@ MapDatabase *ServerMap::createDatabase(
throw BaseException(std::string("Database backend ") + name + " not supported.");
}

void ServerMap::pingDatabase()
{
dbase->pingDatabase();
}

void ServerMap::beginSave()
{
dbase->beginSave();
Expand Down
1 change: 1 addition & 0 deletions src/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ class ServerMap : public Map
Database functions
*/
static MapDatabase *createDatabase(const std::string &name, const std::string &savedir, Settings &conf);
void pingDatabase();

// Call these before and after saving of blocks
void beginSave();
Expand Down
5 changes: 5 additions & 0 deletions src/serverenvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,11 @@ void ServerEnvironment::step(float dtime)
}
}

if (m_database_check_interval.step(dtime, 10.0f)) {
m_auth_database->pingDatabase();
m_player_database->pingDatabase();
m_map->pingDatabase();
}
/*
Manage active block list
*/
Expand Down
1 change: 1 addition & 0 deletions src/serverenvironment.h
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ class ServerEnvironment : public Environment
IntervalLimiter m_object_management_interval;
// List of active blocks
ActiveBlockList m_active_blocks;
IntervalLimiter m_database_check_interval;
IntervalLimiter m_active_blocks_management_interval;
IntervalLimiter m_active_block_modifier_interval;
IntervalLimiter m_active_blocks_nodemetadata_interval;
Expand Down

0 comments on commit 5c588f8

Please sign in to comment.