Skip to content

Commit

Permalink
Allow an UAcontext to have two database connections.
Browse files Browse the repository at this point in the history
For some operations we want to use a private database connection as they
are either long running or may interfere with each other. We now keep
track of the normal shared database connection and a private database
connection used for these kind of operations that can run over a
dedicated database connection without leading to synchronization
problems in the data. Things like BVFS, restore, prune, purge etc can
all use this private database connection so they no longer block others
and for restore also don't lead to race conditions. These connections
we keep lingering until a close_db() call is done on the UA.
  • Loading branch information
pstorz authored and Marco van Wieringen committed Feb 17, 2015
1 parent b08b4c1 commit fd5b098
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 106 deletions.
5 changes: 2 additions & 3 deletions src/dird/protos.h
Expand Up @@ -236,9 +236,8 @@ bool acl_access_ok(UAContext *ua, int acl, const char *item, int len);
bool do_a_command(UAContext *ua);
bool do_a_dot_command(UAContext *ua);
int qmessages_cmd(UAContext *ua, const char *cmd);
bool open_new_client_db(UAContext *ua);
bool open_client_db(UAContext *ua, bool need_private = false);
bool open_db(UAContext *ua, bool need_private = false);
bool open_client_db(UAContext *ua, bool use_private = false);
bool open_db(UAContext *ua, bool use_private = false);
void close_db(UAContext *ua);
int create_pool(JCR *jcr, B_DB *db, POOLRES *pool, e_pool_op op);
void set_pool_dbr_defaults_in_media_dbr(MEDIA_DBR *mr, POOL_DBR *pr);
Expand Down
2 changes: 2 additions & 0 deletions src/dird/ua.h
Expand Up @@ -37,6 +37,8 @@ class UAContext {
BSOCK *sd;
JCR *jcr;
B_DB *db;
B_DB *shared_db; /* Shared database connection used by multiple ua's */
B_DB *private_db; /* Private database connection only used by this ua */
CATRES *catalog;
CONRES *cons; /* Console resource */
POOLMEM *cmd; /* Return command/name buffer */
Expand Down
76 changes: 47 additions & 29 deletions src/dird/ua_cmds.c
Expand Up @@ -1617,7 +1617,7 @@ static int delete_cmd(UAContext *ua, const char *cmd)
NULL
};

if (!open_client_db(ua)) {
if (!open_client_db(ua, true)) {
return 1;
}

Expand Down Expand Up @@ -2285,19 +2285,6 @@ static int version_cmd(UAContext *ua, const char *cmd)
}
#endif

/*
* This call uses open_client_db() and force a
* new private connection to the catalog
*/
bool open_new_client_db(UAContext *ua)
{
/*
* Force a new private connection
*/
close_db(ua);
return open_client_db(ua, true);
}

/*
* This call explicitly checks for a catalog=catalog-name and
* if given, opens that catalog. It also checks for
Expand All @@ -2306,7 +2293,7 @@ bool open_new_client_db(UAContext *ua)
* have a catalog, look for a Job keyword and get the
* catalog from its client record.
*/
bool open_client_db(UAContext *ua, bool need_private)
bool open_client_db(UAContext *ua, bool use_private)
{
int i;
CATRES *catalog;
Expand All @@ -2326,7 +2313,7 @@ bool open_client_db(UAContext *ua, bool need_private)
close_db(ua);
}
ua->catalog = catalog;
return open_db(ua, need_private);
return open_db(ua, use_private);
}
}

Expand All @@ -2348,7 +2335,7 @@ bool open_client_db(UAContext *ua, bool need_private)
return false;
}
ua->catalog = catalog;
return open_db(ua, need_private);
return open_db(ua, use_private);
}
}

Expand All @@ -2370,23 +2357,37 @@ bool open_client_db(UAContext *ua, bool need_private)
return false;
}
ua->catalog = catalog;
return open_db(ua, need_private);
return open_db(ua, use_private);
}
}

return open_db(ua, need_private);
return open_db(ua, use_private);
}

/*
* Open the catalog database.
*/
bool open_db(UAContext *ua, bool need_private)
bool open_db(UAContext *ua, bool use_private)
{
bool mult_db_conn;

if (ua->db) {
/*
* See if we need to do any work at all.
* Point the current used db e.g. ua->db to the correct database connection.
*/
if (use_private) {
if (ua->private_db) {
ua->db = ua->private_db;
return true;
}
} else if (ua->shared_db) {
ua->db = ua->shared_db;
return true;
}

/*
* Select the right catalog to use.
*/
if (!ua->catalog) {
ua->catalog = get_catalog_resource(ua);
if (!ua->catalog) {
Expand All @@ -2399,7 +2400,7 @@ bool open_db(UAContext *ua, bool need_private)
* Some modules like bvfs need their own private catalog connection
*/
mult_db_conn = ua->catalog->mult_db_connections;
if (need_private) {
if (use_private) {
mult_db_conn = true;
}

Expand All @@ -2415,26 +2416,43 @@ bool open_db(UAContext *ua, bool need_private)
ua->catalog->db_socket,
mult_db_conn,
ua->catalog->disable_batch_insert,
need_private);
use_private);
if (ua->db == NULL) {
ua->error_msg(_("Could not open catalog database \"%s\".\n"), ua->catalog->db_name);
return false;
}
ua->jcr->db = ua->db;

/*
* Save the new database connection under the right label e.g. shared or private.
*/
if (use_private) {
ua->private_db = ua->db;
} else {
ua->shared_db = ua->db;
}

if (!ua->api) {
ua->send_msg(_("Using Catalog \"%s\"\n"), ua->catalog->name());
}

Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
return true;
}

void close_db(UAContext *ua)
{
if (ua->db) {
db_sql_close_pooled_connection(ua->jcr, ua->db);
ua->db = NULL;
if (ua->jcr) {
ua->jcr->db = NULL;
}
if (ua->jcr) {
ua->jcr->db = NULL;
}

if (ua->shared_db) {
db_sql_close_pooled_connection(ua->jcr, ua->shared_db);
ua->shared_db = NULL;
}

if (ua->private_db) {
db_sql_close_pooled_connection(ua->jcr, ua->private_db);
ua->private_db = NULL;
}
}

0 comments on commit fd5b098

Please sign in to comment.