LIBCTDB: add support for traverse
[sahlberg/ctdb.git] / libctdb / ctdb.c
index d60c8894db15aa1ddfe885869a3bbedfdb52a942..7115982d1a119651a2d270b06426e80c635a29dd 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"
@@ -42,14 +43,25 @@ struct ctdb_lock {
        struct ctdb_db *ctdb_db;
        TDB_DATA key;
 
-       /* This will always be true by the time user sees this. */
-       bool held;
+       /* This will always be set by the time user sees this. */
+       unsigned long held_magic;
        struct ctdb_ltdb_header *hdr;
 
        /* For convenience, we stash original callback here. */
        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);
        }
@@ -222,7 +282,7 @@ static struct ctdb_reply_call *unpack_reply_call(struct ctdb_connection *ctdb,
        /* Library user error if this isn't a reply to a call. */
        if (req->hdr.hdr->operation != CTDB_REQ_CALL) {
                errno = EINVAL;
-               DEBUG(ctdb, LOG_ERR,
+               DEBUG(ctdb, LOG_ALERT,
                      "This was not a ctdbd call request: operation %u",
                      req->hdr.hdr->operation);
                return NULL;
@@ -230,7 +290,7 @@ static struct ctdb_reply_call *unpack_reply_call(struct ctdb_connection *ctdb,
 
        if (req->hdr.call->callid != callid) {
                errno = EINVAL;
-               DEBUG(ctdb, LOG_ERR,
+               DEBUG(ctdb, LOG_ALERT,
                      "This was not a ctdbd %u call request: %u",
                      callid, req->hdr.call->callid);
                return NULL;
@@ -259,13 +319,13 @@ struct ctdb_reply_control *unpack_reply_control(struct ctdb_connection *ctdb,
        /* Library user error if this isn't a reply to a call. */
        if (len < sizeof(*inhdr)) {
                errno = EINVAL;
-               DEBUG(ctdb, LOG_CRIT,
+               DEBUG(ctdb, LOG_ALERT,
                      "Short ctdbd control reply: %zu bytes", len);
                return NULL;
        }
        if (req->hdr.hdr->operation != CTDB_REQ_CONTROL) {
                errno = EINVAL;
-               DEBUG(ctdb, LOG_ERR,
+               DEBUG(ctdb, LOG_ALERT,
                      "This was not a ctdbd control request: operation %u",
                      req->hdr.hdr->operation);
                return NULL;
@@ -274,7 +334,7 @@ struct ctdb_reply_control *unpack_reply_control(struct ctdb_connection *ctdb,
        /* ... or if it was a different control from what we expected. */
        if (req->hdr.control->opcode != control) {
                errno = EINVAL;
-               DEBUG(ctdb, LOG_ERR,
+               DEBUG(ctdb, LOG_ALERT,
                      "This was not an opcode %u ctdbd control request: %u",
                      control, req->hdr.control->opcode);
                return NULL;
@@ -335,7 +395,7 @@ bool ctdb_service(struct ctdb_connection *ctdb, int revents)
        }
 
        if (holding_lock(ctdb)) {
-               DEBUG(ctdb, LOG_WARNING, "Do not block while holding lock!");
+               DEBUG(ctdb, LOG_ALERT, "Do not block while holding lock!");
        }
 
        if (revents & POLLOUT) {
@@ -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",
@@ -588,7 +673,7 @@ static void destroy_req_db(struct ctdb_connection *ctdb,
 
 struct ctdb_request *
 ctdb_attachdb_send(struct ctdb_connection *ctdb,
-                  const char *name, int persistent, uint32_t tdb_flags,
+                  const char *name, bool persistent, uint32_t tdb_flags,
                   ctdb_callback_t callback, void *private_data)
 {
        struct ctdb_request *req;
@@ -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;
@@ -633,35 +718,47 @@ ctdb_attachdb_send(struct ctdb_connection *ctdb,
        return req;
 }
 
-void ctdb_release_lock(struct ctdb_lock *lock)
+static unsigned long lock_magic(struct ctdb_lock *lock)
 {
-       if (lock->held) {
-               tdb_chainunlock(lock->ctdb_db->tdb, lock->key);
-               DEBUG(lock->ctdb_db->ctdb, LOG_DEBUG,
-                     "ctdb_release_lock %p", lock);
-               lock->held = false;
-               remove_lock(lock->ctdb_db->ctdb, lock);
-       }
+       /* A non-zero magic specific to this structure. */
+       return ((unsigned long)lock->key.dptr
+               ^ (((unsigned long)lock->key.dptr) << 16)
+               ^ 0xBADC0FFEEBADC0DEULL)
+               | 1;
 }
 
-static void ctdb_free_lock(struct ctdb_lock *lock)
+/* This is only called on locks before they're held. */
+static void free_lock(struct ctdb_lock *lock)
 {
-       if (lock->held) {
-               errno = EEXIST;
-               DEBUG(lock->ctdb_db->ctdb, LOG_ERR,
-                       "Lock freed before it was released");
-               ctdb_release_lock(lock);
+       if (lock->held_magic) {
+               DEBUG(lock->ctdb_db->ctdb, LOG_ALERT,
+                     "free_lock invalid lock %p", lock);
        }
        free(lock->hdr);
        free(lock);
 }
 
-static void ctdb_destroy_lock(struct ctdb_lock *lock)
+
+void ctdb_release_lock(struct ctdb_db *ctdb_db, struct ctdb_lock *lock)
 {
-       ctdb_release_lock(lock);
-       ctdb_free_lock(lock);
+       if (lock->held_magic != lock_magic(lock)) {
+               DEBUG(lock->ctdb_db->ctdb, LOG_ALERT,
+                     "ctdb_release_lock invalid lock %p", lock);
+       } else if (lock->ctdb_db != ctdb_db) {
+               errno = EBADF;
+               DEBUG(ctdb_db->ctdb, LOG_ALERT,
+                     "ctdb_release_lock: wrong ctdb_db.");
+       } else {
+               tdb_chainunlock(lock->ctdb_db->tdb, lock->key);
+               DEBUG(lock->ctdb_db->ctdb, LOG_DEBUG,
+                     "ctdb_release_lock %p", lock);
+               remove_lock(lock->ctdb_db->ctdb, lock);
+       }
+       lock->held_magic = 0;
+       free_lock(lock);
 }
 
+
 /* We keep the lock if local node is the dmaster. */
 static bool try_readrecordlock(struct ctdb_lock *lock, TDB_DATA *data)
 {
@@ -677,7 +774,7 @@ static bool try_readrecordlock(struct ctdb_lock *lock, TDB_DATA *data)
        if (hdr && hdr->dmaster == lock->ctdb_db->ctdb->pnn) {
                DEBUG(lock->ctdb_db->ctdb, LOG_DEBUG,
                      "ctdb_readrecordlock_async: got local lock");
-               lock->held = true;
+               lock->held_magic = lock_magic(lock);
                lock->hdr = hdr;
                add_lock(lock->ctdb_db->ctdb, lock);
                return true;
@@ -692,7 +789,7 @@ static bool try_readrecordlock(struct ctdb_lock *lock, TDB_DATA *data)
 static void destroy_lock(struct ctdb_connection *ctdb,
                         struct ctdb_request *req)
 {
-       ctdb_destroy_lock(req->extra);
+       free_lock(req->extra);
 }
 
 static void readrecordlock_retry(struct ctdb_connection *ctdb,
@@ -720,7 +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_free_lock(lock);
+               ctdb_request_free(ctdb, req);
                return;
        }
 
@@ -738,7 +835,7 @@ ctdb_readrecordlock_async(struct ctdb_db *ctdb_db, TDB_DATA key,
        TDB_DATA data;
 
        if (holding_lock(ctdb_db->ctdb)) {
-               DEBUG(ctdb_db->ctdb, LOG_ERR,
+               DEBUG(ctdb_db->ctdb, LOG_ALERT,
                      "ctdb_readrecordlock_async: already holding lock");
                return false;
        }
@@ -755,12 +852,11 @@ ctdb_readrecordlock_async(struct ctdb_db *ctdb_db, TDB_DATA key,
        lock->key.dsize = key.dsize;
        lock->ctdb_db = ctdb_db;
        lock->hdr = NULL;
-       lock->held = false;
+       lock->held_magic = 0;
 
        /* Fast path. */
        if (try_readrecordlock(lock, &data)) {
                callback(ctdb_db, lock, data, cbdata);
-               ctdb_free_lock(lock);
                return true;
        }
 
@@ -770,7 +866,7 @@ ctdb_readrecordlock_async(struct ctdb_db *ctdb_db, TDB_DATA key,
        if (!req) {
                DEBUG(ctdb_db->ctdb, LOG_ERR,
                      "ctdb_readrecordlock_async: allocation failed");
-               ctdb_destroy_lock(lock);
+               free_lock(lock);
                return NULL;
        }
        req->extra = lock;
@@ -792,15 +888,234 @@ ctdb_readrecordlock_async(struct ctdb_db *ctdb_db, TDB_DATA key,
        return true;
 }
 
-int ctdb_writerecord(struct ctdb_lock *lock, TDB_DATA data)
+bool ctdb_writerecord(struct ctdb_db *ctdb_db,
+                     struct ctdb_lock *lock, TDB_DATA data)
 {
-       if (lock->ctdb_db->persistent) {
+       if (lock->ctdb_db != ctdb_db) {
+               errno = EBADF;
+               DEBUG(ctdb_db->ctdb, LOG_ALERT,
+                     "ctdb_writerecord: Can not write, wrong ctdb_db.");
+               return false;
+       }
+
+       if (lock->held_magic != lock_magic(lock)) {
+               errno = EBADF;
+               DEBUG(ctdb_db->ctdb, LOG_ALERT,
+                     "ctdb_writerecord: Can not write. Lock has been released.");
+               return false;
+       }
+               
+       if (ctdb_db->persistent) {
                errno = EINVAL;
-               DEBUG(lock->ctdb_db->ctdb, LOG_ERR,
+               DEBUG(ctdb_db->ctdb, LOG_ALERT,
                      "ctdb_writerecord: cannot write to persistent db");
-               return -1;
+               return false;
        }
 
-       return ctdb_local_store(lock->ctdb_db->tdb, lock->key, lock->hdr,
-                               data);
+       switch (ctdb_local_store(ctdb_db->tdb, lock->key, lock->hdr, data)) {
+       case 0:
+               DEBUG(ctdb_db->ctdb, LOG_DEBUG,
+                     "ctdb_writerecord: optimized away noop write.");
+               /* fall thru */
+       case 1:
+               return true;
+
+       default:
+               switch (errno) {
+               case ENOMEM:
+                       DEBUG(ctdb_db->ctdb, LOG_CRIT,
+                             "ctdb_writerecord: out of memory.");
+                       break;
+               case EINVAL:
+                       DEBUG(ctdb_db->ctdb, LOG_ALERT,
+                             "ctdb_writerecord: record changed under lock?");
+                       break;
+               default: /* TDB already logged. */
+                       break;
+               }
+               return false;
+       }
+}
+
+
+struct ctdb_traverse_state {
+       struct ctdb_request *handle;
+       struct ctdb_db *ctdb_db;
+       uint64_t srvid;
+
+       ctdb_traverse_callback_t callback;
+       void *cbdata;
+};
+
+static void traverse_remhnd_cb(struct ctdb_connection *ctdb,
+                        struct ctdb_request *req, void *private_data)
+{
+       struct ctdb_traverse_state *state = private_data;
+
+       if (!ctdb_remove_message_handler_recv(ctdb, state->handle)) {
+               DEBUG(ctdb, LOG_ERR,
+                               "Failed to remove message handler for"
+                               " traverse.");
+               state->callback(state->ctdb_db->ctdb, state->ctdb_db,
+                               TRAVERSE_STATUS_ERROR,
+                               tdb_null, tdb_null,
+                               state->cbdata);
+       }
+       ctdb_request_free(ctdb, state->handle);
+       state->handle = NULL;
+       free(state);
+}
+       
+static void msg_h(struct ctdb_connection *ctdb, uint64_t srvid,
+          TDB_DATA data, void *private_data)
+{
+       struct ctdb_traverse_state *state = private_data;
+       struct ctdb_db *ctdb_db = state->ctdb_db;
+       struct ctdb_rec_data *d = (struct ctdb_rec_data *)data.dptr;
+       TDB_DATA key;
+
+       if (data.dsize < sizeof(uint32_t) ||
+           d->length != data.dsize) {
+               DEBUG(ctdb, LOG_ERR,
+                       "Bad data size %u in traverse_handler",
+                       (unsigned)data.dsize);
+               state->callback(state->ctdb_db->ctdb, state->ctdb_db,
+                               TRAVERSE_STATUS_ERROR,
+                               tdb_null, tdb_null,
+                               state->cbdata);
+               state->handle = ctdb_remove_message_handler_send(
+                               state->ctdb_db->ctdb, state->srvid,
+                               msg_h, state,
+                               traverse_remhnd_cb, state);
+               return;
+       }
+
+       key.dsize = d->keylen;
+       key.dptr  = &d->data[0];
+       data.dsize = d->datalen;
+       data.dptr = &d->data[d->keylen];
+
+       if (key.dsize == 0 && data.dsize == 0) {
+               state->callback(state->ctdb_db->ctdb, state->ctdb_db,
+                               TRAVERSE_STATUS_FINISHED,
+                               tdb_null, tdb_null,
+                               state->cbdata);
+               state->handle = ctdb_remove_message_handler_send(
+                               state->ctdb_db->ctdb, state->srvid,
+                               msg_h, state,
+                               traverse_remhnd_cb, state);
+               return;
+       }
+
+       if (data.dsize <= sizeof(struct ctdb_ltdb_header)) {
+               /* empty records are deleted records in ctdb */
+               return;
+       }
+
+       data.dsize -= sizeof(struct ctdb_ltdb_header);
+       data.dptr  += sizeof(struct ctdb_ltdb_header);
+
+       if (state->callback(ctdb, ctdb_db,
+                       TRAVERSE_STATUS_RECORD,
+                       key, data, state->cbdata) != 0) {
+               state->handle = ctdb_remove_message_handler_send(
+                               state->ctdb_db->ctdb, state->srvid,
+                               msg_h, state,
+                               traverse_remhnd_cb, state);
+               return;
+       }
+}
+
+static void traverse_start_cb(struct ctdb_connection *ctdb,
+                        struct ctdb_request *req, void *private_data)
+{
+       struct ctdb_traverse_state *state = private_data;
+
+        ctdb_request_free(ctdb, state->handle);
+       state->handle = NULL;
+}
+
+static void traverse_msghnd_cb(struct ctdb_connection *ctdb,
+                        struct ctdb_request *req, void *private_data)
+{
+       struct ctdb_traverse_state *state = private_data;
+       struct ctdb_db *ctdb_db = state->ctdb_db;
+       struct ctdb_traverse_start t;
+
+       if (!ctdb_set_message_handler_recv(ctdb, state->handle)) {
+               DEBUG(ctdb, LOG_ERR,
+                               "Failed to register message handler for"
+                               " traverse.");
+               state->callback(state->ctdb_db->ctdb, state->ctdb_db,
+                               TRAVERSE_STATUS_ERROR,
+                               tdb_null, tdb_null,
+                               state->cbdata);
+               ctdb_request_free(ctdb, state->handle);
+               state->handle = NULL;
+               free(state);
+               return;
+        }
+        ctdb_request_free(ctdb, state->handle);
+       state->handle = NULL;
+
+       t.db_id = ctdb_db->id;
+       t.srvid = state->srvid;
+       t.reqid = 0;
+
+       state->handle = new_ctdb_control_request(ctdb,
+                               CTDB_CONTROL_TRAVERSE_START,
+                               CTDB_CURRENT_NODE,
+                               &t, sizeof(t),
+                               traverse_start_cb, state);
+       if (state->handle == NULL) {
+               DEBUG(ctdb, LOG_ERR,
+                               "ctdb_traverse_async:"
+                               " failed to send traverse_start control");
+               state->callback(state->ctdb_db->ctdb, state->ctdb_db,
+                               TRAVERSE_STATUS_ERROR,
+                               tdb_null, tdb_null,
+                               state->cbdata);
+               state->handle = ctdb_remove_message_handler_send(
+                               state->ctdb_db->ctdb, state->srvid,
+                               msg_h, state,
+                               traverse_remhnd_cb, state);
+               return;
+       }
+}
+
+bool ctdb_traverse_async(struct ctdb_db *ctdb_db,
+                        ctdb_traverse_callback_t callback, void *cbdata)
+{
+       struct ctdb_connection *ctdb = ctdb_db->ctdb;
+       struct ctdb_traverse_state *state;
+       static uint32_t tid = 0;
+
+       state = malloc(sizeof(struct ctdb_traverse_state));
+       if (state == NULL) {
+               DEBUG(ctdb, LOG_ERR,
+                               "ctdb_traverse_async: no memory."
+                               " allocate state failed");
+               return false;
+       }
+
+       tid++;
+       state->srvid = CTDB_SRVID_TRAVERSE_RANGE|tid;
+
+       state->callback = callback;
+       state->cbdata   = cbdata;
+       state->ctdb_db  = ctdb_db;
+
+       state->handle = ctdb_set_message_handler_send(ctdb_db->ctdb,
+                               state->srvid,
+                               msg_h, state,
+                               traverse_msghnd_cb, state);
+       if (state->handle == NULL) {
+               DEBUG(ctdb, LOG_ERR,
+                       "ctdb_traverse_async:"
+                       " failed ctdb_set_message_handler_send");
+               free(state);
+               return false;
+       }
+
+       return true;
 }