fix(query): evict highest-cost node when MaxFrontierSize exceeded in shortest path#9607
Open
veeceey wants to merge 1 commit intodgraph-io:mainfrom
Open
fix(query): evict highest-cost node when MaxFrontierSize exceeded in shortest path#9607veeceey wants to merge 1 commit intodgraph-io:mainfrom
veeceey wants to merge 1 commit intodgraph-io:mainfrom
Conversation
…in shortest path When MaxFrontierSize is triggered, the code was calling pq.Pop() directly on the slice instead of using heap.Pop(). This has two problems: 1. pq.Pop() removes the last element of the underlying array, not the highest-cost item. In a min-heap, the last element has no guaranteed cost ordering, so it may remove a low-cost node that's actually needed for the shortest path. 2. Calling pq.Pop() directly (without heap.Pop) doesn't maintain the heap invariant, corrupting the priority queue for subsequent operations. Fix this by adding a removeMax() method that finds and removes the highest-cost item from the priority queue using heap.Remove(). This ensures we always evict the least promising node while keeping the heap valid, so the shortest path is preserved even when the frontier size is limited. Fixes dgraph-io#9577
Author
Test ResultsQuery package tests all pass: Package builds cleanly: The systest/shortest-path tests require a running dgraph instance so I couldn't run those locally, but the fix is logically straightforward - replacing direct slice manipulation with proper heap removal of the max-cost node. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #9577
The
MaxFrontierSizefeature introduced in #9333 has a bug in how it evicts nodes from the priority queue when the size limit is exceeded.The current code calls
pq.Pop()directly on the slice (notheap.Pop), which:container/heap, corrupting the queue for subsequentheap.Push/heap.Popcalls.The fix adds a
removeMax()method that scans for the highest-cost item and removes it usingheap.Remove(), which properly rebalances the heap. This ensures we always evict the least promising node while preserving the priority queue invariant and keeping shortest-path correctness.Affects both the single shortest path (
shortestPath) and k-shortest-path (runKShortestPaths) code paths.All existing query package tests pass.