From cee2ba9b67982e3a451ad75562ad451509d14916 Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 3 Jun 2020 16:47:54 +0200 Subject: TCC: DEBUG LOCK implemented. --- src/debug.c | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/debug.c b/src/debug.c index d79226bf2..4e69e6a75 100644 --- a/src/debug.c +++ b/src/debug.c @@ -367,6 +367,14 @@ void mallctl_string(client *c, robj **argv, int argc) { } #endif +/* Threaded half of the DEBUG LOCK command. */ +void debugLockThreadedHalf(client *c, void *opt) { + mstime_t sleeptime = (unsigned long)opt; + usleep(sleeptime*1000); + addReplyStatusFormat(c,"Slept for %lu milliseconds", + (unsigned long)sleeptime); +} + void debugCommand(client *c) { if (c->argc == 2 && !strcasecmp(c->argv[1]->ptr,"help")) { const char *help[] = { @@ -396,6 +404,7 @@ void debugCommand(client *c) { "STRUCTSIZE -- Return the size of different Redis core C structures.", "ZIPLIST -- Show low level info about the ziplist encoding.", "STRINGMATCH-TEST -- Run a fuzz tester against the stringmatchlen() function.", +"LOCK ... -- Lock the specified keys for the specified amount of milliseconds. Lock type should be read or write.", #ifdef USE_JEMALLOC "MALLCTL [] -- Get or set a malloc tunning integer.", "MALLCTL-STR [] -- Get or set a malloc tunning string.", @@ -791,11 +800,33 @@ NULL { stringmatchlen_fuzz_test(); addReplyStatus(c,"Apparently Redis did not crash: test passed"); + } else if (!strcasecmp(c->argv[1]->ptr,"lock") && c->argc >= 5) { + /* DEBUG LOCK ... keys ... */ + long ms; + if (getLongFromObjectOrReply(c,c->argv[2],&ms,NULL) != C_OK) return; + if (!strcasecmp(c->argv[3]->ptr,"read")) { + /* The only one supported so far... */ + } else { + addReplyError(c,"DEBUG LOCK only supports read locks so far."); + } + /* Try locking all the keys. */ + for (int j = 4; j < c->argc; j++) { + robj *myobj; + if (lockKey(c,c->argv[j],LOCKEDKEY_READ,&myobj) == C_ERR) { + unlockAllKeys(c); + addReplyErrorFormat(c,"Unable to lock %s. Already locked?", + c->argv[j]->ptr); + return; + } + } + /* Pass control to the threaded part. */ + executeThreadedCommand(c,debugLockThreadedHalf, + (void*)(unsigned long)ms); #ifdef USE_JEMALLOC - } else if(!strcasecmp(c->argv[1]->ptr,"mallctl") && c->argc >= 3) { + } else if (!strcasecmp(c->argv[1]->ptr,"mallctl") && c->argc >= 3) { mallctl_int(c, c->argv+2, c->argc-2); return; - } else if(!strcasecmp(c->argv[1]->ptr,"mallctl-str") && c->argc >= 3) { + } else if (!strcasecmp(c->argv[1]->ptr,"mallctl-str") && c->argc >= 3) { mallctl_string(c, c->argv+2, c->argc-2); return; #endif -- cgit v1.2.1