From db7bd65b1c13991bd44be65c448993fd032e56df Mon Sep 17 00:00:00 2001 From: cheesecrust Date: Thu, 24 Oct 2024 15:58:25 +0900 Subject: [PATCH] FIX: Fix do_command_dupcheck method that didn't show 0 at same key --- lqdetect.c | 19 ++++++---- t/long_query_detect_issue.t | 70 ++++++++++++++++++++++++++++++++++++- 2 files changed, 81 insertions(+), 8 deletions(-) diff --git a/lqdetect.c b/lqdetect.c index 9083759e1..4823a2614 100644 --- a/lqdetect.c +++ b/lqdetect.c @@ -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: @@ -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; } } diff --git a/t/long_query_detect_issue.t b/t/long_query_detect_issue.t index 1a0279ca4..195081c80 100644 --- a/t/long_query_detect_issue.t +++ b/t/long_query_detect_issue.t @@ -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; @@ -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; @@ -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);