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