Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix change notification of backend shard #835

Merged
merged 1 commit into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion pkg/haproxy/dynupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,8 @@ func (d *dynUpdater) checkEndpointPair(backend *hatypes.Backend, pair *epPair) b
}

func (d *dynUpdater) alignSlots() {
for _, back := range d.config.Backends().Items() {
backends := d.config.Backends()
for _, back := range backends.Items() {
if !back.Dynamic.DynUpdate {
// no need to add empty slots if won't dynamically update
continue
Expand All @@ -342,6 +343,7 @@ func (d *dynUpdater) alignSlots() {
blockSize = 1
}
var newFreeSlots int
changed := false
if minFreeSlots == 0 && len(back.Endpoints) == 0 {
newFreeSlots = blockSize
} else {
Expand All @@ -353,6 +355,7 @@ func (d *dynUpdater) alignSlots() {
}
for i := totalFreeSlots; i < minFreeSlots; i++ {
back.AddEmptyEndpoint()
changed = true
}
// * []endpoints == group of blocks
// * block == group of slots
Expand All @@ -362,6 +365,16 @@ func (d *dynUpdater) alignSlots() {
}
for i := 0; i < newFreeSlots; i++ {
back.AddEmptyEndpoint()
changed = true
}
if changed {
// backends from the Items() map are read only and their changes might not be
// reflected in the final configuration. Currently only sharded backends have
// this behavior but this can be expanded to another scenarios in the future,
// so this should be properly handled by the model.
//
// TODO move to the model the responsibility to know that a backend was changed.
backends.BackendChanged(back)
}
}
}
Expand Down
15 changes: 10 additions & 5 deletions pkg/haproxy/types/backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (b *Backends) Shrink() {
changed := false
for name, del := range b.itemsDel {
if add, found := b.itemsAdd[name]; found {
if backendsMatch(add, del) {
if len(add.Endpoints) <= len(del.Endpoints) && backendsMatch(add, del) {
// Such changed backend, when removed from the tracking, need to
// be reincluded into the current state hashmap `items` and also
// into its shard hashmap when backend sharding is enabled.
Expand All @@ -82,10 +82,10 @@ func (b *Backends) Shrink() {
if changed {
b.changedShards = map[int]bool{}
for _, back := range b.itemsAdd {
b.changedShards[back.shard] = true
b.BackendChanged(back)
}
for _, back := range b.itemsDel {
b.changedShards[back.shard] = true
b.BackendChanged(back)
}
}
}
Expand Down Expand Up @@ -138,6 +138,11 @@ func (b *Backends) Changed() bool {
return len(b.itemsAdd) > 0 || len(b.itemsDel) > 0
}

// BackendChanged ...
func (b *Backends) BackendChanged(backend *Backend) {
b.changedShards[backend.shard] = true
}

// ChangedShards ...
func (b *Backends) ChangedShards() []int {
changed := []int{}
Expand Down Expand Up @@ -226,7 +231,7 @@ func (b *Backends) AcquireBackend(namespace, name, port string) *Backend {
if shardCount > 0 {
b.shards[backend.shard][backend.ID] = backend
}
b.changedShards[backend.shard] = true
b.BackendChanged(backend)
return backend
}

Expand Down Expand Up @@ -267,7 +272,7 @@ func (b *Backends) RemoveAll(backendID []BackendID) {
if len(b.shards) > 0 {
delete(b.shards[item.shard], id)
}
b.changedShards[item.shard] = true
b.BackendChanged(item)
b.itemsDel[id] = item
if item == b.DefaultBackend {
b.DefaultBackend = nil
Expand Down