-
Notifications
You must be signed in to change notification settings - Fork 255
feat: merge entity fetches and then schedule optimally #3146
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
19 commits
Select commit
Hold shift + click to select a range
2159f8b
feat: schedule fetch trees optimally
ysmolski d49aaff
Merge branch 'main' of github.com:wundergraph/cosmo into yury/router-…
ysmolski 1db3ab9
use combined merger + scheduler
ysmolski cfb3956
fix fmt
ysmolski 9b5a317
fix tests
ysmolski 5203322
Merge branch 'main' of github.com:wundergraph/cosmo into yury/mf-befo…
ysmolski d385685
fix fmt
ysmolski 811da76
fix complex test
ysmolski e173358
fix test msg
ysmolski 374cf60
Merge branch 'main' of github.com:wundergraph/cosmo into yury/mf-befo…
ysmolski f8f51f7
make features disabled by default
ysmolski 909ba56
Merge branch 'main' of github.com:wundergraph/cosmo into yury/mf-befo…
ysmolski 14977fd
bump golang.org/x/text to v0.39.0
ysmolski d83220f
bump engine
ysmolski 8c1b118
update docs
ysmolski 73a6b12
add a combined test
ysmolski 331e9c2
go fmt
ysmolski ec3b0f9
engine 2.16.0
ysmolski 540bcf9
remove stale comment
ysmolski 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
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
198 changes: 198 additions & 0 deletions
198
router-tests/operations/multi_fetches_scheduling_test.go
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,198 @@ | ||
| package integration | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/sebdah/goldie/v2" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/wundergraph/cosmo/router-tests/testenv" | ||
| "github.com/wundergraph/cosmo/router/pkg/config" | ||
| ) | ||
|
|
||
| func TestMultiFetch(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // multiFetchQuery resolves Consultancy.lead from the employees subgraph, then needs | ||
| // two follow-up entity fetches: Employee.derivedMood and Consultancy.isLeadAvailable | ||
| const multiFetchQuery = `query Requires { | ||
| products { | ||
| __typename | ||
| ... on Consultancy { | ||
| lead { | ||
| __typename | ||
| id | ||
| derivedMood | ||
| } | ||
| isLeadAvailable | ||
| } | ||
| } | ||
| }` | ||
| const multiFetchExpectedResponse = `{"data":{"products":[{"__typename":"Consultancy","lead":{"__typename":"Employee","id":1,"derivedMood":"HAPPY"},"isLeadAvailable":false},{"__typename":"Cosmo"},{"__typename":"SDK"}]}}` | ||
|
|
||
| t.Run("merges same-wave entity fetches to the same subgraph when enabled", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| testenv.Run(t, &testenv.Config{ | ||
| ModifyEngineExecutionConfiguration: func(cfg *config.EngineExecutionConfiguration) { | ||
| cfg.EnableMultiFetch = true | ||
| }, | ||
| }, func(t *testing.T, xEnv *testenv.Environment) { | ||
| res := xEnv.MakeGraphQLRequestOK(testenv.GraphQLRequest{Query: multiFetchQuery}) | ||
| require.JSONEq(t, multiFetchExpectedResponse, res.Body) | ||
|
|
||
| // Root fetch + one merged entity fetch. | ||
| require.EqualValues(t, 2, xEnv.SubgraphRequestCount.Employees.Load()) | ||
|
|
||
| require.EqualValues(t, 1, xEnv.SubgraphRequestCount.Mood.Load()) | ||
| require.EqualValues(t, 1, xEnv.SubgraphRequestCount.Availability.Load()) | ||
| require.EqualValues(t, 4, xEnv.SubgraphRequestCount.Global.Load()) | ||
| }) | ||
| }) | ||
|
|
||
| t.Run("sends one request per entity fetch by default", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| testenv.Run(t, &testenv.Config{}, func(t *testing.T, xEnv *testenv.Environment) { | ||
| res := xEnv.MakeGraphQLRequestOK(testenv.GraphQLRequest{Query: multiFetchQuery}) | ||
| // Merging must not change the result: same response as the merged case above. | ||
| require.JSONEq(t, multiFetchExpectedResponse, res.Body) | ||
|
|
||
| // Root fetch + two separate entity fetches. | ||
| require.EqualValues(t, 3, xEnv.SubgraphRequestCount.Employees.Load()) | ||
|
|
||
| require.EqualValues(t, 1, xEnv.SubgraphRequestCount.Mood.Load()) | ||
| require.EqualValues(t, 1, xEnv.SubgraphRequestCount.Availability.Load()) | ||
| require.EqualValues(t, 5, xEnv.SubgraphRequestCount.Global.Load()) | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| // queryPlanNode is a minimal mirror of the query plan JSON for structural assertions. | ||
| type queryPlanNode struct { | ||
| Kind string `json:"kind"` | ||
| Children []queryPlanNode `json:"children"` | ||
| Fetch *struct { | ||
| Kind string `json:"kind"` | ||
| SubgraphName string `json:"subgraphName"` | ||
| } `json:"fetch"` | ||
| } | ||
|
|
||
| func (n *queryPlanNode) fetchKinds() []string { | ||
| var kinds []string | ||
| if n.Fetch != nil { | ||
| kinds = append(kinds, n.Fetch.Kind) | ||
| } | ||
| for i := range n.Children { | ||
| kinds = append(kinds, n.Children[i].fetchKinds()...) | ||
| } | ||
| return kinds | ||
| } | ||
|
|
||
| // TestMultiFetchAndScheduleFetches verifies that enable_multi_fetch and enable_schedule_fetches | ||
| // can be enabled together and that both affect the generated query plan. | ||
| func TestMultiFetchAndScheduleFetches(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| newGoldie := func(t *testing.T) *goldie.Goldie { | ||
| return goldie.New( | ||
| t, | ||
| goldie.WithFixtureDir("testdata/fixtures/query_plans"), | ||
| goldie.WithNameSuffix(".json"), | ||
| goldie.WithDiffEngine(goldie.ClassicDiff), | ||
| ) | ||
| } | ||
| bothFlags := func(cfg *config.EngineExecutionConfiguration) { | ||
| cfg.EnableMultiFetch = true | ||
| cfg.EnableScheduleFetches = true | ||
| cfg.Debug.AlwaysIncludeQueryPlan = true | ||
| } | ||
|
|
||
| queryPlanFromBody := func(t *testing.T, body string) queryPlanNode { | ||
| t.Helper() | ||
| var resp struct { | ||
| Extensions struct { | ||
| QueryPlan queryPlanNode `json:"queryPlan"` | ||
| } `json:"extensions"` | ||
| } | ||
| require.NoError(t, json.Unmarshal([]byte(body), &resp)) | ||
| return resp.Extensions.QueryPlan | ||
| } | ||
|
|
||
| t.Run("same-subgraph entity fetches merge into one MultiEntity fetch", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // Both employees entity fetches (derivedMood, isLeadAvailable requires) | ||
| // execute at the same point of the plan and merge. | ||
| const query = `query Requires { | ||
| products { | ||
| __typename | ||
| ... on Consultancy { | ||
| lead { | ||
| __typename | ||
| id | ||
| derivedMood | ||
| } | ||
| isLeadAvailable | ||
| } | ||
| } | ||
| }` | ||
|
|
||
| testenv.Run(t, &testenv.Config{ | ||
| ModifyEngineExecutionConfiguration: bothFlags, | ||
| }, func(t *testing.T, xEnv *testenv.Environment) { | ||
| res := xEnv.MakeGraphQLRequestOK(testenv.GraphQLRequest{Query: query}) | ||
| newGoldie(t).Assert(t, "response_with_query_plan_multi_fetch_schedule", indentedJSON(res.Body)) | ||
|
|
||
| plan := queryPlanFromBody(t, res.Body) | ||
| require.Contains(t, plan.fetchKinds(), "MultiEntity") | ||
|
|
||
| // root fetch + 1 merged entity fetch to employees | ||
| require.EqualValues(t, 2, xEnv.SubgraphRequestCount.Employees.Load()) | ||
|
|
||
| require.EqualValues(t, 1, xEnv.SubgraphRequestCount.Mood.Load()) | ||
| require.EqualValues(t, 1, xEnv.SubgraphRequestCount.Availability.Load()) | ||
| require.EqualValues(t, 4, xEnv.SubgraphRequestCount.Global.Load()) | ||
| }) | ||
| }) | ||
|
|
||
| t.Run("independent chains are scheduled without wave barriers", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // Two independent chains: employee -> mood -> employees (derivedMood requires currentMood) | ||
| // and findEmployees (family) -> availability. | ||
| // The legacy organizer produces Sequence(Parallel, Parallel, ...) waves; | ||
| // the scheduler runs both chains side by side under a Parallel root. | ||
| const query = `query IndependentChains { | ||
| employee(id: 1) { | ||
| id | ||
| derivedMood | ||
| } | ||
| findEmployees(criteria: { nationality: GERMAN }) { | ||
| id | ||
| isAvailable | ||
| } | ||
| }` | ||
|
|
||
| testenv.Run(t, &testenv.Config{ | ||
| ModifyEngineExecutionConfiguration: bothFlags, | ||
| }, func(t *testing.T, xEnv *testenv.Environment) { | ||
| res := xEnv.MakeGraphQLRequestOK(testenv.GraphQLRequest{Query: query}) | ||
| newGoldie(t).Assert(t, "response_with_query_plan_schedule_chains", indentedJSON(res.Body)) | ||
|
|
||
| plan := queryPlanFromBody(t, res.Body) | ||
| require.Equal(t, "Parallel", plan.Kind, "scheduled plan must run independent chains in parallel") | ||
| require.Len(t, plan.Children, 2) | ||
| for _, chain := range plan.Children { | ||
| require.Equal(t, "Sequence", chain.Kind) | ||
| } | ||
|
|
||
| require.EqualValues(t, 2, xEnv.SubgraphRequestCount.Employees.Load()) | ||
| require.EqualValues(t, 1, xEnv.SubgraphRequestCount.Family.Load()) | ||
| require.EqualValues(t, 1, xEnv.SubgraphRequestCount.Mood.Load()) | ||
| require.EqualValues(t, 1, xEnv.SubgraphRequestCount.Availability.Load()) | ||
| require.EqualValues(t, 5, xEnv.SubgraphRequestCount.Global.Load()) | ||
| }) | ||
| }) | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.