2019-08-26 08:30:05 +00:00
|
|
|
package wrr
|
|
|
|
|
|
|
|
import (
|
2020-02-26 10:56:05 +00:00
|
|
|
"container/heap"
|
2021-06-25 19:08:11 +00:00
|
|
|
"context"
|
2020-11-06 08:26:03 +00:00
|
|
|
"errors"
|
2019-08-26 08:30:05 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/log"
|
2019-08-26 08:30:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type namedHandler struct {
|
|
|
|
http.Handler
|
2020-02-26 10:56:05 +00:00
|
|
|
name string
|
|
|
|
weight float64
|
|
|
|
deadline float64
|
2019-08-26 08:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type stickyCookie struct {
|
|
|
|
name string
|
|
|
|
secure bool
|
|
|
|
httpOnly bool
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:03 +00:00
|
|
|
// Balancer is a WeightedRoundRobin load balancer based on Earliest Deadline First (EDF).
|
|
|
|
// (https://en.wikipedia.org/wiki/Earliest_deadline_first_scheduling)
|
|
|
|
// Each pick from the schedule has the earliest deadline entry selected.
|
|
|
|
// Entries have deadlines set at currentDeadline + 1 / weight,
|
|
|
|
// providing weighted round robin behavior with floating point weights and an O(log n) pick time.
|
|
|
|
type Balancer struct {
|
2021-06-25 19:08:11 +00:00
|
|
|
stickyCookie *stickyCookie
|
|
|
|
wantsHealthCheck bool
|
2021-03-04 08:02:03 +00:00
|
|
|
|
|
|
|
mutex sync.RWMutex
|
|
|
|
handlers []*namedHandler
|
|
|
|
curDeadline float64
|
2021-06-25 19:08:11 +00:00
|
|
|
// status is a record of which child services of the Balancer are healthy, keyed
|
|
|
|
// by name of child service. A service is initially added to the map when it is
|
|
|
|
// created via AddService, and it is later removed or added to the map as needed,
|
|
|
|
// through the SetStatus method.
|
|
|
|
status map[string]struct{}
|
|
|
|
// updaters is the list of hooks that are run (to update the Balancer
|
|
|
|
// parent(s)), whenever the Balancer status changes.
|
|
|
|
updaters []func(bool)
|
2021-03-04 08:02:03 +00:00
|
|
|
}
|
|
|
|
|
2019-08-26 08:30:05 +00:00
|
|
|
// New creates a new load balancer.
|
2021-06-25 19:08:11 +00:00
|
|
|
func New(sticky *dynamic.Sticky, hc *dynamic.HealthCheck) *Balancer {
|
|
|
|
balancer := &Balancer{
|
|
|
|
status: make(map[string]struct{}),
|
|
|
|
wantsHealthCheck: hc != nil,
|
|
|
|
}
|
2019-08-26 08:30:05 +00:00
|
|
|
if sticky != nil && sticky.Cookie != nil {
|
|
|
|
balancer.stickyCookie = &stickyCookie{
|
|
|
|
name: sticky.Cookie.Name,
|
|
|
|
secure: sticky.Cookie.Secure,
|
|
|
|
httpOnly: sticky.Cookie.HTTPOnly,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return balancer
|
|
|
|
}
|
|
|
|
|
2020-02-26 10:56:05 +00:00
|
|
|
// Len implements heap.Interface/sort.Interface.
|
|
|
|
func (b *Balancer) Len() int { return len(b.handlers) }
|
|
|
|
|
|
|
|
// Less implements heap.Interface/sort.Interface.
|
|
|
|
func (b *Balancer) Less(i, j int) bool {
|
|
|
|
return b.handlers[i].deadline < b.handlers[j].deadline
|
2019-08-26 08:30:05 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 10:56:05 +00:00
|
|
|
// Swap implements heap.Interface/sort.Interface.
|
|
|
|
func (b *Balancer) Swap(i, j int) {
|
|
|
|
b.handlers[i], b.handlers[j] = b.handlers[j], b.handlers[i]
|
2019-08-26 08:30:05 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 10:56:05 +00:00
|
|
|
// Push implements heap.Interface for pushing an item into the heap.
|
|
|
|
func (b *Balancer) Push(x interface{}) {
|
|
|
|
h, ok := x.(*namedHandler)
|
|
|
|
if !ok {
|
|
|
|
return
|
2019-08-26 08:30:05 +00:00
|
|
|
}
|
2020-02-26 10:56:05 +00:00
|
|
|
|
|
|
|
b.handlers = append(b.handlers, h)
|
2019-08-26 08:30:05 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 11:40:09 +00:00
|
|
|
// Pop implements heap.Interface for popping an item from the heap.
|
2020-02-26 10:56:05 +00:00
|
|
|
// It panics if b.Len() < 1.
|
|
|
|
func (b *Balancer) Pop() interface{} {
|
|
|
|
h := b.handlers[len(b.handlers)-1]
|
|
|
|
b.handlers = b.handlers[0 : len(b.handlers)-1]
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2021-06-25 19:08:11 +00:00
|
|
|
// SetStatus sets on the balancer that its given child is now of the given
|
|
|
|
// status. balancerName is only needed for logging purposes.
|
|
|
|
func (b *Balancer) SetStatus(ctx context.Context, childName string, up bool) {
|
|
|
|
b.mutex.Lock()
|
|
|
|
defer b.mutex.Unlock()
|
|
|
|
|
|
|
|
upBefore := len(b.status) > 0
|
|
|
|
|
|
|
|
status := "DOWN"
|
|
|
|
if up {
|
|
|
|
status = "UP"
|
|
|
|
}
|
|
|
|
log.FromContext(ctx).Debugf("Setting status of %s to %v", childName, status)
|
|
|
|
if up {
|
|
|
|
b.status[childName] = struct{}{}
|
|
|
|
} else {
|
|
|
|
delete(b.status, childName)
|
|
|
|
}
|
|
|
|
|
|
|
|
upAfter := len(b.status) > 0
|
|
|
|
status = "DOWN"
|
|
|
|
if upAfter {
|
|
|
|
status = "UP"
|
|
|
|
}
|
|
|
|
|
|
|
|
// No Status Change
|
|
|
|
if upBefore == upAfter {
|
|
|
|
// We're still with the same status, no need to propagate
|
|
|
|
log.FromContext(ctx).Debugf("Still %s, no need to propagate", status)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Status Change
|
|
|
|
log.FromContext(ctx).Debugf("Propagating new %s status", status)
|
|
|
|
for _, fn := range b.updaters {
|
|
|
|
fn(upAfter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterStatusUpdater adds fn to the list of hooks that are run when the
|
|
|
|
// status of the Balancer changes.
|
|
|
|
// Not thread safe.
|
|
|
|
func (b *Balancer) RegisterStatusUpdater(fn func(up bool)) error {
|
|
|
|
if !b.wantsHealthCheck {
|
|
|
|
return errors.New("healthCheck not enabled in config for this weighted service")
|
|
|
|
}
|
|
|
|
b.updaters = append(b.updaters, fn)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var errNoAvailableServer = errors.New("no available server")
|
|
|
|
|
2019-08-26 08:30:05 +00:00
|
|
|
func (b *Balancer) nextServer() (*namedHandler, error) {
|
|
|
|
b.mutex.Lock()
|
|
|
|
defer b.mutex.Unlock()
|
|
|
|
|
|
|
|
if len(b.handlers) == 0 {
|
|
|
|
return nil, fmt.Errorf("no servers in the pool")
|
|
|
|
}
|
2021-06-25 19:08:11 +00:00
|
|
|
if len(b.status) == 0 {
|
|
|
|
return nil, errNoAvailableServer
|
|
|
|
}
|
2019-08-26 08:30:05 +00:00
|
|
|
|
2021-06-25 19:08:11 +00:00
|
|
|
var handler *namedHandler
|
|
|
|
for {
|
|
|
|
// Pick handler with closest deadline.
|
|
|
|
handler = heap.Pop(b).(*namedHandler)
|
2020-02-26 10:56:05 +00:00
|
|
|
|
2021-06-25 19:08:11 +00:00
|
|
|
// curDeadline should be handler's deadline so that new added entry would have a fair competition environment with the old ones.
|
|
|
|
b.curDeadline = handler.deadline
|
|
|
|
handler.deadline += 1 / handler.weight
|
2020-02-26 10:56:05 +00:00
|
|
|
|
2021-06-25 19:08:11 +00:00
|
|
|
heap.Push(b, handler)
|
|
|
|
if _, ok := b.status[handler.name]; ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-02-26 10:56:05 +00:00
|
|
|
|
|
|
|
log.WithoutContext().Debugf("Service selected by WRR: %s", handler.name)
|
|
|
|
return handler, nil
|
2019-08-26 08:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Balancer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|
|
|
if b.stickyCookie != nil {
|
|
|
|
cookie, err := req.Cookie(b.stickyCookie.name)
|
|
|
|
|
2020-11-06 08:26:03 +00:00
|
|
|
if err != nil && !errors.Is(err, http.ErrNoCookie) {
|
2019-08-26 08:30:05 +00:00
|
|
|
log.WithoutContext().Warnf("Error while reading cookie: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil && cookie != nil {
|
|
|
|
for _, handler := range b.handlers {
|
2021-06-25 19:08:11 +00:00
|
|
|
if handler.name != cookie.Value {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
b.mutex.RLock()
|
|
|
|
_, ok := b.status[handler.name]
|
|
|
|
b.mutex.RUnlock()
|
|
|
|
if !ok {
|
|
|
|
// because we already are in the only iteration that matches the cookie, so none
|
|
|
|
// of the following iterations are going to be a match for the cookie anyway.
|
|
|
|
break
|
2019-08-26 08:30:05 +00:00
|
|
|
}
|
2021-06-25 19:08:11 +00:00
|
|
|
|
|
|
|
handler.ServeHTTP(w, req)
|
|
|
|
return
|
2019-08-26 08:30:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
server, err := b.nextServer()
|
|
|
|
if err != nil {
|
2021-06-25 19:08:11 +00:00
|
|
|
if errors.Is(err, errNoAvailableServer) {
|
|
|
|
http.Error(w, errNoAvailableServer.Error(), http.StatusServiceUnavailable)
|
|
|
|
} else {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
2019-08-26 08:30:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.stickyCookie != nil {
|
|
|
|
cookie := &http.Cookie{Name: b.stickyCookie.name, Value: server.name, Path: "/", HttpOnly: b.stickyCookie.httpOnly, Secure: b.stickyCookie.secure}
|
|
|
|
http.SetCookie(w, cookie)
|
|
|
|
}
|
|
|
|
|
|
|
|
server.ServeHTTP(w, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddService adds a handler.
|
2020-02-26 10:56:05 +00:00
|
|
|
// A handler with a non-positive weight is ignored.
|
2019-08-26 08:30:05 +00:00
|
|
|
func (b *Balancer) AddService(name string, handler http.Handler, weight *int) {
|
|
|
|
w := 1
|
|
|
|
if weight != nil {
|
|
|
|
w = *weight
|
|
|
|
}
|
2020-02-26 10:56:05 +00:00
|
|
|
if w <= 0 { // non-positive weight is meaningless
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
h := &namedHandler{Handler: handler, name: name, weight: float64(w)}
|
|
|
|
|
2021-06-25 19:08:11 +00:00
|
|
|
b.mutex.Lock()
|
2020-02-26 10:56:05 +00:00
|
|
|
h.deadline = b.curDeadline + 1/h.weight
|
|
|
|
heap.Push(b, h)
|
2021-06-25 19:08:11 +00:00
|
|
|
b.status[name] = struct{}{}
|
|
|
|
b.mutex.Unlock()
|
2019-08-26 08:30:05 +00:00
|
|
|
}
|