]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/jcr.c
First cut of bat rerun a Job from Jobs Run
[bacula/bacula] / bacula / src / lib / jcr.c
index ce849c807424a0ae0d841e6619a141c00d1e107c..5b88efdc35fe94a765bc957f603f305a1deec98d 100644 (file)
@@ -1,12 +1,12 @@
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
+   Copyright (C) 2000-2010 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.
    This program is Free Software; you can redistribute it and/or
-   modify it under the terms of version two of the GNU General Public
+   modify it under the terms of version three of the GNU Affero General Public
    License as published by the Free Software Foundation and included
    in the file LICENSE.
 
@@ -15,7 +15,7 @@
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    General Public License for more details.
 
-   You should have received a copy of the GNU General Public License
+   You should have received a copy of the GNU Affero General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
@@ -31,8 +31,6 @@
  *
  *  Kern E. Sibbald, December 2000
  *
- *  Version $Id$
- *
  *  These routines are thread safe.
  *
  *  The job list routines were re-written in May 2005 to
@@ -56,7 +54,6 @@
 const int dbglvl = 3400;
 
 /* External variables we reference */
-extern time_t watchdog_time;
 
 /* External referenced functions */
 void free_bregexps(alist *bregexps);
@@ -271,18 +268,6 @@ const char *JCR::get_ActionName(bool past)
    }
 }
 
-/* Set Job type in JCR and also set appropriate read flag */
-void JCR::set_JobType(int32_t JobType)
-{
-   m_JobType = JobType;
-}
-
-/* Set Job level in JCR and also set appropriate read flag */
-void JCR::set_JobLevel(int32_t JobLevel)
-{
-   m_JobLevel = JobLevel;
-}
-
 bool JCR::JobReads()
 {
    switch (m_JobType) {
@@ -323,6 +308,9 @@ static void job_end_pop(JCR *jcr)
    }
 }
 
+/*
+ * Create thread key for thread specific data
+ */
 void create_jcr_key()
 {
    int status = pthread_key_create(&jcr_key, NULL);
@@ -354,8 +342,12 @@ JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
    }
    jcr = (JCR *)malloc(size);
    memset(jcr, 0, size);
-   jcr->my_thread_id = pthread_self();
    jcr->msg_queue = New(dlist(item, &item->link));
+   if ((status = pthread_mutex_init(&jcr->msg_queue_mutex, NULL)) != 0) {
+      berrno be;
+      Jmsg(NULL, M_ABORT, 0, _("Could not init msg_queue mutex. ERR=%s\n"),
+         be.bstrerror(status));
+   }
    jcr->job_end_push.init(1, false);
    jcr->sched_time = time(NULL);
    jcr->daemon_free_jcr = daemon_free_jcr;    /* plug daemon free routine */
@@ -365,13 +357,14 @@ JCR *new_jcr(int size, JCR_free_HANDLER *daemon_free_jcr)
    jcr->VolumeName[0] = 0;
    jcr->errmsg = get_pool_memory(PM_MESSAGE);
    jcr->errmsg[0] = 0;
+   jcr->comment = get_pool_memory(PM_FNAME);
+   jcr->comment[0] = 0;
    /* Setup some dummy values */
    bstrncpy(jcr->Job, "*System*", sizeof(jcr->Job));
    jcr->JobId = 0;
-   jcr->set_JobType(JT_SYSTEM);          /* internal job until defined */
-   jcr->set_JobLevel(L_NONE);
-   set_jcr_job_status(jcr, JS_Created);       /* ready to run */
-   set_jcr_in_tsd(jcr);
+   jcr->setJobType(JT_SYSTEM);           /* internal job until defined */
+   jcr->setJobLevel(L_NONE);
+   jcr->setJobStatus(JS_Created);        /* ready to run */
    sigtimer.sa_flags = 0;
    sigtimer.sa_handler = timeout_handler;
    sigfillset(&sigtimer.sa_mask);
@@ -417,11 +410,16 @@ static void remove_jcr(JCR *jcr)
  */
 static void free_common_jcr(JCR *jcr)
 {
+   /* Uses jcr lock/unlock */
+   remove_jcr_from_tsd(jcr);
+   jcr->set_killable(false);
+
    jcr->destroy_mutex();
 
    if (jcr->msg_queue) {
       delete jcr->msg_queue;
       jcr->msg_queue = NULL;
+      pthread_mutex_destroy(&jcr->msg_queue_mutex);
    }
    close_msg(jcr);                    /* close messages for this job */
 
@@ -475,8 +473,10 @@ static void free_common_jcr(JCR *jcr)
       free_guid_list(jcr->id_list);
       jcr->id_list = NULL;
    }
-   /* Invalidate the tsd jcr data */
-   set_jcr_in_tsd(INVALID_JCR);
+   if (jcr->comment) {
+      free_pool_memory(jcr->comment);
+      jcr->comment = NULL;
+   }
    free(jcr);
 }
 
@@ -501,7 +501,6 @@ void free_jcr(JCR *jcr)
 
 #endif
 
-   dequeue_messages(jcr);
    lock_jcr_chain();
    jcr->dec_use_count();              /* decrement use count */
    if (jcr->use_count() < 0) {
@@ -521,13 +520,15 @@ void free_jcr(JCR *jcr)
             jcr->JobId, jcr->use_count(), jcr->Job);
    }
    remove_jcr(jcr);                   /* remove Jcr from chain */
+   unlock_jcr_chain();
 
+   dequeue_messages(jcr);
    job_end_pop(jcr);                  /* pop and call hooked routines */
 
    Dmsg1(dbglvl, "End job=%d\n", jcr->JobId);
 
    /* Keep some statistics */
-   switch (jcr->get_JobType()) {
+   switch (jcr->getJobType()) {
    case JT_BACKUP:
    case JT_VERIFY:
    case JT_RESTORE:
@@ -540,8 +541,8 @@ void free_jcr(JCR *jcr)
          num_jobs_run++;
          je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
          memset(je, 0, sizeof(struct s_last_job));  /* zero in case unset fields */
-         je->Errors = jcr->Errors;
-         je->JobType = jcr->get_JobType();
+         je->Errors = jcr->JobErrors;
+         je->JobType = jcr->getJobType();
          je->JobId = jcr->JobId;
          je->VolSessionId = jcr->VolSessionId;
          je->VolSessionTime = jcr->VolSessionTime;
@@ -549,7 +550,7 @@ void free_jcr(JCR *jcr)
          je->JobFiles = jcr->JobFiles;
          je->JobBytes = jcr->JobBytes;
          je->JobStatus = jcr->JobStatus;
-         je->JobLevel = jcr->get_JobLevel();
+         je->JobLevel = jcr->getJobLevel();
          je->start_time = jcr->start_time;
          je->end_time = time(NULL);
 
@@ -573,22 +574,70 @@ void free_jcr(JCR *jcr)
       jcr->daemon_free_jcr(jcr);      /* call daemon free routine */
    }
 
-   unlock_jcr_chain();
    free_common_jcr(jcr);
    close_msg(NULL);                   /* flush any daemon messages */
    garbage_collect_memory_pool();
    Dmsg0(dbglvl, "Exit free_jcr\n");
 }
 
+/*
+ * Remove jcr from thread specific data, but
+ *   but make sure it is us who are attached.
+ */
+void remove_jcr_from_tsd(JCR *jcr)
+{
+   JCR *tjcr = get_jcr_from_tsd();
+   if (tjcr == jcr) { 
+      set_jcr_in_tsd(INVALID_JCR);
+   }
+}
+
+void JCR::set_killable(bool killable)
+{
+   JCR *jcr = this;
+   jcr->lock();
+   jcr->my_thread_killable = killable;
+   if (killable) {
+      jcr->my_thread_id = pthread_self();
+   } else {
+      memset(&jcr->my_thread_id, 0, sizeof(jcr->my_thread_id));
+   }
+   jcr->unlock();
+}
+
+/*
+ * Put this jcr in the thread specifc data
+ *  if update_thread_info is true and the jcr is valide,
+ *  we update the my_thread_id in the JCR
+ */
 void set_jcr_in_tsd(JCR *jcr)
 {
    int status = pthread_setspecific(jcr_key, (void *)jcr);
    if (status != 0) {
       berrno be;
-      Jmsg1(jcr, M_ABORT, 0, _("pthread_setspecific failed: ERR=%s\n"), be.bstrerror(status));
+      Jmsg1(jcr, M_ABORT, 0, _("pthread_setspecific failed: ERR=%s\n"), 
+            be.bstrerror(status));
    }
 }
 
+void JCR::my_thread_send_signal(int sig)
+{
+   this->lock();
+   if (this->is_killable() &&
+       !pthread_equal(this->my_thread_id, pthread_self()))
+   {
+      Dmsg1(800, "Send kill to jid=%d\n", this->JobId);
+      pthread_kill(this->my_thread_id, sig);
+
+   } else if (!this->is_killable()) {
+      Dmsg1(10, "Warning, can't send kill to jid=%d\n", this->JobId);
+   }
+   this->unlock();
+}
+
+/*
+ * Give me the jcr that is attached to this thread
+ */
 JCR *get_jcr_from_tsd()
 {
    JCR *jcr = (JCR *)pthread_getspecific(jcr_key);
@@ -637,6 +686,30 @@ JCR *get_jcr_by_id(uint32_t JobId)
    return jcr;
 }
 
+/*
+ * Given a thread id, find the JobId
+ *   Returns: JobId on success
+ *            0 on failure
+ */
+uint32_t get_jobid_from_tid(pthread_t tid)
+{
+   JCR *jcr = NULL;
+   bool found = false;
+
+   foreach_jcr(jcr) {
+      if (pthread_equal(jcr->my_thread_id, tid)) {
+         found = true;
+         break;
+      }
+   }
+   endeach_jcr(jcr);
+   if (found) {
+      return jcr->JobId;
+   }
+   return 0;
+}
+
+
 /*
  * Given a SessionId and SessionTime, find the JCR
  *   Returns: jcr on success
@@ -714,6 +787,58 @@ JCR *get_jcr_by_full_name(char *Job)
    return jcr;
 }
 
+static void update_wait_time(JCR *jcr, int newJobStatus)
+{
+   bool enter_in_waittime;
+   int oldJobStatus = jcr->JobStatus;
+
+   switch (newJobStatus) {
+   case JS_WaitFD:
+   case JS_WaitSD:
+   case JS_WaitMedia:
+   case JS_WaitMount:
+   case JS_WaitStoreRes:
+   case JS_WaitJobRes:
+   case JS_WaitClientRes:
+   case JS_WaitMaxJobs:
+   case JS_WaitPriority:
+      enter_in_waittime = true;
+      break;
+   default:
+      enter_in_waittime = false; /* not a Wait situation */
+      break;
+   }
+   
+   /*
+    * If we were previously waiting and are not any more
+    *   we want to update the wait_time variable, which is
+    *   the start of waiting.
+    */
+   switch (oldJobStatus) {
+   case JS_WaitFD:
+   case JS_WaitSD:
+   case JS_WaitMedia:
+   case JS_WaitMount:
+   case JS_WaitStoreRes:
+   case JS_WaitJobRes:
+   case JS_WaitClientRes:
+   case JS_WaitMaxJobs:
+   case JS_WaitPriority:
+      if (!enter_in_waittime) { /* we get out the wait time */
+         jcr->wait_time_sum += (time(NULL) - jcr->wait_time);
+         jcr->wait_time = 0;
+      }
+      break;
+
+   /* if wait state is new, we keep current time for watchdog MaxWaitTime */
+   default:
+      if (enter_in_waittime) {
+         jcr->wait_time = time(NULL);
+      }
+      break;
+   }
+}
+
 /* 
  * Priority runs from 0 (lowest) to 10 (highest)
  */
@@ -723,10 +848,9 @@ static int get_status_priority(int JobStatus)
    switch (JobStatus) {
    case JS_ErrorTerminated:
    case JS_FatalError:
-      priority = 10;
-      break;
    case JS_Canceled:
-      priority = 9;
+   case JS_Incomplete:
+      priority = 10;
       break;
    case JS_Error:
       priority = 8;
@@ -738,66 +862,46 @@ static int get_status_priority(int JobStatus)
    return priority;
 }
 
+
 void set_jcr_job_status(JCR *jcr, int JobStatus)
 {
-    bool set_waittime = false;
-    int oldJobStatus = jcr->JobStatus;
-    int priority, old_priority;
-
-    priority = get_status_priority(JobStatus);
-    old_priority = get_status_priority(oldJobStatus);
-
-    Dmsg2(800, "set_jcr_job_status(%s, %c)\n", jcr->Job, JobStatus);
-    /* if wait state is new, we keep current time for watchdog MaxWaitTime */
-    switch (JobStatus) {
-       case JS_WaitFD:
-       case JS_WaitSD:
-       case JS_WaitMedia:
-       case JS_WaitMount:
-       case JS_WaitStoreRes:
-       case JS_WaitJobRes:
-       case JS_WaitClientRes:
-       case JS_WaitMaxJobs:
-       case JS_WaitPriority:
-         set_waittime = true;
-       default:
-         break;
-    }
+   jcr->setJobStatus(JobStatus);
+}
+
+void JCR::setJobStatus(int newJobStatus)
+{
+   JCR *jcr = this;
+   int priority, old_priority;
+   int oldJobStatus = jcr->JobStatus;
+   priority = get_status_priority(newJobStatus);
+   old_priority = get_status_priority(oldJobStatus);
+   
+   Dmsg2(800, "set_jcr_job_status(%s, %c)\n", Job, newJobStatus);
+
+   /* Update wait_time depending on newJobStatus and oldJobStatus */
+   update_wait_time(jcr, newJobStatus);
+
    /*
     * For a set of errors, ... keep the current status
     *   so it isn't lost. For all others, set it.
     */
-   Dmsg3(300, "jid=%u OnEntry JobStatus=%c set=%c\n", (uint32_t)jcr->JobId,
-         jcr->JobStatus, JobStatus);
-   if (priority >= old_priority) {
-      jcr->JobStatus = JobStatus;     /* replace with new priority */
-   }
+   Dmsg2(800, "OnEntry JobStatus=%c newJobstatus=%c\n", oldJobStatus, newJobStatus);
    /*
-    * If we were previously waiting and are not any more
-    *   we want to update the wait_time variable, which is
-    *   the start of waiting.
+    * If status priority is > than proposed new status, change it.
+    * If status priority == new priority and both are zero, take
+    *   the new status. 
+    * If it is not zero, then we keep the first non-zero "error" that
+    *   occurred.
     */
-   switch (oldJobStatus) {
-   case JS_WaitFD:
-   case JS_WaitSD:
-   case JS_WaitMedia:
-   case JS_WaitMount:
-   case JS_WaitStoreRes:
-   case JS_WaitJobRes:
-   case JS_WaitClientRes:
-   case JS_WaitMaxJobs:
-   case JS_WaitPriority:
-       set_waittime = false;    /* keep old time */
-   default:
-      if (set_waittime) {
-         Dmsg0(800, "Setting wait_time\n");
-         jcr->wait_time = time(NULL);
-      }
+   if (priority > old_priority || (
+       priority == 0 && old_priority == 0)) {
+      Dmsg4(800, "Set new stat. old: %c,%d new: %c,%d\n",
+         jcr->JobStatus, old_priority, newJobStatus, priority);
+      jcr->JobStatus = newJobStatus;     /* replace with new status */
    }
+
    if (oldJobStatus != jcr->JobStatus) {
-      Dmsg3(200, "jid=%u leave set_old_job_status=%c new_set=%c\n", (uint32_t)jcr->JobId,
-         oldJobStatus, JobStatus);
+      Dmsg2(800, "leave set_job_status old=%c new=%c\n", oldJobStatus, newJobStatus);
 //    generate_plugin_event(jcr, bEventStatusChange, NULL);
    }
 }
@@ -905,6 +1009,24 @@ void jcr_walk_end(JCR *jcr)
    }
 }
 
+/*
+ * Return number of Jobs
+ */
+int job_count()
+{
+   JCR *jcr;
+   int count = 0;
+
+   lock_jcr_chain();
+   for (jcr = (JCR *)jcrs->first(); (jcr = (JCR *)jcrs->next(jcr)); ) {
+      if (jcr->JobId > 0) {
+         count++;
+      }
+   }
+   unlock_jcr_chain();
+   return count;
+}
+
 
 /*
  * Setup to call the timeout check routine every 30 seconds
@@ -949,7 +1071,7 @@ static void jcr_timeout_check(watchdog_t *self)
             Qmsg(jcr, M_ERROR, 0, _(
 "Watchdog sending kill after %d secs to thread stalled reading Storage daemon.\n"),
                  watchdog_time - timer_start);
-            pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
+            jcr->my_thread_send_signal(TIMEOUT_SIGNAL);
          }
       }
       bs = jcr->file_bsock;
@@ -961,7 +1083,7 @@ static void jcr_timeout_check(watchdog_t *self)
             Qmsg(jcr, M_ERROR, 0, _(
 "Watchdog sending kill after %d secs to thread stalled reading File daemon.\n"),
                  watchdog_time - timer_start);
-            pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
+            jcr->my_thread_send_signal(TIMEOUT_SIGNAL);
          }
       }
       bs = jcr->dir_bsock;
@@ -973,7 +1095,7 @@ static void jcr_timeout_check(watchdog_t *self)
             Qmsg(jcr, M_ERROR, 0, _(
 "Watchdog sending kill after %d secs to thread stalled reading Director.\n"),
                  watchdog_time - timer_start);
-            pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
+            jcr->my_thread_send_signal(TIMEOUT_SIGNAL);
          }
       }
    }
@@ -982,6 +1104,41 @@ static void jcr_timeout_check(watchdog_t *self)
    Dmsg0(dbglvl, "Finished JCR timeout checks\n");
 }
 
+/* 
+ * Return next JobId from comma separated list   
+ *
+ * Returns:
+ *   1 if next JobId returned
+ *   0 if no more JobIds are in list
+ *  -1 there is an error
+ */
+int get_next_jobid_from_list(char **p, uint32_t *JobId)
+{
+   const int maxlen = 30;
+   char jobid[maxlen+1];
+   char *q = *p;
+
+   jobid[0] = 0;
+   for (int i=0; i<maxlen; i++) {
+      if (*q == 0) {
+         break;
+      } else if (*q == ',') {
+         q++;
+         break;
+      }
+      jobid[i] = *q++;
+      jobid[i+1] = 0;
+   }
+   if (jobid[0] == 0) {
+      return 0;
+   } else if (!is_a_number(jobid)) {
+      return -1;                      /* error */
+   }
+   *p = q;
+   *JobId = str_to_int64(jobid);
+   return 1;
+}
+
 /*
  * Timeout signal comes here
  */
@@ -990,41 +1147,59 @@ extern "C" void timeout_handler(int sig)
    return;                            /* thus interrupting the function */
 }
 
+/* Used to display specific daemon information after a fatal signal 
+ * (like B_DB in the director)
+ */
+#define MAX_DBG_HOOK 10
+static dbg_jcr_hook_t *dbg_jcr_hooks[MAX_DBG_HOOK];
+static int dbg_jcr_handler_count;
+
+void dbg_jcr_add_hook(dbg_jcr_hook_t *hook)
+{
+   ASSERT(dbg_jcr_handler_count < MAX_DBG_HOOK);
+   dbg_jcr_hooks[dbg_jcr_handler_count++] = hook;
+}
+
 /*
  * !!! WARNING !!! 
  *
- * This function should be used ONLY after a violent signal. We walk through the
- * JCR chain without doing any lock, bacula should not be running.
+ * This function should be used ONLY after a fatal signal. We walk through the
+ * JCR chain without doing any lock, Bacula should not be running.
  */
-void _print_jcr_dbg(FILE *fp)
+void dbg_print_jcr(FILE *fp)
 {
    char buf1[128], buf2[128], buf3[128], buf4[128];
    if (!jcrs) {
       return;
    }
 
-   fprintf(fp, "Attempt to dump current JCRs\n");
+   fprintf(fp, "Attempt to dump current JCRs. njcrs=%d\n", jcrs->size());
 
    for (JCR *jcr = (JCR *)jcrs->first(); jcr ; jcr = (JCR *)jcrs->next(jcr)) {
-      if (!jcr) {               /* protect us against something ? */
-         continue;
-      }
-      
-      fprintf(fp, "JCR=%p JobId=%i name=%s JobStatus=%c\n", 
-              jcr, jcr->JobId, jcr->Job, jcr->JobStatus);
-      fprintf(fp, "\tuse_count=%i threadid=0x%x\n",
-              jcr->use_count(), (int)jcr->my_thread_id);
+      fprintf(fp, "threadid=%p JobId=%d JobStatus=%c jcr=%p name=%s\n", 
+              (void *)jcr->my_thread_id, (int)jcr->JobId, jcr->JobStatus, jcr, jcr->Job);
+      fprintf(fp, "threadid=%p killable=%d JobId=%d JobStatus=%c "
+                  "jcr=%p name=%s\n",
+              (void *)jcr->my_thread_id, jcr->is_killable(),
+              (int)jcr->JobId, jcr->JobStatus, jcr, jcr->Job);
+      fprintf(fp, "\tuse_count=%i\n", jcr->use_count());
       fprintf(fp, "\tJobType=%c JobLevel=%c\n",
-              jcr->get_JobType(), jcr->get_JobLevel());
+              jcr->getJobType(), jcr->getJobLevel());
       bstrftime(buf1, sizeof(buf1), jcr->sched_time);
       bstrftime(buf2, sizeof(buf2), jcr->start_time);
       bstrftime(buf3, sizeof(buf3), jcr->end_time);
       bstrftime(buf4, sizeof(buf4), jcr->wait_time);
       fprintf(fp, "\tsched_time=%s start_time=%s\n\tend_time=%s wait_time=%s\n",
               buf1, buf2, buf3, buf4);
-      fprintf(fp, "\tdequeing=%i\n", jcr->dequeuing);
       fprintf(fp, "\tdb=%p db_batch=%p batch_started=%i\n", 
               jcr->db, jcr->db_batch, jcr->batch_started);
+      
+      /*
+       * Call all the jcr debug hooks
+       */
+      for(int i=0; i < dbg_jcr_handler_count; i++) {
+         dbg_jcr_hook_t *hook = dbg_jcr_hooks[i];
+         hook(jcr, fp);
+      }
    }
 }
-