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