Skip to content

Commit a983639

Browse files
isapegoivandasch
andauthored
GG-33899 IGNITE-14851 Enable partition awareness by default, remove double connect (#51)
(cherry picked from commit fbe61f3) Co-authored-by: Ivan Daschinsky <[email protected]>
1 parent 58ed200 commit a983639

File tree

15 files changed

+128
-62
lines changed

15 files changed

+128
-62
lines changed

docs/async_examples.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ File: `transactions.py`_.
8383

8484
Client transactions are supported for caches with
8585
:py:attr:`~pygridgain.datatypes.cache_config.CacheAtomicityMode.TRANSACTIONAL` mode.
86+
**Supported only python 3.7+**
8687

8788
Let's create transactional cache:
8889

@@ -96,28 +97,28 @@ Let's start a transaction and commit it:
9697
.. literalinclude:: ../examples/transactions.py
9798
:language: python
9899
:dedent: 8
99-
:lines: 36-41
100+
:lines: 36-42
100101

101102
Let's check that the transaction was committed successfully:
102103

103104
.. literalinclude:: ../examples/transactions.py
104105
:language: python
105106
:dedent: 8
106-
:lines: 44-45
107+
:lines: 45-46
107108

108109
Let's check that raising exception inside `async with` block leads to transaction's rollback
109110

110111
.. literalinclude:: ../examples/transactions.py
111112
:language: python
112113
:dedent: 8
113-
:lines: 48-59
114+
:lines: 49-61
114115

115116
Let's check that timed out transaction is successfully rolled back
116117

117118
.. literalinclude:: ../examples/transactions.py
118119
:language: python
119120
:dedent: 8
120-
:lines: 62-73
121+
:lines: 64-75
121122

122123
See more info about transaction's parameters in a documentation of :py:meth:`~pygridgain.aio_client.AioClient.tx_start`
123124

docs/examples.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,35 +218,35 @@ Let's create transactional cache:
218218
.. literalinclude:: ../examples/transactions.py
219219
:language: python
220220
:dedent: 8
221-
:lines: 82-85
221+
:lines: 84-87
222222

223223
Let's start a transaction and commit it:
224224

225225
.. literalinclude:: ../examples/transactions.py
226226
:language: python
227227
:dedent: 8
228-
:lines: 88-92
228+
:lines: 90-96
229229

230230
Let's check that the transaction was committed successfully:
231231

232232
.. literalinclude:: ../examples/transactions.py
233233
:language: python
234234
:dedent: 8
235-
:lines: 94-95
235+
:lines: 98-99
236236

237237
Let's check that raising exception inside `with` block leads to transaction's rollback
238238

239239
.. literalinclude:: ../examples/transactions.py
240240
:language: python
241241
:dedent: 8
242-
:lines: 98-108
242+
:lines: 102-113
243243

244244
Let's check that timed out transaction is successfully rolled back
245245

246246
.. literalinclude:: ../examples/transactions.py
247247
:language: python
248248
:dedent: 8
249-
:lines: 111-121
249+
:lines: 116-126
250250

251251
See more info about transaction's parameters in a documentation of :py:meth:`~pygridgain.client.Client.tx_start`
252252

54.9 KB
Loading
43.7 KB
Loading

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Welcome to GridGain Community Edition binary client Python API documentation!
2121

2222
readme
2323
modules
24+
partition_awareness
2425
examples
2526
async_examples
2627

docs/partition_awareness.rst

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
.. Copyright 2021 GridGain Systems, Inc. and Contributors.
2+
3+
.. Licensed under the GridGain Community Edition License (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
.. https://www.gridgain.com/products/software/community-edition/gridgain-community-edition-license
8+
9+
.. Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
15+
===================
16+
Partition Awareness
17+
===================
18+
19+
Partition awareness allows the thin client to send query requests directly to the node that owns the queried data.
20+
21+
Without partition awareness, an application that is connected to the cluster via a thin client executes all queries and operations via a single server node that acts as a proxy for the incoming requests. These operations are then re-routed to the node that stores the data that is being requested. This results in a bottleneck that could prevent the application from scaling linearly.
22+
23+
.. image:: images/partitionawareness01.png
24+
:alt: Without partition awareness
25+
26+
Notice how queries must pass through the proxy server node, where they are routed to the correct node.
27+
28+
With partition awareness in place, the thin client can directly route queries and operations to the primary nodes that own the data required for the queries. This eliminates the bottleneck, allowing the application to scale more easily.
29+
30+
.. image:: images/partitionawareness02.png
31+
:alt: With partition awareness
32+
33+
Partition awareness can be enabled or disabled by setting `partition_aware` parameter in
34+
:meth:`pygridgain.client.Client.__init__` or :meth:`pygridgain.aio_client.AioClient.__init__` to `True` (by default)
35+
or `False`.
36+
37+
Also, it is recommended to pass list of address and port pairs of all server nodes
38+
to :meth:`pygridgain.client.Client.connect` or to :meth:`pygridgain.aio_client.AioClient.connect`.
39+
40+
For example:
41+
42+
.. code-block:: python3
43+
44+
from pygridgain import Client
45+
46+
client = Client(
47+
partition_awareness=True
48+
)
49+
nodes = [('10.128.0.1', 10800), ('10.128.0.2', 10800),...]
50+
with client.connect(nodes):
51+
....
52+
53+
.. code-block:: python3
54+
55+
from pygridgain import AioClient
56+
57+
client = AioClient(
58+
partition_awareness=True
59+
)
60+
nodes = [('10.128.0.1', 10800), ('10.128.0.2', 10800),...]
61+
async with client.connect(nodes):
62+
....

docs/source/pygridgain.transaction.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
limitations under the License.
1414
1515
pygridgain.transaction module
16-
=========================
16+
===========================
1717

1818
.. automodule:: pygridgain.transaction
1919
:members:

examples/transactions.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,21 @@ async def async_example():
3535
# starting transaction
3636
key = 1
3737
async with client.tx_start(
38-
isolation=TransactionIsolation.REPEATABLE_READ, concurrency=TransactionConcurrency.PESSIMISTIC
38+
isolation=TransactionIsolation.REPEATABLE_READ,
39+
concurrency=TransactionConcurrency.PESSIMISTIC
3940
) as tx:
4041
await cache.put(key, 'success')
4142
await tx.commit()
4243

4344
# key=1 value=success
4445
val = await cache.get(key)
45-
print(f"key=1 value={val}")
46+
print(f"key={key} value={val}")
4647

4748
# rollback transaction.
4849
try:
4950
async with client.tx_start(
50-
isolation=TransactionIsolation.REPEATABLE_READ, concurrency=TransactionConcurrency.PESSIMISTIC
51+
isolation=TransactionIsolation.REPEATABLE_READ,
52+
concurrency=TransactionConcurrency.PESSIMISTIC
5153
):
5254
await cache.put(key, 'fail')
5355
raise RuntimeError('test')
@@ -56,7 +58,7 @@ async def async_example():
5658

5759
# key=1 value=success
5860
val = await cache.get(key)
59-
print(f"key=1 value={val}")
61+
print(f"key={key} value={val}")
6062

6163
# rollback transaction on timeout.
6264
try:
@@ -70,7 +72,7 @@ async def async_example():
7072

7173
# key=1 value=success
7274
val = await cache.get(key)
73-
print(f"key=1 value={val}")
75+
print(f"key={key} value={val}")
7476

7577
# destroy cache
7678
await cache.destroy()
@@ -85,40 +87,43 @@ def sync_example():
8587
})
8688

8789
# starting transaction
90+
key = 1
8891
with client.tx_start(
89-
isolation=TransactionIsolation.REPEATABLE_READ, concurrency=TransactionConcurrency.PESSIMISTIC
92+
isolation=TransactionIsolation.REPEATABLE_READ,
93+
concurrency=TransactionConcurrency.PESSIMISTIC
9094
) as tx:
91-
cache.put(1, 'success')
95+
cache.put(key, 'success')
9296
tx.commit()
9397

9498
# key=1 value=success
95-
print(f"key=1 value={cache.get(1)}")
99+
print(f"key={key} value={cache.get(key)}")
96100

97101
# rollback transaction.
98102
try:
99103
with client.tx_start(
100-
isolation=TransactionIsolation.REPEATABLE_READ, concurrency=TransactionConcurrency.PESSIMISTIC
104+
isolation=TransactionIsolation.REPEATABLE_READ,
105+
concurrency=TransactionConcurrency.PESSIMISTIC
101106
):
102-
cache.put(1, 'fail')
107+
cache.put(key, 'fail')
103108
raise RuntimeError('test')
104109
except RuntimeError:
105110
pass
106111

107112
# key=1 value=success
108-
print(f"key=1 value={cache.get(1)}")
113+
print(f"key={key} value={cache.get(key)}")
109114

110115
# rollback transaction on timeout.
111116
try:
112117
with client.tx_start(timeout=1.0, label='long-tx') as tx:
113-
cache.put(1, 'fail')
118+
cache.put(key, 'fail')
114119
time.sleep(2.0)
115120
tx.commit()
116121
except CacheError as e:
117122
# Cache transaction timed out: GridNearTxLocal[...timeout=1000, ... label=long-tx]
118123
print(e)
119124

120125
# key=1 value=success
121-
print(f"key=1 value={cache.get(1)}")
126+
print(f"key={key} value={cache.get(key)}")
122127

123128
# destroy cache
124129
cache.destroy()

pygridgain/aio_client.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,18 @@ class AioClient(BaseClient):
6161
Asynchronous Client implementation.
6262
"""
6363

64-
def __init__(self, compact_footer: bool = None, partition_aware: bool = False, **kwargs):
64+
def __init__(self, compact_footer: bool = None, partition_aware: bool = True, **kwargs):
6565
"""
6666
Initialize client.
6767
6868
:param compact_footer: (optional) use compact (True, recommended) or
6969
full (False) schema approach when serializing Complex objects.
7070
Default is to use the same approach the server is using (None).
7171
Apache Ignite binary protocol documentation on this topic:
72-
https://apacheignite.readme.io/docs/binary-client-protocol-data-format#section-schema
72+
https://ignite.apache.org/docs/latest/binary-client-protocol/data-format#schema
7373
:param partition_aware: (optional) try to calculate the exact data
7474
placement from the key before to issue the key operation to the
75-
server node:
76-
https://cwiki.apache.org/confluence/display/IGNITE/IEP-23%3A+Best+Effort+Affinity+for+thin+clients
77-
The feature is in experimental status, so the parameter is `False`
78-
by default. This will be changed later.
75+
server node, `True` by default.
7976
"""
8077
super().__init__(compact_footer, partition_aware, **kwargs)
8178
self._registry_mux = asyncio.Lock()
@@ -495,7 +492,7 @@ def tx_start(self, concurrency: TransactionConcurrency = TransactionConcurrency.
495492
isolation: TransactionIsolation = TransactionIsolation.REPEATABLE_READ,
496493
timeout: Union[int, float] = 0, label: Optional[str] = None) -> 'AioTransaction':
497494
"""
498-
Start async thin client transaction.
495+
Start async thin client transaction. **Supported only python 3.7+**
499496
500497
:param concurrency: (optional) transaction concurrency, see
501498
:py:class:`~pygridgain.datatypes.transactions.TransactionConcurrency`

pygridgain/api/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
This module contains functions, that are (more or less) directly mapped to
1818
GridGain binary protocol operations. Read more:
1919
20-
https://apacheignite.readme.io/docs/binary-client-protocol#section-client-operations
20+
https://ignite.apache.org/docs/latest/binary-client-protocol/binary-client-protocol#client-operations
2121
2222
When the binary client protocol changes, these functions also change. For
2323
stable end user API see :mod:`pygridgain.client` module.

0 commit comments

Comments
 (0)