Skip to content

Commit

Permalink
1.fix parse error for multi-key command with single key
Browse files Browse the repository at this point in the history
2.correct some commands property
3.add test cases
  • Loading branch information
fortrue committed Sep 3, 2017
1 parent aeedfae commit 017ae64
Show file tree
Hide file tree
Showing 9 changed files with 721 additions and 30 deletions.
16 changes: 8 additions & 8 deletions src/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ const Command Command::CmdPool[Sentinel] = {
{Exec, "exec", 1, 1, Write|NoKey},
{Discard, "discard", 1, 1, Write|NoKey},
{DiscardServ, "discard", 1, 1, Inner|NoKey},
{Eval, "eval", 4, MaxArgs, Write|KeyAt3},
{Evalsha, "evalsha", 4, MaxArgs, Write|KeyAt3},
{Eval, "eval", 3, MaxArgs, Write|KeyAt3},
{Evalsha, "evalsha", 3, MaxArgs, Write|KeyAt3},
{Script, "script", 2, MaxArgs, Write},
{ScriptLoad, "script", 3, 3, Write|SubCmd},
{Del, "del", 2, MaxArgs, Write|MultiKey},
Expand All @@ -54,7 +54,7 @@ const Command Command::CmdPool[Sentinel] = {
{Rename, "rename", 3, 3, Write},
{Renamenx, "renamenx", 3, 3, Write},
{Restore, "restore", 4, 5, Write},
{Sort, "sort", 2, MaxArgs, Read},
{Sort, "sort", 2, MaxArgs, Write},
{Touch, "touch", 2, MaxArgs, Write|MultiKey},
{Ttl, "ttl", 2, 2, Read},
{TypeCmd, "type", 2, 2, Read},
Expand All @@ -63,7 +63,7 @@ const Command Command::CmdPool[Sentinel] = {
{Append, "append", 3, 3, Write},
{Bitcount, "bitcount", 2, 4, Read},
{Bitfield, "bitfield", 2, MaxArgs, Write},
{Bitop, "bitop", 4, MaxArgs, Write},
{Bitop, "bitop", 4, MaxArgs, Write|KeyAt2},
{Bitpos, "bitpos", 3, 5, Read},
{Decr, "decr", 2, 2, Write},
{Decrby, "decrby", 3, 3, Write},
Expand Down Expand Up @@ -159,14 +159,14 @@ const Command Command::CmdPool[Sentinel] = {
{Geodist, "geodist", 4, 5, Read},
{Geohash, "geohash", 3, MaxArgs, Read},
{Geopos, "geopos", 3, MaxArgs, Read},
{Georadius, "georadius", 6, 16, Read},
{Georadiusbymember, "georadiusbymember",5, 15, Read},
{Georadius, "georadius", 6, 16, Write},
{Georadiusbymember, "georadiusbymember",5, 15, Write},
{Psubscribe, "psubscribe", 2, MaxArgs, Write|SMultiKey|Private},
{Publish, "publish", 3, 3, Write},
{Pubsub, "pubsub", 2, MaxArgs, Read},
{Punsubscribe, "punsubscribe", 1, MaxArgs, Write},
{Punsubscribe, "punsubscribe", 1, MaxArgs, Write|SMultiKey},
{Subscribe, "subscribe", 2, MaxArgs, Write|SMultiKey|Private},
{Unsubscribe, "unsubscribe", 1, MaxArgs, Write},
{Unsubscribe, "unsubscribe", 1, MaxArgs, Write|SMultiKey},
{SubMsg, "\000SubMsg", 0, 0, Admin}
};

Expand Down
9 changes: 5 additions & 4 deletions src/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,13 @@ class Command
MultiKey = 1<<5,
SMultiKey = 1<<6,
MultiKeyVal = 1<<7,
KeyAt3 = 1<<8,
SubCmd = 1<<9,
Inner = 1<<10 //proxy use only
KeyAt2 = 1<<8,
KeyAt3 = 1<<9,
SubCmd = 1<<10,
Inner = 1<<11 //proxy use only
};
static const int AuthMask = Read|Write|Admin;
static const int KeyMask = NoKey|MultiKey|SMultiKey|MultiKeyVal|KeyAt3;
static const int KeyMask = NoKey|MultiKey|SMultiKey|MultiKeyVal|KeyAt2|KeyAt3;
public:
Type type;
const char* name;
Expand Down
8 changes: 4 additions & 4 deletions src/Handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -710,11 +710,11 @@ void Handler::directResponse(Request* req, Response::GenericCode code, ConnectCo
id(), c->peer(), c->fd(), req->id(), code, excp.what());
}
} else {
logInfo("h %d ignore req %ld res code %d c %s %d status %d %s",
logDebug("h %d ignore req %ld res code %d c %s %d status %d %s",
id(), req->id(), code, c->peer(), c->fd(), c->status(), c->statusStr());
}
} else {
logInfo("h %d ignore req %ld res code %d without accept connection",
logDebug("h %d ignore req %ld res code %d without accept connection",
id(), req->id(), code);
}
}
Expand All @@ -741,7 +741,7 @@ void Handler::handleResponse(ConnectConnection* s, Request* req, Response* res)
auto sp = mProxy->serverPool();
AcceptConnection* c = req->connection();
if (!c) {
logInfo("h %d ignore req %ld res %ld", id(), req->id(), res->id());
logDebug("h %d ignore req %ld res %ld", id(), req->id(), res->id());
return;
} else if (!c->good()) {
logWarn("h %d ignore req %ld res %ld for c %s %d with status %d %s",
Expand Down Expand Up @@ -1288,7 +1288,7 @@ void Handler::configSetRequest(Request* req)

void Handler::innerResponse(ConnectConnection* s, Request* req, Response* res)
{
logInfo("h %d s %s %d inner req %ld %s res %ld %s",
logDebug("h %d s %s %d inner req %ld %s res %ld %s",
id(), (s ? s->peer() : "None"), (s ? s->fd() : -1),
req->id(), req->cmd(),
res->id(), res->typeStr());
Expand Down
22 changes: 21 additions & 1 deletion src/Request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ static const GenericRequest GenericRequestDefs[] = {
{Request::DelHead, Command::Del, "*2\r\n$3\r\ndel\r\n"},
{Request::UnlinkHead, Command::Unlink, "*2\r\n$6\r\nunlink\r\n"},
{Request::PsubscribeHead,Command::Psubscribe, "*2\r\n$10\r\npsubscribe\r\n"},
{Request::SubscribeHead,Command::Subscribe, "*2\r\n$9\r\nsubscribe\r\n"}
{Request::SubscribeHead,Command::Subscribe, "*2\r\n$9\r\nsubscribe\r\n"},
{Request::PunsubscribeHead,Command::Punsubscribe, "*2\r\n$12\r\npunsubscribe\r\n"},
{Request::UnsubscribeHead,Command::Unsubscribe, "*2\r\n$11\r\nunsubscribe\r\n"}
};

thread_local static Request* GenericRequests[Request::CodeSentinel];
Expand Down Expand Up @@ -137,8 +139,15 @@ void Request::set(const RequestParser& p, Request* leader)
case Command::Subscribe:
r = GenericRequests[SubscribeHead];
break;
case Command::Punsubscribe:
r = GenericRequests[PunsubscribeHead];
break;
case Command::Unsubscribe:
r = GenericRequests[UnsubscribeHead];
break;
default:
//should never reach
abort();
break;
}
mHead = r->mReq;
Expand Down Expand Up @@ -341,6 +350,15 @@ void Request::setResponse(Response* res)
}
break;
case Command::Msetnx:
if (Response* leaderRes = mLeader->getResponse()) {
if (!leaderRes->isError() &&
(res->isError() || res->integer() == 0)) {
mLeader->mRes = res;
}
} else {
mLeader->mRes = res;
}
break;
case Command::Touch:
case Command::Exists:
case Command::Del:
Expand Down Expand Up @@ -378,6 +396,8 @@ bool Request::isDone() const
case Command::Mget:
case Command::Psubscribe:
case Command::Subscribe:
case Command::Punsubscribe:
case Command::Unsubscribe:
return mDone;
default:
break;
Expand Down
2 changes: 2 additions & 0 deletions src/Request.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class Request :
UnlinkHead,
PsubscribeHead,
SubscribeHead,
PunsubscribeHead,
UnsubscribeHead,

CodeSentinel
};
Expand Down
10 changes: 7 additions & 3 deletions src/RequestParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ inline bool RequestParser::isKey(bool split) const
return mArgCnt > 0;
case Command::MultiKeyVal:
return split ? (mArgCnt & 1) : mArgCnt == 1;
case Command::KeyAt2:
return mArgCnt == 2;
case Command::KeyAt3:
return mArgCnt == 3;
default:
Expand All @@ -53,10 +55,12 @@ inline bool RequestParser::isKey(bool split) const

inline bool RequestParser::isSplit(bool split) const
{
if (mCommand->mode & (Command::MultiKey|Command::MultiKeyVal)) {
return split && mStatus == Normal && isKey(true);
if (mCommand->mode & Command::MultiKey) {
return split && mStatus == Normal && mArgNum > 2 && isKey(true);
} else if (mCommand->mode & Command::MultiKeyVal) {
return split && mStatus == Normal && mArgNum > 3 && isKey(true);
} else if (mCommand->mode & Command::SMultiKey) {
return mStatus == Normal;
return mStatus == Normal && mArgNum > 2;
}
return false;
}
Expand Down
20 changes: 10 additions & 10 deletions src/RequestParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ class RequestParser
KeyLenLF,
KeyBody,
KeyBodyLF,
ArgTag, // $ $
ArgLen, // 3 5
ArgLenLF, // \r\n \r\n
ArgBody, // get hello
ArgBodyLF, // \r\n \r\n
SArgTag, // $ $
SArgLen, // 3 5
SArgLenLF, // \r\n \r\n
SArgBody, // get hello
SArgBodyLF, // \r\n \r\n
ArgTag,
ArgLen,
ArgLenLF,
ArgBody,
ArgBodyLF,
SArgTag,
SArgLen,
SArgLenLF,
SArgBody,
SArgBodyLF,
Finished,

Error
Expand Down
Loading

0 comments on commit 017ae64

Please sign in to comment.