vacuum: refactor insert_record_into_delete_queue out of ctdb_control_schedule_for_del...
[ctdb.git] / server / ctdb_vacuum.c
index 9ec8b31d968934c6cb54101a4b3b469c578a4f33..e34627cf9f5d1a39155da42f6dfc84b3b57cfb5a 100644 (file)
@@ -36,7 +36,9 @@
 enum vacuum_child_status { VACUUM_RUNNING, VACUUM_OK, VACUUM_ERROR, VACUUM_TIMEOUT};
 
 struct ctdb_vacuum_child_context {
+       struct ctdb_vacuum_child_context *next, *prev;
        struct ctdb_vacuum_handle *vacuum_handle;
+       /* fd child writes status to */
        int fd[2];
        pid_t child_pid;
        enum vacuum_child_status status;
@@ -46,6 +48,7 @@ struct ctdb_vacuum_child_context {
 struct ctdb_vacuum_handle {
        struct ctdb_db_context *ctdb_db;
        struct ctdb_vacuum_child_context *child_ctx;
+       uint32_t fast_path_count;
 };
 
 
@@ -65,6 +68,17 @@ struct vacuum_data {
        uint32_t total;
        uint32_t vacuumed;
        uint32_t copied;
+       uint32_t fast_added_to_vacuum_fetch_list;
+       uint32_t fast_added_to_delete_tree;
+       uint32_t fast_deleted;
+       uint32_t fast_skipped;
+       uint32_t fast_error;
+       uint32_t fast_total;
+       uint32_t full_added_to_vacuum_fetch_list;
+       uint32_t full_added_to_delete_tree;
+       uint32_t full_skipped;
+       uint32_t full_error;
+       uint32_t full_total;
 };
 
 /* tuning information stored for every db */
@@ -89,91 +103,99 @@ struct delete_records_list {
        struct ctdb_marshall_buffer *records;
 };
 
-static void ctdb_vacuum_event(struct event_context *ev, struct timed_event *te, 
-                                                         struct timeval t, void *private_data);
+/**
+ * Store key and header in a tree, indexed by the key hash.
+ */
+static int insert_delete_record_data_into_tree(struct ctdb_context *ctdb,
+                                              struct ctdb_db_context *ctdb_db,
+                                              trbt_tree_t *tree,
+                                              const struct ctdb_ltdb_header *hdr,
+                                              TDB_DATA key)
+{
+       struct delete_record_data *dd;
+       uint32_t hash;
 
+       dd = talloc_zero(tree, struct delete_record_data);
+       if (dd == NULL) {
+               DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
+               return -1;
+       }
 
-/*
- * traverse function for gathering the records that can be deleted
- */
-static int vacuum_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
+       dd->ctdb      = ctdb;
+       dd->ctdb_db   = ctdb_db;
+       dd->key.dsize = key.dsize;
+       dd->key.dptr  = talloc_memdup(dd, key.dptr, key.dsize);
+       if (dd->key.dptr == NULL) {
+               DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
+               return -1;
+       }
+
+       dd->hdr = *hdr;
+
+       hash = ctdb_hash(&key);
+
+       trbt_insert32(tree, hash, dd);
+
+       return 0;
+}
+
+static int add_record_to_delete_tree(struct vacuum_data *vdata, TDB_DATA key,
+                                    struct ctdb_ltdb_header *hdr)
 {
-       struct vacuum_data *vdata = talloc_get_type(private, struct vacuum_data);
        struct ctdb_context *ctdb = vdata->ctdb;
        struct ctdb_db_context *ctdb_db = vdata->ctdb_db;
-       uint32_t lmaster;
-       struct ctdb_ltdb_header *hdr;
-       struct ctdb_rec_data *rec;
-       size_t old_size;
-              
-       lmaster = ctdb_lmaster(ctdb, &key);
-       if (lmaster >= ctdb->vnn_map->size) {
-               return 0;
-       }
+       uint32_t hash;
+       int ret;
 
-       if (data.dsize != sizeof(struct ctdb_ltdb_header)) {
-               /* its not a deleted record */
+       hash = ctdb_hash(&key);
+
+       if (trbt_lookup32(vdata->delete_tree, hash)) {
+               DEBUG(DEBUG_INFO, (__location__ " Hash collission when vacuuming, skipping this record.\n"));
                return 0;
        }
 
-       hdr = (struct ctdb_ltdb_header *)data.dptr;
-
-       if (hdr->dmaster != ctdb->pnn) {
-               return 0;
+       ret = insert_delete_record_data_into_tree(ctdb, ctdb_db,
+                                                 vdata->delete_tree,
+                                                 hdr, key);
+       if (ret != 0) {
+               return -1;
        }
 
-       /* is this a records we could possibly delete? I.e.
-          if the record is empty and also we are both lmaster
-          and dmaster for the record we should be able to delete it
-       */
-       if (lmaster == ctdb->pnn) {
-               uint32_t hash;
-
-               hash = ctdb_hash(&key);
-               if (trbt_lookup32(vdata->delete_tree, hash)) {
-                       DEBUG(DEBUG_DEBUG, (__location__ " Hash collission when vacuuming, skipping this record.\n"));
-               } 
-               else {
-                       struct delete_record_data *dd;
-
-                       /* store key and header indexed by the key hash */
-                       dd = talloc_zero(vdata->delete_tree, struct delete_record_data);
-                       if (dd == NULL) {
-                               DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
-                               return -1;
-                       }
-                       dd->ctdb      = ctdb;
-                       dd->ctdb_db   = ctdb_db;
-                       dd->key.dsize = key.dsize;
-                       dd->key.dptr  = talloc_memdup(dd, key.dptr, key.dsize);
-                       if (dd->key.dptr == NULL) {
-                               DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
-                               return -1;
-                       }
+       vdata->delete_count++;
 
-                       dd->hdr = *hdr;
-       
-                       trbt_insert32(vdata->delete_tree, hash, dd);
+       return 0;
+}
 
-                       vdata->delete_count++;
-               }
-       }
+/**
+ * Add a record to the list of records to be sent
+ * to their lmaster with VACUUM_FETCH.
+ */
+static int add_record_to_vacuum_fetch_list(struct vacuum_data *vdata,
+                                          TDB_DATA key)
+{
+       struct ctdb_context *ctdb = vdata->ctdb;
+       struct ctdb_rec_data *rec;
+       uint32_t lmaster;
+       size_t old_size;
+
+       lmaster = ctdb_lmaster(ctdb, &key);
 
-       /* add the record to the blob ready to send to the nodes */
        rec = ctdb_marshall_record(vdata->list[lmaster], ctdb->pnn, key, NULL, tdb_null);
        if (rec == NULL) {
                DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
                vdata->traverse_error = true;
                return -1;
        }
+
        old_size = talloc_get_size(vdata->list[lmaster]);
-       vdata->list[lmaster] = talloc_realloc_size(NULL, vdata->list[lmaster], 
+       vdata->list[lmaster] = talloc_realloc_size(NULL, vdata->list[lmaster],
                                                   old_size + rec->length);
        if (vdata->list[lmaster] == NULL) {
                DEBUG(DEBUG_ERR,(__location__ " Failed to expand\n"));
                vdata->traverse_error = true;
                return -1;
        }
+
        vdata->list[lmaster]->count++;
        memcpy(old_size+(uint8_t *)vdata->list[lmaster], rec, rec->length);
        talloc_free(rec);
@@ -183,6 +205,76 @@ static int vacuum_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
        return 0;
 }
 
+
+static void ctdb_vacuum_event(struct event_context *ev, struct timed_event *te, 
+                                                         struct timeval t, void *private_data);
+
+
+/*
+ * traverse function for gathering the records that can be deleted
+ */
+static int vacuum_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
+{
+       struct vacuum_data *vdata = talloc_get_type(private, struct vacuum_data);
+       struct ctdb_context *ctdb = vdata->ctdb;
+       uint32_t lmaster;
+       struct ctdb_ltdb_header *hdr;
+       int res = 0;
+
+       vdata->full_total++;
+
+       lmaster = ctdb_lmaster(ctdb, &key);
+       if (lmaster >= ctdb->num_nodes) {
+               vdata->full_error++;
+               DEBUG(DEBUG_CRIT, (__location__
+                                  " lmaster[%u] >= ctdb->num_nodes[%u] for key"
+                                  " with hash[%u]!\n",
+                                  (unsigned)lmaster,
+                                  (unsigned)ctdb->num_nodes,
+                                  (unsigned)ctdb_hash(&key)));
+               return -1;
+       }
+
+       if (data.dsize != sizeof(struct ctdb_ltdb_header)) {
+               /* its not a deleted record */
+               vdata->full_skipped++;
+               return 0;
+       }
+
+       hdr = (struct ctdb_ltdb_header *)data.dptr;
+
+       if (hdr->dmaster != ctdb->pnn) {
+               vdata->full_skipped++;
+               return 0;
+       }
+
+       if (lmaster == ctdb->pnn) {
+               /*
+                * We are both lmaster and dmaster, and the record * is empty.
+                * So we should be able to delete it.
+                */
+               res = add_record_to_delete_tree(vdata, key, hdr);
+               if (res != 0) {
+                       vdata->full_error++;
+               } else {
+                       vdata->full_added_to_delete_tree++;
+               }
+       } else {
+               /*
+                * We are not lmaster.
+                * Add the record to the blob ready to send to the nodes.
+                */
+               res = add_record_to_vacuum_fetch_list(vdata, key);
+               if (res != 0) {
+                       vdata->full_error++;
+               } else {
+                       vdata->full_added_to_vacuum_fetch_list++;
+               }
+       }
+
+       return res;
+}
+
 /*
  * traverse the tree of records to delete and marshall them into
  * a blob
@@ -210,18 +302,157 @@ static void delete_traverse(void *param, void *data)
        memcpy(old_size+(uint8_t *)(recs->records), rec, rec->length);
 }
 
+/**
+ * traverse function for the traversal of the delete_queue,
+ * the fast-path vacuuming list.
+ *
+ *  - If the record has been migrated off the node
+ *    or has been revived (filled with data) on the node,
+ *    then skip the record.
+ *
+ *  - If the current node is the record's lmaster and it is
+ *    a record that has never been migrated with data, then
+ *    delete the record from the local tdb.
+ *
+ *  - If the current node is the record's lmaster and it has
+ *    been migrated with data, then schedule it for the normal
+ *    vacuuming procedure (i.e. add it to the delete_list).
+ *
+ *  - If the current node is NOT the record's lmaster then
+ *    add it to the list of records that are to be sent to
+ *    the lmaster with the VACUUM_FETCH message.
+ */
+static void delete_queue_traverse(void *param, void *data)
+{
+       struct delete_record_data *dd =
+               talloc_get_type(data, struct delete_record_data);
+       struct vacuum_data *vdata = talloc_get_type(param, struct vacuum_data);
+       struct ctdb_db_context *ctdb_db = dd->ctdb_db;
+       struct ctdb_context *ctdb = ctdb_db->ctdb; /* or dd->ctdb ??? */
+       int res;
+       struct ctdb_ltdb_header *header;
+       TDB_DATA tdb_data;
+       uint32_t lmaster;
+
+       vdata->fast_total++;
+
+       res = tdb_chainlock(ctdb_db->ltdb->tdb, dd->key);
+       if (res != 0) {
+               DEBUG(DEBUG_ERR, (__location__ " Error getting chainlock.\n"));
+               vdata->fast_error++;
+               return;
+       }
+
+       tdb_data = tdb_fetch(ctdb_db->ltdb->tdb, dd->key);
+       if (tdb_data.dsize < sizeof(struct ctdb_ltdb_header)) {
+               /* Does not exist or not a ctdb record. Skip. */
+               goto skipped;
+       }
+
+       if (tdb_data.dsize > sizeof(struct ctdb_ltdb_header)) {
+               /* The record has been recycled (filled with data). Skip. */
+               goto skipped;
+       }
+
+       header = (struct ctdb_ltdb_header *)tdb_data.dptr;
+
+       if (header->dmaster != ctdb->pnn) {
+               /* The record has been migrated off the node. Skip. */
+               goto skipped;
+       }
+
+
+       if (header->rsn != dd->hdr.rsn) {
+               /*
+                * The record has been migrated off the node and back again.
+                * But not requeued for deletion. Skip it.
+                */
+               goto skipped;
+       }
+
+       /*
+        * We are dmaster, and the record has no data, and it has
+        * not been migrated after it has been queued for deletion.
+        *
+        * At this stage, the record could still have been revived locally
+        * and last been written with empty data. This can only be
+        * fixed with the addition of an active or delete flag. (TODO)
+        */
+
+       lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
+
+       if (lmaster != ctdb->pnn) {
+               res = add_record_to_vacuum_fetch_list(vdata, dd->key);
+
+               if (res != 0) {
+                       DEBUG(DEBUG_ERR,
+                             (__location__ " Error adding record to list "
+                              "of records to send to lmaster.\n"));
+                       vdata->fast_error++;
+               } else {
+                       vdata->fast_added_to_vacuum_fetch_list++;
+               }
+               goto done;
+       }
+
+       /* use header->flags or dd->hdr.flags ?? */
+       if (dd->hdr.flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA) {
+               res = add_record_to_delete_tree(vdata, dd->key, &dd->hdr);
+
+               if (res != 0) {
+                       DEBUG(DEBUG_ERR,
+                             (__location__ " Error adding record to list "
+                              "of records for deletion on lmaster.\n"));
+                       vdata->fast_error++;
+               } else {
+                       vdata->fast_added_to_delete_tree++;
+               }
+       } else {
+               res = tdb_delete(ctdb_db->ltdb->tdb, dd->key);
+
+               if (res != 0) {
+                       DEBUG(DEBUG_ERR,
+                             (__location__ " Error deleting record from local "
+                              "data base.\n"));
+                       vdata->fast_error++;
+               } else {
+                       vdata->fast_deleted++;
+               }
+       }
+
+       goto done;
+
+skipped:
+       vdata->fast_skipped++;
+
+done:
+       if (tdb_data.dptr != NULL) {
+               free(tdb_data.dptr);
+       }
+       tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
+
+       return;
+}
+
 /* 
  * read-only traverse the database in order to find
  * records that can be deleted and try to delete these
  * records on the other nodes
  * this executes in the child context
  */
-static int ctdb_vacuum_db(struct ctdb_db_context *ctdb_db, struct vacuum_data *vdata)
+static int ctdb_vacuum_db(struct ctdb_db_context *ctdb_db,
+                         struct vacuum_data *vdata,
+                         bool full_vacuum_run)
 {
        struct ctdb_context *ctdb = ctdb_db->ctdb;
        const char *name = ctdb_db->db_name;
        int ret, i, pnn;
 
+       DEBUG(DEBUG_INFO, (__location__ " Entering %s vacuum run for db "
+                          "%s db_id[0x%08x]\n",
+                          full_vacuum_run ? "full" : "fast",
+                          ctdb_db->db_name, ctdb_db->db_id));
+
        ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &ctdb->vnn_map);
        if (ret != 0) {
                DEBUG(DEBUG_ERR, ("Unable to get vnnmap from local node\n"));
@@ -235,13 +466,26 @@ static int ctdb_vacuum_db(struct ctdb_db_context *ctdb_db, struct vacuum_data *v
        }
 
        ctdb->pnn = pnn;
+
+       vdata->fast_added_to_delete_tree = 0;
+       vdata->fast_added_to_vacuum_fetch_list = 0;
+       vdata->fast_deleted = 0;
+       vdata->fast_skipped = 0;
+       vdata->fast_error = 0;
+       vdata->fast_total = 0;
+       vdata->full_added_to_delete_tree = 0;
+       vdata->full_added_to_vacuum_fetch_list = 0;
+       vdata->full_skipped = 0;
+       vdata->full_error = 0;
+       vdata->full_total = 0;
+
        /* the list needs to be of length num_nodes */
-       vdata->list = talloc_array(vdata, struct ctdb_marshall_buffer *, ctdb->vnn_map->size);
+       vdata->list = talloc_array(vdata, struct ctdb_marshall_buffer *, ctdb->num_nodes);
        if (vdata->list == NULL) {
                DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
                return -1;
        }
-       for (i = 0; i < ctdb->vnn_map->size; i++) {
+       for (i = 0; i < ctdb->num_nodes; i++) {
                vdata->list[i] = (struct ctdb_marshall_buffer *)
                        talloc_zero_size(vdata->list, 
                                                         offsetof(struct ctdb_marshall_buffer, data));
@@ -252,33 +496,91 @@ static int ctdb_vacuum_db(struct ctdb_db_context *ctdb_db, struct vacuum_data *v
                vdata->list[i]->db_id = ctdb_db->db_id;
        }
 
-       /* read-only traverse, looking for records that might be able to be vacuumed */
-       if (tdb_traverse_read(ctdb_db->ltdb->tdb, vacuum_traverse, vdata) == -1 ||
-           vdata->traverse_error) {
-               DEBUG(DEBUG_ERR,(__location__ " Traverse error in vacuuming '%s'\n", name));
-               return -1;              
+       /*
+        * Traverse the delete_queue.
+        * This builds the same lists as the db traverse.
+        */
+       trbt_traversearray32(ctdb_db->delete_queue, 1, delete_queue_traverse, vdata);
+
+       if (vdata->fast_total > 0) {
+               DEBUG(DEBUG_INFO,
+                     (__location__
+                      " fast vacuuming delete_queue traverse statistics: "
+                      "db[%s] "
+                      "total[%u] "
+                      "del[%u] "
+                      "skp[%u] "
+                      "err[%u] "
+                      "adt[%u] "
+                      "avf[%u]\n",
+                      ctdb_db->db_name,
+                      (unsigned)vdata->fast_total,
+                      (unsigned)vdata->fast_deleted,
+                      (unsigned)vdata->fast_skipped,
+                      (unsigned)vdata->fast_error,
+                      (unsigned)vdata->fast_added_to_delete_tree,
+                      (unsigned)vdata->fast_added_to_vacuum_fetch_list));
        }
 
-       for ( i = 0; i < ctdb->vnn_map->size; i++) {
-               if (vdata->list[i]->count == 0) {
+       /*
+        * read-only traverse of the database, looking for records that
+        * might be able to be vacuumed.
+        *
+        * This is not done each time but only every tunable
+        * VacuumFastPathCount times.
+        */
+       if (full_vacuum_run) {
+               ret = tdb_traverse_read(ctdb_db->ltdb->tdb, vacuum_traverse, vdata);
+               if (ret == -1 || vdata->traverse_error) {
+                       DEBUG(DEBUG_ERR,(__location__ " Traverse error in vacuuming '%s'\n", name));
+                       return -1;
+               }
+               if (vdata->full_total > 0) {
+                       DEBUG(DEBUG_INFO,
+                             (__location__
+                              " full vacuuming db traverse statistics: "
+                              "db[%s] "
+                              "total[%u] "
+                              "skp[%u] "
+                              "err[%u] "
+                              "adt[%u] "
+                              "avf[%u]\n",
+                              ctdb_db->db_name,
+                              (unsigned)vdata->full_total,
+                              (unsigned)vdata->full_skipped,
+                              (unsigned)vdata->full_error,
+                              (unsigned)vdata->full_added_to_delete_tree,
+                              (unsigned)vdata->full_added_to_vacuum_fetch_list));
+               }
+       }
+
+       /*
+        * For records where we are not the lmaster,
+        * tell the lmaster to fetch the record.
+        */
+       for (i = 0; i < ctdb->num_nodes; i++) {
+               TDB_DATA data;
+
+               if (ctdb->nodes[i]->pnn == ctdb->pnn) {
                        continue;
                }
 
-               /* for records where we are not the lmaster, tell the lmaster to fetch the record */
-               if (ctdb->vnn_map->map[i] != ctdb->pnn) {
-                       TDB_DATA data;
-                       DEBUG(DEBUG_INFO,("Found %u records for lmaster %u in '%s'\n", 
-                                                               vdata->list[i]->count, i, name));
-
-                       data.dsize = talloc_get_size(vdata->list[i]);
-                       data.dptr  = (void *)vdata->list[i];
-                       if (ctdb_client_send_message(ctdb, ctdb->vnn_map->map[i], CTDB_SRVID_VACUUM_FETCH, data) != 0) {
-                               DEBUG(DEBUG_ERR,(__location__ " Failed to send vacuum fetch message to %u\n",
-                                        ctdb->vnn_map->map[i]));
-                               return -1;              
-                       }
+               if (vdata->list[i]->count == 0) {
                        continue;
                }
+
+               DEBUG(DEBUG_INFO, ("Found %u records for lmaster %u in '%s'\n",
+                                  vdata->list[i]->count, ctdb->nodes[i]->pnn,
+                                  name));
+
+               data.dsize = talloc_get_size(vdata->list[i]);
+               data.dptr  = (void *)vdata->list[i];
+               if (ctdb_client_send_message(ctdb, ctdb->nodes[i]->pnn, CTDB_SRVID_VACUUM_FETCH, data) != 0) {
+                       DEBUG(DEBUG_ERR, (__location__ " Failed to send vacuum "
+                                         "fetch message to %u\n",
+                                         ctdb->nodes[i]->pnn));
+                       return -1;
+               }
        }       
 
        /* Process all records we can delete (if any) */
@@ -286,6 +588,9 @@ static int ctdb_vacuum_db(struct ctdb_db_context *ctdb_db, struct vacuum_data *v
                struct delete_records_list *recs;
                TDB_DATA indata, outdata;
                int32_t res;
+               struct ctdb_node_map *nodemap;
+               uint32_t *active_nodes;
+               int num_active_nodes;
 
                recs = talloc_zero(vdata, struct delete_records_list);
                if (recs == NULL) {
@@ -311,24 +616,37 @@ static int ctdb_vacuum_db(struct ctdb_db_context *ctdb_db, struct vacuum_data *v
                indata.dptr  = (void *)recs->records;
 
                /* 
-                * now tell all the other nodes to delete all these records
+                * now tell all the active nodes to delete all these records
                 * (if possible)
                 */
-               for (i = 0; i < ctdb->vnn_map->size; i++) {
+
+               ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
+                                          CTDB_CURRENT_NODE,
+                                          recs, /* talloc context */
+                                          &nodemap);
+               if (ret != 0) {
+                       DEBUG(DEBUG_ERR,(__location__ " unable to get node map\n"));
+                       return -1;
+               }
+
+               active_nodes = list_of_active_nodes(ctdb, nodemap,
+                                                   nodemap, /* talloc context */
+                                                   false /* include self */);
+               /* yuck! ;-) */
+               num_active_nodes = talloc_get_size(active_nodes)/sizeof(*active_nodes);
+
+               for (i = 0; i < num_active_nodes; i++) {
                        struct ctdb_marshall_buffer *records;
                        struct ctdb_rec_data *rec;
 
-                       if (ctdb->vnn_map->map[i] == ctdb->pnn) {
-                               /* we dont delete the records on the local node just yet */
-                               continue;
-                       }
-
-                       ret = ctdb_control(ctdb, ctdb->vnn_map->map[i], 0,
+                       ret = ctdb_control(ctdb, active_nodes[i], 0,
                                        CTDB_CONTROL_TRY_DELETE_RECORDS, 0,
                                        indata, recs, &outdata, &res,
                                        NULL, NULL);
                        if (ret != 0 || res != 0) {
-                               DEBUG(DEBUG_ERR,("Failed to delete records on node %u\n", ctdb->vnn_map->map[i]));
+                               DEBUG(DEBUG_ERR, ("Failed to delete records on "
+                                                 "node %u: ret[%d] res[%d]\n",
+                                                 active_nodes[i], ret, res));
                                return -1;
                        }
 
@@ -365,6 +683,9 @@ static int ctdb_vacuum_db(struct ctdb_db_context *ctdb_db, struct vacuum_data *v
                        }           
                }
 
+               /* free nodemap and active_nodes */
+               talloc_free(nodemap);
+
                /* 
                 * The only records remaining in the tree would be those
                 * records where all other nodes could successfully
@@ -576,7 +897,7 @@ static int update_tuning_db(struct ctdb_db_context *ctdb_db, struct vacuum_data
                }
                tdata.last_interval = tdata.new_interval;
        } else {
-               DEBUG(DEBUG_ERR,(__location__ " Cannot find tunedb record for %s. Using default interval\n", ctdb_db->db_name));
+               DEBUG(DEBUG_DEBUG,(__location__ " Cannot find tunedb record for %s. Using default interval\n", ctdb_db->db_name));
                tdata.last_num_repack = freelist;
                tdata.last_num_empty = vdata->delete_count;
                tdata.last_interval = ctdb_db->ctdb->tunable.vacuum_default_interval;
@@ -610,7 +931,9 @@ static int update_tuning_db(struct ctdb_db_context *ctdb_db, struct vacuum_data
  * repack and vaccum a db
  * called from the child context
  */
-static int ctdb_repack_db(struct ctdb_db_context *ctdb_db, TALLOC_CTX *mem_ctx)
+static int ctdb_vacuum_and_repack_db(struct ctdb_db_context *ctdb_db,
+                                    TALLOC_CTX *mem_ctx,
+                                    bool full_vacuum_run)
 {
        uint32_t repack_limit = ctdb_db->ctdb->tunable.repack_limit;
        uint32_t vacuum_limit = ctdb_db->ctdb->tunable.vacuum_limit;
@@ -634,6 +957,7 @@ static int ctdb_repack_db(struct ctdb_db_context *ctdb_db, TALLOC_CTX *mem_ctx)
        vdata->vacuum_limit = vacuum_limit;
        vdata->repack_limit = repack_limit;
        vdata->delete_tree = trbt_create(vdata, 0);
+       vdata->ctdb_db = ctdb_db;
        if (vdata->delete_tree == NULL) {
                DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
                talloc_free(vdata);
@@ -645,7 +969,7 @@ static int ctdb_repack_db(struct ctdb_db_context *ctdb_db, TALLOC_CTX *mem_ctx)
        /*
         * gather all records that can be deleted in vdata
         */
-       if (ctdb_vacuum_db(ctdb_db, vdata) != 0) {
+       if (ctdb_vacuum_db(ctdb_db, vdata, full_vacuum_run) != 0) {
                DEBUG(DEBUG_ERR,(__location__ " Failed to vacuum '%s'\n", name));
        }
 
@@ -699,7 +1023,7 @@ static int get_vacuum_interval(struct ctdb_db_context *ctdb_db)
                       flags,
                       O_RDWR|O_CREAT, 0600);
        if (!tdb) {
-               DEBUG(DEBUG_ERR,("Unable to open/create database %s using default interval\n", vac_dbname));
+               DEBUG(DEBUG_ERR,("Unable to open/create database %s using default interval. Errno : %s (%d)\n", vac_dbname, strerror(errno), errno));
                talloc_free(tmp_ctx);
                return interval;
        }
@@ -741,8 +1065,13 @@ static int vacuum_child_destructor(struct ctdb_vacuum_child_context *child_ctx)
 
        if (child_ctx->child_pid != -1) {
                kill(child_ctx->child_pid, SIGKILL);
+       } else {
+               /* Bump the number of successful fast-path runs. */
+               child_ctx->vacuum_handle->fast_path_count++;
        }
 
+       DLIST_REMOVE(ctdb->vacuumers, child_ctx);
+
        event_add_timed(ctdb->ev, child_ctx->vacuum_handle,
                        timeval_current_ofs(get_vacuum_interval(ctdb_db), 0), 
                        ctdb_vacuum_event, child_ctx->vacuum_handle);
@@ -804,8 +1133,14 @@ ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
        struct tevent_fd *fde;
        int ret;
 
-       /* we dont vacuum if we are in recovery mode */
-       if (ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE) {
+       /* we dont vacuum if we are in recovery mode, or db frozen */
+       if (ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ||
+           ctdb->freeze_mode[ctdb_db->priority] != CTDB_FREEZE_NONE) {
+               DEBUG(DEBUG_INFO, ("Not vacuuming %s (%s)\n", ctdb_db->db_name,
+                                  ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ? "in recovery"
+                                  : ctdb->freeze_mode[ctdb_db->priority] == CTDB_FREEZE_PENDING
+                                  ? "freeze pending"
+                                  : "frozen"));
                event_add_timed(ctdb->ev, vacuum_handle, timeval_current_ofs(ctdb->tunable.vacuum_default_interval, 0), ctdb_vacuum_event, vacuum_handle);
                return;
        }
@@ -825,7 +1160,11 @@ ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
                return;
        }
 
-       child_ctx->child_pid = fork();
+       if (vacuum_handle->fast_path_count > ctdb->tunable.vacuum_fast_path_count) {
+               vacuum_handle->fast_path_count = 0;
+       }
+
+       child_ctx->child_pid = ctdb_fork(ctdb);
        if (child_ctx->child_pid == (pid_t)-1) {
                close(child_ctx->fd[0]);
                close(child_ctx->fd[1]);
@@ -838,11 +1177,12 @@ ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
 
        if (child_ctx->child_pid == 0) {
                char cc = 0;
+               bool full_vacuum_run = false;
                close(child_ctx->fd[0]);
 
                DEBUG(DEBUG_INFO,("Vacuuming child process %d for db %s started\n", getpid(), ctdb_db->db_name));
        
-               if (switch_from_server_to_client(ctdb) != 0) {
+               if (switch_from_server_to_client(ctdb, "vacuum-%s", ctdb_db->db_name) != 0) {
                        DEBUG(DEBUG_CRIT, (__location__ "ERROR: failed to switch vacuum daemon into client mode. Shutting down.\n"));
                        _exit(1);
                }
@@ -850,7 +1190,13 @@ ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
                /* 
                 * repack the db
                 */
-               cc = ctdb_repack_db(ctdb_db, child_ctx);
+               if ((ctdb->tunable.vacuum_fast_path_count > 0) &&
+                   (vacuum_handle->fast_path_count == 0))
+               {
+                       full_vacuum_run = true;
+               }
+               cc = ctdb_vacuum_and_repack_db(ctdb_db, child_ctx,
+                                              full_vacuum_run);
 
                write(child_ctx->fd[1], &cc, 1);
                _exit(0);
@@ -862,8 +1208,20 @@ ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
        child_ctx->status = VACUUM_RUNNING;
        child_ctx->start_time = timeval_current();
 
+       DLIST_ADD(ctdb->vacuumers, child_ctx);
        talloc_set_destructor(child_ctx, vacuum_child_destructor);
 
+       /*
+        * Clear the fastpath vacuuming list in the parent.
+        */
+       talloc_free(ctdb_db->delete_queue);
+       ctdb_db->delete_queue = trbt_create(ctdb_db, 0);
+       if (ctdb_db->delete_queue == NULL) {
+               /* fatal here? ... */
+               ctdb_fatal(ctdb, "Out of memory when re-creating vacuum tree "
+                                "in parent context. Shutting down\n");
+       }
+
        event_add_timed(ctdb->ev, child_ctx,
                timeval_current_ofs(ctdb->tunable.vacuum_max_run_time, 0),
                vacuum_child_timeout, child_ctx);
@@ -878,6 +1236,17 @@ ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
        child_ctx->vacuum_handle = vacuum_handle;
 }
 
+void ctdb_stop_vacuuming(struct ctdb_context *ctdb)
+{
+       /* Simply free them all. */
+       while (ctdb->vacuumers) {
+               DEBUG(DEBUG_INFO, ("Aborting vacuuming for %s (%i)\n",
+                          ctdb->vacuumers->vacuum_handle->ctdb_db->db_name,
+                          (int)ctdb->vacuumers->child_pid));
+               /* vacuum_child_destructor kills it, removes from list */
+               talloc_free(ctdb->vacuumers);
+       }
+}
 
 /* this function initializes the vacuuming context for a database
  * starts the vacuuming events
@@ -900,3 +1269,143 @@ int ctdb_vacuum_init(struct ctdb_db_context *ctdb_db)
 
        return 0;
 }
+
+/**
+ * Insert a record into the ctdb_db context's delete queue,
+ * handling hash collisions.
+ */
+static int insert_record_into_delete_queue(struct ctdb_db_context *ctdb_db,
+                                          const struct ctdb_ltdb_header *hdr,
+                                          TDB_DATA key)
+{
+       struct delete_record_data *kd;
+       uint32_t hash;
+       int ret;
+
+       hash = (uint32_t)ctdb_hash(&key);
+
+       DEBUG(DEBUG_INFO, (__location__ " Schedule for deletion: db[%s] "
+                          "db_id[0x%08x] "
+                          "key_hash[0x%08x] "
+                          "lmaster[%u] "
+                          "migrated_with_data[%s]\n",
+                           ctdb_db->db_name, ctdb_db->db_id,
+                           hash,
+                           ctdb_lmaster(ctdb_db->ctdb, &key),
+                           hdr->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA ? "yes" : "no"));
+
+       kd = (struct delete_record_data *)trbt_lookup32(ctdb_db->delete_queue, hash);
+       if (kd != NULL) {
+               if ((kd->key.dsize != key.dsize) ||
+                   (memcmp(kd->key.dptr, key.dptr, key.dsize) != 0))
+               {
+                       DEBUG(DEBUG_INFO,
+                             ("schedule for deletion: Hash collision (0x%08x)."
+                              " Skipping the record.\n", hash));
+                       return 0;
+               } else {
+                       DEBUG(DEBUG_DEBUG,
+                             ("schedule for deletetion: Overwriting entry for "
+                              "key with hash 0x%08x.\n", hash));
+               }
+       }
+
+       ret = insert_delete_record_data_into_tree(ctdb_db->ctdb, ctdb_db,
+                                                 ctdb_db->delete_queue,
+                                                 hdr, key);
+       if (ret != 0) {
+               return -1;
+       }
+
+       return 0;
+}
+
+/**
+ * Schedule a record for deletetion.
+ * Called from the parent context.
+ */
+int32_t ctdb_control_schedule_for_deletion(struct ctdb_context *ctdb,
+                                          TDB_DATA indata)
+{
+       struct ctdb_control_schedule_for_deletion *dd;
+       struct ctdb_db_context *ctdb_db;
+       int ret;
+       TDB_DATA key;
+
+       dd = (struct ctdb_control_schedule_for_deletion *)indata.dptr;
+
+       ctdb_db = find_ctdb_db(ctdb, dd->db_id);
+       if (ctdb_db == NULL) {
+               DEBUG(DEBUG_ERR, (__location__ " Unknown db id 0x%08x\n",
+                                 dd->db_id));
+               return -1;
+       }
+
+       key.dsize = dd->keylen;
+       key.dptr = dd->key;
+
+       ret = insert_record_into_delete_queue(ctdb_db, &dd->hdr, key);
+
+       return ret;
+}
+
+int32_t ctdb_local_schedule_for_deletion(struct ctdb_db_context *ctdb_db,
+                                        const struct ctdb_ltdb_header *hdr,
+                                        TDB_DATA key)
+{
+       int ret;
+       struct ctdb_control_schedule_for_deletion *dd;
+       TDB_DATA indata;
+       int32_t status;
+
+       if (ctdb_db->ctdb->ctdbd_pid == getpid()) {
+               /* main daemon - directly queue */
+               ret = insert_delete_record_data_into_tree(ctdb_db->ctdb,
+                                                         ctdb_db,
+                                                         ctdb_db->delete_queue,
+                                                         hdr, key);
+               if (ret != 0) {
+                       return -1;
+               }
+               return 0;
+       }
+
+       /* child process: send the main daemon a control */
+
+       indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + key.dsize;
+       indata.dptr = talloc_zero_array(ctdb_db, uint8_t, indata.dsize);
+       if (indata.dptr == NULL) {
+               DEBUG(DEBUG_ERR, (__location__ " out of memory\n"));
+               return -1;
+       }
+       dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
+       dd->db_id = ctdb_db->db_id;
+       dd->hdr = *hdr;
+       dd->keylen = key.dsize;
+       memcpy(dd->key, key.dptr, key.dsize);
+
+       ret = ctdb_control(ctdb_db->ctdb,
+                          CTDB_CURRENT_NODE,
+                          ctdb_db->db_id,
+                          CTDB_CONTROL_SCHEDULE_FOR_DELETION,
+                          CTDB_CTRL_FLAG_NOREPLY, /* flags */
+                          indata,
+                          NULL, /* mem_ctx */
+                          NULL, /* outdata */
+                          &status,
+                          NULL, /* timeout : NULL == wait forever */
+                          NULL); /* error message */
+
+       talloc_free(indata.dptr);
+
+       if (ret != 0 || status != 0) {
+               DEBUG(DEBUG_ERR, (__location__ " Error sending "
+                                 "SCHEDULE_FOR_DELETION "
+                                 "control.\n"));
+               if (status != 0) {
+                       ret = -1;
+               }
+       }
+
+       return ret;
+}