]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/dird/jobq.c
kes Pass jcr to tls routines so debug messages can be handled better.
[bacula/bacula] / bacula / src / dird / jobq.c
index 768d6b34c8816a24a5b5e43e7dcaf29c49d24a90..308acd77307b378da3f8f44bd505cebba34b39ae 100644 (file)
@@ -1,26 +1,7 @@
-/*
- * Bacula job queue routines.
- *
- *  This code consists of three queues, the waiting_jobs
- *  queue, where jobs are initially queued, the ready_jobs
- *  queue, where jobs are placed when all the resources are
- *  allocated and they can immediately be run, and the
- *  running queue where jobs are placed when they are
- *  running.
- *
- *  Kern Sibbald, July MMIII
- *
- *   Version $Id$
- *
- *  This code was adapted from the Bacula workq, which was
- *    adapted from "Programming with POSIX Threads", by
- *    David R. Butenhof
- *
- */
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2003-2007 Free Software Foundation Europe e.V.
+   Copyright (C) 2003-2008 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
    Switzerland, email:ftf@fsfeurope.org.
 */
+/*
+ * Bacula job queue routines.
+ *
+ *  This code consists of three queues, the waiting_jobs
+ *  queue, where jobs are initially queued, the ready_jobs
+ *  queue, where jobs are placed when all the resources are
+ *  allocated and they can immediately be run, and the
+ *  running queue where jobs are placed when they are
+ *  running.
+ *
+ *  Kern Sibbald, July MMIII
+ *
+ *   Version $Id$
+ *
+ *  This code was adapted from the Bacula workq, which was
+ *    adapted from "Programming with POSIX Threads", by
+ *    David R. Butenhof
+ *
+ */
 
 #include "bacula.h"
 #include "dird.h"
@@ -357,8 +357,8 @@ static int start_server(jobq_t *jq)
    pthread_t id;
 
    /*
-    * if any threads are idle, wake one --                
-    *   actually we do a broadcast because on /lib/tls 
+    * if any threads are idle, wake one.
+    *   Actually we do a broadcast because on /lib/tls 
     *   these signals seem to get lost from time to time.
     */
    if (jq->idle_workers > 0) {
@@ -453,6 +453,7 @@ void *jobq_server(void *arg)
             }
          }
          jq->running_jobs->append(je);
+         set_jcr_in_tsd(jcr);
          Dmsg1(2300, "Took jobid=%d from ready and appended to run\n", jcr->JobId);
 
          /* Release job queue lock */
@@ -477,12 +478,14 @@ void *jobq_server(void *arg)
           */
          if (jcr->acquired_resource_locks) {
             if (jcr->rstore) {
-               jcr->rstore->NumConcurrentJobs = 0;
+               jcr->rstore->NumConcurrentJobs--;
                Dmsg1(200, "Dec rncj=%d\n", jcr->rstore->NumConcurrentJobs);
+               ASSERT(jcr->rstore->NumConcurrentJobs >= 0);
             }
             if (jcr->wstore) {
                jcr->wstore->NumConcurrentJobs--;
                Dmsg1(200, "Dec wncj=%d\n", jcr->wstore->NumConcurrentJobs);
+               ASSERT(jcr->wstore->NumConcurrentJobs >= 0);
             }
             jcr->client->NumConcurrentJobs--;
             jcr->job->NumConcurrentJobs--;
@@ -680,6 +683,13 @@ static bool acquire_resources(JCR *jcr)
    bool skip_this_jcr = false;
 
    jcr->acquired_resource_locks = false;
+   if (jcr->rstore == jcr->wstore) {           /* deadlock */
+      Jmsg(jcr, M_FATAL, 0, _("Job canceled. Attempt to read and write same device.\n"
+         "    Read storage \"%s\" (From %s) -- Write storage \"%s\" (From %s)\n"), 
+         jcr->rstore->name(), jcr->rstore_source, jcr->wstore->name(), jcr->wstore_source);
+      set_jcr_job_status(jcr, JS_Canceled);
+      return false;
+   }
    if (jcr->rstore) {
       Dmsg1(200, "Rstore=%s\n", jcr->rstore->name());
       if (jcr->rstore->NumConcurrentJobs == 0 &&
@@ -687,9 +697,10 @@ static bool acquire_resources(JCR *jcr)
          /* Simple case, first job */
          jcr->rstore->NumConcurrentJobs = 1;
          Dmsg0(200, "Set rncj=1\n");
-      } else if (jcr->rstore->NumConcurrentJobs < jcr->rstore->MaxConcurrentJobs) {
-         jcr->rstore->NumConcurrentJobs++;
-         Dmsg1(200, "Inc rncj=%d\n", jcr->rstore->NumConcurrentJobs);
+      /* We can do this only if multi-drive autochanger */
+//    } else if (jcr->rstore->NumConcurrentJobs < jcr->rstore->MaxConcurrentJobs) {
+//       jcr->rstore->NumConcurrentJobs++;
+//       Dmsg1(200, "Inc rncj=%d\n", jcr->rstore->NumConcurrentJobs);
       } else {
          Dmsg1(200, "Fail rncj=%d\n", jcr->rstore->NumConcurrentJobs);
          set_jcr_job_status(jcr, JS_WaitStoreRes);
@@ -699,14 +710,6 @@ static bool acquire_resources(JCR *jcr)
    
    if (jcr->wstore) {
       Dmsg1(200, "Wstore=%s\n", jcr->wstore->name());
-      if (jcr->rstore == jcr->wstore) {           /* deadlock */
-         jcr->rstore->NumConcurrentJobs--;        /* back out rstore */
-         Jmsg(jcr, M_FATAL, 0, _("Job canceled. Attempt to read and write same device.\n"
-            "    Read storage \"%s\" (From %s) -- Write storage \"%s\" (From %s)\n"), 
-            jcr->rstore->name(), jcr->rstore_source, jcr->wstore->name(), jcr->wstore_source);
-         set_jcr_job_status(jcr, JS_Canceled);
-         return false;
-      }
       if (jcr->wstore->NumConcurrentJobs == 0 &&
           jcr->wstore->NumConcurrentJobs < jcr->wstore->MaxConcurrentJobs) {
          /* Simple case, first job */
@@ -718,6 +721,7 @@ static bool acquire_resources(JCR *jcr)
       } else if (jcr->rstore) {
          jcr->rstore->NumConcurrentJobs--;        /* back out rstore */
          Dmsg1(200, "Fail wncj=%d\n", jcr->wstore->NumConcurrentJobs);
+         ASSERT(jcr->rstore->NumConcurrentJobs >= 0);
          skip_this_jcr = true;
       } else {
          Dmsg1(200, "Fail wncj=%d\n", jcr->wstore->NumConcurrentJobs);
@@ -736,10 +740,12 @@ static bool acquire_resources(JCR *jcr)
       if (jcr->wstore) {
          jcr->wstore->NumConcurrentJobs--;
          Dmsg1(200, "Dec wncj=%d\n", jcr->wstore->NumConcurrentJobs);
+         ASSERT(jcr->wstore->NumConcurrentJobs >= 0);
       }
       if (jcr->rstore) {
          jcr->rstore->NumConcurrentJobs--;  
          Dmsg1(200, "Dec rncj=%d\n", jcr->rstore->NumConcurrentJobs);
+         ASSERT(jcr->rstore->NumConcurrentJobs >= 0);
       }
       set_jcr_job_status(jcr, JS_WaitClientRes);
       return false;
@@ -751,10 +757,12 @@ static bool acquire_resources(JCR *jcr)
       if (jcr->wstore) {
          jcr->wstore->NumConcurrentJobs--;
          Dmsg1(200, "Dec wncj=%d\n", jcr->wstore->NumConcurrentJobs);
+         ASSERT(jcr->wstore->NumConcurrentJobs >= 0);
       }
       if (jcr->rstore) {
          jcr->rstore->NumConcurrentJobs--;
          Dmsg1(200, "Dec rncj=%d\n", jcr->rstore->NumConcurrentJobs);
+         ASSERT(jcr->rstore->NumConcurrentJobs >= 0);
       }
       jcr->client->NumConcurrentJobs--;
       set_jcr_job_status(jcr, JS_WaitJobRes);