4 Copyright (C) Rusty Russell 2010
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
25 #include <sys/socket.h>
27 #include <sys/ioctl.h>
28 #include "libctdb_private.h"
30 #include "local_tdb.h"
32 #include <dlinklist.h>
33 #include <ctdb_protocol.h>
35 /* Remove type-safety macros. */
36 #undef ctdb_attachdb_send
37 #undef ctdb_readrecordlock_async
41 struct ctdb_lock *next, *prev;
43 struct ctdb_db *ctdb_db;
46 /* This will always be set by the time user sees this. */
47 unsigned long held_magic;
48 struct ctdb_ltdb_header *hdr;
50 /* For convenience, we stash original callback here. */
51 ctdb_rrl_callback_t callback;
55 struct ctdb_connection *ctdb;
59 struct tdb_context *tdb;
61 ctdb_callback_t callback;
65 static void remove_lock(struct ctdb_connection *ctdb, struct ctdb_lock *lock)
67 DLIST_REMOVE(ctdb->locks, lock);
70 /* FIXME: for thread safety, need tid info too. */
71 static bool holding_lock(struct ctdb_connection *ctdb)
73 /* For the moment, you can't ever hold more than 1 lock. */
74 return (ctdb->locks != NULL);
77 static void add_lock(struct ctdb_connection *ctdb, struct ctdb_lock *lock)
79 DLIST_ADD(ctdb->locks, lock);
82 static void cleanup_locks(struct ctdb_connection *ctdb, struct ctdb_db *db)
84 struct ctdb_lock *i, *next;
86 for (i = ctdb->locks; i; i = next) {
87 /* Grab next pointer, as release_lock will free i */
89 if (i->ctdb_db == db) {
90 ctdb_release_lock(db, i);
95 /* FIXME: Could be in shared util code with rest of ctdb */
96 static void close_noerr(int fd)
103 /* FIXME: Could be in shared util code with rest of ctdb */
104 static void free_noerr(void *p)
111 /* FIXME: Could be in shared util code with rest of ctdb */
112 static void set_nonblocking(int fd)
115 v = fcntl(fd, F_GETFL, 0);
116 fcntl(fd, F_SETFL, v | O_NONBLOCK);
119 /* FIXME: Could be in shared util code with rest of ctdb */
120 static void set_close_on_exec(int fd)
123 v = fcntl(fd, F_GETFD, 0);
124 fcntl(fd, F_SETFD, v | FD_CLOEXEC);
127 static void set_pnn(struct ctdb_connection *ctdb,
128 struct ctdb_request *req,
131 if (!ctdb_getpnn_recv(ctdb, req, &ctdb->pnn)) {
132 DEBUG(ctdb, LOG_CRIT,
133 "ctdb_connect(async): failed to get pnn");
136 ctdb_request_free(req);
139 struct ctdb_connection *ctdb_connect(const char *addr,
140 ctdb_log_fn_t log_fn, void *log_priv)
142 struct ctdb_connection *ctdb;
143 struct sockaddr_un sun;
145 ctdb = malloc(sizeof(*ctdb));
147 /* With no format string, we hope it doesn't use ap! */
149 memset(&ap, 0, sizeof(ap));
151 log_fn(log_priv, LOG_ERR, "ctdb_connect: no memory", ap);
158 ctdb->inqueue = NULL;
159 ctdb->message_handlers = NULL;
161 ctdb->broken = false;
163 ctdb->log_priv = log_priv;
166 memset(&sun, 0, sizeof(sun));
167 sun.sun_family = AF_UNIX;
170 strncpy(sun.sun_path, addr, sizeof(sun.sun_path)-1);
171 ctdb->fd = socket(AF_UNIX, SOCK_STREAM, 0);
175 set_nonblocking(ctdb->fd);
176 set_close_on_exec(ctdb->fd);
178 if (connect(ctdb->fd, (struct sockaddr *)&sun, sizeof(sun)) == -1)
181 /* Immediately queue a request to get our pnn. */
182 if (!ctdb_getpnn_send(ctdb, CTDB_CURRENT_NODE, set_pnn, NULL))
188 close_noerr(ctdb->fd);
195 void ctdb_disconnect(struct ctdb_connection *ctdb)
197 struct ctdb_request *i;
199 DEBUG(ctdb, LOG_DEBUG, "ctdb_disconnect");
201 while ((i = ctdb->outq) != NULL) {
202 DLIST_REMOVE(ctdb->outq, i);
203 ctdb_request_free(i);
206 while ((i = ctdb->doneq) != NULL) {
207 DLIST_REMOVE(ctdb->doneq, i);
208 ctdb_request_free(i);
212 free_io_elem(ctdb->in);
214 remove_message_handlers(ctdb);
217 /* Just in case they try to reuse */
222 int ctdb_get_fd(struct ctdb_connection *ctdb)
227 int ctdb_which_events(struct ctdb_connection *ctdb)
236 struct ctdb_request *new_ctdb_request(struct ctdb_connection *ctdb, size_t len,
237 ctdb_callback_t cb, void *cbdata)
239 struct ctdb_request *req = malloc(sizeof(*req));
242 req->io = new_io_elem(len);
248 req->hdr.hdr = io_elem_data(req->io, NULL);
251 req->priv_data = cbdata;
253 req->extra_destructor = NULL;
257 void ctdb_request_free(struct ctdb_request *req)
259 struct ctdb_connection *ctdb = req->ctdb;
261 if (req->next || req->prev) {
262 DEBUG(ctdb, LOG_ALERT,
263 "ctdb_request_free: request not complete! ctdb_cancel? %p (id %u)",
264 req, req->hdr.hdr ? req->hdr.hdr->reqid : 0);
265 ctdb_cancel(ctdb, req);
268 if (req->extra_destructor) {
269 req->extra_destructor(ctdb, req);
272 free_io_elem(req->reply);
274 free_io_elem(req->io);
278 /* Sanity-checking wrapper for reply. */
279 static struct ctdb_reply_call *unpack_reply_call(struct ctdb_request *req,
283 struct ctdb_reply_call *inhdr = io_elem_data(req->reply, &len);
285 /* Library user error if this isn't a reply to a call. */
286 if (req->hdr.hdr->operation != CTDB_REQ_CALL) {
288 DEBUG(req->ctdb, LOG_ALERT,
289 "This was not a ctdbd call request: operation %u",
290 req->hdr.hdr->operation);
294 if (req->hdr.call->callid != callid) {
296 DEBUG(req->ctdb, LOG_ALERT,
297 "This was not a ctdbd %u call request: %u",
298 callid, req->hdr.call->callid);
302 /* ctdbd or our error if this isn't a reply call. */
303 if (len < sizeof(*inhdr) || inhdr->hdr.operation != CTDB_REPLY_CALL) {
305 DEBUG(req->ctdb, LOG_CRIT,
306 "Invalid ctdbd call reply: len %zu, operation %u",
307 len, inhdr->hdr.operation);
314 /* Sanity-checking wrapper for reply. */
315 struct ctdb_reply_control *unpack_reply_control(struct ctdb_request *req,
316 enum ctdb_controls control)
319 struct ctdb_reply_control *inhdr = io_elem_data(req->reply, &len);
321 /* Library user error if this isn't a reply to a call. */
322 if (len < sizeof(*inhdr)) {
324 DEBUG(req->ctdb, LOG_ALERT,
325 "Short ctdbd control reply: %zu bytes", len);
328 if (req->hdr.hdr->operation != CTDB_REQ_CONTROL) {
330 DEBUG(req->ctdb, LOG_ALERT,
331 "This was not a ctdbd control request: operation %u",
332 req->hdr.hdr->operation);
336 /* ... or if it was a different control from what we expected. */
337 if (req->hdr.control->opcode != control) {
339 DEBUG(req->ctdb, LOG_ALERT,
340 "This was not an opcode %u ctdbd control request: %u",
341 control, req->hdr.control->opcode);
345 /* ctdbd or our error if this isn't a reply call. */
346 if (inhdr->hdr.operation != CTDB_REPLY_CONTROL) {
348 DEBUG(req->ctdb, LOG_CRIT,
349 "Invalid ctdbd control reply: operation %u",
350 inhdr->hdr.operation);
357 static void handle_incoming(struct ctdb_connection *ctdb, struct io_elem *in)
359 struct ctdb_req_header *hdr;
361 struct ctdb_request *i;
363 hdr = io_elem_data(in, &len);
364 /* FIXME: use len to check packet! */
366 if (hdr->operation == CTDB_REQ_MESSAGE) {
367 deliver_message(ctdb, hdr);
371 for (i = ctdb->doneq; i; i = i->next) {
372 if (i->hdr.hdr->reqid == hdr->reqid) {
373 DLIST_REMOVE(ctdb->doneq, i);
375 i->callback(ctdb, i, i->priv_data);
379 DEBUG(ctdb, LOG_WARNING,
380 "Unexpected ctdbd request reply: operation %u reqid %u",
381 hdr->operation, hdr->reqid);
385 /* Remove "harmless" errors. */
386 static ssize_t real_error(ssize_t ret)
388 if (ret < 0 && (errno == EINTR || errno == EWOULDBLOCK))
393 bool ctdb_service(struct ctdb_connection *ctdb, int revents)
399 if (holding_lock(ctdb)) {
400 DEBUG(ctdb, LOG_ALERT, "Do not block while holding lock!");
403 if (revents & POLLOUT) {
405 if (real_error(write_io_elem(ctdb->fd,
406 ctdb->outq->io)) < 0) {
408 "ctdb_service: error writing to ctdbd");
412 if (io_elem_finished(ctdb->outq->io)) {
413 struct ctdb_request *done = ctdb->outq;
414 DLIST_REMOVE(ctdb->outq, done);
415 /* We add at the head: any dead ones
417 DLIST_ADD(ctdb->doneq, done);
422 while (revents & POLLIN) {
426 if (ioctl(ctdb->fd, FIONREAD, &num_ready) != 0) {
428 "ctdb_service: ioctl(FIONREAD) %d", errno);
432 if (num_ready == 0) {
433 /* the descriptor has been closed or we have all our data */
439 ctdb->in = new_io_elem(sizeof(struct ctdb_req_header));
442 "ctdb_service: allocating readbuf");
448 ret = read_io_elem(ctdb->fd, ctdb->in);
449 if (real_error(ret) < 0 || ret == 0) {
450 /* They closed fd? */
454 "ctdb_service: error reading from ctdbd");
457 } else if (ret < 0) {
458 /* No progress, stop loop. */
460 } else if (io_elem_finished(ctdb->in)) {
461 io_elem_queue(ctdb, ctdb->in);
467 while (ctdb->inqueue != NULL) {
468 struct io_elem *io = ctdb->inqueue;
470 io_elem_dequeue(ctdb, io);
471 handle_incoming(ctdb, io);
477 /* This is inefficient. We could pull in idtree.c. */
478 static bool reqid_used(const struct ctdb_connection *ctdb, uint32_t reqid)
480 struct ctdb_request *i;
482 for (i = ctdb->outq; i; i = i->next) {
483 if (i->hdr.hdr->reqid == reqid) {
487 for (i = ctdb->doneq; i; i = i->next) {
488 if (i->hdr.hdr->reqid == reqid) {
495 uint32_t new_reqid(struct ctdb_connection *ctdb)
497 while (reqid_used(ctdb, ctdb->next_id)) {
500 return ctdb->next_id++;
503 struct ctdb_request *new_ctdb_control_request(struct ctdb_connection *ctdb,
506 const void *extra_data,
508 ctdb_callback_t callback,
511 struct ctdb_request *req;
512 struct ctdb_req_control *pkt;
514 req = new_ctdb_request(
515 ctdb, offsetof(struct ctdb_req_control, data) + extra,
520 io_elem_init_req_header(req->io,
521 CTDB_REQ_CONTROL, destnode, new_reqid(ctdb));
523 pkt = req->hdr.control;
525 pkt->opcode = opcode;
529 pkt->datalen = extra;
530 memcpy(pkt->data, extra_data, extra);
531 DLIST_ADD(ctdb->outq, req);
535 void ctdb_cancel_callback(struct ctdb_connection *ctdb,
536 struct ctdb_request *req,
539 ctdb_request_free(req);
542 void ctdb_cancel(struct ctdb_connection *ctdb, struct ctdb_request *req)
544 if (!req->next && !req->prev) {
545 DEBUG(ctdb, LOG_ALERT,
546 "ctdb_cancel: request completed! ctdb_request_free? %p (id %u)",
547 req, req->hdr.hdr ? req->hdr.hdr->reqid : 0);
548 ctdb_request_free(req);
552 DEBUG(ctdb, LOG_DEBUG, "ctdb_cancel: %p (id %u)",
553 req, req->hdr.hdr ? req->hdr.hdr->reqid : 0);
555 /* FIXME: If it's not sent, we could just free it right now. */
556 req->callback = ctdb_cancel_callback;
559 void ctdb_detachdb(struct ctdb_connection *ctdb, struct ctdb_db *db)
561 cleanup_locks(ctdb, db);
566 static void destroy_req_db(struct ctdb_connection *ctdb,
567 struct ctdb_request *req);
568 static void attachdb_done(struct ctdb_connection *ctdb,
569 struct ctdb_request *req,
571 static void attachdb_getdbpath_done(struct ctdb_connection *ctdb,
572 struct ctdb_request *req,
575 struct ctdb_request *
576 ctdb_attachdb_send(struct ctdb_connection *ctdb,
577 const char *name, bool persistent, uint32_t tdb_flags,
578 ctdb_callback_t callback, void *private_data)
580 struct ctdb_request *req;
584 /* FIXME: Search if db already open. */
585 db = malloc(sizeof(*db));
591 opcode = CTDB_CONTROL_DB_ATTACH_PERSISTENT;
593 opcode = CTDB_CONTROL_DB_ATTACH;
596 req = new_ctdb_control_request(ctdb, opcode, CTDB_CURRENT_NODE, name,
597 strlen(name) + 1, attachdb_done, db);
600 "ctdb_attachdb_send: failed allocating DB_ATTACH");
606 db->tdb_flags = tdb_flags;
607 db->persistent = persistent;
608 db->callback = callback;
609 db->private_data = private_data;
611 req->extra_destructor = destroy_req_db;
612 /* This is set non-NULL when we succeed, see ctdb_attachdb_recv */
615 /* Flags get overloaded into srvid. */
616 req->hdr.control->srvid = tdb_flags;
617 DEBUG(db->ctdb, LOG_DEBUG,
618 "ctdb_attachdb_send: DB_ATTACH request %p", req);
622 static void destroy_req_db(struct ctdb_connection *ctdb,
623 struct ctdb_request *req)
625 /* Incomplete db is in priv_data. */
626 free(req->priv_data);
627 /* second request is chained off this one. */
629 ctdb_request_free(req->extra);
633 static void attachdb_done(struct ctdb_connection *ctdb,
634 struct ctdb_request *req,
637 struct ctdb_db *db = _db;
638 struct ctdb_request *req2;
639 struct ctdb_reply_control *reply;
640 enum ctdb_controls control = CTDB_CONTROL_DB_ATTACH;
642 if (db->persistent) {
643 control = CTDB_CONTROL_DB_ATTACH_PERSISTENT;
646 reply = unpack_reply_control(req, control);
647 if (!reply || reply->status != 0) {
650 "ctdb_attachdb_send(async): DB_ATTACH status %i",
653 /* We failed. Hand request to user and have them discover it
654 * via ctdb_attachdb_recv. */
655 db->callback(ctdb, req, db->private_data);
658 db->id = *(uint32_t *)reply->data;
660 /* Now we do another call, to get the dbpath. */
661 req2 = new_ctdb_control_request(db->ctdb, CTDB_CONTROL_GETDBPATH,
663 &db->id, sizeof(db->id),
664 attachdb_getdbpath_done, db);
666 DEBUG(db->ctdb, LOG_ERR,
667 "ctdb_attachdb_send(async): failed to allocate");
668 db->callback(ctdb, req, db->private_data);
673 DEBUG(db->ctdb, LOG_DEBUG,
674 "ctdb_attachdb_send(async): created getdbpath request");
677 static void attachdb_getdbpath_done(struct ctdb_connection *ctdb,
678 struct ctdb_request *req,
681 struct ctdb_db *db = _db;
683 /* Do callback on original request. */
684 db->callback(ctdb, req->extra, db->private_data);
687 struct ctdb_db *ctdb_attachdb_recv(struct ctdb_connection *ctdb,
688 struct ctdb_request *req)
690 struct ctdb_request *dbpath_req = req->extra;
691 struct ctdb_reply_control *reply;
692 struct ctdb_db *db = req->priv_data;
693 uint32_t tdb_flags = db->tdb_flags;
694 struct tdb_logging_context log;
696 /* Never sent the dbpath request? We've failed. */
698 /* FIXME: Save errno? */
703 reply = unpack_reply_control(dbpath_req, CTDB_CONTROL_GETDBPATH);
707 if (reply->status != 0) {
708 DEBUG(db->ctdb, LOG_ERR,
709 "ctdb_attachdb_recv: reply status %i", reply->status);
713 tdb_flags = db->persistent ? TDB_DEFAULT : TDB_NOSYNC;
714 tdb_flags |= TDB_DISALLOW_NESTING;
716 log.log_fn = ctdb_tdb_log_bridge;
717 log.log_private = ctdb;
718 db->tdb = tdb_open_ex((char *)reply->data, 0, tdb_flags, O_RDWR, 0,
720 if (db->tdb == NULL) {
721 DEBUG(db->ctdb, LOG_ERR,
722 "ctdb_attachdb_recv: failed to tdb_open %s",
723 (char *)reply->data);
727 /* Finally, separate the db from the request (see destroy_req_db). */
728 req->priv_data = NULL;
729 DEBUG(db->ctdb, LOG_DEBUG,
730 "ctdb_attachdb_recv: db %p, tdb %s", db, (char *)reply->data);
734 static unsigned long lock_magic(struct ctdb_lock *lock)
736 /* A non-zero magic specific to this structure. */
737 return ((unsigned long)lock->key.dptr
738 ^ (((unsigned long)lock->key.dptr) << 16)
739 ^ 0xBADC0FFEEBADC0DEULL)
743 /* This is only called on locks before they're held. */
744 static void free_lock(struct ctdb_lock *lock)
746 if (lock->held_magic) {
747 DEBUG(lock->ctdb_db->ctdb, LOG_ALERT,
748 "free_lock invalid lock %p", lock);
755 void ctdb_release_lock(struct ctdb_db *ctdb_db, struct ctdb_lock *lock)
757 if (lock->held_magic != lock_magic(lock)) {
758 DEBUG(lock->ctdb_db->ctdb, LOG_ALERT,
759 "ctdb_release_lock invalid lock %p", lock);
760 } else if (lock->ctdb_db != ctdb_db) {
762 DEBUG(ctdb_db->ctdb, LOG_ALERT,
763 "ctdb_release_lock: wrong ctdb_db.");
765 tdb_chainunlock(lock->ctdb_db->tdb, lock->key);
766 DEBUG(lock->ctdb_db->ctdb, LOG_DEBUG,
767 "ctdb_release_lock %p", lock);
768 remove_lock(lock->ctdb_db->ctdb, lock);
770 lock->held_magic = 0;
775 /* We keep the lock if local node is the dmaster. */
776 static bool try_readrecordlock(struct ctdb_lock *lock, TDB_DATA *data)
778 struct ctdb_ltdb_header *hdr;
780 if (tdb_chainlock(lock->ctdb_db->tdb, lock->key) != 0) {
781 DEBUG(lock->ctdb_db->ctdb, LOG_WARNING,
782 "ctdb_readrecordlock_async: failed to chainlock");
786 hdr = ctdb_local_fetch(lock->ctdb_db->tdb, lock->key, data);
787 if (hdr && hdr->dmaster == lock->ctdb_db->ctdb->pnn) {
788 DEBUG(lock->ctdb_db->ctdb, LOG_DEBUG,
789 "ctdb_readrecordlock_async: got local lock");
790 lock->held_magic = lock_magic(lock);
792 add_lock(lock->ctdb_db->ctdb, lock);
796 tdb_chainunlock(lock->ctdb_db->tdb, lock->key);
801 /* If they shutdown before we hand them the lock, we free it here. */
802 static void destroy_lock(struct ctdb_connection *ctdb,
803 struct ctdb_request *req)
805 free_lock(req->extra);
808 static void readrecordlock_retry(struct ctdb_connection *ctdb,
809 struct ctdb_request *req, void *private)
811 struct ctdb_lock *lock = req->extra;
812 struct ctdb_reply_call *reply;
815 /* OK, we've received reply to noop migration */
816 reply = unpack_reply_call(req, CTDB_NULL_FUNC);
817 if (!reply || reply->status != 0) {
820 "ctdb_readrecordlock_async(async):"
821 " NULL_FUNC returned %i", reply->status);
823 lock->callback(lock->ctdb_db, NULL, tdb_null, private);
824 ctdb_request_free(req); /* Also frees lock. */
828 /* Can we get lock now? */
829 if (try_readrecordlock(lock, &data)) {
830 /* Now it's their responsibility to free lock & request! */
831 req->extra_destructor = NULL;
832 lock->callback(lock->ctdb_db, lock, data, private);
833 ctdb_request_free(req);
837 /* Retransmit the same request again (we lost race). */
838 io_elem_reset(req->io);
839 DLIST_ADD(ctdb->outq, req);
843 ctdb_readrecordlock_async(struct ctdb_db *ctdb_db, TDB_DATA key,
844 ctdb_rrl_callback_t callback, void *cbdata)
846 struct ctdb_request *req;
847 struct ctdb_lock *lock;
850 if (holding_lock(ctdb_db->ctdb)) {
851 DEBUG(ctdb_db->ctdb, LOG_ALERT,
852 "ctdb_readrecordlock_async: already holding lock");
857 lock = malloc(sizeof(*lock) + key.dsize);
859 DEBUG(ctdb_db->ctdb, LOG_ERR,
860 "ctdb_readrecordlock_async: lock allocation failed");
863 lock->key.dptr = (void *)(lock + 1);
864 memcpy(lock->key.dptr, key.dptr, key.dsize);
865 lock->key.dsize = key.dsize;
866 lock->ctdb_db = ctdb_db;
868 lock->held_magic = 0;
871 if (try_readrecordlock(lock, &data)) {
872 callback(ctdb_db, lock, data, cbdata);
876 /* Slow path: create request. */
877 req = new_ctdb_request(
879 offsetof(struct ctdb_req_call, data) + key.dsize,
880 readrecordlock_retry, cbdata);
882 DEBUG(ctdb_db->ctdb, LOG_ERR,
883 "ctdb_readrecordlock_async: allocation failed");
888 req->extra_destructor = destroy_lock;
889 /* We store the original callback in the lock, and use our own. */
890 lock->callback = callback;
892 io_elem_init_req_header(req->io, CTDB_REQ_CALL, CTDB_CURRENT_NODE,
893 new_reqid(ctdb_db->ctdb));
895 req->hdr.call->flags = CTDB_IMMEDIATE_MIGRATION;
896 req->hdr.call->db_id = ctdb_db->id;
897 req->hdr.call->callid = CTDB_NULL_FUNC;
898 req->hdr.call->hopcount = 0;
899 req->hdr.call->keylen = key.dsize;
900 req->hdr.call->calldatalen = 0;
901 memcpy(req->hdr.call->data, key.dptr, key.dsize);
902 DLIST_ADD(ctdb_db->ctdb->outq, req);
906 bool ctdb_writerecord(struct ctdb_db *ctdb_db,
907 struct ctdb_lock *lock, TDB_DATA data)
909 if (lock->ctdb_db != ctdb_db) {
911 DEBUG(ctdb_db->ctdb, LOG_ALERT,
912 "ctdb_writerecord: Can not write, wrong ctdb_db.");
916 if (lock->held_magic != lock_magic(lock)) {
918 DEBUG(ctdb_db->ctdb, LOG_ALERT,
919 "ctdb_writerecord: Can not write. Lock has been released.");
923 if (ctdb_db->persistent) {
925 DEBUG(ctdb_db->ctdb, LOG_ALERT,
926 "ctdb_writerecord: cannot write to persistent db");
930 switch (ctdb_local_store(ctdb_db->tdb, lock->key, lock->hdr, data)) {
932 DEBUG(ctdb_db->ctdb, LOG_DEBUG,
933 "ctdb_writerecord: optimized away noop write.");
941 DEBUG(ctdb_db->ctdb, LOG_CRIT,
942 "ctdb_writerecord: out of memory.");
945 DEBUG(ctdb_db->ctdb, LOG_ALERT,
946 "ctdb_writerecord: record changed under lock?");
948 default: /* TDB already logged. */
956 struct ctdb_traverse_state {
957 struct ctdb_request *handle;
958 struct ctdb_db *ctdb_db;
961 ctdb_traverse_callback_t callback;
965 static void traverse_remhnd_cb(struct ctdb_connection *ctdb,
966 struct ctdb_request *req, void *private_data)
968 struct ctdb_traverse_state *state = private_data;
970 if (!ctdb_remove_message_handler_recv(ctdb, state->handle)) {
972 "Failed to remove message handler for"
974 state->callback(state->ctdb_db->ctdb, state->ctdb_db,
975 TRAVERSE_STATUS_ERROR,
979 ctdb_request_free(state->handle);
980 state->handle = NULL;
984 static void msg_h(struct ctdb_connection *ctdb, uint64_t srvid,
985 TDB_DATA data, void *private_data)
987 struct ctdb_traverse_state *state = private_data;
988 struct ctdb_db *ctdb_db = state->ctdb_db;
989 struct ctdb_rec_data *d = (struct ctdb_rec_data *)data.dptr;
992 if (data.dsize < sizeof(uint32_t) ||
993 d->length != data.dsize) {
995 "Bad data size %u in traverse_handler",
996 (unsigned)data.dsize);
997 state->callback(state->ctdb_db->ctdb, state->ctdb_db,
998 TRAVERSE_STATUS_ERROR,
1001 state->handle = ctdb_remove_message_handler_send(
1002 state->ctdb_db->ctdb, state->srvid,
1004 traverse_remhnd_cb, state);
1008 key.dsize = d->keylen;
1009 key.dptr = &d->data[0];
1010 data.dsize = d->datalen;
1011 data.dptr = &d->data[d->keylen];
1013 if (key.dsize == 0 && data.dsize == 0) {
1014 state->callback(state->ctdb_db->ctdb, state->ctdb_db,
1015 TRAVERSE_STATUS_FINISHED,
1018 state->handle = ctdb_remove_message_handler_send(
1019 state->ctdb_db->ctdb, state->srvid,
1021 traverse_remhnd_cb, state);
1025 if (data.dsize <= sizeof(struct ctdb_ltdb_header)) {
1026 /* empty records are deleted records in ctdb */
1030 data.dsize -= sizeof(struct ctdb_ltdb_header);
1031 data.dptr += sizeof(struct ctdb_ltdb_header);
1033 if (state->callback(ctdb, ctdb_db,
1034 TRAVERSE_STATUS_RECORD,
1035 key, data, state->cbdata) != 0) {
1036 state->handle = ctdb_remove_message_handler_send(
1037 state->ctdb_db->ctdb, state->srvid,
1039 traverse_remhnd_cb, state);
1044 static void traverse_start_cb(struct ctdb_connection *ctdb,
1045 struct ctdb_request *req, void *private_data)
1047 struct ctdb_traverse_state *state = private_data;
1049 ctdb_request_free(state->handle);
1050 state->handle = NULL;
1053 static void traverse_msghnd_cb(struct ctdb_connection *ctdb,
1054 struct ctdb_request *req, void *private_data)
1056 struct ctdb_traverse_state *state = private_data;
1057 struct ctdb_db *ctdb_db = state->ctdb_db;
1058 struct ctdb_traverse_start t;
1060 if (!ctdb_set_message_handler_recv(ctdb, state->handle)) {
1061 DEBUG(ctdb, LOG_ERR,
1062 "Failed to register message handler for"
1064 state->callback(state->ctdb_db->ctdb, state->ctdb_db,
1065 TRAVERSE_STATUS_ERROR,
1068 ctdb_request_free(state->handle);
1069 state->handle = NULL;
1073 ctdb_request_free(state->handle);
1074 state->handle = NULL;
1076 t.db_id = ctdb_db->id;
1077 t.srvid = state->srvid;
1080 state->handle = new_ctdb_control_request(ctdb,
1081 CTDB_CONTROL_TRAVERSE_START,
1084 traverse_start_cb, state);
1085 if (state->handle == NULL) {
1086 DEBUG(ctdb, LOG_ERR,
1087 "ctdb_traverse_async:"
1088 " failed to send traverse_start control");
1089 state->callback(state->ctdb_db->ctdb, state->ctdb_db,
1090 TRAVERSE_STATUS_ERROR,
1093 state->handle = ctdb_remove_message_handler_send(
1094 state->ctdb_db->ctdb, state->srvid,
1096 traverse_remhnd_cb, state);
1101 bool ctdb_traverse_async(struct ctdb_db *ctdb_db,
1102 ctdb_traverse_callback_t callback, void *cbdata)
1104 struct ctdb_connection *ctdb = ctdb_db->ctdb;
1105 struct ctdb_traverse_state *state;
1106 static uint32_t tid = 0;
1108 state = malloc(sizeof(struct ctdb_traverse_state));
1109 if (state == NULL) {
1110 DEBUG(ctdb, LOG_ERR,
1111 "ctdb_traverse_async: no memory."
1112 " allocate state failed");
1117 state->srvid = CTDB_SRVID_TRAVERSE_RANGE|tid;
1119 state->callback = callback;
1120 state->cbdata = cbdata;
1121 state->ctdb_db = ctdb_db;
1123 state->handle = ctdb_set_message_handler_send(ctdb_db->ctdb,
1126 traverse_msghnd_cb, state);
1127 if (state->handle == NULL) {
1128 DEBUG(ctdb, LOG_ERR,
1129 "ctdb_traverse_async:"
1130 " failed ctdb_set_message_handler_send");
1138 int ctdb_num_out_queue(struct ctdb_connection *ctdb)
1140 struct ctdb_request *req;
1143 for (i = 0, req = ctdb->outq; req; req = req->next, i++)
1149 int ctdb_num_in_flight(struct ctdb_connection *ctdb)
1151 struct ctdb_request *req;
1154 for (i = 0, req = ctdb->doneq; req; req = req->next, i++)
1160 int ctdb_num_active(struct ctdb_connection *ctdb)
1162 return ctdb_num_out_queue(ctdb)
1163 + ctdb_num_in_flight(ctdb);