Skip to content

Commit

Permalink
Merge pull request #107 from doyoubi/feature/RedirectStat
Browse files Browse the repository at this point in the history
Add stat for redirect
  • Loading branch information
doyoubi authored Feb 28, 2017
2 parents 6f29cd5 + fd0ef10 commit 274f3c4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ static int cmd_format_stats(char *dest, size_t n, struct stats *stats, char *lat
"remote_latency:%.6f\r\n"
"total_latency:%.6f\r\n"
"last_command_latency:%s\r\n"
"ask_recv:%lld\r\n"
"moved_recv:%lld\r\n"
"remotes:%s\r\n",
config.cluster, VERSION, getpid(), config.thread,
CV_MALLOC_LIB,
Expand All @@ -195,6 +197,8 @@ static int cmd_format_stats(char *dest, size_t n, struct stats *stats, char *lat
stats->basic.recv_bytes, stats->basic.send_bytes,
stats->basic.remote_latency / 1000000.0,
stats->basic.total_latency / 1000000.0, latency,
stats->basic.ask_recv,
stats->basic.moved_recv,
stats->remote_nodes);
}

Expand Down
2 changes: 2 additions & 0 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,12 @@ int server_read_reply(struct connection *server, struct command *cmd)
}
switch (info.type) {
case CMD_ERR_MOVED:
ATOMIC_INC(cmd->ctx->stats.moved_recv, 1);
slot_create_job(SLOT_UPDATE);
CHECK_REDIRECTED(cmd, info.addr, rep_redirect_err);
return server_redirect(cmd, &info);
case CMD_ERR_ASK:
ATOMIC_INC(cmd->ctx->stats.ask_recv, 1);
CHECK_REDIRECTED(cmd, info.addr, rep_redirect_err);
cmd->asking = 1;
return server_redirect(cmd, &info);
Expand Down
6 changes: 6 additions & 0 deletions src/stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ static inline void stats_copy_basic_fields(struct basic_stats *dst, struct basic
dst->total_latency = ATOMIC_GET(src->total_latency);
dst->recv_bytes = ATOMIC_GET(src->recv_bytes);
dst->send_bytes = ATOMIC_GET(src->send_bytes);
dst->ask_recv = ATOMIC_GET(src->ask_recv);
dst->moved_recv = ATOMIC_GET(src->moved_recv);
}

static inline void stats_cumulate(struct stats *stats)
Expand All @@ -64,6 +66,8 @@ static inline void stats_cumulate(struct stats *stats)
ATOMIC_INC(cumulation.basic.total_latency, stats->basic.total_latency);
ATOMIC_INC(cumulation.basic.recv_bytes, stats->basic.recv_bytes);
ATOMIC_INC(cumulation.basic.send_bytes, stats->basic.send_bytes);
ATOMIC_INC(cumulation.basic.ask_recv, stats->basic.ask_recv);
ATOMIC_INC(cumulation.basic.moved_recv, stats->basic.moved_recv);
}

static void stats_send(char *metric, double value)
Expand Down Expand Up @@ -143,6 +147,8 @@ void stats_get_simple(struct stats *stats, bool reset)
STATS_ASSIGN(total_latency);
STATS_ASSIGN(recv_bytes);
STATS_ASSIGN(send_bytes);
STATS_ASSIGN(ask_recv);
STATS_ASSIGN(moved_recv);
stats->basic.connected_clients += ATOMIC_GET(contexts[i].stats.connected_clients);
}

Expand Down
3 changes: 3 additions & 0 deletions src/stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ struct basic_stats {

long long remote_latency;
long long total_latency;

long long ask_recv;
long long moved_recv;
};

struct stats {
Expand Down
10 changes: 10 additions & 0 deletions tests/test_stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ void set_stats(struct context *ctx)
ctx->stats.recv_bytes = 16;
ctx->stats.send_bytes = 32;
ctx->stats.connected_clients = 5;
ctx->stats.ask_recv = 233;
ctx->stats.moved_recv = 600;
}

TEST(test_stats_get_simple_reset) {
Expand All @@ -29,12 +31,16 @@ TEST(test_stats_get_simple_reset) {
ASSERT(stats.basic.recv_bytes == 16);
ASSERT(stats.basic.send_bytes == 32);
ASSERT(stats.basic.connected_clients == 5);
ASSERT(stats.basic.ask_recv == 233);
ASSERT(stats.basic.moved_recv == 600);

ASSERT(ctxs[0].stats.completed_commands == 0);
ASSERT(ctxs[0].stats.remote_latency == 0);
ASSERT(ctxs[0].stats.total_latency == 0);
ASSERT(ctxs[0].stats.recv_bytes == 0);
ASSERT(ctxs[0].stats.send_bytes == 0);
ASSERT(ctxs[0].stats.ask_recv == 0);
ASSERT(ctxs[0].stats.moved_recv == 0);
PASS(NULL);
}

Expand All @@ -54,13 +60,17 @@ TEST(test_stats_get_simple_cumulative) {
ASSERT(stats.basic.recv_bytes == 32);
ASSERT(stats.basic.send_bytes == 64);
ASSERT(stats.basic.connected_clients == 5);
ASSERT(stats.basic.ask_recv == 466);
ASSERT(stats.basic.moved_recv == 1200);

ASSERT(ctxs[0].stats.completed_commands == 10);
ASSERT(ctxs[0].stats.remote_latency == 1000);
ASSERT(ctxs[0].stats.total_latency == 10000);
ASSERT(ctxs[0].stats.recv_bytes == 16);
ASSERT(ctxs[0].stats.send_bytes == 32);
ASSERT(ctxs[0].stats.connected_clients == 5);
ASSERT(ctxs[0].stats.ask_recv == 233);
ASSERT(ctxs[0].stats.moved_recv == 600);
PASS(NULL);
}

Expand Down

0 comments on commit 274f3c4

Please sign in to comment.