Skip to content

Commit

Permalink
Prevent from DB lock waits on concurrent use of .bvfs_update
Browse files Browse the repository at this point in the history
Until this change, the HasCache column in the Job table was either 0 or
1, where 0 represents the state bvfs cache does not exist and 1
represents bvfs cache exists. This change introduces -1 representing the
state bvfs cache update in progress. Before starting the bvfs cache
update, it is now checked if HasCache is -1, in that case the
.bvfs_update jobid=n command will return an ERROR: BVFS reported a
problem for 107. This change also prevents from duplicate key violations
that could occur before on the PathHierarchy table.
  • Loading branch information
sduehr committed Nov 18, 2016
1 parent 40e4255 commit 2c2457a
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions src/cats/bvfs.c
Expand Up @@ -248,9 +248,19 @@ static void build_path_hierarchy(JCR *jcr, B_DB *mdb,
}
ppathid_cache.insert(pathid);

/* Prevent from ERROR: duplicate key value violates unique
* constraint "pathhierarchy_pkey" when multiple .bvfs_update
* to prevent from unique key violation when multiple .bvfs_update
* are run parallel.
* The syntax of the following quere works for PostgreSQL, MySQL
* and SQLite
*/
Mmsg(mdb->cmd,
"INSERT INTO PathHierarchy (PathId, PPathId) "
"VALUES (%s,%lld)",
"INSERT INTO PathHierarchy (PathId,PPathId) "
"SELECT ph.PathId,ph.PPathId FROM "
"(SELECT %s AS PathId, %lld AS PPathId) ph "
"WHERE NOT EXISTS "
"(SELECT 1 FROM PathHierarchy phi WHERE phi.PathId = ph.PathId)",
pathid, (uint64_t) parent.PathId);

if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
Expand Down Expand Up @@ -300,6 +310,23 @@ static bool update_path_hierarchy_cache(JCR *jcr,
goto bail_out;
}

/* prevent from DB lock waits when already in progress */
Mmsg(mdb->cmd, "SELECT 1 FROM Job WHERE JobId = %s AND HasCache=-1", jobid);

if (!QUERY_DB(jcr, mdb, mdb->cmd) || sql_num_rows(mdb) > 0) {
Dmsg1(dbglevel, "already in progress %d\n", (uint32_t)JobId );
retval = false;
goto bail_out;
}

/* set HasCache to -1 in Job (in progress) */
Mmsg(mdb->cmd, "UPDATE Job SET HasCache=-1 WHERE JobId=%s", jobid);
UPDATE_DB(jcr, mdb, mdb->cmd);

/* need to COMMIT here and start a new transaction */
db_end_transaction(jcr, mdb);
db_start_transaction(jcr, mdb);

/* Inserting path records for JobId */
Mmsg(mdb->cmd, "INSERT INTO PathVisibility (PathId, JobId) "
"SELECT DISTINCT PathId, JobId "
Expand Down Expand Up @@ -450,7 +477,7 @@ bool bvfs_update_path_hierarchy_cache(JCR *jcr, B_DB *mdb, char *jobids)
char *p;
int status;
JobId_t JobId;
bool retval = false;
bool retval = true;
pathid_cache ppathid_cache;

p = jobids;
Expand All @@ -464,13 +491,12 @@ bool bvfs_update_path_hierarchy_cache(JCR *jcr, B_DB *mdb, char *jobids)
/*
* We reached the end of the list.
*/
retval = true;
goto bail_out;
}

Dmsg1(dbglevel, "Updating cache for %lld\n", (uint64_t)JobId);
if (!update_path_hierarchy_cache(jcr, mdb, ppathid_cache, JobId)) {
goto bail_out;
retval = false;
}
}

Expand Down

0 comments on commit 2c2457a

Please sign in to comment.