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