fs_mgr.cpp
8.67 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
* Copyright 2002-2014 the original author or authors.
*
* 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
*
* CC/LICENSE
*
* 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 "ims_lock.h"
#include "ims_log.h"
#include "ims_tool.h"
#include "fs_mgr.h"
static const char* cut_path(const char* in) {
const char* p, *ret = in;
char delims[] = "/\\";
char* i;
for (i = delims; *i; i++) {
p = in;
while ((p = strchr(p, *i)) != 0) {
ret = ++p;
}
}
return ret;
}
void esl_log_ims(const char* file, const char* func, int line, int level, const char* fmt, ...) {
const char* fp;
char* data;
va_list ap;
int ret;
fp = cut_path(file);
va_start(ap, fmt);
ret = vasprintf(&data, fmt, ap);
if (ret != -1) {
BGCC_TRACE("esl", "%s:%d %s() %s", fp, line, func, data);
/*
if(level<=3){//ERROR
FATAL_LOG("%s:%d %s() %s", fp, line, func, data);
}
else if(4==level){//WARING
WARNING_LOG("%s:%d %s() %s", fp, line, func, data);
}
else if(5==level){//NOTICE
NOTICE_LOG("%s:%d %s() %s", fp, line, func, data);
}
else if(6==level){//trace
TRACE_LOG("%s:%d %s() %s", fp, line, func, data);
}
else{//DEBUG
DEBUG_LOG("%s:%d %s() %s", fp, line, func, data);
}
*/
free(data);
}
va_end(ap);
}
int32_t fs_mgr_t::fetch_fs_no(uint32_t ip, uint32_t& fs_no) {
bgcc::Guard<bgcc::Mutex> lock(&_info_mutex);
if (!lock.is_locked()) {
WARNING_LOG("fetch fs_no failed(lock falied).");
return IMS_FAIL_LOCK;
}
for (std::map<uint32_t, fs_info_t*>::iterator it = _fsinfo.begin();
it != _fsinfo.end(); ++it) {
if (it->second && ip == it->second->get_address()) {
fs_no = it->first;
TRACE_LOG("fetch fs_no=%u success.", fs_no);
return IMS_SUCCESS;
} else if (it->second && 0x7f000001 == it->second->get_address()) {
if (ims_tool_t::is_local_ipv4(ip)) {
fs_no = it->first;
TRACE_LOG("fetch fs_no=%u success.", fs_no);
return IMS_SUCCESS;
}
}
}
return IMS_FAIL_GENERAL;
}
int32_t fs_mgr_t::clear_fs() {
{
bgcc::Guard<bgcc::Mutex> lock(&_info_mutex);
if (!lock.is_locked()) {
return IMS_FAIL_LOCK;
}
for (std::map<uint32_t, fs_info_t*>::iterator it = _fsinfo.begin();
it != _fsinfo.end(); ++it) {
if (it->second) {
it->second->uninit();
}
}
_fsinfo.clear();
}
{
//删除运行信息
rw_lock_t lock(_runinfo_rwlock, true);
if (!lock.locked()) {
return IMS_FAIL_LOCK;
}
_runinfo.clear();
}
return IMS_SUCCESS;
}
void fs_mgr_t::update_log(bool open) {
esl_global_set_logger(open ? esl_log_ims : NULL);
}
int32_t fs_mgr_t::update_runinfo(uint32_t fs_no, event_data_heartbeat_t& run) {
rw_lock_t lock(_runinfo_rwlock, true);
if (!lock.locked()) {
return IMS_FAIL_LOCK;
}
run.used_times = 0;
event_data_heartbeat_t* pinfo = NULL;
if (_runinfo.count(fs_no) == 0 || NULL == _runinfo[fs_no]) {
pinfo = _evt_hb_pool.construct();
} else {
pinfo = _runinfo[fs_no];
}
if (NULL != pinfo) {
_runinfo[fs_no] = pinfo;
memcpy(pinfo, &run, sizeof(run));
return IMS_SUCCESS;
}
return IMS_FAIL_MEM;
}
int32_t fs_mgr_t::del_fs(uint32_t fs_no) {
{
//删除链接信息
bgcc::Guard<bgcc::Mutex> lock(&_info_mutex);
if (!lock.is_locked()) {
return IMS_FAIL_LOCK;
}
fs_info_t* fsinfo = NULL;
if (_fsinfo.count(fs_no) == 0) {
return IMS_FAIL_NOTEXISTS;
}
fsinfo = _fsinfo[fs_no];
if (fsinfo) {
fsinfo->uninit();
}
_fsinfo.erase(fs_no);
}
{
//删除运行信息
rw_lock_t lock(_runinfo_rwlock, true);
if (!lock.locked()) {
return IMS_FAIL_LOCK;
}
_runinfo.erase(fs_no);
}
return IMS_SUCCESS;
}
int32_t fs_mgr_t::add_fs(fs_info_t& info) {
bgcc::Guard<bgcc::Mutex> lock(&_info_mutex);
if (!lock.is_locked()) {
return IMS_FAIL_LOCK;
}
if (_fsinfo.count(info.get_no()) != 0) {
return IMS_FAIL_EXISTS;
}
//fs_info_t *pinfo=_rp.createp<fs_info_t>(info);
fs_info_t* pinfo = _fsinfo_pool.construct(info);
if (NULL == pinfo) {
return IMS_FAIL_MEM;
}
int32_t ret = pinfo->init();
if (IMS_SUCCESS != ret) {
return ret;
}
_fsinfo[info.get_no()] = pinfo;
if (_fsinfo.count(info.get_no()) == 0) {
return IMS_FAIL_GENERAL;
}
event_data_heartbeat_t data;
data.cur_session = 0;
data.all_session = 0;
data.cpu_idle = 100;
data.used_times = 0;
update_runinfo(pinfo->get_no(), data);
return IMS_SUCCESS;
}
int32_t fs_mgr_t::fetch_opr(uint32_t fs_no, fs_opr_t** opr) {
bgcc::Guard<bgcc::Mutex> lock(&_info_mutex);
if (!lock.is_locked()) {
WARNING_LOG("fs[%u] fetch_opr failed, IMS_FAIL_LOCK", fs_no);
return IMS_FAIL_LOCK;
}
fs_info_t* fs_info;
if (_fsinfo.count(fs_no) == 0) {
WARNING_LOG("fs[%u] fetch_opr failed, IMS_FAIL_INVALID_FSNO", fs_no);
return IMS_FAIL_INVALID_FSNO;
}
fs_info = _fsinfo[fs_no];
if (fs_info) {
return fs_info->fetch(opr);
} else {
WARNING_LOG("fs[%u] fetch_opr failed, IMS_FAIL_MEM", fs_no);
return IMS_FAIL_MEM;
}
}
int32_t fs_mgr_t::free_opr(fs_opr_t** opr) {
if (!opr || !(*opr)) {
return IMS_FAIL_MEM;
}
bgcc::Guard<bgcc::Mutex> lock(&_info_mutex);
if (!lock.is_locked()) {
return IMS_FAIL_LOCK;
}
fs_info_t* fs_info;
if (_fsinfo.count((*opr)->get_fs_no()) == 0) {
return IMS_FAIL_INVALID_FSNO;
}
fs_info = _fsinfo[(*opr)->get_fs_no()];
if (fs_info) {
return fs_info->free(opr);
} else {
return IMS_FAIL_MEM;
}
}
int32_t fs_mgr_t::decision_fs(uint32_t& fs_no) {
rw_lock_t lock(_runinfo_rwlock, true);
if (!lock.locked()) {
WARNING_LOG("decision_fs failed. IMS_FAIL_LOCK ");
return IMS_FAIL_LOCK;
}
if (_runinfo.begin() == _runinfo.end()) {
WARNING_LOG("decision_fs failed. IMS_FAIL_NOTEXISTS");
return IMS_FAIL_NOTEXISTS;
}
fs_no = 0xffff;
event_data_heartbeat_t fs_data;
fs_data.cpu_idle = 0;
fs_data.cur_session = 0xaaaa; //don't use 0xffff to avoid overflow when added.
fs_data.used_times = 0xaaaa;
//int heavyload_node_count = 0; //num of freeswitch node reached the utmost-heavy load
std::map<uint32_t, event_data_heartbeat_t*>::iterator iter = _runinfo.begin();
event_data_heartbeat_t* final = NULL;
while (iter != _runinfo.end()) {
uint32_t key = iter->first;
event_data_heartbeat_t* curr = iter->second;
bool change = false;
if (curr->cur_session + curr->used_times * 2 < fs_data.cur_session + fs_data.used_times * 2) {
change = true;
} else if (curr->cur_session + curr->used_times * 2 == fs_data.cur_session + fs_data.used_times * 2) {
if (key < fs_no) {
change = true;
}
}
DEBUG_LOG("fs_no(=%u), cpu_idle(=%u), cur_session(=%u), 2*used_times(=%u), all_session(=%u)",
key, curr->cpu_idle, curr->cur_session, curr->used_times * 2, curr->all_session);
if (change) {
fs_no = key;
fs_data.cpu_idle = curr->cpu_idle;
fs_data.cur_session = curr->cur_session;
fs_data.used_times = curr->used_times;
final = curr;
}
++iter;
}
// return IMS_FAIL_GENERAL when all freeswitch reached the utmost-heavy load
// reject the next call to ensure the service quality of current calls.
//if(heavyload_node_count == _runinfo.size()) {
// return IMS_FAIL_ALL_BUSY;
//}
if ((~0) == fs_no) {
return IMS_FAIL_GENERAL;
} else {
if (final) {
++ final->used_times;
}
return IMS_SUCCESS;
}
}
/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */