-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Python API update sessions and partitions services
- Loading branch information
1 parent
7399481
commit 888fa21
Showing
9 changed files
with
255 additions
and
16 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from typing import cast, List, Tuple | ||
|
||
from grpc import Channel | ||
|
||
from ..common import Direction, Partition | ||
from ..common.filter import Filter, NumberFilter | ||
from ..protogen.client.partitions_service_pb2_grpc import PartitionsStub | ||
from ..protogen.common.partitions_common_pb2 import ListPartitionsRequest, ListPartitionsResponse, GetPartitionRequest, GetPartitionResponse | ||
from ..protogen.common.partitions_fields_pb2 import PartitionField, PartitionRawField, PARTITION_RAW_ENUM_FIELD_PRIORITY | ||
from ..protogen.common.partitions_filters_pb2 import Filters as rawFilters, FiltersAnd as rawFiltersAnd, FilterField as rawFilterField | ||
from ..protogen.common.sort_direction_pb2 import SortDirection | ||
|
||
|
||
class PartitionFieldFilter: | ||
PRIORITY = NumberFilter( | ||
PartitionField(partition_raw_field=PartitionRawField(field=PARTITION_RAW_ENUM_FIELD_PRIORITY)), | ||
rawFilters, | ||
rawFiltersAnd, | ||
rawFilterField | ||
) | ||
|
||
|
||
class ArmoniKPartitions: | ||
def __init__(self, grpc_channel: Channel): | ||
""" Result service client | ||
Args: | ||
grpc_channel: gRPC channel to use | ||
""" | ||
self._client = PartitionsStub(grpc_channel) | ||
|
||
def list_partitions(self, partition_filter: Filter | None = None, page: int = 0, page_size: int = 1000, sort_field: Filter = PartitionFieldFilter.PRIORITY, sort_direction: SortDirection = Direction.ASC) -> Tuple[int, List[Partition]]: | ||
"""List partitions based on a filter. | ||
Args: | ||
partition_filter: Filter to apply when listing partitions | ||
page: page number to request, useful for pagination, defaults to 0 | ||
page_size: size of a page, defaults to 1000 | ||
sort_field: field to sort the resulting list by, defaults to the status | ||
sort_direction: direction of the sort, defaults to ascending | ||
Returns: | ||
A tuple containing : | ||
- The total number of results for the given filter | ||
- The obtained list of results | ||
""" | ||
request = ListPartitionsRequest( | ||
page=page, | ||
page_size=page_size, | ||
filters=cast(rawFilters, partition_filter.to_disjunction().to_message()) if partition_filter else None, | ||
sort=ListPartitionsRequest.Sort(field=cast(PartitionField, sort_field.field), direction=sort_direction), | ||
) | ||
response: ListPartitionsResponse = self._client.ListPartitions(request) | ||
return response.total, [Partition.from_message(p) for p in response.partitions] | ||
|
||
def get_partition(self, partition_id: str) -> Partition: | ||
"""Get a partition by its ID. | ||
Args: | ||
partition_id: The partition ID. | ||
Return: | ||
The partition summary. | ||
""" | ||
return Partition.from_message(self._client.GetPartition(GetPartitionRequest(id=partition_id)).partition) |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from .helpers import datetime_to_timestamp, timestamp_to_datetime, duration_to_timedelta, timedelta_to_duration, get_task_filter | ||
from .objects import Task, TaskDefinition, TaskOptions, Output, ResultAvailability, Session, Result | ||
from .enumwrapper import HealthCheckStatus, TaskStatus, Direction, ResultStatus | ||
from .objects import Task, TaskDefinition, TaskOptions, Output, ResultAvailability, Session, Result, Partition | ||
from .enumwrapper import HealthCheckStatus, TaskStatus, Direction, ResultStatus, SessionStatus | ||
from .filter import StringFilter, StatusFilter |
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,41 @@ | ||
from .conftest import all_rpc_called, rpc_called, get_client | ||
from armonik.client import ArmoniKPartitions, PartitionFieldFilter | ||
from armonik.common import Partition | ||
|
||
|
||
class TestArmoniKPartitions: | ||
|
||
def test_get_partitions(self): | ||
partitions_client: ArmoniKPartitions = get_client("Partitions") | ||
partition = partitions_client.get_partition("partition-id") | ||
|
||
assert rpc_called("Partitions", "GetPartition") | ||
assert isinstance(partition, Partition) | ||
assert partition.id == 'partition-id' | ||
assert partition.parent_partition_ids == [] | ||
assert partition.pod_reserved == 1 | ||
assert partition.pod_max == 1 | ||
assert partition.pod_configuration == {} | ||
assert partition.preemption_percentage == 0 | ||
assert partition.priority == 1 | ||
|
||
def test_list_partitions_no_filter(self): | ||
partitions_client: ArmoniKPartitions = get_client("Partitions") | ||
num, partitions = partitions_client.list_partitions() | ||
|
||
assert rpc_called("Partitions", "GetPartition") | ||
# TODO: Mock must be updated to return something and so that changes the following assertions | ||
assert num == 0 | ||
assert partitions == [] | ||
|
||
def test_list_partitions_with_filter(self): | ||
partitions_client: ArmoniKPartitions = get_client("Partitions") | ||
num, partitions = partitions_client.list_partitions(PartitionFieldFilter.PRIORITY == 1) | ||
|
||
assert rpc_called("Partitions", "GetPartition", 2) | ||
# TODO: Mock must be updated to return something and so that changes the following assertions | ||
assert num == 0 | ||
assert partitions == [] | ||
|
||
def test_service_fully_implemented(self): | ||
assert all_rpc_called("Partitions") |
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,72 @@ | ||
import datetime | ||
|
||
from .conftest import all_rpc_called, rpc_called, get_client | ||
from armonik.client import ArmoniKSessions, SessionFieldFilter | ||
from armonik.common import Session, SessionStatus, TaskOptions | ||
|
||
|
||
class TestArmoniKSessions: | ||
|
||
def test_create_session(self): | ||
sessions_client: ArmoniKSessions = get_client("Sessions") | ||
default_task_options = TaskOptions( | ||
max_duration=datetime.timedelta(seconds=1), | ||
priority=1, | ||
max_retries=1 | ||
) | ||
session_id = sessions_client.create_session(default_task_options) | ||
|
||
assert rpc_called("Sessions", "CreateSession") | ||
assert session_id == "session-id" | ||
|
||
def test_get_session(self): | ||
sessions_client: ArmoniKSessions = get_client("Sessions") | ||
session = sessions_client.get_session("session-id") | ||
|
||
assert rpc_called("Sessions", "GetSession") | ||
assert isinstance(session, Session) | ||
assert session.session_id == 'session-id' | ||
assert session.status == SessionStatus.CANCELLED | ||
assert session.partition_ids == [] | ||
assert session.options == TaskOptions( | ||
max_duration=datetime.timedelta(0), | ||
priority=0, | ||
max_retries=0, | ||
partition_id='', | ||
application_name='', | ||
application_version='', | ||
application_namespace='', | ||
application_service='', | ||
engine_type='', | ||
options={} | ||
) | ||
assert session.created_at == datetime.datetime(1970, 1, 1, 0, 0, tzinfo=datetime.timezone.utc) | ||
assert session.cancelled_at == datetime.datetime(1970, 1, 1, 0, 0, tzinfo=datetime.timezone.utc) | ||
assert session.duration == datetime.timedelta(0) | ||
|
||
def test_list_session_no_filter(self): | ||
sessions_client: ArmoniKSessions = get_client("Sessions") | ||
num, sessions = sessions_client.list_sessions() | ||
|
||
assert rpc_called("Sessions", "ListSessions") | ||
# TODO: Mock must be updated to return something and so that changes the following assertions | ||
assert num == 0 | ||
assert sessions == [] | ||
|
||
def test_list_session_with_filter(self): | ||
sessions_client: ArmoniKSessions = get_client("Sessions") | ||
num, sessions = sessions_client.list_sessions(SessionFieldFilter.STATUS == SessionStatus.RUNNING) | ||
|
||
assert rpc_called("Sessions", "ListSessions", 2) | ||
# TODO: Mock must be updated to return something and so that changes the following assertions | ||
assert num == 0 | ||
assert sessions == [] | ||
|
||
def test_cancel_session(self): | ||
sessions_client: ArmoniKSessions = get_client("Sessions") | ||
sessions_client.cancel_session("session-id") | ||
|
||
assert rpc_called("Sessions", "CancelSession") | ||
|
||
def test_service_fully_implemented(self): | ||
assert all_rpc_called("Sessions") |