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;
73 msg = talloc_vasprintf(ctdb, fmt, ap);
75 ctdb_fatal(ctdb, "Unable to allocate error in ctdb_send_error\n");
79 msglen = strlen(msg)+1;
80 len = offsetof(struct ctdb_reply_error, msg);
81 r = ctdb_transport_allocate(ctdb, msg, CTDB_REPLY_ERROR, len + msglen,
82 struct ctdb_reply_error);
83 CTDB_NO_MEMORY_FATAL(ctdb, r);
85 r->hdr.destnode = hdr->srcnode;
86 r->hdr.reqid = hdr->reqid;
89 memcpy(&r->msg[0], msg, msglen);
91 ctdb_queue_packet(ctdb, &r->hdr);
100 static void ctdb_call_send_redirect(struct ctdb_context *ctdb,
102 struct ctdb_req_call *c,
103 struct ctdb_ltdb_header *header)
106 uint32_t lmaster = ctdb_lmaster(ctdb, &key);
107 if (ctdb->vnn == lmaster) {
108 c->hdr.destnode = header->dmaster;
109 } else if ((c->hopcount % ctdb->tunable.max_redirect_count) == 0) {
110 c->hdr.destnode = lmaster;
112 c->hdr.destnode = header->dmaster;
115 ctdb_queue_packet(ctdb, &c->hdr);
122 caller must have the chainlock before calling this routine. Caller must be
125 static void ctdb_send_dmaster_reply(struct ctdb_db_context *ctdb_db,
126 struct ctdb_ltdb_header *header,
127 TDB_DATA key, TDB_DATA data,
128 uint32_t new_dmaster,
131 struct ctdb_context *ctdb = ctdb_db->ctdb;
132 struct ctdb_reply_dmaster *r;
136 if (ctdb->vnn != ctdb_lmaster(ctdb, &key)) {
137 DEBUG(0,(__location__ " Caller is not lmaster!\n"));
141 header->dmaster = new_dmaster;
142 ret = ctdb_ltdb_store(ctdb_db, key, header, data);
144 ctdb_fatal(ctdb, "ctdb_req_dmaster unable to update dmaster");
148 /* put the packet on a temporary context, allowing us to safely free
149 it below even if ctdb_reply_dmaster() has freed it already */
150 tmp_ctx = talloc_new(ctdb);
152 /* send the CTDB_REPLY_DMASTER */
153 len = offsetof(struct ctdb_reply_dmaster, data) + key.dsize + data.dsize;
154 r = ctdb_transport_allocate(ctdb, tmp_ctx, CTDB_REPLY_DMASTER, len,
155 struct ctdb_reply_dmaster);
156 CTDB_NO_MEMORY_FATAL(ctdb, r);
158 r->hdr.destnode = new_dmaster;
159 r->hdr.reqid = reqid;
160 r->rsn = header->rsn;
161 r->keylen = key.dsize;
162 r->datalen = data.dsize;
163 r->db_id = ctdb_db->db_id;
164 memcpy(&r->data[0], key.dptr, key.dsize);
165 memcpy(&r->data[key.dsize], data.dptr, data.dsize);
167 ctdb_queue_packet(ctdb, &r->hdr);
169 talloc_free(tmp_ctx);
173 send a dmaster request (give another node the dmaster for a record)
175 This is always sent to the lmaster, which ensures that the lmaster
176 always knows who the dmaster is. The lmaster will then send a
177 CTDB_REPLY_DMASTER to the new dmaster
179 static void ctdb_call_send_dmaster(struct ctdb_db_context *ctdb_db,
180 struct ctdb_req_call *c,
181 struct ctdb_ltdb_header *header,
182 TDB_DATA *key, TDB_DATA *data)
184 struct ctdb_req_dmaster *r;
185 struct ctdb_context *ctdb = ctdb_db->ctdb;
187 uint32_t lmaster = ctdb_lmaster(ctdb, key);
189 if (lmaster == ctdb->vnn) {
190 ctdb_send_dmaster_reply(ctdb_db, header, *key, *data,
191 c->hdr.srcnode, c->hdr.reqid);
195 len = offsetof(struct ctdb_req_dmaster, data) + key->dsize + data->dsize;
196 r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REQ_DMASTER, len,
197 struct ctdb_req_dmaster);
198 CTDB_NO_MEMORY_FATAL(ctdb, r);
199 r->hdr.destnode = lmaster;
200 r->hdr.reqid = c->hdr.reqid;
202 r->rsn = header->rsn;
203 r->dmaster = c->hdr.srcnode;
204 r->keylen = key->dsize;
205 r->datalen = data->dsize;
206 memcpy(&r->data[0], key->dptr, key->dsize);
207 memcpy(&r->data[key->dsize], data->dptr, data->dsize);
209 header->dmaster = c->hdr.srcnode;
210 if (ctdb_ltdb_store(ctdb_db, *key, header, *data) != 0) {
211 ctdb_fatal(ctdb, "Failed to store record in ctdb_call_send_dmaster");
214 ctdb_queue_packet(ctdb, &r->hdr);
220 called when a CTDB_REPLY_DMASTER packet comes in, or when the lmaster
221 gets a CTDB_REQUEST_DMASTER for itself. We become the dmaster.
223 must be called with the chainlock held. This function releases the chainlock
225 static void ctdb_become_dmaster(struct ctdb_db_context *ctdb_db,
226 uint32_t reqid, TDB_DATA key, TDB_DATA data,
229 struct ctdb_call_state *state;
230 struct ctdb_context *ctdb = ctdb_db->ctdb;
231 struct ctdb_ltdb_header header;
233 DEBUG(2,("vnn %u dmaster response %08x\n", ctdb->vnn, ctdb_hash(&key)));
236 header.rsn = rsn + 1;
237 header.dmaster = ctdb->vnn;
239 if (ctdb_ltdb_store(ctdb_db, key, &header, data) != 0) {
240 ctdb_fatal(ctdb, "ctdb_reply_dmaster store failed\n");
241 ctdb_ltdb_unlock(ctdb_db, key);
245 state = ctdb_reqid_find(ctdb, reqid, struct ctdb_call_state);
248 DEBUG(0,("vnn %u Invalid reqid %u in ctdb_become_dmaster\n",
250 ctdb_ltdb_unlock(ctdb_db, key);
254 if (reqid != state->reqid) {
255 /* we found a record but it was the wrong one */
256 DEBUG(0, ("Dropped orphan in ctdb_become_dmaster with reqid:%u\n",reqid));
257 ctdb_ltdb_unlock(ctdb_db, key);
261 ctdb_call_local(ctdb_db, &state->call, &header, state, &data, ctdb->vnn);
263 ctdb_ltdb_unlock(ctdb_db, state->call.key);
265 state->state = CTDB_CALL_DONE;
266 if (state->async.fn) {
267 state->async.fn(state);
274 called when a CTDB_REQ_DMASTER packet comes in
276 this comes into the lmaster for a record when the current dmaster
277 wants to give up the dmaster role and give it to someone else
279 void ctdb_request_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
281 struct ctdb_req_dmaster *c = (struct ctdb_req_dmaster *)hdr;
282 TDB_DATA key, data, data2;
283 struct ctdb_ltdb_header header;
284 struct ctdb_db_context *ctdb_db;
288 key.dsize = c->keylen;
289 data.dptr = c->data + c->keylen;
290 data.dsize = c->datalen;
292 ctdb_db = find_ctdb_db(ctdb, c->db_id);
294 ctdb_send_error(ctdb, hdr, -1,
295 "Unknown database in request. db_id==0x%08x",
300 /* fetch the current record */
301 ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, key, &header, hdr, &data2,
302 ctdb_call_input_pkt, ctdb, False);
304 ctdb_fatal(ctdb, "ctdb_req_dmaster failed to fetch record");
308 DEBUG(2,(__location__ " deferring ctdb_request_dmaster\n"));
312 if (ctdb_lmaster(ctdb, &key) != ctdb->vnn) {
313 DEBUG(0,("vnn %u dmaster request to non-lmaster lmaster=%u gen=%u curgen=%u\n",
314 ctdb->vnn, ctdb_lmaster(ctdb, &key),
315 hdr->generation, ctdb->vnn_map->generation));
316 ctdb_fatal(ctdb, "ctdb_req_dmaster to non-lmaster");
319 DEBUG(2,("vnn %u dmaster request on %08x for %u from %u\n",
320 ctdb->vnn, ctdb_hash(&key), c->dmaster, c->hdr.srcnode));
322 /* its a protocol error if the sending node is not the current dmaster */
323 if (header.dmaster != hdr->srcnode) {
324 DEBUG(0,("vnn %u dmaster request non-master %u dmaster=%u key %08x dbid 0x%08x gen=%u curgen=%u\n",
325 ctdb->vnn, hdr->srcnode, header.dmaster, ctdb_hash(&key),
326 ctdb_db->db_id, hdr->generation, ctdb->vnn_map->generation));
327 ctdb_fatal(ctdb, "ctdb_req_dmaster from non-master");
331 /* use the rsn from the sending node */
334 /* check if the new dmaster is the lmaster, in which case we
335 skip the dmaster reply */
336 if (c->dmaster == ctdb->vnn) {
337 ctdb_become_dmaster(ctdb_db, hdr->reqid, key, data, c->rsn);
339 ctdb_send_dmaster_reply(ctdb_db, &header, key, data, c->dmaster, hdr->reqid);
340 ctdb_ltdb_unlock(ctdb_db, key);
346 called when a CTDB_REQ_CALL packet comes in
348 void ctdb_request_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
350 struct ctdb_req_call *c = (struct ctdb_req_call *)hdr;
352 struct ctdb_reply_call *r;
354 struct ctdb_ltdb_header header;
355 struct ctdb_call call;
356 struct ctdb_db_context *ctdb_db;
358 ctdb_db = find_ctdb_db(ctdb, c->db_id);
360 ctdb_send_error(ctdb, hdr, -1,
361 "Unknown database in request. db_id==0x%08x",
366 call.call_id = c->callid;
367 call.key.dptr = c->data;
368 call.key.dsize = c->keylen;
369 call.call_data.dptr = c->data + c->keylen;
370 call.call_data.dsize = c->calldatalen;
372 /* determine if we are the dmaster for this key. This also
373 fetches the record data (if any), thus avoiding a 2nd fetch of the data
374 if the call will be answered locally */
376 ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, call.key, &header, hdr, &data,
377 ctdb_call_input_pkt, ctdb, False);
379 ctdb_send_error(ctdb, hdr, ret, "ltdb fetch failed in ctdb_request_call");
383 DEBUG(2,(__location__ " deferred ctdb_request_call\n"));
387 /* if we are not the dmaster, then send a redirect to the
389 if (header.dmaster != ctdb->vnn) {
390 talloc_free(data.dptr);
391 ctdb_call_send_redirect(ctdb, call.key, c, &header);
392 ctdb_ltdb_unlock(ctdb_db, call.key);
396 if (c->hopcount > ctdb->statistics.max_hop_count) {
397 ctdb->statistics.max_hop_count = c->hopcount;
400 /* if this nodes has done enough consecutive calls on the same record
401 then give them the record
402 or if the node requested an immediate migration
404 if ( c->hdr.srcnode != ctdb->vnn &&
405 ((header.laccessor == c->hdr.srcnode
406 && header.lacount >= ctdb->tunable.max_lacount)
407 || (c->flags & CTDB_IMMEDIATE_MIGRATION)) ) {
408 DEBUG(2,("vnn %u starting migration of %08x to %u\n",
409 ctdb->vnn, ctdb_hash(&call.key), c->hdr.srcnode));
410 ctdb_call_send_dmaster(ctdb_db, c, &header, &call.key, &data);
411 talloc_free(data.dptr);
412 ctdb_ltdb_unlock(ctdb_db, call.key);
416 ctdb_call_local(ctdb_db, &call, &header, hdr, &data, c->hdr.srcnode);
418 ctdb_ltdb_unlock(ctdb_db, call.key);
420 len = offsetof(struct ctdb_reply_call, data) + call.reply_data.dsize;
421 r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REPLY_CALL, len,
422 struct ctdb_reply_call);
423 CTDB_NO_MEMORY_FATAL(ctdb, r);
424 r->hdr.destnode = hdr->srcnode;
425 r->hdr.reqid = hdr->reqid;
426 r->status = call.status;
427 r->datalen = call.reply_data.dsize;
428 if (call.reply_data.dsize) {
429 memcpy(&r->data[0], call.reply_data.dptr, call.reply_data.dsize);
432 ctdb_queue_packet(ctdb, &r->hdr);
438 called when a CTDB_REPLY_CALL packet comes in
440 This packet comes in response to a CTDB_REQ_CALL request packet. It
441 contains any reply data from the call
443 void ctdb_reply_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
445 struct ctdb_reply_call *c = (struct ctdb_reply_call *)hdr;
446 struct ctdb_call_state *state;
448 state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_call_state);
450 DEBUG(0, (__location__ " reqid %u not found\n", hdr->reqid));
454 if (hdr->reqid != state->reqid) {
455 /* we found a record but it was the wrong one */
456 DEBUG(0, ("Dropped orphaned call reply with reqid:%u\n",hdr->reqid));
460 state->call.reply_data.dptr = c->data;
461 state->call.reply_data.dsize = c->datalen;
462 state->call.status = c->status;
464 talloc_steal(state, c);
466 state->state = CTDB_CALL_DONE;
467 if (state->async.fn) {
468 state->async.fn(state);
474 called when a CTDB_REPLY_DMASTER packet comes in
476 This packet comes in from the lmaster response to a CTDB_REQ_CALL
477 request packet. It means that the current dmaster wants to give us
480 void ctdb_reply_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
482 struct ctdb_reply_dmaster *c = (struct ctdb_reply_dmaster *)hdr;
483 struct ctdb_db_context *ctdb_db;
487 ctdb_db = find_ctdb_db(ctdb, c->db_id);
488 if (ctdb_db == NULL) {
489 DEBUG(0,("Unknown db_id 0x%x in ctdb_reply_dmaster\n", c->db_id));
494 key.dsize = c->keylen;
495 data.dptr = &c->data[key.dsize];
496 data.dsize = c->datalen;
498 ret = ctdb_ltdb_lock_requeue(ctdb_db, key, hdr,
499 ctdb_call_input_pkt, ctdb, False);
504 DEBUG(0,(__location__ " Failed to get lock in ctdb_reply_dmaster\n"));
508 ctdb_become_dmaster(ctdb_db, hdr->reqid, key, data, c->rsn);
513 called when a CTDB_REPLY_ERROR packet comes in
515 void ctdb_reply_error(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
517 struct ctdb_reply_error *c = (struct ctdb_reply_error *)hdr;
518 struct ctdb_call_state *state;
520 state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_call_state);
522 DEBUG(0,("vnn %u Invalid reqid %u in ctdb_reply_error\n",
523 ctdb->vnn, hdr->reqid));
527 if (hdr->reqid != state->reqid) {
528 /* we found a record but it was the wrong one */
529 DEBUG(0, ("Dropped orphaned error reply with reqid:%u\n",hdr->reqid));
533 talloc_steal(state, c);
535 state->state = CTDB_CALL_ERROR;
536 state->errmsg = (char *)c->msg;
537 if (state->async.fn) {
538 state->async.fn(state);
546 static int ctdb_call_destructor(struct ctdb_call_state *state)
548 DLIST_REMOVE(state->ctdb_db->ctdb->pending_calls, state);
549 ctdb_reqid_remove(state->ctdb_db->ctdb, state->reqid);
555 called when a ctdb_call needs to be resent after a reconfigure event
557 static void ctdb_call_resend(struct ctdb_call_state *state)
559 struct ctdb_context *ctdb = state->ctdb_db->ctdb;
561 state->generation = ctdb->vnn_map->generation;
563 /* use a new reqid, in case the old reply does eventually come in */
564 ctdb_reqid_remove(ctdb, state->reqid);
565 state->reqid = ctdb_reqid_new(ctdb, state);
566 state->c->hdr.reqid = state->reqid;
568 /* update the generation count for this request, so its valid with the new vnn_map */
569 state->c->hdr.generation = state->generation;
571 /* send the packet to ourselves, it will be redirected appropriately */
572 state->c->hdr.destnode = ctdb->vnn;
574 ctdb_queue_packet(ctdb, &state->c->hdr);
575 DEBUG(0,("resent ctdb_call\n"));
579 resend all pending calls on recovery
581 void ctdb_call_resend_all(struct ctdb_context *ctdb)
583 struct ctdb_call_state *state, *next;
584 for (state=ctdb->pending_calls;state;state=next) {
586 ctdb_call_resend(state);
591 this allows the caller to setup a async.fn
593 static void call_local_trigger(struct event_context *ev, struct timed_event *te,
594 struct timeval t, void *private_data)
596 struct ctdb_call_state *state = talloc_get_type(private_data, struct ctdb_call_state);
597 if (state->async.fn) {
598 state->async.fn(state);
604 construct an event driven local ctdb_call
606 this is used so that locally processed ctdb_call requests are processed
607 in an event driven manner
609 struct ctdb_call_state *ctdb_call_local_send(struct ctdb_db_context *ctdb_db,
610 struct ctdb_call *call,
611 struct ctdb_ltdb_header *header,
614 struct ctdb_call_state *state;
615 struct ctdb_context *ctdb = ctdb_db->ctdb;
618 state = talloc_zero(ctdb_db, struct ctdb_call_state);
619 CTDB_NO_MEMORY_NULL(ctdb, state);
621 talloc_steal(state, data->dptr);
623 state->state = CTDB_CALL_DONE;
625 state->ctdb_db = ctdb_db;
627 ret = ctdb_call_local(ctdb_db, &state->call, header, state, data, ctdb->vnn);
629 event_add_timed(ctdb->ev, state, timeval_zero(), call_local_trigger, state);
636 make a remote ctdb call - async send. Called in daemon context.
638 This constructs a ctdb_call request and queues it for processing.
639 This call never blocks.
641 struct ctdb_call_state *ctdb_daemon_call_send_remote(struct ctdb_db_context *ctdb_db,
642 struct ctdb_call *call,
643 struct ctdb_ltdb_header *header)
646 struct ctdb_call_state *state;
647 struct ctdb_context *ctdb = ctdb_db->ctdb;
649 state = talloc_zero(ctdb_db, struct ctdb_call_state);
650 CTDB_NO_MEMORY_NULL(ctdb, state);
651 state->reqid = ctdb_reqid_new(ctdb, state);
652 state->ctdb_db = ctdb_db;
653 talloc_set_destructor(state, ctdb_call_destructor);
655 len = offsetof(struct ctdb_req_call, data) + call->key.dsize + call->call_data.dsize;
656 state->c = ctdb_transport_allocate(ctdb, state, CTDB_REQ_CALL, len,
657 struct ctdb_req_call);
658 CTDB_NO_MEMORY_NULL(ctdb, state->c);
659 state->c->hdr.destnode = header->dmaster;
661 /* this limits us to 16k outstanding messages - not unreasonable */
662 state->c->hdr.reqid = state->reqid;
663 state->c->flags = call->flags;
664 state->c->db_id = ctdb_db->db_id;
665 state->c->callid = call->call_id;
666 state->c->hopcount = 0;
667 state->c->keylen = call->key.dsize;
668 state->c->calldatalen = call->call_data.dsize;
669 memcpy(&state->c->data[0], call->key.dptr, call->key.dsize);
670 memcpy(&state->c->data[call->key.dsize],
671 call->call_data.dptr, call->call_data.dsize);
673 state->call.call_data.dptr = &state->c->data[call->key.dsize];
674 state->call.key.dptr = &state->c->data[0];
676 state->state = CTDB_CALL_WAIT;
677 state->generation = ctdb->vnn_map->generation;
679 DLIST_ADD(ctdb->pending_calls, state);
681 ctdb_queue_packet(ctdb, &state->c->hdr);
687 make a remote ctdb call - async recv - called in daemon context
689 This is called when the program wants to wait for a ctdb_call to complete and get the
690 results. This call will block unless the call has already completed.
692 int ctdb_daemon_call_recv(struct ctdb_call_state *state, struct ctdb_call *call)
694 while (state->state < CTDB_CALL_DONE) {
695 event_loop_once(state->ctdb_db->ctdb->ev);
697 if (state->state != CTDB_CALL_DONE) {
698 ctdb_set_error(state->ctdb_db->ctdb, "%s", state->errmsg);
703 if (state->call.reply_data.dsize) {
704 call->reply_data.dptr = talloc_memdup(state->ctdb_db->ctdb,
705 state->call.reply_data.dptr,
706 state->call.reply_data.dsize);
707 call->reply_data.dsize = state->call.reply_data.dsize;
709 call->reply_data.dptr = NULL;
710 call->reply_data.dsize = 0;
712 call->status = state->call.status;
719 send a keepalive packet to the other node
721 void ctdb_send_keepalive(struct ctdb_context *ctdb, uint32_t destnode)
723 struct ctdb_req_keepalive *r;
725 r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REQ_KEEPALIVE,
726 sizeof(struct ctdb_req_keepalive),
727 struct ctdb_req_keepalive);
728 CTDB_NO_MEMORY_FATAL(ctdb, r);
729 r->hdr.destnode = destnode;
732 ctdb->statistics.keepalive_packets_sent++;
734 ctdb_queue_packet(ctdb, &r->hdr);