Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Better tries and weight tracking

  • Loading branch information...
commit d2ea47e99168f307c334777c4befc85aeae94173 1 parent 779d43b
Grzegorz Nosek authored June 14, 2008

Showing 1 changed file with 21 additions and 22 deletions. Show diff stats Hide diff stats

  1. 43  ngx_http_upstream_fair_module.c
43  ngx_http_upstream_fair_module.c
@@ -475,12 +475,13 @@ ngx_http_upstream_fair_try_peer(ngx_peer_connection_t *pc,
475 475
         }
476 476
 
477 477
         if (now - peer->accessed > peer->fail_timeout) {
  478
+            ngx_log_debug3(NGX_LOG_DEBUG_HTTP, pc->log, 0, "[upstream_fair] resetting fail count for peer %d, time delta %d > %d",
  479
+                peer_id, now - peer->accessed, peer->fail_timeout);
478 480
             peer->fails = 0;
479 481
             return NGX_OK;
480 482
         }
481 483
     }
482 484
 
483  
-    ngx_bitvector_set(fp->tried, peer_id);
484 485
     return NGX_BUSY;
485 486
 }
486 487
 
@@ -493,6 +494,7 @@ ngx_http_upstream_choose_fair_peer(ngx_peer_connection_t *pc,
493 494
     ngx_http_upstream_fair_shared_t     fsc;
494 495
     time_t                              now;
495 496
     ngx_uint_t                          best_sched_score = UINT_MAX, sched_score;
  497
+    ngx_http_upstream_rr_peer_t        *peer;
496 498
 
497 499
     npeers = fp->rrp->number;
498 500
 
@@ -520,7 +522,7 @@ ngx_http_upstream_choose_fair_peer(ngx_peer_connection_t *pc,
520 522
      * calculate sched scores for all the peers, choosing the lowest one
521 523
      */
522 524
     for (i = 0; i < npeers; i++, n = (n + 1) % npeers) {
523  
-        ngx_http_upstream_rr_peer_t *peer;
  525
+        ngx_uint_t weight;
524 526
 
525 527
         if (ngx_http_upstream_fair_try_peer(pc, fp, n, now) != NGX_OK) {
526 528
             if (!pc->tries) {
@@ -528,29 +530,28 @@ ngx_http_upstream_choose_fair_peer(ngx_peer_connection_t *pc,
528 530
                 return NGX_BUSY;
529 531
             }
530 532
 
531  
-            ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0, "[upstream_fair] backend %d is dead", n);
  533
+            ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0, "[upstream_fair] backend %d already tried", n);
532 534
             continue;
533 535
         }
534 536
 
535 537
         peer = &fp->rrp->peer[n];
536  
-
537  
-        if (peer->current_weight-- == 0) {
538  
-            peer->current_weight = peer->weight;
539  
-            ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, "[upstream_fair] peer %d expired weight, reset to %d", n, peer->weight);
540  
-            continue;
541  
-        }
542  
-
543 538
         fsc = fp->shared[n];
544 539
         sched_score = ngx_http_upstream_fair_sched_score(pc, &fsc, peer, n);
545 540
 
546 541
         /*
547 542
          * take peer weight into account
548 543
          */
549  
-        if (peer->current_weight > 0) {
550  
-            sched_score /= peer->current_weight;
  544
+        weight = peer->current_weight;
  545
+        if (peer->max_fails) {
  546
+            ngx_uint_t mf = peer->max_fails;
  547
+            weight = peer->current_weight * (mf - peer->fails) / mf;
  548
+        }
  549
+        if (weight > 0) {
  550
+            sched_score /= weight;
551 551
         }
552 552
 
553  
-        ngx_log_debug3(NGX_LOG_DEBUG_HTTP, pc->log, 0, "[upstream_fair] bss = %i, ss = %i (n = %d)", best_sched_score, sched_score, n);
  553
+        ngx_log_debug7(NGX_LOG_DEBUG_HTTP, pc->log, 0, "[upstream_fair] bss = %i, ss = %i (n = %d, w = %d/%d, f = %d/%d, weight = %d)",
  554
+            best_sched_score, sched_score, n, peer->current_weight, peer->weight, peer->fails, peer->max_fails, weight);
554 555
 
555 556
         if (sched_score <= best_sched_score) {
556 557
             *peer_id = n;
@@ -558,6 +559,13 @@ ngx_http_upstream_choose_fair_peer(ngx_peer_connection_t *pc,
558 559
         }
559 560
     }
560 561
 
  562
+    peer = &fp->rrp->peer[*peer_id];
  563
+    ngx_bitvector_set(fp->tried, *peer_id);
  564
+    if (peer->current_weight-- == 0) {
  565
+        peer->current_weight = peer->weight;
  566
+        ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, "[upstream_fair] peer %d expired weight, reset to %d", n, peer->weight);
  567
+    }
  568
+
561 569
     return NGX_OK;
562 570
 }
563 571
 
@@ -608,7 +616,6 @@ ngx_http_upstream_free_fair_peer(ngx_peer_connection_t *pc, void *data,
608 616
 {
609 617
     ngx_http_upstream_fair_peer_data_t     *fp = data;
610 618
     ngx_http_upstream_rr_peer_t            *peer;
611  
-    ngx_uint_t                              weight_delta;
612 619
 
613 620
     ngx_log_debug4(NGX_LOG_DEBUG_HTTP, pc->log, 0, "[upstream_fair] fp->current = %d, state = %ui, pc->tries = %d, pc->data = %p",
614 621
         fp->current, state, pc->tries, pc->data);
@@ -632,14 +639,6 @@ ngx_http_upstream_free_fair_peer(ngx_peer_connection_t *pc, void *data,
632 639
 
633 640
         peer->fails++;
634 641
         peer->accessed = ngx_time();
635  
-
636  
-        weight_delta = peer->weight / peer->max_fails;
637  
-
638  
-        if ((ngx_uint_t) peer->current_weight < weight_delta) {
639  
-            peer->current_weight = 0;
640  
-        } else {
641  
-            peer->current_weight -= weight_delta;
642  
-        }
643 642
     }
644 643
 }
645 644
 

0 notes on commit d2ea47e

Please sign in to comment.
Something went wrong with that request. Please try again.