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

Commit dbfa2e5

Browse files
committed
Add basic infrastructure to submit tasks to the brain.
1 parent 4a7a792 commit dbfa2e5

File tree

4 files changed

+106
-36
lines changed

4 files changed

+106
-36
lines changed

src/include/brain/brain.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// brain.h
6+
//
7+
// Identification: src/include/brain/brain.h
8+
//
9+
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#pragma once
14+
#include <unordered_map>
15+
#include <string>
16+
#include <cstdio>
17+
#include <utility>
18+
#include "common/notifiable_task.h"
19+
20+
namespace peloton {
21+
namespace brain {
22+
23+
class BrainEnvironment {
24+
// TODO(tianyu): provide interface for accessing various resources of the brain,
25+
// such as network connection to a peloton engine.
26+
};
27+
28+
class BrainJob {
29+
public:
30+
// TODO(tianyu): Extend this interface for richer interaction
31+
virtual void RunJob(BrainEnvironment &env) { printf("foo"); };
32+
};
33+
34+
class Brain {
35+
public:
36+
// TODO(tianyu): Add necessary parameters to initialize the brain's resources
37+
Brain() : scheduler_(0) {}
38+
39+
inline void RegisterJob(const struct timeval *period,
40+
std::string name,
41+
BrainJob &job) {
42+
auto callback = [&](int, short, void *env) {
43+
job.RunJob(*reinterpret_cast<BrainEnvironment *>(env));
44+
};
45+
job_handles_[name] =
46+
scheduler_.RegisterPeriodicEvent(period, callback, &env);
47+
}
48+
49+
inline void Run() {
50+
scheduler_.EventLoop();
51+
}
52+
53+
inline void Terminate() {
54+
scheduler_.ExitLoop();
55+
}
56+
57+
private:
58+
NotifiableTask scheduler_;
59+
std::unordered_map<std::string, struct event *> job_handles_;
60+
// TODO(tianyu): May need to have multiple env instead of shared
61+
BrainEnvironment env;
62+
};
63+
}
64+
}

src/include/brain/scheduler_task.h

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/include/settings/settings.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ SETTING_bool(brain,
156156
false,
157157
true, true)
158158

159+
SETTING_string(peloton_address,
160+
"ip and port of the peloton rpc service, address:port",
161+
"127.0.0.1:15445",
162+
false, false)
163+
159164
// Size of the brain task queue
160165
SETTING_int(brain_task_queue_size,
161166
"Brain Task Queue Size (default: 32)",

src/main/peloton/peloton.cpp

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,42 @@
1717
#include "common/logger.h"
1818
#include "network/peloton_server.h"
1919
#include "settings/settings_manager.h"
20+
#include "brain/brain.h"
2021

2122
// For GFlag's built-in help message flag
2223
DECLARE_bool(help);
2324

25+
void RunPelotonEngine() {
26+
try {
27+
// Setup
28+
peloton::PelotonInit::Initialize();
29+
30+
peloton::network::PelotonServer peloton_server;
31+
32+
peloton::network::PelotonServer::LoadSSLFileSettings();
33+
peloton::network::PelotonServer::SSLInit();
34+
35+
peloton_server.SetupServer().ServerLoop();
36+
} catch (peloton::ConnectionException &exception) {
37+
// Nothing to do here!
38+
}
39+
40+
// Teardown
41+
peloton::PelotonInit::Shutdown();
42+
}
43+
44+
45+
void RunPelotonBrain() {
46+
// TODO(tianyu): boot up other peloton resources as needed here
47+
peloton::brain::Brain brain;
48+
evthread_use_pthreads();
49+
// TODO(tianyu): register jobs here
50+
struct timeval *one_minute;
51+
one_minute->tv_sec = 60;
52+
brain.RegisterJob(one_minute, "test", peloton::brain::BrainJob());
53+
brain.Run();
54+
}
55+
2456
int main(int argc, char *argv[]) {
2557

2658
// Parse the command line flags
@@ -39,22 +71,10 @@ int main(int argc, char *argv[]) {
3971
settings.ShowInfo();
4072
}
4173

42-
try {
43-
// Setup
44-
peloton::PelotonInit::Initialize();
45-
46-
peloton::network::PelotonServer peloton_server;
47-
48-
peloton::network::PelotonServer::LoadSSLFileSettings();
49-
peloton::network::PelotonServer::SSLInit();
50-
51-
peloton_server.SetupServer().ServerLoop();
52-
} catch (peloton::ConnectionException &exception) {
53-
// Nothing to do here!
54-
}
55-
56-
// Teardown
57-
peloton::PelotonInit::Shutdown();
58-
74+
if (peloton::settings::SettingsManager::GetBool(
75+
peloton::settings::SettingId::brain))
76+
RunPelotonBrain();
77+
else
78+
RunPelotonEngine();
5979
return 0;
6080
}

0 commit comments

Comments
 (0)