Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit 84bbbda

Browse files
committed
Refactored brain code for better interface.
1 parent ca0dc71 commit 84bbbda

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

src/include/brain/brain.h

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,45 @@
2323
namespace peloton {
2424
namespace brain {
2525

26+
/**
27+
* Provides an access point to the various resources available to the jobs in
28+
* the brain, such as RPC and Catalog.
29+
*/
2630
class BrainEnvironment {
27-
// TODO(tianyu): provide interface for accessing various resources of the brain,
28-
// such as network connection to a peloton engine.
31+
// TODO(tianyu): fill in as needed
2932
};
3033

34+
/**
35+
* Interface that represents a piece of task to be run on Brain. To use this
36+
* abstract class, extend it with a concrete class and fill in the method
37+
* OnJobInvocation.
38+
*/
3139
class BrainJob {
32-
friend class Brain;
3340
public:
3441
explicit BrainJob(BrainEnvironment *env) : env_(env) {}
42+
3543
virtual ~BrainJob() = default;
44+
45+
// This is separate from the user-defined OnJobInvocation to allow for better
46+
// interfacing with the libevent API.
47+
/**
48+
* Invokes this job to be run. The brain framework will call this method.
49+
*
50+
*/
3651
inline void Invoke() { OnJobInvocation(env_); }
37-
// TODO(tianyu): Extend this interface for richer interaction
52+
// TODO(tianyu): Extend this interface for richer behavior
53+
/**
54+
* Executed as the main body of the job, filled in by the user. Use the
55+
* provided BrainEnvironment for interaction with Brain's resources.
56+
*/
3857
virtual void OnJobInvocation(BrainEnvironment *) = 0;
3958
private:
4059
BrainEnvironment *env_;
4160
};
4261

62+
/**
63+
* Simple implementation of a BrainJob.
64+
*/
4365
class SimpleBrainJob : public BrainJob {
4466
public:
4567
explicit SimpleBrainJob(BrainEnvironment *env,
@@ -50,15 +72,19 @@ class SimpleBrainJob : public BrainJob {
5072
std::function<void(BrainEnvironment *)> task_;
5173
};
5274

75+
/**
76+
* Main running component of the brain. Events can be registered on this event
77+
* loop and once Run is called, it will invoke handlers every specified time
78+
* interval
79+
*/
5380
class Brain {
5481
public:
5582
// TODO(tianyu): Add necessary parameters to initialize the brain's resources
5683
Brain() : scheduler_(0) {}
84+
5785
~Brain() {
5886
for (auto entry : jobs_)
5987
delete entry.second;
60-
for (auto entry : job_handles_)
61-
event_free(entry.second);
6288
}
6389

6490
template <typename BrainJob, typename... Args>

src/main/peloton/peloton.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ void RunPelotonBrain() {
4949
// TODO(tianyu): register jobs here
5050
struct timeval one_second;
5151
one_second.tv_sec = 1;
52+
one_second.tv_usec = 0;
5253

53-
constexpr auto example_task = [](peloton::brain::BrainEnvironment *) {
54+
auto example_task = [](peloton::brain::BrainEnvironment *) {
5455
// TODO(tianyu): Replace with real address
5556
capnp::EzRpcClient client("localhost:15445");
5657
PelotonService::Client peloton_service = client.getMain<PelotonService>();

src/network/peloton_server.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ void PelotonServer::ServerLoop() {
270270
if (settings::SettingsManager::GetBool(settings::SettingId::rpc_enabled)) {
271271
int rpc_port =
272272
settings::SettingsManager::GetInt(settings::SettingId::rpc_port);
273-
auto rpc_task = std::make_shared<PelotonRpcHandlerTask>(("127.0.0.1:"
274-
+ std::to_string(rpc_port)).c_str());
273+
std::string address = "127.0.0.1:" + std::to_string(rpc_port);
274+
auto rpc_task = std::make_shared<PelotonRpcHandlerTask>(address.c_str());
275275
DedicatedThreadRegistry::GetInstance()
276276
.RegisterDedicatedThread<PelotonRpcHandlerTask>(this, rpc_task);
277277
}

0 commit comments

Comments
 (0)