Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
B
brpc
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
brpc
Commits
91a43765
Commit
91a43765
authored
Jan 16, 2020
by
jamesge
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rename RedisCommandHandler::Result to make related code cleaner
parent
d81ba3f1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
48 additions
and
47 deletions
+48
-47
redis_server.cpp
example/redis_c++/redis_server.cpp
+6
-6
redis_protocol.cpp
src/brpc/policy/redis_protocol.cpp
+8
-8
redis.h
src/brpc/redis.h
+15
-14
brpc_redis_unittest.cpp
test/brpc_redis_unittest.cpp
+19
-19
No files found.
example/redis_c++/redis_server.cpp
View file @
91a43765
...
...
@@ -64,12 +64,12 @@ public:
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
::
R
edisCommandHandler
::
OK
;
return
brpc
::
R
EDIS_CMD_HANDLED
;
}
const
std
::
string
key
(
args
[
1
]);
std
::
string
value
;
...
...
@@ -78,7 +78,7 @@ public:
}
else
{
output
->
SetNullString
();
}
return
brpc
::
R
edisCommandHandler
::
OK
;
return
brpc
::
R
EDIS_CMD_HANDLED
;
}
private
:
...
...
@@ -90,18 +90,18 @@ public:
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
::
R
edisCommandHandler
::
OK
;
return
brpc
::
R
EDIS_CMD_HANDLED
;
}
const
std
::
string
key
(
args
[
1
]);
const
std
::
string
value
(
args
[
2
]);
_rsimpl
->
Set
(
key
,
value
);
output
->
SetStatus
(
"OK"
);
return
brpc
::
R
edisCommandHandler
::
OK
;
return
brpc
::
R
EDIS_CMD_HANDLED
;
}
private
:
...
...
src/brpc/policy/redis_protocol.cpp
View file @
91a43765
...
...
@@ -80,12 +80,12 @@ int ConsumeCommand(RedisConnContext* ctx,
bool
flush_batched
,
butil
::
IOBufAppender
*
appender
)
{
RedisReply
output
(
&
ctx
->
arena
);
RedisCommandHandler
::
Result
result
=
RedisCommandHandler
::
OK
;
RedisCommandHandler
Result
result
=
REDIS_CMD_HANDLED
;
if
(
ctx
->
transaction_handler
)
{
result
=
ctx
->
transaction_handler
->
Run
(
commands
,
&
output
,
flush_batched
);
if
(
result
==
R
edisCommandHandler
::
OK
)
{
if
(
result
==
R
EDIS_CMD_HANDLED
)
{
ctx
->
transaction_handler
.
reset
(
NULL
);
}
else
if
(
result
==
R
edisCommandHandler
::
BATCHED
)
{
}
else
if
(
result
==
R
EDIS_CMD_
BATCHED
)
{
LOG
(
ERROR
)
<<
"BATCHED should not be returned by a transaction handler."
;
return
-
1
;
}
...
...
@@ -97,18 +97,18 @@ int ConsumeCommand(RedisConnContext* ctx,
output
.
SetError
(
buf
);
}
else
{
result
=
ch
->
Run
(
commands
,
&
output
,
flush_batched
);
if
(
result
==
R
edisCommandHandler
::
CONTINUE
)
{
if
(
result
==
R
EDIS_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
==
R
edisCommandHandler
::
BATCHED
)
{
}
else
if
(
result
==
R
EDIS_CMD_
BATCHED
)
{
ctx
->
batched_size
++
;
}
}
}
if
(
result
==
R
edisCommandHandler
::
OK
)
{
if
(
result
==
R
EDIS_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, "
...
...
@@ -122,9 +122,9 @@ int ConsumeCommand(RedisConnContext* ctx,
}
else
{
output
.
SerializeTo
(
appender
);
}
}
else
if
(
result
==
R
edisCommandHandler
::
CONTINUE
)
{
}
else
if
(
result
==
R
EDIS_CMD_
CONTINUE
)
{
output
.
SerializeTo
(
appender
);
}
else
if
(
result
==
R
edisCommandHandler
::
BATCHED
)
{
}
else
if
(
result
==
R
EDIS_CMD_
BATCHED
)
{
// just do nothing and wait handler to return OK.
}
else
{
LOG
(
ERROR
)
<<
"unknown status="
<<
result
;
...
...
src/brpc/redis.h
View file @
91a43765
...
...
@@ -231,14 +231,15 @@ private:
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
...
...
@@ -250,24 +251,24 @@ public:
// 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 R
edisCommandHandler::
BATCHED. Once `flush_batched' is true,
// the commands and return R
EDIS_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 R
edisCommandHandler::OK
.
// result of batched commands and return R
EDIS_CMD_HANDLED
.
//
// The return value should be R
edisCommandHandler::OK
for normal cases. If you want
// to implement transaction, return R
edisCommandHandler::
CONTINUE once server receives
// The return value should be R
EDIS_CMD_HANDLED
for normal cases. If you want
// to implement transaction, return R
EDIS_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 R
edisCommandHandler::OK
. Read the comment below.
virtual
RedisCommandHandler
::
Result
Run
(
const
std
::
vector
<
const
char
*>&
args
,
brpc
::
RedisReply
*
output
,
bool
flush_batched
)
=
0
;
// it returns R
EDIS_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 R
edisCommandHandler::
CONTINUE,
// 1) First command is "multi" and Run() should return R
EDIS_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.
...
...
test/brpc_redis_unittest.cpp
View file @
91a43765
...
...
@@ -812,7 +812,7 @@ public:
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
)
{
...
...
@@ -820,7 +820,7 @@ public:
}
else
if
(
strcmp
(
args
[
0
],
"get"
)
==
0
)
{
DoGet
(
args
[
1
],
output
);
}
return
brpc
::
R
edisCommandHandler
::
OK
;
return
brpc
::
R
EDIS_CMD_HANDLED
;
}
std
::
vector
<
std
::
string
>
comm
;
for
(
int
i
=
0
;
i
<
(
int
)
args
.
size
();
++
i
)
{
...
...
@@ -838,9 +838,9 @@ public:
}
_batch_count
++
;
_batched_command
.
clear
();
return
brpc
::
R
edisCommandHandler
::
OK
;
return
brpc
::
R
EDIS_CMD_HANDLED
;
}
else
{
return
brpc
::
R
edisCommandHandler
::
BATCHED
;
return
brpc
::
R
EDIS_CMD_
BATCHED
;
}
}
...
...
@@ -869,18 +869,18 @@ public:
:
_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
::
R
edisCommandHandler
::
OK
;
return
brpc
::
R
EDIS_CMD_HANDLED
;
}
if
(
_batch_process
)
{
return
_rs
->
OnBatched
(
args
,
output
,
flush_batched
);
}
else
{
DoSet
(
args
[
1
],
args
[
2
],
output
);
return
brpc
::
R
edisCommandHandler
::
OK
;
return
brpc
::
R
EDIS_CMD_HANDLED
;
}
}
...
...
@@ -900,18 +900,18 @@ public:
:
_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
::
R
edisCommandHandler
::
OK
;
return
brpc
::
R
EDIS_CMD_HANDLED
;
}
if
(
_batch_process
)
{
return
_rs
->
OnBatched
(
args
,
output
,
flush_batched
);
}
else
{
DoGet
(
args
[
1
],
output
);
return
brpc
::
R
edisCommandHandler
::
OK
;
return
brpc
::
R
EDIS_CMD_HANDLED
;
}
}
...
...
@@ -933,12 +933,12 @@ 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
::
R
edisCommandHandler
::
OK
;
return
brpc
::
R
EDIS_CMD_HANDLED
;
}
const
std
::
string
&
key
=
args
[
1
];
int64_t
value
;
...
...
@@ -946,7 +946,7 @@ public:
value
=
++
int_map
[
key
];
s_mutex
.
unlock
();
output
->
SetInteger
(
value
);
return
brpc
::
R
edisCommandHandler
::
OK
;
return
brpc
::
R
EDIS_CMD_HANDLED
;
}
};
...
...
@@ -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
::
R
edisCommandHandler
::
CONTINUE
;
return
brpc
::
R
EDIS_CMD_
CONTINUE
;
}
RedisCommandHandler
*
NewTransactionHandler
()
override
{
...
...
@@ -1060,12 +1060,12 @@ public:
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
::
R
edisCommandHandler
::
CONTINUE
;
return
brpc
::
R
EDIS_CMD_
CONTINUE
;
}
if
(
strcmp
(
args
[
0
],
"exec"
)
!=
0
)
{
std
::
vector
<
std
::
string
>
comm
;
...
...
@@ -1074,7 +1074,7 @@ public:
}
_commands
.
push_back
(
comm
);
output
->
SetStatus
(
"QUEUED"
);
return
brpc
::
R
edisCommandHandler
::
CONTINUE
;
return
brpc
::
R
EDIS_CMD_
CONTINUE
;
}
output
->
SetArray
(
_commands
.
size
());
s_mutex
.
lock
();
...
...
@@ -1088,7 +1088,7 @@ public:
}
}
s_mutex
.
unlock
();
return
brpc
::
R
edisCommandHandler
::
OK
;
return
brpc
::
R
EDIS_CMD_HANDLED
;
}
private
:
std
::
vector
<
std
::
vector
<
std
::
string
>
>
_commands
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment