-
Notifications
You must be signed in to change notification settings - Fork 550
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: watch bufer overrun for RouteStatus
Fixes #8157 This PR contains two fixes, both related to the same problem. Several routes for different links but same IPv6 destination might exist at the same time, so route resource ID should handle that. The problem was that these routes were mis-reported causing internally updates for the same resources multiple times (equal to the number of the links). Don't trigger controllers more often than 10 times/seconds (with burst of 5) for kernel notifications. This ensures Talos doesn't try to reflect current state of the network subsystem too often as resources, which causes excessive CPU usage and might potentially lead to the buffer overrun under high rate of changes. Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
- Loading branch information
Showing
16 changed files
with
152 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
internal/app/machined/pkg/controllers/network/watch/trigger.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package watch | ||
|
||
import ( | ||
"context" | ||
|
||
"golang.org/x/time/rate" | ||
) | ||
|
||
// RateLimitedTrigger wraps a Trigger with rate limiting. | ||
type RateLimitedTrigger struct { | ||
trigger Trigger | ||
limiter *rate.Limiter | ||
ch chan struct{} | ||
} | ||
|
||
// Interface check. | ||
var _ Trigger = &RateLimitedTrigger{} | ||
|
||
// NewRateLimitedTrigger creates a new RateLimitedTrigger with specified params. | ||
// | ||
// Trigger's goroutine exists when the context is canceled. | ||
func NewRateLimitedTrigger(ctx context.Context, trigger Trigger, rateLimit rate.Limit, burst int) *RateLimitedTrigger { | ||
t := &RateLimitedTrigger{ | ||
trigger: trigger, | ||
limiter: rate.NewLimiter(rateLimit, burst), | ||
ch: make(chan struct{}), | ||
} | ||
|
||
go t.run(ctx) | ||
|
||
return t | ||
} | ||
|
||
// NewDefaultRateLimitedTrigger creates a new RateLimitedTrigger with default params. | ||
func NewDefaultRateLimitedTrigger(ctx context.Context, trigger Trigger) *RateLimitedTrigger { | ||
const ( | ||
defaultRate = 10 // 10 events per second | ||
defaultBurst = 5 // 5 events | ||
) | ||
|
||
return NewRateLimitedTrigger(ctx, trigger, defaultRate, defaultBurst) | ||
} | ||
|
||
// QueueReconcile implements Trigger interface. | ||
// | ||
// The event is queued if the goroutine is ready to accept it (otherwise it's already | ||
// busy processing a previous event). | ||
// This function returns immediately. | ||
func (t *RateLimitedTrigger) QueueReconcile() { | ||
select { | ||
case t.ch <- struct{}{}: | ||
default: | ||
} | ||
} | ||
|
||
func (t *RateLimitedTrigger) run(ctx context.Context) { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case <-t.ch: | ||
} | ||
|
||
if err := t.limiter.Wait(ctx); err != nil { | ||
return | ||
} | ||
|
||
t.trigger.QueueReconcile() | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
internal/app/machined/pkg/controllers/network/watch/trigger_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package watch_test | ||
|
||
import ( | ||
"context" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/siderolabs/talos/internal/app/machined/pkg/controllers/network/watch" | ||
) | ||
|
||
type mockTrigger struct { | ||
count atomic.Int64 | ||
} | ||
|
||
func (t *mockTrigger) QueueReconcile() { | ||
t.count.Add(1) | ||
} | ||
|
||
func (t *mockTrigger) Get() int64 { | ||
return t.count.Load() | ||
} | ||
|
||
func TestRateLimitedTrigger(t *testing.T) { | ||
mock := &mockTrigger{} | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
t.Cleanup(cancel) | ||
|
||
trigger := watch.NewRateLimitedTrigger(ctx, mock, 10, 5) | ||
|
||
start := time.Now() | ||
|
||
for time.Since(start) < time.Second { | ||
trigger.QueueReconcile() | ||
} | ||
|
||
assert.InDelta(t, int64(14), mock.Get(), 5) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters