-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor coroutine * add Cancelable * add example/use-anyflow-with-brpc
- Loading branch information
Showing
51 changed files
with
2,335 additions
and
945 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
7.3.1 | ||
7.3.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
common --registry=https://bcr.bazel.build | ||
common --registry=https://baidu.github.io/babylon/registry | ||
common --registry=https://raw.githubusercontent.com/bazelboost/registry/main | ||
|
||
build --cxxopt=-std=c++17 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
7.3.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
proto_library( | ||
name = 'search_proto', | ||
srcs = ['search.proto'], | ||
) | ||
|
||
cc_proto_library( | ||
name = 'cc_search_proto', | ||
deps = [':search_proto'], | ||
) | ||
|
||
cc_library( | ||
name = 'butex_interface', | ||
hdrs = ['butex_interface.h'], | ||
deps = [ | ||
'@babylon', | ||
'@brpc', | ||
], | ||
) | ||
|
||
cc_library( | ||
name = 'bthread_graph_executor', | ||
srcs = ['bthread_graph_executor.cpp'], | ||
hdrs = ['bthread_graph_executor.h'], | ||
deps = [ | ||
':butex_interface', | ||
], | ||
) | ||
|
||
cc_library( | ||
name = 'graph_configurator', | ||
hdrs = ['graph_configurator.h'], | ||
deps = [ | ||
':bthread_graph_executor', | ||
'@yaml-cpp', | ||
], | ||
) | ||
|
||
cc_library( | ||
name = 'processors', | ||
srcs = ['parse.cpp', 'rank.cpp', 'recall.cpp', 'response.cpp', 'user_profile.cpp'], | ||
deps = [ | ||
':cc_search_proto', | ||
'@babylon', | ||
'@yaml-cpp', | ||
], | ||
# BABYLON_REGISTER_COMPONENT need alwayslink | ||
alwayslink = True, | ||
) | ||
|
||
cc_binary( | ||
name = 'client', | ||
srcs = ['client.cpp'], | ||
deps = [ | ||
':cc_search_proto', | ||
'@brpc', | ||
], | ||
) | ||
|
||
cc_binary( | ||
name = 'server', | ||
srcs = ['server.cpp'], | ||
deps = [ | ||
':cc_search_proto', | ||
':graph_configurator', | ||
':processors', | ||
'@brpc', | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
bazel_dep(name = 'babylon', version = '1.3.2') | ||
bazel_dep(name = 'brpc', version = '1.9.0') | ||
bazel_dep(name = 'yaml-cpp', version = '0.8.0') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Use anyflow with brpc | ||
|
||
结合brpc,使用anyflow进行服务模块化的简单样例 | ||
|
||
## 示例构成 | ||
- `:graph_configurator`: anyflow本身只提供了基础API,一般业务实际场景需要结合自己所使用的配置机制,做一层包装,这里用yaml为例展示了这个包装的基础做法 | ||
- `dag.yaml`: 对应的配置文件 | ||
- `:processors`: 模块化分割的搜索服务实现拆分样例,采用babylon::ApplicationContext的注册机制管理组件构造 | ||
- 包含['parse.cpp', 'rank.cpp', 'recall.cpp', 'response.cpp', 'user_profile.cpp'] | ||
- `:server`: 顶层服务封装,只包含初始化`graph_configurator`,以及将service的request和response注入到graph内作为初始节点 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include "bthread_graph_executor.h" | ||
|
||
#include "babylon/logging/logger.h" | ||
|
||
#include "butex_interface.h" | ||
|
||
#include <tuple> | ||
|
||
BABYLON_NAMESPACE_BEGIN | ||
namespace anyflow { | ||
|
||
static void* execute_invoke_vertex(void* args) { | ||
auto param = | ||
reinterpret_cast<::std::tuple<GraphVertex*, GraphVertexClosure>*>(args); | ||
auto vertex = ::std::get<0>(*param); | ||
auto& closure = ::std::get<1>(*param); | ||
vertex->run(::std::move(closure)); | ||
delete param; | ||
return NULL; | ||
} | ||
|
||
static void* execute_invoke_closure(void* args) { | ||
auto param = | ||
reinterpret_cast<::std::tuple<ClosureContext*, Closure::Callback*>*>( | ||
args); | ||
auto closure = ::std::get<0>(*param); | ||
auto callback = ::std::get<1>(*param); | ||
closure->run(callback); | ||
delete param; | ||
return NULL; | ||
} | ||
|
||
BthreadGraphExecutor& BthreadGraphExecutor::instance() { | ||
static BthreadGraphExecutor executor; | ||
return executor; | ||
} | ||
|
||
Closure BthreadGraphExecutor::create_closure() noexcept { | ||
return Closure::create<ButexInterface>(*this); | ||
} | ||
|
||
int BthreadGraphExecutor::run(GraphVertex* vertex, | ||
GraphVertexClosure&& closure) noexcept { | ||
bthread_t th; | ||
auto param = new ::std::tuple<GraphVertex*, GraphVertexClosure>( | ||
vertex, ::std::move(closure)); | ||
if (0 != bthread_start_background(&th, NULL, execute_invoke_vertex, param)) { | ||
LOG(WARNING) << "start bthread to run vertex failed"; | ||
closure = ::std::move(::std::get<1>(*param)); | ||
delete param; | ||
return -1; | ||
} | ||
return 0; | ||
} | ||
|
||
int BthreadGraphExecutor::run(ClosureContext* closure, | ||
Closure::Callback* callback) noexcept { | ||
bthread_t th; | ||
auto param = | ||
new ::std::tuple<ClosureContext*, Closure::Callback*>(closure, callback); | ||
if (0 != bthread_start_background(&th, NULL, execute_invoke_closure, param)) { | ||
LOG(WARNING) << "start bthread to run closure failed"; | ||
delete param; | ||
return -1; | ||
} | ||
return 0; | ||
} | ||
|
||
} // namespace anyflow | ||
BABYLON_NAMESPACE_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include "babylon/anyflow/closure.h" | ||
#include "babylon/anyflow/executor.h" | ||
#include "babylon/anyflow/vertex.h" | ||
|
||
#include "bthread/bthread.h" | ||
|
||
BABYLON_NAMESPACE_BEGIN | ||
namespace anyflow { | ||
|
||
class BthreadGraphExecutor : public GraphExecutor { | ||
public: | ||
static BthreadGraphExecutor& instance(); | ||
virtual Closure create_closure() noexcept override; | ||
virtual int run(GraphVertex* vertex, | ||
GraphVertexClosure&& closure) noexcept override; | ||
virtual int run(ClosureContext* closure, | ||
Closure::Callback* callback) noexcept override; | ||
}; | ||
|
||
} // namespace anyflow | ||
BABYLON_NAMESPACE_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/sh | ||
set -ex | ||
|
||
bazel build server client |
Oops, something went wrong.