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

FIX: Fix do_command_dupcheck method that didn't show 0 at same key #794

Merged
merged 1 commit into from
Oct 29, 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
29 changes: 14 additions & 15 deletions lqdetect.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ struct lq_detect_global {
};
struct lq_detect_global lqdetect;

static bool do_command_dupcheck(char *key, enum lq_detect_command cmd, struct lq_detect_argument *arg)
static bool is_command_duplicated(char *key, int keylen, enum lq_detect_command cmd, struct lq_detect_argument *arg)
{
int count = lqdetect.buffer[cmd].nsaved;
struct lq_detect_buffer *buf = &lqdetect.buffer[cmd];

switch (cmd) {
case LQCMD_LOP_INSERT:
Expand All @@ -102,15 +103,10 @@ static bool do_command_dupcheck(char *key, enum lq_detect_command cmd, struct lq
case LQCMD_MOP_GET:
case LQCMD_SOP_GET:
for (int ii = 0; ii < count; ii++) {
if (arg->count == 0) {
uint32_t offset = lqdetect.buffer[cmd].keypos[ii];
uint32_t cmplen = lqdetect.buffer[cmd].keylen[ii];
if (cmplen == strlen(key) &&
memcmp(lqdetect.buffer[cmd].data+offset, key, cmplen) == 0) {
cheesecrust marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
} else {
if (strcmp(lqdetect.arg[cmd][ii].query, arg->query) == 0) {
if (strcmp(lqdetect.arg[cmd][ii].query, arg->query) == 0) {
if (arg->count > 0) return true;
if (buf->keylen[ii] == keylen &&
memcmp(buf->data + buf->keypos[ii], key, keylen) == 0) {
return true;
}
}
Expand All @@ -131,11 +127,15 @@ static void do_lqdetect_write(char *client_ip, char *key,
char *bufptr = buffer->data + buffer->offset;
uint32_t nwrite, length, keylen = strlen(key);
char keybuf[251];
char *keyptr = key;

if (keylen > 250) { /* long key string */
keylen = snprintf(keybuf, sizeof(keybuf), "%.*s...%.*s", 124, key, 123, (key+(keylen - 123)));
} else { /* short key string */
keylen = snprintf(keybuf, sizeof(keybuf), "%s", key);
keyptr = keybuf;
}

if (is_command_duplicated(keyptr, keylen, cmd, arg) == true) {
return;
}

gettimeofday(&val, NULL);
Expand All @@ -152,7 +152,7 @@ static void do_lqdetect_write(char *client_ip, char *key,
length -= nwrite;
bufptr += nwrite;

snprintf(bufptr, length, "%s %s\n", keybuf, arg->query);
snprintf(bufptr, length, "%s %s\n", keyptr, arg->query);
nwrite += strlen(bufptr);
buffer->offset += nwrite;
lqdetect.arg[cmd][nsaved] = *arg;
Expand All @@ -175,8 +175,7 @@ static void do_lqdetect_save_cmd(char *client_ip, char* key,
pthread_mutex_lock(&lqdetect.lock);
if (lqdetect_in_use) {
lqdetect.buffer[cmd].ntotal++;
if (lqdetect.buffer[cmd].nsaved < LQ_SAVE_CNT &&
do_command_dupcheck(key, cmd, arg) == false) {
if (lqdetect.buffer[cmd].nsaved < LQ_SAVE_CNT) {
/* write to buffer */
do_lqdetect_write(client_ip, key, cmd, arg);
/* internal stop */
Expand Down
70 changes: 69 additions & 1 deletion t/long_query_detect_issue.t
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/perl

use strict;
use Test::More tests => 107;
use Test::More tests => 125;
use FindBin qw($Bin);
use lib "$Bin/lib";
use MemcachedTest;
Expand Down Expand Up @@ -102,6 +102,63 @@ sub do_btree_efilter {
mem_cmd_is($sock, $cmd, "", $rst);
}

sub do_sop_prepare {
my $long_key = "A" x 251;

mem_cmd_is($sock, "sop insert skey1 5 create 0 0 5", "datum", "CREATED_STORED");
mem_cmd_is($sock, "sop insert skey2 5 create 0 0 5", "datum", "CREATED_STORED");
mem_cmd_is($sock, "sop insert $long_key 5 create 0 0 5", "datum", "CREATED_STORED");
}

sub do_sop_get {
my $idx;
my $long_key = "A" x 251;

$rst = "VALUE 0 1\n"
. "5 datum\n"
. "END";
for ($idx = 1; $idx < 3; $idx += 1) {
mem_cmd_is($sock, "sop get skey1 $idx", "", $rst);
}
mem_cmd_is($sock, "sop get skey1 0", "", $rst);
for ($idx = 0; $idx < 3; $idx += 1) {
mem_cmd_is($sock, "sop get skey2 $idx", "", $rst);
}
$rst = "VALUE 0 1\n"
. "5 datum\n"
. "END";
mem_cmd_is($sock, "sop get $long_key 0", "", $rst);
mem_cmd_is($sock, "sop get $long_key 0", "", $rst);
}

sub do_lqdetect_show {
my $idx;
my $substring;
my $line;
my $long_key = "A" x 124 + "..." + "A" x 123;

print $sock "lqdetect show\r\n";
$line = scalar <$sock>;
print $line;
for ($idx = 1; $idx < 3; $idx += 1) {
$line = scalar <$sock>;
$substring = "sop get skey1 $idx";
like($line, qr/$substring/, "lqdetect show $substring");
}
$line = scalar <$sock>;
$substring = "sop get skey1 0";
like($line, qr/$substring/, "lqdetect show $substring");
$line = scalar <$sock>;
$substring = "sop get skey2 0";
like($line, qr/$substring/, "lqdetect show $substring");
$line = scalar <$sock>;
$substring = "sop get "+ $long_key + " 0";
like($line, qr/$substring/, "lqdetect show $substring");
$line = scalar <$sock>;
$substring = "mop delete : 0";
like($line, qr/$substring/, "lqdetect show $substring");
}

# do test
my $key = "AA";
my $line;
Expand All @@ -116,5 +173,16 @@ $line = scalar <$sock>;
$line = scalar <$sock>;
mem_cmd_is($sock, "delete $key", "", "DELETED");

$rst = "long query detection started";
print $sock "lqdetect start 1\r\n";
$line = scalar <$sock>;
like($line, qr/$rst/, "lqdetect start");
$line = scalar <$sock>;
do_sop_prepare();
do_sop_get();
do_lqdetect_show();
print $sock "lqdetect stop\r\n";
$line = scalar <$sock>;
$line = scalar <$sock>;
cheesecrust marked this conversation as resolved.
Show resolved Hide resolved
# after test
release_memcached($engine, $server);
Loading