From: Sree Harsha Totakura Date: Wed, 20 Jun 2012 10:04:01 +0000 (+0000) Subject: -changes required for acquire retry X-Git-Tag: initial-import-from-subversion-38251~12948 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=8bd22bae6f169bf30b13c4565cfcf66e0ed45c1c;p=oweals%2Fgnunet.git -changes required for acquire retry --- diff --git a/src/lockmanager/Makefile.am b/src/lockmanager/Makefile.am index 056b8cc08..9a672cbef 100644 --- a/src/lockmanager/Makefile.am +++ b/src/lockmanager/Makefile.am @@ -39,9 +39,9 @@ libgnunetlockmanager_la_LDFLAGS = \ -version-info 0:0:0 check_PROGRAMS = \ - test-lockmanager-api \ - test-lockmanager-api-lockrelease \ - test-lockmanager-api-servercrash + test_lockmanager_api \ + test_lockmanager_api_lockrelease \ + test_lockmanager_api_servercrash EXTRA_DIST = \ test_lockmanager_api.conf diff --git a/src/lockmanager/lockmanager_api.c b/src/lockmanager/lockmanager_api.c index bfc4d1c1a..07e0220dd 100644 --- a/src/lockmanager/lockmanager_api.c +++ b/src/lockmanager/lockmanager_api.c @@ -100,6 +100,11 @@ struct GNUNET_LOCKMANAGER_Handle * Double linked list tail for message queue */ struct MessageQueue *mq_tail; + + /** + * Are we currently handling replies? + */ + int in_replies; }; @@ -162,6 +167,17 @@ struct LockingRequestMatch }; +/** + * Handler for server replies + * + * @param cls the LOCKMANAGER_Handle + * @param msg received message, NULL on timeout or fatal error + */ +static void +handle_replies (void *cls, + const struct GNUNET_MessageHeader *msg); + + /** * Transmit notify for sending message to server * @@ -178,13 +194,20 @@ transmit_notify (void *cls, size_t size, void *buf) uint16_t msg_size; handle->transmit_handle = NULL; + queue_entity = handle->mq_head; + GNUNET_assert (NULL != queue_entity); if ((0 == size) || (NULL == buf)) { - /* FIXME: Timed out -- requeue? */ + handle->transmit_handle = + GNUNET_CLIENT_notify_transmit_ready (handle->conn, + ntohs + (queue_entity->msg->header.size), + GNUNET_TIME_UNIT_FOREVER_REL, + GNUNET_YES, + &transmit_notify, + handle); return 0; - } - queue_entity = handle->mq_head; - GNUNET_assert (NULL != queue_entity); + } msg_size = ntohs (queue_entity->msg->header.size); GNUNET_assert (size >= msg_size); memcpy (buf, queue_entity->msg, msg_size); @@ -207,6 +230,14 @@ transmit_notify (void *cls, size_t size, void *buf) &transmit_notify, handle); } + if (GNUNET_NO == handle->in_replies) + { + GNUNET_CLIENT_receive (handle->conn, + &handle_replies, + handle, + GNUNET_TIME_UNIT_FOREVER_REL); + handle->in_replies = GNUNET_YES; + } return msg_size; } @@ -344,7 +375,32 @@ call_status_cb_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) /** - * Iterator to call relase and free all LockingRequest entries + * Function to generate acquire message for a lock + * + * @param domain_name the domain name of the lock + * @param lock the lock number + * @return the generated GNUNET_LOCKMANAGER_Message + */ +static struct GNUNET_LOCKMANAGER_Message * +generate_acquire_msg (const char *domain_name, uint32_t lock) +{ + struct GNUNET_LOCKMANAGER_Message *msg; + size_t domain_name_len; + uint16_t msg_size; + + domain_name_len = strlen (domain_name) + 1; + msg_size = sizeof (struct GNUNET_LOCKMANAGER_Message) + domain_name_len; + msg = GNUNET_malloc (msg_size); + msg->header.type = htons (GNUNET_MESSAGE_TYPE_LOCKMANAGER_ACQUIRE); + msg->header.size = htons (msg_size); + msg->lock = htonl (lock); + memcpy (&msg[1], domain_name, domain_name_len); + return msg; +} + + +/** + * Iterator to call relase on locks * * @param cls the lockmanager handle * @param key current key code @@ -354,29 +410,29 @@ call_status_cb_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) * GNUNET_NO if not. */ static int -release_iterator(void *cls, - const struct GNUNET_HashCode * key, - void *value) +release_n_retry_iterator (void *cls, + const struct GNUNET_HashCode * key, + void *value) { - struct GNUNET_LOCKMANAGER_Handle *h = cls; struct GNUNET_LOCKMANAGER_LockingRequest *r = value; + struct GNUNET_LOCKMANAGER_Handle *h = cls; + struct GNUNET_LOCKMANAGER_Message *msg; + if (GNUNET_LOCKMANAGER_RELEASE == r->status) + return GNUNET_YES; if (NULL != r->status_cb) { LOG (GNUNET_ERROR_TYPE_DEBUG, "Calling status change for RELEASE on lock num: %d, domain: %s\n", r->lock, r->domain); + r->status = GNUNET_LOCKMANAGER_RELEASE; r->status_cb (r->status_cb_cls, r->domain, r->lock, - GNUNET_LOCKMANAGER_RELEASE); + GNUNET_LOCKMANAGER_RELEASE); } - GNUNET_assert (GNUNET_YES == - GNUNET_CONTAINER_multihashmap_remove (h->hashmap, - key, - value)); - GNUNET_free (r->domain); - GNUNET_free (r); + msg = generate_acquire_msg (r->domain, r->lock); + queue_message (h, msg); return GNUNET_YES; } @@ -398,14 +454,15 @@ handle_replies (void *cls, struct GNUNET_HashCode hash; uint32_t lock; uint16_t msize; - + + handle->in_replies = GNUNET_NO; if (NULL == msg) { LOG (GNUNET_ERROR_TYPE_DEBUG, - "Lockmanager service not available or went down\n"); - /* Should release all locks and free its locking requests */ + "Lockmanager service not available or went down\n"); + /* Should release all locks and retry to acquire them */ GNUNET_CONTAINER_multihashmap_iterate (handle->hashmap, - &release_iterator, + &release_n_retry_iterator, handle); return; } @@ -413,6 +470,7 @@ handle_replies (void *cls, &handle_replies, handle, GNUNET_TIME_UNIT_FOREVER_REL); + handle->in_replies = GNUNET_YES; if (GNUNET_MESSAGE_TYPE_LOCKMANAGER_SUCCESS != ntohs(msg->type)) { GNUNET_break (0); @@ -522,7 +580,7 @@ GNUNET_LOCKMANAGER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg) &handle_replies, h, GNUNET_TIME_UNIT_FOREVER_REL); - + h->in_replies = GNUNET_YES; LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__); return h; } @@ -604,7 +662,6 @@ GNUNET_LOCKMANAGER_acquire_lock (struct GNUNET_LOCKMANAGER_Handle *handle, struct GNUNET_LOCKMANAGER_LockingRequest *r; struct GNUNET_LOCKMANAGER_Message *msg; struct GNUNET_HashCode hash; - uint16_t msg_size; size_t domain_name_length; LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __func__); @@ -617,12 +674,7 @@ GNUNET_LOCKMANAGER_acquire_lock (struct GNUNET_LOCKMANAGER_Handle *handle, r->status_cb = status_cb; r->status_cb_cls = status_cb_cls; memcpy (r->domain, domain_name, domain_name_length); - msg_size = sizeof (struct GNUNET_LOCKMANAGER_Message) + domain_name_length; - msg = GNUNET_malloc (msg_size); - msg->header.type = htons (GNUNET_MESSAGE_TYPE_LOCKMANAGER_ACQUIRE); - msg->header.size = htons (msg_size); - msg->lock = htonl (lock); - memcpy (&msg[1], r->domain, domain_name_length); + msg = generate_acquire_msg (r->domain, r->lock); LOG (GNUNET_ERROR_TYPE_DEBUG, "Queueing ACQUIRE message\n"); queue_message (handle, msg); get_key (r->domain, r->lock, &hash); @@ -638,8 +690,8 @@ GNUNET_LOCKMANAGER_acquire_lock (struct GNUNET_LOCKMANAGER_Handle *handle, /** * Function to cancel the locking request generated by - * GNUNET_LOCKMANAGER_acquire_lock. If the lock is acquired us then the lock is - * released. GNUNET_LOCKMANAGER_StatusCallback will not be called upon any + * GNUNET_LOCKMANAGER_acquire_lock. If the lock is acquired by us then the lock + * is released. GNUNET_LOCKMANAGER_StatusCallback will not be called upon any * status changes resulting due to this call. * * @param request the LockingRequest to cancel diff --git a/src/lockmanager/test_lockmanager_api.c b/src/lockmanager/test_lockmanager_api.c index e8d04128f..f9f7aac0c 100644 --- a/src/lockmanager/test_lockmanager_api.c +++ b/src/lockmanager/test_lockmanager_api.c @@ -239,7 +239,7 @@ int main (int argc, char **argv) { int ret; - char *const argv2[] = { "test-lockmanager-api", + char *const argv2[] = { "test_lockmanager_api", "-c", "test_lockmanager_api.conf", #if VERBOSE "-L", "DEBUG", @@ -251,7 +251,7 @@ int main (int argc, char **argv) GNUNET_GETOPT_OPTION_END }; - GNUNET_log_setup ("test-lockmanager-api", + GNUNET_log_setup ("test_lockmanager_api", #if VERBOSE "DEBUG", #else @@ -261,7 +261,7 @@ int main (int argc, char **argv) ret = GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2, - "test-lockmanager-api", "nohelp", options, &run, NULL); + "test_lockmanager_api", "nohelp", options, &run, NULL); if (GNUNET_OK != ret) { diff --git a/src/lockmanager/test_lockmanager_api_acquireretry.c b/src/lockmanager/test_lockmanager_api_acquireretry.c new file mode 100644 index 000000000..6f4f1379e --- /dev/null +++ b/src/lockmanager/test_lockmanager_api_acquireretry.c @@ -0,0 +1,284 @@ +/* + This file is part of GNUnet. + (C) 2012 Christian Grothoff (and other contributing authors) + + GNUnet is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 2, or (at your + option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNUnet; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +/** + * @file lockmanager/test_lockmanager_api_acquireretry.c + * @brief Test cases for lockmanager_api where the server crashes and comes + * back; the api should try to acqurie the lock again + * @author Sree Harsha Totakura + */ + +#include "platform.h" +#include "gnunet_util_lib.h" +#include "gnunet_lockmanager_service.h" + +/** + * Generic logging shortcut + */ +#define LOG(kind,...) \ + GNUNET_log (kind, __VA_ARGS__) + +#define TIME_REL_SECS(sec) \ + GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec) + +/** + * Various stages in test + */ +enum Test + { + /** + * Signal test failure + */ + TEST_FAIL, + + /** + * Testing just began + */ + TEST_INIT, + + /** + * Client has successfully acquired the lock + */ + TEST_CLIENT_LOCK_SUCESS, + + /** + * Client has lost the lock + */ + TEST_CLIENT_LOCK_RELEASE, + + /** + * Client has again acquired the lock + */ + TEST_CLIENT_LOCK_AGAIN_SUCCESS + }; + +/** + * The process id of the GNUNET ARM process + */ +static struct GNUNET_OS_Process *arm_pid = NULL; + +/** + * Configuration Handle + */ +static struct GNUNET_CONFIGURATION_Handle *config; + +/** + * The handle to the lockmanager service + */ +static struct GNUNET_LOCKMANAGER_Handle *handle; + +/** + * The locking request + */ +static struct GNUNET_LOCKMANAGER_LockingRequest *request; + +/** + * Abort task identifier + */ +static GNUNET_SCHEDULER_TaskIdentifier abort_task_id; + +/** + * The test result + */ +enum Test result; + + +/** + * Shutdown nicely + * + * @param cls + * @param tc the task context + */ +static void +do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + if (GNUNET_SCHEDULER_NO_TASK != abort_task_id) + { + GNUNET_SCHEDULER_cancel (abort_task_id); + abort_task_id = GNUNET_SCHEDULER_NO_TASK; + } + if (NULL != handle) + GNUNET_LOCKMANAGER_disconnect (handle); + if (NULL != arm_pid) + { + if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM)) + { + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Kill gnunet-service-arm manually\n"); + } + GNUNET_OS_process_wait (arm_pid); + GNUNET_OS_process_destroy (arm_pid); + } + if (NULL != config) + GNUNET_CONFIGURATION_destroy (config); +} + +/** + * Abort + * + * @param cls + * @param tc the task context + */ +static void +do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, "Aborting test...\n"); + abort_task_id = GNUNET_SCHEDULER_NO_TASK; + result = TEST_FAIL; + do_shutdown (cls, tc); +} + + +/** + * Callback for lock status changes + * + * @param cls the handle + * + * @param domain_name the locking domain of the lock + * + * @param lock the lock for which this status is relevant + * + * @param status GNUNET_LOCKMANAGER_SUCCESS if the lock has been successfully + * acquired; GNUNET_LOCKMANAGER_RELEASE when the acquired lock is lost + */ +static void +status_cb (void *cls, + const char *domain_name, + uint32_t lock, + enum GNUNET_LOCKMANAGER_Status status) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Status change callback called on lock: %d of domain: %s\n", + lock, domain_name); + switch (result) + { + case TEST_INIT: + GNUNET_assert (handle == cls); + GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status); + result = TEST_CLIENT_LOCK_SUCCESS; + /* We should kill the lockmanager process */ + if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM)) + { + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Kill gnunet-service-arm manually\n"); + } + GNUNET_OS_process_wait (arm_pid); + GNUNET_OS_process_destroy (arm_pid); + arm_pid =NULL; + break; + case TEST_CLIENT_LOCK_SUCCESS: + GNUNET_assert (handle == cls); + GNUNET_assert (GNUNET_LOCKMANAGER_RELEASE == status); + result = TEST_CLIENT_LOCK_RELEASE; + /* Now we should start again the lockmanager process */ + arm_pid = + GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm", + "gnunet-service-arm", + "-c", "test_lockmanager_api.conf", NULL); + GNUNET_assert (NULL != arm_pid); + break; + case TEST_CLIENT_LOCK_RELEASE: + GNUNET_asset (handle == cls); + GNUNET_assert (GNUNET_LOCKMANAGER_SUCCESS == status); + result = TEST_CLIENT_LOCK_AGAIN_SUCCESS; + GNUNET_LOCKMANAGER_cancel_request (request); + request = NULL; + GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(1), &do_shutdown, NULL); + break; + default: + GNUNET_assert (0); /* We should never reach here */ + } +} + + +/** + * Testing function + * + * @param cls NULL + * @param tc the task context + */ +static void +test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + result = TEST_INIT; + handle = GNUNET_LOCKMANAGER_connect (config); + GNUNET_assert (NULL != handle); + request = GNUNET_LOCKMANAGER_acquire_lock (handle, + "GNUNET_LOCKMANAGER_TESTING", + 99, + &status_cb, + handle); + GNUNET_assert (NULL != request); + abort_task_id = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (10), + &do_abort, + NULL); +} + + +/** + * Main point of test execution + */ +static void +run (void *cls, char *const *args, const char *cfgfile, + const struct GNUNET_CONFIGURATION_Handle *cfg) +{ + config = cfg; + arm_pid = + GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm", + "gnunet-service-arm", + "-c", "test_lockmanager_api.conf", NULL); + GNUNET_assert (NULL != arm_pid); + GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(3), &test, NULL); +} + + +/** + * Main function + */ +int main (int argc, char **argv) +{ + int ret; + + char *const argv2[] = { "test_lockmanager_api_servercrash", + "-c", "test_lockmanager_api.conf", + NULL + }; + struct GNUNET_GETOPT_CommandLineOption options[] = { + GNUNET_GETOPT_OPTION_END + }; + + ret = + GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2, + "test_lockmanager_api_servercrash", + "nohelp", options, &run, NULL); + if (GNUNET_OK != ret) + { + LOG (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n", + ret); + return 1; + } + if (TEST_CLIENT_LOCK_AGAIN_SUCCESS != result) + { + LOG (GNUNET_ERROR_TYPE_WARNING, "test failed\n"); + return 1; + } + LOG (GNUNET_ERROR_TYPE_INFO, "test OK\n"); + return 0; +} diff --git a/src/lockmanager/test_lockmanager_api_lockrelease.c b/src/lockmanager/test_lockmanager_api_lockrelease.c index 7e24d10ce..64e328ba0 100644 --- a/src/lockmanager/test_lockmanager_api_lockrelease.c +++ b/src/lockmanager/test_lockmanager_api_lockrelease.c @@ -260,7 +260,7 @@ int main (int argc, char **argv) { int ret; - char *const argv2[] = { "test-lockmanager-api-lockrelease", + char *const argv2[] = { "test_lockmanager_api_lockrelease", "-c", "test_lockmanager_api.conf", #if VERBOSE "-L", "DEBUG", @@ -272,7 +272,7 @@ int main (int argc, char **argv) GNUNET_GETOPT_OPTION_END }; - GNUNET_log_setup ("test-lockmanager-api-lockrelease", + GNUNET_log_setup ("test_lockmanager_api_lockrelease", #if VERBOSE "DEBUG", #else @@ -282,7 +282,7 @@ int main (int argc, char **argv) ret = GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2, - "test-lockmanager-api-lockrelease", + "test_lockmanager_api_lockrelease", "nohelp", options, &run, NULL); if (GNUNET_OK != ret) diff --git a/src/lockmanager/test_lockmanager_api_servercrash.c b/src/lockmanager/test_lockmanager_api_servercrash.c index 3fa6418c8..974a31f7e 100644 --- a/src/lockmanager/test_lockmanager_api_servercrash.c +++ b/src/lockmanager/test_lockmanager_api_servercrash.c @@ -217,6 +217,8 @@ status_cb (void *cls, GNUNET_assert (99 == lock); GNUNET_assert (0 == strcmp (domain_name, "GNUNET_LOCKMANAGER_TESTING")); result = TEST_CLIENT2_SERVER_CRASH_SUCCESS; + GNUNET_LOCKMANAGER_cancel_request (request2); + request2 = NULL; GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (1), &do_shutdown, NULL); @@ -285,7 +287,7 @@ int main (int argc, char **argv) { int ret; - char *const argv2[] = { "test-lockmanager-api-servercrash", + char *const argv2[] = { "test_lockmanager_api_servercrash", "-c", "test_lockmanager_api.conf", #if VERBOSE "-L", "DEBUG", @@ -297,7 +299,7 @@ int main (int argc, char **argv) GNUNET_GETOPT_OPTION_END }; - GNUNET_log_setup ("test-lockmanager-api-servercrash", + GNUNET_log_setup ("test_lockmanager_api_servercrash", #if VERBOSE "DEBUG", #else @@ -307,7 +309,7 @@ int main (int argc, char **argv) ret = GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2, - "test-lockmanager-api-servercrash", + "test_lockmanager_api_servercrash", "nohelp", options, &run, NULL); if (GNUNET_OK != ret)