Add a control to read the PNN number of the local node to libctdb
authorRonnie Sahlberg <ronniesahlberg@gmail.com>
Thu, 13 May 2010 00:14:27 +0000 (10:14 +1000)
committerRonnie Sahlberg <ronniesahlberg@gmail.com>
Thu, 13 May 2010 00:14:27 +0000 (10:14 +1000)
include/ctdb.h
libctdb/libctdb.c
libctdb/tst.c

index 12bdb8957f920a1fd36d90f8eb2c0a3f5edc0311..03f2e2641884ae971108a2e575e5aa029fe15f8d 100644 (file)
 #ifndef _CTDB_H
 #define _CTDB_H
 
+/* Functions are not thread safe so all function calls must be wrapped
+ * inside a pthread_mutex for threaded applications.
+ *
+ * All _send() functions are guaranteed to be non-blocking and fully
+ * asynchronous.
+ *
+ * Avoid using the synchronous calls
+ */
+
+/*
+ * Connect to ctdb using the specified domain socket.
+ * Returns a ctdb context if successful or NULL.
+ */
 struct ctdb_context *ctdb_connect(const char *addr);
 
 int ctdb_get_fd(struct ctdb_context *ctdb);
@@ -34,17 +47,15 @@ typedef void ctdb_handle;
 
 
 /*
- * function to cancel a request/call
+ * messaging functions
+ * these functions provide a messaging layer for applications to communicate
+ * with eachother across
  */
-int ctdb_cancel(ctdb_handle *);
-
-
+typedef void (*ctdb_message_fn_t)(struct ctdb_context *, uint64_t srvid, TDB_DATA data, void *);
 
 /*
- * messaging functions 
+ * register a message handler and start listening on a service port
  */
-typedef void (*ctdb_message_fn_t)(struct ctdb_context *, uint64_t srvid, TDB_DATA data, void *);
-
 typedef void (*ctdb_set_message_handler_cb)(int32_t status, void *private_data);
 
 ctdb_handle *
@@ -59,6 +70,10 @@ int ctdb_set_message_handler(struct ctdb_context *ctdb, uint64_t srvid,
                             ctdb_message_fn_t handler, void *private_data);
 
 
+
+/*
+ * unregister a message handler and stop listening on teh specified port
+ */
 typedef void (*ctdb_remove_message_handler_cb)(int32_t status, void *private_data);
 
 ctdb_handle *
@@ -72,20 +87,42 @@ int ctdb_remove_message_handler_recv(struct ctdb_context *ctdb,
 int ctdb_remove_message_handler(struct ctdb_context *ctdb, uint64_t srvid,
                                void *private_data);
 
+/*
+ * send a message to a specific node/port
+ * this function is non-blocking
+ */
 int ctdb_send_message(struct ctdb_context *ctdb, uint32_t pnn, uint64_t srvid, TDB_DATA data);
 
 
 
 
+/*
+ * functions to read the pnn number of the local node
+ */
+typedef void (*ctdb_getpnn_cb)(int32_t status, int32_t pnn, void *private_data);
+
+ctdb_handle *
+ctdb_getpnn_send(struct ctdb_context *ctdb,
+                ctdb_getpnn_cb callback,
+                void *private_data);
+int ctdb_getpnn_recv(struct ctdb_context *ctdb,
+                    ctdb_handle *handle,
+                    uint32_t *pnn);
+int ctdb_getpnn(struct ctdb_context *ctdb,
+               uint32_t *pnn);
+
+
+
+
 /*
  * functions to read the recovery master of a node
  */
-typedef void (*ctdb_get_recmaster_cb)(int32_t status, int32_t recmaster, void *private_data);
+typedef void (*ctdb_getrecmaster_cb)(int32_t status, int32_t recmaster, void *private_data);
 
 ctdb_handle *
 ctdb_getrecmaster_send(struct ctdb_context *ctdb,
                        uint32_t destnode,
-                       ctdb_get_recmaster_cb callback,
+                       ctdb_getrecmaster_cb callback,
                        void *private_data);
 int ctdb_getrecmaster_recv(struct ctdb_context *ctdb,
                        ctdb_handle *handle,
@@ -95,4 +132,13 @@ int ctdb_getrecmaster(struct ctdb_context *ctdb,
                        uint32_t *recmaster);
 
 
+
+
+/*
+ * cancel a request/call
+ */
+int ctdb_cancel(ctdb_handle *);
+
+
+
 #endif
index 47cc7c24d66c81bcb4de1e9e422ce5960ff02aa1..73bbb9f3668ef704cf227a7ed723301cda087509 100644 (file)
@@ -145,23 +145,100 @@ struct ctdb_control_cb_data {
        void *private_data;
 };
 
+
+
+
+/*************************
+ * GET PNN of local node *
+ *************************/
 static void
-ctdb_getrecmaster_recv_cb(struct ctdb_client_control_state *state)
+ctdb_getpnn_recv_cb(struct ctdb_client_control_state *state)
 {
        struct ctdb_control_cb_data *cb_data = state->async.private_data;
-       ctdb_get_recmaster_cb callback = (ctdb_get_recmaster_cb)cb_data->callback;
+       ctdb_getpnn_cb callback = (ctdb_getpnn_cb)cb_data->callback;
 
        callback(0, state->status, cb_data->private_data);
 }
 
+ctdb_handle *
+ctdb_getpnn_send(struct ctdb_context *ctdb,
+                       ctdb_getpnn_cb callback,
+                       void *private_data)
+{
+       struct ctdb_client_control_state *state;
+       struct ctdb_control_cb_data *cb_data;
+
+       state = ctdb_control_send(ctdb, CTDB_CURRENT_NODE, 0, 
+                          CTDB_CONTROL_GET_PNN, 0, tdb_null, 
+                          ctdb, NULL);
+
+       if (state == NULL) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to send GET_PNN control\n"));
+               return NULL;
+       }
+
+       if (callback != NULL) {
+               cb_data = talloc(state, struct ctdb_control_cb_data);
+               cb_data->callback     = callback;
+               cb_data->private_data = private_data;
+
+               state->async.fn           = ctdb_getpnn_recv_cb;
+               state->async.private_data = cb_data;
+       }
+
+       return (ctdb_handle *)state;
+}
+
+int ctdb_getpnn_recv(struct ctdb_context *ctdb, ctdb_handle *handle, uint32_t *pnn)
+{
+       struct ctdb_client_control_state *state = talloc_get_type(handle, struct ctdb_client_control_state);
+       int ret;
+       int32_t res;
+
+       ret = ctdb_control_recv(ctdb, state, state, NULL, &res, NULL);
+       if (ret != 0) {
+               DEBUG(DEBUG_ERR,(__location__ " ctdb_getpnn_recv failed\n"));
+               return -1;
+       }
+
+       if (pnn != NULL) {
+               *pnn = (uint32_t)res;
+       }
+
+       return state->status;
+}
+
+int ctdb_getpnn(struct ctdb_context *ctdb, uint32_t *pnn)
+{
+       struct ctdb_client_control_state *state;
+       
+       state = ctdb_getpnn_send(ctdb, NULL, NULL);
+       if (state == NULL) {
+               DEBUG(DEBUG_ERR,(__location__ " ctdb_getpnn_send() failed.\n"));
+               return -1;
+       }
+
+       return ctdb_getpnn_recv(ctdb, state, pnn);
+}
+
+
+
+/***********************
+ * GET RECOVERY MASTER *
+ ***********************/
+static void
+ctdb_getrecmaster_recv_cb(struct ctdb_client_control_state *state)
+{
+       struct ctdb_control_cb_data *cb_data = state->async.private_data;
+       ctdb_getrecmaster_cb callback = (ctdb_getrecmaster_cb)cb_data->callback;
+
+       callback(0, state->status, cb_data->private_data);
+}
 
-/*
-  get the recovery master of a remote node
- */
 ctdb_handle *
 ctdb_getrecmaster_send(struct ctdb_context *ctdb,
                        uint32_t destnode,
-                       ctdb_get_recmaster_cb callback,
+                       ctdb_getrecmaster_cb callback,
                        void *private_data)
 {
        struct ctdb_client_control_state *state;
@@ -200,7 +277,7 @@ int ctdb_getrecmaster_recv(struct ctdb_context *ctdb, ctdb_handle *handle, uint3
                return -1;
        }
 
-       if (recmaster) {
+       if (recmaster != NULL) {
                *recmaster = (uint32_t)res;
        }
 
@@ -211,7 +288,7 @@ int ctdb_getrecmaster(struct ctdb_context *ctdb, uint32_t destnode, uint32_t *re
 {
        struct ctdb_client_control_state *state;
        
-       state = ctdb_getrecmaster_send(ctdb, destnode, NULL, recmaster);
+       state = ctdb_getrecmaster_send(ctdb, destnode, NULL, NULL);
        if (state == NULL) {
                DEBUG(DEBUG_ERR,(__location__ " ctdb_getrecmaster_send() failed.\n"));
                return -1;
@@ -221,6 +298,15 @@ int ctdb_getrecmaster(struct ctdb_context *ctdb, uint32_t destnode, uint32_t *re
 }
 
 
+
+
+
+
+
+
+
+
+
 static void
 ctdb_set_message_handler_recv_cb(struct ctdb_client_control_state *state)
 {
index 002d77b47530dc6e3ce6dcc380e16a1b60f1f2a4..e47a0eceeeaa7d8f6d00ea3fcd89679dc86c3c95 100644 (file)
@@ -1,7 +1,9 @@
 #include <stdio.h>
 #include <stdint.h>
+#include <stdlib.h>
 #include <poll.h>
 #include <fcntl.h>
+#include <string.h>
 #include "lib/tdb/include/tdb.h"
 #include "include/ctdb.h"
 
@@ -11,9 +13,14 @@ void msg_h(struct ctdb_context *ctdb, uint64_t srvid, TDB_DATA data, void *priva
 }
 
 
+void pnn_cb(int32_t status, int32_t pnn, void *private_data)
+{
+       printf("status:%d pnn:%d\n", status, pnn);
+}
+
 void rm_cb(int32_t status, int32_t recmaster, void *private_data)
 {
-       printf("recmaster:%d\n", recmaster);
+       printf("status:%d recmaster:%d\n", status, recmaster);
 }
 
 int main(int argc, char *argv[])
@@ -27,8 +34,6 @@ int main(int argc, char *argv[])
        ctdb_context = ctdb_connect("/tmp/ctdb.socket");
 
 
-       pfd.fd = ctdb_get_fd(ctdb_context);
-
        handle = ctdb_set_message_handler_send(ctdb_context, 55, NULL, msg_h, NULL);
        if (handle == NULL) {
                printf("Failed to register message port\n");
@@ -49,6 +54,12 @@ int main(int argc, char *argv[])
                exit(10);
        }
 
+       handle = ctdb_getpnn_send(ctdb_context, pnn_cb, NULL);
+       if (handle == NULL) {
+               printf("Failed to send get_pnn control\n");
+               exit(10);
+       }
+
        handle = ctdb_getrecmaster_send(ctdb_context, 0, rm_cb, NULL);
        if (handle == NULL) {
                printf("Failed to send get_recmaster control\n");
@@ -56,8 +67,8 @@ int main(int argc, char *argv[])
        }
 
 
+       pfd.fd = ctdb_get_fd(ctdb_context);
        for (;;) {
-
          pfd.events = ctdb_which_events(ctdb_context);
          if (poll(&pfd, 1, -1) < 0) {
            printf("Poll failed");