-
Notifications
You must be signed in to change notification settings - Fork 7k
[data] Ranker Interface #58513
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
bveeramani
merged 6 commits into
ray-project:master
from
iamjustinhsu:jhsu/ranker-iterface
Nov 12, 2025
Merged
[data] Ranker Interface #58513
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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,8 @@ | ||
| from .ranker import DefaultRanker, Ranker | ||
|
|
||
|
|
||
| def create_ranker() -> Ranker: | ||
| """Create a ranker instance based on environment and configuration.""" | ||
| from ray._private.ray_constants import env_bool | ||
|
|
||
| return DefaultRanker() |
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,100 @@ | ||
| """Ranker component for operator selection in streaming executor.""" | ||
|
|
||
| from abc import ABC, abstractmethod | ||
| from typing import TYPE_CHECKING, Generic, List, Protocol, Tuple, TypeVar | ||
|
|
||
| from ray.data._internal.execution.interfaces import PhysicalOperator | ||
| from ray.data._internal.execution.resource_manager import ResourceManager | ||
|
|
||
| if TYPE_CHECKING: | ||
| from ray.data._internal.execution.streaming_executor_state import Topology | ||
|
|
||
| # Protocol for comparable ranking values | ||
| class Comparable(Protocol): | ||
| """Protocol for types that can be compared for ranking.""" | ||
|
|
||
| def __lt__(self, other: "Comparable") -> bool: | ||
| ... | ||
|
|
||
| def __le__(self, other: "Comparable") -> bool: | ||
| ... | ||
|
|
||
| def __gt__(self, other: "Comparable") -> bool: | ||
| ... | ||
|
|
||
| def __ge__(self, other: "Comparable") -> bool: | ||
| ... | ||
|
|
||
| def __eq__(self, other: "Comparable") -> bool: | ||
| ... | ||
|
|
||
|
|
||
| # Generic type for comparable ranking values | ||
| RankingValue = TypeVar("RankingValue", bound=Comparable) | ||
|
|
||
|
|
||
| class Ranker(ABC, Generic[RankingValue]): | ||
| """Abstract base class for operator ranking strategies.""" | ||
|
|
||
| @abstractmethod | ||
| def rank_operator( | ||
| self, | ||
| op: PhysicalOperator, | ||
| topology: "Topology", | ||
| resource_manager: ResourceManager, | ||
| ) -> RankingValue: | ||
| """Rank operator for execution priority. | ||
|
|
||
| Operator to run next is selected as the one with the *smallest* value | ||
| of the lexicographically ordered ranks composed of (in order): | ||
|
|
||
| Args: | ||
| ops: Operator to rank | ||
| topology: Current execution topology | ||
| resource_manager: Resource manager for usage information | ||
|
|
||
| Returns: | ||
| Rank (tuple) for operator | ||
| """ | ||
| pass | ||
|
|
||
| def rank_operators( | ||
| self, | ||
| ops: List[PhysicalOperator], | ||
| topology: "Topology", | ||
| resource_manager: ResourceManager, | ||
| ) -> List[RankingValue]: | ||
|
|
||
| assert len(ops) > 0 | ||
| return [self.rank_operator(op, topology, resource_manager) for op in ops] | ||
|
|
||
|
|
||
| class DefaultRanker(Ranker[Tuple[int, int]]): | ||
| """Ranker implementation.""" | ||
|
|
||
| def rank_operator( | ||
| self, | ||
| op: PhysicalOperator, | ||
| topology: "Topology", | ||
| resource_manager: ResourceManager, | ||
| ) -> Tuple[int, int]: | ||
| """Computes rank for op. *Lower means better rank* | ||
|
|
||
| 1. Whether operator's could be throttled (int) | ||
| 2. Operators' object store utilization | ||
|
|
||
| Args: | ||
| op: Operators to rank | ||
iamjustinhsu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| topology: Current execution topology | ||
| resource_manager: Resource manager for usage information | ||
|
|
||
| Returns: | ||
| Rank (tuple) for operator | ||
| """ | ||
|
|
||
| throttling_disabled = 0 if op.throttling_disabled() else 1 | ||
|
|
||
| return ( | ||
| throttling_disabled, | ||
| resource_manager.get_op_usage(op).object_store_memory, | ||
| ) | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| """Comprehensive tests for the generic ranker type system.""" | ||
|
|
||
| from unittest.mock import MagicMock | ||
|
|
||
| import pytest | ||
|
|
||
| from ray.data._internal.execution.interfaces import PhysicalOperator | ||
| from ray.data._internal.execution.ranker import DefaultRanker, Ranker | ||
| from ray.data._internal.execution.resource_manager import ResourceManager | ||
| from ray.data._internal.execution.streaming_executor_state import Topology | ||
|
|
||
|
|
||
| def test_default_ranker(): | ||
| """Test that the ranker interface works correctly.""" | ||
| ranker = DefaultRanker() | ||
|
|
||
| # Mock objects | ||
| op1 = MagicMock() | ||
| op1.throttling_disabled.return_value = False | ||
| op2 = MagicMock() | ||
| op2.throttling_disabled.return_value = True | ||
| topology = {} | ||
| resource_manager = MagicMock() | ||
| resource_manager.get_op_usage.return_value = MagicMock() | ||
| resource_manager.get_op_usage.return_value.object_store_memory = 1024 | ||
|
|
||
| # Test rank_operator for first op | ||
| rank1 = ranker.rank_operator(op1, topology, resource_manager) | ||
| assert rank1 == (1, 1024) # throttling_disabled=False -> 1, memory=1024 | ||
|
|
||
| # Test rank_operator for second op | ||
| rank2 = ranker.rank_operator(op2, topology, resource_manager) | ||
| assert rank2 == (0, 1024) # throttling_disabled=True -> 0, memory=1024 | ||
|
|
||
| # Test rank_operators with both ops | ||
| ops = [op1, op2] | ||
| ranks = ranker.rank_operators(ops, topology, resource_manager) | ||
| assert ranks == [(1, 1024), (0, 1024)] | ||
|
|
||
|
|
||
| class IntRanker(Ranker[int]): | ||
| """Ranker that returns integer rankings.""" | ||
|
|
||
| def rank_operator( | ||
| self, | ||
| op: PhysicalOperator, | ||
| topology: "Topology", | ||
| resource_manager: ResourceManager, | ||
| ) -> int: | ||
| """Return integer ranking.""" | ||
| return resource_manager.get_op_usage(op).object_store_memory | ||
|
|
||
|
|
||
| def test_generic_types(): | ||
| """Test that specific generic types work correctly.""" | ||
| # Test integer ranker | ||
| int_ranker = IntRanker() | ||
| op1 = MagicMock() | ||
| op2 = MagicMock() | ||
| topology = {} | ||
| resource_manager = MagicMock() | ||
| resource_manager.get_op_usage.return_value = MagicMock() | ||
| resource_manager.get_op_usage.return_value.object_store_memory = 1024 | ||
|
|
||
| # Test rank_operator for first op | ||
| rank1 = int_ranker.rank_operator(op1, topology, resource_manager) | ||
| assert rank1 == 1024 | ||
|
|
||
| # Test rank_operator for second op | ||
| rank2 = int_ranker.rank_operator(op2, topology, resource_manager) | ||
| assert rank2 == 1024 | ||
|
|
||
| # Test rank_operators with both ops | ||
| ops = [op1, op2] | ||
| ranks = int_ranker.rank_operators(ops, topology, resource_manager) | ||
| assert ranks == [1024, 1024] | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import sys | ||
|
|
||
| sys.exit(pytest.main(["-v", __file__])) |
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
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.