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
a79093b4
Commit
a79093b4
authored
Dec 04, 2019
by
zhujiashun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
redis_server_protocol: revise to sync interface
parent
491a1860
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
26 additions
and
86 deletions
+26
-86
redis_server.cpp
example/redis_c++/redis_server.cpp
+4
-8
redis_protocol.cpp
src/brpc/policy/redis_protocol.cpp
+0
-0
redis.h
src/brpc/redis.h
+9
-11
brpc_redis_unittest.cpp
test/brpc_redis_unittest.cpp
+13
-67
No files found.
example/redis_c++/redis_server.cpp
View file @
a79093b4
...
@@ -60,9 +60,7 @@ public:
...
@@ -60,9 +60,7 @@ public:
:
_rsimpl
(
rsimpl
)
{}
:
_rsimpl
(
rsimpl
)
{}
brpc
::
RedisCommandHandler
::
Result
Run
(
const
char
*
args
[],
brpc
::
RedisCommandHandler
::
Result
Run
(
const
char
*
args
[],
brpc
::
RedisReply
*
output
,
brpc
::
RedisReply
*
output
)
{
google
::
protobuf
::
Closure
*
done
)
{
brpc
::
ClosureGuard
done_guard
(
done
);
if
(
args
[
1
]
==
NULL
)
{
if
(
args
[
1
]
==
NULL
)
{
output
->
SetError
(
"ERR wrong number of arguments for 'get' command"
);
output
->
SetError
(
"ERR wrong number of arguments for 'get' command"
);
return
brpc
::
RedisCommandHandler
::
OK
;
return
brpc
::
RedisCommandHandler
::
OK
;
...
@@ -88,14 +86,12 @@ public:
...
@@ -88,14 +86,12 @@ public:
:
_rsimpl
(
rsimpl
)
{}
:
_rsimpl
(
rsimpl
)
{}
brpc
::
RedisCommandHandler
::
Result
Run
(
const
char
*
args
[],
brpc
::
RedisCommandHandler
::
Result
Run
(
const
char
*
args
[],
brpc
::
RedisReply
*
output
,
brpc
::
RedisReply
*
output
)
{
google
::
protobuf
::
Closure
*
done
)
{
if
(
args
[
1
]
==
NULL
||
args
[
2
]
==
NULL
)
{
brpc
::
ClosureGuard
done_guard
(
done
);
std
::
string
key
=
args
[
1
];
if
(
args
[
2
]
==
NULL
)
{
output
->
SetError
(
"ERR wrong number of arguments for 'set' command"
);
output
->
SetError
(
"ERR wrong number of arguments for 'set' command"
);
return
brpc
::
RedisCommandHandler
::
OK
;
return
brpc
::
RedisCommandHandler
::
OK
;
}
}
std
::
string
key
=
args
[
1
];
_rsimpl
->
Set
(
key
,
args
[
2
]);
_rsimpl
->
Set
(
key
,
args
[
2
]);
output
->
SetStatus
(
"OK"
);
output
->
SetStatus
(
"OK"
);
return
brpc
::
RedisCommandHandler
::
OK
;
return
brpc
::
RedisCommandHandler
::
OK
;
...
...
src/brpc/policy/redis_protocol.cpp
View file @
a79093b4
This diff is collapsed.
Click to expand it.
src/brpc/redis.h
View file @
a79093b4
...
@@ -248,19 +248,17 @@ public:
...
@@ -248,19 +248,17 @@ public:
// args[2] == "bar" and args[3] == nullptr.
// args[2] == "bar" and args[3] == nullptr.
// `output`, which should be filled by user, is the content that sent to client side.
// `output`, which should be filled by user, is the content that sent to client side.
// Read brpc/src/redis_reply.h for more usage.
// Read brpc/src/redis_reply.h for more usage.
// Remember to call `done->Run()` when everything is set up into `output`. The return
// The return value should be RedisCommandHandler::OK for normal cases. If you want
// value should be RedisCommandHandler::OK for normal cases. If you want to implement
// to implement transaction, return RedisCommandHandler::CONTINUE until server receives
// transaction, return RedisCommandHandler::CONTINUE until server receives an ending
// an ending marker. The first handler that return RedisCommandHandler::CONTINUE will
// marker. The first handler that return RedisCommandHandler::CONTINUE will continue
// continue receiving the following commands until it receives an ending marker and
// receiving the following commands until it receives a ending marker and return
// return RedisCommandHandler::OK to end transaction. For example, the return value
// RedisCommandHandler::OK to end transaction. For example, the return value of
// of commands "multi; set k1 v1; set k2 v2; set k3 v3; exec" should be four
// commands "multi; set k1 v1; set k2 v2; set k3 v3; exec" should be four
// RedisCommandHandler::CONTINUE and one RedisCommandHandler::OK since exec is the
// RedisCommandHandler::CONTINUE and one RedisCommandHandler::OK since exec is the
// marker that ends the transaction. User
may
queue the commands and execute them
// marker that ends the transaction. User
should
queue the commands and execute them
// all once
an
ending marker is received.
// all once
the
ending marker is received.
virtual
RedisCommandHandler
::
Result
Run
(
const
char
*
args
[],
virtual
RedisCommandHandler
::
Result
Run
(
const
char
*
args
[],
RedisReply
*
output
,
RedisReply
*
output
)
=
0
;
google
::
protobuf
::
Closure
*
done
)
=
0
;
// Whenever a tcp connection is established, a bunch of new handlers would be created
// Whenever a tcp connection is established, a bunch of new handlers would be created
// using New() of the corresponding handler and brpc makes sure that all requests from
// using New() of the corresponding handler and brpc makes sure that all requests from
...
...
test/brpc_redis_unittest.cpp
View file @
a79093b4
...
@@ -665,60 +665,31 @@ butil::Mutex s_mutex;
...
@@ -665,60 +665,31 @@ butil::Mutex s_mutex;
std
::
unordered_map
<
std
::
string
,
std
::
string
>
m
;
std
::
unordered_map
<
std
::
string
,
std
::
string
>
m
;
std
::
unordered_map
<
std
::
string
,
int64_t
>
int_map
;
std
::
unordered_map
<
std
::
string
,
int64_t
>
int_map
;
struct
SleepArgs
{
int
sleep_ms
;
google
::
protobuf
::
Closure
*
done
;
};
void
*
sleep
(
void
*
arg
)
{
SleepArgs
*
args
=
static_cast
<
SleepArgs
*>
(
arg
);
bthread_usleep
(
args
->
sleep_ms
*
1000
);
args
->
done
->
Run
();
delete
args
;
return
NULL
;
}
class
SetCommandHandler
:
public
brpc
::
RedisCommandHandler
{
class
SetCommandHandler
:
public
brpc
::
RedisCommandHandler
{
public
:
public
:
SetCommandHandler
(
bool
sleep
=
false
)
SetCommandHandler
()
{}
:
_sleep
(
sleep
)
{}
brpc
::
RedisCommandHandler
::
Result
Run
(
const
char
*
args
[],
brpc
::
RedisCommandHandler
::
Result
Run
(
const
char
*
args
[],
brpc
::
RedisReply
*
output
,
brpc
::
RedisReply
*
output
)
{
google
::
protobuf
::
Closure
*
done
)
{
brpc
::
ClosureGuard
done_guard
(
done
);
std
::
string
key
=
args
[
1
];
std
::
string
key
=
args
[
1
];
std
::
string
value
=
args
[
2
];
std
::
string
value
=
args
[
2
];
m
[
key
]
=
value
;
m
[
key
]
=
value
;
output
->
SetStatus
(
"OK"
);
output
->
SetStatus
(
"OK"
);
if
(
_sleep
)
{
SleepArgs
*
args
=
new
SleepArgs
;
args
->
sleep_ms
=
_sleep_ms
;
args
->
done
=
done_guard
.
release
();
bthread_t
bth
;
EXPECT_EQ
(
0
,
bthread_start_background
(
&
bth
,
NULL
,
sleep
,
args
));
if
(
_sleep_ms
>
20
)
_sleep_ms
-=
20
;
}
return
brpc
::
RedisCommandHandler
::
OK
;
return
brpc
::
RedisCommandHandler
::
OK
;
}
}
RedisCommandHandler
*
New
()
{
_new_count
++
;
return
new
SetCommandHandler
(
_sleep
);
}
RedisCommandHandler
*
New
()
{
_new_count
++
;
return
new
SetCommandHandler
();
}
int
new_count
()
{
return
_new_count
;
}
int
new_count
()
{
return
_new_count
;
}
private
:
private
:
int
_sleep_ms
=
100
;
int
_new_count
=
0
;
int
_new_count
=
0
;
bool
_sleep
=
false
;
};
};
class
GetCommandHandler
:
public
brpc
::
RedisCommandHandler
{
class
GetCommandHandler
:
public
brpc
::
RedisCommandHandler
{
public
:
public
:
GetCommandHandler
(
bool
sleep
=
false
)
GetCommandHandler
()
{}
:
_sleep
(
sleep
)
{}
brpc
::
RedisCommandHandler
::
Result
Run
(
const
char
*
args
[],
brpc
::
RedisCommandHandler
::
Result
Run
(
const
char
*
args
[],
brpc
::
RedisReply
*
output
,
brpc
::
RedisReply
*
output
)
{
google
::
protobuf
::
Closure
*
done
)
{
brpc
::
ClosureGuard
done_guard
(
done
);
std
::
string
key
=
args
[
1
];
std
::
string
key
=
args
[
1
];
auto
it
=
m
.
find
(
key
);
auto
it
=
m
.
find
(
key
);
if
(
it
!=
m
.
end
())
{
if
(
it
!=
m
.
end
())
{
...
@@ -726,56 +697,33 @@ public:
...
@@ -726,56 +697,33 @@ public:
}
else
{
}
else
{
output
->
SetNilString
();
output
->
SetNilString
();
}
}
if
(
_sleep
)
{
SleepArgs
*
args
=
new
SleepArgs
;
args
->
sleep_ms
=
_sleep_ms
;
args
->
done
=
done_guard
.
release
();
bthread_t
bth
;
EXPECT_EQ
(
0
,
bthread_start_background
(
&
bth
,
NULL
,
sleep
,
args
));
if
(
_sleep_ms
>
20
)
_sleep_ms
-=
20
;
}
return
brpc
::
RedisCommandHandler
::
OK
;
return
brpc
::
RedisCommandHandler
::
OK
;
}
}
RedisCommandHandler
*
New
()
{
_new_count
++
;
return
new
GetCommandHandler
(
_sleep
);
}
RedisCommandHandler
*
New
()
{
_new_count
++
;
return
new
GetCommandHandler
();
}
int
new_count
()
{
return
_new_count
;
}
int
new_count
()
{
return
_new_count
;
}
private
:
private
:
int
_sleep_ms
=
100
;
int
_new_count
=
0
;
int
_new_count
=
0
;
bool
_sleep
=
false
;
};
};
class
IncrCommandHandler
:
public
brpc
::
RedisCommandHandler
{
class
IncrCommandHandler
:
public
brpc
::
RedisCommandHandler
{
public
:
public
:
IncrCommandHandler
(
bool
sleep
=
false
)
IncrCommandHandler
()
{}
:
_sleep
(
sleep
)
{}
brpc
::
RedisCommandHandler
::
Result
Run
(
const
char
*
args
[],
brpc
::
RedisCommandHandler
::
Result
Run
(
const
char
*
args
[],
brpc
::
RedisReply
*
output
,
brpc
::
RedisReply
*
output
)
{
google
::
protobuf
::
Closure
*
done
)
{
brpc
::
ClosureGuard
done_guard
(
done
);
int64_t
value
;
int64_t
value
;
s_mutex
.
lock
();
s_mutex
.
lock
();
value
=
++
int_map
[
args
[
1
]];
value
=
++
int_map
[
args
[
1
]];
s_mutex
.
unlock
();
s_mutex
.
unlock
();
output
->
SetInteger
(
value
);
output
->
SetInteger
(
value
);
if
(
_sleep
)
{
SleepArgs
*
args
=
new
SleepArgs
;
args
->
sleep_ms
=
_sleep_ms
;
args
->
done
=
done_guard
.
release
();
bthread_t
bth
;
EXPECT_EQ
(
0
,
bthread_start_background
(
&
bth
,
NULL
,
sleep
,
args
));
if
(
_sleep_ms
>
20
)
_sleep_ms
-=
20
;
}
return
brpc
::
RedisCommandHandler
::
OK
;
return
brpc
::
RedisCommandHandler
::
OK
;
}
}
RedisCommandHandler
*
New
()
{
_new_count
++
;
return
new
IncrCommandHandler
(
_sleep
);
}
RedisCommandHandler
*
New
()
{
_new_count
++
;
return
new
IncrCommandHandler
();
}
int
new_count
()
{
return
_new_count
;
}
int
new_count
()
{
return
_new_count
;
}
private
:
private
:
int
_sleep_ms
=
100
;
int
_new_count
=
0
;
int
_new_count
=
0
;
bool
_sleep
=
false
;
};
};
class
RedisServiceImpl
:
public
brpc
::
RedisService
{
};
class
RedisServiceImpl
:
public
brpc
::
RedisService
{
};
...
@@ -784,9 +732,9 @@ TEST_F(RedisTest, server_sanity) {
...
@@ -784,9 +732,9 @@ TEST_F(RedisTest, server_sanity) {
brpc
::
Server
server
;
brpc
::
Server
server
;
brpc
::
ServerOptions
server_options
;
brpc
::
ServerOptions
server_options
;
RedisServiceImpl
*
rsimpl
=
new
RedisServiceImpl
;
RedisServiceImpl
*
rsimpl
=
new
RedisServiceImpl
;
GetCommandHandler
*
gh
=
new
GetCommandHandler
(
true
)
;
GetCommandHandler
*
gh
=
new
GetCommandHandler
;
SetCommandHandler
*
sh
=
new
SetCommandHandler
(
true
)
;
SetCommandHandler
*
sh
=
new
SetCommandHandler
;
IncrCommandHandler
*
ih
=
new
IncrCommandHandler
(
true
)
;
IncrCommandHandler
*
ih
=
new
IncrCommandHandler
;
rsimpl
->
AddCommandHandler
(
"get"
,
gh
);
rsimpl
->
AddCommandHandler
(
"get"
,
gh
);
rsimpl
->
AddCommandHandler
(
"set"
,
sh
);
rsimpl
->
AddCommandHandler
(
"set"
,
sh
);
rsimpl
->
AddCommandHandler
(
"incr"
,
ih
);
rsimpl
->
AddCommandHandler
(
"incr"
,
ih
);
...
@@ -884,9 +832,7 @@ public:
...
@@ -884,9 +832,7 @@ public:
:
_started
(
false
)
{}
:
_started
(
false
)
{}
RedisCommandHandler
::
Result
Run
(
const
char
*
args
[],
RedisCommandHandler
::
Result
Run
(
const
char
*
args
[],
brpc
::
RedisReply
*
output
,
brpc
::
RedisReply
*
output
)
{
google
::
protobuf
::
Closure
*
done
)
{
brpc
::
ClosureGuard
done_guard
(
done
);
if
(
strcasecmp
(
args
[
0
],
"multi"
)
==
0
)
{
if
(
strcasecmp
(
args
[
0
],
"multi"
)
==
0
)
{
if
(
!
_started
)
{
if
(
!
_started
)
{
output
->
SetStatus
(
"OK"
);
output
->
SetStatus
(
"OK"
);
...
...
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