-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmpsock_scheduler.c
704 lines (605 loc) · 21.9 KB
/
mpsock_scheduler.c
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
#include "mpsock_scheduler.h"
#include "mpsock_pool.h"
#include "mpsock_socket.h"
#include "http_parser.h"
#include "mpsock_connection.h"
#include "mpsock_dns.h"
#include "mpsock_interface.h"
#include "mpsock_scheduler_minimum_throughput_assurance.h"
#include "mpsock_scheduler_algorithms.h"
#include "mpsock_def.h"
mpsock_scheduler *create_scheduler(struct mpsock_connection *connection)
{
mpsock_scheduler *scheduler = (mpsock_scheduler*)malloc(sizeof(mpsock_scheduler));
scheduler->connection = connection;
scheduler->send_start.tv_sec=0;
scheduler->send_start.tv_usec=0;
scheduler->last_read.tv_sec=0;
scheduler->last_read.tv_usec=0;
scheduler->first_read.tv_sec=0;
scheduler->first_read.tv_usec=0;
scheduler->first_recv.tv_sec=0;
scheduler->first_recv.tv_usec=0;
scheduler->tmp_time.tv_sec=0;
scheduler->tmp_time.tv_usec=0;
scheduler->last_byte_read.tv_sec=0;
scheduler->last_byte_read.tv_usec=0;
scheduler->total_number_bytes = 0;
scheduler->rtt = 0;
scheduler->bandwidth = 0;
scheduler->last_bandwidth = 0;
scheduler->num_samples = 0;
scheduler->num_reads = 0;
scheduler->num_sends = 0;
scheduler->alpha = connection->pool->mpsocket->initial_alpha;
scheduler->is_decreased = FALSE;
scheduler->bad_connection = FALSE;
return scheduler;
}
void reinit_scheduler(mpsock_scheduler *scheduler)
{
scheduler->send_start.tv_sec=0;
scheduler->send_start.tv_usec=0;
scheduler->last_read.tv_sec=0;
scheduler->last_read.tv_usec=0;
scheduler->first_read.tv_sec=0;
scheduler->first_read.tv_usec=0;
scheduler->first_recv.tv_sec=0;
scheduler->first_recv.tv_usec=0;
scheduler->tmp_time.tv_sec=0;
scheduler->tmp_time.tv_usec=0;
scheduler->last_byte_read.tv_sec=0;
scheduler->last_byte_read.tv_usec=0;
scheduler->num_reads = 0;
scheduler->num_sends = 0;
scheduler->total_number_bytes = 0;
// TODO: this should only be reinitialized if the connection serves another host (in which case this whole struct would be freed anyways)
//scheduler->num_samples = 0;
//scheduler->rtt = 0;
//scheduler->bandwidth = 0;
//scheduler->last_bandwidth = 0;
//scheduler->alpha = connection->pool->mpsocket->initial_alpha;
//scheduler->bad_connection = FALSE;
return;
}
int performs_bad(mpsock_scheduler *scheduler)
{
// TODO: implement
if(scheduler->bad_connection) return BAD_PERF_CLOSE;
//if(scheduler->connection->chunk_size == 0) return BAD_PERF_CLOSE;
//if((rand()%1000) < 10) return TRUE;
// check whether we did not receive data in a 'long' time
gettimeofday(&(scheduler->tmp_time),NULL);
double tmp_diff = 0;
// TODO: verify with send_start
if(scheduler->last_read.tv_sec < scheduler->send_start.tv_sec)
{
tmp_diff = scheduler->tmp_time.tv_sec - scheduler->send_start.tv_sec;
tmp_diff += (double)(scheduler->tmp_time.tv_usec - scheduler->send_start.tv_usec)/1000000;
}
else
{
// TODO: use different ts than last_read!!!
tmp_diff = scheduler->tmp_time.tv_sec - scheduler->last_byte_read.tv_sec;
tmp_diff += (double)(scheduler->tmp_time.tv_usec - scheduler->last_byte_read.tv_usec)/1000000;
}
if(tmp_diff > MAX_DATA_WAIT)
{
struct timeval tt_ts;
gettimeofday(&tt_ts,NULL);
double tt_diff = tt_ts.tv_sec - global_start_ts.tv_sec;
tt_diff += (double)(tt_ts.tv_usec - global_start_ts.tv_usec)/1000000;
LOG_INFO("%ssd#%d at %fs data receive timeout",RESULT_EVENT,scheduler->connection->sd,tt_diff);
return BAD_PERF_CLOSE;
}
// check whether this is the last chunk in request and if there is a better connection to request it from
if(!scheduler->connection->waiting)
{
if(optimize_final_chunk_request(scheduler))
{
struct timeval tt_ts;
gettimeofday(&tt_ts,NULL);
double tt_diff = tt_ts.tv_sec - global_start_ts.tv_sec;
tt_diff += (double)(tt_ts.tv_usec - global_start_ts.tv_usec)/1000000;
LOG_INFO("%ssd#%d at %fs request last chunk over different connection",RESULT_EVENT,scheduler->connection->sd,tt_diff);
return BAD_PERF_KEEP;
}
}
return FALSE;
}
int partial_req_over_new_con(mpsock_scheduler *scheduler)
{
// TODO: implement
mpsock_pool *pool = scheduler->connection->pool;
// check whether last chunk is already requested
if(pool->next_start_byte >= pool->current_file_size && HASH_COUNT(pool->partial_chunk_table) <= 1) return FALSE;
// still a lot of bytes to request?
if(pool->next_start_byte < pool->current_file_size*0.85 || HASH_COUNT(pool->partial_chunk_table) > 1) return TRUE;
return FALSE;
}
int reached_maximum(mpsock_scheduler *scheduler)
{
if(scheduler->num_sends == scheduler->connection->pool->mpsocket->max_req_con && scheduler->connection->pool->mpsocket->max_req_con > 0)
{
LOG_INFO("%sconnection sd#%d needs to be closed",RESULT_EVENT,scheduler->connection->sd);
return TRUE;
}
else
{
return FALSE;
}
}
int needs_new_connection(mpsock_scheduler *scheduler)
{
LOG_DEBUG("%snum_sends = %zu",FUN_EVENT,scheduler->num_sends);
if(scheduler->num_sends == scheduler->connection->pool->mpsocket->max_req_con-1 && scheduler->connection->pool->next_start_byte < scheduler->connection->pool->current_file_size && scheduler->connection->pool->mpsocket->max_req_con > 0)
{
LOG_INFO("%ssd#%d creates new connection",COND_EVENT,scheduler->connection->sd);
return TRUE;
}
else
{
return FALSE;
}
}
void scheduler_send_event(mpsock_scheduler *scheduler)
{
gettimeofday(&(scheduler->send_start),NULL);
LOG_DEBUG("%ssd=%d send_timestamp=%zu.%zu",RESULT_EVENT,scheduler->connection->sd,scheduler->send_start.tv_sec,scheduler->send_start.tv_usec);
memset(&(scheduler->first_recv),0,sizeof(struct timeval));
memset(&(scheduler->first_read),0,sizeof(struct timeval));
scheduler->total_number_bytes = 0;
scheduler->num_reads = 0;
scheduler->num_sends++;
lock_for_calc(scheduler->connection->pool);
scheduler->connection->pool->num_sends++;
scheduler->connection->pool->mpsocket->num_sends++;
unlock_for_calc(scheduler->connection->pool);
LOG_DEBUG("%stotal_requests=%zu",RESULT_EVENT,scheduler->connection->pool->mpsocket->num_sends);
return;
}
void scheduler_read_event(mpsock_scheduler *scheduler, int bytes)
{
if(bytes == 0) return;
scheduler->num_reads++;
scheduler->total_number_bytes += bytes;
scheduler->connection->bytes_received += bytes;
if(!((scheduler->first_recv.tv_sec == 0 && scheduler->first_recv.tv_usec == 0) || (scheduler->num_reads % (scheduler->connection->pool->mpsocket->processing_skips+1) == 0)))
{
// save processing power
return;
}
if(USE_ASSERTS) assert(scheduler->send_start.tv_sec != 0);
gettimeofday(&(scheduler->last_read),NULL);
if(bytes > 0)
{
gettimeofday(&(scheduler->last_byte_read),NULL);
}
/* ================== RTT Estimate =================*/
if(scheduler->first_recv.tv_sec == 0 && scheduler->first_recv.tv_usec == 0)
{
//if(USE_ASSERTS) assert(scheduler->total_number_bytes == 0);
if(USE_ASSERTS) assert(scheduler->num_reads == 1);
scheduler->first_recv.tv_sec = scheduler->last_read.tv_sec;
scheduler->first_recv.tv_usec = scheduler->last_read.tv_usec;
if(USE_ASSERTS) assert(scheduler->rtt >= 0);
double rtt_sample = scheduler->first_recv.tv_sec - scheduler->send_start.tv_sec;
rtt_sample += (double)(scheduler->first_recv.tv_usec - scheduler->send_start.tv_usec)/1000000;
if(USE_ASSERTS) assert(rtt_sample > 0);
LOG_DEBUG("%srtt sample = %fs",RESULT_EVENT,rtt_sample);
if(scheduler->rtt > 0)
{
// calculate new RTT
// NewRTT = alpha*OldRTT + (1-alpha)*NewSample
scheduler->rtt = DIST_FACTOR*scheduler->rtt + (1-DIST_FACTOR)*rtt_sample;
}
else
{
// very first estimate
LOG_DEBUG("%ssd#%d first RTT",COND_EVENT,scheduler->connection->sd);
scheduler->rtt = rtt_sample;
}
if(scheduler->connection->pool->mpsocket->log_metrics)
{
double sync_diff = scheduler->last_read.tv_sec - global_start_ts.tv_sec;
sync_diff += (double)(scheduler->last_read.tv_usec - global_start_ts.tv_usec)/1000000;
//LOG_METRICS("rtt %d %f %f\n",scheduler->connection->sd,sync_diff,scheduler->rtt);
printf("rtt sd#%d %f %f\n",scheduler->connection->sd,sync_diff,scheduler->rtt);
}
if(USE_ASSERTS) assert(scheduler->rtt > 0);
LOG_DEBUG("%ssd#%d RTT = %f",RESULT_EVENT,scheduler->connection->sd,scheduler->rtt);
}
/* ========================================================= */
if(scheduler->first_read.tv_sec == 0 && scheduler->first_read.tv_usec == 0)
{
scheduler->first_read.tv_sec = scheduler->last_read.tv_sec;
scheduler->first_read.tv_usec = scheduler->last_read.tv_usec;
}
/* ==================== Bandwidth Estimate ======================== */
if(scheduler->num_reads > READ_SKIPS && (scheduler->num_reads % (scheduler->connection->pool->mpsocket->processing_skips+1) == 0))
{
/*
if(scheduler->first_read.tv_sec == 0 && scheduler->first_read.tv_usec == 0)
{
if(USE_ASSERTS) assert(scheduler->num_reads == READ_SKIPS + 1);
scheduler->first_read.tv_sec = scheduler->last_read.tv_sec;
scheduler->first_read.tv_usec = scheduler->last_read.tv_usec;
return;
}
*/
// TODO: measure first from send_start
double diff = 0;
if(scheduler->bandwidth == 0)
{
diff = (double)(scheduler->last_read.tv_sec - scheduler->send_start.tv_sec);
diff += (double)(scheduler->last_read.tv_usec - scheduler->send_start.tv_usec)/1000000;
}
else
{
diff = (double)(scheduler->last_read.tv_sec - scheduler->first_read.tv_sec);
diff += (double)(scheduler->last_read.tv_usec - scheduler->first_read.tv_usec)/1000000;
}
LOG_DEBUG("%ssd#%d difference: %f",RESULT_EVENT,scheduler->connection->sd,diff);
if(USE_ASSERTS) assert(diff >= 0);
if(diff > 0)
{
//scheduler->total_number_bytes += bytes;
size_t bandwidth_sample = scheduler->total_number_bytes/diff;
scheduler->num_samples++;
if(USE_ASSERTS) assert(bandwidth_sample > 0);
if(scheduler->bandwidth > 0)
{
// calculate new Bandwidth
// NewBandwidth = alpha*OldBandwidth + (1-alpha)*NewSample
if(USE_HARMONIC_MEAN)
{
double num_samples = scheduler->num_samples;
scheduler->bandwidth = num_samples/(((num_samples-1)/scheduler->bandwidth) + (1/(double)(bandwidth_sample)));
}
else
{
scheduler->bandwidth = DIST_FACTOR*scheduler->bandwidth + (1-DIST_FACTOR)*bandwidth_sample;
}
}
else
{
// very first sample
scheduler->bandwidth = bandwidth_sample;
}
if(scheduler->connection->pool->mpsocket->log_metrics)
{
double sync_diff = scheduler->last_read.tv_sec - global_start_ts.tv_sec;
sync_diff += (double)(scheduler->last_read.tv_usec - global_start_ts.tv_usec)/1000000;
//LOG_METRICS("B %d %f %zu\n",scheduler->connection->sd,sync_diff,scheduler->bandwidth/1024);
printf("Bandwidth sd#%d %f %zu\n",scheduler->connection->sd,sync_diff,scheduler->bandwidth/1024);
}
}
}
/* ==================================================== */
LOG_DEBUG("%ssd=%d last_read_timestamp=%zu.%zu",RESULT_EVENT,scheduler->connection->sd,scheduler->last_read.tv_sec,scheduler->last_read.tv_usec);
return;
}
int use_scheduler(mpsock_scheduler *scheduler)
{
if(scheduler->connection->pool->current_file_size > MIN_SIZE_FOR_SCHEDULER && USE_SCHEDULER)
{
return TRUE;
}
else
{
return FALSE;
}
}
size_t scheduler_get_chunk_size(mpsock_scheduler *scheduler)
{
// TODO: bad connection reconnects -> max_connection needs to be adjusted!
lock_for_calc(scheduler->connection->pool);
size_t chunk_size;
if(use_scheduler(scheduler))
{
if(USE_ASSERTS) assert(scheduler->bandwidth >= 0);
/* ==================== Bandwidth Estimate ======================== */
/*
size_t bandwidth_sample = 0;
double diff = (double)(scheduler->last_read.tv_sec - scheduler->send_start.tv_sec);
diff += (double)(scheduler->last_read.tv_usec - scheduler->send_start.tv_usec)/1000000;
LOG_DEBUG("%ssd#%d difference: %f",RESULT_EVENT,scheduler->connection->sd,diff);
if(USE_ASSERTS) assert(diff >= 0);
if(diff > 0)
{
bandwidth_sample = scheduler->total_number_bytes/diff;
if(USE_ASSERTS) assert(bandwidth_sample > 0);
if(scheduler->bandwidth > 0)
{
// calculate new Bandwidth
// NewBandwidth = alpha*OldBandwidth + (1-alpha)*NewSample
scheduler->bandwidth = DIST_FACTOR*scheduler->bandwidth + (1-DIST_FACTOR)*bandwidth_sample;
}
else
{
// very first sample
scheduler->bandwidth = bandwidth_sample;
}
scheduler->last_bandwidth = bandwidth_sample;
}
*/
/* ==================================================== */
LOG_DEBUG("%ssd#%d Total Received Bytes = %zu",RESULT_EVENT,scheduler->connection->sd,scheduler->total_number_bytes);
LOG_DEBUG("%ssd#%d Estimated Bandwidth = %zu KB/s, Previous Bandwidth = %zu KB/s",RESULT_EVENT,scheduler->connection->sd,scheduler->bandwidth/1024,scheduler->last_bandwidth/1024);
//if(USE_ASSERTS) assert((DYNAMIC_ALPHA_ALGORITHM + TIME_CHUNK_ALGORITHM) == 1);
if(scheduler->connection->pool->mpsocket->scheduler_algo == 1)
{
chunk_size = dynamic_alpha_algorithm(scheduler);
}
else if(scheduler->connection->pool->mpsocket->scheduler_algo == 2)
{
chunk_size = time_chunk_algorithm(scheduler);
}
else if(scheduler->connection->pool->mpsocket->scheduler_algo == 0)
{
chunk_size = scheduler->connection->pool->mpsocket->initial_chunk_size;
}
else
{
perror("No valid Scheduler Algorithm selected! Selected");
exit(1);
}
}
else
{
// no scheduler -> simply double until max
chunk_size = scheduler->connection->chunk_size;
while(scheduler->connection->pool->current_file_size - scheduler->connection->pool->mpsocket->max_connections*chunk_size > 0 && chunk_size < MAXIMUM_CHUNK_SIZE)
{
// double
chunk_size *= 2;
}
}
// final adjustments
// in case it is too high
if(chunk_size > MAXIMUM_CHUNK_SIZE)
{
chunk_size = MAXIMUM_CHUNK_SIZE;
LOG_INFO("%ssd#%d due to maximum_size reduced chunk_size to %zu KB",RESULT_EVENT,scheduler->connection->sd,chunk_size/1024);
}
if(chunk_size < MINIMUM_CHUNK_SIZE)
{
chunk_size = MINIMUM_CHUNK_SIZE;
LOG_INFO("%ssd#%d due to minimum_size raised chunk_size to %zu KB",RESULT_EVENT,scheduler->connection->sd,chunk_size/1024);
}
// adjust so we do not exceed maximum request limit
if(scheduler->connection->pool->current_file_size - scheduler->connection->pool->next_start_byte > chunk_size*(scheduler->connection->pool->mpsocket->max_req_serv - scheduler->connection->pool->num_sends) && scheduler->connection->pool->mpsocket->max_req_serv > 0)
{
// adjust
chunk_size = (scheduler->connection->pool->current_file_size - scheduler->connection->pool->next_start_byte)/(scheduler->connection->pool->mpsocket->max_req_serv - scheduler->connection->pool->num_sends);
LOG_INFO("%ssd#%d to avoid request limit set chunk_size to %zu KB",RESULT_EVENT,scheduler->connection->sd,chunk_size/1024);
}
// adjust in case it is too big
if(chunk_size > scheduler->connection->pool->current_file_size - scheduler->connection->pool->current_chunk_request->start_byte)
{
chunk_size = scheduler->connection->pool->current_file_size - scheduler->connection->pool->current_chunk_request->start_byte;
LOG_INFO("%ssd#%d due to file_size reduced chunk_size to %zu KB",RESULT_EVENT,scheduler->connection->sd,chunk_size/1024);
}
if(USE_ASSERTS) assert(chunk_size >= 0);
// determine best connection in terms of bandwidth
// TODO: remove this!
if(scheduler->connection->pool->best_connection == NULL)
{
scheduler->connection->pool->best_connection = scheduler->connection;
}
else if(scheduler->connection->pool->best_connection->scheduler->bandwidth < scheduler->bandwidth)
{
scheduler->connection->pool->best_connection = scheduler->connection;
}
// ensure that in case of single byte left, we also take care of that
size_t rem_diff = scheduler->connection->pool->current_file_size - (scheduler->connection->pool->current_chunk_request->start_byte + chunk_size);
if(rem_diff > 0 && rem_diff < MAX_BYTES_LEFT)
{
LOG_INFO("%ssd#%d just %zu bytes of file remaining -> add to current request",RESULT_EVENT,scheduler->connection->sd,rem_diff);
chunk_size += rem_diff;
}
unlock_for_calc(scheduler->connection->pool);
LOG_INFO("%ssd#%d bandwidth = %zu kb/s\tRTT = %f s",RESULT_EVENT,scheduler->connection->sd,scheduler->bandwidth/1024,scheduler->rtt);
return chunk_size;
}
int enough_res_for_conn(mpsock_scheduler *scheduler)
{
// baseline scheduler
if(scheduler->connection->pool->mpsocket->scheduler_algo == 0)
{
if(scheduler->connection->pool->block_new_conns)
{
return FALSE;
}
}
mpsock_connection *conn = scheduler->connection;
// TODO: better
//if(HASH_COUNT(conn->pool->connection_table) < conn->pool->mpsocket->max_connections && conn->parser->status_code == 206)
if(num_active_connections(conn->pool) - num_waiting_connections(conn->pool) < conn->pool->mpsocket->max_connections && conn->parser->status_code == 206)
{
// TODO: good idea to place this here???
if(scheduler->rtt * ALPHA_MIN * scheduler->bandwidth > conn->pool->current_file_size)
{
conn->pool->finishing = TRUE;
}
double download_time = (double)((double)(conn->pool->current_file_size)/(double)(scheduler->bandwidth));
//if(conn->pool->current_file_size > conn->chunk_size)
//if((conn->pool->next_start_byte + conn->chunk_size < conn->pool->current_file_size || HASH_COUNT(conn->pool->partial_chunk_table) > 1) && conn->pool->current_file_size > conn->chunk_size && conn->pool->current_file_size >= MIN_SIZE_FOR_MULTIPATH)
if((download_time > MIN_DOWNLOAD_TIME && conn->pool->next_start_byte < conn->pool->current_file_size) || HASH_COUNT(conn->pool->partial_chunk_table) > 1)
{
LOG_DEBUG("%sEstimated Download Time: %f",FUN_EVENT,download_time);
return TRUE;
}
else
{
return FALSE;
}
}
else
{
return FALSE;
}
}
unsigned long scheduler_get_ip(mpsock_scheduler *scheduler, const char *host)
{
int i;
mpsock_addrs *e;
e = lookup_by_name(host);
if(USE_ASSERTS) assert(e != NULL);
scheduler->connection->adrs = e;
if(e)
{
for(i = 0 ; i < e->count ; i++)
{
if(e->ipset[i].inuse == FALSE/*&& e->ipset[i].priority*/)
{
e->ipset[i].inuse = TRUE;
scheduler->connection->adrs_index = i;
LOG_DEBUG("%sIndex %d",RESULT_EVENT,i);
scheduler->connection->ip = e->ipset[i].ip;
LOG_INFO("%ssd#%d using host %zu",RESULT_EVENT,scheduler->connection->sd,e->ipset[i].ip);
return e->ipset[i].ip;
}
}
} // error handling? if (e == NULL)
scheduler->connection->adrs_index = 0;
scheduler->connection->ip = e->ipset[0].ip;
LOG_DEBUG("%sBLAB %d",RESULT_EVENT,i);
LOG_INFO("%ssd#%d using host %zu",RESULT_EVENT,scheduler->connection->sd,e->ipset[0].ip);
return e->ipset[0].ip;
}
mpsock_interface *scheduler_get_interface(mpsock_scheduler *scheduler)
{
mpsock_interface *first = NULL;
mpsock_interface *second = NULL;
mpsock_interface *e;
lock_for_interface(scheduler->connection->pool);
if(scheduler->connection->pool->mpsocket->use_random_path)
{
for(e = mpsock_interface_table ; e != NULL ; e = e->hh.next)
{
if(first != NULL && second == NULL)
{
second = e;
}
if(first == NULL)
{
first = e;
}
}
mpsock_interface *res;
if(first->num_used == second->num_used)
{
if(rand()%2)
{
res = first;
scheduler->connection->pool->first_iface++;
}
else
{
res = second;
scheduler->connection->pool->second_iface++;
}
}
else if(first->num_used < second->num_used)
{
res = first;
scheduler->connection->pool->first_iface++;
}
else
{
res = second;
scheduler->connection->pool->second_iface++;
}
scheduler->connection->interface = res;
res->num_used++;
LOG_INFO("%ssd#%d using interface#%d: %s",RESULT_EVENT,scheduler->connection->sd,res->if_id,res->name);
unlock_for_interface(scheduler->connection->pool);
return res;
}
else
{
for(e = mpsock_interface_table ; e != NULL ; e = e->hh.next)
{
if(first != NULL && second == NULL)
{
second = e;
if(scheduler->connection->pool->first_iface > scheduler->connection->pool->second_iface && scheduler->connection->pool->second_iface == 0)
{
scheduler->connection->interface = e;
//e->used_by = scheduler->connection;
e->num_used++;
scheduler->connection->pool->second_iface++;
LOG_INFO("%ssd#%d using interface#%d: %s",RESULT_EVENT,scheduler->connection->sd,e->if_id,e->name);
unlock_for_interface(scheduler->connection->pool);
return e;
}
}
if(first == NULL)
{
first = e;
if(scheduler->connection->pool->first_iface <= scheduler->connection->pool->second_iface && scheduler->connection->pool->first_iface == 0)
{
scheduler->connection->interface = e;
//e->used_by = scheduler->connection;
e->num_used++;
scheduler->connection->pool->first_iface++;
LOG_INFO("%ssd#%d using interface#%d: %s",RESULT_EVENT,scheduler->connection->sd,e->if_id,e->name);
unlock_for_interface(scheduler->connection->pool);
return e;
}
}
// TODO: enhance: use usage counter instead of flag
//if(e->in_use == FALSE)
if(e->num_used == 0)
{
LOG_DEBUG("%swill use interface#%d: %s", COND_EVENT, e->if_id, e->name);
//e->in_use = TRUE;
//e->used_by = scheduler->connection;
scheduler->connection->interface = e;
//e->used_by = scheduler->connection;
e->num_used++;
LOG_INFO("%ssd#%d using interface#%d: %s",RESULT_EVENT,scheduler->connection->sd,e->if_id,e->name);
unlock_for_interface(scheduler->connection->pool);
return e;
}
}
// TODO: verify
if(first->num_used <= second->num_used)
{
e = first;
}
else
{
e = second;
}
e->num_used++;
unlock_for_interface(scheduler->connection->pool);
// none free -> shuffle
//if((rand()%10) < 5 || second == NULL)
//{
// e = first;
//}
//else
//{
// e = second;
//}
// return first interface otherwise
//e = first;
scheduler->connection->interface = e;
LOG_INFO("%ssd#%d using interface#%d: %s",RESULT_EVENT,scheduler->connection->sd,e->if_id,e->name);
return e;
}
}
unsigned long scheduler_get_first_ip(mpsock_scheduler *scheduler, unsigned long ip)
{
mpsock_addrs *e;
e = lookup_by_keyip(ip);
//if(e == NULL) perror("Can't find the IP entry.");
if(USE_ASSERTS) assert(e != NULL);
scheduler->connection->adrs = e;
scheduler->connection->adrs_index = 0;
scheduler->connection->ip = e->ipset[0].ip;
e->ipset[0].inuse = 1;
return e->ipset[0].ip;
}