Skip to content

Commit

Permalink
INTERNAL: Refactor lqdetect show
Browse files Browse the repository at this point in the history
refactor lqdetect show filed_t to char *
  • Loading branch information
cheesecrust committed Jul 19, 2024
1 parent 447601d commit 0dcdf02
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 80 deletions.
55 changes: 15 additions & 40 deletions lqdetect.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ struct lq_detect_global {
struct lq_detect_buffer buffer[LQ_CMD_NUM];
struct lq_detect_stats stats;
int overflow_cnt;
uint16_t refcount; /* lqdetect show reference count */
};
struct lq_detect_global lqdetect;

Expand Down Expand Up @@ -240,7 +239,6 @@ int lqdetect_init(EXTENSION_LOGGER_DESCRIPTOR *logger)
}
memset(lqdetect.arg[ii], 0, LQ_SAVE_CNT * sizeof(struct lq_detect_argument));
}
lqdetect.refcount = 0;
return 0;
}

Expand All @@ -262,12 +260,6 @@ int lqdetect_start(uint32_t threshold, bool *already_started)
break;
}

/* if lqdetect showing, start is fail */
if (lqdetect.refcount != 0) {
ret = -1;
break;
}

/* prepare detect long query buffer, argument and counts*/
for (int ii = 0; ii < LQ_CMD_NUM; ii++) {
lqdetect.buffer[ii].ntotal = 0;
Expand Down Expand Up @@ -335,47 +327,30 @@ char *lqdetect_stats(void)
return str;
}

field_t *lqdetect_result_get(int *size)
field_t lqdetect_result_get(void)
{
int hdrlen = 32;
int fldarr_size = LQ_CMD_NUM * 2 * sizeof(field_t);
int hdrarr_size = LQ_CMD_NUM * hdrlen; /* command, ntotal */
/* field_t and header string array */
field_t *fldarr = (field_t*)malloc(fldarr_size + hdrarr_size);
if (fldarr == NULL) {
return NULL;
}
char *hdrptr = (char*)fldarr + fldarr_size;
int fldcnt = 0;
int bytes = hdrlen * LQ_CMD_NUM;
field_t result = {NULL, 0};

pthread_mutex_lock(&lqdetect.lock);
/* Each result consists of header and body. */
for (int i = 0; i < LQ_CMD_NUM; i++) {
struct lq_detect_buffer ldb = lqdetect.buffer[i];
/* header */
fldarr[fldcnt].length = snprintf(hdrptr, hdrlen, "%s : %u\n", command_str[i], ldb.ntotal);
fldarr[fldcnt].value = hdrptr;
hdrptr += hdrlen;
fldcnt++;
/* body */
if (ldb.ntotal != 0) {
fldarr[fldcnt].length = ldb.offset;
fldarr[fldcnt].value = ldb.data;
fldcnt++;
bytes += lqdetect.buffer[i].offset;
}
if ((result.value = (char*)malloc(bytes)) != NULL) {
char *nxt = result.value;
for (int i = 0; i < LQ_CMD_NUM; i++) {
struct lq_detect_buffer ldb = lqdetect.buffer[i];
nxt += snprintf(nxt, bytes - (nxt - result.value), "%s : %u\n", command_str[i], ldb.ntotal);
if (ldb.ntotal != 0) {
nxt += snprintf(nxt, ldb.offset + 1, "%s", ldb.data);
}
}
result.length = nxt - result.value;
}
lqdetect.refcount++;
pthread_mutex_unlock(&lqdetect.lock);
*size = fldcnt;
return fldarr;
}

void lqdetect_result_release(field_t *results)
{
free(results);
pthread_mutex_lock(&lqdetect.lock);
lqdetect.refcount--;
pthread_mutex_unlock(&lqdetect.lock);
return result;
}

void lqdetect_lop_insert(char *client_ip, char *key, int coll_index)
Expand Down
3 changes: 1 addition & 2 deletions lqdetect.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ extern bool lqdetect_in_use;

int lqdetect_init(EXTENSION_LOGGER_DESCRIPTOR *logger);
void lqdetect_final(void);
field_t *lqdetect_result_get(int *size);
void lqdetect_result_release(field_t *results);
field_t lqdetect_result_get(void);
int lqdetect_start(uint32_t threshold, bool *already_started);
void lqdetect_stop(bool *already_stopped);
char *lqdetect_stats(void);
Expand Down
42 changes: 5 additions & 37 deletions memcached.c
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ conn *conn_new(const int sfd, STATE_FUNC init_state,
c->conn_next = NULL;

#ifdef DETECT_LONG_QUERY
c->lq_result = NULL;
c->lq_result = false;
#endif

c->write_and_go = init_state;
Expand Down Expand Up @@ -833,13 +833,6 @@ static void conn_cleanup(conn *c)
c->item = 0;
}

#ifdef DETECT_LONG_QUERY
if (c->lq_result) {
lqdetect_result_release(c->lq_result);
c->lq_result = NULL;
}
#endif

if (c->coll_eitem != NULL) {
conn_coll_eitem_free(c);
c->coll_eitem = NULL;
Expand Down Expand Up @@ -7580,12 +7573,6 @@ static void reset_cmd_handler(conn *c)
mc_engine.v1->release(mc_engine.v0, c, c->item);
c->item = NULL;
}
#ifdef DETECT_LONG_QUERY
if (c->lq_result) {
lqdetect_result_release(c->lq_result);
c->lq_result = NULL;
}
#endif
if (c->coll_eitem != NULL) {
conn_coll_eitem_free(c);
c->coll_eitem = NULL;
Expand Down Expand Up @@ -10024,25 +10011,12 @@ static void process_lqdetect_command(conn *c, token_t *tokens, size_t ntokens)
out_string(c, "\tlong query detection stopped.\n");
}
} else if (ntokens > 2 && strcmp(type, "show") == 0) {
int size, i;
c->lq_result = lqdetect_result_get(&size);
if (c->lq_result == NULL) {
out_string(c, "SERVER ERROR out of memory");
return;
}
for (i = 0; i < size; i++) {
if (add_iov(c, c->lq_result[i].value, c->lq_result[i].length) != 0) {
break;
}
}
if (i < size) {
lqdetect_result_release(c->lq_result);
c->lq_result = NULL;
field_t fld = lqdetect_result_get();
if (fld.value) {
write_and_free(c, fld.value, fld.length);
} else {
out_string(c, "SERVER ERROR out of memory writing show response");
return;
}
conn_set_state(c, conn_mwrite);
c->msgcurr = 0;
} else if (ntokens > 2 && strcmp(type, "stats") == 0) {
char *str = lqdetect_stats();
if (str) {
Expand Down Expand Up @@ -13953,12 +13927,6 @@ bool conn_mwrite(conn *c)
c->suffixcurr++;
c->suffixleft--;
}
#ifdef DETECT_LONG_QUERY
if (c->lq_result) {
lqdetect_result_release(c->lq_result);
c->lq_result = NULL;
}
#endif
if (c->coll_eitem != NULL) {
conn_coll_eitem_free(c);
c->coll_eitem = NULL;
Expand Down
2 changes: 1 addition & 1 deletion memcached.h
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ struct conn {
int suffixleft;

#ifdef DETECT_LONG_QUERY
field_t *lq_result;
bool lq_result;
#endif

enum protocol protocol; /* which protocol this connection speaks */
Expand Down

0 comments on commit 0dcdf02

Please sign in to comment.