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