From: Ronnie Sahlberg Date: Tue, 18 May 2010 01:24:39 +0000 (+1000) Subject: make the getrecovery master control use the new generic set_callback X-Git-Url: http://git.samba.org/?p=sahlberg%2Fctdb.git;a=commitdiff_plain;h=e9418d95052490b0d8de98cb4c37b74f25ea8842 make the getrecovery master control use the new generic set_callback api and remove the callback argument from the _send() signature Update the example tst.c function to show the three different modes to talk to ctdb --- diff --git a/include/ctdb.h b/include/ctdb.h index 980e8557..86886e5c 100644 --- a/include/ctdb.h +++ b/include/ctdb.h @@ -242,9 +242,7 @@ typedef void (*ctdb_getrecmaster_cb)(int32_t status, int32_t recmaster, void *pr ctdb_handle * ctdb_getrecmaster_send(struct ctdb_context *ctdb, - uint32_t destnode, - ctdb_getrecmaster_cb callback, - void *private_data); + uint32_t destnode); int ctdb_getrecmaster_recv(struct ctdb_context *ctdb, ctdb_handle *handle, uint32_t *recmaster); diff --git a/libctdb/libctdb.c b/libctdb/libctdb.c index a17ad143..e4a5ea6e 100644 --- a/libctdb/libctdb.c +++ b/libctdb/libctdb.c @@ -292,29 +292,11 @@ int ctdb_getpnn(struct ctdb_context *ctdb, uint32_t destnode, uint32_t *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; - - if (state->state != CTDB_CONTROL_DONE) { - DEBUG(DEBUG_ERR, (__location__ " ctdb_getrecmaster_recv_cb failed with state:%d\n", state->state)); - callback(-1, 0, cb_data->private_data); - return; - } - - callback(0, state->status, cb_data->private_data); -} - ctdb_handle * ctdb_getrecmaster_send(struct ctdb_context *ctdb, - uint32_t destnode, - ctdb_getrecmaster_cb callback, - void *private_data) + uint32_t destnode) { struct ctdb_client_control_state *state; - struct ctdb_control_cb_data *cb_data; state = ctdb_control_send(ctdb, destnode, 0, CTDB_CONTROL_GET_RECMASTER, 0, tdb_null, @@ -325,15 +307,6 @@ ctdb_getrecmaster_send(struct ctdb_context *ctdb, 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_getrecmaster_recv_cb; - state->async.private_data = cb_data; - } - return (ctdb_handle *)state; } @@ -341,16 +314,15 @@ int ctdb_getrecmaster_recv(struct ctdb_context *ctdb, ctdb_handle *handle, uint3 { 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); + ret = ctdb_control_recv(ctdb, state, state, NULL, NULL, NULL); if (ret != 0) { DEBUG(DEBUG_ERR,(__location__ " ctdb_getrecmaster_recv failed\n")); return -1; } if (recmaster != NULL) { - *recmaster = (uint32_t)res; + *recmaster = (uint32_t)state->status; } return 0; @@ -360,7 +332,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, NULL); + state = ctdb_getrecmaster_send(ctdb, destnode); if (state == NULL) { DEBUG(DEBUG_ERR,(__location__ " ctdb_getrecmaster_send() failed.\n")); return -1; diff --git a/libctdb/tst.c b/libctdb/tst.c index f8e8f7f7..d3db1330 100644 --- a/libctdb/tst.c +++ b/libctdb/tst.c @@ -34,11 +34,26 @@ void pnn_cb(int32_t status, struct ctdb_context *ctdb, ctdb_handle *handle, void printf("status:%d pnn:%d\n", status, pnn); } -void rm_cb(int32_t status, int32_t recmaster, void *private_data) +void rm_cb(int32_t status, struct ctdb_context *ctdb, ctdb_handle *handle, void *private_data) { - printf("status:%d recmaster:%d\n", status, recmaster); + uint32_t rm; + int ret; + + if (status != 0) { + printf("Error reading RECMASTER\n"); + return; + } + + ret = ctdb_getrecmaster_recv(ctdb, handle, &rm); + if (ret != 0) { + printf("Failed to read getpnn reply\n"); + return; + } + + printf("GETRECMASTER ASYNC: status:%d recmaster:%d\n", status, rm); } + /* * example on how to first read(non-existing recortds are implicitely created * on demand) a record and change it in the callback. @@ -86,6 +101,7 @@ int main(int argc, char *argv[]) ctdb_handle *handle; struct pollfd pfd; int ret; + uint32_t recmaster; TDB_DATA msg; key.dptr = "Test Record"; @@ -126,6 +142,55 @@ int main(int argc, char *argv[]) + /* + * ASYNC call with callback to read the recmaster + * this is the preferred way to use libctdb + */ + handle = ctdb_getrecmaster_send(ctdb, CTDB_CURRENT_NODE); + if (handle == NULL) { + printf("Failed to send get_recmaster control\n"); + exit(10); + } + ret = ctdb_set_callback(handle, rm_cb, NULL); + if (ret != 0) { + printf("Failed to set callback for get_recmaster\n"); + ctdb_free(handle); + exit(10); + } + + /* + * SEMI-SYNC call with callback to read the recmaster + * calls the blocking *_recv() function. + * Avoid this mode for performance critical tasks + */ + handle = ctdb_getrecmaster_send(ctdb, CTDB_CURRENT_NODE); + if (handle == NULL) { + printf("Failed to send get_recmaster control\n"); + exit(10); + } + ret = ctdb_getrecmaster_recv(ctdb, handle, &recmaster); + if (ret != 0) { + printf("Failed to receive response to getrecmaster\n"); + ctdb_free(handle); + exit(10); + } + printf("GETRECMASTER SEMI-SYNC: status:%d recmaster:%d\n", ret, recmaster); + + + /* + * SYNC call with callback to read the recmaster + * calls the blocking sync function. + * Avoid this mode for performance critical tasks + */ + ret = ctdb_getrecmaster(ctdb, CTDB_CURRENT_NODE, &recmaster); + if (ret != 0) { + printf("Failed to receive response to getrecmaster\n"); + exit(10); + } + printf("GETRECMASTER SYNC: status:%d recmaster:%d\n", ret, recmaster); + + + handle = ctdb_getpnn_send(ctdb, CTDB_CURRENT_NODE); if (handle == NULL) { @@ -140,8 +205,6 @@ int main(int argc, char *argv[]) } - - handle = ctdb_readrecordlock_send(ctdb, ctdb_db_context, key, rrl_cb, NULL); if (handle == NULL) { printf("Failed to send READRECORDLOCK\n");