Skip to content

Commit

Permalink
improve performance
Browse files Browse the repository at this point in the history
fix scan for sentinel groups
  • Loading branch information
joyield committed Jul 30, 2017
1 parent 9d86d04 commit ecc63e5
Show file tree
Hide file tree
Showing 28 changed files with 367 additions and 215 deletions.
4 changes: 2 additions & 2 deletions conf/auth.conf
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# Auth bcd {
# Mode admin
# }
#### password is "abc", the client must send command Auth bcd
#### password is "bcd", the client must send command Auth bcd
#### Mode admin, client connection can read and write and admin,
#### the CONFIG command need admin permission
#### No KeyPrefix, ReadKeyPrefix, WriteKeyPrefix define, all key can be visit
Expand All @@ -47,7 +47,7 @@
# ReadKeyPrefix User Stats
# WriteKeyPrefix User
# }
#### password is "cde", the client must send command Auth cde
#### password is "def", the client must send command Auth def
#### Mode read, client connection can read and write, but read and write
#### keyspace is diffrent, client can GET User.123 and also
#### SET User.123 SomeValue, but SET Stats.123 will be deny
Expand Down
4 changes: 2 additions & 2 deletions conf/latency.conf
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
## see latency monitor for specify server
## redis> INFO ServerLatency ServAddr [name]
##
## reset all stats info, include latency monitor
## redis> INFO ResetStats
## reset all stats info, include latency monitor, require admin permission
## redis> CONFIG ResetStat
##
## Examples:
## LatencyMonitor name {
Expand Down
5 changes: 2 additions & 3 deletions src/AcceptConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,10 @@ void AcceptConnection::parse(Handler* h, Buffer* buf, int pos)
req->set(mParser);
h->handleRequest(req);
} else {
SegmentStr<RequestParser::MaxCmdLen> cmd(mParser.cmd());
ResponsePtr res = ResponseAlloc::create();
char err[1024];
int len = snprintf(err, sizeof(err), "unknown command '%.*s'",
cmd.length(), cmd.data());
int len = snprintf(err, sizeof(err), "unknown command '%s'",
mParser.cmd());
res->setErr(err, len);
h->handleResponse(nullptr, req, res);
}
Expand Down
2 changes: 1 addition & 1 deletion src/AcceptConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class AcceptConnection :
public Subscribe,
public ListNode<AcceptConnection, SharePtr<AcceptConnection>>,
public DequeNode<AcceptConnection, SharePtr<AcceptConnection>>,
public RefCntObj<AcceptConnection>
public RefCntObj<AcceptConnection, AtomicInt>
{
public:
typedef AcceptConnection Value;
Expand Down
5 changes: 3 additions & 2 deletions src/Alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ thread_local T* Alloc<T, CacheSize>::Free[CacheSize];
template<class T, int CacheSize>
thread_local int Alloc<T, CacheSize>::Size = 0;

template<class T>
template<class T, class CntType = int>
class RefCntObj
{
public:
Expand All @@ -138,6 +138,7 @@ class RefCntObj
}
void ref()
{
FuncCallTimer();
++mCnt;
}
void unref()
Expand All @@ -156,7 +157,7 @@ class RefCntObj
mCnt = 0;
}
private:
AtomicInt mCnt;
CntType mCnt;
};

template<class T>
Expand Down
2 changes: 1 addition & 1 deletion src/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class Segment
}
bool empty() const
{
return mCur.buf == mEnd.buf && mCur.pos == mEnd.pos;
return mBegin.buf == mEnd.buf && mBegin.pos == mEnd.pos;
}
private:
BufferPos mBegin;
Expand Down
3 changes: 1 addition & 2 deletions src/ClusterServerPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ClusterServerPool::~ClusterServerPool()
}
}

Server* ClusterServerPool::getServer(Handler* h, Request* req) const
Server* ClusterServerPool::getServer(Handler* h, Request* req, const String& key) const
{
FuncCallTimer();
switch (req->type()) {
Expand All @@ -43,7 +43,6 @@ Server* ClusterServerPool::getServer(Handler* h, Request* req) const
default:
break;
}
SegmentStr<Const::MaxKeyLen> key(req->key());
int i = mHash.hash(key.data(), key.length(), HashTag);
i &= Const::RedisClusterSlotsMask;
ServerGroup* g = mSlots[i];
Expand Down
2 changes: 1 addition & 1 deletion src/ClusterServerPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ClusterServerPool : public ServerPoolTmpl<ClusterServerPool>
return mServPool;
}
private:
Server* getServer(Handler* h, Request* req) const;
Server* getServer(Handler* h, Request* req, const String& key) const;
void refreshRequest(Handler* h);
void handleResponse(Handler* h, ConnectConnection* s, Request* req, Response* res);
ServerGroup* getGroup(const String& nodeid) const
Expand Down
5 changes: 2 additions & 3 deletions src/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "Command.h"

const Command Command::CmdPool[Sentinel] = {
{None, "", 0, 0, Read},
{None, "", 0, MaxArgs, Read},
{Ping, "ping", 1, 2, Read},
{PingServ, "ping", 1, 2, Inner},
{Echo, "echo", 2, 2, Read},
Expand All @@ -24,7 +24,7 @@ const Command Command::CmdPool[Sentinel] = {
{SentinelSlaves, "sentinel slaves", 3, 3, Inner},
{Cmd, "command", 1, 1, Read},
{Info, "info", 1, 4, Read},
{Config, "config", 3, 4, Admin},
{Config, "config", 2, 4, Admin},
{Cluster, "cluster", 2, 2, Inner},
{ClusterNodes, "cluster nodes", 2, 2, SubCmd|Inner},
{Asking, "asking", 1, 1, Inner},
Expand Down Expand Up @@ -171,7 +171,6 @@ const Command Command::CmdPool[Sentinel] = {
};

Command::CommandMap Command::CmdMap;

void Command::init()
{
int type = 0;
Expand Down
16 changes: 15 additions & 1 deletion src/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#ifndef _PREDIXY_COMMAND_H_
#define _PREDIXY_COMMAND_H_

#include <unordered_map>
#include "Exception.h"
#include "HashFunc.h"

class Command
{
Expand Down Expand Up @@ -224,6 +226,10 @@ class Command
{
return mode & MultiKeyVal;
}
bool isAnyMulti() const
{
return mode & (MultiKey|SMultiKey|MultiKeyVal);
}
static void init();
static const Command& get(Type type)
{
Expand All @@ -244,7 +250,15 @@ class Command
private:
static const int MaxArgs = 100000000;
static const Command CmdPool[Sentinel];
typedef std::map<String, const Command*, StringCaseCmp> CommandMap;
class H
{
public:
size_t operator()(const String& s) const
{
return Hash::crc16(s.data(), s.length());
}
};
typedef std::unordered_map<String, const Command*, H> CommandMap;
static CommandMap CmdMap;
};

Expand Down
1 change: 1 addition & 0 deletions src/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace Const
static const int MaxAddrLen = 128;
static const int MaxDcLen = 32;
static const int MaxIOVecLen = IOV_MAX;
static const int MaxCmdLen = 32;
static const int MaxKeyLen = 512;
static const int BufferAllocCacheSize = 64;
static const int RequestAllocCacheSize = 32;
Expand Down
8 changes: 0 additions & 8 deletions src/ConnectConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,6 @@ void ConnectConnection::handleResponse(Handler* h)
}
}

void ConnectConnection::send(Handler* h, Request* req)
{
FuncCallTimer();
mSendRequests.push_back(req);
logDebug("h %d s %s %d pend req %ld",
h->id(), peer(), fd(), req->id());
}

void ConnectConnection::close(Handler* h)
{
SendRequestList* reqs[2] = {&mSentRequests, &mSendRequests};
Expand Down
5 changes: 4 additions & 1 deletion src/ConnectConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ class ConnectConnection :
~ConnectConnection();
bool writeEvent(Handler* h);
void readEvent(Handler* h);
void send(Handler* h, Request* req);
void close(Handler* h);
void send(Handler* h, Request* req)
{
mSendRequests.push_back(req);
}
Server* server() const
{
return mServ;
Expand Down
19 changes: 12 additions & 7 deletions src/Handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Handler::~Handler()

void Handler::run()
{
Request::init();
Response::init();
auto conf = mProxy->conf();
refreshServerPool();
while (!mStop) {
Expand Down Expand Up @@ -134,7 +136,7 @@ void Handler::postEvent()
}
}

void Handler::addPostEvent(AcceptConnection* c, int evts)
inline void Handler::addPostEvent(AcceptConnection* c, int evts)
{
if (!c->getPostEvent()) {
mPostAcceptConns.push_back(c);
Expand All @@ -143,12 +145,12 @@ void Handler::addPostEvent(AcceptConnection* c, int evts)
c->addPostEvent(evts);
}

void Handler::addPostEvent(ConnectConnection* c, int evts)
inline void Handler::addPostEvent(ConnectConnection* s, int evts)
{
if (!c->getPostEvent()) {
mPostConnectConns.push_back(c);
if (!s->getPostEvent()) {
mPostConnectConns.push_back(s);
}
c->addPostEvent(evts);
s->addPostEvent(evts);
}

void Handler::postAcceptConnectionEvent()
Expand Down Expand Up @@ -353,7 +355,6 @@ void Handler::handleAcceptConnectionEvent(AcceptConnection* c, int evts)
try {
if (c->good() && (evts & Multiplexor::ReadEvent)) {
c->readEvent(this);
setAcceptConnectionActiveTime(c);
}
if (c->good() && (evts & Multiplexor::WriteEvent)) {
addPostEvent(c, Multiplexor::WriteEvent);
Expand Down Expand Up @@ -483,7 +484,7 @@ void Handler::handleRequest(Request* req)
return;
}
auto sp = mProxy->serverPool();
Server* serv = sp->getServer(this, req);
Server* serv = sp->getServer(this, req, key);
if (!serv) {
directResponse(req, Response::NoServer);
return;
Expand Down Expand Up @@ -634,6 +635,7 @@ bool Handler::preHandleRequest(Request* req, const String& key)

void Handler::postHandleRequest(Request* req, ConnectConnection* s)
{
FuncCallTimer();
auto c = req->connection();
if (!c) {
return;
Expand Down Expand Up @@ -1127,6 +1129,9 @@ void Handler::configRequest(Request* req, const String& key)
configGetRequest(req);
} else if (key.equal("set", true)) {
configSetRequest(req);
} else if (key.equal("resetstat", true)) {
mProxy->incrStatsVer();
directResponse(req, Response::Ok);
} else {
directResponse(req, Response::ConfigSubCmdUnknown);
}
Expand Down
13 changes: 0 additions & 13 deletions src/HashFunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,6 @@ const char* Hash::hashTagStr(const char* buf, int& len, const char* tag)
return buf;
}

long Hash::hash(const char* buf, int len) const
{
switch (mType) {
case Atol:
return atol(buf, len);
case Crc16:
return crc16(buf, len);
default:
break;
}
return 0;
}

long Hash::atol(const char* buf, int len)
{
long v = 0;
Expand Down
13 changes: 12 additions & 1 deletion src/HashFunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,18 @@ class Hash
{
return mType;
}
long hash(const char* buf, int len) const;
long hash(const char* buf, int len) const
{
switch (mType) {
case Atol:
return atol(buf, len);
case Crc16:
return crc16(buf, len);
default:
break;
}
return 0;
}
long hash(const char* buf, int len, const char* tag) const
{
buf = hashTagStr(buf, len, tag);
Expand Down
2 changes: 0 additions & 2 deletions src/Proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ bool Proxy::init(int argc, char* argv[])
}

mLatencyMonitorSet.init(mConf->latencyMonitors());
Request::init();
Response::init();
ListenSocket* s = new ListenSocket(mConf->bind(), SOCK_STREAM);
if (!s->setNonBlock()) {
logError("proxy listener set nonblock fail:%s", StrError());
Expand Down
Loading

0 comments on commit ecc63e5

Please sign in to comment.