Skip to content

Commit

Permalink
FIX: Fix do_command_dupcheck method that didn't show 0 at same key
Browse files Browse the repository at this point in the history
  • Loading branch information
cheesecrust committed Oct 28, 2024
1 parent 63c8a8d commit db7bd65
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 8 deletions.
19 changes: 12 additions & 7 deletions lqdetect.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,14 @@ struct lq_detect_global lqdetect;
static bool do_command_dupcheck(char *key, enum lq_detect_command cmd, struct lq_detect_argument *arg)
{
int count = lqdetect.buffer[cmd].nsaved;
char keybuf[251];
int keylen = strlen(key);
char* keyptr = key;

if (keylen > 250) {
keylen = snprintf(keybuf, sizeof(keybuf), "%.*s...%.*s", 124, key, 123, (key+(keylen - 123)));
keyptr = keybuf;
}
switch (cmd) {
case LQCMD_LOP_INSERT:
case LQCMD_LOP_DELETE:
Expand All @@ -102,15 +109,13 @@ 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) {
if (strcmp(lqdetect.arg[cmd][ii].query, arg->query) == 0) {
if (arg->count > 0) return true;
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) {
return true;
}
} else {
if (strcmp(lqdetect.arg[cmd][ii].query, arg->query) == 0) {
if (cmplen == keylen &&
memcmp(lqdetect.buffer[cmd].data+offset, keyptr, cmplen) == 0 &&
strcmp(lqdetect.arg[cmd][ii].query, arg->query) == 0) {
return true;
}
}
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>;
# after test
release_memcached($engine, $server);

0 comments on commit db7bd65

Please sign in to comment.