-
Notifications
You must be signed in to change notification settings - Fork 183
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
New proxy server counting logic for agent and server #634
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
206e457
Add static and cached server counters
carreter dfffce9
Consistent file naming
carreter 2a8282e
Wire in new ServerCounter to proxy agent
carreter fa72e2d
Wire in new ServerCounter to proxy server
carreter 2cb1d7e
Set default ServerCounter on agent
carreter 4bc512d
Remove empty file
carreter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package servercounter | ||
|
||
import ( | ||
"time" | ||
|
||
"k8s.io/klog/v2" | ||
) | ||
|
||
// A CachedServerCounter wraps a ServerCounter to cache its server count value. | ||
// Cache refreshes occur when CountServers() is called after a user-configurable | ||
// cache expiration duration. | ||
type CachedServerCounter struct { | ||
inner ServerCounter | ||
cachedCount int | ||
expiration time.Duration | ||
lastRefresh time.Time | ||
} | ||
|
||
// CountServers returns the last cached server count and updates the cached count | ||
// if it has expired since the last call. | ||
func (csc *CachedServerCounter) CountServers() int { | ||
// Refresh the cache if expiry time has passed since last call. | ||
if time.Now().Sub(csc.lastRefresh) >= csc.expiration { | ||
newCount := csc.inner.CountServers() | ||
if newCount != csc.cachedCount { | ||
klog.Infof("updated cached server count from %v to %v", csc.cachedCount, newCount) | ||
} | ||
csc.lastRefresh = time.Now() | ||
} | ||
|
||
return csc.cachedCount | ||
} | ||
|
||
// NewCachedServerCounter creates a new CachedServerCounter with a given expiration | ||
// time wrapping the provided ServerCounter. | ||
func NewCachedServerCounter(serverCounter ServerCounter, expiration time.Duration) *CachedServerCounter { | ||
return &CachedServerCounter{ | ||
inner: serverCounter, | ||
cachedCount: serverCounter.CountServers(), | ||
expiration: expiration, | ||
lastRefresh: time.Now(), | ||
} | ||
} |
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,52 @@ | ||
package servercounter | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
type MockServerCounter struct { | ||
NumCalls int | ||
Count int | ||
} | ||
|
||
func (msc *MockServerCounter) CountServers() int { | ||
msc.NumCalls += 1 | ||
return msc.Count | ||
} | ||
|
||
func TestCachedServerCounter(t *testing.T) { | ||
initialCount := 3 | ||
mockCounter := &MockServerCounter{NumCalls: 0, Count: initialCount} | ||
|
||
cacheExpiry := time.Second // This can be tuned down to make this test run faster, just don't make it too small! | ||
cachedCounter := NewCachedServerCounter(mockCounter, cacheExpiry) | ||
|
||
if mockCounter.NumCalls != 1 { | ||
t.Errorf("inner server counter have been called once during cached counter creation, got %v calls isntead", mockCounter.NumCalls) | ||
} | ||
|
||
// Updates in the underlying ServerCounter should not matter until the cache expires. | ||
callAttemptsWithoutRefresh := 5 | ||
for i := 0; i < callAttemptsWithoutRefresh; i++ { | ||
mockCounter.Count = 100 + i | ||
time.Sleep(cacheExpiry / time.Duration(callAttemptsWithoutRefresh) / time.Duration(2)) | ||
got := cachedCounter.CountServers() | ||
if got != initialCount { | ||
t.Errorf("server count should not have been updated yet; wanted: %v, got %v", initialCount, got) | ||
} else if mockCounter.NumCalls != 1 { | ||
t.Errorf("inner server counter should not have been called yet; expected 1 call, got %v instead", mockCounter.NumCalls) | ||
} | ||
} | ||
|
||
// Once the cache expires, the cached count should update by calling the underlying | ||
// ServerCounter. | ||
mockCounter.Count = 5 | ||
time.Sleep(cacheExpiry) | ||
got := cachedCounter.CountServers() | ||
if got != initialCount { | ||
t.Errorf("server count should have been updated yet; wanted: %v, got %v", mockCounter.Count, got) | ||
} else if mockCounter.NumCalls != 2 { | ||
t.Errorf("inner server counter should have been called one more time; expected 2 calls, got %v instead", mockCounter.NumCalls) | ||
} | ||
} |
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,14 @@ | ||
package servercounter | ||
|
||
// A ServerCounter counts the number of available proxy servers. | ||
type ServerCounter interface { | ||
CountServers() int | ||
} | ||
|
||
// A StaticServerCounter stores a static server count. | ||
type StaticServerCounter int | ||
|
||
// CountServers returns the current (static) proxy server count. | ||
func (sc StaticServerCounter) CountServers() int { | ||
return int(sc) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should chat on this. During certain events with multiple KASs/Konnectivity servers we can get different server counts from different servers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, @avrittrohwer also raised this issue. One potential solution would be to cache the last n received server counts and take the highest value. Ultimately this gets solved by having proxy agents count the leases themselves.