diff options
author | Vijay Bellur <vijay@gluster.com> | 2010-08-10 01:57:45 +0000 |
---|---|---|
committer | Anand V. Avati <avati@dev.gluster.com> | 2010-08-11 22:24:47 -0700 |
commit | b0f54a49164a1c618b9e207349bd96705b44dfa6 (patch) | |
tree | 464a4026e786d6791a0ea2c92a6137dcd5e066b3 /cli/src | |
parent | 5f7018275bf8006ff758817037d03936b8a95d43 (diff) |
cli: Introduce timeouts for command response and connection
Signed-off-by: Vijay Bellur <vijay@gluster.com>
Signed-off-by: Anand V. Avati <avati@dev.gluster.com>
BUG: 1262 ()
URL: http://bugs.gluster.com/cgi-bin/bugzilla3/show_bug.cgi?id=1262
Diffstat (limited to 'cli/src')
-rw-r--r-- | cli/src/cli-cmd.c | 47 | ||||
-rw-r--r-- | cli/src/cli.h | 2 | ||||
-rw-r--r-- | cli/src/cli3_1-cops.c | 2 | ||||
-rw-r--r-- | cli/src/input.c | 1 |
4 files changed, 43 insertions, 9 deletions
diff --git a/cli/src/cli-cmd.c b/cli/src/cli-cmd.c index f7c04443c05..b61fc819f37 100644 --- a/cli/src/cli-cmd.c +++ b/cli/src/cli-cmd.c @@ -35,6 +35,7 @@ #include <fnmatch.h> static int cmd_done; +static int cmd_sent; static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; static pthread_mutex_t cond_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t conn = PTHREAD_COND_INITIALIZER; @@ -78,6 +79,15 @@ cli_cmd_process (struct cli_state *state, int argc, char **argv) return -1; } + ret = cli_cmd_await_connected (); + if (ret) { + cli_out ("Connection failed. Please check if gluster daemon" + " is operational."); + gf_log ("", GF_LOG_NORMAL, "Exiting with: %d", ret); + exit (ret); + } + + ret = word->cbkfn (state, word, (const char **)argv, argc); return ret; @@ -208,19 +218,32 @@ cli_cmd_unlock () int cli_cmd_await_response () { + struct timespec ts = {0,}; + int ret = 0; + cli_op_ret = -1; cmd_done = 0; - while (!cmd_done) - pthread_cond_wait (&cond, &cond_mutex); + time (&ts.tv_sec); + ts.tv_sec += CLI_DEFAULT_CMD_TIMEOUT; + while (!cmd_done && !ret) { + ret = pthread_cond_timedwait (&cond, &cond_mutex, + &ts); + } cli_cmd_unlock (); + if (ret) + return ret; + return cli_op_ret; } int cli_cmd_broadcast_response (int32_t status) { + if (!cmd_sent) + goto out; + pthread_mutex_lock (&cond_mutex); { cmd_done = 1; @@ -230,23 +253,29 @@ cli_cmd_broadcast_response (int32_t status) pthread_mutex_unlock (&cond_mutex); +out: return 0; } int32_t cli_cmd_await_connected () { + int32_t ret = 0; + struct timespec ts = {0,}; - pthread_mutex_lock (&conn_mutex); + pthread_mutex_lock (&conn_mutex); { - while (!connected) { - pthread_cond_wait (&conn, &conn_mutex); + time (&ts.tv_sec); + ts.tv_sec += CLI_DEFAULT_CONN_TIMEOUT; + while (!connected && !ret) { + ret = pthread_cond_timedwait (&conn, &conn_mutex, + &ts); } } pthread_mutex_unlock (&conn_mutex); - return 0; + return ret; } int32_t @@ -273,13 +302,15 @@ cli_cmd_submit (void *req, call_frame_t *frame, int ret = -1; cli_cmd_lock (); + cmd_sent = 0; ret = cli_submit_request (req, frame, prog, procnum, NULL, sfunc, this, cbkfn); - if (!ret) + if (!ret) { + cmd_sent = 1; ret = cli_cmd_await_response (); - else + } else cli_cmd_unlock (); gf_log ("cli", GF_LOG_DEBUG, "Returning %d", ret); diff --git a/cli/src/cli.h b/cli/src/cli.h index 36ac7214bfd..854c609d2ca 100644 --- a/cli/src/cli.h +++ b/cli/src/cli.h @@ -31,6 +31,8 @@ #define DEFAULT_EVENT_POOL_SIZE 16384 #define CLI_GLUSTERD_PORT 6969 +#define CLI_DEFAULT_CONN_TIMEOUT 120 +#define CLI_DEFAULT_CMD_TIMEOUT 120 enum argp_option_keys { ARGP_DEBUG_KEY = 133, diff --git a/cli/src/cli3_1-cops.c b/cli/src/cli3_1-cops.c index de3aa6ed2dd..d9dd3f6feb8 100644 --- a/cli/src/cli3_1-cops.c +++ b/cli/src/cli3_1-cops.c @@ -138,6 +138,8 @@ gf_cli3_1_list_friends_cbk (struct rpc_req *req, struct iovec *iov, gf_log ("cli", GF_LOG_NORMAL, "Received resp to list: %d", rsp.op_ret); + ret = rsp.op_ret; + if (!rsp.op_ret) { if (!rsp.friends.friends_len) { diff --git a/cli/src/input.c b/cli/src/input.c index 25a7cb62dd9..a577a0f4c13 100644 --- a/cli/src/input.c +++ b/cli/src/input.c @@ -41,7 +41,6 @@ cli_batch (void *d) state = d; - cli_cmd_await_connected (); ret = cli_cmd_process (state, state->argc, state->argv); gf_log ("", GF_LOG_NORMAL, "Exiting with: %d", ret); exit (ret); |