vacuum: use get_vacuum_interval() in ctdb_vacuum_event()
[ctdb.git] / server / ctdb_vacuum.c
1 /*
2    ctdb vacuuming events
3
4    Copyright (C) Ronnie Sahlberg  2009
5    Copyright (C) Michael Adam 2010-2011
6    Copyright (C) Stefan Metzmacher 2010-2011
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "lib/tevent/tevent.h"
24 #include "lib/tdb/include/tdb.h"
25 #include "system/network.h"
26 #include "system/filesys.h"
27 #include "system/dir.h"
28 #include "../include/ctdb_private.h"
29 #include "db_wrap.h"
30 #include "lib/util/dlinklist.h"
31 #include "lib/tevent/tevent.h"
32 #include "../include/ctdb_private.h"
33 #include "../common/rb_tree.h"
34
35 #define TIMELIMIT() timeval_current_ofs(10, 0)
36
37 enum vacuum_child_status { VACUUM_RUNNING, VACUUM_OK, VACUUM_ERROR, VACUUM_TIMEOUT};
38
39 struct ctdb_vacuum_child_context {
40         struct ctdb_vacuum_child_context *next, *prev;
41         struct ctdb_vacuum_handle *vacuum_handle;
42         /* fd child writes status to */
43         int fd[2];
44         pid_t child_pid;
45         enum vacuum_child_status status;
46         struct timeval start_time;
47 };
48
49 struct ctdb_vacuum_handle {
50         struct ctdb_db_context *ctdb_db;
51         struct ctdb_vacuum_child_context *child_ctx;
52         uint32_t fast_path_count;
53 };
54
55
56 /*  a list of records to possibly delete */
57 struct vacuum_data {
58         uint32_t vacuum_limit;
59         uint32_t repack_limit;
60         struct ctdb_context *ctdb;
61         struct ctdb_db_context *ctdb_db;
62         struct tdb_context *dest_db;
63         trbt_tree_t *delete_tree;
64         uint32_t delete_count;
65         struct ctdb_marshall_buffer **list;
66         struct timeval start;
67         bool traverse_error;
68         bool vacuum;
69         uint32_t total;
70         uint32_t vacuumed;
71         uint32_t copied;
72         uint32_t fast_added_to_vacuum_fetch_list;
73         uint32_t fast_added_to_delete_tree;
74         uint32_t fast_deleted;
75         uint32_t fast_skipped;
76         uint32_t fast_error;
77         uint32_t fast_total;
78         uint32_t full_added_to_vacuum_fetch_list;
79         uint32_t full_added_to_delete_tree;
80         uint32_t full_skipped;
81         uint32_t full_error;
82         uint32_t full_total;
83 };
84
85 /* tuning information stored for every db */
86 struct vacuum_tuning_data {
87         uint32_t last_num_repack;
88         uint32_t last_num_empty;
89         uint32_t last_interval;
90         uint32_t new_interval;
91         struct timeval last_start;
92         double   last_duration;
93 };
94
95 /* this structure contains the information for one record to be deleted */
96 struct delete_record_data {
97         struct ctdb_context *ctdb;
98         struct ctdb_db_context *ctdb_db;
99         struct ctdb_ltdb_header hdr;
100         TDB_DATA key;
101 };
102
103 struct delete_records_list {
104         struct ctdb_marshall_buffer *records;
105 };
106
107 /**
108  * Store key and header in a tree, indexed by the key hash.
109  */
110 static int insert_delete_record_data_into_tree(struct ctdb_context *ctdb,
111                                                struct ctdb_db_context *ctdb_db,
112                                                trbt_tree_t *tree,
113                                                const struct ctdb_ltdb_header *hdr,
114                                                TDB_DATA key)
115 {
116         struct delete_record_data *dd;
117         uint32_t hash;
118
119         dd = talloc_zero(tree, struct delete_record_data);
120         if (dd == NULL) {
121                 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
122                 return -1;
123         }
124
125         dd->ctdb      = ctdb;
126         dd->ctdb_db   = ctdb_db;
127         dd->key.dsize = key.dsize;
128         dd->key.dptr  = talloc_memdup(dd, key.dptr, key.dsize);
129         if (dd->key.dptr == NULL) {
130                 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
131                 return -1;
132         }
133
134         dd->hdr = *hdr;
135
136         hash = ctdb_hash(&key);
137
138         trbt_insert32(tree, hash, dd);
139
140         return 0;
141 }
142
143 static int add_record_to_delete_tree(struct vacuum_data *vdata, TDB_DATA key,
144                                      struct ctdb_ltdb_header *hdr)
145 {
146         struct ctdb_context *ctdb = vdata->ctdb;
147         struct ctdb_db_context *ctdb_db = vdata->ctdb_db;
148         uint32_t hash;
149         int ret;
150
151         hash = ctdb_hash(&key);
152
153         if (trbt_lookup32(vdata->delete_tree, hash)) {
154                 DEBUG(DEBUG_INFO, (__location__ " Hash collission when vacuuming, skipping this record.\n"));
155                 return 0;
156         }
157
158         ret = insert_delete_record_data_into_tree(ctdb, ctdb_db,
159                                                   vdata->delete_tree,
160                                                   hdr, key);
161         if (ret != 0) {
162                 return -1;
163         }
164
165         vdata->delete_count++;
166
167         return 0;
168 }
169
170 /**
171  * Add a record to the list of records to be sent
172  * to their lmaster with VACUUM_FETCH.
173  */
174 static int add_record_to_vacuum_fetch_list(struct vacuum_data *vdata,
175                                            TDB_DATA key)
176 {
177         struct ctdb_context *ctdb = vdata->ctdb;
178         struct ctdb_rec_data *rec;
179         uint32_t lmaster;
180         size_t old_size;
181
182         lmaster = ctdb_lmaster(ctdb, &key);
183
184         rec = ctdb_marshall_record(vdata->list[lmaster], ctdb->pnn, key, NULL, tdb_null);
185         if (rec == NULL) {
186                 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
187                 vdata->traverse_error = true;
188                 return -1;
189         }
190
191         old_size = talloc_get_size(vdata->list[lmaster]);
192         vdata->list[lmaster] = talloc_realloc_size(NULL, vdata->list[lmaster],
193                                                    old_size + rec->length);
194         if (vdata->list[lmaster] == NULL) {
195                 DEBUG(DEBUG_ERR,(__location__ " Failed to expand\n"));
196                 vdata->traverse_error = true;
197                 return -1;
198         }
199
200         vdata->list[lmaster]->count++;
201         memcpy(old_size+(uint8_t *)vdata->list[lmaster], rec, rec->length);
202         talloc_free(rec);
203
204         vdata->total++;
205
206         return 0;
207 }
208
209
210 static void ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
211                               struct timeval t, void *private_data);
212
213
214 /*
215  * traverse function for gathering the records that can be deleted
216  */
217 static int vacuum_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
218 {
219         struct vacuum_data *vdata = talloc_get_type(private, struct vacuum_data);
220         struct ctdb_context *ctdb = vdata->ctdb;
221         uint32_t lmaster;
222         struct ctdb_ltdb_header *hdr;
223         int res = 0;
224
225         vdata->full_total++;
226
227         lmaster = ctdb_lmaster(ctdb, &key);
228         if (lmaster >= ctdb->num_nodes) {
229                 vdata->full_error++;
230                 DEBUG(DEBUG_CRIT, (__location__
231                                    " lmaster[%u] >= ctdb->num_nodes[%u] for key"
232                                    " with hash[%u]!\n",
233                                    (unsigned)lmaster,
234                                    (unsigned)ctdb->num_nodes,
235                                    (unsigned)ctdb_hash(&key)));
236                 return -1;
237         }
238
239         if (data.dsize != sizeof(struct ctdb_ltdb_header)) {
240                 /* it is not a deleted record */
241                 vdata->full_skipped++;
242                 return 0;
243         }
244
245         hdr = (struct ctdb_ltdb_header *)data.dptr;
246
247         if (hdr->dmaster != ctdb->pnn) {
248                 vdata->full_skipped++;
249                 return 0;
250         }
251
252         if (lmaster == ctdb->pnn) {
253                 /*
254                  * We are both lmaster and dmaster, and the record is empty.
255                  * So we should be able to delete it.
256                  */
257                 res = add_record_to_delete_tree(vdata, key, hdr);
258                 if (res != 0) {
259                         vdata->full_error++;
260                 } else {
261                         vdata->full_added_to_delete_tree++;
262                 }
263         } else {
264                 /*
265                  * We are not lmaster.
266                  * Add the record to the blob ready to send to the nodes.
267                  */
268                 res = add_record_to_vacuum_fetch_list(vdata, key);
269                 if (res != 0) {
270                         vdata->full_error++;
271                 } else {
272                         vdata->full_added_to_vacuum_fetch_list++;
273                 }
274         }
275
276         return res;
277 }
278
279 /*
280  * traverse the tree of records to delete and marshall them into
281  * a blob
282  */
283 static int delete_traverse(void *param, void *data)
284 {
285         struct delete_record_data *dd = talloc_get_type(data, struct delete_record_data);
286         struct delete_records_list *recs = talloc_get_type(param, struct delete_records_list);
287         struct ctdb_rec_data *rec;
288         size_t old_size;
289
290         rec = ctdb_marshall_record(dd, recs->records->db_id, dd->key, &dd->hdr, tdb_null);
291         if (rec == NULL) {
292                 DEBUG(DEBUG_ERR, (__location__ " failed to marshall record\n"));
293                 return 0;
294         }
295
296         old_size = talloc_get_size(recs->records);
297         recs->records = talloc_realloc_size(NULL, recs->records, old_size + rec->length);
298         if (recs->records == NULL) {
299                 DEBUG(DEBUG_ERR,(__location__ " Failed to expand\n"));
300                 return 0;
301         }
302         recs->records->count++;
303         memcpy(old_size+(uint8_t *)(recs->records), rec, rec->length);
304         return 0;
305 }
306
307 /**
308  * traverse function for the traversal of the delete_queue,
309  * the fast-path vacuuming list.
310  *
311  *  - If the record has been migrated off the node
312  *    or has been revived (filled with data) on the node,
313  *    then skip the record.
314  *
315  *  - If the current node is the record's lmaster and it is
316  *    a record that has never been migrated with data, then
317  *    delete the record from the local tdb.
318  *
319  *  - If the current node is the record's lmaster and it has
320  *    been migrated with data, then schedule it for the normal
321  *    vacuuming procedure (i.e. add it to the delete_list).
322  *
323  *  - If the current node is NOT the record's lmaster then
324  *    add it to the list of records that are to be sent to
325  *    the lmaster with the VACUUM_FETCH message.
326  */
327 static int delete_queue_traverse(void *param, void *data)
328 {
329         struct delete_record_data *dd =
330                 talloc_get_type(data, struct delete_record_data);
331         struct vacuum_data *vdata = talloc_get_type(param, struct vacuum_data);
332         struct ctdb_db_context *ctdb_db = dd->ctdb_db;
333         struct ctdb_context *ctdb = ctdb_db->ctdb; /* or dd->ctdb ??? */
334         int res;
335         struct ctdb_ltdb_header *header;
336         TDB_DATA tdb_data;
337         uint32_t lmaster;
338
339         vdata->fast_total++;
340
341         res = tdb_chainlock(ctdb_db->ltdb->tdb, dd->key);
342         if (res != 0) {
343                 DEBUG(DEBUG_ERR, (__location__ " Error getting chainlock.\n"));
344                 vdata->fast_error++;
345                 return 0;
346         }
347
348         tdb_data = tdb_fetch(ctdb_db->ltdb->tdb, dd->key);
349         if (tdb_data.dsize < sizeof(struct ctdb_ltdb_header)) {
350                 /* Does not exist or not a ctdb record. Skip. */
351                 goto skipped;
352         }
353
354         if (tdb_data.dsize > sizeof(struct ctdb_ltdb_header)) {
355                 /* The record has been recycled (filled with data). Skip. */
356                 goto skipped;
357         }
358
359         header = (struct ctdb_ltdb_header *)tdb_data.dptr;
360
361         if (header->dmaster != ctdb->pnn) {
362                 /* The record has been migrated off the node. Skip. */
363                 goto skipped;
364         }
365
366
367         if (header->rsn != dd->hdr.rsn) {
368                 /*
369                  * The record has been migrated off the node and back again.
370                  * But not requeued for deletion. Skip it.
371                  */
372                 goto skipped;
373         }
374
375         /*
376          * We are dmaster, and the record has no data, and it has
377          * not been migrated after it has been queued for deletion.
378          *
379          * At this stage, the record could still have been revived locally
380          * and last been written with empty data. This can only be
381          * fixed with the addition of an active or delete flag. (TODO)
382          */
383
384         lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
385
386         if (lmaster != ctdb->pnn) {
387                 res = add_record_to_vacuum_fetch_list(vdata, dd->key);
388
389                 if (res != 0) {
390                         DEBUG(DEBUG_ERR,
391                               (__location__ " Error adding record to list "
392                                "of records to send to lmaster.\n"));
393                         vdata->fast_error++;
394                 } else {
395                         vdata->fast_added_to_vacuum_fetch_list++;
396                 }
397                 goto done;
398         }
399
400         /* use header->flags or dd->hdr.flags ?? */
401         if (dd->hdr.flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA) {
402                 res = add_record_to_delete_tree(vdata, dd->key, &dd->hdr);
403
404                 if (res != 0) {
405                         DEBUG(DEBUG_ERR,
406                               (__location__ " Error adding record to list "
407                                "of records for deletion on lmaster.\n"));
408                         vdata->fast_error++;
409                 } else {
410                         vdata->fast_added_to_delete_tree++;
411                 }
412         } else {
413                 res = tdb_delete(ctdb_db->ltdb->tdb, dd->key);
414
415                 if (res != 0) {
416                         DEBUG(DEBUG_ERR,
417                               (__location__ " Error deleting record from local "
418                                "data base.\n"));
419                         vdata->fast_error++;
420                 } else {
421                         vdata->fast_deleted++;
422                 }
423         }
424
425         goto done;
426
427 skipped:
428         vdata->fast_skipped++;
429
430 done:
431         if (tdb_data.dptr != NULL) {
432                 free(tdb_data.dptr);
433         }
434         tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
435
436         return 0;
437 }
438
439 /**
440  * Vacuum a DB:
441  *  - Always do the fast vacuuming run, which traverses
442  *    the in-memory delete queue: these records have been
443  *    scheduled for deletion.
444  *  - Only if explicitly requested, the database is traversed
445  *    in order to use the traditional heuristics on empty records
446  *    to trigger deletion.
447  *    This is done only every VacuumFastPathCount'th vacuuming run.
448  *
449  * The traverse runs fill two lists:
450  *
451  * - The delete_list:
452  *   This is the list of empty records the current
453  *   node is lmaster and dmaster for. These records are later
454  *   deleted first on other nodes and then locally.
455  *
456  *   The fast vacuuming run has a short cut for those records
457  *   that have never been migrated with data: these records
458  *   are immediately deleted locally, since they have left
459  *   no trace on other nodes.
460  *
461  * - The vacuum_fetch lists
462  *   (one for each other lmaster node):
463  *   The records in this list are sent for deletion to
464  *   their lmaster in a bulk VACUUM_FETCH message.
465  *
466  *   The lmaster then migrates all these records to itelf
467  *   so that they can be vacuumed there.
468  *
469  * This executes in the child context.
470  */
471 static int ctdb_vacuum_db(struct ctdb_db_context *ctdb_db,
472                           struct vacuum_data *vdata,
473                           bool full_vacuum_run)
474 {
475         struct ctdb_context *ctdb = ctdb_db->ctdb;
476         const char *name = ctdb_db->db_name;
477         int ret, i, pnn;
478
479         DEBUG(DEBUG_INFO, (__location__ " Entering %s vacuum run for db "
480                            "%s db_id[0x%08x]\n",
481                            full_vacuum_run ? "full" : "fast",
482                            ctdb_db->db_name, ctdb_db->db_id));
483
484         ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &ctdb->vnn_map);
485         if (ret != 0) {
486                 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from local node\n"));
487                 return ret;
488         }
489
490         pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
491         if (pnn == -1) {
492                 DEBUG(DEBUG_ERR, ("Unable to get pnn from local node\n"));
493                 return -1;
494         }
495
496         ctdb->pnn = pnn;
497
498         vdata->fast_added_to_delete_tree = 0;
499         vdata->fast_added_to_vacuum_fetch_list = 0;
500         vdata->fast_deleted = 0;
501         vdata->fast_skipped = 0;
502         vdata->fast_error = 0;
503         vdata->fast_total = 0;
504         vdata->full_added_to_delete_tree = 0;
505         vdata->full_added_to_vacuum_fetch_list = 0;
506         vdata->full_skipped = 0;
507         vdata->full_error = 0;
508         vdata->full_total = 0;
509
510         /* the list needs to be of length num_nodes */
511         vdata->list = talloc_array(vdata, struct ctdb_marshall_buffer *, ctdb->num_nodes);
512         if (vdata->list == NULL) {
513                 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
514                 return -1;
515         }
516         for (i = 0; i < ctdb->num_nodes; i++) {
517                 vdata->list[i] = (struct ctdb_marshall_buffer *)
518                         talloc_zero_size(vdata->list,
519                                          offsetof(struct ctdb_marshall_buffer, data));
520                 if (vdata->list[i] == NULL) {
521                         DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
522                         return -1;
523                 }
524                 vdata->list[i]->db_id = ctdb_db->db_id;
525         }
526
527         /*
528          * Traverse the delete_queue.
529          * This builds the same lists as the db traverse.
530          */
531         trbt_traversearray32(ctdb_db->delete_queue, 1, delete_queue_traverse, vdata);
532
533         if (vdata->fast_total > 0) {
534                 DEBUG(DEBUG_INFO,
535                       (__location__
536                        " fast vacuuming delete_queue traverse statistics: "
537                        "db[%s] "
538                        "total[%u] "
539                        "del[%u] "
540                        "skp[%u] "
541                        "err[%u] "
542                        "adt[%u] "
543                        "avf[%u]\n",
544                        ctdb_db->db_name,
545                        (unsigned)vdata->fast_total,
546                        (unsigned)vdata->fast_deleted,
547                        (unsigned)vdata->fast_skipped,
548                        (unsigned)vdata->fast_error,
549                        (unsigned)vdata->fast_added_to_delete_tree,
550                        (unsigned)vdata->fast_added_to_vacuum_fetch_list));
551         }
552
553         /*
554          * read-only traverse of the database, looking for records that
555          * might be able to be vacuumed.
556          *
557          * This is not done each time but only every tunable
558          * VacuumFastPathCount times.
559          */
560         if (full_vacuum_run) {
561                 ret = tdb_traverse_read(ctdb_db->ltdb->tdb, vacuum_traverse, vdata);
562                 if (ret == -1 || vdata->traverse_error) {
563                         DEBUG(DEBUG_ERR,(__location__ " Traverse error in vacuuming '%s'\n", name));
564                         return -1;
565                 }
566                 if (vdata->full_total > 0) {
567                         DEBUG(DEBUG_INFO,
568                               (__location__
569                                " full vacuuming db traverse statistics: "
570                                "db[%s] "
571                                "total[%u] "
572                                "skp[%u] "
573                                "err[%u] "
574                                "adt[%u] "
575                                "avf[%u]\n",
576                                ctdb_db->db_name,
577                                (unsigned)vdata->full_total,
578                                (unsigned)vdata->full_skipped,
579                                (unsigned)vdata->full_error,
580                                (unsigned)vdata->full_added_to_delete_tree,
581                                (unsigned)vdata->full_added_to_vacuum_fetch_list));
582                 }
583         }
584
585         /*
586          * For records where we are not the lmaster,
587          * tell the lmaster to fetch the record.
588          */
589         for (i = 0; i < ctdb->num_nodes; i++) {
590                 TDB_DATA data;
591
592                 if (ctdb->nodes[i]->pnn == ctdb->pnn) {
593                         continue;
594                 }
595
596                 if (vdata->list[i]->count == 0) {
597                         continue;
598                 }
599
600                 DEBUG(DEBUG_INFO, ("Found %u records for lmaster %u in '%s'\n",
601                                    vdata->list[i]->count, ctdb->nodes[i]->pnn,
602                                    name));
603
604                 data.dsize = talloc_get_size(vdata->list[i]);
605                 data.dptr  = (void *)vdata->list[i];
606                 if (ctdb_client_send_message(ctdb, ctdb->nodes[i]->pnn, CTDB_SRVID_VACUUM_FETCH, data) != 0) {
607                         DEBUG(DEBUG_ERR, (__location__ " Failed to send vacuum "
608                                           "fetch message to %u\n",
609                                           ctdb->nodes[i]->pnn));
610                         return -1;
611                 }
612         }       
613
614         /* Process all records we can delete (if any) */
615         if (vdata->delete_count > 0) {
616                 struct delete_records_list *recs;
617                 TDB_DATA indata, outdata;
618                 int32_t res;
619                 struct ctdb_node_map *nodemap;
620                 uint32_t *active_nodes;
621                 int num_active_nodes;
622
623                 recs = talloc_zero(vdata, struct delete_records_list);
624                 if (recs == NULL) {
625                         DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
626                         return -1;
627                 }
628                 recs->records = (struct ctdb_marshall_buffer *)
629                         talloc_zero_size(vdata, 
630                                     offsetof(struct ctdb_marshall_buffer, data));
631                 if (recs->records == NULL) {
632                         DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
633                         return -1;
634                 }
635                 recs->records->db_id = ctdb_db->db_id;
636
637                 /* 
638                  * traverse the tree of all records we want to delete and
639                  * create a blob we can send to the other nodes.
640                  */
641                 trbt_traversearray32(vdata->delete_tree, 1, delete_traverse, recs);
642
643                 indata.dsize = talloc_get_size(recs->records);
644                 indata.dptr  = (void *)recs->records;
645
646                 /* 
647                  * now tell all the active nodes to delete all these records
648                  * (if possible)
649                  */
650
651                 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
652                                            CTDB_CURRENT_NODE,
653                                            recs, /* talloc context */
654                                            &nodemap);
655                 if (ret != 0) {
656                         DEBUG(DEBUG_ERR,(__location__ " unable to get node map\n"));
657                         return -1;
658                 }
659
660                 active_nodes = list_of_active_nodes(ctdb, nodemap,
661                                                     nodemap, /* talloc context */
662                                                     false /* include self */);
663                 /* yuck! ;-) */
664                 num_active_nodes = talloc_get_size(active_nodes)/sizeof(*active_nodes);
665
666                 for (i = 0; i < num_active_nodes; i++) {
667                         struct ctdb_marshall_buffer *records;
668                         struct ctdb_rec_data *rec;
669
670                         ret = ctdb_control(ctdb, active_nodes[i], 0,
671                                         CTDB_CONTROL_TRY_DELETE_RECORDS, 0,
672                                         indata, recs, &outdata, &res,
673                                         NULL, NULL);
674                         if (ret != 0 || res != 0) {
675                                 DEBUG(DEBUG_ERR, ("Failed to delete records on "
676                                                   "node %u: ret[%d] res[%d]\n",
677                                                   active_nodes[i], ret, res));
678                                 return -1;
679                         }
680
681                         /* 
682                          * outdata countains the list of records coming back
683                          * from the node which the node could not delete
684                          */
685                         records = (struct ctdb_marshall_buffer *)outdata.dptr;
686                         rec = (struct ctdb_rec_data *)&records->data[0];
687                         while (records->count-- > 1) {
688                                 TDB_DATA reckey, recdata;
689                                 struct ctdb_ltdb_header *rechdr;
690
691                                 reckey.dptr = &rec->data[0];
692                                 reckey.dsize = rec->keylen;
693                                 recdata.dptr = &rec->data[reckey.dsize];
694                                 recdata.dsize = rec->datalen;
695
696                                 if (recdata.dsize < sizeof(struct ctdb_ltdb_header)) {
697                                         DEBUG(DEBUG_CRIT,(__location__ " bad ltdb record\n"));
698                                         return -1;
699                                 }
700                                 rechdr = (struct ctdb_ltdb_header *)recdata.dptr;
701                                 recdata.dptr += sizeof(*rechdr);
702                                 recdata.dsize -= sizeof(*rechdr);
703
704                                 /* 
705                                  * that other node couldnt delete the record
706                                  * so we should delete it and thereby remove it from the tree
707                                  */
708                                 talloc_free(trbt_lookup32(vdata->delete_tree, ctdb_hash(&reckey)));
709
710                                 rec = (struct ctdb_rec_data *)(rec->length + (uint8_t *)rec);
711                         }           
712                 }
713
714                 /* free nodemap and active_nodes */
715                 talloc_free(nodemap);
716
717                 /* 
718                  * The only records remaining in the tree would be those
719                  * records where all other nodes could successfully
720                  * delete them, so we can safely delete them on the
721                  * lmaster as well. Deletion implictely happens while
722                  * we repack the database. The repack algorithm revisits 
723                  * the tree in order to find the records that don't need
724                  * to be copied / repacked.
725                  */
726         }
727
728         /* this ensures we run our event queue */
729         ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
730
731         return 0;
732 }
733
734
735 /*
736  * traverse function for repacking
737  */
738 static int repack_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
739 {
740         struct vacuum_data *vdata = (struct vacuum_data *)private;
741
742         if (vdata->vacuum) {
743                 uint32_t hash = ctdb_hash(&key);
744                 struct delete_record_data *kd;
745                 /*
746                  * check if we can ignore this record because it's in the delete_tree
747                  */
748                 kd = (struct delete_record_data *)trbt_lookup32(vdata->delete_tree, hash);
749                 /*
750                  * there might be hash collisions so we have to compare the keys here to be sure
751                  */
752                 if (kd && kd->key.dsize == key.dsize && memcmp(kd->key.dptr, key.dptr, key.dsize) == 0) {
753                         struct ctdb_ltdb_header *hdr = (struct ctdb_ltdb_header *)data.dptr;
754                         /*
755                          * we have to check if the record hasn't changed in the meantime in order to
756                          * savely remove it from the database
757                          */
758                         if (data.dsize == sizeof(struct ctdb_ltdb_header) &&
759                                 hdr->dmaster == kd->ctdb->pnn &&
760                                 ctdb_lmaster(kd->ctdb, &(kd->key)) == kd->ctdb->pnn &&
761                                 kd->hdr.rsn == hdr->rsn) {
762                                 vdata->vacuumed++;
763                                 return 0;
764                         }
765                 }
766         }
767         if (tdb_store(vdata->dest_db, key, data, TDB_INSERT) != 0) {
768                 vdata->traverse_error = true;
769                 return -1;
770         }
771         vdata->copied++;
772         return 0;
773 }
774
775 /*
776  * repack a tdb
777  */
778 static int ctdb_repack_tdb(struct tdb_context *tdb, TALLOC_CTX *mem_ctx, struct vacuum_data *vdata)
779 {
780         struct tdb_context *tmp_db;
781
782         if (tdb_transaction_start(tdb) != 0) {
783                 DEBUG(DEBUG_ERR,(__location__ " Failed to start transaction\n"));
784                 return -1;
785         }
786
787         tmp_db = tdb_open("tmpdb", tdb_hash_size(tdb),
788                           TDB_INTERNAL|TDB_DISALLOW_NESTING,
789                           O_RDWR|O_CREAT, 0);
790         if (tmp_db == NULL) {
791                 DEBUG(DEBUG_ERR,(__location__ " Failed to create tmp_db\n"));
792                 tdb_transaction_cancel(tdb);
793                 return -1;
794         }
795
796         vdata->traverse_error = false;
797         vdata->dest_db = tmp_db;
798         vdata->vacuum = true;
799         vdata->vacuumed = 0;
800         vdata->copied = 0;
801
802         /*
803          * repack and vacuum on-the-fly by not writing the records that are
804          * no longer needed
805          */
806         if (tdb_traverse_read(tdb, repack_traverse, vdata) == -1) {
807                 DEBUG(DEBUG_ERR,(__location__ " Failed to traverse copying out\n"));
808                 tdb_transaction_cancel(tdb);
809                 tdb_close(tmp_db);
810                 return -1;              
811         }
812
813         DEBUG(DEBUG_INFO,(__location__ " %u records vacuumed\n", vdata->vacuumed));
814         
815         if (vdata->traverse_error) {
816                 DEBUG(DEBUG_ERR,(__location__ " Error during traversal\n"));
817                 tdb_transaction_cancel(tdb);
818                 tdb_close(tmp_db);
819                 return -1;
820         }
821
822         if (tdb_wipe_all(tdb) != 0) {
823                 DEBUG(DEBUG_ERR,(__location__ " Failed to wipe database\n"));
824                 tdb_transaction_cancel(tdb);
825                 tdb_close(tmp_db);
826                 return -1;
827         }
828
829         vdata->traverse_error = false;
830         vdata->dest_db = tdb;
831         vdata->vacuum = false;
832         vdata->copied = 0;
833
834         if (tdb_traverse_read(tmp_db, repack_traverse, vdata) == -1) {
835                 DEBUG(DEBUG_ERR,(__location__ " Failed to traverse copying back\n"));
836                 tdb_transaction_cancel(tdb);
837                 tdb_close(tmp_db);
838                 return -1;              
839         }
840
841         if (vdata->traverse_error) {
842                 DEBUG(DEBUG_ERR,(__location__ " Error during second traversal\n"));
843                 tdb_transaction_cancel(tdb);
844                 tdb_close(tmp_db);
845                 return -1;
846         }
847
848         tdb_close(tmp_db);
849
850
851         if (tdb_transaction_commit(tdb) != 0) {
852                 DEBUG(DEBUG_ERR,(__location__ " Failed to commit\n"));
853                 return -1;
854         }
855         DEBUG(DEBUG_INFO,(__location__ " %u records copied\n", vdata->copied));
856
857         return 0;
858 }
859
860 /*
861  * repack and vaccum a db
862  * called from the child context
863  */
864 static int ctdb_vacuum_and_repack_db(struct ctdb_db_context *ctdb_db,
865                                      TALLOC_CTX *mem_ctx,
866                                      bool full_vacuum_run)
867 {
868         uint32_t repack_limit = ctdb_db->ctdb->tunable.repack_limit;
869         uint32_t vacuum_limit = ctdb_db->ctdb->tunable.vacuum_limit;
870         const char *name = ctdb_db->db_name;
871         int freelist_size;
872         struct vacuum_data *vdata;
873
874         freelist_size = tdb_freelist_size(ctdb_db->ltdb->tdb);
875         if (freelist_size == -1) {
876                 DEBUG(DEBUG_ERR,(__location__ " Failed to get freelist size for '%s'\n", name));
877                 return -1;
878         }
879
880         vdata = talloc_zero(mem_ctx, struct vacuum_data);
881         if (vdata == NULL) {
882                 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
883                 return -1;
884         }
885
886         vdata->ctdb = ctdb_db->ctdb;
887         vdata->vacuum_limit = vacuum_limit;
888         vdata->repack_limit = repack_limit;
889         vdata->delete_tree = trbt_create(vdata, 0);
890         vdata->ctdb_db = ctdb_db;
891         if (vdata->delete_tree == NULL) {
892                 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
893                 talloc_free(vdata);
894                 return -1;
895         }
896
897         vdata->start = timeval_current();
898  
899         /*
900          * gather all records that can be deleted in vdata
901          */
902         if (ctdb_vacuum_db(ctdb_db, vdata, full_vacuum_run) != 0) {
903                 DEBUG(DEBUG_ERR,(__location__ " Failed to vacuum '%s'\n", name));
904         }
905
906         /*
907          * decide if a repack is necessary
908          */
909         if (freelist_size < repack_limit && vdata->delete_count < vacuum_limit)
910         {
911                 talloc_free(vdata);
912                 return 0;
913         }
914
915         DEBUG(DEBUG_INFO,("Repacking %s with %u freelist entries and %u records to delete\n", 
916                         name, freelist_size, vdata->delete_count));
917
918         /*
919          * repack and implicitely get rid of the records we can delete
920          */
921         if (ctdb_repack_tdb(ctdb_db->ltdb->tdb, mem_ctx, vdata) != 0) {
922                 DEBUG(DEBUG_ERR,(__location__ " Failed to repack '%s'\n", name));
923                 talloc_free(vdata);
924                 return -1;
925         }
926         talloc_free(vdata);
927
928         return 0;
929 }
930
931 static int get_vacuum_interval(struct ctdb_db_context *ctdb_db)
932 {
933         uint interval = ctdb_db->ctdb->tunable.vacuum_default_interval;
934
935         return interval;
936 }
937
938 static int vacuum_child_destructor(struct ctdb_vacuum_child_context *child_ctx)
939 {
940         double l = timeval_elapsed(&child_ctx->start_time);
941         struct ctdb_db_context *ctdb_db = child_ctx->vacuum_handle->ctdb_db;
942         struct ctdb_context *ctdb = ctdb_db->ctdb;
943
944         DEBUG(DEBUG_INFO,("Vacuuming took %.3f seconds for database %s\n", l, ctdb_db->db_name));
945
946         if (child_ctx->child_pid != -1) {
947                 kill(child_ctx->child_pid, SIGKILL);
948         } else {
949                 /* Bump the number of successful fast-path runs. */
950                 child_ctx->vacuum_handle->fast_path_count++;
951         }
952
953         DLIST_REMOVE(ctdb->vacuumers, child_ctx);
954
955         event_add_timed(ctdb->ev, child_ctx->vacuum_handle,
956                         timeval_current_ofs(get_vacuum_interval(ctdb_db), 0), 
957                         ctdb_vacuum_event, child_ctx->vacuum_handle);
958
959         return 0;
960 }
961
962 /*
963  * this event is generated when a vacuum child process times out
964  */
965 static void vacuum_child_timeout(struct event_context *ev, struct timed_event *te,
966                                          struct timeval t, void *private_data)
967 {
968         struct ctdb_vacuum_child_context *child_ctx = talloc_get_type(private_data, struct ctdb_vacuum_child_context);
969
970         DEBUG(DEBUG_ERR,("Vacuuming child process timed out for db %s\n", child_ctx->vacuum_handle->ctdb_db->db_name));
971
972         child_ctx->status = VACUUM_TIMEOUT;
973
974         talloc_free(child_ctx);
975 }
976
977
978 /*
979  * this event is generated when a vacuum child process has completed
980  */
981 static void vacuum_child_handler(struct event_context *ev, struct fd_event *fde,
982                              uint16_t flags, void *private_data)
983 {
984         struct ctdb_vacuum_child_context *child_ctx = talloc_get_type(private_data, struct ctdb_vacuum_child_context);
985         char c = 0;
986         int ret;
987
988         DEBUG(DEBUG_INFO,("Vacuuming child process %d finished for db %s\n", child_ctx->child_pid, child_ctx->vacuum_handle->ctdb_db->db_name));
989         child_ctx->child_pid = -1;
990
991         ret = read(child_ctx->fd[0], &c, 1);
992         if (ret != 1 || c != 0) {
993                 child_ctx->status = VACUUM_ERROR;
994                 DEBUG(DEBUG_ERR, ("A vacuum child process failed with an error for database %s. ret=%d c=%d\n", child_ctx->vacuum_handle->ctdb_db->db_name, ret, c));
995         } else {
996                 child_ctx->status = VACUUM_OK;
997         }
998
999         talloc_free(child_ctx);
1000 }
1001
1002 /*
1003  * this event is called every time we need to start a new vacuum process
1004  */
1005 static void
1006 ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
1007                                struct timeval t, void *private_data)
1008 {
1009         struct ctdb_vacuum_handle *vacuum_handle = talloc_get_type(private_data, struct ctdb_vacuum_handle);
1010         struct ctdb_db_context *ctdb_db = vacuum_handle->ctdb_db;
1011         struct ctdb_context *ctdb = ctdb_db->ctdb;
1012         struct ctdb_vacuum_child_context *child_ctx;
1013         struct tevent_fd *fde;
1014         int ret;
1015
1016         /* we dont vacuum if we are in recovery mode, or db frozen */
1017         if (ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ||
1018             ctdb->freeze_mode[ctdb_db->priority] != CTDB_FREEZE_NONE) {
1019                 DEBUG(DEBUG_INFO, ("Not vacuuming %s (%s)\n", ctdb_db->db_name,
1020                                    ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ? "in recovery"
1021                                    : ctdb->freeze_mode[ctdb_db->priority] == CTDB_FREEZE_PENDING
1022                                    ? "freeze pending"
1023                                    : "frozen"));
1024                 event_add_timed(ctdb->ev, vacuum_handle,
1025                         timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1026                         ctdb_vacuum_event, vacuum_handle);
1027                 return;
1028         }
1029
1030         child_ctx = talloc(vacuum_handle, struct ctdb_vacuum_child_context);
1031         if (child_ctx == NULL) {
1032                 DEBUG(DEBUG_CRIT, (__location__ " Failed to allocate child context for vacuuming of %s\n", ctdb_db->db_name));
1033                 ctdb_fatal(ctdb, "Out of memory when crating vacuum child context. Shutting down\n");
1034         }
1035
1036
1037         ret = pipe(child_ctx->fd);
1038         if (ret != 0) {
1039                 talloc_free(child_ctx);
1040                 DEBUG(DEBUG_ERR, ("Failed to create pipe for vacuum child process.\n"));
1041                 event_add_timed(ctdb->ev, vacuum_handle,
1042                         timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1043                         ctdb_vacuum_event, vacuum_handle);
1044                 return;
1045         }
1046
1047         if (vacuum_handle->fast_path_count > ctdb->tunable.vacuum_fast_path_count) {
1048                 vacuum_handle->fast_path_count = 0;
1049         }
1050
1051         child_ctx->child_pid = ctdb_fork(ctdb);
1052         if (child_ctx->child_pid == (pid_t)-1) {
1053                 close(child_ctx->fd[0]);
1054                 close(child_ctx->fd[1]);
1055                 talloc_free(child_ctx);
1056                 DEBUG(DEBUG_ERR, ("Failed to fork vacuum child process.\n"));
1057                 event_add_timed(ctdb->ev, vacuum_handle,
1058                         timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1059                         ctdb_vacuum_event, vacuum_handle);
1060                 return;
1061         }
1062
1063
1064         if (child_ctx->child_pid == 0) {
1065                 char cc = 0;
1066                 bool full_vacuum_run = false;
1067                 close(child_ctx->fd[0]);
1068
1069                 DEBUG(DEBUG_INFO,("Vacuuming child process %d for db %s started\n", getpid(), ctdb_db->db_name));
1070         
1071                 if (switch_from_server_to_client(ctdb, "vacuum-%s", ctdb_db->db_name) != 0) {
1072                         DEBUG(DEBUG_CRIT, (__location__ "ERROR: failed to switch vacuum daemon into client mode. Shutting down.\n"));
1073                         _exit(1);
1074                 }
1075
1076                 /* 
1077                  * repack the db
1078                  */
1079                 if ((ctdb->tunable.vacuum_fast_path_count > 0) &&
1080                     (vacuum_handle->fast_path_count == 0))
1081                 {
1082                         full_vacuum_run = true;
1083                 }
1084                 cc = ctdb_vacuum_and_repack_db(ctdb_db, child_ctx,
1085                                                full_vacuum_run);
1086
1087                 write(child_ctx->fd[1], &cc, 1);
1088                 _exit(0);
1089         }
1090
1091         set_close_on_exec(child_ctx->fd[0]);
1092         close(child_ctx->fd[1]);
1093
1094         child_ctx->status = VACUUM_RUNNING;
1095         child_ctx->start_time = timeval_current();
1096
1097         DLIST_ADD(ctdb->vacuumers, child_ctx);
1098         talloc_set_destructor(child_ctx, vacuum_child_destructor);
1099
1100         /*
1101          * Clear the fastpath vacuuming list in the parent.
1102          */
1103         talloc_free(ctdb_db->delete_queue);
1104         ctdb_db->delete_queue = trbt_create(ctdb_db, 0);
1105         if (ctdb_db->delete_queue == NULL) {
1106                 /* fatal here? ... */
1107                 ctdb_fatal(ctdb, "Out of memory when re-creating vacuum tree "
1108                                  "in parent context. Shutting down\n");
1109         }
1110
1111         event_add_timed(ctdb->ev, child_ctx,
1112                 timeval_current_ofs(ctdb->tunable.vacuum_max_run_time, 0),
1113                 vacuum_child_timeout, child_ctx);
1114
1115         DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d to child vacuum process\n", child_ctx->fd[0]));
1116
1117         fde = event_add_fd(ctdb->ev, child_ctx, child_ctx->fd[0],
1118                            EVENT_FD_READ, vacuum_child_handler, child_ctx);
1119         tevent_fd_set_auto_close(fde);
1120
1121         vacuum_handle->child_ctx = child_ctx;
1122         child_ctx->vacuum_handle = vacuum_handle;
1123 }
1124
1125 void ctdb_stop_vacuuming(struct ctdb_context *ctdb)
1126 {
1127         /* Simply free them all. */
1128         while (ctdb->vacuumers) {
1129                 DEBUG(DEBUG_INFO, ("Aborting vacuuming for %s (%i)\n",
1130                            ctdb->vacuumers->vacuum_handle->ctdb_db->db_name,
1131                            (int)ctdb->vacuumers->child_pid));
1132                 /* vacuum_child_destructor kills it, removes from list */
1133                 talloc_free(ctdb->vacuumers);
1134         }
1135 }
1136
1137 /* this function initializes the vacuuming context for a database
1138  * starts the vacuuming events
1139  */
1140 int ctdb_vacuum_init(struct ctdb_db_context *ctdb_db)
1141 {
1142         if (ctdb_db->persistent != 0) {
1143                 DEBUG(DEBUG_ERR,("Vacuuming is disabled for persistent database %s\n", ctdb_db->db_name));
1144                 return 0;
1145         }
1146
1147         ctdb_db->vacuum_handle = talloc(ctdb_db, struct ctdb_vacuum_handle);
1148         CTDB_NO_MEMORY(ctdb_db->ctdb, ctdb_db->vacuum_handle);
1149
1150         ctdb_db->vacuum_handle->ctdb_db         = ctdb_db;
1151         ctdb_db->vacuum_handle->fast_path_count = 0;
1152
1153         event_add_timed(ctdb_db->ctdb->ev, ctdb_db->vacuum_handle, 
1154                         timeval_current_ofs(get_vacuum_interval(ctdb_db), 0), 
1155                         ctdb_vacuum_event, ctdb_db->vacuum_handle);
1156
1157         return 0;
1158 }
1159
1160 static void remove_record_from_delete_queue(struct ctdb_db_context *ctdb_db,
1161                                             const struct ctdb_ltdb_header *hdr,
1162                                             const TDB_DATA key)
1163 {
1164         struct delete_record_data *kd;
1165         uint32_t hash;
1166
1167         hash = (uint32_t)ctdb_hash(&key);
1168
1169         DEBUG(DEBUG_DEBUG, (__location__
1170                             " remove_record_from_delete_queue: db[%s] "
1171                             "db_id[0x%08x] "
1172                             "key_hash[0x%08x] "
1173                             "lmaster[%u] "
1174                             "migrated_with_data[%s]\n",
1175                              ctdb_db->db_name, ctdb_db->db_id,
1176                              hash,
1177                              ctdb_lmaster(ctdb_db->ctdb, &key),
1178                              hdr->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA ? "yes" : "no"));
1179
1180         kd = (struct delete_record_data *)trbt_lookup32(ctdb_db->delete_queue, hash);
1181         if (kd == NULL) {
1182                 return;
1183         }
1184         if (kd->key.dsize != key.dsize) {
1185                 return;
1186         }
1187         if (memcmp(kd->key.dptr, key.dptr, key.dsize) != 0) {
1188                 return;
1189         }
1190
1191         talloc_free(kd);
1192
1193         return;
1194 }
1195
1196 /**
1197  * Insert a record into the ctdb_db context's delete queue,
1198  * handling hash collisions.
1199  */
1200 static int insert_record_into_delete_queue(struct ctdb_db_context *ctdb_db,
1201                                            const struct ctdb_ltdb_header *hdr,
1202                                            TDB_DATA key)
1203 {
1204         struct delete_record_data *kd;
1205         uint32_t hash;
1206         int ret;
1207
1208         hash = (uint32_t)ctdb_hash(&key);
1209
1210         DEBUG(DEBUG_INFO, (__location__ " Schedule for deletion: db[%s] "
1211                            "db_id[0x%08x] "
1212                            "key_hash[0x%08x] "
1213                            "lmaster[%u] "
1214                            "migrated_with_data[%s]\n",
1215                             ctdb_db->db_name, ctdb_db->db_id,
1216                             hash,
1217                             ctdb_lmaster(ctdb_db->ctdb, &key),
1218                             hdr->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA ? "yes" : "no"));
1219
1220         kd = (struct delete_record_data *)trbt_lookup32(ctdb_db->delete_queue, hash);
1221         if (kd != NULL) {
1222                 if ((kd->key.dsize != key.dsize) ||
1223                     (memcmp(kd->key.dptr, key.dptr, key.dsize) != 0))
1224                 {
1225                         DEBUG(DEBUG_INFO,
1226                               ("schedule for deletion: Hash collision (0x%08x)."
1227                                " Skipping the record.\n", hash));
1228                         return 0;
1229                 } else {
1230                         DEBUG(DEBUG_DEBUG,
1231                               ("schedule for deletion: Overwriting entry for "
1232                                "key with hash 0x%08x.\n", hash));
1233                 }
1234         }
1235
1236         ret = insert_delete_record_data_into_tree(ctdb_db->ctdb, ctdb_db,
1237                                                   ctdb_db->delete_queue,
1238                                                   hdr, key);
1239         if (ret != 0) {
1240                 return -1;
1241         }
1242
1243         return 0;
1244 }
1245
1246 /**
1247  * Schedule a record for deletetion.
1248  * Called from the parent context.
1249  */
1250 int32_t ctdb_control_schedule_for_deletion(struct ctdb_context *ctdb,
1251                                            TDB_DATA indata)
1252 {
1253         struct ctdb_control_schedule_for_deletion *dd;
1254         struct ctdb_db_context *ctdb_db;
1255         int ret;
1256         TDB_DATA key;
1257
1258         dd = (struct ctdb_control_schedule_for_deletion *)indata.dptr;
1259
1260         ctdb_db = find_ctdb_db(ctdb, dd->db_id);
1261         if (ctdb_db == NULL) {
1262                 DEBUG(DEBUG_ERR, (__location__ " Unknown db id 0x%08x\n",
1263                                   dd->db_id));
1264                 return -1;
1265         }
1266
1267         key.dsize = dd->keylen;
1268         key.dptr = dd->key;
1269
1270         ret = insert_record_into_delete_queue(ctdb_db, &dd->hdr, key);
1271
1272         return ret;
1273 }
1274
1275 int32_t ctdb_local_schedule_for_deletion(struct ctdb_db_context *ctdb_db,
1276                                          const struct ctdb_ltdb_header *hdr,
1277                                          TDB_DATA key)
1278 {
1279         int ret;
1280         struct ctdb_control_schedule_for_deletion *dd;
1281         TDB_DATA indata;
1282         int32_t status;
1283
1284         if (ctdb_db->ctdb->ctdbd_pid == getpid()) {
1285                 /* main daemon - directly queue */
1286                 ret = insert_record_into_delete_queue(ctdb_db, hdr, key);
1287
1288                 return ret;
1289         }
1290
1291         /* child process: send the main daemon a control */
1292
1293         indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + key.dsize;
1294         indata.dptr = talloc_zero_array(ctdb_db, uint8_t, indata.dsize);
1295         if (indata.dptr == NULL) {
1296                 DEBUG(DEBUG_ERR, (__location__ " out of memory\n"));
1297                 return -1;
1298         }
1299         dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
1300         dd->db_id = ctdb_db->db_id;
1301         dd->hdr = *hdr;
1302         dd->keylen = key.dsize;
1303         memcpy(dd->key, key.dptr, key.dsize);
1304
1305         ret = ctdb_control(ctdb_db->ctdb,
1306                            CTDB_CURRENT_NODE,
1307                            ctdb_db->db_id,
1308                            CTDB_CONTROL_SCHEDULE_FOR_DELETION,
1309                            CTDB_CTRL_FLAG_NOREPLY, /* flags */
1310                            indata,
1311                            NULL, /* mem_ctx */
1312                            NULL, /* outdata */
1313                            &status,
1314                            NULL, /* timeout : NULL == wait forever */
1315                            NULL); /* error message */
1316
1317         talloc_free(indata.dptr);
1318
1319         if (ret != 0 || status != 0) {
1320                 DEBUG(DEBUG_ERR, (__location__ " Error sending "
1321                                   "SCHEDULE_FOR_DELETION "
1322                                   "control.\n"));
1323                 if (status != 0) {
1324                         ret = -1;
1325                 }
1326         }
1327
1328         return ret;
1329 }
1330
1331 void ctdb_local_remove_from_delete_queue(struct ctdb_db_context *ctdb_db,
1332                                          const struct ctdb_ltdb_header *hdr,
1333                                          const TDB_DATA key)
1334 {
1335         if (ctdb_db->ctdb->ctdbd_pid != getpid()) {
1336                 /*
1337                  * Only remove the record from the delete queue if called
1338                  * in the main daemon.
1339                  */
1340                 return;
1341         }
1342
1343         remove_record_from_delete_queue(ctdb_db, hdr, key);
1344
1345         return;
1346 }