ctdb-vacuum: use tdb_parse_record instead of tdb_fetch in delete_queue_traverse()
[metze/samba/wip.git] / ctdb / server / ctdb_vacuum.c
1 /*
2    ctdb vacuuming events
3
4    Copyright (C) Ronnie Sahlberg  2009
5    Copyright (C) Michael Adam 2010-2013
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 "tdb.h"
24 #include "system/network.h"
25 #include "system/filesys.h"
26 #include "system/dir.h"
27 #include "../include/ctdb_private.h"
28 #include "db_wrap.h"
29 #include "lib/util/dlinklist.h"
30 #include "../include/ctdb_private.h"
31 #include "../common/rb_tree.h"
32
33 #define TIMELIMIT() timeval_current_ofs(10, 0)
34
35 enum vacuum_child_status { VACUUM_RUNNING, VACUUM_OK, VACUUM_ERROR, VACUUM_TIMEOUT};
36
37 struct ctdb_vacuum_child_context {
38         struct ctdb_vacuum_child_context *next, *prev;
39         struct ctdb_vacuum_handle *vacuum_handle;
40         /* fd child writes status to */
41         int fd[2];
42         pid_t child_pid;
43         enum vacuum_child_status status;
44         struct timeval start_time;
45 };
46
47 struct ctdb_vacuum_handle {
48         struct ctdb_db_context *ctdb_db;
49         struct ctdb_vacuum_child_context *child_ctx;
50         uint32_t fast_path_count;
51 };
52
53
54 /*  a list of records to possibly delete */
55 struct vacuum_data {
56         uint32_t vacuum_limit;
57         uint32_t repack_limit;
58         struct ctdb_context *ctdb;
59         struct ctdb_db_context *ctdb_db;
60         struct tdb_context *dest_db;
61         trbt_tree_t *delete_list;
62         uint32_t delete_count;
63         struct ctdb_marshall_buffer **vacuum_fetch_list;
64         struct timeval start;
65         bool traverse_error;
66         bool vacuum;
67         uint32_t total;
68         uint32_t vacuumed;
69         uint32_t copied;
70         uint32_t fast_added_to_vacuum_fetch_list;
71         uint32_t fast_added_to_delete_list;
72         uint32_t fast_deleted;
73         uint32_t fast_skipped;
74         uint32_t fast_error;
75         uint32_t fast_total;
76         uint32_t full_added_to_vacuum_fetch_list;
77         uint32_t full_added_to_delete_list;
78         uint32_t full_skipped;
79         uint32_t full_error;
80         uint32_t full_total;
81         uint32_t delete_left;
82         uint32_t delete_remote_error;
83         uint32_t delete_local_error;
84         uint32_t delete_deleted;
85         uint32_t delete_skipped;
86 };
87
88 /* this structure contains the information for one record to be deleted */
89 struct delete_record_data {
90         struct ctdb_context *ctdb;
91         struct ctdb_db_context *ctdb_db;
92         struct ctdb_ltdb_header hdr;
93         TDB_DATA key;
94         uint8_t keydata[1];
95 };
96
97 struct delete_records_list {
98         struct ctdb_marshall_buffer *records;
99         struct vacuum_data *vdata;
100 };
101
102 /**
103  * Store key and header in a tree, indexed by the key hash.
104  */
105 static int insert_delete_record_data_into_tree(struct ctdb_context *ctdb,
106                                                struct ctdb_db_context *ctdb_db,
107                                                trbt_tree_t *tree,
108                                                const struct ctdb_ltdb_header *hdr,
109                                                TDB_DATA key)
110 {
111         struct delete_record_data *dd;
112         uint32_t hash;
113         size_t len;
114
115         len = offsetof(struct delete_record_data, keydata) + key.dsize;
116
117         dd = (struct delete_record_data *)talloc_size(tree, len);
118         if (dd == NULL) {
119                 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
120                 return -1;
121         }
122         talloc_set_name_const(dd, "struct delete_record_data");
123
124         dd->ctdb      = ctdb;
125         dd->ctdb_db   = ctdb_db;
126         dd->key.dsize = key.dsize;
127         dd->key.dptr  = dd->keydata;
128         memcpy(dd->keydata, key.dptr, key.dsize);
129
130         dd->hdr = *hdr;
131
132         hash = ctdb_hash(&key);
133
134         trbt_insert32(tree, hash, dd);
135
136         return 0;
137 }
138
139 static int add_record_to_delete_list(struct vacuum_data *vdata, TDB_DATA key,
140                                      struct ctdb_ltdb_header *hdr)
141 {
142         struct ctdb_context *ctdb = vdata->ctdb;
143         struct ctdb_db_context *ctdb_db = vdata->ctdb_db;
144         uint32_t hash;
145         int ret;
146
147         hash = ctdb_hash(&key);
148
149         if (trbt_lookup32(vdata->delete_list, hash)) {
150                 DEBUG(DEBUG_INFO, (__location__ " Hash collision when vacuuming, skipping this record.\n"));
151                 return 0;
152         }
153
154         ret = insert_delete_record_data_into_tree(ctdb, ctdb_db,
155                                                   vdata->delete_list,
156                                                   hdr, key);
157         if (ret != 0) {
158                 return -1;
159         }
160
161         vdata->delete_count++;
162
163         return 0;
164 }
165
166 /**
167  * Add a record to the list of records to be sent
168  * to their lmaster with VACUUM_FETCH.
169  */
170 static int add_record_to_vacuum_fetch_list(struct vacuum_data *vdata,
171                                            TDB_DATA key)
172 {
173         struct ctdb_context *ctdb = vdata->ctdb;
174         struct ctdb_rec_data *rec;
175         uint32_t lmaster;
176         size_t old_size;
177         struct ctdb_marshall_buffer *vfl;
178
179         lmaster = ctdb_lmaster(ctdb, &key);
180
181         vfl = vdata->vacuum_fetch_list[lmaster];
182
183         rec = ctdb_marshall_record(vfl, ctdb->pnn, key, NULL, tdb_null);
184         if (rec == NULL) {
185                 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
186                 vdata->traverse_error = true;
187                 return -1;
188         }
189
190         old_size = talloc_get_size(vfl);
191         vfl = talloc_realloc_size(NULL, vfl, old_size + rec->length);
192         if (vfl == NULL) {
193                 DEBUG(DEBUG_ERR,(__location__ " Failed to expand\n"));
194                 vdata->traverse_error = true;
195                 return -1;
196         }
197         vdata->vacuum_fetch_list[lmaster] = vfl;
198
199         vfl->count++;
200         memcpy(old_size+(uint8_t *)vfl, rec, rec->length);
201         talloc_free(rec);
202
203         vdata->total++;
204
205         return 0;
206 }
207
208
209 static void ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
210                               struct timeval t, void *private_data);
211
212 static int vacuum_record_parser(TDB_DATA key, TDB_DATA data, void *private_data)
213 {
214         struct ctdb_ltdb_header *header =
215                 (struct ctdb_ltdb_header *)private_data;
216
217         if (data.dsize != sizeof(struct ctdb_ltdb_header)) {
218                 return -1;
219         }
220
221         *header = *(struct ctdb_ltdb_header *)data.dptr;
222
223         return 0;
224 }
225
226 /*
227  * traverse function for gathering the records that can be deleted
228  */
229 static int vacuum_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
230 {
231         struct vacuum_data *vdata = talloc_get_type(private, struct vacuum_data);
232         struct ctdb_context *ctdb = vdata->ctdb;
233         uint32_t lmaster;
234         struct ctdb_ltdb_header *hdr;
235         int res = 0;
236
237         vdata->full_total++;
238
239         lmaster = ctdb_lmaster(ctdb, &key);
240         if (lmaster >= ctdb->num_nodes) {
241                 vdata->full_error++;
242                 DEBUG(DEBUG_CRIT, (__location__
243                                    " lmaster[%u] >= ctdb->num_nodes[%u] for key"
244                                    " with hash[%u]!\n",
245                                    (unsigned)lmaster,
246                                    (unsigned)ctdb->num_nodes,
247                                    (unsigned)ctdb_hash(&key)));
248                 return -1;
249         }
250
251         if (data.dsize != sizeof(struct ctdb_ltdb_header)) {
252                 /* it is not a deleted record */
253                 vdata->full_skipped++;
254                 return 0;
255         }
256
257         hdr = (struct ctdb_ltdb_header *)data.dptr;
258
259         if (hdr->dmaster != ctdb->pnn) {
260                 vdata->full_skipped++;
261                 return 0;
262         }
263
264         if (lmaster == ctdb->pnn) {
265                 /*
266                  * We are both lmaster and dmaster, and the record is empty.
267                  * So we should be able to delete it.
268                  */
269                 res = add_record_to_delete_list(vdata, key, hdr);
270                 if (res != 0) {
271                         vdata->full_error++;
272                 } else {
273                         vdata->full_added_to_delete_list++;
274                 }
275         } else {
276                 /*
277                  * We are not lmaster.
278                  * Add the record to the blob ready to send to the nodes.
279                  */
280                 res = add_record_to_vacuum_fetch_list(vdata, key);
281                 if (res != 0) {
282                         vdata->full_error++;
283                 } else {
284                         vdata->full_added_to_vacuum_fetch_list++;
285                 }
286         }
287
288         return res;
289 }
290
291 /*
292  * traverse the tree of records to delete and marshall them into
293  * a blob
294  */
295 static int delete_marshall_traverse(void *param, void *data)
296 {
297         struct delete_record_data *dd = talloc_get_type(data, struct delete_record_data);
298         struct delete_records_list *recs = talloc_get_type(param, struct delete_records_list);
299         struct ctdb_rec_data *rec;
300         size_t old_size;
301
302         rec = ctdb_marshall_record(dd, recs->records->db_id, dd->key, &dd->hdr, tdb_null);
303         if (rec == NULL) {
304                 DEBUG(DEBUG_ERR, (__location__ " failed to marshall record\n"));
305                 return 0;
306         }
307
308         old_size = talloc_get_size(recs->records);
309         recs->records = talloc_realloc_size(NULL, recs->records, old_size + rec->length);
310         if (recs->records == NULL) {
311                 DEBUG(DEBUG_ERR,(__location__ " Failed to expand\n"));
312                 return 0;
313         }
314         recs->records->count++;
315         memcpy(old_size+(uint8_t *)(recs->records), rec, rec->length);
316         return 0;
317 }
318
319 /**
320  * Variant of delete_marshall_traverse() that bumps the
321  * RSN of each traversed record in the database.
322  *
323  * This is needed to ensure that when rolling out our
324  * empty record copy before remote deletion, we as the
325  * record's dmaster keep a higher RSN than the non-dmaster
326  * nodes. This is needed to prevent old copies from
327  * resurrection in recoveries.
328  */
329 static int delete_marshall_traverse_first(void *param, void *data)
330 {
331         struct delete_record_data *dd = talloc_get_type(data, struct delete_record_data);
332         struct delete_records_list *recs = talloc_get_type(param, struct delete_records_list);
333         struct ctdb_db_context *ctdb_db = dd->ctdb_db;
334         struct ctdb_context *ctdb = ctdb_db->ctdb;
335         struct ctdb_ltdb_header *header;
336         TDB_DATA tdb_data, ctdb_data;
337         uint32_t lmaster;
338         uint32_t hash = ctdb_hash(&(dd->key));
339         int res;
340
341         res = tdb_chainlock(ctdb_db->ltdb->tdb, dd->key);
342         if (res != 0) {
343                 DEBUG(DEBUG_ERR,
344                       (__location__ " Error getting chainlock on record with "
345                        "key hash [0x%08x] on database db[%s].\n",
346                        hash, ctdb_db->db_name));
347                 recs->vdata->delete_skipped++;
348                 talloc_free(dd);
349                 return 0;
350         }
351
352         /*
353          * Verify that the record is still empty, its RSN has not
354          * changed and that we are still its lmaster and dmaster.
355          */
356
357         tdb_data = tdb_fetch(ctdb_db->ltdb->tdb, dd->key);
358         if (tdb_data.dsize < sizeof(struct ctdb_ltdb_header)) {
359                 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
360                                    "on database db[%s] does not exist or is not"
361                                    " a ctdb-record.  skipping.\n",
362                                    hash, ctdb_db->db_name));
363                 goto skip;
364         }
365
366         if (tdb_data.dsize > sizeof(struct ctdb_ltdb_header)) {
367                 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
368                                    "on database db[%s] has been recycled. "
369                                    "skipping.\n",
370                                    hash, ctdb_db->db_name));
371                 goto skip;
372         }
373
374         header = (struct ctdb_ltdb_header *)tdb_data.dptr;
375
376         if (header->flags & CTDB_REC_RO_FLAGS) {
377                 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
378                                    "on database db[%s] has read-only flags. "
379                                    "skipping.\n",
380                                    hash, ctdb_db->db_name));
381                 goto skip;
382         }
383
384         if (header->dmaster != ctdb->pnn) {
385                 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
386                                    "on database db[%s] has been migrated away. "
387                                    "skipping.\n",
388                                    hash, ctdb_db->db_name));
389                 goto skip;
390         }
391
392         if (header->rsn != dd->hdr.rsn) {
393                 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
394                                    "on database db[%s] seems to have been "
395                                    "migrated away and back again (with empty "
396                                    "data). skipping.\n",
397                                    hash, ctdb_db->db_name));
398                 goto skip;
399         }
400
401         lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
402
403         if (lmaster != ctdb->pnn) {
404                 DEBUG(DEBUG_INFO, (__location__ ": not lmaster for record in "
405                                    "delete list (key hash [0x%08x], db[%s]). "
406                                    "Strange! skipping.\n",
407                                    hash, ctdb_db->db_name));
408                 goto skip;
409         }
410
411         /*
412          * Increment the record's RSN to ensure the dmaster (i.e. the current
413          * node) has the highest RSN of the record in the cluster.
414          * This is to prevent old record copies from resurrecting in recoveries
415          * if something should fail during the deletion process.
416          * Note that ctdb_ltdb_store_server() increments the RSN if called
417          * on the record's dmaster.
418          */
419
420         ctdb_data.dptr = tdb_data.dptr + sizeof(struct ctdb_ltdb_header);
421         ctdb_data.dsize = tdb_data.dsize - sizeof(struct ctdb_ltdb_header);
422
423         res = ctdb_ltdb_store(ctdb_db, dd->key, header, ctdb_data);
424         if (res != 0) {
425                 DEBUG(DEBUG_ERR, (__location__ ": Failed to store record with "
426                                   "key hash [0x%08x] on database db[%s].\n",
427                                   hash, ctdb_db->db_name));
428                 goto skip;
429         }
430
431         tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
432
433         goto done;
434
435 skip:
436         tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
437
438         recs->vdata->delete_skipped++;
439         talloc_free(dd);
440         dd = NULL;
441
442 done:
443         if (tdb_data.dptr != NULL) {
444                 free(tdb_data.dptr);
445         }
446
447         if (dd == NULL) {
448                 return 0;
449         }
450
451         return delete_marshall_traverse(param, data);
452 }
453
454 /**
455  * traverse function for the traversal of the delete_queue,
456  * the fast-path vacuuming list.
457  *
458  *  - If the record has been migrated off the node
459  *    or has been revived (filled with data) on the node,
460  *    then skip the record.
461  *
462  *  - If the current node is the record's lmaster and it is
463  *    a record that has never been migrated with data, then
464  *    delete the record from the local tdb.
465  *
466  *  - If the current node is the record's lmaster and it has
467  *    been migrated with data, then schedule it for the normal
468  *    vacuuming procedure (i.e. add it to the delete_list).
469  *
470  *  - If the current node is NOT the record's lmaster then
471  *    add it to the list of records that are to be sent to
472  *    the lmaster with the VACUUM_FETCH message.
473  */
474 static int delete_queue_traverse(void *param, void *data)
475 {
476         struct delete_record_data *dd =
477                 talloc_get_type(data, struct delete_record_data);
478         struct vacuum_data *vdata = talloc_get_type(param, struct vacuum_data);
479         struct ctdb_db_context *ctdb_db = dd->ctdb_db;
480         struct ctdb_context *ctdb = ctdb_db->ctdb; /* or dd->ctdb ??? */
481         int res;
482         struct ctdb_ltdb_header header;
483         uint32_t lmaster;
484         uint32_t hash = ctdb_hash(&(dd->key));
485
486         vdata->fast_total++;
487
488         res = tdb_chainlock(ctdb_db->ltdb->tdb, dd->key);
489         if (res != 0) {
490                 DEBUG(DEBUG_ERR,
491                       (__location__ " Error getting chainlock on record with "
492                        "key hash [0x%08x] on database db[%s].\n",
493                        hash, ctdb_db->db_name));
494                 vdata->fast_error++;
495                 return 0;
496         }
497
498         res = tdb_parse_record(ctdb_db->ltdb->tdb, dd->key,
499                                vacuum_record_parser, &header);
500         if (res != 0) {
501                 goto skipped;
502         }
503
504         if (header.dmaster != ctdb->pnn) {
505                 /* The record has been migrated off the node. Skip. */
506                 goto skipped;
507         }
508
509         if (header.rsn != dd->hdr.rsn) {
510                 /*
511                  * The record has been migrated off the node and back again.
512                  * But not requeued for deletion. Skip it.
513                  */
514                 goto skipped;
515         }
516
517         /*
518          * We are dmaster, and the record has no data, and it has
519          * not been migrated after it has been queued for deletion.
520          *
521          * At this stage, the record could still have been revived locally
522          * and last been written with empty data. This can only be
523          * fixed with the addition of an active or delete flag. (TODO)
524          */
525
526         lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
527
528         if (lmaster != ctdb->pnn) {
529                 res = add_record_to_vacuum_fetch_list(vdata, dd->key);
530
531                 if (res != 0) {
532                         DEBUG(DEBUG_ERR,
533                               (__location__ " Error adding record to list "
534                                "of records to send to lmaster.\n"));
535                         vdata->fast_error++;
536                 } else {
537                         vdata->fast_added_to_vacuum_fetch_list++;
538                 }
539                 goto done;
540         }
541
542         /* use header->flags or dd->hdr.flags ?? */
543         if (dd->hdr.flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA) {
544                 res = add_record_to_delete_list(vdata, dd->key, &dd->hdr);
545
546                 if (res != 0) {
547                         DEBUG(DEBUG_ERR,
548                               (__location__ " Error adding record to list "
549                                "of records for deletion on lmaster.\n"));
550                         vdata->fast_error++;
551                 } else {
552                         vdata->fast_added_to_delete_list++;
553                 }
554         } else {
555                 res = tdb_delete(ctdb_db->ltdb->tdb, dd->key);
556
557                 if (res != 0) {
558                         DEBUG(DEBUG_ERR,
559                               (__location__ " Error deleting record with key "
560                                "hash [0x%08x] from local data base db[%s].\n",
561                                hash, ctdb_db->db_name));
562                         vdata->fast_error++;
563                         goto done;
564                 }
565
566                 DEBUG(DEBUG_DEBUG,
567                       (__location__ " Deleted record with key hash "
568                        "[0x%08x] from local data base db[%s].\n",
569                        hash, ctdb_db->db_name));
570                 vdata->fast_deleted++;
571         }
572
573         goto done;
574
575 skipped:
576         vdata->fast_skipped++;
577
578 done:
579         tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
580
581         return 0;
582 }
583
584 /**
585  * Delete the records that we are lmaster and dmaster for and
586  * that could be deleted on all other nodes via the TRY_DELETE_RECORDS
587  * control.
588  */
589 static int delete_record_traverse(void *param, void *data)
590 {
591         struct delete_record_data *dd =
592                 talloc_get_type(data, struct delete_record_data);
593         struct vacuum_data *vdata = talloc_get_type(param, struct vacuum_data);
594         struct ctdb_db_context *ctdb_db = dd->ctdb_db;
595         struct ctdb_context *ctdb = ctdb_db->ctdb;
596         int res;
597         struct ctdb_ltdb_header *header;
598         TDB_DATA tdb_data;
599         uint32_t lmaster;
600         uint32_t hash = ctdb_hash(&(dd->key));
601
602         res = tdb_chainlock(ctdb_db->ltdb->tdb, dd->key);
603         if (res != 0) {
604                 DEBUG(DEBUG_ERR,
605                       (__location__ " Error getting chainlock on record with "
606                        "key hash [0x%08x] on database db[%s].\n",
607                        hash, ctdb_db->db_name));
608                 vdata->delete_local_error++;
609                 vdata->delete_left--;
610                 talloc_free(dd);
611                 return 0;
612         }
613
614         /*
615          * Verify that the record is still empty, its RSN has not
616          * changed and that we are still its lmaster and dmaster.
617          */
618
619         tdb_data = tdb_fetch(ctdb_db->ltdb->tdb, dd->key);
620         if (tdb_data.dsize < sizeof(struct ctdb_ltdb_header)) {
621                 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
622                                    "on database db[%s] does not exist or is not"
623                                    " a ctdb-record.  skipping.\n",
624                                    hash, ctdb_db->db_name));
625                 goto skip;
626         }
627
628         if (tdb_data.dsize > sizeof(struct ctdb_ltdb_header)) {
629                 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
630                                    "on database db[%s] has been recycled. "
631                                    "skipping.\n",
632                                    hash, ctdb_db->db_name));
633                 goto skip;
634         }
635
636         header = (struct ctdb_ltdb_header *)tdb_data.dptr;
637
638         if (header->flags & CTDB_REC_RO_FLAGS) {
639                 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
640                                    "on database db[%s] has read-only flags. "
641                                    "skipping.\n",
642                                    hash, ctdb_db->db_name));
643                 goto skip;
644         }
645
646         if (header->dmaster != ctdb->pnn) {
647                 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
648                                    "on database db[%s] has been migrated away. "
649                                    "skipping.\n",
650                                    hash, ctdb_db->db_name));
651                 goto skip;
652         }
653
654         if (header->rsn != dd->hdr.rsn + 1) {
655                 /*
656                  * The record has been migrated off the node and back again.
657                  * But not requeued for deletion. Skip it.
658                  * (Note that the first marshall traverse has bumped the RSN
659                  *  on disk.)
660                  */
661                 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
662                                    "on database db[%s] seems to have been "
663                                    "migrated away and back again (with empty "
664                                    "data). skipping.\n",
665                                    hash, ctdb_db->db_name));
666                 goto skip;
667         }
668
669         lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
670
671         if (lmaster != ctdb->pnn) {
672                 DEBUG(DEBUG_INFO, (__location__ ": not lmaster for record in "
673                                    "delete list (key hash [0x%08x], db[%s]). "
674                                    "Strange! skipping.\n",
675                                    hash, ctdb_db->db_name));
676                 goto skip;
677         }
678
679         res = tdb_delete(ctdb_db->ltdb->tdb, dd->key);
680
681         if (res != 0) {
682                 DEBUG(DEBUG_ERR,
683                       (__location__ " Error deleting record with key hash "
684                        "[0x%08x] from local data base db[%s].\n",
685                        hash, ctdb_db->db_name));
686                 vdata->delete_local_error++;
687                 goto done;
688         }
689
690         DEBUG(DEBUG_DEBUG,
691               (__location__ " Deleted record with key hash [0x%08x] from "
692                "local data base db[%s].\n", hash, ctdb_db->db_name));
693
694         vdata->delete_deleted++;
695         goto done;
696
697 skip:
698         vdata->delete_skipped++;
699
700 done:
701         free(tdb_data.dptr);
702
703         tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
704
705         talloc_free(dd);
706         vdata->delete_left--;
707
708         return 0;
709 }
710
711 /**
712  * Fast vacuuming run:
713  * Traverse the delete_queue.
714  * This fills the same lists as the database traverse.
715  */
716 static void ctdb_vacuum_db_fast(struct ctdb_db_context *ctdb_db,
717                                 struct vacuum_data *vdata)
718 {
719         trbt_traversearray32(ctdb_db->delete_queue, 1, delete_queue_traverse, vdata);
720
721         if (vdata->fast_total > 0) {
722                 DEBUG(DEBUG_INFO,
723                       (__location__
724                        " fast vacuuming delete_queue traverse statistics: "
725                        "db[%s] "
726                        "total[%u] "
727                        "del[%u] "
728                        "skp[%u] "
729                        "err[%u] "
730                        "adl[%u] "
731                        "avf[%u]\n",
732                        ctdb_db->db_name,
733                        (unsigned)vdata->fast_total,
734                        (unsigned)vdata->fast_deleted,
735                        (unsigned)vdata->fast_skipped,
736                        (unsigned)vdata->fast_error,
737                        (unsigned)vdata->fast_added_to_delete_list,
738                        (unsigned)vdata->fast_added_to_vacuum_fetch_list));
739         }
740
741         return;
742 }
743
744 /**
745  * Full vacuum run:
746  * read-only traverse of the database, looking for records that
747  * might be able to be vacuumed.
748  *
749  * This is not done each time but only every tunable
750  * VacuumFastPathCount times.
751  */
752 static int ctdb_vacuum_db_full(struct ctdb_db_context *ctdb_db,
753                                struct vacuum_data *vdata,
754                                bool full_vacuum_run)
755 {
756         int ret;
757
758         if (!full_vacuum_run) {
759                 return 0;
760         }
761
762         ret = tdb_traverse_read(ctdb_db->ltdb->tdb, vacuum_traverse, vdata);
763         if (ret == -1 || vdata->traverse_error) {
764                 DEBUG(DEBUG_ERR, (__location__ " Traverse error in vacuuming "
765                                   "'%s'\n", ctdb_db->db_name));
766                 return -1;
767         }
768
769         if (vdata->full_total > 0) {
770                 DEBUG(DEBUG_INFO,
771                       (__location__
772                        " full vacuuming db traverse statistics: "
773                        "db[%s] "
774                        "total[%u] "
775                        "skp[%u] "
776                        "err[%u] "
777                        "adl[%u] "
778                        "avf[%u]\n",
779                        ctdb_db->db_name,
780                        (unsigned)vdata->full_total,
781                        (unsigned)vdata->full_skipped,
782                        (unsigned)vdata->full_error,
783                        (unsigned)vdata->full_added_to_delete_list,
784                        (unsigned)vdata->full_added_to_vacuum_fetch_list));
785         }
786
787         return 0;
788 }
789
790 /**
791  * Process the vacuum fetch lists:
792  * For records for which we are not the lmaster, tell the lmaster to
793  * fetch the record.
794  */
795 static int ctdb_process_vacuum_fetch_lists(struct ctdb_db_context *ctdb_db,
796                                            struct vacuum_data *vdata)
797 {
798         int i;
799         struct ctdb_context *ctdb = ctdb_db->ctdb;
800
801         for (i = 0; i < ctdb->num_nodes; i++) {
802                 TDB_DATA data;
803                 struct ctdb_marshall_buffer *vfl = vdata->vacuum_fetch_list[i];
804
805                 if (ctdb->nodes[i]->pnn == ctdb->pnn) {
806                         continue;
807                 }
808
809                 if (vfl->count == 0) {
810                         continue;
811                 }
812
813                 DEBUG(DEBUG_INFO, ("Found %u records for lmaster %u in '%s'\n",
814                                    vfl->count, ctdb->nodes[i]->pnn,
815                                    ctdb_db->db_name));
816
817                 data.dsize = talloc_get_size(vfl);
818                 data.dptr  = (void *)vfl;
819                 if (ctdb_client_send_message(ctdb, ctdb->nodes[i]->pnn,
820                                              CTDB_SRVID_VACUUM_FETCH,
821                                              data) != 0)
822                 {
823                         DEBUG(DEBUG_ERR, (__location__ " Failed to send vacuum "
824                                           "fetch message to %u\n",
825                                           ctdb->nodes[i]->pnn));
826                         return -1;
827                 }
828         }
829
830         return 0;
831 }
832
833 /**
834  * Process the delete list:
835  *
836  * This is the last step of vacuuming that consistently deletes
837  * those records that have been migrated with data and can hence
838  * not be deleted when leaving a node.
839  *
840  * In this step, the lmaster does the final deletion of those empty
841  * records that it is also dmaster for. It has ususally received
842  * at least some of these records previously from the former dmasters
843  * with the vacuum fetch message.
844  *
845  * This last step is implemented as a 3-phase process to protect from
846  * races leading to data corruption:
847  *
848  *  1) Send the lmaster's copy to all other active nodes with the
849  *     RECEIVE_RECORDS control: The remote nodes store the lmaster's copy.
850  *  2) Send the records that could successfully be stored remotely
851  *     in step #1 to all active nodes with the TRY_DELETE_RECORDS
852  *     control. The remote notes delete their local copy.
853  *  3) The lmaster locally deletes its copies of all records that
854  *     could successfully be deleted remotely in step #2.
855  */
856 static int ctdb_process_delete_list(struct ctdb_db_context *ctdb_db,
857                                     struct vacuum_data *vdata)
858 {
859         int ret, i;
860         struct ctdb_context *ctdb = ctdb_db->ctdb;
861         struct delete_records_list *recs;
862         TDB_DATA indata;
863         struct ctdb_node_map *nodemap;
864         uint32_t *active_nodes;
865         int num_active_nodes;
866         TALLOC_CTX *tmp_ctx;
867
868         if (vdata->delete_count == 0) {
869                 return 0;
870         }
871
872         tmp_ctx = talloc_new(vdata);
873         if (tmp_ctx == NULL) {
874                 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
875                 return 0;
876         }
877
878         vdata->delete_left = vdata->delete_count;
879
880         /*
881          * get the list of currently active nodes
882          */
883
884         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
885                                    CTDB_CURRENT_NODE,
886                                    tmp_ctx,
887                                    &nodemap);
888         if (ret != 0) {
889                 DEBUG(DEBUG_ERR,(__location__ " unable to get node map\n"));
890                 ret = -1;
891                 goto done;
892         }
893
894         active_nodes = list_of_active_nodes(ctdb, nodemap,
895                                             nodemap, /* talloc context */
896                                             false /* include self */);
897         /* yuck! ;-) */
898         num_active_nodes = talloc_get_size(active_nodes)/sizeof(*active_nodes);
899
900         /*
901          * Now delete the records all active nodes in a three-phase process:
902          * 1) send all active remote nodes the current empty copy with this
903          *    node as DMASTER
904          * 2) if all nodes could store the new copy,
905          *    tell all the active remote nodes to delete all their copy
906          * 3) if all remote nodes deleted their record copy, delete it locally
907          */
908
909         /*
910          * Step 1:
911          * Send currently empty record copy to all active nodes for storing.
912          */
913
914         recs = talloc_zero(tmp_ctx, struct delete_records_list);
915         if (recs == NULL) {
916                 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
917                 ret = -1;
918                 goto done;
919         }
920         recs->records = (struct ctdb_marshall_buffer *)
921                 talloc_zero_size(recs,
922                                  offsetof(struct ctdb_marshall_buffer, data));
923         if (recs->records == NULL) {
924                 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
925                 ret = -1;
926                 goto done;
927         }
928         recs->records->db_id = ctdb_db->db_id;
929         recs->vdata = vdata;
930
931         /*
932          * traverse the tree of all records we want to delete and
933          * create a blob we can send to the other nodes.
934          *
935          * We call delete_marshall_traverse_first() to bump the
936          * records' RSNs in the database, to ensure we (as dmaster)
937          * keep the highest RSN of the records in the cluster.
938          */
939         trbt_traversearray32(vdata->delete_list, 1,
940                              delete_marshall_traverse_first, recs);
941
942         indata.dsize = talloc_get_size(recs->records);
943         indata.dptr  = (void *)recs->records;
944
945         for (i = 0; i < num_active_nodes; i++) {
946                 struct ctdb_marshall_buffer *records;
947                 struct ctdb_rec_data *rec;
948                 int32_t res;
949                 TDB_DATA outdata;
950
951                 ret = ctdb_control(ctdb, active_nodes[i], 0,
952                                 CTDB_CONTROL_RECEIVE_RECORDS, 0,
953                                 indata, recs, &outdata, &res,
954                                 NULL, NULL);
955                 if (ret != 0 || res != 0) {
956                         DEBUG(DEBUG_ERR, ("Error storing record copies on "
957                                           "node %u: ret[%d] res[%d]\n",
958                                           active_nodes[i], ret, res));
959                         ret = -1;
960                         goto done;
961                 }
962
963                 /*
964                  * outdata contains the list of records coming back
965                  * from the node: These are the records that the
966                  * remote node could not store. We remove these from
967                  * the list to process further.
968                  */
969                 records = (struct ctdb_marshall_buffer *)outdata.dptr;
970                 rec = (struct ctdb_rec_data *)&records->data[0];
971                 while (records->count-- > 1) {
972                         TDB_DATA reckey, recdata;
973                         struct ctdb_ltdb_header *rechdr;
974                         struct delete_record_data *dd;
975
976                         reckey.dptr = &rec->data[0];
977                         reckey.dsize = rec->keylen;
978                         recdata.dptr = &rec->data[reckey.dsize];
979                         recdata.dsize = rec->datalen;
980
981                         if (recdata.dsize < sizeof(struct ctdb_ltdb_header)) {
982                                 DEBUG(DEBUG_CRIT,(__location__ " bad ltdb record\n"));
983                                 ret = -1;
984                                 goto done;
985                         }
986                         rechdr = (struct ctdb_ltdb_header *)recdata.dptr;
987                         recdata.dptr += sizeof(*rechdr);
988                         recdata.dsize -= sizeof(*rechdr);
989
990                         dd = (struct delete_record_data *)trbt_lookup32(
991                                         vdata->delete_list,
992                                         ctdb_hash(&reckey));
993                         if (dd != NULL) {
994                                 /*
995                                  * The other node could not store the record
996                                  * copy and it is the first node that failed.
997                                  * So we should remove it from the tree and
998                                  * update statistics.
999                                  */
1000                                 talloc_free(dd);
1001                                 vdata->delete_remote_error++;
1002                                 vdata->delete_left--;
1003                         }
1004
1005                         rec = (struct ctdb_rec_data *)(rec->length + (uint8_t *)rec);
1006                 }
1007         }
1008
1009         if (vdata->delete_left == 0) {
1010                 goto success;
1011         }
1012
1013         /*
1014          * Step 2:
1015          * Send the remaining records to all active nodes for deletion.
1016          *
1017          * The lmaster's (i.e. our) copies of these records have been stored
1018          * successfully on the other nodes.
1019          */
1020
1021         /*
1022          * Create a marshall blob from the remaining list of records to delete.
1023          */
1024
1025         talloc_free(recs->records);
1026
1027         recs->records = (struct ctdb_marshall_buffer *)
1028                 talloc_zero_size(recs,
1029                                  offsetof(struct ctdb_marshall_buffer, data));
1030         if (recs->records == NULL) {
1031                 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1032                 ret = -1;
1033                 goto done;
1034         }
1035         recs->records->db_id = ctdb_db->db_id;
1036
1037         trbt_traversearray32(vdata->delete_list, 1,
1038                              delete_marshall_traverse, recs);
1039
1040         indata.dsize = talloc_get_size(recs->records);
1041         indata.dptr  = (void *)recs->records;
1042
1043         for (i = 0; i < num_active_nodes; i++) {
1044                 struct ctdb_marshall_buffer *records;
1045                 struct ctdb_rec_data *rec;
1046                 int32_t res;
1047                 TDB_DATA outdata;
1048
1049                 ret = ctdb_control(ctdb, active_nodes[i], 0,
1050                                 CTDB_CONTROL_TRY_DELETE_RECORDS, 0,
1051                                 indata, recs, &outdata, &res,
1052                                 NULL, NULL);
1053                 if (ret != 0 || res != 0) {
1054                         DEBUG(DEBUG_ERR, ("Failed to delete records on "
1055                                           "node %u: ret[%d] res[%d]\n",
1056                                           active_nodes[i], ret, res));
1057                         ret = -1;
1058                         goto done;
1059                 }
1060
1061                 /*
1062                  * outdata contains the list of records coming back
1063                  * from the node: These are the records that the
1064                  * remote node could not delete. We remove these from
1065                  * the list to delete locally.
1066                  */
1067                 records = (struct ctdb_marshall_buffer *)outdata.dptr;
1068                 rec = (struct ctdb_rec_data *)&records->data[0];
1069                 while (records->count-- > 1) {
1070                         TDB_DATA reckey, recdata;
1071                         struct ctdb_ltdb_header *rechdr;
1072                         struct delete_record_data *dd;
1073
1074                         reckey.dptr = &rec->data[0];
1075                         reckey.dsize = rec->keylen;
1076                         recdata.dptr = &rec->data[reckey.dsize];
1077                         recdata.dsize = rec->datalen;
1078
1079                         if (recdata.dsize < sizeof(struct ctdb_ltdb_header)) {
1080                                 DEBUG(DEBUG_CRIT,(__location__ " bad ltdb record\n"));
1081                                 ret = -1;
1082                                 goto done;
1083                         }
1084                         rechdr = (struct ctdb_ltdb_header *)recdata.dptr;
1085                         recdata.dptr += sizeof(*rechdr);
1086                         recdata.dsize -= sizeof(*rechdr);
1087
1088                         dd = (struct delete_record_data *)trbt_lookup32(
1089                                         vdata->delete_list,
1090                                         ctdb_hash(&reckey));
1091                         if (dd != NULL) {
1092                                 /*
1093                                  * The other node could not delete the
1094                                  * record and it is the first node that
1095                                  * failed. So we should remove it from
1096                                  * the tree and update statistics.
1097                                  */
1098                                 talloc_free(dd);
1099                                 vdata->delete_remote_error++;
1100                                 vdata->delete_left--;
1101                         }
1102
1103                         rec = (struct ctdb_rec_data *)(rec->length + (uint8_t *)rec);
1104                 }
1105         }
1106
1107         if (vdata->delete_left == 0) {
1108                 goto success;
1109         }
1110
1111         /*
1112          * Step 3:
1113          * Delete the remaining records locally.
1114          *
1115          * These records have successfully been deleted on all
1116          * active remote nodes.
1117          */
1118
1119         trbt_traversearray32(vdata->delete_list, 1,
1120                              delete_record_traverse, vdata);
1121
1122 success:
1123
1124         if (vdata->delete_count > 0) {
1125                 DEBUG(DEBUG_INFO,
1126                       (__location__
1127                        " vacuum delete list statistics: "
1128                        "db[%s] "
1129                        "total[%u] "
1130                        "del[%u] "
1131                        "skip[%u] "
1132                        "rem.err[%u] "
1133                        "loc.err[%u] "
1134                        "left[%u]\n",
1135                        ctdb_db->db_name,
1136                        (unsigned)vdata->delete_count,
1137                        (unsigned)vdata->delete_deleted,
1138                        (unsigned)vdata->delete_skipped,
1139                        (unsigned)vdata->delete_remote_error,
1140                        (unsigned)vdata->delete_local_error,
1141                        (unsigned)vdata->delete_left));
1142         }
1143
1144         ret = 0;
1145
1146 done:
1147         talloc_free(tmp_ctx);
1148
1149         return ret;
1150 }
1151
1152 /**
1153  * initialize the vacuum_data
1154  */
1155 static int ctdb_vacuum_init_vacuum_data(struct ctdb_db_context *ctdb_db,
1156                                         struct vacuum_data *vdata)
1157 {
1158         int i;
1159         struct ctdb_context *ctdb = ctdb_db->ctdb;
1160
1161         vdata->fast_added_to_delete_list = 0;
1162         vdata->fast_added_to_vacuum_fetch_list = 0;
1163         vdata->fast_deleted = 0;
1164         vdata->fast_skipped = 0;
1165         vdata->fast_error = 0;
1166         vdata->fast_total = 0;
1167         vdata->full_added_to_delete_list = 0;
1168         vdata->full_added_to_vacuum_fetch_list = 0;
1169         vdata->full_skipped = 0;
1170         vdata->full_error = 0;
1171         vdata->full_total = 0;
1172         vdata->delete_count = 0;
1173         vdata->delete_left = 0;
1174         vdata->delete_remote_error = 0;
1175         vdata->delete_local_error = 0;
1176         vdata->delete_skipped = 0;
1177         vdata->delete_deleted = 0;
1178
1179         /* the list needs to be of length num_nodes */
1180         vdata->vacuum_fetch_list = talloc_zero_array(vdata,
1181                                                 struct ctdb_marshall_buffer *,
1182                                                 ctdb->num_nodes);
1183         if (vdata->vacuum_fetch_list == NULL) {
1184                 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1185                 return -1;
1186         }
1187         for (i = 0; i < ctdb->num_nodes; i++) {
1188                 vdata->vacuum_fetch_list[i] = (struct ctdb_marshall_buffer *)
1189                         talloc_zero_size(vdata->vacuum_fetch_list,
1190                                          offsetof(struct ctdb_marshall_buffer, data));
1191                 if (vdata->vacuum_fetch_list[i] == NULL) {
1192                         DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1193                         return -1;
1194                 }
1195                 vdata->vacuum_fetch_list[i]->db_id = ctdb_db->db_id;
1196         }
1197
1198         return 0;
1199 }
1200
1201 /**
1202  * Vacuum a DB:
1203  *  - Always do the fast vacuuming run, which traverses
1204  *    the in-memory delete queue: these records have been
1205  *    scheduled for deletion.
1206  *  - Only if explicitly requested, the database is traversed
1207  *    in order to use the traditional heuristics on empty records
1208  *    to trigger deletion.
1209  *    This is done only every VacuumFastPathCount'th vacuuming run.
1210  *
1211  * The traverse runs fill two lists:
1212  *
1213  * - The delete_list:
1214  *   This is the list of empty records the current
1215  *   node is lmaster and dmaster for. These records are later
1216  *   deleted first on other nodes and then locally.
1217  *
1218  *   The fast vacuuming run has a short cut for those records
1219  *   that have never been migrated with data: these records
1220  *   are immediately deleted locally, since they have left
1221  *   no trace on other nodes.
1222  *
1223  * - The vacuum_fetch lists
1224  *   (one for each other lmaster node):
1225  *   The records in this list are sent for deletion to
1226  *   their lmaster in a bulk VACUUM_FETCH message.
1227  *
1228  *   The lmaster then migrates all these records to itelf
1229  *   so that they can be vacuumed there.
1230  *
1231  * This executes in the child context.
1232  */
1233 static int ctdb_vacuum_db(struct ctdb_db_context *ctdb_db,
1234                           struct vacuum_data *vdata,
1235                           bool full_vacuum_run)
1236 {
1237         struct ctdb_context *ctdb = ctdb_db->ctdb;
1238         int ret, pnn;
1239
1240         DEBUG(DEBUG_INFO, (__location__ " Entering %s vacuum run for db "
1241                            "%s db_id[0x%08x]\n",
1242                            full_vacuum_run ? "full" : "fast",
1243                            ctdb_db->db_name, ctdb_db->db_id));
1244
1245         ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &ctdb->vnn_map);
1246         if (ret != 0) {
1247                 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from local node\n"));
1248                 return ret;
1249         }
1250
1251         pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1252         if (pnn == -1) {
1253                 DEBUG(DEBUG_ERR, ("Unable to get pnn from local node\n"));
1254                 return -1;
1255         }
1256
1257         ctdb->pnn = pnn;
1258
1259         ret = ctdb_vacuum_init_vacuum_data(ctdb_db, vdata);
1260         if (ret != 0) {
1261                 return ret;
1262         }
1263
1264         ctdb_vacuum_db_fast(ctdb_db, vdata);
1265
1266         ret = ctdb_vacuum_db_full(ctdb_db, vdata, full_vacuum_run);
1267         if (ret != 0) {
1268                 return ret;
1269         }
1270
1271         ret = ctdb_process_vacuum_fetch_lists(ctdb_db, vdata);
1272         if (ret != 0) {
1273                 return ret;
1274         }
1275
1276         ret = ctdb_process_delete_list(ctdb_db, vdata);
1277         if (ret != 0) {
1278                 return ret;
1279         }
1280
1281         /* this ensures we run our event queue */
1282         ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1283
1284         return 0;
1285 }
1286
1287
1288 /*
1289  * traverse function for repacking
1290  */
1291 static int repack_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
1292 {
1293         struct vacuum_data *vdata = (struct vacuum_data *)private;
1294
1295         if (vdata->vacuum) {
1296                 uint32_t hash = ctdb_hash(&key);
1297                 struct delete_record_data *kd;
1298                 /*
1299                  * check if we can ignore this record because it's in the delete_list
1300                  */
1301                 kd = (struct delete_record_data *)trbt_lookup32(vdata->delete_list, hash);
1302                 /*
1303                  * there might be hash collisions so we have to compare the keys here to be sure
1304                  */
1305                 if (kd && kd->key.dsize == key.dsize && memcmp(kd->key.dptr, key.dptr, key.dsize) == 0) {
1306                         struct ctdb_ltdb_header *hdr = (struct ctdb_ltdb_header *)data.dptr;
1307                         /*
1308                          * we have to check if the record hasn't changed in the meantime in order to
1309                          * savely remove it from the database
1310                          */
1311                         if (data.dsize == sizeof(struct ctdb_ltdb_header) &&
1312                                 hdr->dmaster == kd->ctdb->pnn &&
1313                                 ctdb_lmaster(kd->ctdb, &(kd->key)) == kd->ctdb->pnn &&
1314                                 kd->hdr.rsn == hdr->rsn) {
1315                                 vdata->vacuumed++;
1316                                 return 0;
1317                         }
1318                 }
1319         }
1320         if (tdb_store(vdata->dest_db, key, data, TDB_INSERT) != 0) {
1321                 vdata->traverse_error = true;
1322                 return -1;
1323         }
1324         vdata->copied++;
1325         return 0;
1326 }
1327
1328 /*
1329  * repack a tdb
1330  */
1331 static int ctdb_repack_tdb(struct tdb_context *tdb, TALLOC_CTX *mem_ctx, struct vacuum_data *vdata)
1332 {
1333         struct tdb_context *tmp_db;
1334
1335         if (tdb_transaction_start(tdb) != 0) {
1336                 DEBUG(DEBUG_ERR,(__location__ " Failed to start transaction\n"));
1337                 return -1;
1338         }
1339
1340         tmp_db = tdb_open("tmpdb", tdb_hash_size(tdb),
1341                           TDB_INTERNAL|TDB_DISALLOW_NESTING,
1342                           O_RDWR|O_CREAT, 0);
1343         if (tmp_db == NULL) {
1344                 DEBUG(DEBUG_ERR,(__location__ " Failed to create tmp_db\n"));
1345                 tdb_transaction_cancel(tdb);
1346                 return -1;
1347         }
1348
1349         vdata->traverse_error = false;
1350         vdata->dest_db = tmp_db;
1351         vdata->vacuum = true;
1352         vdata->vacuumed = 0;
1353         vdata->copied = 0;
1354
1355         /*
1356          * repack and vacuum on-the-fly by not writing the records that are
1357          * no longer needed
1358          */
1359         if (tdb_traverse_read(tdb, repack_traverse, vdata) == -1) {
1360                 DEBUG(DEBUG_ERR,(__location__ " Failed to traverse copying out\n"));
1361                 tdb_transaction_cancel(tdb);
1362                 tdb_close(tmp_db);
1363                 return -1;              
1364         }
1365
1366         DEBUG(DEBUG_INFO,(__location__ " %u records vacuumed\n", vdata->vacuumed));
1367         
1368         if (vdata->traverse_error) {
1369                 DEBUG(DEBUG_ERR,(__location__ " Error during traversal\n"));
1370                 tdb_transaction_cancel(tdb);
1371                 tdb_close(tmp_db);
1372                 return -1;
1373         }
1374
1375         if (tdb_wipe_all(tdb) != 0) {
1376                 DEBUG(DEBUG_ERR,(__location__ " Failed to wipe database\n"));
1377                 tdb_transaction_cancel(tdb);
1378                 tdb_close(tmp_db);
1379                 return -1;
1380         }
1381
1382         vdata->traverse_error = false;
1383         vdata->dest_db = tdb;
1384         vdata->vacuum = false;
1385         vdata->copied = 0;
1386
1387         if (tdb_traverse_read(tmp_db, repack_traverse, vdata) == -1) {
1388                 DEBUG(DEBUG_ERR,(__location__ " Failed to traverse copying back\n"));
1389                 tdb_transaction_cancel(tdb);
1390                 tdb_close(tmp_db);
1391                 return -1;              
1392         }
1393
1394         if (vdata->traverse_error) {
1395                 DEBUG(DEBUG_ERR,(__location__ " Error during second traversal\n"));
1396                 tdb_transaction_cancel(tdb);
1397                 tdb_close(tmp_db);
1398                 return -1;
1399         }
1400
1401         tdb_close(tmp_db);
1402
1403
1404         if (tdb_transaction_commit(tdb) != 0) {
1405                 DEBUG(DEBUG_ERR,(__location__ " Failed to commit\n"));
1406                 return -1;
1407         }
1408         DEBUG(DEBUG_INFO,(__location__ " %u records copied\n", vdata->copied));
1409
1410         return 0;
1411 }
1412
1413 /*
1414  * repack and vaccum a db
1415  * called from the child context
1416  */
1417 static int ctdb_vacuum_and_repack_db(struct ctdb_db_context *ctdb_db,
1418                                      TALLOC_CTX *mem_ctx,
1419                                      bool full_vacuum_run)
1420 {
1421         uint32_t repack_limit = ctdb_db->ctdb->tunable.repack_limit;
1422         uint32_t vacuum_limit = ctdb_db->ctdb->tunable.vacuum_limit;
1423         const char *name = ctdb_db->db_name;
1424         int freelist_size = 0;
1425         struct vacuum_data *vdata;
1426
1427         vdata = talloc_zero(mem_ctx, struct vacuum_data);
1428         if (vdata == NULL) {
1429                 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1430                 return -1;
1431         }
1432
1433         vdata->ctdb = ctdb_db->ctdb;
1434         vdata->vacuum_limit = vacuum_limit;
1435         vdata->repack_limit = repack_limit;
1436         vdata->delete_list = trbt_create(vdata, 0);
1437         vdata->ctdb_db = ctdb_db;
1438         if (vdata->delete_list == NULL) {
1439                 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1440                 talloc_free(vdata);
1441                 return -1;
1442         }
1443
1444         vdata->start = timeval_current();
1445  
1446         /*
1447          * gather all records that can be deleted in vdata
1448          */
1449         if (ctdb_vacuum_db(ctdb_db, vdata, full_vacuum_run) != 0) {
1450                 DEBUG(DEBUG_ERR,(__location__ " Failed to vacuum '%s'\n", name));
1451         }
1452
1453         if (repack_limit != 0) {
1454                 freelist_size = tdb_freelist_size(ctdb_db->ltdb->tdb);
1455                 if (freelist_size == -1) {
1456                         DEBUG(DEBUG_ERR,(__location__ " Failed to get freelist size for '%s'\n", name));
1457                         talloc_free(vdata);
1458                         return -1;
1459                 }
1460         }
1461
1462         /*
1463          * decide if a repack is necessary
1464          */
1465         if ((repack_limit == 0 || (uint32_t)freelist_size < repack_limit) &&
1466             vdata->delete_left < vacuum_limit)
1467         {
1468                 talloc_free(vdata);
1469                 return 0;
1470         }
1471
1472         DEBUG(DEBUG_INFO,("Repacking %s with %u freelist entries and %u records to delete\n", 
1473                         name, freelist_size, vdata->delete_left));
1474
1475         /*
1476          * repack and implicitely get rid of the records we can delete
1477          */
1478         if (ctdb_repack_tdb(ctdb_db->ltdb->tdb, mem_ctx, vdata) != 0) {
1479                 DEBUG(DEBUG_ERR,(__location__ " Failed to repack '%s'\n", name));
1480                 talloc_free(vdata);
1481                 return -1;
1482         }
1483         talloc_free(vdata);
1484
1485         return 0;
1486 }
1487
1488 static uint32_t get_vacuum_interval(struct ctdb_db_context *ctdb_db)
1489 {
1490         uint32_t interval = ctdb_db->ctdb->tunable.vacuum_interval;
1491
1492         return interval;
1493 }
1494
1495 static int vacuum_child_destructor(struct ctdb_vacuum_child_context *child_ctx)
1496 {
1497         double l = timeval_elapsed(&child_ctx->start_time);
1498         struct ctdb_db_context *ctdb_db = child_ctx->vacuum_handle->ctdb_db;
1499         struct ctdb_context *ctdb = ctdb_db->ctdb;
1500
1501         DEBUG(DEBUG_INFO,("Vacuuming took %.3f seconds for database %s\n", l, ctdb_db->db_name));
1502
1503         if (child_ctx->child_pid != -1) {
1504                 ctdb_kill(ctdb, child_ctx->child_pid, SIGKILL);
1505         } else {
1506                 /* Bump the number of successful fast-path runs. */
1507                 child_ctx->vacuum_handle->fast_path_count++;
1508         }
1509
1510         DLIST_REMOVE(ctdb->vacuumers, child_ctx);
1511
1512         event_add_timed(ctdb->ev, child_ctx->vacuum_handle,
1513                         timeval_current_ofs(get_vacuum_interval(ctdb_db), 0), 
1514                         ctdb_vacuum_event, child_ctx->vacuum_handle);
1515
1516         return 0;
1517 }
1518
1519 /*
1520  * this event is generated when a vacuum child process times out
1521  */
1522 static void vacuum_child_timeout(struct event_context *ev, struct timed_event *te,
1523                                          struct timeval t, void *private_data)
1524 {
1525         struct ctdb_vacuum_child_context *child_ctx = talloc_get_type(private_data, struct ctdb_vacuum_child_context);
1526
1527         DEBUG(DEBUG_ERR,("Vacuuming child process timed out for db %s\n", child_ctx->vacuum_handle->ctdb_db->db_name));
1528
1529         child_ctx->status = VACUUM_TIMEOUT;
1530
1531         talloc_free(child_ctx);
1532 }
1533
1534
1535 /*
1536  * this event is generated when a vacuum child process has completed
1537  */
1538 static void vacuum_child_handler(struct event_context *ev, struct fd_event *fde,
1539                              uint16_t flags, void *private_data)
1540 {
1541         struct ctdb_vacuum_child_context *child_ctx = talloc_get_type(private_data, struct ctdb_vacuum_child_context);
1542         char c = 0;
1543         int ret;
1544
1545         DEBUG(DEBUG_INFO,("Vacuuming child process %d finished for db %s\n", child_ctx->child_pid, child_ctx->vacuum_handle->ctdb_db->db_name));
1546         child_ctx->child_pid = -1;
1547
1548         ret = read(child_ctx->fd[0], &c, 1);
1549         if (ret != 1 || c != 0) {
1550                 child_ctx->status = VACUUM_ERROR;
1551                 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));
1552         } else {
1553                 child_ctx->status = VACUUM_OK;
1554         }
1555
1556         talloc_free(child_ctx);
1557 }
1558
1559 /*
1560  * this event is called every time we need to start a new vacuum process
1561  */
1562 static void
1563 ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
1564                                struct timeval t, void *private_data)
1565 {
1566         struct ctdb_vacuum_handle *vacuum_handle = talloc_get_type(private_data, struct ctdb_vacuum_handle);
1567         struct ctdb_db_context *ctdb_db = vacuum_handle->ctdb_db;
1568         struct ctdb_context *ctdb = ctdb_db->ctdb;
1569         struct ctdb_vacuum_child_context *child_ctx;
1570         struct tevent_fd *fde;
1571         int ret;
1572
1573         /* we dont vacuum if we are in recovery mode, or db frozen */
1574         if (ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ||
1575             ctdb->freeze_mode[ctdb_db->priority] != CTDB_FREEZE_NONE) {
1576                 DEBUG(DEBUG_INFO, ("Not vacuuming %s (%s)\n", ctdb_db->db_name,
1577                                    ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ? "in recovery"
1578                                    : ctdb->freeze_mode[ctdb_db->priority] == CTDB_FREEZE_PENDING
1579                                    ? "freeze pending"
1580                                    : "frozen"));
1581                 event_add_timed(ctdb->ev, vacuum_handle,
1582                         timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1583                         ctdb_vacuum_event, vacuum_handle);
1584                 return;
1585         }
1586
1587         child_ctx = talloc(vacuum_handle, struct ctdb_vacuum_child_context);
1588         if (child_ctx == NULL) {
1589                 DEBUG(DEBUG_CRIT, (__location__ " Failed to allocate child context for vacuuming of %s\n", ctdb_db->db_name));
1590                 ctdb_fatal(ctdb, "Out of memory when crating vacuum child context. Shutting down\n");
1591         }
1592
1593
1594         ret = pipe(child_ctx->fd);
1595         if (ret != 0) {
1596                 talloc_free(child_ctx);
1597                 DEBUG(DEBUG_ERR, ("Failed to create pipe for vacuum child process.\n"));
1598                 event_add_timed(ctdb->ev, vacuum_handle,
1599                         timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1600                         ctdb_vacuum_event, vacuum_handle);
1601                 return;
1602         }
1603
1604         if (vacuum_handle->fast_path_count > ctdb->tunable.vacuum_fast_path_count) {
1605                 vacuum_handle->fast_path_count = 0;
1606         }
1607
1608         child_ctx->child_pid = ctdb_fork(ctdb);
1609         if (child_ctx->child_pid == (pid_t)-1) {
1610                 close(child_ctx->fd[0]);
1611                 close(child_ctx->fd[1]);
1612                 talloc_free(child_ctx);
1613                 DEBUG(DEBUG_ERR, ("Failed to fork vacuum child process.\n"));
1614                 event_add_timed(ctdb->ev, vacuum_handle,
1615                         timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1616                         ctdb_vacuum_event, vacuum_handle);
1617                 return;
1618         }
1619
1620
1621         if (child_ctx->child_pid == 0) {
1622                 char cc = 0;
1623                 bool full_vacuum_run = false;
1624                 close(child_ctx->fd[0]);
1625
1626                 DEBUG(DEBUG_INFO,("Vacuuming child process %d for db %s started\n", getpid(), ctdb_db->db_name));
1627                 ctdb_set_process_name("ctdb_vacuum");
1628                 if (switch_from_server_to_client(ctdb, "vacuum-%s", ctdb_db->db_name) != 0) {
1629                         DEBUG(DEBUG_CRIT, (__location__ "ERROR: failed to switch vacuum daemon into client mode. Shutting down.\n"));
1630                         _exit(1);
1631                 }
1632
1633                 /* 
1634                  * repack the db
1635                  */
1636                 if ((ctdb->tunable.vacuum_fast_path_count > 0) &&
1637                     (vacuum_handle->fast_path_count == 0))
1638                 {
1639                         full_vacuum_run = true;
1640                 }
1641                 cc = ctdb_vacuum_and_repack_db(ctdb_db, child_ctx,
1642                                                full_vacuum_run);
1643
1644                 write(child_ctx->fd[1], &cc, 1);
1645                 _exit(0);
1646         }
1647
1648         set_close_on_exec(child_ctx->fd[0]);
1649         close(child_ctx->fd[1]);
1650
1651         child_ctx->status = VACUUM_RUNNING;
1652         child_ctx->start_time = timeval_current();
1653
1654         DLIST_ADD(ctdb->vacuumers, child_ctx);
1655         talloc_set_destructor(child_ctx, vacuum_child_destructor);
1656
1657         /*
1658          * Clear the fastpath vacuuming list in the parent.
1659          */
1660         talloc_free(ctdb_db->delete_queue);
1661         ctdb_db->delete_queue = trbt_create(ctdb_db, 0);
1662         if (ctdb_db->delete_queue == NULL) {
1663                 /* fatal here? ... */
1664                 ctdb_fatal(ctdb, "Out of memory when re-creating vacuum tree "
1665                                  "in parent context. Shutting down\n");
1666         }
1667
1668         event_add_timed(ctdb->ev, child_ctx,
1669                 timeval_current_ofs(ctdb->tunable.vacuum_max_run_time, 0),
1670                 vacuum_child_timeout, child_ctx);
1671
1672         DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d to child vacuum process\n", child_ctx->fd[0]));
1673
1674         fde = event_add_fd(ctdb->ev, child_ctx, child_ctx->fd[0],
1675                            EVENT_FD_READ, vacuum_child_handler, child_ctx);
1676         tevent_fd_set_auto_close(fde);
1677
1678         vacuum_handle->child_ctx = child_ctx;
1679         child_ctx->vacuum_handle = vacuum_handle;
1680 }
1681
1682 void ctdb_stop_vacuuming(struct ctdb_context *ctdb)
1683 {
1684         /* Simply free them all. */
1685         while (ctdb->vacuumers) {
1686                 DEBUG(DEBUG_INFO, ("Aborting vacuuming for %s (%i)\n",
1687                            ctdb->vacuumers->vacuum_handle->ctdb_db->db_name,
1688                            (int)ctdb->vacuumers->child_pid));
1689                 /* vacuum_child_destructor kills it, removes from list */
1690                 talloc_free(ctdb->vacuumers);
1691         }
1692 }
1693
1694 /* this function initializes the vacuuming context for a database
1695  * starts the vacuuming events
1696  */
1697 int ctdb_vacuum_init(struct ctdb_db_context *ctdb_db)
1698 {
1699         if (ctdb_db->persistent != 0) {
1700                 DEBUG(DEBUG_ERR,("Vacuuming is disabled for persistent database %s\n", ctdb_db->db_name));
1701                 return 0;
1702         }
1703
1704         ctdb_db->vacuum_handle = talloc(ctdb_db, struct ctdb_vacuum_handle);
1705         CTDB_NO_MEMORY(ctdb_db->ctdb, ctdb_db->vacuum_handle);
1706
1707         ctdb_db->vacuum_handle->ctdb_db         = ctdb_db;
1708         ctdb_db->vacuum_handle->fast_path_count = 0;
1709
1710         event_add_timed(ctdb_db->ctdb->ev, ctdb_db->vacuum_handle, 
1711                         timeval_current_ofs(get_vacuum_interval(ctdb_db), 0), 
1712                         ctdb_vacuum_event, ctdb_db->vacuum_handle);
1713
1714         return 0;
1715 }
1716
1717 static void remove_record_from_delete_queue(struct ctdb_db_context *ctdb_db,
1718                                             const struct ctdb_ltdb_header *hdr,
1719                                             const TDB_DATA key)
1720 {
1721         struct delete_record_data *kd;
1722         uint32_t hash;
1723
1724         hash = (uint32_t)ctdb_hash(&key);
1725
1726         DEBUG(DEBUG_DEBUG, (__location__
1727                             " remove_record_from_delete_queue: "
1728                             "db[%s] "
1729                             "db_id[0x%08x] "
1730                             "key_hash[0x%08x] "
1731                             "lmaster[%u] "
1732                             "migrated_with_data[%s]\n",
1733                              ctdb_db->db_name, ctdb_db->db_id,
1734                              hash,
1735                              ctdb_lmaster(ctdb_db->ctdb, &key),
1736                              hdr->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA ? "yes" : "no"));
1737
1738         kd = (struct delete_record_data *)trbt_lookup32(ctdb_db->delete_queue, hash);
1739         if (kd == NULL) {
1740                 DEBUG(DEBUG_DEBUG, (__location__
1741                                     " remove_record_from_delete_queue: "
1742                                     "record not in queue (hash[0x%08x])\n.",
1743                                     hash));
1744                 return;
1745         }
1746
1747         if ((kd->key.dsize != key.dsize) ||
1748             (memcmp(kd->key.dptr, key.dptr, key.dsize) != 0))
1749         {
1750                 DEBUG(DEBUG_DEBUG, (__location__
1751                                     " remove_record_from_delete_queue: "
1752                                     "hash collision for key with hash[0x%08x] "
1753                                     "in db[%s] - skipping\n",
1754                                     hash, ctdb_db->db_name));
1755                 return;
1756         }
1757
1758         DEBUG(DEBUG_DEBUG, (__location__
1759                             " remove_record_from_delete_queue: "
1760                             "removing key with hash[0x%08x]\n",
1761                              hash));
1762
1763         talloc_free(kd);
1764
1765         return;
1766 }
1767
1768 /**
1769  * Insert a record into the ctdb_db context's delete queue,
1770  * handling hash collisions.
1771  */
1772 static int insert_record_into_delete_queue(struct ctdb_db_context *ctdb_db,
1773                                            const struct ctdb_ltdb_header *hdr,
1774                                            TDB_DATA key)
1775 {
1776         struct delete_record_data *kd;
1777         uint32_t hash;
1778         int ret;
1779
1780         hash = (uint32_t)ctdb_hash(&key);
1781
1782         DEBUG(DEBUG_INFO, (__location__ " schedule for deletion: db[%s] "
1783                            "db_id[0x%08x] "
1784                            "key_hash[0x%08x] "
1785                            "lmaster[%u] "
1786                            "migrated_with_data[%s]\n",
1787                             ctdb_db->db_name, ctdb_db->db_id,
1788                             hash,
1789                             ctdb_lmaster(ctdb_db->ctdb, &key),
1790                             hdr->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA ? "yes" : "no"));
1791
1792         kd = (struct delete_record_data *)trbt_lookup32(ctdb_db->delete_queue, hash);
1793         if (kd != NULL) {
1794                 if ((kd->key.dsize != key.dsize) ||
1795                     (memcmp(kd->key.dptr, key.dptr, key.dsize) != 0))
1796                 {
1797                         DEBUG(DEBUG_INFO,
1798                               (__location__ " schedule for deletion: "
1799                                "hash collision for key hash [0x%08x]. "
1800                                "Skipping the record.\n", hash));
1801                         return 0;
1802                 } else {
1803                         DEBUG(DEBUG_DEBUG,
1804                               (__location__ " schedule for deletion: "
1805                                "updating entry for key with hash [0x%08x].\n",
1806                                hash));
1807                 }
1808         }
1809
1810         ret = insert_delete_record_data_into_tree(ctdb_db->ctdb, ctdb_db,
1811                                                   ctdb_db->delete_queue,
1812                                                   hdr, key);
1813         if (ret != 0) {
1814                 DEBUG(DEBUG_INFO,
1815                       (__location__ " schedule for deletion: error "
1816                        "inserting key with hash [0x%08x] into delete queue\n",
1817                        hash));
1818                 return -1;
1819         }
1820
1821         return 0;
1822 }
1823
1824 /**
1825  * Schedule a record for deletetion.
1826  * Called from the parent context.
1827  */
1828 int32_t ctdb_control_schedule_for_deletion(struct ctdb_context *ctdb,
1829                                            TDB_DATA indata)
1830 {
1831         struct ctdb_control_schedule_for_deletion *dd;
1832         struct ctdb_db_context *ctdb_db;
1833         int ret;
1834         TDB_DATA key;
1835
1836         dd = (struct ctdb_control_schedule_for_deletion *)indata.dptr;
1837
1838         ctdb_db = find_ctdb_db(ctdb, dd->db_id);
1839         if (ctdb_db == NULL) {
1840                 DEBUG(DEBUG_ERR, (__location__ " Unknown db id 0x%08x\n",
1841                                   dd->db_id));
1842                 return -1;
1843         }
1844
1845         key.dsize = dd->keylen;
1846         key.dptr = dd->key;
1847
1848         ret = insert_record_into_delete_queue(ctdb_db, &dd->hdr, key);
1849
1850         return ret;
1851 }
1852
1853 int32_t ctdb_local_schedule_for_deletion(struct ctdb_db_context *ctdb_db,
1854                                          const struct ctdb_ltdb_header *hdr,
1855                                          TDB_DATA key)
1856 {
1857         int ret;
1858         struct ctdb_control_schedule_for_deletion *dd;
1859         TDB_DATA indata;
1860         int32_t status;
1861
1862         if (ctdb_db->ctdb->ctdbd_pid == getpid()) {
1863                 /* main daemon - directly queue */
1864                 ret = insert_record_into_delete_queue(ctdb_db, hdr, key);
1865
1866                 return ret;
1867         }
1868
1869         /* if we dont have a connection to the daemon we can not send
1870            a control. For example sometimes from update_record control child
1871            process.
1872         */
1873         if (!ctdb_db->ctdb->can_send_controls) {
1874                 return -1;
1875         }
1876
1877
1878         /* child process: send the main daemon a control */
1879         indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + key.dsize;
1880         indata.dptr = talloc_zero_array(ctdb_db, uint8_t, indata.dsize);
1881         if (indata.dptr == NULL) {
1882                 DEBUG(DEBUG_ERR, (__location__ " out of memory\n"));
1883                 return -1;
1884         }
1885         dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
1886         dd->db_id = ctdb_db->db_id;
1887         dd->hdr = *hdr;
1888         dd->keylen = key.dsize;
1889         memcpy(dd->key, key.dptr, key.dsize);
1890
1891         ret = ctdb_control(ctdb_db->ctdb,
1892                            CTDB_CURRENT_NODE,
1893                            ctdb_db->db_id,
1894                            CTDB_CONTROL_SCHEDULE_FOR_DELETION,
1895                            CTDB_CTRL_FLAG_NOREPLY, /* flags */
1896                            indata,
1897                            NULL, /* mem_ctx */
1898                            NULL, /* outdata */
1899                            &status,
1900                            NULL, /* timeout : NULL == wait forever */
1901                            NULL); /* error message */
1902
1903         talloc_free(indata.dptr);
1904
1905         if (ret != 0 || status != 0) {
1906                 DEBUG(DEBUG_ERR, (__location__ " Error sending "
1907                                   "SCHEDULE_FOR_DELETION "
1908                                   "control.\n"));
1909                 if (status != 0) {
1910                         ret = -1;
1911                 }
1912         }
1913
1914         return ret;
1915 }
1916
1917 void ctdb_local_remove_from_delete_queue(struct ctdb_db_context *ctdb_db,
1918                                          const struct ctdb_ltdb_header *hdr,
1919                                          const TDB_DATA key)
1920 {
1921         if (ctdb_db->ctdb->ctdbd_pid != getpid()) {
1922                 /*
1923                  * Only remove the record from the delete queue if called
1924                  * in the main daemon.
1925                  */
1926                 return;
1927         }
1928
1929         remove_record_from_delete_queue(ctdb_db, hdr, key);
1930
1931         return;
1932 }