1 | #include "module-stat.h"
|
---|
2 | #include "module-cccam.h"
|
---|
3 |
|
---|
4 | #define UNDEF_AVG_TIME 80000
|
---|
5 | #define MAX_ECM_SEND_CACHE 16
|
---|
6 |
|
---|
7 | static int stat_load_save;
|
---|
8 | static struct timeb nulltime;
|
---|
9 |
|
---|
10 | void init_stat()
|
---|
11 | {
|
---|
12 | cs_ftime(&nulltime);
|
---|
13 | stat_load_save = -100;
|
---|
14 |
|
---|
15 | //checking config
|
---|
16 | if (cfg->lb_nbest_readers < 2)
|
---|
17 | cfg->lb_nbest_readers = DEFAULT_NBEST;
|
---|
18 | if (cfg->lb_nfb_readers < 2)
|
---|
19 | cfg->lb_nfb_readers = DEFAULT_NFB;
|
---|
20 | if (cfg->lb_min_ecmcount < 1)
|
---|
21 | cfg->lb_min_ecmcount = DEFAULT_MIN_ECM_COUNT;
|
---|
22 | if (cfg->lb_max_ecmcount < 2)
|
---|
23 | cfg->lb_max_ecmcount = DEFAULT_MAX_ECM_COUNT;
|
---|
24 | if (cfg->lb_reopen_seconds < 10)
|
---|
25 | cfg->lb_reopen_seconds = DEFAULT_REOPEN_SECONDS;
|
---|
26 | if (cfg->lb_retrylimit <= 0)
|
---|
27 | cfg->lb_retrylimit = DEFAULT_RETRYLIMIT;
|
---|
28 | }
|
---|
29 |
|
---|
30 | void load_stat_from_file()
|
---|
31 | {
|
---|
32 | stat_load_save = 0;
|
---|
33 | char buf[256];
|
---|
34 | sprintf(buf, "%s/stat", get_tmp_dir());
|
---|
35 | FILE *file = fopen(buf, "r");
|
---|
36 | if (!file) {
|
---|
37 | cs_log("loadbalancer: can't read from file %s", buf);
|
---|
38 | return;
|
---|
39 | }
|
---|
40 | cs_debug_mask(D_TRACE, "loadbalancer: load statistics from %s", buf);
|
---|
41 |
|
---|
42 | struct s_reader *rdr = NULL;
|
---|
43 | READER_STAT *stat, *dup;
|
---|
44 |
|
---|
45 | int i=1;
|
---|
46 | int count=0;
|
---|
47 | do
|
---|
48 | {
|
---|
49 | stat = malloc(sizeof(READER_STAT));
|
---|
50 | memset(stat, 0, sizeof(READER_STAT));
|
---|
51 | i = fscanf(file, "%s rc %d caid %04hX prid %06lX srvid %04hX time avg %dms ecms %d last %ld\n",
|
---|
52 | buf, &stat->rc, &stat->caid, &stat->prid, &stat->srvid,
|
---|
53 | &stat->time_avg, &stat->ecm_count, &stat->last_received);
|
---|
54 |
|
---|
55 |
|
---|
56 | if (i > 5) {
|
---|
57 | if (rdr == NULL || strcmp(buf, rdr->label) != 0) {
|
---|
58 | for (rdr=first_reader; rdr ; rdr=rdr->next) {
|
---|
59 | if (strcmp(rdr->label, buf) == 0) {
|
---|
60 | break;
|
---|
61 | }
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | if (rdr != NULL && strcmp(buf, rdr->label) == 0) {
|
---|
66 | if (!rdr->lb_stat)
|
---|
67 | rdr->lb_stat = ll_create();
|
---|
68 |
|
---|
69 | //Duplicate check:
|
---|
70 | dup = get_stat(rdr, stat->caid, stat->prid, stat->srvid);
|
---|
71 | if (dup)
|
---|
72 | free(stat); //already loaded
|
---|
73 | else {
|
---|
74 |
|
---|
75 | ll_append(rdr->lb_stat, stat);
|
---|
76 | count++;
|
---|
77 | }
|
---|
78 | }
|
---|
79 | else
|
---|
80 | {
|
---|
81 | cs_log("loadbalancer: statistics could not be loaded for %s", buf);
|
---|
82 | free(stat);
|
---|
83 | }
|
---|
84 | }
|
---|
85 | else if (i!=EOF && i>0)
|
---|
86 | {
|
---|
87 | cs_debug_mask(D_TRACE, "loadbalancer: statistics ERROR %s rc=%d i=%d", buf, stat->rc, i);
|
---|
88 | free(stat);
|
---|
89 | }
|
---|
90 | } while(i!=EOF && i>0);
|
---|
91 | fclose(file);
|
---|
92 | cs_debug_mask(D_TRACE, "loadbalancer: statistic loaded %d records", count);
|
---|
93 | }
|
---|
94 | /**
|
---|
95 | * get statistic values for reader ridx and caid/prid/srvid
|
---|
96 | */
|
---|
97 | READER_STAT *get_stat(struct s_reader *rdr, ushort caid, ulong prid, ushort srvid)
|
---|
98 | {
|
---|
99 | if (!rdr->lb_stat)
|
---|
100 | rdr->lb_stat = ll_create();
|
---|
101 |
|
---|
102 | LL_ITER *it = ll_iter_create(rdr->lb_stat);
|
---|
103 | READER_STAT *stat = NULL;
|
---|
104 | int i = 0;
|
---|
105 | while ((stat = ll_iter_next(it))) {
|
---|
106 | if (stat->caid==caid && stat->prid==prid && stat->srvid==srvid) {
|
---|
107 | if (i > 5) { //Move to first if not under top 5:
|
---|
108 | ll_iter_remove(it);
|
---|
109 | ll_insert_at_nolock(rdr->lb_stat, stat, 0); //move to first!
|
---|
110 | }
|
---|
111 | break;
|
---|
112 | }
|
---|
113 | i++;
|
---|
114 | }
|
---|
115 | ll_iter_release(it);
|
---|
116 | return stat;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * removes caid/prid/srvid from stat-list of reader ridx
|
---|
121 | */
|
---|
122 | int remove_stat(struct s_reader *rdr, ushort caid, ulong prid, ushort srvid)
|
---|
123 | {
|
---|
124 | if (!rdr->lb_stat)
|
---|
125 | return 0;
|
---|
126 |
|
---|
127 | int c = 0;
|
---|
128 | LL_ITER *it = ll_iter_create(rdr->lb_stat);
|
---|
129 | READER_STAT *stat;
|
---|
130 | while ((stat = ll_iter_next(it))) {
|
---|
131 | if (stat->caid==caid && stat->prid==prid && stat->srvid==srvid) {
|
---|
132 | ll_iter_remove_data(it);
|
---|
133 | c++;
|
---|
134 | }
|
---|
135 | }
|
---|
136 | ll_iter_release(it);
|
---|
137 | return c;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Calculates average time
|
---|
142 | */
|
---|
143 | void calc_stat(READER_STAT *stat)
|
---|
144 | {
|
---|
145 | int i;
|
---|
146 | int c=0;
|
---|
147 | long t = 0;
|
---|
148 | for (i = 0; i < LB_MAX_STAT_TIME; i++) {
|
---|
149 | if (stat->time_stat[i] > 0) {
|
---|
150 | t += (long)stat->time_stat[i];
|
---|
151 | c++;
|
---|
152 | }
|
---|
153 | }
|
---|
154 | if (!c)
|
---|
155 | stat->time_avg = UNDEF_AVG_TIME;
|
---|
156 | else
|
---|
157 | stat->time_avg = t / c;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Saves statistik to /tmp/.oscam/stat.n where n is reader-index
|
---|
162 | */
|
---|
163 | void save_stat_to_file()
|
---|
164 | {
|
---|
165 | stat_load_save = 0;
|
---|
166 | char buf[256];
|
---|
167 | sprintf(buf, "%s/stat", get_tmp_dir());
|
---|
168 |
|
---|
169 | FILE *file = fopen(buf, "w");
|
---|
170 | if (!file) {
|
---|
171 | cs_log("can't write to file %s", buf);
|
---|
172 | return;
|
---|
173 | }
|
---|
174 |
|
---|
175 | int count=0;
|
---|
176 | struct s_reader *rdr;
|
---|
177 | for (rdr=first_reader; rdr ; rdr=rdr->next) {
|
---|
178 |
|
---|
179 | if (rdr->lb_stat) {
|
---|
180 | LL_ITER *it = ll_iter_create(rdr->lb_stat);
|
---|
181 | READER_STAT *stat;
|
---|
182 | while ((stat = ll_iter_next(it))) {
|
---|
183 |
|
---|
184 | fprintf(file, "%s rc %d caid %04hX prid %06lX srvid %04hX time avg %dms ecms %d last %ld\n",
|
---|
185 | rdr->label, stat->rc, stat->caid, stat->prid,
|
---|
186 | stat->srvid, stat->time_avg, stat->ecm_count, stat->last_received);
|
---|
187 | count++;
|
---|
188 | }
|
---|
189 | ll_iter_release(it);
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | fclose(file);
|
---|
194 | cs_log("loadbalancer: statistic saved %d records", count);
|
---|
195 | }
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Adds caid/prid/srvid to stat-list for reader ridx with time/rc
|
---|
199 | */
|
---|
200 | void add_stat(struct s_reader *rdr, ECM_REQUEST *er, int ecm_time, int rc)
|
---|
201 | {
|
---|
202 | if (!rdr || !er)
|
---|
203 | return;
|
---|
204 | READER_STAT *stat = get_stat(rdr, er->caid, er->prid, er->srvid);
|
---|
205 | if (!stat) {
|
---|
206 | stat = malloc(sizeof(READER_STAT));
|
---|
207 | memset(stat, 0, sizeof(READER_STAT));
|
---|
208 | stat->caid = er->caid;
|
---|
209 | stat->prid = er->prid;
|
---|
210 | stat->srvid = er->srvid;
|
---|
211 | stat->time_avg = UNDEF_AVG_TIME; //dummy placeholder
|
---|
212 | ll_append(rdr->lb_stat, stat);
|
---|
213 | }
|
---|
214 |
|
---|
215 | //inc ecm_count if found, drop to 0 if not found:
|
---|
216 | // rc codes:
|
---|
217 | // 0 = found +
|
---|
218 | // 1 = cache1 #
|
---|
219 | // 2 = cache2 #
|
---|
220 | // 3 = emu +
|
---|
221 | // 4 = not found -
|
---|
222 | // 5 = timeout -2
|
---|
223 | // 6 = sleeping #
|
---|
224 | // 7 = fake #
|
---|
225 | // 8 = invalid #
|
---|
226 | // 9 = corrupt #
|
---|
227 | // 10= no card #
|
---|
228 | // 11= expdate #
|
---|
229 | // 12= disabled #
|
---|
230 | // 13= stopped #
|
---|
231 | // 100= unhandled #
|
---|
232 | // + = adds statistic values
|
---|
233 | // # = ignored because of duplicate values, temporary failures or softblocks
|
---|
234 | // - = causes loadbalancer to block this reader for this caid/prov/sid
|
---|
235 | // -2 = causes loadbalancer to block if happens too often
|
---|
236 |
|
---|
237 | if (stat->ecm_count < 0)
|
---|
238 | stat->ecm_count=0;
|
---|
239 |
|
---|
240 | if (rc == 0) { //found
|
---|
241 | stat->rc = 0;
|
---|
242 | stat->ecm_count++;
|
---|
243 | stat->last_received = time(NULL);
|
---|
244 | stat->request_count = 0;
|
---|
245 |
|
---|
246 | //FASTEST READER:
|
---|
247 | stat->time_idx++;
|
---|
248 | if (stat->time_idx >= LB_MAX_STAT_TIME)
|
---|
249 | stat->time_idx = 0;
|
---|
250 | stat->time_stat[stat->time_idx] = ecm_time;
|
---|
251 | calc_stat(stat);
|
---|
252 |
|
---|
253 | //OLDEST READER now set by get best reader!
|
---|
254 |
|
---|
255 |
|
---|
256 | //USAGELEVEL:
|
---|
257 | int ule = rdr->lb_usagelevel_ecmcount;
|
---|
258 | if (ule > 0 && ((ule / cfg->lb_min_ecmcount) > 0)) //update every MIN_ECM_COUNT usagelevel:
|
---|
259 | {
|
---|
260 | time_t t = (time(NULL)-rdr->lb_usagelevel_time);
|
---|
261 | rdr->lb_usagelevel = 1000/(t<1?1:t);
|
---|
262 | ule = 0;
|
---|
263 | }
|
---|
264 | if (ule == 0)
|
---|
265 | rdr->lb_usagelevel_time = time(NULL);
|
---|
266 | rdr->lb_usagelevel_ecmcount = ule+1;
|
---|
267 |
|
---|
268 | }
|
---|
269 | else if (rc == 1 || rc == 2) { //cache
|
---|
270 | //no increase of statistics here, cachetime is not real time
|
---|
271 | stat->last_received = time(NULL);
|
---|
272 | stat->request_count = 0;
|
---|
273 | }
|
---|
274 | else if (rc == 4) { //not found
|
---|
275 | stat->rc = rc;
|
---|
276 | //stat->last_received = time(NULL); do not change time, this would prevent reopen
|
---|
277 | //stat->ecm_count = 0; Keep ecm_count!
|
---|
278 | }
|
---|
279 | else if (rc == 5) { //timeout
|
---|
280 | stat->request_count++;
|
---|
281 | stat->last_received = time(NULL);
|
---|
282 |
|
---|
283 | //add timeout to stat:
|
---|
284 | if (ecm_time<=0)
|
---|
285 | ecm_time = cfg->ctimeout;
|
---|
286 | stat->time_idx++;
|
---|
287 | if (stat->time_idx >= LB_MAX_STAT_TIME)
|
---|
288 | stat->time_idx = 0;
|
---|
289 | stat->time_stat[stat->time_idx] = ecm_time;
|
---|
290 | calc_stat(stat);
|
---|
291 | }
|
---|
292 | else
|
---|
293 | {
|
---|
294 | if (rc >= 0)
|
---|
295 | cs_debug_mask(D_TRACE, "loadbalancer: not handled stat for reader %s: rc %d caid %04hX prid %06lX srvid %04hX time %dms usagelevel %d",
|
---|
296 | rdr->label, rc, er->caid, er->prid, er->srvid, ecm_time, rdr->lb_usagelevel);
|
---|
297 |
|
---|
298 | return;
|
---|
299 | }
|
---|
300 |
|
---|
301 | cs_debug_mask(D_TRACE, "loadbalancer: adding stat for reader %s: rc %d caid %04hX prid %06lX srvid %04hX time %dms usagelevel %d",
|
---|
302 | rdr->label, rc, er->caid, er->prid, er->srvid, ecm_time, rdr->lb_usagelevel);
|
---|
303 |
|
---|
304 | //debug only:
|
---|
305 | if (cfg->lb_save) {
|
---|
306 | stat_load_save++;
|
---|
307 | if (stat_load_save > cfg->lb_save) {
|
---|
308 | save_stat_to_file();
|
---|
309 | }
|
---|
310 | }
|
---|
311 | }
|
---|
312 |
|
---|
313 | void reset_stat(ushort caid, ulong prid, ushort srvid)
|
---|
314 | {
|
---|
315 | //cs_debug_mask(D_TRACE, "loadbalance: resetting ecm count");
|
---|
316 | struct s_reader *rdr;
|
---|
317 | for (rdr=first_reader; rdr ; rdr=rdr->next) {
|
---|
318 | if (rdr->lb_stat && rdr->client) {
|
---|
319 | READER_STAT *stat = get_stat(rdr, caid, prid, srvid);
|
---|
320 | if (stat) {
|
---|
321 | if (stat->ecm_count > 0)
|
---|
322 | stat->ecm_count = 1; //not zero, so we know it's decodeable
|
---|
323 | stat->rc = 0;
|
---|
324 | stat->request_count = 0;
|
---|
325 | }
|
---|
326 | }
|
---|
327 | }
|
---|
328 | }
|
---|
329 |
|
---|
330 | int has_ident(FTAB *ftab, ECM_REQUEST *er) {
|
---|
331 |
|
---|
332 | if (!ftab || !ftab->filts)
|
---|
333 | return 0;
|
---|
334 |
|
---|
335 | int j, k;
|
---|
336 |
|
---|
337 | for (j = 0; j < ftab->nfilts; j++) {
|
---|
338 | if (ftab->filts[j].caid) {
|
---|
339 | if (ftab->filts[j].caid==er->caid) { //caid matches!
|
---|
340 |
|
---|
341 | int nprids = ftab->filts[j].nprids;
|
---|
342 | if (!nprids) // No Provider ->Ok
|
---|
343 | return 1;
|
---|
344 |
|
---|
345 | for (k = 0; k < nprids; k++) {
|
---|
346 | ulong prid = ftab->filts[j].prids[k];
|
---|
347 | if (prid == er->prid) { //Provider matches
|
---|
348 | return 1;
|
---|
349 | }
|
---|
350 | }
|
---|
351 | }
|
---|
352 | }
|
---|
353 | }
|
---|
354 | return 0; //No match!
|
---|
355 | }
|
---|
356 |
|
---|
357 |
|
---|
358 |
|
---|
359 | /**
|
---|
360 | * Gets best reader for caid/prid/srvid.
|
---|
361 | * Best reader is evaluated by lowest avg time but only if ecm_count > cfg->lb_min_ecmcount (5)
|
---|
362 | * Also the reader is asked if he is "available"
|
---|
363 | * returns ridx when found or -1 when not found
|
---|
364 | */
|
---|
365 | int get_best_reader(ECM_REQUEST *er)
|
---|
366 | {
|
---|
367 | int i;
|
---|
368 | int rdr_count = er->rdr_count;
|
---|
369 | uint8 result[rdr_count];
|
---|
370 | memset(result, 0, sizeof(result));
|
---|
371 | int re[rdr_count];
|
---|
372 | int ti[rdr_count];
|
---|
373 |
|
---|
374 | //resulting values:
|
---|
375 | memset(re, 0, sizeof(re));
|
---|
376 | memset(ti, 0, sizeof(ti));
|
---|
377 |
|
---|
378 | struct timeb new_nulltime;
|
---|
379 | memset(&new_nulltime, 0, sizeof(new_nulltime));
|
---|
380 | time_t current_time = time(NULL);
|
---|
381 |
|
---|
382 | int current = -1;
|
---|
383 |
|
---|
384 | READER_STAT *stat = NULL;
|
---|
385 | struct s_reader *rdr;
|
---|
386 |
|
---|
387 | int nlocal_readers = 0;
|
---|
388 |
|
---|
389 | #ifdef WITH_DEBUG
|
---|
390 | char rdrs[rdr_count+1];
|
---|
391 | for (i=0;i<rdr_count;i++)
|
---|
392 | rdrs[i] = er->matching_rdr[i]+'0';
|
---|
393 | rdrs[rdr_count]=0;
|
---|
394 | cs_debug_mask(D_TRACE, "loadbalancer: client %s for %04X/%06X/%04X: valid readers: %s",
|
---|
395 | username(er->client), er->caid, er->prid, er->srvid, rdrs);
|
---|
396 | #endif
|
---|
397 |
|
---|
398 | for (i=0,rdr=first_reader; rdr ; rdr=rdr->next, i++) {
|
---|
399 | if (i >= rdr_count)
|
---|
400 | break;
|
---|
401 |
|
---|
402 | if (er->matching_rdr[i]) {
|
---|
403 | int weight = rdr->lb_weight <= 0?100:rdr->lb_weight;
|
---|
404 |
|
---|
405 | stat = get_stat(rdr, er->caid, er->prid, er->srvid);
|
---|
406 | if (!stat) {
|
---|
407 | cs_debug_mask(D_TRACE, "loadbalancer: starting statistics for reader %s", rdr->label);
|
---|
408 | add_stat(rdr, er, 1, -1);
|
---|
409 | result[i] = 1; //no statistics, this reader is active (now) but we need statistics first!
|
---|
410 | continue;
|
---|
411 | }
|
---|
412 |
|
---|
413 | ti[i] = stat->time_avg;
|
---|
414 |
|
---|
415 | if (stat->ecm_count < 0||(stat->ecm_count > cfg->lb_max_ecmcount && stat->time_avg > (int)cfg->ftimeout)) {
|
---|
416 | cs_debug_mask(D_TRACE, "loadbalancer: max ecms (%d) reached by reader %s, resetting statistics", cfg->lb_max_ecmcount, rdr->label);
|
---|
417 | reset_stat(er->caid, er->prid, er->srvid);
|
---|
418 | result[i] = 1;//max ecm reached, get new statistics
|
---|
419 | }
|
---|
420 |
|
---|
421 | if (stat->rc == 0 && stat->ecm_count < cfg->lb_min_ecmcount) {
|
---|
422 | cs_debug_mask(D_TRACE, "loadbalancer: reader %s needs more statistics", rdr->label);
|
---|
423 | result[i] = 1; //need more statistics!
|
---|
424 | }
|
---|
425 |
|
---|
426 | int hassrvid = has_srvid(rdr->client, er) || has_ident(&rdr->ftab, er);
|
---|
427 |
|
---|
428 | if (!hassrvid && stat->rc == 0 && stat->request_count > cfg->lb_min_ecmcount) { // 5 unanswered requests or timeouts?
|
---|
429 | cs_debug_mask(D_TRACE, "loadbalancer: reader %s does not answer, blocking", rdr->label);
|
---|
430 | add_stat(rdr, er, 1, 4); //reader marked as unuseable
|
---|
431 | result[i] = 0;
|
---|
432 | continue;
|
---|
433 | }
|
---|
434 |
|
---|
435 |
|
---|
436 | //Reader can decode this service (rc==0) and has lb_min_ecmcount ecms:
|
---|
437 | if (stat->rc == 0 || hassrvid) {
|
---|
438 | if (cfg->preferlocalcards && !(rdr->typ & R_IS_NETWORK))
|
---|
439 | nlocal_readers++; //Prefer local readers!
|
---|
440 |
|
---|
441 | switch (cfg->lb_mode) {
|
---|
442 | default:
|
---|
443 | case LB_NONE:
|
---|
444 | //cs_debug_mask(D_TRACE, "loadbalance disabled");
|
---|
445 | result[i] = 1;
|
---|
446 | current = 1;
|
---|
447 | break;
|
---|
448 | case LB_FASTEST_READER_FIRST:
|
---|
449 | current = stat->time_avg * 100 / weight;
|
---|
450 | break;
|
---|
451 | case LB_OLDEST_READER_FIRST:
|
---|
452 | if (!rdr->lb_last.time)
|
---|
453 | rdr->lb_last = nulltime;
|
---|
454 | current = (1000*(rdr->lb_last.time-nulltime.time)+
|
---|
455 | rdr->lb_last.millitm-nulltime.millitm);
|
---|
456 | if (!new_nulltime.time || (1000*(rdr->lb_last.time-new_nulltime.time)+
|
---|
457 | rdr->lb_last.millitm-new_nulltime.millitm) < 0)
|
---|
458 | new_nulltime = rdr->lb_last;
|
---|
459 | break;
|
---|
460 | case LB_LOWEST_USAGELEVEL:
|
---|
461 | current = rdr->lb_usagelevel * 100 / weight;
|
---|
462 | break;
|
---|
463 | }
|
---|
464 | #ifdef WEBIF
|
---|
465 | rdr->lbvalue = current;
|
---|
466 | #endif
|
---|
467 | if (rdr->ph.c_available
|
---|
468 | && !rdr->ph.c_available(rdr,
|
---|
469 | AVAIL_CHECK_LOADBALANCE)) {
|
---|
470 | current=current*2;
|
---|
471 | }
|
---|
472 | if (current < 1)
|
---|
473 | re[i]=1;
|
---|
474 | else
|
---|
475 | re[i] = current;
|
---|
476 | }
|
---|
477 | }
|
---|
478 | }
|
---|
479 |
|
---|
480 | int nbest_readers = cfg->lb_nbest_readers;
|
---|
481 | int nfb_readers = cfg->lb_nfb_readers;
|
---|
482 | if (nlocal_readers > nbest_readers) { //if we have local readers, we prefer them!
|
---|
483 | nlocal_readers = nbest_readers;
|
---|
484 | nbest_readers = 0;
|
---|
485 | }
|
---|
486 | else
|
---|
487 | nbest_readers = nbest_readers-nlocal_readers;
|
---|
488 |
|
---|
489 | int best_ridx = -1;
|
---|
490 | struct s_reader *best_rdr = NULL;
|
---|
491 | struct s_reader *best_rdri = NULL;
|
---|
492 |
|
---|
493 | int n=0;
|
---|
494 | while (1) {
|
---|
495 | int best_idx=-1;
|
---|
496 | int best=0;
|
---|
497 | int j;
|
---|
498 | struct s_reader *rdr2;
|
---|
499 | for (j=0,rdr2=first_reader; rdr2 ; rdr2=rdr2->next, j++) {
|
---|
500 | if (j>=rdr_count)
|
---|
501 | break;
|
---|
502 | if (nlocal_readers && (rdr2->typ & R_IS_NETWORK))
|
---|
503 | continue;
|
---|
504 |
|
---|
505 | if (re[j] && (!best || re[j] < best)) {
|
---|
506 | best_idx=j;
|
---|
507 | best=re[j];
|
---|
508 | best_rdri = rdr2;
|
---|
509 | }
|
---|
510 | }
|
---|
511 | if (best_idx<0)
|
---|
512 | break;
|
---|
513 | else {
|
---|
514 | n++;
|
---|
515 | re[best_idx]=0;
|
---|
516 | if (nlocal_readers) {//primary readers, local
|
---|
517 | nlocal_readers--;
|
---|
518 | result[best_idx] = 1;
|
---|
519 | //OLDEST_READER:
|
---|
520 | cs_ftime(&best_rdri->lb_last);
|
---|
521 | if (best_ridx <0) {
|
---|
522 | best_ridx = best_idx;
|
---|
523 | best_rdr = best_rdri;
|
---|
524 | }
|
---|
525 | }
|
---|
526 | else if (nbest_readers) {//primary readers, other
|
---|
527 | nbest_readers--;
|
---|
528 | result[best_idx] = 1;
|
---|
529 | //OLDEST_READER:
|
---|
530 | cs_ftime(&best_rdri->lb_last);
|
---|
531 | if (best_ridx <0) {
|
---|
532 | best_ridx = best_idx;
|
---|
533 | best_rdr = best_rdri;
|
---|
534 | }
|
---|
535 | }
|
---|
536 | else if (nfb_readers) { //fallbacks:
|
---|
537 | nfb_readers--;
|
---|
538 | if (!result[best_idx])
|
---|
539 | result[best_idx] = 2;
|
---|
540 | }
|
---|
541 | else
|
---|
542 | break;
|
---|
543 | }
|
---|
544 | }
|
---|
545 |
|
---|
546 | if (!n) //no best reader found? reopen if we have ecm_count>0
|
---|
547 | {
|
---|
548 | cs_debug_mask(D_TRACE, "loadbalancer: NO MATCHING READER FOUND, reopen last valid:");
|
---|
549 | for (i=0,rdr=first_reader; rdr ; rdr=rdr->next, i++) {
|
---|
550 | if (i>=rdr_count)
|
---|
551 | break;
|
---|
552 | if (er->matching_rdr[i]) { //primary readers
|
---|
553 | stat = get_stat(rdr, er->caid, er->prid, er->srvid);
|
---|
554 | if (stat && stat->ecm_count>0) {
|
---|
555 | result[i] = 1;
|
---|
556 | n++;
|
---|
557 | cs_debug_mask(D_TRACE, "loadbalancer: reopened reader %s", rdr->label);
|
---|
558 | }
|
---|
559 | }
|
---|
560 | }
|
---|
561 | cs_debug_mask(D_TRACE, "loadbalancer: reopened %d readers", n);
|
---|
562 | }
|
---|
563 |
|
---|
564 | memcpy(er->matching_rdr, result, sizeof(result));
|
---|
565 | #ifdef WITH_DEBUG
|
---|
566 |
|
---|
567 | for (i=0;i<rdr_count;i++)
|
---|
568 | rdrs[i] = er->matching_rdr[i]+'0';
|
---|
569 | rdrs[rdr_count]=0;
|
---|
570 | cs_debug_mask(D_TRACE, "loadbalancer: client %s for %04X/%06X/%04X: %s readers: %s",
|
---|
571 | username(er->client), er->caid, er->prid, er->srvid,
|
---|
572 | best_rdr?best_rdr->label:"NONE", rdrs);
|
---|
573 | #endif
|
---|
574 |
|
---|
575 | //reopen other reader only if responsetime>retrylimit:
|
---|
576 | int reopen = (best_ridx>=0 && ti[best_ridx] && (ti[best_ridx] > cfg->lb_retrylimit));
|
---|
577 | if (reopen)
|
---|
578 | cs_debug_mask(D_TRACE, "loadbalancer: reader %s reached retrylimit (%dms), reopening other readers", best_rdr->label, ti[best_ridx]);
|
---|
579 |
|
---|
580 | for (i=0,rdr=first_reader; rdr ; rdr=rdr->next, i++) {
|
---|
581 | if (i>=rdr_count)
|
---|
582 | break;
|
---|
583 |
|
---|
584 | if (!er->matching_rdr[i])
|
---|
585 | continue;
|
---|
586 |
|
---|
587 | if (result[i] == 1) { //primary readers
|
---|
588 | stat = get_stat(rdr, er->caid, er->prid, er->srvid);
|
---|
589 | //algo for finding unanswered requests (newcamd reader for example:)
|
---|
590 | if (stat && current_time > stat->last_received+(time_t)(cfg->ctimeout/1000)) {
|
---|
591 | stat->request_count++;
|
---|
592 | stat->last_received = current_time;
|
---|
593 | cs_debug_mask(D_TRACE, "loadbalancer: reader %s increment request count to %d", rdr->label, stat->request_count);
|
---|
594 | }
|
---|
595 | } else if (reopen) { //retrylimit reached:
|
---|
596 | stat = get_stat(rdr, er->caid, er->prid, er->srvid);
|
---|
597 | if (stat && stat->rc != 0) {
|
---|
598 | int seconds = cfg->lb_reopen_seconds;
|
---|
599 | if (!rdr->audisabled && (er->client->autoau || er->client->aureader == rdr))
|
---|
600 | seconds = seconds/10; //reopen faster if reader is a au reader
|
---|
601 |
|
---|
602 | if (stat->last_received+seconds < current_time) { //Retrying reader every (900/conf) seconds
|
---|
603 | stat->last_received = current_time;
|
---|
604 | result[i] = 1;
|
---|
605 | cs_log("loadbalancer: retrying reader %s", rdr->label);
|
---|
606 | }
|
---|
607 | }
|
---|
608 | }
|
---|
609 | }
|
---|
610 |
|
---|
611 | if (new_nulltime.time)
|
---|
612 | nulltime = new_nulltime;
|
---|
613 |
|
---|
614 | return 1;
|
---|
615 | }
|
---|
616 |
|
---|
617 | /**
|
---|
618 | * clears statistic of reader ridx.
|
---|
619 | **/
|
---|
620 | void clear_reader_stat(struct s_reader *rdr)
|
---|
621 | {
|
---|
622 | if (!rdr->lb_stat)
|
---|
623 | return;
|
---|
624 |
|
---|
625 | ll_clear_data(rdr->lb_stat);
|
---|
626 | }
|
---|