2 ctdb_call protocol code
4 Copyright (C) Andrew Tridgell 2006
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/>.
20 see http://wiki.samba.org/index.php/Samba_%26_Clustering for
21 protocol design and packet details
24 #include "lib/events/events.h"
25 #include "lib/tdb/include/tdb.h"
26 #include "lib/util/dlinklist.h"
27 #include "system/network.h"
28 #include "system/filesys.h"
29 #include "../include/ctdb_private.h"
32 find the ctdb_db from a db index
34 struct ctdb_db_context *find_ctdb_db(struct ctdb_context *ctdb, uint32_t id)
36 struct ctdb_db_context *ctdb_db;
38 for (ctdb_db=ctdb->db_list; ctdb_db; ctdb_db=ctdb_db->next) {
39 if (ctdb_db->db_id == id) {
48 a varient of input packet that can be used in lock requeue
50 static void ctdb_call_input_pkt(void *p, struct ctdb_req_header *hdr)
52 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
53 ctdb_input_pkt(ctdb, hdr);
60 static void ctdb_send_error(struct ctdb_context *ctdb,
61 struct ctdb_req_header *hdr, uint32_t status,
62 const char *fmt, ...) PRINTF_ATTRIBUTE(4,5);
63 static void ctdb_send_error(struct ctdb_context *ctdb,
64 struct ctdb_req_header *hdr, uint32_t status,
68 struct ctdb_reply_error *r;
72 if (ctdb->methods == NULL) {
73 DEBUG(DEBUG_ERR,(__location__ " Failed to send error. Transport is DOWN\n"));
78 msg = talloc_vasprintf(ctdb, fmt, ap);
80 ctdb_fatal(ctdb, "Unable to allocate error in ctdb_send_error\n");
84 msglen = strlen(msg)+1;
85 len = offsetof(struct ctdb_reply_error, msg);
86 r = ctdb_transport_allocate(ctdb, msg, CTDB_REPLY_ERROR, len + msglen,
87 struct ctdb_reply_error);
88 CTDB_NO_MEMORY_FATAL(ctdb, r);
90 r->hdr.destnode = hdr->srcnode;
91 r->hdr.reqid = hdr->reqid;
94 memcpy(&r->msg[0], msg, msglen);
96 ctdb_queue_packet(ctdb, &r->hdr);
103 * send a redirect reply
105 * The logic behind this function is this:
107 * A client wants to grab a record and sends a CTDB_REQ_CALL packet
108 * to its local ctdb (ctdb_request_call). If the node is not itself
109 * the record's DMASTER, it first redirects the packet to the
110 * record's LMASTER. The LMASTER then redirects the call packet to
111 * the current DMASTER. But there is a race: The record may have
112 * been migrated off the DMASTER while the redirected packet is
113 * on the wire (or in the local queue). So in case the record has
114 * migrated off the new destinaton of the call packet, instead of
115 * going back to the LMASTER to get the new DMASTER, we try to
116 * reduce rountrips by fist chasing the record a couple of times
117 * before giving up the direct chase and finally going back to the
118 * LMASTER (again). Note that this works because of this: When
119 * a record is migrated off a node, then the new DMASTER is stored
120 * in the record's copy on the former DMASTER.
122 * The maxiumum number of attempts for direct chase to make before
123 * going back to the LMASTER is configurable by the tunable
124 * "MaxRedirectCount".
126 static void ctdb_call_send_redirect(struct ctdb_context *ctdb,
128 struct ctdb_req_call *c,
129 struct ctdb_ltdb_header *header)
132 uint32_t lmaster = ctdb_lmaster(ctdb, &key);
133 if (ctdb->pnn == lmaster) {
134 c->hdr.destnode = header->dmaster;
135 } else if ((c->hopcount % ctdb->tunable.max_redirect_count) == 0) {
136 c->hdr.destnode = lmaster;
138 c->hdr.destnode = header->dmaster;
141 ctdb_queue_packet(ctdb, &c->hdr);
148 caller must have the chainlock before calling this routine. Caller must be
151 static void ctdb_send_dmaster_reply(struct ctdb_db_context *ctdb_db,
152 struct ctdb_ltdb_header *header,
153 TDB_DATA key, TDB_DATA data,
154 uint32_t new_dmaster,
157 struct ctdb_context *ctdb = ctdb_db->ctdb;
158 struct ctdb_reply_dmaster *r;
162 if (ctdb->pnn != ctdb_lmaster(ctdb, &key)) {
163 DEBUG(DEBUG_ALERT,(__location__ " Caller is not lmaster!\n"));
167 header->dmaster = new_dmaster;
168 ret = ctdb_ltdb_store(ctdb_db, key, header, data);
170 ctdb_fatal(ctdb, "ctdb_send_dmaster_reply unable to update dmaster");
174 if (ctdb->methods == NULL) {
175 ctdb_fatal(ctdb, "ctdb_send_dmaster_reply cant update dmaster since transport is down");
179 /* put the packet on a temporary context, allowing us to safely free
180 it below even if ctdb_reply_dmaster() has freed it already */
181 tmp_ctx = talloc_new(ctdb);
183 /* send the CTDB_REPLY_DMASTER */
184 len = offsetof(struct ctdb_reply_dmaster, data) + key.dsize + data.dsize + sizeof(uint32_t);
185 r = ctdb_transport_allocate(ctdb, tmp_ctx, CTDB_REPLY_DMASTER, len,
186 struct ctdb_reply_dmaster);
187 CTDB_NO_MEMORY_FATAL(ctdb, r);
189 r->hdr.destnode = new_dmaster;
190 r->hdr.reqid = reqid;
191 r->rsn = header->rsn;
192 r->keylen = key.dsize;
193 r->datalen = data.dsize;
194 r->db_id = ctdb_db->db_id;
195 memcpy(&r->data[0], key.dptr, key.dsize);
196 memcpy(&r->data[key.dsize], data.dptr, data.dsize);
197 memcpy(&r->data[key.dsize+data.dsize], &header->flags, sizeof(uint32_t));
199 ctdb_queue_packet(ctdb, &r->hdr);
201 talloc_free(tmp_ctx);
205 send a dmaster request (give another node the dmaster for a record)
207 This is always sent to the lmaster, which ensures that the lmaster
208 always knows who the dmaster is. The lmaster will then send a
209 CTDB_REPLY_DMASTER to the new dmaster
211 static void ctdb_call_send_dmaster(struct ctdb_db_context *ctdb_db,
212 struct ctdb_req_call *c,
213 struct ctdb_ltdb_header *header,
214 TDB_DATA *key, TDB_DATA *data)
216 struct ctdb_req_dmaster *r;
217 struct ctdb_context *ctdb = ctdb_db->ctdb;
219 uint32_t lmaster = ctdb_lmaster(ctdb, key);
221 if (ctdb->methods == NULL) {
222 ctdb_fatal(ctdb, "Failed ctdb_call_send_dmaster since transport is down");
226 if (data->dsize != 0) {
227 header->flags |= CTDB_REC_FLAG_MIGRATED_WITH_DATA;
230 if (lmaster == ctdb->pnn) {
231 ctdb_send_dmaster_reply(ctdb_db, header, *key, *data,
232 c->hdr.srcnode, c->hdr.reqid);
236 len = offsetof(struct ctdb_req_dmaster, data) + key->dsize + data->dsize
238 r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REQ_DMASTER, len,
239 struct ctdb_req_dmaster);
240 CTDB_NO_MEMORY_FATAL(ctdb, r);
241 r->hdr.destnode = lmaster;
242 r->hdr.reqid = c->hdr.reqid;
244 r->rsn = header->rsn;
245 r->dmaster = c->hdr.srcnode;
246 r->keylen = key->dsize;
247 r->datalen = data->dsize;
248 memcpy(&r->data[0], key->dptr, key->dsize);
249 memcpy(&r->data[key->dsize], data->dptr, data->dsize);
250 memcpy(&r->data[key->dsize + data->dsize], &header->flags, sizeof(uint32_t));
252 header->dmaster = c->hdr.srcnode;
253 if (ctdb_ltdb_store(ctdb_db, *key, header, *data) != 0) {
254 ctdb_fatal(ctdb, "Failed to store record in ctdb_call_send_dmaster");
257 ctdb_queue_packet(ctdb, &r->hdr);
263 called when a CTDB_REPLY_DMASTER packet comes in, or when the lmaster
264 gets a CTDB_REQUEST_DMASTER for itself. We become the dmaster.
266 must be called with the chainlock held. This function releases the chainlock
268 static void ctdb_become_dmaster(struct ctdb_db_context *ctdb_db,
269 struct ctdb_req_header *hdr,
270 TDB_DATA key, TDB_DATA data,
271 uint64_t rsn, uint32_t record_flags)
273 struct ctdb_call_state *state;
274 struct ctdb_context *ctdb = ctdb_db->ctdb;
275 struct ctdb_ltdb_header header;
277 DEBUG(DEBUG_DEBUG,("pnn %u dmaster response %08x\n", ctdb->pnn, ctdb_hash(&key)));
281 header.dmaster = ctdb->pnn;
282 header.flags = record_flags;
284 state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_call_state);
287 if (state->call->flags & CTDB_CALL_FLAG_VACUUM_MIGRATION) {
289 * We temporarily add the VACUUM_MIGRATED flag to
290 * the record flags, so that ctdb_ltdb_store can
291 * decide whether the record should be stored or
294 header.flags |= CTDB_REC_FLAG_VACUUM_MIGRATED;
298 if (ctdb_ltdb_store(ctdb_db, key, &header, data) != 0) {
299 ctdb_fatal(ctdb, "ctdb_reply_dmaster store failed\n");
300 ctdb_ltdb_unlock(ctdb_db, key);
306 DEBUG(DEBUG_ERR,("pnn %u Invalid reqid %u in ctdb_become_dmaster from node %u\n",
307 ctdb->pnn, hdr->reqid, hdr->srcnode));
308 ctdb_ltdb_unlock(ctdb_db, key);
312 if (hdr->reqid != state->reqid) {
313 /* we found a record but it was the wrong one */
314 DEBUG(DEBUG_ERR, ("Dropped orphan in ctdb_become_dmaster with reqid:%u\n from node %u", hdr->reqid, hdr->srcnode));
315 ctdb_ltdb_unlock(ctdb_db, key);
319 ctdb_call_local(ctdb_db, state->call, &header, state, &data);
321 ctdb_ltdb_unlock(ctdb_db, state->call->key);
323 state->state = CTDB_CALL_DONE;
324 if (state->async.fn) {
325 state->async.fn(state);
332 called when a CTDB_REQ_DMASTER packet comes in
334 this comes into the lmaster for a record when the current dmaster
335 wants to give up the dmaster role and give it to someone else
337 void ctdb_request_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
339 struct ctdb_req_dmaster *c = (struct ctdb_req_dmaster *)hdr;
340 TDB_DATA key, data, data2;
341 struct ctdb_ltdb_header header;
342 struct ctdb_db_context *ctdb_db;
343 uint32_t record_flags = 0;
348 key.dsize = c->keylen;
349 data.dptr = c->data + c->keylen;
350 data.dsize = c->datalen;
351 len = offsetof(struct ctdb_req_dmaster, data) + key.dsize + data.dsize
353 if (len <= c->hdr.length) {
354 record_flags = *(uint32_t *)&c->data[c->keylen + c->datalen];
357 ctdb_db = find_ctdb_db(ctdb, c->db_id);
359 ctdb_send_error(ctdb, hdr, -1,
360 "Unknown database in request. db_id==0x%08x",
365 /* fetch the current record */
366 ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, key, &header, hdr, &data2,
367 ctdb_call_input_pkt, ctdb, False);
369 ctdb_fatal(ctdb, "ctdb_req_dmaster failed to fetch record");
373 DEBUG(DEBUG_INFO,(__location__ " deferring ctdb_request_dmaster\n"));
377 if (ctdb_lmaster(ctdb, &key) != ctdb->pnn) {
378 DEBUG(DEBUG_ALERT,("pnn %u dmaster request to non-lmaster lmaster=%u gen=%u curgen=%u\n",
379 ctdb->pnn, ctdb_lmaster(ctdb, &key),
380 hdr->generation, ctdb->vnn_map->generation));
381 ctdb_fatal(ctdb, "ctdb_req_dmaster to non-lmaster");
384 DEBUG(DEBUG_DEBUG,("pnn %u dmaster request on %08x for %u from %u\n",
385 ctdb->pnn, ctdb_hash(&key), c->dmaster, c->hdr.srcnode));
387 /* its a protocol error if the sending node is not the current dmaster */
388 if (header.dmaster != hdr->srcnode) {
389 DEBUG(DEBUG_ALERT,("pnn %u dmaster request for new-dmaster %u from non-master %u real-dmaster=%u key %08x dbid 0x%08x gen=%u curgen=%u c->rsn=%llu header.rsn=%llu reqid=%u keyval=0x%08x\n",
390 ctdb->pnn, c->dmaster, hdr->srcnode, header.dmaster, ctdb_hash(&key),
391 ctdb_db->db_id, hdr->generation, ctdb->vnn_map->generation,
392 (unsigned long long)c->rsn, (unsigned long long)header.rsn, c->hdr.reqid,
393 (key.dsize >= 4)?(*(uint32_t *)key.dptr):0));
394 if (header.rsn != 0 || header.dmaster != ctdb->pnn) {
395 ctdb_fatal(ctdb, "ctdb_req_dmaster from non-master");
400 if (header.rsn > c->rsn) {
401 DEBUG(DEBUG_ALERT,("pnn %u dmaster request with older RSN new-dmaster %u from %u real-dmaster=%u key %08x dbid 0x%08x gen=%u curgen=%u c->rsn=%llu header.rsn=%llu reqid=%u\n",
402 ctdb->pnn, c->dmaster, hdr->srcnode, header.dmaster, ctdb_hash(&key),
403 ctdb_db->db_id, hdr->generation, ctdb->vnn_map->generation,
404 (unsigned long long)c->rsn, (unsigned long long)header.rsn, c->hdr.reqid));
407 /* use the rsn from the sending node */
410 /* store the record flags from the sending node */
411 header.flags = record_flags;
413 /* check if the new dmaster is the lmaster, in which case we
414 skip the dmaster reply */
415 if (c->dmaster == ctdb->pnn) {
416 ctdb_become_dmaster(ctdb_db, hdr, key, data, c->rsn, record_flags);
418 ctdb_send_dmaster_reply(ctdb_db, &header, key, data, c->dmaster, hdr->reqid);
420 ret = ctdb_ltdb_unlock(ctdb_db, key);
422 DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
429 called when a CTDB_REQ_CALL packet comes in
431 void ctdb_request_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
433 struct ctdb_req_call *c = (struct ctdb_req_call *)hdr;
435 struct ctdb_reply_call *r;
437 struct ctdb_ltdb_header header;
438 struct ctdb_call *call;
439 struct ctdb_db_context *ctdb_db;
441 if (ctdb->methods == NULL) {
442 DEBUG(DEBUG_ERR,(__location__ " Failed ctdb_request_call. Transport is DOWN\n"));
447 ctdb_db = find_ctdb_db(ctdb, c->db_id);
449 ctdb_send_error(ctdb, hdr, -1,
450 "Unknown database in request. db_id==0x%08x",
455 call = talloc(hdr, struct ctdb_call);
456 CTDB_NO_MEMORY_FATAL(ctdb, call);
458 call->call_id = c->callid;
459 call->key.dptr = c->data;
460 call->key.dsize = c->keylen;
461 call->call_data.dptr = c->data + c->keylen;
462 call->call_data.dsize = c->calldatalen;
464 /* determine if we are the dmaster for this key. This also
465 fetches the record data (if any), thus avoiding a 2nd fetch of the data
466 if the call will be answered locally */
468 ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, call->key, &header, hdr, &data,
469 ctdb_call_input_pkt, ctdb, False);
471 ctdb_send_error(ctdb, hdr, ret, "ltdb fetch failed in ctdb_request_call");
475 DEBUG(DEBUG_INFO,(__location__ " deferred ctdb_request_call\n"));
479 /* if we are not the dmaster, then send a redirect to the
481 if (header.dmaster != ctdb->pnn) {
482 talloc_free(data.dptr);
483 ctdb_call_send_redirect(ctdb, call->key, c, &header);
485 ret = ctdb_ltdb_unlock(ctdb_db, call->key);
487 DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
492 if (c->hopcount > ctdb->statistics.max_hop_count) {
493 ctdb->statistics.max_hop_count = c->hopcount;
496 /* Try if possible to migrate the record off to the caller node.
497 * From the clients perspective a fetch of the data is just as
498 * expensive as a migration.
500 if (c->hdr.srcnode != ctdb->pnn) {
501 if (ctdb_db->transaction_active) {
502 DEBUG(DEBUG_INFO, (__location__ " refusing migration"
503 " of key %s while transaction is active\n",
504 (char *)call->key.dptr));
506 DEBUG(DEBUG_DEBUG,("pnn %u starting migration of %08x to %u\n",
507 ctdb->pnn, ctdb_hash(&(call->key)), c->hdr.srcnode));
508 ctdb_call_send_dmaster(ctdb_db, c, &header, &(call->key), &data);
509 talloc_free(data.dptr);
511 ret = ctdb_ltdb_unlock(ctdb_db, call->key);
513 DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
519 ctdb_call_local(ctdb_db, call, &header, hdr, &data);
521 ret = ctdb_ltdb_unlock(ctdb_db, call->key);
523 DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
526 len = offsetof(struct ctdb_reply_call, data) + call->reply_data.dsize;
527 r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REPLY_CALL, len,
528 struct ctdb_reply_call);
529 CTDB_NO_MEMORY_FATAL(ctdb, r);
530 r->hdr.destnode = hdr->srcnode;
531 r->hdr.reqid = hdr->reqid;
532 r->status = call->status;
533 r->datalen = call->reply_data.dsize;
534 if (call->reply_data.dsize) {
535 memcpy(&r->data[0], call->reply_data.dptr, call->reply_data.dsize);
538 ctdb_queue_packet(ctdb, &r->hdr);
544 called when a CTDB_REPLY_CALL packet comes in
546 This packet comes in response to a CTDB_REQ_CALL request packet. It
547 contains any reply data from the call
549 void ctdb_reply_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
551 struct ctdb_reply_call *c = (struct ctdb_reply_call *)hdr;
552 struct ctdb_call_state *state;
554 state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_call_state);
556 DEBUG(DEBUG_ERR, (__location__ " reqid %u not found\n", hdr->reqid));
560 if (hdr->reqid != state->reqid) {
561 /* we found a record but it was the wrong one */
562 DEBUG(DEBUG_ERR, ("Dropped orphaned call reply with reqid:%u\n",hdr->reqid));
566 state->call->reply_data.dptr = c->data;
567 state->call->reply_data.dsize = c->datalen;
568 state->call->status = c->status;
570 talloc_steal(state, c);
572 state->state = CTDB_CALL_DONE;
573 if (state->async.fn) {
574 state->async.fn(state);
580 called when a CTDB_REPLY_DMASTER packet comes in
582 This packet comes in from the lmaster response to a CTDB_REQ_CALL
583 request packet. It means that the current dmaster wants to give us
586 void ctdb_reply_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
588 struct ctdb_reply_dmaster *c = (struct ctdb_reply_dmaster *)hdr;
589 struct ctdb_db_context *ctdb_db;
591 uint32_t record_flags = 0;
595 ctdb_db = find_ctdb_db(ctdb, c->db_id);
596 if (ctdb_db == NULL) {
597 DEBUG(DEBUG_ERR,("Unknown db_id 0x%x in ctdb_reply_dmaster\n", c->db_id));
602 key.dsize = c->keylen;
603 data.dptr = &c->data[key.dsize];
604 data.dsize = c->datalen;
605 len = offsetof(struct ctdb_reply_dmaster, data) + key.dsize + data.dsize
607 if (len <= c->hdr.length) {
608 record_flags = *(uint32_t *)&c->data[c->keylen + c->datalen];
611 ret = ctdb_ltdb_lock_requeue(ctdb_db, key, hdr,
612 ctdb_call_input_pkt, ctdb, False);
617 DEBUG(DEBUG_ERR,(__location__ " Failed to get lock in ctdb_reply_dmaster\n"));
621 ctdb_become_dmaster(ctdb_db, hdr, key, data, c->rsn, record_flags);
626 called when a CTDB_REPLY_ERROR packet comes in
628 void ctdb_reply_error(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
630 struct ctdb_reply_error *c = (struct ctdb_reply_error *)hdr;
631 struct ctdb_call_state *state;
633 state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_call_state);
635 DEBUG(DEBUG_ERR,("pnn %u Invalid reqid %u in ctdb_reply_error\n",
636 ctdb->pnn, hdr->reqid));
640 if (hdr->reqid != state->reqid) {
641 /* we found a record but it was the wrong one */
642 DEBUG(DEBUG_ERR, ("Dropped orphaned error reply with reqid:%u\n",hdr->reqid));
646 talloc_steal(state, c);
648 state->state = CTDB_CALL_ERROR;
649 state->errmsg = (char *)c->msg;
650 if (state->async.fn) {
651 state->async.fn(state);
659 static int ctdb_call_destructor(struct ctdb_call_state *state)
661 DLIST_REMOVE(state->ctdb_db->ctdb->pending_calls, state);
662 ctdb_reqid_remove(state->ctdb_db->ctdb, state->reqid);
668 called when a ctdb_call needs to be resent after a reconfigure event
670 static void ctdb_call_resend(struct ctdb_call_state *state)
672 struct ctdb_context *ctdb = state->ctdb_db->ctdb;
674 state->generation = ctdb->vnn_map->generation;
676 /* use a new reqid, in case the old reply does eventually come in */
677 ctdb_reqid_remove(ctdb, state->reqid);
678 state->reqid = ctdb_reqid_new(ctdb, state);
679 state->c->hdr.reqid = state->reqid;
681 /* update the generation count for this request, so its valid with the new vnn_map */
682 state->c->hdr.generation = state->generation;
684 /* send the packet to ourselves, it will be redirected appropriately */
685 state->c->hdr.destnode = ctdb->pnn;
687 ctdb_queue_packet(ctdb, &state->c->hdr);
688 DEBUG(DEBUG_NOTICE,("resent ctdb_call\n"));
692 resend all pending calls on recovery
694 void ctdb_call_resend_all(struct ctdb_context *ctdb)
696 struct ctdb_call_state *state, *next;
697 for (state=ctdb->pending_calls;state;state=next) {
699 ctdb_call_resend(state);
704 this allows the caller to setup a async.fn
706 static void call_local_trigger(struct event_context *ev, struct timed_event *te,
707 struct timeval t, void *private_data)
709 struct ctdb_call_state *state = talloc_get_type(private_data, struct ctdb_call_state);
710 if (state->async.fn) {
711 state->async.fn(state);
717 construct an event driven local ctdb_call
719 this is used so that locally processed ctdb_call requests are processed
720 in an event driven manner
722 struct ctdb_call_state *ctdb_call_local_send(struct ctdb_db_context *ctdb_db,
723 struct ctdb_call *call,
724 struct ctdb_ltdb_header *header,
727 struct ctdb_call_state *state;
728 struct ctdb_context *ctdb = ctdb_db->ctdb;
731 state = talloc_zero(ctdb_db, struct ctdb_call_state);
732 CTDB_NO_MEMORY_NULL(ctdb, state);
734 talloc_steal(state, data->dptr);
736 state->state = CTDB_CALL_DONE;
737 state->call = talloc(state, struct ctdb_call);
738 CTDB_NO_MEMORY_NULL(ctdb, state->call);
739 *(state->call) = *call;
740 state->ctdb_db = ctdb_db;
742 ret = ctdb_call_local(ctdb_db, state->call, header, state, data);
744 event_add_timed(ctdb->ev, state, timeval_zero(), call_local_trigger, state);
751 make a remote ctdb call - async send. Called in daemon context.
753 This constructs a ctdb_call request and queues it for processing.
754 This call never blocks.
756 struct ctdb_call_state *ctdb_daemon_call_send_remote(struct ctdb_db_context *ctdb_db,
757 struct ctdb_call *call,
758 struct ctdb_ltdb_header *header)
761 struct ctdb_call_state *state;
762 struct ctdb_context *ctdb = ctdb_db->ctdb;
764 if (ctdb->methods == NULL) {
765 DEBUG(DEBUG_ERR,(__location__ " Failed send packet. Transport is down\n"));
769 state = talloc_zero(ctdb_db, struct ctdb_call_state);
770 CTDB_NO_MEMORY_NULL(ctdb, state);
771 state->call = talloc(state, struct ctdb_call);
772 CTDB_NO_MEMORY_NULL(ctdb, state->call);
774 state->reqid = ctdb_reqid_new(ctdb, state);
775 state->ctdb_db = ctdb_db;
776 talloc_set_destructor(state, ctdb_call_destructor);
778 len = offsetof(struct ctdb_req_call, data) + call->key.dsize + call->call_data.dsize;
779 state->c = ctdb_transport_allocate(ctdb, state, CTDB_REQ_CALL, len,
780 struct ctdb_req_call);
781 CTDB_NO_MEMORY_NULL(ctdb, state->c);
782 state->c->hdr.destnode = header->dmaster;
784 /* this limits us to 16k outstanding messages - not unreasonable */
785 state->c->hdr.reqid = state->reqid;
786 state->c->flags = call->flags;
787 state->c->db_id = ctdb_db->db_id;
788 state->c->callid = call->call_id;
789 state->c->hopcount = 0;
790 state->c->keylen = call->key.dsize;
791 state->c->calldatalen = call->call_data.dsize;
792 memcpy(&state->c->data[0], call->key.dptr, call->key.dsize);
793 memcpy(&state->c->data[call->key.dsize],
794 call->call_data.dptr, call->call_data.dsize);
795 *(state->call) = *call;
796 state->call->call_data.dptr = &state->c->data[call->key.dsize];
797 state->call->key.dptr = &state->c->data[0];
799 state->state = CTDB_CALL_WAIT;
800 state->generation = ctdb->vnn_map->generation;
802 DLIST_ADD(ctdb->pending_calls, state);
804 ctdb_queue_packet(ctdb, &state->c->hdr);
810 make a remote ctdb call - async recv - called in daemon context
812 This is called when the program wants to wait for a ctdb_call to complete and get the
813 results. This call will block unless the call has already completed.
815 int ctdb_daemon_call_recv(struct ctdb_call_state *state, struct ctdb_call *call)
817 while (state->state < CTDB_CALL_DONE) {
818 event_loop_once(state->ctdb_db->ctdb->ev);
820 if (state->state != CTDB_CALL_DONE) {
821 ctdb_set_error(state->ctdb_db->ctdb, "%s", state->errmsg);
826 if (state->call->reply_data.dsize) {
827 call->reply_data.dptr = talloc_memdup(call,
828 state->call->reply_data.dptr,
829 state->call->reply_data.dsize);
830 call->reply_data.dsize = state->call->reply_data.dsize;
832 call->reply_data.dptr = NULL;
833 call->reply_data.dsize = 0;
835 call->status = state->call->status;
842 send a keepalive packet to the other node
844 void ctdb_send_keepalive(struct ctdb_context *ctdb, uint32_t destnode)
846 struct ctdb_req_keepalive *r;
848 if (ctdb->methods == NULL) {
849 DEBUG(DEBUG_ERR,(__location__ " Failed to send keepalive. Transport is DOWN\n"));
853 r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REQ_KEEPALIVE,
854 sizeof(struct ctdb_req_keepalive),
855 struct ctdb_req_keepalive);
856 CTDB_NO_MEMORY_FATAL(ctdb, r);
857 r->hdr.destnode = destnode;
860 ctdb->statistics.keepalive_packets_sent++;
862 ctdb_queue_packet(ctdb, &r->hdr);