Skip to content

Commit

Permalink
Rename RedisCommandHandler::Result to make related code cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesge committed Jan 16, 2020
1 parent d81ba3f commit 91a4376
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 47 deletions.
12 changes: 6 additions & 6 deletions example/redis_c++/redis_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ class GetCommandHandler : public brpc::RedisCommandHandler {
explicit GetCommandHandler(RedisServiceImpl* rsimpl)
: _rsimpl(rsimpl) {}

brpc::RedisCommandHandler::Result Run(const std::vector<const char*>& args,
brpc::RedisCommandHandlerResult Run(const std::vector<const char*>& args,
brpc::RedisReply* output,
bool /*flush_batched*/) override {
if (args.size() != 2ul) {
output->FormatError("Expect 1 arg for 'get', actually %lu", args.size()-1);
return brpc::RedisCommandHandler::OK;
return brpc::REDIS_CMD_HANDLED;
}
const std::string key(args[1]);
std::string value;
Expand All @@ -78,7 +78,7 @@ class GetCommandHandler : public brpc::RedisCommandHandler {
} else {
output->SetNullString();
}
return brpc::RedisCommandHandler::OK;
return brpc::REDIS_CMD_HANDLED;
}

private:
Expand All @@ -90,18 +90,18 @@ class SetCommandHandler : public brpc::RedisCommandHandler {
explicit SetCommandHandler(RedisServiceImpl* rsimpl)
: _rsimpl(rsimpl) {}

brpc::RedisCommandHandler::Result Run(const std::vector<const char*>& args,
brpc::RedisCommandHandlerResult Run(const std::vector<const char*>& args,
brpc::RedisReply* output,
bool /*flush_batched*/) override {
if (args.size() != 3ul) {
output->FormatError("Expect 2 args for 'set', actually %lu", args.size()-1);
return brpc::RedisCommandHandler::OK;
return brpc::REDIS_CMD_HANDLED;
}
const std::string key(args[1]);
const std::string value(args[2]);
_rsimpl->Set(key, value);
output->SetStatus("OK");
return brpc::RedisCommandHandler::OK;
return brpc::REDIS_CMD_HANDLED;
}

private:
Expand Down
16 changes: 8 additions & 8 deletions src/brpc/policy/redis_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ int ConsumeCommand(RedisConnContext* ctx,
bool flush_batched,
butil::IOBufAppender* appender) {
RedisReply output(&ctx->arena);
RedisCommandHandler::Result result = RedisCommandHandler::OK;
RedisCommandHandlerResult result = REDIS_CMD_HANDLED;
if (ctx->transaction_handler) {
result = ctx->transaction_handler->Run(commands, &output, flush_batched);
if (result == RedisCommandHandler::OK) {
if (result == REDIS_CMD_HANDLED) {
ctx->transaction_handler.reset(NULL);
} else if (result == RedisCommandHandler::BATCHED) {
} else if (result == REDIS_CMD_BATCHED) {
LOG(ERROR) << "BATCHED should not be returned by a transaction handler.";
return -1;
}
Expand All @@ -97,18 +97,18 @@ int ConsumeCommand(RedisConnContext* ctx,
output.SetError(buf);
} else {
result = ch->Run(commands, &output, flush_batched);
if (result == RedisCommandHandler::CONTINUE) {
if (result == REDIS_CMD_CONTINUE) {
if (ctx->batched_size != 0) {
LOG(ERROR) << "CONTINUE should not be returned in a batched process.";
return -1;
}
ctx->transaction_handler.reset(ch->NewTransactionHandler());
} else if (result == RedisCommandHandler::BATCHED) {
} else if (result == REDIS_CMD_BATCHED) {
ctx->batched_size++;
}
}
}
if (result == RedisCommandHandler::OK) {
if (result == REDIS_CMD_HANDLED) {
if (ctx->batched_size) {
if ((int)output.size() != (ctx->batched_size + 1)) {
LOG(ERROR) << "reply array size can't be matched with batched size, "
Expand All @@ -122,9 +122,9 @@ int ConsumeCommand(RedisConnContext* ctx,
} else {
output.SerializeTo(appender);
}
} else if (result == RedisCommandHandler::CONTINUE) {
} else if (result == REDIS_CMD_CONTINUE) {
output.SerializeTo(appender);
} else if (result == RedisCommandHandler::BATCHED) {
} else if (result == REDIS_CMD_BATCHED) {
// just do nothing and wait handler to return OK.
} else {
LOG(ERROR) << "unknown status=" << result;
Expand Down
29 changes: 15 additions & 14 deletions src/brpc/redis.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,15 @@ class RedisService {
CommandMap _command_map;
};

enum RedisCommandHandlerResult {
REDIS_CMD_HANDLED = 0,
REDIS_CMD_CONTINUE = 1,
REDIS_CMD_BATCHED = 2,
};

// The Command handler for a redis request. User should impletement Run().
class RedisCommandHandler {
public:
enum Result {
OK = 0,
CONTINUE = 1,
BATCHED = 2,
};
~RedisCommandHandler() {}

// Once Server receives commands, it will first find the corresponding handlers and
Expand All @@ -250,24 +251,24 @@ class RedisCommandHandler {
// Read brpc/src/redis_reply.h for more usage.
// `flush_batched' indicates whether the user should flush all the results of
// batched commands. If user want to do some batch processing, user should buffer
// the commands and return RedisCommandHandler::BATCHED. Once `flush_batched' is true,
// the commands and return REDIS_CMD_BATCHED. Once `flush_batched' is true,
// run all the commands, set `output' to be an array in which every element is the
// result of batched commands and return RedisCommandHandler::OK.
// result of batched commands and return REDIS_CMD_HANDLED.
//
// The return value should be RedisCommandHandler::OK for normal cases. If you want
// to implement transaction, return RedisCommandHandler::CONTINUE once server receives
// The return value should be REDIS_CMD_HANDLED for normal cases. If you want
// to implement transaction, return REDIS_CMD_CONTINUE once server receives
// an start marker and brpc will call MultiTransactionHandler() to new a transaction
// handler that all the following commands are sent to this tranction handler until
// it returns RedisCommandHandler::OK. Read the comment below.
virtual RedisCommandHandler::Result Run(const std::vector<const char*>& args,
brpc::RedisReply* output,
bool flush_batched) = 0;
// it returns REDIS_CMD_HANDLED. Read the comment below.
virtual RedisCommandHandlerResult Run(const std::vector<const char*>& args,
brpc::RedisReply* output,
bool flush_batched) = 0;

// The Run() returns CONTINUE for "multi", which makes brpc call this method to
// create a transaction_handler to process following commands until transaction_handler
// returns OK. For example, for command "multi; set k1 v1; set k2 v2; set k3 v3;
// exec":
// 1) First command is "multi" and Run() should return RedisCommandHandler::CONTINUE,
// 1) First command is "multi" and Run() should return REDIS_CMD_CONTINUE,
// then brpc calls NewTransactionHandler() to new a transaction_handler.
// 2) brpc calls transaction_handler.Run() with command "set k1 v1",
// which should return CONTINUE.
Expand Down
38 changes: 19 additions & 19 deletions test/brpc_redis_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -812,15 +812,15 @@ class RedisServiceImpl : public brpc::RedisService {
RedisServiceImpl()
: _batch_count(0) {}

brpc::RedisCommandHandler::Result OnBatched(const std::vector<const char*> args,
brpc::RedisCommandHandlerResult OnBatched(const std::vector<const char*> args,
brpc::RedisReply* output, bool flush_batched) {
if (_batched_command.empty() && flush_batched) {
if (strcmp(args[0], "set") == 0) {
DoSet(args[1], args[2], output);
} else if (strcmp(args[0], "get") == 0) {
DoGet(args[1], output);
}
return brpc::RedisCommandHandler::OK;
return brpc::REDIS_CMD_HANDLED;
}
std::vector<std::string> comm;
for (int i = 0; i < (int)args.size(); ++i) {
Expand All @@ -838,9 +838,9 @@ class RedisServiceImpl : public brpc::RedisService {
}
_batch_count++;
_batched_command.clear();
return brpc::RedisCommandHandler::OK;
return brpc::REDIS_CMD_HANDLED;
} else {
return brpc::RedisCommandHandler::BATCHED;
return brpc::REDIS_CMD_BATCHED;
}
}

Expand Down Expand Up @@ -869,18 +869,18 @@ class SetCommandHandler : public brpc::RedisCommandHandler {
: _rs(rs)
, _batch_process(batch_process) {}

brpc::RedisCommandHandler::Result Run(const std::vector<const char*>& args,
brpc::RedisCommandHandlerResult Run(const std::vector<const char*>& args,
brpc::RedisReply* output,
bool flush_batched) {
if (args.size() < 3) {
output->SetError("ERR wrong number of arguments for 'set' command");
return brpc::RedisCommandHandler::OK;
return brpc::REDIS_CMD_HANDLED;
}
if (_batch_process) {
return _rs->OnBatched(args, output, flush_batched);
} else {
DoSet(args[1], args[2], output);
return brpc::RedisCommandHandler::OK;
return brpc::REDIS_CMD_HANDLED;
}
}

Expand All @@ -900,18 +900,18 @@ class GetCommandHandler : public brpc::RedisCommandHandler {
: _rs(rs)
, _batch_process(batch_process) {}

brpc::RedisCommandHandler::Result Run(const std::vector<const char*>& args,
brpc::RedisCommandHandlerResult Run(const std::vector<const char*>& args,
brpc::RedisReply* output,
bool flush_batched) {
if (args.size() < 2) {
output->SetError("ERR wrong number of arguments for 'get' command");
return brpc::RedisCommandHandler::OK;
return brpc::REDIS_CMD_HANDLED;
}
if (_batch_process) {
return _rs->OnBatched(args, output, flush_batched);
} else {
DoGet(args[1], output);
return brpc::RedisCommandHandler::OK;
return brpc::REDIS_CMD_HANDLED;
}
}

Expand All @@ -933,20 +933,20 @@ class IncrCommandHandler : public brpc::RedisCommandHandler {
public:
IncrCommandHandler() {}

brpc::RedisCommandHandler::Result Run(const std::vector<const char*>& args,
brpc::RedisCommandHandlerResult Run(const std::vector<const char*>& args,
brpc::RedisReply* output,
bool flush_batched) {
if (args.size() < 2) {
output->SetError("ERR wrong number of arguments for 'incr' command");
return brpc::RedisCommandHandler::OK;
return brpc::REDIS_CMD_HANDLED;
}
const std::string& key = args[1];
int64_t value;
s_mutex.lock();
value = ++int_map[key];
s_mutex.unlock();
output->SetInteger(value);
return brpc::RedisCommandHandler::OK;
return brpc::REDIS_CMD_HANDLED;
}
};

Expand Down Expand Up @@ -1047,11 +1047,11 @@ class MultiCommandHandler : public brpc::RedisCommandHandler {
public:
MultiCommandHandler() {}

brpc::RedisCommandHandler::Result Run(const std::vector<const char*>& args,
brpc::RedisCommandHandlerResult Run(const std::vector<const char*>& args,
brpc::RedisReply* output,
bool flush_batched) {
output->SetStatus("OK");
return brpc::RedisCommandHandler::CONTINUE;
return brpc::REDIS_CMD_CONTINUE;
}

RedisCommandHandler* NewTransactionHandler() override {
Expand All @@ -1060,12 +1060,12 @@ class MultiCommandHandler : public brpc::RedisCommandHandler {

class MultiTransactionHandler : public brpc::RedisCommandHandler {
public:
brpc::RedisCommandHandler::Result Run(const std::vector<const char*>& args,
brpc::RedisCommandHandlerResult Run(const std::vector<const char*>& args,
brpc::RedisReply* output,
bool flush_batched) {
if (strcmp(args[0], "multi") == 0) {
output->SetError("ERR duplicate multi");
return brpc::RedisCommandHandler::CONTINUE;
return brpc::REDIS_CMD_CONTINUE;
}
if (strcmp(args[0], "exec") != 0) {
std::vector<std::string> comm;
Expand All @@ -1074,7 +1074,7 @@ class MultiCommandHandler : public brpc::RedisCommandHandler {
}
_commands.push_back(comm);
output->SetStatus("QUEUED");
return brpc::RedisCommandHandler::CONTINUE;
return brpc::REDIS_CMD_CONTINUE;
}
output->SetArray(_commands.size());
s_mutex.lock();
Expand All @@ -1088,7 +1088,7 @@ class MultiCommandHandler : public brpc::RedisCommandHandler {
}
}
s_mutex.unlock();
return brpc::RedisCommandHandler::OK;
return brpc::REDIS_CMD_HANDLED;
}
private:
std::vector<std::vector<std::string> > _commands;
Expand Down

0 comments on commit 91a4376

Please sign in to comment.