Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

db flex tape #8627

Merged
merged 4 commits into from
May 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions code/_global_vars/sensitive.dm
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// MySQL configuration
GLOBAL_REAL_VAR(sqladdress) = "localhost"
GLOBAL_REAL_VAR(sqlport) = "3306"
GLOBAL_REAL_VAR(sqldb) = "tgstation"
GLOBAL_REAL_VAR(sqllogin) = "root"
GLOBAL_REAL_VAR(sqladdress) = ""
GLOBAL_REAL_VAR(sqlport) = ""
GLOBAL_REAL_VAR(sqldb) = ""
GLOBAL_REAL_VAR(sqllogin) = ""
GLOBAL_REAL_VAR(sqlpass) = ""
// Feedback gathering sql connection
GLOBAL_REAL_VAR(sqlfdbkdb) = "test"
GLOBAL_REAL_VAR(sqlfdbklogin) = "root"
GLOBAL_REAL_VAR(sqlfdbkdb) = ""
GLOBAL_REAL_VAR(sqlfdbklogin) = ""
GLOBAL_REAL_VAR(sqlfdbkpass) = ""
GLOBAL_REAL_VAR(sqllogging) = 0 // Should we log deaths, population stats, etc.?
GLOBAL_REAL_VAR(sqllogging) = 0 // Should we log deaths, population stats, etc.?
41 changes: 0 additions & 41 deletions code/controllers/configuration.dm
Original file line number Diff line number Diff line change
Expand Up @@ -1065,47 +1065,6 @@ var/global/list/gamemode_cache = list()
else
log_misc("Unknown setting in configuration: '[name]'")

/datum/configuration/proc/loadforumsql(filename) // -- TLE
var/list/Lines = file2list(filename)
for(var/t in Lines)
if(!t) continue

t = trim(t)
if (length(t) == 0)
continue
else if (copytext(t, 1, 2) == "#")
continue

var/pos = findtext(t, " ")
var/name = null
var/value = null

if (pos)
name = lowertext(copytext(t, 1, pos))
value = copytext(t, pos + 1)
else
name = lowertext(t)

if (!name)
continue

switch (name)
if ("address")
forumsqladdress = value
if ("port")
forumsqlport = value
if ("database")
forumsqldb = value
if ("login")
forumsqllogin = value
if ("password")
forumsqlpass = value
if ("activatedgroup")
forum_activated_group = value
if ("authenticatedgroup")
forum_authenticated_group = value
else
log_misc("Unknown setting in configuration: '[name]'")

/datum/configuration/proc/pick_mode(mode_name)
// I wish I didn't have to instance the game modes in order to look up
Expand Down
96 changes: 96 additions & 0 deletions code/controllers/subsystems/misc_slow.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
SUBSYSTEM_DEF(misc_slow)
name = "Misc Slow"
flags = SS_NO_INIT
runlevels = RUNLEVEL_LOBBY | RUNLEVEL_GAME | RUNLEVEL_POSTGAME
wait = 5 MINUTES

/// The number of times dbcon can fail in a row before being considered dead
var/static/const/DB_RECONNECTS_ALLOWED = 3

/// a list of (reconnects succeeded, reconnects failed) for dbcon
var/static/db_status = list(0, 0)

/// A convenience cache of db_status for stat_entry
var/static/db_info = "-"

/// The number of times dbcon_old can fail in a row before being considered dead
var/static/const/DB_OLD_RECONNECTS_ALLOWED = 3

/// a list of (reconnects succeeded, reconnects failed) for dbcon_old
var/static/db_old_status = list(0, 0)

/// A convenience cache of db_old_status for stat_entry
var/static/db_old_info = "-"


/datum/controller/subsystem/misc_slow/VV_static()
return ..() + list(
"DB_RECONNECTS_ALLOWED",
"DB_OLD_RECONNECTS_ALLOWED"
)


/datum/controller/subsystem/misc_slow/Recover()
RecoverDoReconnects()


/datum/controller/subsystem/misc_slow/stat_entry(msg)
..(list(
msg, "sql: [config.sql_enabled ? "Y" : "N"] | DB: [db_info] | DB Old: [db_old_info]"
).Join(null))


/datum/controller/subsystem/misc_slow/fire(resumed, no_mc_tick)
DoReconnects()


/// Clear the state of members related to db connection maintenance
/datum/controller/subsystem/misc_slow/proc/RecoverDoReconnects()
db_status = list(0, 0)
db_info = "-"
db_old_status = list(0, 0)
db_old_info = "-"


/// Run connection maintenance for dbcon and dbcon_old, if sql is enabled correct(ish).
/datum/controller/subsystem/misc_slow/proc/DoReconnects()
if (!config.sql_enabled)
return
if (!sqladdress || !sqlport || !sqllogin || !sqldb || !sqlfdbklogin || !sqlfdbkdb)
log_debug("SS [name]: SQL credentials misconfigured.")
return
var/db_dsn = "dbi:mysql:[sqlfdbkdb]:[sqladdress]:[sqlport]"
ReconnectDB("DB", dbcon, db_dsn, sqlfdbklogin, sqlfdbkpass, db_status, DB_RECONNECTS_ALLOWED)
db_info = "DEAD"
if (db_status[2] < DB_RECONNECTS_ALLOWED)
db_info = "R[db_status[1]],F[db_status[2]]"
var/db_old_dsn = "dbi:mysql:[sqldb]:[sqladdress]:[sqlport]"
ReconnectDB("Old DB", dbcon_old, db_old_dsn, sqllogin, sqlpass, db_old_status, DB_OLD_RECONNECTS_ALLOWED)
db_old_info = "DEAD"
if (db_old_status[2] < DB_OLD_RECONNECTS_ALLOWED)
db_old_info = "R[db_old_status[1]],F[db_old_status[2]]"


/// An ugly proc that attempts to reopen the passed database's connection if it has timed out or been lost.
/datum/controller/subsystem/misc_slow/proc/ReconnectDB(log_name, DBConnection/db, dsn, user, pass, list/status, limit)
if (status[2] >= limit) // We've failed too many times.
return
if (!isnull(db._db_con))
if (_dm_db_is_connected(db._db_con))
var/query = _dm_db_new_query() // Do a little dance to keep the connection interested.
if (_dm_db_execute(query, "SELECT 1;", db._db_con, null, null))
return
log_debug("SS [name]: [log_name] reconnecting.")
_dm_db_close(dbcon._db_con)
dbcon._db_con = null
var/connection = _dm_db_new_con()
var/success = _dm_db_connect(connection, dsn, user, pass, null, null)
if (success)
log_debug("SS [name]: [log_name] reconnected ([++status[1]]\th time).")
db._db_con = connection
status[2] = 0
return
log_debug("SS [name]: [log_name] reconnect failed ([++status[2]]\th time).")
if (status[1] < limit)
return
log_debug("SS [name]: [log_name] reconnect failed too many times. No more attempts will be made.")
5 changes: 4 additions & 1 deletion code/game/world.dm
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,6 @@ var/global/world_topic_spam_protect_time = world.timeofday
config.load("config/config.txt")
config.load("config/game_options.txt","game_options")
config.loadsql("config/dbconfig.txt")
config.loadforumsql("config/forumdbconfig.txt")

/hook/startup/proc/loadMods()
world.load_mods()
Expand Down Expand Up @@ -580,6 +579,8 @@ var/global/failed_old_db_connections = 0

//This proc ensures that the connection to the feedback database (global variable dbcon) is established
/proc/establish_db_connection()
if (!config.sql_enabled)
return 0
if(failed_db_connections > FAILED_DB_CONNECTION_CUTOFF)
return 0

Expand Down Expand Up @@ -625,6 +626,8 @@ var/global/failed_old_db_connections = 0

//This proc ensures that the connection to the feedback database (global variable dbcon) is established
/proc/establish_old_db_connection()
if (!config.sql_enabled)
return 0
if(failed_old_db_connections > FAILED_DB_CONNECTION_CUTOFF)
return 0

Expand Down
9 changes: 0 additions & 9 deletions code/global.dm
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,6 @@ var/global/datum/metric/metric = new() // Metric datum, used to keep track of th

var/global/list/awaydestinations = list() // Away missions. A list of landmarks that the warpgate can take you to.

// Forum MySQL configuration. (for use with forum account/key authentication)
// These are all default values that will load should the forumdbconfig.txt file fail to read for whatever reason.
var/global/forumsqladdress = "localhost"
var/global/forumsqlport = "3306"
var/global/forumsqldb = "tgstation"
var/global/forumsqllogin = "root"
var/global/forumsqlpass = ""
var/global/forum_activated_group = "2"
var/global/forum_authenticated_group = "10"

// For FTP requests. (i.e. downloading runtime logs.)
// However it'd be ok to use for accessing attack logs and such too, which are even laggier.
Expand Down
5 changes: 0 additions & 5 deletions code/modules/admin/view_variables/view_variables_global.dm
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ GLOBAL_DATUM(debug_real_globals, /debug_real_globals) // :3c

/debug_real_globals/VV_hidden()
return list(
"forumsqladdress",
"forumsqldb",
"forumsqllogin",
"forumsqlpass",
"forumsqlport",
"sqladdress",
"sqldb",
"sqllogin",
Expand Down
19 changes: 0 additions & 19 deletions config/example/forumdbconfig.txt

This file was deleted.

1 change: 1 addition & 0 deletions polaris.dme
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@
#include "code\controllers\subsystems\lighting.dm"
#include "code\controllers\subsystems\machines.dm"
#include "code\controllers\subsystems\mapping.dm"
#include "code\controllers\subsystems\misc_slow.dm"
#include "code\controllers\subsystems\mobs.dm"
#include "code\controllers\subsystems\nanoui.dm"
#include "code\controllers\subsystems\nightshift.dm"
Expand Down