vacuum: correctly send TRY_DELETE_RECORDS ctrl to all active nodes
[ctdb.git] / server / ctdb_vacuum.c
index dc26d089a495e0f7e76f8c8099188776df454b3c..2c643f72cc3e9e319dba02f2d4e69ced96d6c967 100644 (file)
@@ -124,7 +124,7 @@ static int vacuum_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
                return 0;
        }
 
-       /* is this a records we could possibly delete? I.e.
+       /* Is this a record 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
        */
@@ -238,12 +238,12 @@ static int ctdb_vacuum_db(struct ctdb_db_context *ctdb_db, struct vacuum_data *v
 
        ctdb->pnn = pnn;
        /* 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));
@@ -261,22 +261,24 @@ static int ctdb_vacuum_db(struct ctdb_db_context *ctdb_db, struct vacuum_data *v
                return -1;              
        }
 
-       for ( i = 0; i < ctdb->vnn_map->size; i++) {
+       for (i = 0; i < ctdb->num_nodes; i++) {
                if (vdata->list[i]->count == 0) {
                        continue;
                }
 
                /* for records where we are not the lmaster, tell the lmaster to fetch the record */
-               if (ctdb->vnn_map->map[i] != ctdb->pnn) {
+               if (ctdb->nodes[i]->pnn != ctdb->pnn) {
                        TDB_DATA data;
-                       DEBUG(DEBUG_INFO,("Found %u records for lmaster %u in '%s'\n", 
-                                                               vdata->list[i]->count, i, name));
+                       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->vnn_map->map[i], CTDB_SRVID_VACUUM_FETCH, data) != 0) {
+                       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->vnn_map->map[i]));
+                                        ctdb->nodes[i]->pnn));
                                return -1;              
                        }
                        continue;
@@ -288,6 +290,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) {
@@ -313,24 +318,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;
                        }
 
@@ -367,6 +385,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
@@ -578,7 +599,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;
@@ -702,7 +723,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;
        }
@@ -836,7 +857,7 @@ ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
                return;
        }
 
-       child_ctx->child_pid = fork();
+       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]);