ReadOnly: If record does not exist, upgrade to write-lock
[sahlberg/ctdb.git] / libctdb / ctdb.c
index 141f42f63d9cd747324d84f81ca1f56b38858a54..099ceac0877523720885625940e8c05c9c10d4db 100644 (file)
@@ -2,6 +2,7 @@
    core of libctdb
 
    Copyright (C) Rusty Russell 2010
+   Copyright (C) Ronnie Sahlberg 2011
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -35,6 +36,7 @@
 /* Remove type-safety macros. */
 #undef ctdb_attachdb_send
 #undef ctdb_readrecordlock_async
+#undef ctdb_readonlyrecordlock_async
 #undef ctdb_connect
 
 struct ctdb_lock {
@@ -43,6 +45,9 @@ struct ctdb_lock {
        struct ctdb_db *ctdb_db;
        TDB_DATA key;
 
+       /* Is this a request for read-only lock ? */
+       bool readonly;
+
        /* This will always be set by the time user sees this. */
        unsigned long held_magic;
        struct ctdb_ltdb_header *hdr;
@@ -133,7 +138,7 @@ static void set_pnn(struct ctdb_connection *ctdb,
                      "ctdb_connect(async): failed to get pnn");
                ctdb->broken = true;
        }
-       ctdb_request_free(ctdb, req);
+       ctdb_request_free(req);
 }
 
 struct ctdb_connection *ctdb_connect(const char *addr,
@@ -151,6 +156,7 @@ struct ctdb_connection *ctdb_connect(const char *addr,
                log_fn(log_priv, LOG_ERR, "ctdb_connect: no memory", ap);
                goto fail;
        }
+       ctdb->pnn = -1;
        ctdb->outq = NULL;
        ctdb->doneq = NULL;
        ctdb->in = NULL;
@@ -166,7 +172,7 @@ struct ctdb_connection *ctdb_connect(const char *addr,
        sun.sun_family = AF_UNIX;
        if (!addr)
                addr = CTDB_PATH;
-       strncpy(sun.sun_path, addr, sizeof(sun.sun_path));
+       strncpy(sun.sun_path, addr, sizeof(sun.sun_path)-1);
        ctdb->fd = socket(AF_UNIX, SOCK_STREAM, 0);
        if (ctdb->fd < 0)
                goto free_fail;
@@ -199,12 +205,12 @@ void ctdb_disconnect(struct ctdb_connection *ctdb)
 
        while ((i = ctdb->outq) != NULL) {
                DLIST_REMOVE(ctdb->outq, i);
-               ctdb_request_free(ctdb, i);
+               ctdb_request_free(i);
        }
 
        while ((i = ctdb->doneq) != NULL) {
                DLIST_REMOVE(ctdb->doneq, i);
-               ctdb_request_free(ctdb, i);
+               ctdb_request_free(i);
        }
 
        if (ctdb->in)
@@ -232,7 +238,7 @@ int ctdb_which_events(struct ctdb_connection *ctdb)
        return events;
 }
 
-struct ctdb_request *new_ctdb_request(size_t len,
+struct ctdb_request *new_ctdb_request(struct ctdb_connection *ctdb, size_t len,
                                      ctdb_callback_t cb, void *cbdata)
 {
        struct ctdb_request *req = malloc(sizeof(*req));
@@ -243,6 +249,7 @@ struct ctdb_request *new_ctdb_request(size_t len,
                free(req);
                return NULL;
        }
+       req->ctdb = ctdb;
        req->hdr.hdr = io_elem_data(req->io, NULL);
        req->reply = NULL;
        req->callback = cb;
@@ -252,8 +259,10 @@ struct ctdb_request *new_ctdb_request(size_t len,
        return req;
 }
 
-void ctdb_request_free(struct ctdb_connection *ctdb, struct ctdb_request *req)
+void ctdb_request_free(struct ctdb_request *req)
 {
+       struct ctdb_connection *ctdb = req->ctdb;
+
        if (req->next || req->prev) {
                DEBUG(ctdb, LOG_ALERT,
                      "ctdb_request_free: request not complete! ctdb_cancel? %p (id %u)",
@@ -272,8 +281,7 @@ void ctdb_request_free(struct ctdb_connection *ctdb, struct ctdb_request *req)
 }
 
 /* Sanity-checking wrapper for reply. */
-static struct ctdb_reply_call *unpack_reply_call(struct ctdb_connection *ctdb,
-                                                struct ctdb_request *req,
+static struct ctdb_reply_call *unpack_reply_call(struct ctdb_request *req,
                                                 uint32_t callid)
 {
        size_t len;
@@ -282,7 +290,7 @@ static struct ctdb_reply_call *unpack_reply_call(struct ctdb_connection *ctdb,
        /* Library user error if this isn't a reply to a call. */
        if (req->hdr.hdr->operation != CTDB_REQ_CALL) {
                errno = EINVAL;
-               DEBUG(ctdb, LOG_ALERT,
+               DEBUG(req->ctdb, LOG_ALERT,
                      "This was not a ctdbd call request: operation %u",
                      req->hdr.hdr->operation);
                return NULL;
@@ -290,7 +298,7 @@ static struct ctdb_reply_call *unpack_reply_call(struct ctdb_connection *ctdb,
 
        if (req->hdr.call->callid != callid) {
                errno = EINVAL;
-               DEBUG(ctdb, LOG_ALERT,
+               DEBUG(req->ctdb, LOG_ALERT,
                      "This was not a ctdbd %u call request: %u",
                      callid, req->hdr.call->callid);
                return NULL;
@@ -299,7 +307,7 @@ static struct ctdb_reply_call *unpack_reply_call(struct ctdb_connection *ctdb,
        /* ctdbd or our error if this isn't a reply call. */
        if (len < sizeof(*inhdr) || inhdr->hdr.operation != CTDB_REPLY_CALL) {
                errno = EIO;
-               DEBUG(ctdb, LOG_CRIT,
+               DEBUG(req->ctdb, LOG_CRIT,
                      "Invalid ctdbd call reply: len %zu, operation %u",
                      len, inhdr->hdr.operation);
                return NULL;
@@ -309,8 +317,7 @@ static struct ctdb_reply_call *unpack_reply_call(struct ctdb_connection *ctdb,
 }
 
 /* Sanity-checking wrapper for reply. */
-struct ctdb_reply_control *unpack_reply_control(struct ctdb_connection *ctdb,
-                                               struct ctdb_request *req,
+struct ctdb_reply_control *unpack_reply_control(struct ctdb_request *req,
                                                enum ctdb_controls control)
 {
        size_t len;
@@ -319,13 +326,13 @@ struct ctdb_reply_control *unpack_reply_control(struct ctdb_connection *ctdb,
        /* Library user error if this isn't a reply to a call. */
        if (len < sizeof(*inhdr)) {
                errno = EINVAL;
-               DEBUG(ctdb, LOG_ALERT,
+               DEBUG(req->ctdb, LOG_ALERT,
                      "Short ctdbd control reply: %zu bytes", len);
                return NULL;
        }
        if (req->hdr.hdr->operation != CTDB_REQ_CONTROL) {
                errno = EINVAL;
-               DEBUG(ctdb, LOG_ALERT,
+               DEBUG(req->ctdb, LOG_ALERT,
                      "This was not a ctdbd control request: operation %u",
                      req->hdr.hdr->operation);
                return NULL;
@@ -334,7 +341,7 @@ struct ctdb_reply_control *unpack_reply_control(struct ctdb_connection *ctdb,
        /* ... or if it was a different control from what we expected. */
        if (req->hdr.control->opcode != control) {
                errno = EINVAL;
-               DEBUG(ctdb, LOG_ALERT,
+               DEBUG(req->ctdb, LOG_ALERT,
                      "This was not an opcode %u ctdbd control request: %u",
                      control, req->hdr.control->opcode);
                return NULL;
@@ -343,7 +350,7 @@ struct ctdb_reply_control *unpack_reply_control(struct ctdb_connection *ctdb,
        /* ctdbd or our error if this isn't a reply call. */
        if (inhdr->hdr.operation != CTDB_REPLY_CONTROL) {
                errno = EIO;
-               DEBUG(ctdb, LOG_CRIT,
+               DEBUG(req->ctdb, LOG_CRIT,
                      "Invalid ctdbd control reply: operation %u",
                      inhdr->hdr.operation);
                return NULL;
@@ -509,7 +516,9 @@ struct ctdb_request *new_ctdb_control_request(struct ctdb_connection *ctdb,
        struct ctdb_request *req;
        struct ctdb_req_control *pkt;
 
-       req = new_ctdb_request(offsetof(struct ctdb_req_control, data) + extra, callback, cbdata);
+       req = new_ctdb_request(
+               ctdb, offsetof(struct ctdb_req_control, data) + extra,
+               callback, cbdata);
        if (!req)
                return NULL;
 
@@ -532,7 +541,7 @@ void ctdb_cancel_callback(struct ctdb_connection *ctdb,
                          struct ctdb_request *req,
                          void *unused)
 {
-       ctdb_request_free(ctdb, req);
+       ctdb_request_free(req);
 }
 
 void ctdb_cancel(struct ctdb_connection *ctdb, struct ctdb_request *req)
@@ -541,7 +550,7 @@ void ctdb_cancel(struct ctdb_connection *ctdb, struct ctdb_request *req)
                DEBUG(ctdb, LOG_ALERT,
                      "ctdb_cancel: request completed! ctdb_request_free? %p (id %u)",
                      req, req->hdr.hdr ? req->hdr.hdr->reqid : 0);
-               ctdb_request_free(ctdb, req);
+               ctdb_request_free(req);
                return;
        }
 
@@ -622,7 +631,7 @@ static void destroy_req_db(struct ctdb_connection *ctdb,
        free(req->priv_data);
        /* second request is chained off this one. */
        if (req->extra) {
-               ctdb_request_free(ctdb, req->extra);
+               ctdb_request_free(req->extra);
        }
 }
 
@@ -639,7 +648,7 @@ static void attachdb_done(struct ctdb_connection *ctdb,
                control = CTDB_CONTROL_DB_ATTACH_PERSISTENT;
        }
 
-       reply = unpack_reply_control(ctdb, req, control);
+       reply = unpack_reply_control(req, control);
        if (!reply || reply->status != 0) {
                if (reply) {
                        DEBUG(ctdb, LOG_ERR,
@@ -696,7 +705,7 @@ struct ctdb_db *ctdb_attachdb_recv(struct ctdb_connection *ctdb,
                return NULL;
        }
 
-       reply = unpack_reply_control(ctdb, dbpath_req, CTDB_CONTROL_GETDBPATH);
+       reply = unpack_reply_control(dbpath_req, CTDB_CONTROL_GETDBPATH);
        if (!reply) {
                return NULL;
        }
@@ -780,6 +789,14 @@ static bool try_readrecordlock(struct ctdb_lock *lock, TDB_DATA *data)
        }
 
        hdr = ctdb_local_fetch(lock->ctdb_db->tdb, lock->key, data);
+       if (hdr && lock->readonly && (hdr->flags & CTDB_REC_RO_HAVE_READONLY) ) {
+               DEBUG(lock->ctdb_db->ctdb, LOG_DEBUG,
+                     "ctdb_readrecordlock_async: got local lock for ro");
+               lock->held_magic = lock_magic(lock);
+               lock->hdr = hdr;
+               add_lock(lock->ctdb_db->ctdb, lock);
+               return true;
+       }
        if (hdr && hdr->dmaster == lock->ctdb_db->ctdb->pnn) {
                DEBUG(lock->ctdb_db->ctdb, LOG_DEBUG,
                      "ctdb_readrecordlock_async: got local lock");
@@ -789,6 +806,13 @@ static bool try_readrecordlock(struct ctdb_lock *lock, TDB_DATA *data)
                return true;
        }
 
+       /* we dont have the record locally,
+        * drop to writelock to force a migration
+        */
+       if (!hdr && lock->readonly) {
+               lock->readonly = false;
+       }
+
        tdb_chainunlock(lock->ctdb_db->tdb, lock->key);
        free(hdr);
        return NULL;
@@ -808,16 +832,16 @@ static void readrecordlock_retry(struct ctdb_connection *ctdb,
        struct ctdb_reply_call *reply;
        TDB_DATA data;
 
-       /* OK, we've received reply to noop migration */
-       reply = unpack_reply_call(ctdb, req, CTDB_NULL_FUNC);
+       /* OK, we've received reply to fetch-with-header migration */
+       reply = unpack_reply_call(req, CTDB_FETCH_WITH_HEADER_FUNC);
        if (!reply || reply->status != 0) {
                if (reply) {
                        DEBUG(ctdb, LOG_ERR,
                              "ctdb_readrecordlock_async(async):"
-                             " NULL_FUNC returned %i", reply->status);
+                             " FETCH_WITH_HEADER_FUNC returned %i", reply->status);
                }
                lock->callback(lock->ctdb_db, NULL, tdb_null, private);
-               ctdb_request_free(ctdb, req); /* Also frees lock. */
+               ctdb_request_free(req); /* Also frees lock. */
                return;
        }
 
@@ -826,7 +850,7 @@ static void readrecordlock_retry(struct ctdb_connection *ctdb,
                /* Now it's their responsibility to free lock & request! */
                req->extra_destructor = NULL;
                lock->callback(lock->ctdb_db, lock, data, private);
-               ctdb_request_free(ctdb, req);
+               ctdb_request_free(req);
                return;
        }
 
@@ -835,9 +859,10 @@ static void readrecordlock_retry(struct ctdb_connection *ctdb,
        DLIST_ADD(ctdb->outq, req);
 }
 
-bool
-ctdb_readrecordlock_async(struct ctdb_db *ctdb_db, TDB_DATA key,
-                         ctdb_rrl_callback_t callback, void *cbdata)
+static bool
+ctdb_readrecordlock_internal(struct ctdb_db *ctdb_db, TDB_DATA key,
+                            bool readonly,
+                            ctdb_rrl_callback_t callback, void *cbdata)
 {
        struct ctdb_request *req;
        struct ctdb_lock *lock;
@@ -862,6 +887,7 @@ ctdb_readrecordlock_async(struct ctdb_db *ctdb_db, TDB_DATA key,
        lock->ctdb_db = ctdb_db;
        lock->hdr = NULL;
        lock->held_magic = 0;
+       lock->readonly = readonly;
 
        /* Fast path. */
        if (try_readrecordlock(lock, &data)) {
@@ -870,8 +896,10 @@ ctdb_readrecordlock_async(struct ctdb_db *ctdb_db, TDB_DATA key,
        }
 
        /* Slow path: create request. */
-       req = new_ctdb_request(offsetof(struct ctdb_req_call, data)
-                              + key.dsize, readrecordlock_retry, cbdata);
+       req = new_ctdb_request(
+               ctdb_db->ctdb,
+               offsetof(struct ctdb_req_call, data) + key.dsize,
+               readrecordlock_retry, cbdata);
        if (!req) {
                DEBUG(ctdb_db->ctdb, LOG_ERR,
                      "ctdb_readrecordlock_async: allocation failed");
@@ -886,9 +914,13 @@ ctdb_readrecordlock_async(struct ctdb_db *ctdb_db, TDB_DATA key,
        io_elem_init_req_header(req->io, CTDB_REQ_CALL, CTDB_CURRENT_NODE,
                                new_reqid(ctdb_db->ctdb));
 
-       req->hdr.call->flags = CTDB_IMMEDIATE_MIGRATION;
+       if (lock->readonly) {
+               req->hdr.call->flags = CTDB_WANT_READONLY;
+       } else {
+               req->hdr.call->flags = CTDB_IMMEDIATE_MIGRATION;
+       }
        req->hdr.call->db_id = ctdb_db->id;
-       req->hdr.call->callid = CTDB_NULL_FUNC;
+       req->hdr.call->callid = CTDB_FETCH_WITH_HEADER_FUNC;
        req->hdr.call->hopcount = 0;
        req->hdr.call->keylen = key.dsize;
        req->hdr.call->calldatalen = 0;
@@ -897,6 +929,24 @@ ctdb_readrecordlock_async(struct ctdb_db *ctdb_db, TDB_DATA key,
        return true;
 }
 
+bool
+ctdb_readrecordlock_async(struct ctdb_db *ctdb_db, TDB_DATA key,
+                         ctdb_rrl_callback_t callback, void *cbdata)
+{
+       return ctdb_readrecordlock_internal(ctdb_db, key,
+                       false,
+                       callback, cbdata);
+}
+
+bool
+ctdb_readonlyrecordlock_async(struct ctdb_db *ctdb_db, TDB_DATA key,
+                         ctdb_rrl_callback_t callback, void *cbdata)
+{
+       return ctdb_readrecordlock_internal(ctdb_db, key,
+                       true,
+                       callback, cbdata);
+}
+
 bool ctdb_writerecord(struct ctdb_db *ctdb_db,
                      struct ctdb_lock *lock, TDB_DATA data)
 {
@@ -970,7 +1020,7 @@ static void traverse_remhnd_cb(struct ctdb_connection *ctdb,
                                tdb_null, tdb_null,
                                state->cbdata);
        }
-       ctdb_request_free(ctdb, state->handle);
+       ctdb_request_free(state->handle);
        state->handle = NULL;
        free(state);
 }
@@ -1040,7 +1090,7 @@ static void traverse_start_cb(struct ctdb_connection *ctdb,
 {
        struct ctdb_traverse_state *state = private_data;
 
-        ctdb_request_free(ctdb, state->handle);
+        ctdb_request_free(state->handle);
        state->handle = NULL;
 }
 
@@ -1059,12 +1109,12 @@ static void traverse_msghnd_cb(struct ctdb_connection *ctdb,
                                TRAVERSE_STATUS_ERROR,
                                tdb_null, tdb_null,
                                state->cbdata);
-               ctdb_request_free(ctdb, state->handle);
+               ctdb_request_free(state->handle);
                state->handle = NULL;
                free(state);
                return;
         }
-        ctdb_request_free(ctdb, state->handle);
+        ctdb_request_free(state->handle);
        state->handle = NULL;
 
        t.db_id = ctdb_db->id;
@@ -1128,3 +1178,32 @@ bool ctdb_traverse_async(struct ctdb_db *ctdb_db,
 
        return true;
 }
+
+int ctdb_num_out_queue(struct ctdb_connection *ctdb)
+{
+       struct ctdb_request *req;
+       int i;
+
+       for (i = 0, req = ctdb->outq; req; req = req->next, i++)
+               ;
+
+       return i;
+}
+
+int ctdb_num_in_flight(struct ctdb_connection *ctdb)
+{
+       struct ctdb_request *req;
+       int i;
+
+       for (i = 0, req = ctdb->doneq; req; req = req->next, i++)
+               ;
+
+       return i;
+}
+
+int ctdb_num_active(struct ctdb_connection *ctdb)
+{
+       return ctdb_num_out_queue(ctdb)
+                + ctdb_num_in_flight(ctdb);
+}
+