-
Notifications
You must be signed in to change notification settings - Fork 418
/
grape_engine.cc
241 lines (211 loc) · 7.06 KB
/
grape_engine.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/** Copyright 2020 Alibaba Group Holding Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <glog/logging.h>
#include <mpi.h>
#include <sys/signal.h>
#include <csignal>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <limits>
#include <memory>
#include <thread>
#include <utility>
#include "boost/leaf/capture.hpp"
#include "boost/leaf/error.hpp"
#include "boost/leaf/handle_errors.hpp"
#include "boost/leaf/result.hpp"
#include "gflags/gflags.h"
#include "grape/communication/sync_comm.h"
#include "grape/config.h"
#include "grape/worker/comm_spec.h"
#include "grpcpp/server.h"
#include "core/context/i_context.h"
#include "core/error.h"
#include "core/flags.h"
#include "core/grape_instance.h"
#include "core/launcher.h"
#include "core/object/i_fragment_wrapper.h"
#include "core/server/analytical_server.h"
#include "core/server/dispatcher.h"
#include "core/server/rpc_utils.h"
namespace bl = boost::leaf;
struct tm;
namespace gs {
/**
* @brief GrapeEngine is responsible to assemble and start four major
* components of analytical engine, including VineyardServer, GrapeInstance,
* Dispatcher and GraphScopeServer.
*/
class GrapeEngine {
public:
/**
* @brief Construct a new GrapeEngine object in service mode, listening at the
* host:port for incoming rpc requests.
*
* @param host
* @param port
*/
GrapeEngine(std::string host, int port) : GrapeEngine() {
if (comm_spec_.worker_id() == grape::kCoordinatorRank) {
rpc_server_ =
std::make_shared<rpc::AnalyticalServer>(dispatcher_, host, port);
}
}
/**
* @brief Construct a new GrapeEngine object in job mode, processing the
* workflow defined in the dagfile.
*
* @param dag_file
*/
explicit GrapeEngine(std::string dag_file) : GrapeEngine() {
dag_file_ = std::move(dag_file);
}
void Start() {
vineyard_server_->Start();
grape_instance_->Init(vineyard_server_->vineyard_socket());
dispatcher_->Subscribe(grape_instance_);
if (rpc_server_ != nullptr) {
service_thread_ = std::thread([this]() { rpc_server_->StartServer(); });
}
dispatcher_->Start();
}
void Stop() {
if (rpc_server_) {
LOG(INFO) << "grape-engine (master) RPC server is stopping...";
rpc_server_->StopServer();
service_thread_.join();
}
if (dispatcher_) {
LOG(INFO) << "grape-engine dispatcher is stopping...";
dispatcher_->Stop();
}
if (vineyard_server_) {
LOG(INFO) << "vineyardd instance is stopping...";
vineyard_server_->Stop();
}
}
int RunDAGFile() {
return bl::try_handle_all(
[this]() -> bl::result<int> {
BOOST_LEAF_AUTO(dag_def, rpc::ReadDagFromFile(dag_file_));
for (const auto& op : dag_def.op()) {
auto cmd = OpToCmd(op);
dispatcher_->SetCommand(cmd);
}
vineyard_server_->Stop();
return 0;
},
[](vineyard::GSError& e) {
LOG(ERROR) << ErrorCodeToString(e.error_code) << " " << e.error_msg;
return 1;
},
[](const bl::error_info& e) {
LOG(ERROR) << "Unmatched error" << e;
return 1;
});
}
private:
GrapeEngine() {
comm_spec_.Init(MPI_COMM_WORLD);
vineyard_server_ = std::make_shared<VineyardServer>(comm_spec_);
grape_instance_ = std::make_shared<GrapeInstance>(comm_spec_);
dispatcher_ = std::make_shared<Dispatcher>(comm_spec_);
}
grape::CommSpec comm_spec_;
std::shared_ptr<VineyardServer> vineyard_server_;
std::shared_ptr<GrapeInstance> grape_instance_;
std::shared_ptr<Dispatcher> dispatcher_;
std::shared_ptr<rpc::AnalyticalServer> rpc_server_;
std::string dag_file_;
std::thread service_thread_;
};
} // namespace gs
static std::shared_ptr<gs::GrapeEngine> grape_engine_ptr;
// do proper cleanup in response to signals
void InstallSignalHandlers(void (*signal_handler)(int, siginfo_t*, void*)) {
struct sigaction sig_action;
memset(&sig_action, 0, sizeof(sig_action));
sigemptyset(&sig_action.sa_mask);
sig_action.sa_flags |= SA_SIGINFO;
sig_action.sa_sigaction = signal_handler;
sigaction(SIGTERM, &sig_action, NULL);
sigaction(SIGINT, &sig_action, NULL);
}
extern "C" void master_signal_handler(int sig, siginfo_t* info, void* context) {
if (sig == SIGTERM || sig == SIGINT) {
grape_engine_ptr->Stop();
// grape::FinalizeMPIComm();
std::exit(0);
}
}
// A custom sinker to redirect glog messages to stdout/stderr.
class RedirectLogSink : public google::LogSink {
virtual void send(google::LogSeverity severity, const char* full_filename,
const char* base_filename, int line,
#if defined(GLOG_VERSION) && GLOG_VERSION >= 6
const google::LogMessageTime& tm_time,
#else
const struct ::tm* tm_time,
#endif
const char* message, size_t message_len) {
// we redirect GLOG_ERROR/GLOG_WARNING message to stderr, others to stdout
if (severity == google::GLOG_ERROR || severity == google::GLOG_WARNING) {
std::cerr << google::LogSink::ToString(severity, full_filename, line,
tm_time, message, message_len)
<< std::endl;
} else {
std::cout << google::LogSink::ToString(severity, full_filename, line,
tm_time, message, message_len)
<< std::endl;
}
}
};
int main(int argc, char* argv[]) {
int exit_code = 0;
// not output any log to stderr by glog.
FLAGS_stderrthreshold = std::numeric_limits<int>::max();
grape::gflags::SetUsageMessage(
"Usage: mpiexec [mpi_opts] ./grape_engine [grape_opts]");
if (argc == 1) {
grape::gflags::ShowUsageWithFlagsRestrict(argv[0], "core/flags");
exit(1);
}
grape::gflags::ParseCommandLineFlags(&argc, &argv, true);
grape::gflags::ShutDownCommandLineFlags();
google::InitGoogleLogging(argv[0]);
google::InstallFailureSignalHandler();
RedirectLogSink redirect_log_sink;
google::AddLogSink(&redirect_log_sink);
// Init MPI
grape::InitMPIComm();
auto host = FLAGS_host;
auto port = FLAGS_port;
auto dag_file = FLAGS_dag_file;
if (dag_file.empty()) {
grape_engine_ptr = std::make_shared<gs::GrapeEngine>(host, port);
} else {
grape_engine_ptr = std::make_shared<gs::GrapeEngine>(dag_file);
}
InstallSignalHandlers(&master_signal_handler);
grape_engine_ptr->Start();
if (!dag_file.empty()) {
exit_code = grape_engine_ptr->RunDAGFile();
}
grape::FinalizeMPIComm();
//////////////////////////////////////////////
google::ShutdownGoogleLogging();
return exit_code;
}