Merge branch 'ent-8565-control-bloqueos-pandora_db-con-historico' into 'develop'

Fix locking issues in pandora_db.

See merge request artica/pandorafms!4786
This commit is contained in:
Daniel Rodriguez 2022-03-30 11:11:16 +00:00
commit 4b086448da
2 changed files with 63 additions and 6 deletions

View File

@ -42,6 +42,7 @@ our @EXPORT = qw(
db_disconnect
db_do
db_get_lock
db_get_pandora_lock
db_insert
db_insert_get_values
db_insert_from_array_hash
@ -49,6 +50,7 @@ our @EXPORT = qw(
db_process_insert
db_process_update
db_release_lock
db_release_pandora_lock
db_string
db_text
db_update
@ -1575,6 +1577,52 @@ sub db_release_lock($$) {
my ($lock) = $sth->fetchrow;
}
########################################################################
## Try to obtain a persistent lock using Pandora FMS's database.
########################################################################
sub db_get_pandora_lock($$;$) {
my ($dbh, $lock_name, $lock_timeout) = @_;
my $rv;
# Lock.
my $lock = db_get_lock($dbh, $lock_name, $lock_timeout);
if ($lock != 0) {
my $lock_value = get_db_value($dbh, "SELECT `value` FROM tconfig WHERE token = 'pandora_lock_$lock_name'");
if (!defined($lock_value)) {
my $sth = $dbh->prepare('INSERT INTO tconfig (`token`, `value`) VALUES (?, ?)');
$rv = $sth->execute('pandora_lock_' . $lock_name, '1');
} elsif ($lock_value == 0) {
my $sth = $dbh->prepare('UPDATE tconfig SET `value`=? WHERE `token`=?');
$rv = $sth->execute('1', 'pandora_lock_' . $lock_name);
}
db_release_lock($dbh, $lock_name);
}
# Lock acquired.
if ($rv) {
return 1;
}
# Something went wrong.
return 0;
}
########################################################################
## Release a persistent lock.
########################################################################
sub db_release_pandora_lock($$;$) {
my ($dbh, $lock_name, $lock_timeout) = @_;
my $rv;
# Lock.
my $lock = db_get_lock($dbh, $lock_name, $lock_timeout);
if ($lock != 0) {
my $sth = $dbh->prepare('UPDATE tconfig SET `value`=? WHERE `token`=?');
$rv = $sth->execute('0', 'pandora_lock_' . $lock_name);
db_release_lock($dbh, $lock_name);
}
}
########################################################################
## Set SSL options globally for the module.
########################################################################

View File

@ -47,6 +47,9 @@ my $BIG_OPERATION_STEP = 100; # 100 is default
#Increate to 3000~5000 in fast systems decrease to 500 or 250 on systems with locks
my $SMALL_OPERATION_STEP = 1000; # 1000 is default
# Timeout for lock acquisition.
my $LOCK_TIMEOUT = 60;
# FLUSH in each IO
$| = 1;
@ -1218,10 +1221,18 @@ if ($conf{'_force'} == 0 && pandora_is_master(\%conf) == 0) {
exit 1;
}
# Get a lock on dbname.
# Set the lock name for pandora_db.
my $lock_name = $conf{'dbname'};
my $lock = db_get_lock ($dbh, $lock_name);
if ($lock == 0 && $conf{'_force'} == 0) {
# Release the database lock in forced mode.
if ($conf{'_force'} == 1) {
log_message ('', " [*] Releasing database lock.\n\n");
db_release_pandora_lock($dbh, $lock_name, $LOCK_TIMEOUT);
}
# Get a lock on dbname.
my $lock = db_get_pandora_lock ($dbh, $lock_name, $LOCK_TIMEOUT);
if ($lock == 0) {
log_message ('', " [*] Another instance of DB Tool seems to be running.\n\n");
exit 1;
}
@ -1264,9 +1275,7 @@ if (scalar(@types) != 0) {
}
# Release the lock
if ($lock == 1) {
db_release_lock ($dbh, $lock_name);
}
db_release_pandora_lock ($dbh, $lock_name);
# Cleanup and exit
db_disconnect ($history_dbh) if defined ($history_dbh);