From 6d0cca04d5fbd956501b92ae0e29b8144dab9976 Mon Sep 17 00:00:00 2001 From: Kern Sibbald Date: Sat, 5 Jul 2003 13:03:54 +0000 Subject: [PATCH] Fix minor logic error in recycling code git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@618 91ce42f0-d328-0410-95d8-f526ca767f89 --- bacula/src/dird/catreq.c | 109 +++++++++++++++++++------------------ bacula/src/dird/recycle.c | 2 +- bacula/src/dird/ua_purge.c | 4 +- 3 files changed, 59 insertions(+), 56 deletions(-) diff --git a/bacula/src/dird/catreq.c b/bacula/src/dird/catreq.c index 2ffc82b5f7..b2453e5cc4 100644 --- a/bacula/src/dird/catreq.c +++ b/bacula/src/dird/catreq.c @@ -89,69 +89,72 @@ void catalog_request(JCR *jcr, BSOCK *bs, char *msg) /* * Find the Next Volume for Append */ -next_volume: - strcpy(mr.VolStatus, "Append"); /* want only appendable volumes */ - ok = db_find_next_volume(jcr, jcr->db, index, &mr); - Dmsg2(100, "catreq after find_next_vol ok=%d FW=%d\n", ok, mr.FirstWritten); - if (!ok) { - /* Well, try finding recycled tapes */ - ok = find_recycled_volume(jcr, &mr); - Dmsg2(100, "find_recycled_volume1 %d FW=%d\n", ok, mr.FirstWritten); + for ( ;; ) { + strcpy(mr.VolStatus, "Append"); /* want only appendable volumes */ + ok = db_find_next_volume(jcr, jcr->db, index, &mr); + Dmsg2(100, "catreq after find_next_vol ok=%d FW=%d\n", ok, mr.FirstWritten); if (!ok) { - prune_volumes(jcr); - ok = recycle_oldest_purged_volume(jcr, &mr); - Dmsg2(200, "find_recycled_volume2 %d FW=%d\n", ok, mr.FirstWritten); + /* Well, try finding recycled volumes */ + ok = find_recycled_volume(jcr, &mr); + Dmsg2(100, "find_recycled_volume %d FW=%d\n", ok, mr.FirstWritten); if (!ok) { - /* See if we can create a new Volume */ - ok = newVolume(jcr, &mr); + prune_volumes(jcr); + ok = recycle_oldest_purged_volume(jcr, &mr); + Dmsg2(200, "find_recycled_volume2 %d FW=%d\n", ok, mr.FirstWritten); + if (!ok) { + /* See if we can create a new Volume */ + ok = newVolume(jcr, &mr); + } } - } - if (!ok && jcr->pool->purge_oldest_volume) { - Dmsg1(200, "No next volume found. PurgeOldest=%d\n", - jcr->pool->purge_oldest_volume); - /* Find oldest volume to recycle */ - ok = db_find_next_volume(jcr, jcr->db, -1, &mr); - Dmsg1(400, "Find oldest=%d\n", ok); - if (ok) { - UAContext *ua; - Dmsg0(400, "Try purge.\n"); - /* Try to purge oldest volume */ - ua = new_ua_context(jcr); - Jmsg(jcr, M_INFO, 0, _("Purging oldest volume \"%s\"\n"), mr.VolumeName); - ok = purge_jobs_from_volume(ua, &mr); - free_ua_context(ua); + if (!ok && jcr->pool->purge_oldest_volume) { + Dmsg1(200, "No next volume found. PurgeOldest=%d\n", + jcr->pool->purge_oldest_volume); + /* Find oldest volume to recycle */ + ok = db_find_next_volume(jcr, jcr->db, -1, &mr); + Dmsg1(400, "Find oldest=%d\n", ok); if (ok) { - ok = recycle_oldest_purged_volume(jcr, &mr); - Dmsg1(400, "Recycle after recycle oldest=%d\n", ok); + UAContext *ua; + Dmsg0(400, "Try purge.\n"); + /* Try to purge oldest volume */ + ua = new_ua_context(jcr); + Jmsg(jcr, M_INFO, 0, _("Purging oldest volume \"%s\"\n"), mr.VolumeName); + ok = purge_jobs_from_volume(ua, &mr); + free_ua_context(ua); + if (ok) { + ok = recycle_oldest_purged_volume(jcr, &mr); + Dmsg1(400, "Recycle after recycle oldest=%d\n", ok); + } } } } - } - /* Check if use duration has expired */ - Dmsg2(100, "VolJobs=%d FirstWritten=%d\n", mr.VolJobs, mr.FirstWritten); - if (ok && mr.VolJobs > 0 && mr.VolUseDuration > 0 && - strcmp(mr.VolStatus, "Append") == 0) { - utime_t now = time(NULL); - if (mr.VolUseDuration <= (now - mr.FirstWritten)) { - Dmsg4(100, "Duration=%d now=%d start=%d now-start=%d\n", - (int)mr.VolUseDuration, (int)now, (int)mr.FirstWritten, - (int)(now-mr.FirstWritten)); - Jmsg(jcr, M_INFO, 0, _("Max configured use duration exceeded. " - "Marking Volume \"%s\" as Used.\n"), mr.VolumeName); - strcpy(mr.VolStatus, "Used"); /* yes, mark as used */ - if (!db_update_media_record(jcr, jcr->db, &mr)) { - Jmsg(jcr, M_ERROR, 0, _("Catalog error updating Media record. %s"), - db_strerror(jcr->db)); - } else if (retry++ < 200) { /* sanity check */ - goto next_volume; - } else { - Jmsg(jcr, M_ERROR, 0, _( -"We seem to be looping trying to find the next volume. I give up. Ask operator.\n")); + /* Check if use duration applies, then if it has expired */ + Dmsg2(100, "VolJobs=%d FirstWritten=%d\n", mr.VolJobs, mr.FirstWritten); + if (ok && mr.VolJobs > 0 && mr.VolUseDuration > 0 && + strcmp(mr.VolStatus, "Append") == 0) { + utime_t now = time(NULL); + if (mr.VolUseDuration <= (now - mr.FirstWritten)) { + ok = FALSE; + Dmsg4(100, "Duration=%d now=%d start=%d now-start=%d\n", + (int)mr.VolUseDuration, (int)now, (int)mr.FirstWritten, + (int)(now-mr.FirstWritten)); + Jmsg(jcr, M_INFO, 0, _("Max configured use duration exceeded. " + "Marking Volume \"%s\" as Used.\n"), mr.VolumeName); + strcpy(mr.VolStatus, "Used"); /* yes, mark as used */ + if (!db_update_media_record(jcr, jcr->db, &mr)) { + Jmsg(jcr, M_ERROR, 0, _("Catalog error updating volume \"%s\". ERR=%s"), + mr.VolumeName, db_strerror(jcr->db)); + } + if (retry++ < 200) { /* sanity check */ + continue; /* try again from the top */ + } else { + Jmsg(jcr, M_ERROR, 0, _( +"We seem to be looping trying to find the next volume. I give up.\n")); + } } - ok = FALSE; /* give up */ } - } + break; + } /* end for loop */ /* * Send Find Media response to Storage daemon diff --git a/bacula/src/dird/recycle.c b/bacula/src/dird/recycle.c index fcfaf2e8ff..6da0b13369 100644 --- a/bacula/src/dird/recycle.c +++ b/bacula/src/dird/recycle.c @@ -82,7 +82,7 @@ int recycle_oldest_purged_volume(JCR *jcr, MEDIA_DBR *mr) Dmsg0(100, "Enter recycle_oldest_purged_volume\n"); oldest.MediaId = 0; - strcpy(oldest.LastWritten, "9999-99-99 99:99:99"); + bstrncpy(oldest.LastWritten, "9999-99-99 99:99:99", sizeof(oldest.LastWritten)); Mmsg(&query, select, mr->PoolId, mr->MediaType); if (!db_sql_query(jcr->db, query, oldest_handler, (void *)&oldest)) { Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db)); diff --git a/bacula/src/dird/ua_purge.c b/bacula/src/dird/ua_purge.c index 237511dcaa..c0ce011d35 100644 --- a/bacula/src/dird/ua_purge.c +++ b/bacula/src/dird/ua_purge.c @@ -429,10 +429,10 @@ void purge_files_from_job(UAContext *ua, JOB_DBR *jr) { char *query = (char *)get_pool_memory(PM_MESSAGE); - Mmsg(&query, "DELETE FROM File WHERE JobId=%d", jr->JobId); + Mmsg(&query, "DELETE FROM File WHERE JobId=%u", jr->JobId); db_sql_query(ua->db, query, NULL, (void *)NULL); - Mmsg(&query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%d", jr->JobId); + Mmsg(&query, "UPDATE Job Set PurgedFiles=1 WHERE JobId=%u", jr->JobId); db_sql_query(ua->db, query, NULL, (void *)NULL); free_pool_memory(query); -- 2.39.5