Repository navigation
fix(router): resolve the async decision on the path, not the request target (refs #857) #1304
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
13c060d
test: failing-first tests and bench for Async routes requested with a…
FumingPower3925 33c9e7e
fix: resolve the async dispatch decision on the path, not the request…
FumingPower3925 13ad19e
test: describe the revert-check churn accurately (refs #857)
FumingPower3925 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 hidden or 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 hidden or 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,41 @@ | ||
| package celeris | ||
|
|
||
| import "testing" | ||
|
|
||
| var routeAsyncSink857 bool | ||
|
|
||
| // BenchmarkRouterAdapterRouteAsync857 is the per-request dispatch decision the H1 | ||
| // engines take before a handler runs (internal/conn/h1.go). noquery is the | ||
| // common case and must stay allocation free; query is the request target of | ||
| // celeris#857, which used to miss the static map and walk the tree. | ||
| func BenchmarkRouterAdapterRouteAsync857(b *testing.B) { | ||
| s := New(Config{}) | ||
| for _, p := range []string{"/", "/ping", "/json", "/plaintext", "/api/v1/health", "/api/v1/items"} { | ||
| s.GET(p, noopHandler) | ||
| } | ||
| s.GET("/mc-multiget", noopHandler).Async() | ||
| s.GET("/db/users", noopHandler).Async() | ||
| s.GET("/users/:id", noopHandler) | ||
| s.GET("/u/:id", noopHandler).Async() | ||
| s.POST("/db/users", noopHandler).Async() | ||
| a := &routerAdapter{server: s} | ||
| for _, tc := range []struct{ name, method, target string }{ | ||
| {"static_async_noquery", "GET", "/mc-multiget"}, | ||
| {"static_sync_noquery", "GET", "/plaintext"}, | ||
| {"param_async_noquery", "GET", "/u/12345"}, | ||
| {"unmatched_noquery", "GET", "/nope"}, | ||
| {"static_async_query", "GET", "/mc-multiget?keys=10"}, | ||
| {"static_async_query_long", "GET", "/db/users?limit=50&offset=100&order=name&fields=id,name,email"}, | ||
| {"param_async_query", "GET", "/u/12345?limit=50"}, | ||
| {"unmatched_query", "GET", "/nope?a=1"}, | ||
| } { | ||
| b.Run(tc.name, func(b *testing.B) { | ||
| b.ReportAllocs() | ||
| var r bool | ||
| for i := 0; i < b.N; i++ { | ||
| r = a.RouteAsync(tc.method, tc.target) | ||
| } | ||
| routeAsyncSink857 = r | ||
| }) | ||
| } | ||
| } |
This file contains hidden or 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,63 @@ | ||
| package celeris | ||
|
|
||
| import "testing" | ||
|
|
||
| // celeris#857: the H1 engines hand RouteAsync the request target as it came off | ||
| // the wire, query string included (internal/conn/h1.go: state.req.Path), while | ||
| // the router is keyed by path alone and Context.extractRequestInfo cuts the | ||
| // target at the first '?'. routerAdapter.RouteAsync must cut it the same way, or | ||
| // an Async route requested with a query string is never promoted to the | ||
| // dispatch goroutine. | ||
| func TestRouterAdapterRouteAsyncIgnoresQuery857(t *testing.T) { | ||
| s := New(Config{}) | ||
| s.GET("/x", noopHandler).Async() | ||
| s.GET("/u/:id", noopHandler).Async() | ||
| s.GET("/sync", noopHandler) | ||
| s.GET("/only-get", noopHandler).Async() | ||
| s.Handle("PURGE", "/cache", noopHandler).Async() | ||
| a := &routerAdapter{server: s} | ||
|
|
||
| for _, tc := range []struct { | ||
| name, method, target string | ||
| want bool | ||
| }{ | ||
| {"static no query", "GET", "/x", true}, | ||
| {"static with query", "GET", "/x?keys=10", true}, | ||
| {"static empty query", "GET", "/x?", true}, | ||
| {"static query holding a slash", "GET", "/x?next=/sync&a=b", true}, | ||
| {"static query holding a second ?", "GET", "/x?a=?b", true}, | ||
| {"param no query", "GET", "/u/7", true}, | ||
| {"param with query", "GET", "/u/7?limit=50", true}, | ||
| {"param with a slash in the query", "GET", "/u/7?next=/a/b", true}, | ||
| {"sync with query", "GET", "/sync?a=1", false}, | ||
| {"sync no query", "GET", "/sync", false}, | ||
| {"unmatched no query", "GET", "/missing", false}, | ||
| {"unmatched with query", "GET", "/missing?a=1", false}, | ||
| {"query only names another route", "GET", "/sync?x=/x", false}, | ||
| {"HEAD as GET no query", "HEAD", "/only-get", true}, | ||
| {"HEAD as GET with query", "HEAD", "/only-get?a=1", true}, | ||
| {"HEAD as GET sync with query", "HEAD", "/sync?a=1", false}, | ||
| {"custom method no query", "PURGE", "/cache", true}, | ||
| {"custom method with query", "PURGE", "/cache?a=1", true}, | ||
| {"wrong method with query", "POST", "/x?a=1", false}, | ||
| } { | ||
| if got := a.RouteAsync(tc.method, tc.target); got != tc.want { | ||
| t.Errorf("%s: RouteAsync(%q, %q) = %v, want %v", tc.name, tc.method, tc.target, got, tc.want) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // An adaptive route (server default AsyncHandlers) that has been promoted is | ||
| // found through its query string too: the adaptive key is the route pattern. | ||
| func TestRouterAdapterRouteAsyncAdaptiveIgnoresQuery857(t *testing.T) { | ||
| s := New(Config{AsyncHandlers: true}) | ||
| s.GET("/ad", noopHandler) | ||
| a := &routerAdapter{server: s} | ||
| if a.RouteAsync("GET", "/ad?a=1") { | ||
| t.Fatal("an adaptive route starts inline, with or without a query") | ||
| } | ||
| s.router.promoteRoute("/ad") | ||
| if !a.RouteAsync("GET", "/ad") || !a.RouteAsync("GET", "/ad?a=1") { | ||
| t.Fatal("a promoted adaptive route must resolve async with and without a query") | ||
| } | ||
| } |
This file contains hidden or 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,208 @@ | ||
| //go:build linux | ||
|
|
||
| package celeris_test | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "fmt" | ||
| "io" | ||
| "net" | ||
| "net/http" | ||
| "sync" | ||
| "sync/atomic" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "golang.org/x/net/http2" | ||
|
|
||
| "github.com/goceleris/celeris" | ||
| ) | ||
|
|
||
| // celeris#857. The H1 engines ask the router whether a request's route is | ||
| // Async with the request target as it came off the wire, query string | ||
| // included, and the router is keyed by path alone: an Async route requested | ||
| // as /block?keys=10 was never promoted to the dispatch goroutine, so its | ||
| // blocking handler ran on the event loop, one request per loop at a time. | ||
| // The rig below serves a handler that blocks until released, on a server of | ||
| // two loops, and sends it n requests at once on n connections (H1) or n | ||
| // streams of one connection (h2c by prior knowledge, on a server that also | ||
| // answers the readiness probe over H1). With the route promoted, all n are in the | ||
| // handler together; on the event loop only as many as there are loops (two). | ||
|
|
||
| const ( | ||
| conc857h = 8 | ||
| inflightWait857 = 5 * time.Second | ||
| ) | ||
|
|
||
| type block857 struct { | ||
| inflight, max atomic.Int64 | ||
| release chan struct{} | ||
| once sync.Once | ||
| } | ||
|
|
||
| func (b *block857) open() { b.once.Do(func() { close(b.release) }) } | ||
|
|
||
| func (b *block857) handler(c *celeris.Context) error { | ||
| n := b.inflight.Add(1) | ||
| defer b.inflight.Add(-1) | ||
| for { | ||
| m := b.max.Load() | ||
| if n <= m || b.max.CompareAndSwap(m, n) { | ||
| break | ||
| } | ||
| } | ||
| select { | ||
| case <-b.release: | ||
| case <-time.After(30 * time.Second): | ||
| } | ||
| return c.String(http.StatusOK, "ok") | ||
| } | ||
|
|
||
| // waitInflight857 waits until n requests are inside the handler, or the | ||
| // window ends, and returns the most that were there at once. | ||
| func (b *block857) waitInflight(n int64) int64 { | ||
| for deadline := time.Now().Add(inflightWait857); time.Now().Before(deadline) && b.max.Load() < n; { | ||
| time.Sleep(5 * time.Millisecond) | ||
| } | ||
| return b.max.Load() | ||
| } | ||
|
|
||
| func startBlock857(t *testing.T, a arm848, proto celeris.Protocol) (string, *block857) { | ||
| t.Helper() | ||
| need848(t, a.eng) | ||
| if a.start != "" { | ||
| t.Setenv("CELERIS_ADAPTIVE_START", a.start) | ||
| } | ||
| b := &block857{release: make(chan struct{})} | ||
| _, addr := startPickedPing1104(t, celeris.Config{Engine: a.eng, Protocol: proto, Workers: 2}, func(s *celeris.Server) { | ||
| ping1104(s) | ||
| s.GET("/block", b.handler).Async() | ||
| }) | ||
| t.Cleanup(b.open) // runs before the server's shutdown: cleanups run last in, first out | ||
| return addr, b | ||
| } | ||
|
|
||
| func TestAsyncRouteWithQueryRunsOnDispatchGoroutineH1857(t *testing.T) { | ||
| for _, tc := range []struct{ name, target string }{ | ||
| {"noquery", "/block"}, | ||
| {"query", "/block?keys=10"}, | ||
| {"emptyquery", "/block?"}, | ||
| } { | ||
| target := tc.target | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| for _, a := range arms848() { | ||
| t.Run(a.name, func(t *testing.T) { | ||
| addr, b := startBlock857(t, a, celeris.HTTP1) | ||
| var wg sync.WaitGroup | ||
| for i := 0; i < conc857h; i++ { | ||
| c, err := net.DialTimeout("tcp", addr, 5*time.Second) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| t.Cleanup(func() { _ = c.Close() }) | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| _ = c.SetDeadline(time.Now().Add(45 * time.Second)) | ||
| if _, err := fmt.Fprintf(c, "GET %s HTTP/1.1\r\nHost: a\r\n\r\n", target); err != nil { | ||
| t.Errorf("write: %v", err) | ||
| return | ||
| } | ||
| resp, err := http.ReadResponse(bufio.NewReader(c), nil) | ||
| if err != nil { | ||
| t.Errorf("read: %v", err) | ||
| return | ||
| } | ||
| _, _ = io.Copy(io.Discard, resp.Body) | ||
| _ = resp.Body.Close() | ||
| if resp.StatusCode != http.StatusOK { | ||
| t.Errorf("status %d", resp.StatusCode) | ||
| } | ||
| }() | ||
| } | ||
| got := b.waitInflight(conc857h) | ||
| b.open() | ||
| wg.Wait() | ||
| if got != conc857h { | ||
| t.Fatalf("GET %s: at most %d of %d concurrent requests were in the Async handler at once: the route was not promoted to the dispatch goroutine, so the handler ran on the event loop", target, got, conc857h) | ||
| } | ||
| }) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // A connection promoted by an Async route requested with a query string stays | ||
| // promoted: the revert check of the io_uring engine (canRevertToInline) asks the | ||
| // router about the promoting route again at every park, with the path it | ||
| // recorded (CurrentRoute, query included). Unless RouteAsync itself cuts the | ||
| // query, that comes back "not async" and the conn churns between the loop and | ||
| // the dispatch goroutine (one promotion per request). On main it was never | ||
| // promoted at all (rose by 0). | ||
| func TestAsyncRouteWithQueryKeepsConnPromotedOnce857(t *testing.T) { | ||
| for _, a := range arms848() { | ||
| t.Run(a.name, func(t *testing.T) { | ||
| need848(t, a.eng) | ||
| if a.start != "" { | ||
| t.Setenv("CELERIS_ADAPTIVE_START", a.start) | ||
| } | ||
| s, addr := startPickedPing1104(t, celeris.Config{Engine: a.eng, Protocol: celeris.HTTP1, Workers: 2}, func(s *celeris.Server) { | ||
| ping1104(s) | ||
| s.GET("/q", func(c *celeris.Context) error { return c.String(http.StatusOK, "ok") }).Async() | ||
| }) | ||
| before := s.EngineInfo().Metrics.AsyncPromotedConns | ||
| c, err := net.DialTimeout("tcp", addr, 5*time.Second) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer func() { _ = c.Close() }() | ||
| br := bufio.NewReader(c) | ||
| for i := 0; i < 10; i++ { | ||
| if got := get889(t, c, br, fmt.Sprintf("/q?i=%d", i)); got != "ok" { | ||
| t.Fatalf("request %d: %q", i, got) | ||
| } | ||
| time.Sleep(2 * time.Millisecond) // the dispatch goroutine parks between requests | ||
| } | ||
| if got := s.EngineInfo().Metrics.AsyncPromotedConns - before; got != 1 { | ||
| t.Fatalf("AsyncPromotedConns rose by %d over 10 requests of one connection to an Async route with a query string, want 1", got) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // The h2 processor cuts the query off :path itself before asking the router, | ||
| // so h2 never had the defect; this keeps it that way. | ||
| func TestAsyncRouteWithQueryRunsOffTheLoopH2857(t *testing.T) { | ||
| for _, a := range arms848() { | ||
| t.Run(a.name, func(t *testing.T) { | ||
| addr, b := startBlock857(t, a, celeris.Auto) | ||
| c := dialH2raw889(t, addr, h2dial889{}) | ||
| for i := 0; i < conc857h; i++ { | ||
| c.request(uint32(2*i+1), "GET", "/block?keys=10", true, true) | ||
| } | ||
| got := b.waitInflight(conc857h) | ||
| b.open() | ||
| ended := 0 | ||
| _ = c.conn.SetReadDeadline(time.Now().Add(30 * time.Second)) | ||
| for ended < conc857h { | ||
| f, err := c.fr.ReadFrame() | ||
| if err != nil { | ||
| t.Fatalf("after %d of %d streams ended: %v", ended, conc857h, err) | ||
| } | ||
| switch f := f.(type) { | ||
| case *http2.DataFrame: | ||
| if f.StreamEnded() { | ||
| ended++ | ||
| } | ||
| case *http2.HeadersFrame: | ||
| if f.StreamEnded() { | ||
| ended++ | ||
| } | ||
| } | ||
| } | ||
| if got != conc857h { | ||
| t.Fatalf("at most %d of %d concurrent h2 streams were in the Async handler at once", got, conc857h) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
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.
Wait for dispatch parking instead of sleeping.
Line 164 assumes the dispatch goroutine parks within 2 ms. If scheduling delays parking, the next request can arrive before the revert check runs. The test can then pass without exercising the promotion/reversion behavior it claims to cover.
Use a park-completion signal or a synchronized parked-state condition before sending the next request.
As per path instructions: “no sleep used as synchronisation.”
Source: Path instructions