Eventscripts: lower the fail/restart limits for nfsd.
[sahlberg/ctdb.git] / libctdb / ctdb.c
index 8b248f18ec8e7983ed5eabfa9dae6d99f5a99554..e06c66ca85497d3b1904ea89d77a31745721b1bd 100644 (file)
@@ -24,6 +24,7 @@
 #include <stdlib.h>
 #include <sys/socket.h>
 #include <sys/un.h>
+#include <sys/ioctl.h>
 #include "libctdb_private.h"
 #include "io_elem.h"
 #include "local_tdb.h"
@@ -50,6 +51,17 @@ struct ctdb_lock {
        ctdb_rrl_callback_t callback;
 };
 
+struct ctdb_db {
+       struct ctdb_connection *ctdb;
+       bool persistent;
+       uint32_t tdb_flags;
+       uint32_t id;
+       struct tdb_context *tdb;
+
+       ctdb_callback_t callback;
+       void *private_data;
+};
+
 static void remove_lock(struct ctdb_connection *ctdb, struct ctdb_lock *lock)
 {
        DLIST_REMOVE(ctdb->locks, lock);
@@ -67,6 +79,19 @@ static void add_lock(struct ctdb_connection *ctdb, struct ctdb_lock *lock)
        DLIST_ADD(ctdb->locks, lock);
 }
 
+static void cleanup_locks(struct ctdb_connection *ctdb, struct ctdb_db *db)
+{
+       struct ctdb_lock *i, *next;
+
+       for (i = ctdb->locks; i; i = next) {
+               /* Grab next pointer, as release_lock will free i */
+               next = i->next;
+               if (i->ctdb_db == db) {
+                       ctdb_release_lock(db, i);
+               }
+       }
+}
+
 /* FIXME: Could be in shared util code with rest of ctdb */
 static void close_noerr(int fd)
 {
@@ -129,6 +154,7 @@ struct ctdb_connection *ctdb_connect(const char *addr,
        ctdb->outq = NULL;
        ctdb->doneq = NULL;
        ctdb->in = NULL;
+       ctdb->inqueue = NULL;
        ctdb->message_handlers = NULL;
        ctdb->next_id = 0;
        ctdb->broken = false;
@@ -165,6 +191,33 @@ fail:
        return NULL;
 }
 
+void ctdb_disconnect(struct ctdb_connection *ctdb)
+{
+       struct ctdb_request *i;
+
+       DEBUG(ctdb, LOG_DEBUG, "ctdb_disconnect");
+
+       while ((i = ctdb->outq) != NULL) {
+               DLIST_REMOVE(ctdb->outq, i);
+               ctdb_request_free(ctdb, i);
+       }
+
+       while ((i = ctdb->doneq) != NULL) {
+               DLIST_REMOVE(ctdb->doneq, i);
+               ctdb_request_free(ctdb, i);
+       }
+
+       if (ctdb->in)
+               free_io_elem(ctdb->in);
+
+       remove_message_handlers(ctdb);
+
+       close(ctdb->fd);
+       /* Just in case they try to reuse */
+       ctdb->fd = -1;
+       free(ctdb);
+}
+
 int ctdb_get_fd(struct ctdb_connection *ctdb)
 {
        return ctdb->fd;
@@ -201,6 +254,13 @@ struct ctdb_request *new_ctdb_request(size_t len,
 
 void ctdb_request_free(struct ctdb_connection *ctdb, struct ctdb_request *req)
 {
+       if (req->next || req->prev) {
+               DEBUG(ctdb, LOG_ALERT,
+                     "ctdb_request_free: request not complete! ctdb_cancel? %p (id %u)",
+                     req, req->hdr.hdr ? req->hdr.hdr->reqid : 0);
+               ctdb_cancel(ctdb, req);
+               return;
+       }
        if (req->extra_destructor) {
                req->extra_destructor(ctdb, req);
        }
@@ -359,6 +419,19 @@ bool ctdb_service(struct ctdb_connection *ctdb, int revents)
 
        while (revents & POLLIN) {
                int ret;
+               int num_ready = 0;
+
+               if (ioctl(ctdb->fd, FIONREAD, &num_ready) != 0) {
+                       DEBUG(ctdb, LOG_ERR,
+                             "ctdb_service: ioctl(FIONREAD) %d", errno);
+                       ctdb->broken = true;
+                       return false;
+               }
+               if (num_ready == 0) {
+                       /* the descriptor has been closed or we have all our data */
+                       break;
+               }
+
 
                if (!ctdb->in) {
                        ctdb->in = new_io_elem(sizeof(struct ctdb_req_header));
@@ -381,13 +454,21 @@ bool ctdb_service(struct ctdb_connection *ctdb, int revents)
                        return false;
                } else if (ret < 0) {
                        /* No progress, stop loop. */
-                       revents = 0;
+                       break;
                } else if (io_elem_finished(ctdb->in)) {
-                       handle_incoming(ctdb, ctdb->in);
+                       io_elem_queue(ctdb, ctdb->in);
                        ctdb->in = NULL;
                }
        }
 
+
+       while (ctdb->inqueue != NULL) {
+               struct io_elem *io = ctdb->inqueue;
+
+               io_elem_dequeue(ctdb, io);
+               handle_incoming(ctdb, io);
+       }
+
        return true;
 }
 
@@ -456,6 +537,14 @@ void ctdb_cancel_callback(struct ctdb_connection *ctdb,
 
 void ctdb_cancel(struct ctdb_connection *ctdb, struct ctdb_request *req)
 {
+       if (!req->next && !req->prev) {
+               DEBUG(ctdb, LOG_ALERT,
+                     "ctdb_cancel: request completed! ctdb_request_free? %p (id %u)",
+                     req, req->hdr.hdr ? req->hdr.hdr->reqid : 0);
+               ctdb_request_free(ctdb, req);
+               return;
+       }
+
        DEBUG(ctdb, LOG_DEBUG, "ctdb_cancel: %p (id %u)",
              req, req->hdr.hdr ? req->hdr.hdr->reqid : 0);
 
@@ -463,19 +552,12 @@ void ctdb_cancel(struct ctdb_connection *ctdb, struct ctdb_request *req)
        req->callback = ctdb_cancel_callback;
 }
 
-struct ctdb_db {
-       struct ctdb_connection *ctdb;
-       bool persistent;
-       uint32_t tdb_flags;
-       uint32_t id;
-       struct tdb_context *tdb;
-
-       /* The lock we are holding, if any (we can only have one!) */
-       struct ctdb_lock *lock;
-
-       ctdb_callback_t callback;
-       void *private_data;
-};
+void ctdb_detachdb(struct ctdb_connection *ctdb, struct ctdb_db *db)
+{
+       cleanup_locks(ctdb, db);
+       tdb_close(db->tdb);
+       free(db);
+}
 
 static void attachdb_getdbpath_done(struct ctdb_connection *ctdb,
                                    struct ctdb_request *req,
@@ -494,6 +576,7 @@ struct ctdb_db *ctdb_attachdb_recv(struct ctdb_connection *ctdb,
        struct ctdb_reply_control *reply;
        struct ctdb_db *db = req->priv_data;
        uint32_t tdb_flags = db->tdb_flags;
+       struct tdb_logging_context log;
 
        /* Never sent the dbpath request?  We've failed. */
        if (!dbpath_req) {
@@ -515,8 +598,10 @@ struct ctdb_db *ctdb_attachdb_recv(struct ctdb_connection *ctdb,
        tdb_flags = db->persistent ? TDB_DEFAULT : TDB_NOSYNC;
        tdb_flags |= TDB_DISALLOW_NESTING;
 
-       /* FIXME: Setup logging to go through our logging. */
-       db->tdb = tdb_open((char *)reply->data, 0, tdb_flags, O_RDWR, 0);
+       log.log_fn = ctdb_tdb_log_bridge;
+       log.log_private = ctdb;
+       db->tdb = tdb_open_ex((char *)reply->data, 0, tdb_flags, O_RDWR, 0,
+                             &log, NULL);
        if (db->tdb == NULL) {
                DEBUG(db->ctdb, LOG_ERR,
                      "ctdb_attachdb_recv: failed to tdb_open %s",
@@ -610,7 +695,7 @@ ctdb_attachdb_send(struct ctdb_connection *ctdb,
        req = new_ctdb_control_request(ctdb, opcode, CTDB_CURRENT_NODE, name,
                                       strlen(name) + 1, attachdb_done, db);
        if (!req) {
-               DEBUG(db->ctdb, LOG_ERR,
+               DEBUG(ctdb, LOG_ERR,
                      "ctdb_attachdb_send: failed allocating DB_ATTACH");
                free(db);
                return NULL;
@@ -732,6 +817,7 @@ static void readrecordlock_retry(struct ctdb_connection *ctdb,
                /* Now it's their responsibility to free lock & request! */
                req->extra_destructor = NULL;
                lock->callback(lock->ctdb_db, lock, data, private);
+               ctdb_request_free(ctdb, req);
                return;
        }
 
@@ -844,10 +930,7 @@ bool ctdb_writerecord(struct ctdb_db *ctdb_db,
                        DEBUG(ctdb_db->ctdb, LOG_ALERT,
                              "ctdb_writerecord: record changed under lock?");
                        break;
-               default:
-                       /* FIXME: replace with proper tdb logging. */
-                       DEBUG(ctdb_db->ctdb, LOG_CRIT,
-                             "ctdb_writerecord: tdb error.");
+               default: /* TDB already logged. */
                        break;
                }
                return false;