]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/filed/heartbeat.c
Big backport from Enterprise
[bacula/bacula] / bacula / src / filed / heartbeat.c
index ba7841f6262d8d9425a5506ebf2bc913593f18c9..2a8eeafd8b8d71ae850a73edb4bdc30c84539748 100644 (file)
@@ -1,3 +1,21 @@
+/*
+   Bacula(R) - The Network Backup Solution
+
+   Copyright (C) 2000-2016 Kern Sibbald
+
+   The original author of Bacula is Kern Sibbald, with contributions
+   from many others, a complete list can be found in the file AUTHORS.
+
+   You may use this file and others of this release according to the
+   license defined in the LICENSE file, which includes the Affero General
+   Public License, v3.0 ("AGPLv3") and some additional permissions and
+   terms pursuant to its AGPLv3 Section 7.
+
+   This notice must be preserved when any source code is 
+   conveyed and/or propagated.
+
+   Bacula(R) is a registered trademark of Kern Sibbald.
+*/
 /*
  *  Bacula File Daemon heartbeat routines
  *    Listens for heartbeats coming from the SD
  *
  *    Kern Sibbald, May MMIII
  *
- *   Version $Id$
- *
- */
-/*
-   Copyright (C) 2000-2003 Kern Sibbald and John Walker
-
-   This program is free software; you can redistribute it and/or
-   modify it under the terms of the GNU General Public License as
-   published by the Free Software Foundation; either version 2 of
-   the License, or (at your option) any later version.
-
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   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 along with this program; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
-   MA 02111-1307, USA.
-
  */
 
 #include "bacula.h"
 #include "filed.h"
 
-/* 
+#define WAIT_INTERVAL 5
+
+extern "C" void *sd_heartbeat_thread(void *arg);
+extern "C" void *dir_heartbeat_thread(void *arg);
+extern bool no_signals;
+
+/*
  * Listen on the SD socket for heartbeat signals.
  * Send heartbeats to the Director every HB_TIME
  *   seconds.
  */
-static void *heartbeat_thread(void *arg)
+extern "C" void *sd_heartbeat_thread(void *arg)
 {
    int32_t n;
    JCR *jcr = (JCR *)arg;
@@ -44,60 +47,151 @@ static void *heartbeat_thread(void *arg)
    time_t last_heartbeat = time(NULL);
    time_t now;
 
+   set_jcr_in_tsd(jcr);
    pthread_detach(pthread_self());
 
    /* Get our own local copy */
    sd = dup_bsock(jcr->store_bsock);
    dir = dup_bsock(jcr->dir_bsock);
 
-   jcr->duped_sd = sd;
+   jcr->hb_bsock = sd;
+   jcr->hb_started = true;
+   jcr->hb_dir_bsock = dir;
+   dir->suppress_error_messages(true);
+   sd->suppress_error_messages(true);
 
    /* Hang reading the socket to the SD, and every time we get
-    *  a heartbeat, we simply send it on to the Director to
-    *  keep him alive.
+    *   a heartbeat or we get a wait timeout (5 seconds), we
+    *   check to see if we need to send a heartbeat to the
+    *   Director.
     */
-   for ( ; !is_bnet_stop(sd); ) {
-      n = bnet_wait_data_intr(sd, 60);
+   while (!sd->is_stop()) {
+      n = sd->wait_data_intr(WAIT_INTERVAL);
+      if (n < 0 || sd->is_stop()) {
+         break;
+      }
       if (me->heartbeat_interval) {
-        now = time(NULL);
-        if (now-last_heartbeat >= me->heartbeat_interval) {
-           bnet_sig(dir, BNET_HEARTBEAT);
-           last_heartbeat = now;
-        }
+         now = time(NULL);
+         if (now-last_heartbeat >= me->heartbeat_interval) {
+            dir->signal(BNET_HEARTBEAT);
+            if (dir->is_stop()) {
+               break;
+            }
+            last_heartbeat = now;
+         }
       }
-      if (n == 1) {                  /* input waiting */
-        bnet_recv(sd);               /* read it -- probably heartbeat from sd */
-/*       Dmsg1(000, "Got %d from SD\n", sd->msglen); */
+      if (n == 1) {               /* input waiting */
+         sd->recv();              /* read it -- probably heartbeat from sd */
+         if (sd->is_stop()) {
+            break;
+         }
+         if (sd->msglen <= 0) {
+            Dmsg1(100, "Got BNET_SIG %d from SD\n", sd->msglen);
+         } else {
+            Dmsg2(100, "Got %d bytes from SD. MSG=%s\n", sd->msglen, sd->msg);
+         }
       }
+      Dmsg2(200, "wait_intr=%d stop=%d\n", n, sd->is_stop());
    }
-   bnet_close(sd);
-   bnet_close(dir);
-   jcr->duped_sd = NULL;
+   sd->close();
+   dir->close();
+   jcr->hb_bsock = NULL;
+   jcr->hb_started = false;
+   jcr->hb_dir_bsock = NULL;
    return NULL;
 }
 
 /* Startup the heartbeat thread -- see above */
 void start_heartbeat_monitor(JCR *jcr)
 {
-   jcr->duped_sd = NULL;
-   pthread_create(&jcr->heartbeat_id, NULL, heartbeat_thread, (void *)jcr);
+   /*
+    * If no signals are set, do not start the heartbeat because
+    * it gives a constant stream of TIMEOUT_SIGNAL signals that
+    * make debugging impossible.
+    */
+   if (!no_signals && (me->heartbeat_interval > 0)) {
+      jcr->hb_bsock = NULL;
+      jcr->hb_started = false;
+      jcr->hb_dir_bsock = NULL;
+      pthread_create(&jcr->heartbeat_id, NULL, sd_heartbeat_thread, (void *)jcr);
+   }
 }
 
-/* Terminate the heartbeat thread */
-void stop_heartbeat_monitor(JCR *jcr) 
+/* Terminate the heartbeat thread. Used for both SD and DIR */
+void stop_heartbeat_monitor(JCR *jcr)
 {
-   /* Wait for heartbeat thread to start */
-   while (jcr->duped_sd == NULL) {
-      bmicrosleep(0, 50);            /* avoid race */
+   int cnt = 0;
+   if (no_signals) {
+      return;
+   }
+   /* Wait max 10 secs for heartbeat thread to start */
+   while (!jcr->hb_started && cnt++ < 200) {
+      bmicrosleep(0, 50000);         /* wait for start */
+   }
+
+   if (jcr->hb_started) {
+      jcr->hb_bsock->set_timed_out();       /* set timed_out to terminate read */
+      jcr->hb_bsock->set_terminated();      /* set to terminate read */
    }
-   jcr->duped_sd->timed_out = 1;      /* set timed_out to terminate read */
-   jcr->duped_sd->terminated = 1;     /* set to terminate read */
+   if (jcr->hb_dir_bsock) {
+      jcr->hb_dir_bsock->set_timed_out();     /* set timed_out to terminate read */
+      jcr->hb_dir_bsock->set_terminated();    /* set to terminate read */
+   }
+   if (jcr->hb_started) {
+      Dmsg0(100, "Send kill to heartbeat id\n");
+      pthread_kill(jcr->heartbeat_id, TIMEOUT_SIGNAL);  /* make heartbeat thread go away */
+      bmicrosleep(0, 50000);
+   }
+   cnt = 0;
+   /* Wait max 100 secs for heartbeat thread to stop */
+   while (jcr->hb_started && cnt++ < 200) {
+      pthread_kill(jcr->heartbeat_id, TIMEOUT_SIGNAL);  /* make heartbeat thread go away */
+      bmicrosleep(0, 500000);
+   }
+}
+
+/*
+ * Thread for sending heartbeats to the Director when there
+ *   is no SD monitoring needed -- e.g. restore and verify Vol
+ *   both do their own read() on the SD socket.
+ */
+extern "C" void *dir_heartbeat_thread(void *arg)
+{
+   JCR *jcr = (JCR *)arg;
+   BSOCK *dir;
+   time_t last_heartbeat = time(NULL);
+
+   pthread_detach(pthread_self());
+
+   /* Get our own local copy */
+   dir = dup_bsock(jcr->dir_bsock);
+
+   jcr->hb_bsock = dir;
+   jcr->hb_started = true;
+   dir->suppress_error_messages(true);
+
+   while (!dir->is_stop()) {
+      time_t now, next;
 
-   /* Wait for heartbeat thread to stop */
-   while (jcr->duped_sd) {
-      pthread_kill(jcr->heartbeat_id, TIMEOUT_SIGNAL); /* make heartbeat thread go away */
-      bmicrosleep(0, 20);
+      now = time(NULL);
+      next = now - last_heartbeat;
+      if (next >= me->heartbeat_interval) {
+         dir->signal(BNET_HEARTBEAT);
+         if (dir->is_stop()) {
+            break;
+         }
+         last_heartbeat = now;
+      }
+      /* This should never happen, but it might ... */
+      if (next <= 0) {
+         next = 1;
+      }
+      bmicrosleep(next, 0);
    }
+   dir->close();
+   jcr->hb_bsock = NULL;
+   jcr->hb_started = false;
+   return NULL;
 }
 
 /*
@@ -105,10 +199,15 @@ void stop_heartbeat_monitor(JCR *jcr)
  */
 void start_dir_heartbeat(JCR *jcr)
 {
-   /* ***FIXME*** implement */
+   if (!no_signals && (me->heartbeat_interval > 0)) {
+      jcr->dir_bsock->set_locking();
+      pthread_create(&jcr->heartbeat_id, NULL, dir_heartbeat_thread, (void *)jcr);
+   }
 }
 
 void stop_dir_heartbeat(JCR *jcr)
 {
-   /* ***FIXME*** implement */
+   if (me->heartbeat_interval > 0) {
+      stop_heartbeat_monitor(jcr);
+   }
 }