make ctdb_ctrl_transaction_active public.
[sahlberg/ctdb.git] / client / ctdb_client.c
index 283b7886cfb25a92b5b1c7aa5de3e54f51674f58..0b8cd5ba2e3e6e94ff0d63c7febb7379cbc073f3 100644 (file)
@@ -1854,9 +1854,6 @@ static int dumpdb_fn(struct ctdb_context *ctdb, TDB_DATA key, TDB_DATA data, voi
        FILE *f = (FILE *)p;
        struct ctdb_ltdb_header *h = (struct ctdb_ltdb_header *)data.dptr;
 
-       fprintf(f, "dmaster: %u\n", h->dmaster);
-       fprintf(f, "rsn: %llu\n", (unsigned long long)h->rsn);
-
        fprintf(f, "key(%u) = \"", (unsigned)key.dsize);
        for (i=0;i<key.dsize;i++) {
                if (ISASCII(key.dptr[i])) {
@@ -1867,6 +1864,9 @@ static int dumpdb_fn(struct ctdb_context *ctdb, TDB_DATA key, TDB_DATA data, voi
        }
        fprintf(f, "\"\n");
 
+       fprintf(f, "dmaster: %u\n", h->dmaster);
+       fprintf(f, "rsn: %llu\n", (unsigned long long)h->rsn);
+
        fprintf(f, "data(%u) = \"", (unsigned)data.dsize);
        for (i=sizeof(*h);i<data.dsize;i++) {
                if (ISASCII(data.dptr[i])) {
@@ -1877,6 +1877,8 @@ static int dumpdb_fn(struct ctdb_context *ctdb, TDB_DATA key, TDB_DATA data, voi
        }
        fprintf(f, "\"\n");
 
+       fprintf(f, "\n");
+
        return 0;
 }
 
@@ -3146,9 +3148,9 @@ int ctdb_ctrl_getcapabilities(struct ctdb_context *ctdb, struct timeval timeout,
 /**
  * check whether a transaction is active on a given db on a given node
  */
-static int32_t ctdb_ctrl_transaction_active(struct ctdb_context *ctdb,
-                                           uint32_t destnode,
-                                           uint32_t db_id)
+int32_t ctdb_ctrl_transaction_active(struct ctdb_context *ctdb,
+                                    uint32_t destnode,
+                                    uint32_t db_id)
 {
        int32_t status;
        int ret;
@@ -3213,25 +3215,29 @@ static int ctdb_transaction_fetch_start(struct ctdb_transaction_handle *h)
        }
 
 again:
+       tmp_ctx = talloc_new(h);
+
+       rh = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, NULL);
+       if (rh == NULL) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to fetch_lock database\n"));
+               talloc_free(tmp_ctx);
+               return -1;
+       }
+
        status = ctdb_ctrl_transaction_active(ctdb_db->ctdb,
                                              CTDB_CURRENT_NODE,
                                              ctdb_db->db_id);
        if (status == 1) {
+               unsigned long int usec = (1000 + random()) % 100000;
                DEBUG(DEBUG_NOTICE, (__location__ " transaction is active "
-                                    "on db_id[%u]. waiting for 1 second\n",
-                                    ctdb_db->db_id));
-               sleep(1);
+                                    "on db_id[0x%08x]. waiting for %lu "
+                                    "microseconds\n",
+                                    ctdb_db->db_id, usec));
+               talloc_free(tmp_ctx);
+               usleep(usec);
                goto again;
        }
 
-       tmp_ctx = talloc_new(h);
-
-       rh = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, NULL);
-       if (rh == NULL) {
-               DEBUG(DEBUG_ERR,(__location__ " Failed to fetch_lock database\n"));             
-               talloc_free(tmp_ctx);
-               return -1;
-       }
        /*
         * store the pid in the database:
         * it is not enough that the node is dmaster...
@@ -3497,6 +3503,9 @@ again:
                           &timeout, NULL);
        if (ret != 0 || status != 0) {
                tdb_transaction_cancel(h->ctdb_db->ltdb->tdb);
+               DEBUG(DEBUG_WARNING, (__location__ " transaction commit%s failed"
+                                     ", retrying after 1 second...\n",
+                                     (retries==0)?"":"retry "));
                sleep(1);
 
                if (ret != 0) {