Skip to content

Commit

Permalink
Merge pull request #545 from bareos/dev/sduehr/master/fix-bvfs-update…
Browse files Browse the repository at this point in the history
…-constraint-violation

bvfs: Prevent from unique key violations on .bvfs_update
  • Loading branch information
sduehr committed Jul 17, 2020
2 parents f3c4ebf + 686394a commit fb7305d
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 7 deletions.
4 changes: 3 additions & 1 deletion core/src/cats/bdb_query_enum_class.h
Expand Up @@ -81,6 +81,8 @@ class BareosDbQueryEnum {
bvfs_ls_sub_dirs_5 = 74,
list_volumes_select_0 = 75,
list_volumes_select_long_0 = 76,
SQL_QUERY_NUMBER = 77
bvfs_lock_pathhierarchy_0 = 77,
bvfs_unlock_tables_0 = 78,
SQL_QUERY_NUMBER = 79
};
};
2 changes: 2 additions & 0 deletions core/src/cats/bdb_query_names.inc
Expand Up @@ -78,5 +78,7 @@ const char *BareosDb::query_names[] = {
"bvfs_ls_sub_dirs_5",
"list_volumes_select_0",
"list_volumes_select_long_0",
"bvfs_lock_pathhierarchy_0",
"bvfs_unlock_tables_0",
NULL
};
11 changes: 11 additions & 0 deletions core/src/cats/bvfs.cc
Expand Up @@ -283,6 +283,14 @@ bool BareosDb::UpdatePathHierarchyCache(JobControlRecord* jcr,
result[i++] = strdup(row[1]);
}

/* The PathHierarchy table needs exclusive write lock here to
* prevent from unique key constraint violations (PostgreSQL)
* or duplicate entry errors (MySQL/MariaDB) when multiple
* bvfs update operations are run simultaneously.
*/
FillQuery(cmd, SQL_QUERY::bvfs_lock_pathhierarchy_0);
if (!QUERY_DB(jcr, cmd)) { goto bail_out; }

i = 0;
while (num > 0) {
BuildPathHierarchy(jcr, ppathid_cache, result[i], result[i + 1]);
Expand All @@ -291,6 +299,9 @@ bool BareosDb::UpdatePathHierarchyCache(JobControlRecord* jcr,
num--;
}
free(result);

FillQuery(cmd, SQL_QUERY::bvfs_unlock_tables_0);
if (!QUERY_DB(jcr, cmd)) { goto bail_out; }
}

StartTransaction(jcr);
Expand Down
1 change: 1 addition & 0 deletions core/src/cats/dml/0078_bvfs_lock_pathhierarchy_0
@@ -0,0 +1 @@
BEGIN
1 change: 1 addition & 0 deletions core/src/cats/dml/0078_bvfs_lock_pathhierarchy_0.mysql
@@ -0,0 +1 @@
LOCK TABLES PathHierarchy write, Path write
@@ -0,0 +1 @@
BEGIN; LOCK TABLE PathHierarchy IN SHARE ROW EXCLUSIVE MODE
1 change: 1 addition & 0 deletions core/src/cats/dml/0079_bvfs_unlock_tables_0
@@ -0,0 +1 @@
COMMIT
1 change: 1 addition & 0 deletions core/src/cats/dml/0079_bvfs_unlock_tables_0.mysql
@@ -0,0 +1 @@
UNLOCK TABLES
8 changes: 8 additions & 0 deletions core/src/cats/mysql_queries.inc
Expand Up @@ -1008,5 +1008,13 @@ const char *BareosDbMysql::query_definitions[] = {
"LEFT JOIN Storage USING(StorageId) "
,

/* 0078_bvfs_lock_pathhierarchy_0.mysql */
"LOCK TABLES PathHierarchy write, Path write "
,

/* 0079_bvfs_unlock_tables_0.mysql */
"UNLOCK TABLES "
,

NULL
};
14 changes: 11 additions & 3 deletions core/src/cats/postgresql_queries.inc
Expand Up @@ -545,9 +545,9 @@ const char *BareosDbPostgresql::query_definitions[] = {
"Path.Path||File.Name AS Name, "
"StartTime, "
"TYPE AS JobType, "
"JobStatus, "
"JobFiles, "
"JobBytes "
"JobStatus, "
"JobFiles, "
"JobBytes "
"FROM Client, "
"Job, "
"File, "
Expand Down Expand Up @@ -987,5 +987,13 @@ const char *BareosDbPostgresql::query_definitions[] = {
"LEFT JOIN Storage USING(StorageId) "
,

/* 0078_bvfs_lock_pathhierarchy_0.postgresql */
"BEGIN; LOCK TABLE PathHierarchy IN SHARE ROW EXCLUSIVE MODE "
,

/* 0079_bvfs_unlock_tables_0 */
"COMMIT "
,

NULL
};
14 changes: 11 additions & 3 deletions core/src/cats/sqlite_queries.inc
Expand Up @@ -565,9 +565,9 @@ const char *BareosDbSqlite::query_definitions[] = {
"Path.Path||File.Name AS Name, "
"StartTime, "
"TYPE AS JobType, "
"JobStatus, "
"JobFiles, "
"JobBytes "
"JobStatus, "
"JobFiles, "
"JobBytes "
"FROM Client, "
"Job, "
"File, "
Expand Down Expand Up @@ -993,5 +993,13 @@ const char *BareosDbSqlite::query_definitions[] = {
"LEFT JOIN Storage USING(StorageId) "
,

/* 0078_bvfs_lock_pathhierarchy_0 */
"BEGIN "
,

/* 0079_bvfs_unlock_tables_0 */
"COMMIT "
,

NULL
};

0 comments on commit fb7305d

Please sign in to comment.