2 ctdb_call protocol code
4 Copyright (C) Andrew Tridgell 2006
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 3 of the License, or (at your option) any later version.
11 This library 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 GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 see http://wiki.samba.org/index.php/Samba_%26_Clustering for
22 protocol design and packet details
25 #include "lib/events/events.h"
26 #include "lib/tdb/include/tdb.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 local version of ctdb_call
50 int ctdb_call_local(struct ctdb_db_context *ctdb_db, struct ctdb_call *call,
51 struct ctdb_ltdb_header *header, TDB_DATA *data,
54 struct ctdb_call_info *c;
55 struct ctdb_registered_call *fn;
56 struct ctdb_context *ctdb = ctdb_db->ctdb;
58 c = talloc(ctdb, struct ctdb_call_info);
59 CTDB_NO_MEMORY(ctdb, c);
62 c->call_data = &call->call_data;
63 c->record_data.dptr = talloc_memdup(c, data->dptr, data->dsize);
64 c->record_data.dsize = data->dsize;
65 CTDB_NO_MEMORY(ctdb, c->record_data.dptr);
70 for (fn=ctdb_db->calls;fn;fn=fn->next) {
71 if (fn->id == call->call_id) break;
74 ctdb_set_error(ctdb, "Unknown call id %u\n", call->call_id);
80 ctdb_set_error(ctdb, "ctdb_call %u failed\n", call->call_id);
85 if (header->laccessor != caller) {
88 header->laccessor = caller;
91 /* we need to force the record to be written out if this was a remote access,
92 so that the lacount is updated */
93 if (c->new_data == NULL && header->laccessor != ctdb->vnn) {
94 c->new_data = &c->record_data;
98 if (ctdb_ltdb_store(ctdb_db, call->key, header, *c->new_data) != 0) {
99 ctdb_set_error(ctdb, "ctdb_call tdb_store failed\n");
106 call->reply_data = *c->reply_data;
107 talloc_steal(ctdb, call->reply_data.dptr);
108 talloc_set_name_const(call->reply_data.dptr, __location__);
110 call->reply_data.dptr = NULL;
111 call->reply_data.dsize = 0;
113 call->status = c->status;
123 static void ctdb_send_error(struct ctdb_context *ctdb,
124 struct ctdb_req_header *hdr, uint32_t status,
125 const char *fmt, ...) PRINTF_ATTRIBUTE(4,5);
126 static void ctdb_send_error(struct ctdb_context *ctdb,
127 struct ctdb_req_header *hdr, uint32_t status,
128 const char *fmt, ...)
131 struct ctdb_reply_error *r;
136 msg = talloc_vasprintf(ctdb, fmt, ap);
138 ctdb_fatal(ctdb, "Unable to allocate error in ctdb_send_error\n");
142 msglen = strlen(msg)+1;
143 len = offsetof(struct ctdb_reply_error, msg);
144 r = ctdb->methods->allocate_pkt(msg, len + msglen);
145 CTDB_NO_MEMORY_FATAL(ctdb, r);
146 talloc_set_name_const(r, "send_error packet");
148 r->hdr.length = len + msglen;
149 r->hdr.ctdb_magic = CTDB_MAGIC;
150 r->hdr.ctdb_version = CTDB_VERSION;
151 r->hdr.operation = CTDB_REPLY_ERROR;
152 r->hdr.destnode = hdr->srcnode;
153 r->hdr.srcnode = ctdb->vnn;
154 r->hdr.reqid = hdr->reqid;
157 memcpy(&r->msg[0], msg, msglen);
159 ctdb_queue_packet(ctdb, &r->hdr);
166 send a redirect reply
168 static void ctdb_call_send_redirect(struct ctdb_context *ctdb,
169 struct ctdb_req_call *c,
170 struct ctdb_ltdb_header *header)
172 struct ctdb_reply_redirect *r;
174 r = ctdb->methods->allocate_pkt(ctdb, sizeof(*r));
175 CTDB_NO_MEMORY_FATAL(ctdb, r);
176 talloc_set_name_const(r, "send_redirect packet");
177 r->hdr.length = sizeof(*r);
178 r->hdr.ctdb_magic = CTDB_MAGIC;
179 r->hdr.ctdb_version = CTDB_VERSION;
180 r->hdr.operation = CTDB_REPLY_REDIRECT;
181 r->hdr.destnode = c->hdr.srcnode;
182 r->hdr.srcnode = ctdb->vnn;
183 r->hdr.reqid = c->hdr.reqid;
184 r->dmaster = header->dmaster;
186 ctdb_queue_packet(ctdb, &r->hdr);
192 send a dmaster request (give another node the dmaster for a record)
194 This is always sent to the lmaster, which ensures that the lmaster
195 always knows who the dmaster is. The lmaster will then send a
196 CTDB_REPLY_DMASTER to the new dmaster
198 static void ctdb_call_send_dmaster(struct ctdb_db_context *ctdb_db,
199 struct ctdb_req_call *c,
200 struct ctdb_ltdb_header *header,
201 TDB_DATA *key, TDB_DATA *data)
203 struct ctdb_req_dmaster *r;
204 struct ctdb_context *ctdb = ctdb_db->ctdb;
207 len = offsetof(struct ctdb_req_dmaster, data) + key->dsize + data->dsize;
208 r = ctdb->methods->allocate_pkt(ctdb, len);
209 CTDB_NO_MEMORY_FATAL(ctdb, r);
210 talloc_set_name_const(r, "send_dmaster packet");
212 r->hdr.ctdb_magic = CTDB_MAGIC;
213 r->hdr.ctdb_version = CTDB_VERSION;
214 r->hdr.operation = CTDB_REQ_DMASTER;
215 r->hdr.destnode = ctdb_lmaster(ctdb, key);
216 r->hdr.srcnode = ctdb->vnn;
217 r->hdr.reqid = c->hdr.reqid;
219 r->dmaster = c->hdr.srcnode;
220 r->keylen = key->dsize;
221 r->datalen = data->dsize;
222 memcpy(&r->data[0], key->dptr, key->dsize);
223 memcpy(&r->data[key->dsize], data->dptr, data->dsize);
225 /* XXX - probably not necessary when lmaster==dmaster
226 update the ltdb to record the new dmaster */
227 header->dmaster = r->hdr.destnode;
228 ctdb_ltdb_store(ctdb_db, *key, header, *data);
230 ctdb_queue_packet(ctdb, &r->hdr);
237 called when a CTDB_REQ_DMASTER packet comes in
239 this comes into the lmaster for a record when the current dmaster
240 wants to give up the dmaster role and give it to someone else
242 void ctdb_request_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
244 struct ctdb_req_dmaster *c = (struct ctdb_req_dmaster *)hdr;
245 struct ctdb_reply_dmaster *r;
246 TDB_DATA key, data, data2;
247 struct ctdb_ltdb_header header;
248 struct ctdb_db_context *ctdb_db;
253 key.dsize = c->keylen;
254 data.dptr = c->data + c->keylen;
255 data.dsize = c->datalen;
257 ctdb_db = find_ctdb_db(ctdb, c->db_id);
259 ctdb_send_error(ctdb, hdr, -1,
260 "Unknown database in request. db_id==0x%08x",
265 /* fetch the current record */
266 ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, key, &header, hdr, &data2,
267 ctdb_recv_raw_pkt, ctdb);
269 ctdb_fatal(ctdb, "ctdb_req_dmaster failed to fetch record");
273 DEBUG(2,(__location__ " deferring ctdb_request_dmaster\n"));
277 /* its a protocol error if the sending node is not the current dmaster */
278 if (header.dmaster != hdr->srcnode &&
279 hdr->srcnode != ctdb_lmaster(ctdb_db->ctdb, &key)) {
280 ctdb_fatal(ctdb, "dmaster request from non-master");
284 header.dmaster = c->dmaster;
285 ret = ctdb_ltdb_store(ctdb_db, key, &header, data);
286 ctdb_ltdb_unlock(ctdb_db, key);
288 ctdb_fatal(ctdb, "ctdb_req_dmaster unable to update dmaster");
292 /* put the packet on a temporary context, allowing us to safely free
293 it below even if ctdb_reply_dmaster() has freed it already */
294 tmp_ctx = talloc_new(ctdb);
296 /* send the CTDB_REPLY_DMASTER */
297 len = offsetof(struct ctdb_reply_dmaster, data) + data.dsize;
298 r = ctdb->methods->allocate_pkt(tmp_ctx, len);
299 CTDB_NO_MEMORY_FATAL(ctdb, r);
301 talloc_set_name_const(r, "reply_dmaster packet");
303 r->hdr.ctdb_magic = CTDB_MAGIC;
304 r->hdr.ctdb_version = CTDB_VERSION;
305 r->hdr.operation = CTDB_REPLY_DMASTER;
306 r->hdr.destnode = c->dmaster;
307 r->hdr.srcnode = ctdb->vnn;
308 r->hdr.reqid = hdr->reqid;
309 r->datalen = data.dsize;
310 memcpy(&r->data[0], data.dptr, data.dsize);
312 ctdb_queue_packet(ctdb, &r->hdr);
314 talloc_free(tmp_ctx);
319 called when a CTDB_REQ_CALL packet comes in
321 void ctdb_request_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
323 struct ctdb_req_call *c = (struct ctdb_req_call *)hdr;
325 struct ctdb_reply_call *r;
327 struct ctdb_ltdb_header header;
328 struct ctdb_call call;
329 struct ctdb_db_context *ctdb_db;
331 ctdb_db = find_ctdb_db(ctdb, c->db_id);
333 ctdb_send_error(ctdb, hdr, -1,
334 "Unknown database in request. db_id==0x%08x",
339 call.call_id = c->callid;
340 call.key.dptr = c->data;
341 call.key.dsize = c->keylen;
342 call.call_data.dptr = c->data + c->keylen;
343 call.call_data.dsize = c->calldatalen;
345 /* determine if we are the dmaster for this key. This also
346 fetches the record data (if any), thus avoiding a 2nd fetch of the data
347 if the call will be answered locally */
349 ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, call.key, &header, hdr, &data,
350 ctdb_recv_raw_pkt, ctdb);
352 ctdb_send_error(ctdb, hdr, ret, "ltdb fetch failed in ctdb_request_call");
356 DEBUG(2,(__location__ " deferred ctdb_request_call\n"));
360 /* if we are not the dmaster, then send a redirect to the
362 if (header.dmaster != ctdb->vnn) {
363 ctdb_call_send_redirect(ctdb, c, &header);
364 talloc_free(data.dptr);
365 ctdb_ltdb_unlock(ctdb_db, call.key);
369 /* if this nodes has done enough consecutive calls on the same record
370 then give them the record
371 or if the node requested an immediate migration
373 if ( (header.laccessor == c->hdr.srcnode
374 && header.lacount >= ctdb->max_lacount)
375 || c->flags&CTDB_IMMEDIATE_MIGRATION ) {
376 ctdb_call_send_dmaster(ctdb_db, c, &header, &call.key, &data);
377 talloc_free(data.dptr);
378 ctdb_ltdb_unlock(ctdb_db, call.key);
382 ctdb_call_local(ctdb_db, &call, &header, &data, c->hdr.srcnode);
384 ctdb_ltdb_unlock(ctdb_db, call.key);
386 len = offsetof(struct ctdb_reply_call, data) + call.reply_data.dsize;
387 r = ctdb->methods->allocate_pkt(ctdb, len);
388 CTDB_NO_MEMORY_FATAL(ctdb, r);
389 talloc_set_name_const(r, "reply_call packet");
391 r->hdr.ctdb_magic = CTDB_MAGIC;
392 r->hdr.ctdb_version = CTDB_VERSION;
393 r->hdr.operation = CTDB_REPLY_CALL;
394 r->hdr.destnode = hdr->srcnode;
395 r->hdr.srcnode = hdr->destnode;
396 r->hdr.reqid = hdr->reqid;
397 r->status = call.status;
398 r->datalen = call.reply_data.dsize;
399 if (call.reply_data.dsize) {
400 memcpy(&r->data[0], call.reply_data.dptr, call.reply_data.dsize);
401 talloc_free(call.reply_data.dptr);
404 ctdb_queue_packet(ctdb, &r->hdr);
410 called when a CTDB_REPLY_CALL packet comes in
412 This packet comes in response to a CTDB_REQ_CALL request packet. It
413 contains any reply data from the call
415 void ctdb_reply_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
417 struct ctdb_reply_call *c = (struct ctdb_reply_call *)hdr;
418 struct ctdb_call_state *state;
420 state = idr_find_type(ctdb->idr, hdr->reqid, struct ctdb_call_state);
422 DEBUG(0, ("reqid %d not found\n", hdr->reqid));
426 state->call.reply_data.dptr = c->data;
427 state->call.reply_data.dsize = c->datalen;
428 state->call.status = c->status;
430 talloc_steal(state, c);
432 state->state = CTDB_CALL_DONE;
433 if (state->async.fn) {
434 state->async.fn(state);
439 called when a CTDB_REPLY_DMASTER packet comes in
441 This packet comes in from the lmaster response to a CTDB_REQ_CALL
442 request packet. It means that the current dmaster wants to give us
445 void ctdb_reply_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
447 struct ctdb_reply_dmaster *c = (struct ctdb_reply_dmaster *)hdr;
448 struct ctdb_call_state *state;
449 struct ctdb_db_context *ctdb_db;
453 state = idr_find_type(ctdb->idr, hdr->reqid, struct ctdb_call_state);
458 ctdb_db = state->ctdb_db;
460 ret = ctdb_ltdb_lock_requeue(ctdb_db, state->call.key, hdr,
461 ctdb_recv_raw_pkt, ctdb);
466 DEBUG(0,(__location__ " Failed to get lock in ctdb_reply_dmaster\n"));
471 data.dsize = c->datalen;
473 talloc_steal(state, c);
475 /* we're now the dmaster - update our local ltdb with new header
477 state->header.dmaster = ctdb->vnn;
479 if (ctdb_ltdb_store(ctdb_db, state->call.key, &state->header, data) != 0) {
480 ctdb_ltdb_unlock(ctdb_db, state->call.key);
481 ctdb_fatal(ctdb, "ctdb_reply_dmaster store failed\n");
485 ctdb_call_local(ctdb_db, &state->call, &state->header, &data, ctdb->vnn);
487 ctdb_ltdb_unlock(ctdb_db, state->call.key);
489 talloc_steal(state, state->call.reply_data.dptr);
491 state->state = CTDB_CALL_DONE;
492 if (state->async.fn) {
493 state->async.fn(state);
499 called when a CTDB_REPLY_ERROR packet comes in
501 void ctdb_reply_error(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
503 struct ctdb_reply_error *c = (struct ctdb_reply_error *)hdr;
504 struct ctdb_call_state *state;
506 state = idr_find_type(ctdb->idr, hdr->reqid, struct ctdb_call_state);
507 if (state == NULL) return;
509 talloc_steal(state, c);
511 state->state = CTDB_CALL_ERROR;
512 state->errmsg = (char *)c->msg;
513 if (state->async.fn) {
514 state->async.fn(state);
520 called when a CTDB_REPLY_REDIRECT packet comes in
522 This packet arrives when we have sent a CTDB_REQ_CALL request and
523 the node that received it is not the dmaster for the given key. We
524 are given a hint as to what node to try next.
526 void ctdb_reply_redirect(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
528 struct ctdb_reply_redirect *c = (struct ctdb_reply_redirect *)hdr;
529 struct ctdb_call_state *state;
531 state = idr_find_type(ctdb->idr, hdr->reqid, struct ctdb_call_state);
532 if (state == NULL) return;
534 talloc_steal(state, c);
536 /* don't allow for too many redirects */
537 if (state->redirect_count++ == CTDB_MAX_REDIRECT) {
538 c->dmaster = ctdb_lmaster(ctdb, &state->call.key);
541 /* send it off again */
542 state->node = ctdb->nodes[c->dmaster];
543 state->c->hdr.destnode = c->dmaster;
545 ctdb_queue_packet(ctdb, &state->c->hdr);
551 static int ctdb_call_destructor(struct ctdb_call_state *state)
553 idr_remove(state->node->ctdb->idr, state->c->hdr.reqid);
559 called when a ctdb_call times out
561 void ctdb_call_timeout(struct event_context *ev, struct timed_event *te,
562 struct timeval t, void *private_data)
564 struct ctdb_call_state *state = talloc_get_type(private_data, struct ctdb_call_state);
565 state->state = CTDB_CALL_ERROR;
566 ctdb_set_error(state->node->ctdb, "ctdb_call %u timed out",
567 state->c->hdr.reqid);
568 if (state->async.fn) {
569 state->async.fn(state);
574 this allows the caller to setup a async.fn
576 static void call_local_trigger(struct event_context *ev, struct timed_event *te,
577 struct timeval t, void *private_data)
579 struct ctdb_call_state *state = talloc_get_type(private_data, struct ctdb_call_state);
580 if (state->async.fn) {
581 state->async.fn(state);
587 construct an event driven local ctdb_call
589 this is used so that locally processed ctdb_call requests are processed
590 in an event driven manner
592 struct ctdb_call_state *ctdb_call_local_send(struct ctdb_db_context *ctdb_db,
593 struct ctdb_call *call,
594 struct ctdb_ltdb_header *header,
597 struct ctdb_call_state *state;
598 struct ctdb_context *ctdb = ctdb_db->ctdb;
601 state = talloc_zero(ctdb_db, struct ctdb_call_state);
602 CTDB_NO_MEMORY_NULL(ctdb, state);
604 talloc_steal(state, data->dptr);
606 state->state = CTDB_CALL_DONE;
607 state->node = ctdb->nodes[ctdb->vnn];
609 state->ctdb_db = ctdb_db;
611 ret = ctdb_call_local(ctdb_db, &state->call, header, data, ctdb->vnn);
612 talloc_steal(state, state->call.reply_data.dptr);
614 event_add_timed(ctdb->ev, state, timeval_zero(), call_local_trigger, state);
621 make a remote ctdb call - async send. Called in daemon context.
623 This constructs a ctdb_call request and queues it for processing.
624 This call never blocks.
626 struct ctdb_call_state *ctdb_daemon_call_send_remote(struct ctdb_db_context *ctdb_db,
627 struct ctdb_call *call,
628 struct ctdb_ltdb_header *header)
631 struct ctdb_call_state *state;
632 struct ctdb_context *ctdb = ctdb_db->ctdb;
634 state = talloc_zero(ctdb_db, struct ctdb_call_state);
635 CTDB_NO_MEMORY_NULL(ctdb, state);
637 len = offsetof(struct ctdb_req_call, data) + call->key.dsize + call->call_data.dsize;
638 state->c = ctdb->methods->allocate_pkt(state, len);
639 CTDB_NO_MEMORY_NULL(ctdb, state->c);
640 talloc_set_name_const(state->c, "req_call packet");
642 state->c->hdr.length = len;
643 state->c->hdr.ctdb_magic = CTDB_MAGIC;
644 state->c->hdr.ctdb_version = CTDB_VERSION;
645 state->c->hdr.operation = CTDB_REQ_CALL;
646 state->c->hdr.destnode = header->dmaster;
647 state->c->hdr.srcnode = ctdb->vnn;
648 /* this limits us to 16k outstanding messages - not unreasonable */
649 state->c->hdr.reqid = idr_get_new(ctdb->idr, state, 0xFFFF);
650 state->c->flags = call->flags;
651 state->c->db_id = ctdb_db->db_id;
652 state->c->callid = call->call_id;
653 state->c->keylen = call->key.dsize;
654 state->c->calldatalen = call->call_data.dsize;
655 memcpy(&state->c->data[0], call->key.dptr, call->key.dsize);
656 memcpy(&state->c->data[call->key.dsize],
657 call->call_data.dptr, call->call_data.dsize);
659 state->call.call_data.dptr = &state->c->data[call->key.dsize];
660 state->call.key.dptr = &state->c->data[0];
662 state->node = ctdb->nodes[header->dmaster];
663 state->state = CTDB_CALL_WAIT;
664 state->header = *header;
665 state->ctdb_db = ctdb_db;
667 talloc_set_destructor(state, ctdb_call_destructor);
669 ctdb_queue_packet(ctdb, &state->c->hdr);
671 event_add_timed(ctdb->ev, state, timeval_current_ofs(CTDB_REQ_TIMEOUT, 0),
672 ctdb_call_timeout, state);
677 make a remote ctdb call - async recv - called in daemon context
679 This is called when the program wants to wait for a ctdb_call to complete and get the
680 results. This call will block unless the call has already completed.
682 int ctdb_daemon_call_recv(struct ctdb_call_state *state, struct ctdb_call *call)
684 while (state->state < CTDB_CALL_DONE) {
685 event_loop_once(state->node->ctdb->ev);
687 if (state->state != CTDB_CALL_DONE) {
688 ctdb_set_error(state->node->ctdb, "%s", state->errmsg);
693 if (state->call.reply_data.dsize) {
694 call->reply_data.dptr = talloc_memdup(state->node->ctdb,
695 state->call.reply_data.dptr,
696 state->call.reply_data.dsize);
697 call->reply_data.dsize = state->call.reply_data.dsize;
699 call->reply_data.dptr = NULL;
700 call->reply_data.dsize = 0;
702 call->status = state->call.status;