change debug output from vnn to pnn
[metze/ctdb/wip.git] / server / ctdb_call.c
1 /* 
2    ctdb_call protocol code
3
4    Copyright (C) Andrew Tridgell  2006
5
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.
10    
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.
15    
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/>.
18 */
19 /*
20   see http://wiki.samba.org/index.php/Samba_%26_Clustering for
21   protocol design and packet details
22 */
23 #include "includes.h"
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"
30
31 /*
32   find the ctdb_db from a db index
33  */
34  struct ctdb_db_context *find_ctdb_db(struct ctdb_context *ctdb, uint32_t id)
35 {
36         struct ctdb_db_context *ctdb_db;
37
38         for (ctdb_db=ctdb->db_list; ctdb_db; ctdb_db=ctdb_db->next) {
39                 if (ctdb_db->db_id == id) {
40                         break;
41                 }
42         }
43         return ctdb_db;
44 }
45
46
47 /*
48   a varient of input packet that can be used in lock requeue
49 */
50 static void ctdb_call_input_pkt(void *p, struct ctdb_req_header *hdr)
51 {
52         struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
53         ctdb_input_pkt(ctdb, hdr);
54 }
55
56
57 /*
58   send an error reply
59 */
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,
65                             const char *fmt, ...)
66 {
67         va_list ap;
68         struct ctdb_reply_error *r;
69         char *msg;
70         int msglen, len;
71
72         va_start(ap, fmt);
73         msg = talloc_vasprintf(ctdb, fmt, ap);
74         if (msg == NULL) {
75                 ctdb_fatal(ctdb, "Unable to allocate error in ctdb_send_error\n");
76         }
77         va_end(ap);
78
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);
84
85         r->hdr.destnode  = hdr->srcnode;
86         r->hdr.reqid     = hdr->reqid;
87         r->status        = status;
88         r->msglen        = msglen;
89         memcpy(&r->msg[0], msg, msglen);
90
91         ctdb_queue_packet(ctdb, &r->hdr);
92
93         talloc_free(msg);
94 }
95
96
97 /*
98   send a redirect reply
99 */
100 static void ctdb_call_send_redirect(struct ctdb_context *ctdb, 
101                                     TDB_DATA key,
102                                     struct ctdb_req_call *c, 
103                                     struct ctdb_ltdb_header *header)
104 {
105         
106         uint32_t lmaster = ctdb_lmaster(ctdb, &key);
107         if (ctdb->pnn == lmaster) {
108                 c->hdr.destnode = header->dmaster;
109         } else if ((c->hopcount % ctdb->tunable.max_redirect_count) == 0) {
110                 c->hdr.destnode = lmaster;
111         } else {
112                 c->hdr.destnode = header->dmaster;
113         }
114         c->hopcount++;
115         ctdb_queue_packet(ctdb, &c->hdr);
116 }
117
118
119 /*
120   send a dmaster reply
121
122   caller must have the chainlock before calling this routine. Caller must be
123   the lmaster
124 */
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,
129                                     uint32_t reqid)
130 {
131         struct ctdb_context *ctdb = ctdb_db->ctdb;
132         struct ctdb_reply_dmaster *r;
133         int ret, len;
134         TALLOC_CTX *tmp_ctx;
135
136         if (ctdb->pnn != ctdb_lmaster(ctdb, &key)) {
137                 DEBUG(0,(__location__ " Caller is not lmaster!\n"));
138                 return;
139         }
140
141         header->dmaster = new_dmaster;
142         ret = ctdb_ltdb_store(ctdb_db, key, header, data);
143         if (ret != 0) {
144                 ctdb_fatal(ctdb, "ctdb_req_dmaster unable to update dmaster");
145                 return;
146         }
147
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);
151
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);
157
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);
166
167         ctdb_queue_packet(ctdb, &r->hdr);
168
169         talloc_free(tmp_ctx);
170 }
171
172 /*
173   send a dmaster request (give another node the dmaster for a record)
174
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
178 */
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)
183 {
184         struct ctdb_req_dmaster *r;
185         struct ctdb_context *ctdb = ctdb_db->ctdb;
186         int len;
187         uint32_t lmaster = ctdb_lmaster(ctdb, key);
188
189         if (lmaster == ctdb->pnn) {
190                 ctdb_send_dmaster_reply(ctdb_db, header, *key, *data, 
191                                         c->hdr.srcnode, c->hdr.reqid);
192                 return;
193         }
194         
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;
201         r->db_id         = c->db_id;
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);
208
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");
212         }
213         
214         ctdb_queue_packet(ctdb, &r->hdr);
215
216         talloc_free(r);
217 }
218
219 /*
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.
222
223   must be called with the chainlock held. This function releases the chainlock
224 */
225 static void ctdb_become_dmaster(struct ctdb_db_context *ctdb_db, 
226                                 struct ctdb_req_header *hdr,
227                                 TDB_DATA key, TDB_DATA data,
228                                 uint64_t rsn)
229 {
230         struct ctdb_call_state *state;
231         struct ctdb_context *ctdb = ctdb_db->ctdb;
232         struct ctdb_ltdb_header header;
233
234         DEBUG(2,("pnn %u dmaster response %08x\n", ctdb->pnn, ctdb_hash(&key)));
235
236         ZERO_STRUCT(header);
237         header.rsn = rsn + 1;
238         header.dmaster = ctdb->pnn;
239
240         if (ctdb_ltdb_store(ctdb_db, key, &header, data) != 0) {
241                 ctdb_fatal(ctdb, "ctdb_reply_dmaster store failed\n");
242                 ctdb_ltdb_unlock(ctdb_db, key);
243                 return;
244         }
245
246         state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_call_state);
247
248         if (state == NULL) {
249                 DEBUG(0,("pnn %u Invalid reqid %u in ctdb_become_dmaster from node %u\n",
250                          ctdb->pnn, hdr->reqid, hdr->srcnode));
251                 ctdb_ltdb_unlock(ctdb_db, key);
252                 return;
253         }
254
255         if (hdr->reqid != state->reqid) {
256                 /* we found a record  but it was the wrong one */
257                 DEBUG(0, ("Dropped orphan in ctdb_become_dmaster with reqid:%u\n from node %u", hdr->reqid, hdr->srcnode));
258                 ctdb_ltdb_unlock(ctdb_db, key);
259                 return;
260         }
261
262         ctdb_call_local(ctdb_db, &state->call, &header, state, &data, ctdb->pnn);
263
264         ctdb_ltdb_unlock(ctdb_db, state->call.key);
265
266         state->state = CTDB_CALL_DONE;
267         if (state->async.fn) {
268                 state->async.fn(state);
269         }
270 }
271
272
273
274 /*
275   called when a CTDB_REQ_DMASTER packet comes in
276
277   this comes into the lmaster for a record when the current dmaster
278   wants to give up the dmaster role and give it to someone else
279 */
280 void ctdb_request_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
281 {
282         struct ctdb_req_dmaster *c = (struct ctdb_req_dmaster *)hdr;
283         TDB_DATA key, data, data2;
284         struct ctdb_ltdb_header header;
285         struct ctdb_db_context *ctdb_db;
286         int ret;
287
288         key.dptr = c->data;
289         key.dsize = c->keylen;
290         data.dptr = c->data + c->keylen;
291         data.dsize = c->datalen;
292
293         ctdb_db = find_ctdb_db(ctdb, c->db_id);
294         if (!ctdb_db) {
295                 ctdb_send_error(ctdb, hdr, -1,
296                                 "Unknown database in request. db_id==0x%08x",
297                                 c->db_id);
298                 return;
299         }
300         
301         /* fetch the current record */
302         ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, key, &header, hdr, &data2,
303                                            ctdb_call_input_pkt, ctdb, False);
304         if (ret == -1) {
305                 ctdb_fatal(ctdb, "ctdb_req_dmaster failed to fetch record");
306                 return;
307         }
308         if (ret == -2) {
309                 DEBUG(2,(__location__ " deferring ctdb_request_dmaster\n"));
310                 return;
311         }
312
313         if (ctdb_lmaster(ctdb, &key) != ctdb->pnn) {
314                 DEBUG(0,("pnn %u dmaster request to non-lmaster lmaster=%u gen=%u curgen=%u\n",
315                          ctdb->pnn, ctdb_lmaster(ctdb, &key), 
316                          hdr->generation, ctdb->vnn_map->generation));
317                 ctdb_fatal(ctdb, "ctdb_req_dmaster to non-lmaster");
318         }
319
320         DEBUG(2,("pnn %u dmaster request on %08x for %u from %u\n", 
321                  ctdb->pnn, ctdb_hash(&key), c->dmaster, c->hdr.srcnode));
322
323         /* its a protocol error if the sending node is not the current dmaster */
324         if (header.dmaster != hdr->srcnode) {
325                 DEBUG(0,("pnn %u dmaster request non-master %u dmaster=%u key %08x dbid 0x%08x gen=%u curgen=%u\n",
326                          ctdb->pnn, hdr->srcnode, header.dmaster, ctdb_hash(&key),
327                          ctdb_db->db_id, hdr->generation, ctdb->vnn_map->generation));
328                 ctdb_fatal(ctdb, "ctdb_req_dmaster from non-master");
329                 return;
330         }
331
332         /* use the rsn from the sending node */
333         header.rsn = c->rsn;
334
335         /* check if the new dmaster is the lmaster, in which case we
336            skip the dmaster reply */
337         if (c->dmaster == ctdb->pnn) {
338                 ctdb_become_dmaster(ctdb_db, hdr, key, data, c->rsn);
339         } else {
340                 ctdb_send_dmaster_reply(ctdb_db, &header, key, data, c->dmaster, hdr->reqid);
341                 ctdb_ltdb_unlock(ctdb_db, key);
342         }
343 }
344
345
346 /*
347   called when a CTDB_REQ_CALL packet comes in
348 */
349 void ctdb_request_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
350 {
351         struct ctdb_req_call *c = (struct ctdb_req_call *)hdr;
352         TDB_DATA data;
353         struct ctdb_reply_call *r;
354         int ret, len;
355         struct ctdb_ltdb_header header;
356         struct ctdb_call call;
357         struct ctdb_db_context *ctdb_db;
358
359         ctdb_db = find_ctdb_db(ctdb, c->db_id);
360         if (!ctdb_db) {
361                 ctdb_send_error(ctdb, hdr, -1,
362                                 "Unknown database in request. db_id==0x%08x",
363                                 c->db_id);
364                 return;
365         }
366
367         call.call_id  = c->callid;
368         call.key.dptr = c->data;
369         call.key.dsize = c->keylen;
370         call.call_data.dptr = c->data + c->keylen;
371         call.call_data.dsize = c->calldatalen;
372
373         /* determine if we are the dmaster for this key. This also
374            fetches the record data (if any), thus avoiding a 2nd fetch of the data 
375            if the call will be answered locally */
376
377         ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, call.key, &header, hdr, &data,
378                                            ctdb_call_input_pkt, ctdb, False);
379         if (ret == -1) {
380                 ctdb_send_error(ctdb, hdr, ret, "ltdb fetch failed in ctdb_request_call");
381                 return;
382         }
383         if (ret == -2) {
384                 DEBUG(2,(__location__ " deferred ctdb_request_call\n"));
385                 return;
386         }
387
388         /* if we are not the dmaster, then send a redirect to the
389            requesting node */
390         if (header.dmaster != ctdb->pnn) {
391                 talloc_free(data.dptr);
392                 ctdb_call_send_redirect(ctdb, call.key, c, &header);
393                 ctdb_ltdb_unlock(ctdb_db, call.key);
394                 return;
395         }
396
397         if (c->hopcount > ctdb->statistics.max_hop_count) {
398                 ctdb->statistics.max_hop_count = c->hopcount;
399         }
400
401         /* if this nodes has done enough consecutive calls on the same record
402            then give them the record
403            or if the node requested an immediate migration
404         */
405         if ( c->hdr.srcnode != ctdb->pnn &&
406              ((header.laccessor == c->hdr.srcnode
407                && header.lacount >= ctdb->tunable.max_lacount)
408               || (c->flags & CTDB_IMMEDIATE_MIGRATION)) ) {
409                 DEBUG(2,("pnn %u starting migration of %08x to %u\n", 
410                          ctdb->pnn, ctdb_hash(&call.key), c->hdr.srcnode));
411                 ctdb_call_send_dmaster(ctdb_db, c, &header, &call.key, &data);
412                 talloc_free(data.dptr);
413                 ctdb_ltdb_unlock(ctdb_db, call.key);
414                 return;
415         }
416
417         ctdb_call_local(ctdb_db, &call, &header, hdr, &data, c->hdr.srcnode);
418
419         ctdb_ltdb_unlock(ctdb_db, call.key);
420
421         len = offsetof(struct ctdb_reply_call, data) + call.reply_data.dsize;
422         r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REPLY_CALL, len, 
423                                     struct ctdb_reply_call);
424         CTDB_NO_MEMORY_FATAL(ctdb, r);
425         r->hdr.destnode  = hdr->srcnode;
426         r->hdr.reqid     = hdr->reqid;
427         r->status        = call.status;
428         r->datalen       = call.reply_data.dsize;
429         if (call.reply_data.dsize) {
430                 memcpy(&r->data[0], call.reply_data.dptr, call.reply_data.dsize);
431         }
432
433         ctdb_queue_packet(ctdb, &r->hdr);
434
435         talloc_free(r);
436 }
437
438 /*
439   called when a CTDB_REPLY_CALL packet comes in
440
441   This packet comes in response to a CTDB_REQ_CALL request packet. It
442   contains any reply data from the call
443 */
444 void ctdb_reply_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
445 {
446         struct ctdb_reply_call *c = (struct ctdb_reply_call *)hdr;
447         struct ctdb_call_state *state;
448
449         state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_call_state);
450         if (state == NULL) {
451                 DEBUG(0, (__location__ " reqid %u not found\n", hdr->reqid));
452                 return;
453         }
454
455         if (hdr->reqid != state->reqid) {
456                 /* we found a record  but it was the wrong one */
457                 DEBUG(0, ("Dropped orphaned call reply with reqid:%u\n",hdr->reqid));
458                 return;
459         }
460
461         state->call.reply_data.dptr = c->data;
462         state->call.reply_data.dsize = c->datalen;
463         state->call.status = c->status;
464
465         talloc_steal(state, c);
466
467         state->state = CTDB_CALL_DONE;
468         if (state->async.fn) {
469                 state->async.fn(state);
470         }
471 }
472
473
474 /*
475   called when a CTDB_REPLY_DMASTER packet comes in
476
477   This packet comes in from the lmaster response to a CTDB_REQ_CALL
478   request packet. It means that the current dmaster wants to give us
479   the dmaster role
480 */
481 void ctdb_reply_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
482 {
483         struct ctdb_reply_dmaster *c = (struct ctdb_reply_dmaster *)hdr;
484         struct ctdb_db_context *ctdb_db;
485         TDB_DATA key, data;
486         int ret;
487
488         ctdb_db = find_ctdb_db(ctdb, c->db_id);
489         if (ctdb_db == NULL) {
490                 DEBUG(0,("Unknown db_id 0x%x in ctdb_reply_dmaster\n", c->db_id));
491                 return;
492         }
493         
494         key.dptr = c->data;
495         key.dsize = c->keylen;
496         data.dptr = &c->data[key.dsize];
497         data.dsize = c->datalen;
498
499         ret = ctdb_ltdb_lock_requeue(ctdb_db, key, hdr,
500                                      ctdb_call_input_pkt, ctdb, False);
501         if (ret == -2) {
502                 return;
503         }
504         if (ret != 0) {
505                 DEBUG(0,(__location__ " Failed to get lock in ctdb_reply_dmaster\n"));
506                 return;
507         }
508
509         ctdb_become_dmaster(ctdb_db, hdr, key, data, c->rsn);
510 }
511
512
513 /*
514   called when a CTDB_REPLY_ERROR packet comes in
515 */
516 void ctdb_reply_error(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
517 {
518         struct ctdb_reply_error *c = (struct ctdb_reply_error *)hdr;
519         struct ctdb_call_state *state;
520
521         state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_call_state);
522         if (state == NULL) {
523                 DEBUG(0,("pnn %u Invalid reqid %u in ctdb_reply_error\n",
524                          ctdb->pnn, hdr->reqid));
525                 return;
526         }
527
528         if (hdr->reqid != state->reqid) {
529                 /* we found a record  but it was the wrong one */
530                 DEBUG(0, ("Dropped orphaned error reply with reqid:%u\n",hdr->reqid));
531                 return;
532         }
533
534         talloc_steal(state, c);
535
536         state->state  = CTDB_CALL_ERROR;
537         state->errmsg = (char *)c->msg;
538         if (state->async.fn) {
539                 state->async.fn(state);
540         }
541 }
542
543
544 /*
545   destroy a ctdb_call
546 */
547 static int ctdb_call_destructor(struct ctdb_call_state *state)
548 {
549         DLIST_REMOVE(state->ctdb_db->ctdb->pending_calls, state);
550         ctdb_reqid_remove(state->ctdb_db->ctdb, state->reqid);
551         return 0;
552 }
553
554
555 /*
556   called when a ctdb_call needs to be resent after a reconfigure event
557 */
558 static void ctdb_call_resend(struct ctdb_call_state *state)
559 {
560         struct ctdb_context *ctdb = state->ctdb_db->ctdb;
561
562         state->generation = ctdb->vnn_map->generation;
563
564         /* use a new reqid, in case the old reply does eventually come in */
565         ctdb_reqid_remove(ctdb, state->reqid);
566         state->reqid = ctdb_reqid_new(ctdb, state);
567         state->c->hdr.reqid = state->reqid;
568
569         /* update the generation count for this request, so its valid with the new vnn_map */
570         state->c->hdr.generation = state->generation;
571
572         /* send the packet to ourselves, it will be redirected appropriately */
573         state->c->hdr.destnode = ctdb->pnn;
574
575         ctdb_queue_packet(ctdb, &state->c->hdr);
576         DEBUG(0,("resent ctdb_call\n"));
577 }
578
579 /*
580   resend all pending calls on recovery
581  */
582 void ctdb_call_resend_all(struct ctdb_context *ctdb)
583 {
584         struct ctdb_call_state *state, *next;
585         for (state=ctdb->pending_calls;state;state=next) {
586                 next = state->next;
587                 ctdb_call_resend(state);
588         }
589 }
590
591 /*
592   this allows the caller to setup a async.fn 
593 */
594 static void call_local_trigger(struct event_context *ev, struct timed_event *te, 
595                        struct timeval t, void *private_data)
596 {
597         struct ctdb_call_state *state = talloc_get_type(private_data, struct ctdb_call_state);
598         if (state->async.fn) {
599                 state->async.fn(state);
600         }
601 }       
602
603
604 /*
605   construct an event driven local ctdb_call
606
607   this is used so that locally processed ctdb_call requests are processed
608   in an event driven manner
609 */
610 struct ctdb_call_state *ctdb_call_local_send(struct ctdb_db_context *ctdb_db, 
611                                              struct ctdb_call *call,
612                                              struct ctdb_ltdb_header *header,
613                                              TDB_DATA *data)
614 {
615         struct ctdb_call_state *state;
616         struct ctdb_context *ctdb = ctdb_db->ctdb;
617         int ret;
618
619         state = talloc_zero(ctdb_db, struct ctdb_call_state);
620         CTDB_NO_MEMORY_NULL(ctdb, state);
621
622         talloc_steal(state, data->dptr);
623
624         state->state = CTDB_CALL_DONE;
625         state->call = *call;
626         state->ctdb_db = ctdb_db;
627
628         ret = ctdb_call_local(ctdb_db, &state->call, header, state, data, ctdb->pnn);
629
630         event_add_timed(ctdb->ev, state, timeval_zero(), call_local_trigger, state);
631
632         return state;
633 }
634
635
636 /*
637   make a remote ctdb call - async send. Called in daemon context.
638
639   This constructs a ctdb_call request and queues it for processing. 
640   This call never blocks.
641 */
642 struct ctdb_call_state *ctdb_daemon_call_send_remote(struct ctdb_db_context *ctdb_db, 
643                                                      struct ctdb_call *call, 
644                                                      struct ctdb_ltdb_header *header)
645 {
646         uint32_t len;
647         struct ctdb_call_state *state;
648         struct ctdb_context *ctdb = ctdb_db->ctdb;
649
650         state = talloc_zero(ctdb_db, struct ctdb_call_state);
651         CTDB_NO_MEMORY_NULL(ctdb, state);
652         state->reqid = ctdb_reqid_new(ctdb, state);
653         state->ctdb_db = ctdb_db;
654         talloc_set_destructor(state, ctdb_call_destructor);
655
656         len = offsetof(struct ctdb_req_call, data) + call->key.dsize + call->call_data.dsize;
657         state->c = ctdb_transport_allocate(ctdb, state, CTDB_REQ_CALL, len, 
658                                            struct ctdb_req_call);
659         CTDB_NO_MEMORY_NULL(ctdb, state->c);
660         state->c->hdr.destnode  = header->dmaster;
661
662         /* this limits us to 16k outstanding messages - not unreasonable */
663         state->c->hdr.reqid     = state->reqid;
664         state->c->flags         = call->flags;
665         state->c->db_id         = ctdb_db->db_id;
666         state->c->callid        = call->call_id;
667         state->c->hopcount      = 0;
668         state->c->keylen        = call->key.dsize;
669         state->c->calldatalen   = call->call_data.dsize;
670         memcpy(&state->c->data[0], call->key.dptr, call->key.dsize);
671         memcpy(&state->c->data[call->key.dsize], 
672                call->call_data.dptr, call->call_data.dsize);
673         state->call                = *call;
674         state->call.call_data.dptr = &state->c->data[call->key.dsize];
675         state->call.key.dptr       = &state->c->data[0];
676
677         state->state  = CTDB_CALL_WAIT;
678         state->generation = ctdb->vnn_map->generation;
679
680         DLIST_ADD(ctdb->pending_calls, state);
681
682         ctdb_queue_packet(ctdb, &state->c->hdr);
683
684         return state;
685 }
686
687 /*
688   make a remote ctdb call - async recv - called in daemon context
689
690   This is called when the program wants to wait for a ctdb_call to complete and get the 
691   results. This call will block unless the call has already completed.
692 */
693 int ctdb_daemon_call_recv(struct ctdb_call_state *state, struct ctdb_call *call)
694 {
695         while (state->state < CTDB_CALL_DONE) {
696                 event_loop_once(state->ctdb_db->ctdb->ev);
697         }
698         if (state->state != CTDB_CALL_DONE) {
699                 ctdb_set_error(state->ctdb_db->ctdb, "%s", state->errmsg);
700                 talloc_free(state);
701                 return -1;
702         }
703
704         if (state->call.reply_data.dsize) {
705                 call->reply_data.dptr = talloc_memdup(state->ctdb_db->ctdb,
706                                                       state->call.reply_data.dptr,
707                                                       state->call.reply_data.dsize);
708                 call->reply_data.dsize = state->call.reply_data.dsize;
709         } else {
710                 call->reply_data.dptr = NULL;
711                 call->reply_data.dsize = 0;
712         }
713         call->status = state->call.status;
714         talloc_free(state);
715         return 0;
716 }
717
718
719 /* 
720    send a keepalive packet to the other node
721 */
722 void ctdb_send_keepalive(struct ctdb_context *ctdb, uint32_t destnode)
723 {
724         struct ctdb_req_keepalive *r;
725         
726         r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REQ_KEEPALIVE,
727                                     sizeof(struct ctdb_req_keepalive), 
728                                     struct ctdb_req_keepalive);
729         CTDB_NO_MEMORY_FATAL(ctdb, r);
730         r->hdr.destnode  = destnode;
731         r->hdr.reqid     = 0;
732         
733         ctdb->statistics.keepalive_packets_sent++;
734
735         ctdb_queue_packet(ctdb, &r->hdr);
736
737         talloc_free(r);
738 }