-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler.cpp
More file actions
330 lines (289 loc) · 5.94 KB
/
scheduler.cpp
File metadata and controls
330 lines (289 loc) · 5.94 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <string.h>
#include "capi.h"
#include "worker.h"
#include "cnodemodule.h"
#include "scheduler.h"
#define MAX_WORKER_COUNT 255
Scheduler::Scheduler()
: main_node_id_(0)
{
}
Scheduler::~Scheduler()
{
}
bool Scheduler::Create()
{
int worker_count;
std::string mainnode;
std::string nodeconfig;
lua_State* L = luaL_newstate();
luaL_dofile(L, "config.lua");
// worker count
lua_getglobal(L, "worker");
worker_count = luaL_checkint(L, -1);
if (worker_count == 0)
worker_count = 1;
else if (worker_count > MAX_WORKER_COUNT)
worker_count = MAX_WORKER_COUNT;
lua_pop(L, 1);
// main node
lua_getglobal(L, "main");
if (!lua_istable(L, -1))
{
lua_close(L);
return false;
}
lua_getfield(L, -1, "name");
const char* sz = luaL_checkstring(L, -1);
if (sz == nullptr || strlen(sz) == 0)
{
lua_close(L);
return false;
}
mainnode = sz;
lua_pop(L, 1);
lua_getfield(L, -1, "config");
sz = lua_tostring(L, -1);
if (sz)
{
nodeconfig = sz;
}
lua_pop(L, 2);
// load lnode
for (size_t i = 0; i < (size_t)worker_count; ++i)
{
Worker* worker = new Worker(this, (unsigned int)(i + 1) << 24);
if (!worker->Create())
return false;
workers_.push_back(worker);
}
// load cnode
lua_getglobal(L, "cnode");
if (lua_istable(L, -1))
{
int cnode_count = luaL_len(L, -1);
for (int i = 1; i <= cnode_count; ++i)
{
unsigned int node_id = i + 1;
std::string node_name;
std::string lib_name;
std::string node_config;
lua_rawgeti(L, -1, i);
// node name
lua_getfield(L, -1, "name");
node_name = luaL_checkstring(L, -1);
lua_pop(L, 1);
// lib name
lua_getfield(L, -1, "lib");
lib_name = luaL_checkstring(L, -1);
lua_pop(L, 1);
// node config
lua_getfield(L, -1, "config");
if (!lua_isnil(L, -1))
{
node_config = luaL_checkstring(L, -1);
}
lua_pop(L, 1);
if (!LoadCnode(lib_name, node_id, node_name, node_config))
{
lua_close(L);
return false;
}
}
}
lua_close(L);
// spawn main node
main_node_id_ = SpawnNode(mainnode.c_str(), nodeconfig.c_str());
if (main_node_id_ == 0)
{
return false;
}
return true;
}
void Scheduler::Close()
{
// stop lnode
for (WorkerVec::iterator it = workers_.begin();
it != workers_.end(); ++it)
{
Worker* worker = *it;
worker->Destroy();
}
// stop cnode
for (CnodeInstMap::iterator it = cnodeinsts_.begin();
it != cnodeinsts_.end(); ++it)
{
CnodeInst& inst = it->second;
Cnode* node = inst.node;
node->Close();
}
// clear cnode
for (CnodeInstMap::iterator it = cnodeinsts_.begin();
it != cnodeinsts_.end(); ++it)
{
CnodeInst& inst = it->second;
Cnode* node = inst.node;
module->ReleaseCnode(node);
}
cnodeinsts_.clear();
for (CnodeModuleMap::iterator it = cnode_modules_.begin();
it != cnode_modules_.end(); ++it)
{
CnodeModule* module = it->second;
module->Close();
delete module;
}
cnode_modules_.clear();
// clear lnode
for (WorkerVec::iterator it = workers_.begin();
it != workers_.end(); ++it)
{
Worker* worker = *it;
delete worker;
}
workers_.clear();
}
unsigned int Scheduler::SpawnNode(
const char* name,
const char* config)
{
if (name == nullptr)
return 0;
int worker_idx = NextWorkerIdx();
Worker* worker = workers_[worker_idx];
if (worker == nullptr)
return 0;
return worker->SpawnNode(name, config);
}
void Scheduler::CloseNode(unsigned int nid)
{
if (nid == 0)
{
return;
}
else if (nid == 1)
{
// scheduler
// internal quit
// TODO
}
else
{
Worker* worker = GetWorkerByNodeId(nid);
if (worker == nullptr)
return;
worker->CloseNode(nid);
}
}
unsigned int Scheduler::GetCnodeId(const char* name)
{
if (name == nullptr)
return 0;
std::string strname(name);
for (CnodeInstMap::iterator it = cnodeinsts_.begin();
it != cnodeinsts_.end(); ++it)
{
CnodeInst& inst = it->second;
if (inst.name == strname)
return it->first;
}
return 0;
}
void Scheduler::SendMsg(const Message& msg)
{
if (msg.to == 0)
{
return;
}
else if (msg.to == 1) // send to scheduler
{
// send to main lnode temporarily
Message redirect_msg = msg;
redirect_msg.to = main_node_id_;
Worker* worker = GetWorkerByNodeId(redirect_msg.to);
if (worker == nullptr)
return;
worker->SendMsg(redirect_msg);
}
else if (msg.to < workers_[0]->GetId()) // send to cnode
{
CnodeInstMap::iterator it = cnodeinsts_.find(msg.to);
if (it != cnodeinsts_.end())
{
CnodeInst& inst = it->second;
Cnode* node = inst.node;
node->OnMessage(msg);
}
}
else // send to lnode
{
Worker* worker = GetWorkerByNodeId(msg.to);
if (worker == nullptr)
return;
worker->SendMsg(msg);
}
}
int Scheduler::NextWorkerIdx()
{
return counter_.fetch_add(1) % workers_.size();
}
Worker* Scheduler::GetWorkerByNodeId(unsigned int nid)
{
if (nid == 0)
return nullptr;
size_t worker_idx = (nid >> 24) - 1;
if (worker_idx >= workers_.size())
return nullptr;
Worker* w = workers_[worker_idx];
return w;
}
bool Scheduler::LoadCnode(
const std::string& libname,
unsigned int id,
const std::string& name,
const std::string& config)
{
// find whether has the same name cnode
for (CnodeInstMap::iterator it = cnodeinsts_.begin();
it != cnodeinsts_.end(); ++it)
{
CnodeInst& inst = it->second;
if (inst.name == name)
return false;
}
// load module
CnodeModule* module = nullptr;
CnodeModuleMap::iterator it = cnode_modules_.find(libname);
if (it == cnode_modules_.end())
{
CnodeModule* new_module = new CnodeModule;
if (!new_module->Load(libname.c_str()))
{
delete module;
return false;
}
cnode_modules_.insert(std::make_pair(libname, new_module));
module = new_module;
}
else
{
module = it->second;
}
// create node
Cnode* node = module->CreateCnode();
if (node)
{
if (!node->Create(id, this, config.c_str()))
{
module->ReleaseCnode(node);
return false;
}
CnodeInst inst;
inst.name = name;
inst.node = node;
inst.module = module;
cnodeinsts_.insert(std::make_pair(id, inst));
return true;
}
return false;
}