-
Notifications
You must be signed in to change notification settings - Fork 49
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
Better concurrent request handling for model host address #38
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
742c07b
Better concurrent request handling for host address
alpe 4d3035b
Review feedback and refactorings
alpe 5f02710
Fix double close and polish code
alpe b023ebc
Ensure race conditions handled
alpe d57436a
Add benchmark
alpe c5374c4
Apply review feedback
alpe 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
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,117 @@ | ||
package endpoints | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"sync/atomic" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"k8s.io/apimachinery/pkg/util/rand" | ||
) | ||
|
||
func TestConcurrentAccess(t *testing.T) { | ||
const myService = "myService" | ||
const myPort = "myPort" | ||
|
||
testCases := map[string]struct { | ||
readerCount int | ||
writerCount int | ||
}{ | ||
"lot of reader": {readerCount: 1_000, writerCount: 1}, | ||
"lot of writer": {readerCount: 1, writerCount: 1_000}, | ||
"lot of both": {readerCount: 1_000, writerCount: 1_000}, | ||
} | ||
for name, spec := range testCases { | ||
randomReadFn := []func(g *endpointGroup){ | ||
func(g *endpointGroup) { g.getBestHost(nil, myPort) }, | ||
func(g *endpointGroup) { g.getAllHosts(myPort) }, | ||
func(g *endpointGroup) { g.lenIPs() }, | ||
} | ||
t.Run(name, func(t *testing.T) { | ||
// setup endpoint with one service so that requests are not waiting | ||
endpoint := newEndpointGroup() | ||
endpoint.setIPs( | ||
map[string]struct{}{myService: {}}, | ||
map[string]int32{myPort: 1}, | ||
) | ||
|
||
var startWg, doneWg sync.WaitGroup | ||
startTogether := func(n int, f func()) { | ||
startWg.Add(n) | ||
doneWg.Add(n) | ||
for i := 0; i < n; i++ { | ||
go func() { | ||
startWg.Done() | ||
startWg.Wait() | ||
f() | ||
doneWg.Done() | ||
}() | ||
} | ||
} | ||
// when | ||
startTogether(spec.readerCount, func() { randomReadFn[rand.Intn(len(randomReadFn)-1)](endpoint) }) | ||
startTogether(spec.writerCount, func() { | ||
endpoint.setIPs( | ||
map[string]struct{}{rand.String(1): {}}, | ||
map[string]int32{rand.String(1): 1}, | ||
) | ||
}) | ||
doneWg.Wait() | ||
}) | ||
} | ||
} | ||
|
||
func TestBlockAndWaitForEndpoints(t *testing.T) { | ||
var completed atomic.Int32 | ||
var startWg, doneWg sync.WaitGroup | ||
startTogether := func(n int, f func()) { | ||
startWg.Add(n) | ||
doneWg.Add(n) | ||
for i := 0; i < n; i++ { | ||
go func() { | ||
startWg.Done() | ||
startWg.Wait() | ||
f() | ||
completed.Add(1) | ||
doneWg.Done() | ||
}() | ||
} | ||
} | ||
endpoint := newEndpointGroup() | ||
ctx := context.TODO() | ||
startTogether(100, func() { | ||
endpoint.getBestHost(ctx, rand.String(4)) | ||
}) | ||
startWg.Wait() | ||
|
||
// when broadcast triggered | ||
endpoint.setIPs( | ||
map[string]struct{}{rand.String(4): {}}, | ||
map[string]int32{rand.String(4): 1}, | ||
) | ||
// then | ||
doneWg.Wait() | ||
assert.Equal(t, int32(100), completed.Load()) | ||
} | ||
|
||
func TestAbortOnCtxCancel(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
|
||
var startWg, doneWg sync.WaitGroup | ||
startWg.Add(1) | ||
doneWg.Add(1) | ||
go func(t *testing.T) { | ||
startWg.Wait() | ||
endpoint := newEndpointGroup() | ||
_, err := endpoint.getBestHost(ctx, rand.String(4)) | ||
require.Error(t, err) | ||
doneWg.Done() | ||
}(t) | ||
startWg.Done() | ||
cancel() | ||
|
||
doneWg.Wait() | ||
} |
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,20 @@ | ||
package endpoints | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
) | ||
|
||
func BenchmarkEndpointGroup(b *testing.B) { | ||
e := newEndpointGroup() | ||
e.setIPs(map[string]struct{}{"10.0.0.1": {}}, map[string]int32{"testPort": 1}) | ||
b.ResetTimer() | ||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
_, err := e.getBestHost(context.Background(), "testPort") | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
} | ||
}) | ||
} |
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,59 @@ | ||
package endpoints | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAwaitBestHost(t *testing.T) { | ||
const myService = "myService" | ||
const myPort = "myPort" | ||
|
||
manager := &Manager{endpoints: make(map[string]*endpointGroup, 1)} | ||
manager.getEndpoints(myService). | ||
setIPs(map[string]struct{}{myService: {}}, map[string]int32{myPort: 1}) | ||
|
||
testCases := map[string]struct { | ||
service string | ||
portName string | ||
timeout time.Duration | ||
expErr error | ||
}{ | ||
"all good": { | ||
service: myService, | ||
portName: myPort, | ||
timeout: time.Millisecond, | ||
}, | ||
"unknown port - returns default if only 1": { | ||
service: myService, | ||
portName: "unknownPort", | ||
timeout: time.Millisecond, | ||
}, | ||
"unknown service - blocks until timeout": { | ||
service: "unknownService", | ||
portName: myPort, | ||
timeout: time.Millisecond, | ||
expErr: context.DeadlineExceeded, | ||
}, | ||
// not covered: unknown port with multiple ports on entrypoint | ||
} | ||
|
||
for name, spec := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), spec.timeout) | ||
defer cancel() | ||
|
||
gotHost, gotErr := manager.AwaitHostAddress(ctx, spec.service, spec.portName) | ||
if spec.expErr != nil { | ||
require.ErrorIs(t, spec.expErr, gotErr) | ||
return | ||
} | ||
require.NoError(t, gotErr) | ||
assert.Equal(t, myService+":1", gotHost) | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
I think these would be better written as individual tests instead of a table of tests cases. They each test different things. For example: for the timeout example it would be good to assert that the returned error is due to context cancellation and this code would only be used for that test case.
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.
I agree with the error check a bit vague but IMHO it makes sense to have a spec for the methods that defines all cases. I find it more readable.
But to be fair, I use table tests as my default structure for unit tests and may be biased. If this is very important for you, I can refactor. The error type is checked now
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.
Not a strong opinion, good with this.