Skip to content

Commit 832cf97

Browse files
authored
GG-33025: Add typing for Cluster and AioCluster (#39)
1 parent 7a62cd3 commit 832cf97

File tree

11 files changed

+56
-20
lines changed

11 files changed

+56
-20
lines changed

pygridgain/aio_cluster.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def __init__(self, client: 'AioClient'):
3333
self._client = client
3434

3535
@status_to_exception(ClusterError)
36-
async def get_state(self):
36+
async def get_state(self) -> 'ClusterState':
3737
"""
3838
Gets current cluster state.
3939
@@ -43,7 +43,7 @@ async def get_state(self):
4343
return await cluster_get_state_async(await self._client.random_node())
4444

4545
@status_to_exception(ClusterError)
46-
async def set_state(self, state):
46+
async def set_state(self, state: 'ClusterState'):
4747
"""
4848
Changes current cluster state to the given.
4949

pygridgain/api/cluster.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
from pygridgain.api import APIResult
1717
from pygridgain.connection import AioConnection, Connection
1818
from pygridgain.datatypes import Byte
19-
from pygridgain.exceptions import NotSupportedByClusterError
19+
from pygridgain.datatypes.cluster_state import ClusterState
20+
from pygridgain.exceptions import NotSupportedByClusterError, ClusterError
2021
from pygridgain.queries import Query, query_perform
2122
from pygridgain.queries.op_codes import OP_CLUSTER_GET_STATE, OP_CLUSTER_CHANGE_STATE
2223

@@ -44,7 +45,7 @@ async def cluster_get_state_async(connection: 'AioConnection', query_id=None) ->
4445

4546
def __post_process_get_state(result):
4647
if result.status == 0:
47-
result.value = result.value['state']
48+
result.value = ClusterState(result.value['state'])
4849
return result
4950

5051

@@ -60,7 +61,7 @@ def __cluster_get_state(connection, query_id):
6061
)
6162

6263

63-
def cluster_set_state(connection: 'Connection', state: int, query_id=None) -> 'APIResult':
64+
def cluster_set_state(connection: 'Connection', state: ClusterState, query_id=None) -> 'APIResult':
6465
"""
6566
Set cluster state.
6667
@@ -75,7 +76,7 @@ def cluster_set_state(connection: 'Connection', state: int, query_id=None) -> 'A
7576
return __cluster_set_state(connection, state, query_id)
7677

7778

78-
async def cluster_set_state_async(connection: 'AioConnection', state: int, query_id=None) -> 'APIResult':
79+
async def cluster_set_state_async(connection: 'AioConnection', state: ClusterState, query_id=None) -> 'APIResult':
7980
"""
8081
Async version of cluster_get_state
8182
"""
@@ -89,6 +90,9 @@ def __post_process_set_state(result):
8990

9091

9192
def __cluster_set_state(connection, state, query_id):
93+
if not ClusterState.has_value(state):
94+
raise ClusterError(f'Unknown cluster state [state={state}]')
95+
9296
if not connection.protocol_context.is_cluster_api_supported():
9397
raise NotSupportedByClusterError('Cluster API is not supported by the cluster')
9498

pygridgain/api/key_value.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ def cache_put(connection: 'Connection', cache: Union[str, int], key: Any, value:
5757

5858

5959
async def cache_put_async(connection: 'AioConnection', cache: Union[str, int], key: Any, value: Any,
60-
key_hint: 'GridGainDataType' = None, value_hint: 'GridGainDataType' = None, binary: bool = False,
60+
key_hint: 'GridGainDataType' = None, value_hint: 'GridGainDataType' = None,
61+
binary: bool = False,
6162
query_id: Optional[int] = None) -> 'APIResult':
6263
"""
6364
Async version of cache_put
@@ -413,7 +414,8 @@ def __cache_get_and_put(connection, cache, key, value, key_hint, value_hint, bin
413414

414415

415416
def cache_get_and_replace(connection: 'Connection', cache: Union[str, int], key: Any, value: Any,
416-
key_hint: 'GridGainDataType' = None, value_hint: 'GridGainDataType' = None, binary: bool = False,
417+
key_hint: 'GridGainDataType' = None, value_hint: 'GridGainDataType' = None,
418+
binary: bool = False,
417419
query_id: Optional[int] = None) -> 'APIResult':
418420
"""
419421
Puts a value with a given key to cache, returning previous value
@@ -473,7 +475,8 @@ def __cache_get_and_replace(connection, cache, key, key_hint, value, value_hint,
473475
)
474476

475477

476-
def cache_get_and_remove(connection: 'Connection', cache: Union[str, int], key: Any, key_hint: 'GridGainDataType' = None,
478+
def cache_get_and_remove(connection: 'Connection', cache: Union[str, int], key: Any,
479+
key_hint: 'GridGainDataType' = None,
477480
binary: bool = False, query_id: Optional[int] = None) -> 'APIResult':
478481
"""
479482
Removes the cache entry with specified key, returning the value.
@@ -740,7 +743,8 @@ def cache_replace_if_equals(connection: 'Connection', cache: Union[str, int], ke
740743

741744
async def cache_replace_if_equals_async(
742745
connection: 'AioConnection', cache: Union[str, int], key: Any, sample: Any, value: Any,
743-
key_hint: 'GridGainDataType' = None, sample_hint: 'GridGainDataType' = None, value_hint: 'GridGainDataType' = None,
746+
key_hint: 'GridGainDataType' = None, sample_hint: 'GridGainDataType' = None,
747+
value_hint: 'GridGainDataType' = None,
744748
binary: bool = False, query_id: Optional[int] = None) -> 'APIResult':
745749
"""
746750
Async version of cache_replace_if_equals.
@@ -1259,4 +1263,5 @@ def internal(result):
12591263
result.value = result.value[key]
12601264

12611265
return result
1266+
12621267
return internal

pygridgain/cluster.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def __init__(self, client: 'Client'):
3434
self._client = client
3535

3636
@status_to_exception(ClusterError)
37-
def get_state(self):
37+
def get_state(self) -> 'ClusterState':
3838
"""
3939
Gets current cluster state.
4040
@@ -44,7 +44,7 @@ def get_state(self):
4444
return cluster_get_state(self._client.random_node)
4545

4646
@status_to_exception(ClusterError)
47-
def set_state(self, state):
47+
def set_state(self, state: 'ClusterState'):
4848
"""
4949
Changes current cluster state to the given.
5050

pygridgain/connection/handshake.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from typing import Optional
1717

1818
from pygridgain.connection.protocol_context import ProtocolContext
19-
from pygridgain.datatypes import Byte, ByteArrayObject, Int, MapObject, Short, String, UUIDObject, ByteArrayObject
19+
from pygridgain.datatypes import Byte, Int, MapObject, Short, String, UUIDObject, ByteArrayObject
2020
from pygridgain.datatypes.internal import Struct
2121
from pygridgain.stream import READ_BACKWARD
2222

pygridgain/datatypes/cluster_state.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,9 @@ class ClusterState(IntEnum):
2727
#: Cluster activated. Cache read operation allowed, Cache data change operation
2828
#: aren't allowed.
2929
ACTIVE_READ_ONLY = 2
30+
31+
@classmethod
32+
def has_value(cls, value):
33+
if not hasattr(cls, 'values'):
34+
cls.values = set(item.value for item in ClusterState)
35+
return value in cls.values

tests/affinity/test_affinity_bad_servers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from tests.affinity.conftest import CLIENT_SOCKET_TIMEOUT
2121
from tests.util import start_ignite, kill_process_tree
2222

23+
2324
@pytest.fixture(params=['with-partition-awareness', 'without-partition-awareness'])
2425
def with_partition_awareness(request):
2526
yield request.param == 'with-partition-awareness'

tests/affinity/test_affinity_request_routing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
from pygridgain.constants import PROTOCOL_BYTE_ORDER
2828
from pygridgain.datatypes import String, LongObject
2929
from pygridgain.datatypes.cache_config import CacheMode
30-
from pygridgain.datatypes.prop_codes import PROP_NAME, PROP_BACKUPS_NUMBER, PROP_CACHE_KEY_CONFIGURATION, PROP_CACHE_MODE
30+
from pygridgain.datatypes.prop_codes import PROP_NAME, PROP_BACKUPS_NUMBER, PROP_CACHE_KEY_CONFIGURATION,\
31+
PROP_CACHE_MODE
3132
from tests.util import wait_for_condition, wait_for_condition_async, start_ignite, kill_process_tree
3233

3334
try:

tests/common/test_sql.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
# limitations under the License.
1515
#
1616
import pytest
17-
from pygridgain.exceptions import SQLError
1817

1918
from pygridgain import AioClient
2019
from pygridgain.aio_cache import AioCache

tests/custom/test_cluster.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import pytest
1818

1919
from pygridgain import Client, AioClient
20-
from pygridgain.exceptions import CacheError
20+
from pygridgain.exceptions import CacheError, ClusterError
2121
from tests.util import clear_ignite_work_dir, start_ignite_gen
2222

2323
from pygridgain.datatypes.cluster_state import ClusterState
@@ -45,7 +45,17 @@ def server2(with_persistence, cleanup):
4545
yield from start_ignite_gen(idx=2, use_persistence=with_persistence)
4646

4747

48-
def test_cluster_set_active(with_persistence):
48+
def check_cluster_state_error(cluster, state: int):
49+
with pytest.raises(ClusterError, match=f'Unknown cluster state \\[state={state}\\]'):
50+
cluster.set_state(state)
51+
52+
53+
async def check_cluster_state_error_async(cluster, state: int):
54+
with pytest.raises(ClusterError, match=f'Unknown cluster state \\[state={state}\\]'):
55+
await cluster.set_state(state)
56+
57+
58+
def test_cluster_set_state(with_persistence):
4959
key = 42
5060
val = 42
5161
start_state = ClusterState.INACTIVE if with_persistence else ClusterState.ACTIVE
@@ -58,6 +68,11 @@ def test_cluster_set_active(with_persistence):
5868
cluster.set_state(ClusterState.ACTIVE)
5969
assert cluster.get_state() == ClusterState.ACTIVE
6070

71+
check_cluster_state_error(cluster, 3)
72+
check_cluster_state_error(cluster, 42)
73+
check_cluster_state_error(cluster, 1234567890987654321)
74+
check_cluster_state_error(cluster, -1)
75+
6176
cache = client.get_or_create_cache("test_cache")
6277
cache.put(key, val)
6378
assert cache.get(key) == val
@@ -86,7 +101,7 @@ def test_cluster_set_active(with_persistence):
86101

87102

88103
@pytest.mark.asyncio
89-
async def test_cluster_set_active_async(with_persistence):
104+
async def test_cluster_set_state_async(with_persistence):
90105
key = 42
91106
val = 42
92107
start_state = ClusterState.INACTIVE if with_persistence else ClusterState.ACTIVE
@@ -99,6 +114,11 @@ async def test_cluster_set_active_async(with_persistence):
99114
await cluster.set_state(ClusterState.ACTIVE)
100115
assert await cluster.get_state() == ClusterState.ACTIVE
101116

117+
await check_cluster_state_error_async(cluster, 3)
118+
await check_cluster_state_error_async(cluster, 42)
119+
await check_cluster_state_error_async(cluster, 1234567890987654321)
120+
await check_cluster_state_error_async(cluster, -1)
121+
102122
cache = await client.get_or_create_cache("test_cache")
103123
await cache.put(key, val)
104124
assert await cache.get(key) == val

0 commit comments

Comments
 (0)