Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INTERNAL: Refactor lqdetect show #780

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 16 additions & 41 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)
char *lqdetect_result_get(int *size)
cheesecrust marked this conversation as resolved.
Show resolved Hide resolved
{
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 offset = 0;
int length = 32 * LQ_CMD_NUM; // header length
char *str;

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++;
length += lqdetect.buffer[i].offset;
}
str = (char*)malloc(length);
if (str != NULL) {
for (int i = 0; i < LQ_CMD_NUM; i++) {
struct lq_detect_buffer *ldb = &lqdetect.buffer[i];
offset += snprintf(str + offset, length - offset, "%s : %u\n", command_str[i], ldb->ntotal);
if (ldb->ntotal > 0) {
offset += snprintf(str + offset, length - offset, "%s", ldb->data);
}
}
}
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);
*size = offset;
return str;
}

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);
char *lqdetect_result_get(int *size);
int lqdetect_start(uint32_t threshold, bool *already_started);
void lqdetect_stop(bool *already_stopped);
char *lqdetect_stats(void);
Expand Down
45 changes: 5 additions & 40 deletions memcached.c
Original file line number Diff line number Diff line change
Expand Up @@ -668,10 +668,6 @@ conn *conn_new(const int sfd, STATE_FUNC init_state,
c->conn_prev = NULL;
c->conn_next = NULL;

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

c->write_and_go = init_state;
c->write_and_free = 0;
c->item = 0;
Expand Down Expand Up @@ -833,13 +829,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 +7569,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 @@ -10025,25 +10008,13 @@ 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;
int size;
char *str = lqdetect_result_get(&size);
if (str) {
write_and_free(c, str, size);
} 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 @@ -13954,12 +13925,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
4 changes: 0 additions & 4 deletions memcached.h
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,6 @@ struct conn {
char **suffixcurr;
int suffixleft;

#ifdef DETECT_LONG_QUERY
field_t *lq_result;
#endif

enum protocol protocol; /* which protocol this connection speaks */
enum network_transport transport; /* what transport is used by this connection */

Expand Down
Loading