Content-Length: 399211 | pFad | http://github.com/naver/arcus-memcached/commit/941c9f814fa10129526233a659fbc997f5ac1a7a

C7 CLEANUP: refactored lqdetect_get_stats() function. · naver/arcus-memcached@941c9f8 · GitHub
Skip to content

Commit

Permalink
CLEANUP: refactored lqdetect_get_stats() function.
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhwanJang committed Oct 28, 2021
1 parent cabc720 commit 941c9f8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 44 deletions.
78 changes: 41 additions & 37 deletions lqdetect.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

#include "lqdetect.h"

#define LQ_SAVE_CNT 20 /* save key count */
#define LQ_INPUT_SIZE 500 /* the size of input(time, ip, command, argument) */
#define LQ_SAVE_CNT 20 /* save key count */
#define LQ_INPUT_SIZE 500 /* the size of input(time, ip, command, argument) */
#define LQ_STAT_STRLEN 300 /* max length of stats */

static EXTENSION_LOGGER_DESCRIPTOR *mc_logger;
static const char *command_str[LQ_CMD_NUM] = {
Expand All @@ -36,7 +37,7 @@ struct lq_detect_global {
struct lq_detect_buffer buffer[LQ_CMD_NUM];
struct lq_detect_stats stats;
int overflow_cnt;
bool on_detecting;
bool on_detect;
uint16_t refcount; /* lqdetect show reference count */
};
struct lq_detect_global lqdetect;
Expand All @@ -45,18 +46,18 @@ struct lq_detect_global lqdetect;
static void do_lqdetect_stop(int cause)
{
/* detect long query lock has already been held */
lqdetect.stats.stop_cause = cause;
lqdetect.stats.state = cause;
lqdetect.stats.enddate = getnowdate_int();
lqdetect.stats.endtime = getnowtime_int();
lqdetect.on_detecting = false;
lqdetect.on_detect = false;
}

int lqdetect_init(EXTENSION_LOGGER_DESCRIPTOR *logger)
{
mc_logger = logger;
int ii, jj;
pthread_mutex_init(&lqdetect.lock, NULL);
lqdetect.on_detecting = false;
lqdetect.on_detect = false;

memset(lqdetect.buffer, 0, LQ_CMD_NUM * sizeof(struct lq_detect_buffer));
for(ii = 0; ii < LQ_CMD_NUM; ii++) {
Expand Down Expand Up @@ -89,7 +90,7 @@ int lqdetect_start(uint32_t lqdetect_standard, bool *already_started)

pthread_mutex_lock(&lqdetect.lock);
do {
if (lqdetect.on_detecting) {
if (lqdetect.on_detect) {
*already_started = true;
break;
}
Expand All @@ -111,11 +112,11 @@ int lqdetect_start(uint32_t lqdetect_standard, bool *already_started)
memset(&lqdetect.stats, 0, sizeof(struct lq_detect_stats));
lqdetect.stats.bgndate = getnowdate_int();
lqdetect.stats.bgntime = getnowtime_int();
lqdetect.stats.stop_cause = LQ_RUNNING;
lqdetect.stats.state = LQ_RUNNING;
lqdetect.stats.standard = lqdetect_standard;

lqdetect.overflow_cnt = 0;
lqdetect.on_detecting = true;
lqdetect.on_detect = true;
ret = 0;
} while(0);
pthread_mutex_unlock(&lqdetect.lock);
Expand All @@ -126,43 +127,46 @@ int lqdetect_start(uint32_t lqdetect_standard, bool *already_started)
void lqdetect_stop(bool *already_stopped)
{
pthread_mutex_lock(&lqdetect.lock);
if (lqdetect.on_detecting == true) {
if (lqdetect.on_detect == true) {
do_lqdetect_stop(LQ_EXPLICIT_STOP);
} else {
*already_stopped = true;
}
pthread_mutex_unlock(&lqdetect.lock);
}

void lqdetect_get_stats(char* str)
char *lqdetect_stats(void)
{
int ii;
char *stop_cause_str[3] = {
"stopped by explicit request", // LQ_EXPLICIT_STOP
"stopped by long query overflow", // LQ_OVERFLOW_STOP
"running" // LQ_RUNNING
};
struct lq_detect_stats stats = lqdetect.stats;

if (lqdetect.on_detecting) {
stats.enddate = 0;
stats.endtime = 0;
}
char *str = (char*)malloc(LQ_STAT_STRLEN);
if (str) {
char *state_str[3] = {
"stopped by explicit request", // LQ_EXPLICIT_STOP
"stopped by internal buffer overflow", // LQ_OVERFLOW_STOP
"running" // LQ_RUNNING
};
struct lq_detect_stats stats = lqdetect.stats;

if (lqdetect.on_detect) {
stats.enddate = 0;
stats.endtime = 0;
}

stats.total_lqcmds = 0;
for (ii=0; ii < LQ_CMD_NUM; ii++) {
stats.total_lqcmds += lqdetect.buffer[ii].ntotal;
}
stats.total_lqcmds = 0;
for (int i=0; i < LQ_CMD_NUM; i++) {
stats.total_lqcmds += lqdetect.buffer[i].ntotal;
}

snprintf(str, LQ_STAT_STRLEN,
"\t" "Long query detection stats : %s" "\n"
"\t" "The last running time : %d_%d ~ %d_%d" "\n"
"\t" "The number of total long query commands : %d" "\n"
"\t" "The detection standard : %u" "\n",
(stats.stop_cause >= 0 && stats.stop_cause <= 2 ?
stop_cause_str[stats.stop_cause] : "unknown"),
stats.bgndate, stats.bgntime, stats.enddate, stats.endtime,
stats.total_lqcmds, stats.standard);
snprintf(str, LQ_STAT_STRLEN,
"\t" "Long query detection stats : %s" "\n"
"\t" "The last running time : %d_%d ~ %d_%d" "\n"
"\t" "The number of total long query commands : %d" "\n"
"\t" "The detection standard : %u" "\n",
(stats.state >= 0 && stats.state <= 2 ?
state_str[stats.state] : "unknown"),
stats.bgndate, stats.bgntime, stats.enddate, stats.endtime,
stats.total_lqcmds, stats.standard);
}
return str;
}

char *lqdetect_buffer_get(int cmd, uint32_t *length, uint32_t *cmdcnt)
Expand Down Expand Up @@ -335,7 +339,7 @@ static bool lqdetect_save_cmd(char client_ip[], char* key,

pthread_mutex_lock(&lqdetect.lock);
do {
if (! lqdetect.on_detecting) {
if (! lqdetect.on_detect) {
ret = false;
break;
}
Expand Down
7 changes: 3 additions & 4 deletions lqdetect.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "memcached/util.h"

#define DETECT_LONG_QUERY
#define LQ_STAT_STRLEN 300
#define LQ_STANDARD_DEFAULT 4000 /* default detect standard */
/* longest range: "<longest bkey>..<longest bkey> efilter" */
#define LQ_RANGE_SIZE (64*2+16)
Expand Down Expand Up @@ -43,8 +42,8 @@ struct lq_detect_argument {
struct lq_detect_stats {
int bgndate, bgntime;
int enddate, endtime;
int total_lqcmds; /* number of total long query command */
int stop_cause; /* how stopped */
int total_lqcmds; /* number of total long query command */
int state; /* lqdetect state */
uint32_t standard;
};

Expand All @@ -54,7 +53,7 @@ char *lqdetect_buffer_get(int cmd, uint32_t *length, uint32_t *cmdcnt);
void lqdetect_buffer_release(int bufcnt);
int lqdetect_start(uint32_t lqdetect_base, bool *already_started);
void lqdetect_stop(bool *already_stopped);
void lqdetect_get_stats(char* str);
char *lqdetect_stats(void);

bool lqdetect_lop_insert(char *client_ip, char *key, int coll_index);
bool lqdetect_lop_delete(char *client_ip, char *key, uint32_t del_count,
Expand Down
9 changes: 6 additions & 3 deletions memcached.c
Original file line number Diff line number Diff line change
Expand Up @@ -9706,9 +9706,12 @@ static void process_lqdetect_command(conn *c, token_t *tokens, size_t ntokens)
} else if (ntokens > 2 && strcmp(type, "show") == 0) {
lqdetect_show(c);
} else if (ntokens > 2 && strcmp(type, "stats") == 0) {
char str[LQ_STAT_STRLEN];
lqdetect_get_stats(str);
out_string(c, str);
char *str = lqdetect_stats();
if (str) {
write_and_free(c, str, strlen(str));
} else {
out_string(c, "\tlong query detection failed to get stats memory.\n");
}
} else {
out_string(c,
"\t" "* Usage: lqdetect [start [standard] | stop | show | stats]" "\n"
Expand Down

0 comments on commit 941c9f8

Please sign in to comment.








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/naver/arcus-memcached/commit/941c9f814fa10129526233a659fbc997f5ac1a7a

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy