Skip to content

Commit fbe61f3

Browse files
committed
IGNITE-14851 Enable partition awareness by default, fix unnecessary double connect - Fixes #42.
1 parent c64fb10 commit fbe61f3

File tree

15 files changed

+129
-62
lines changed

15 files changed

+129
-62
lines changed

docs/async_examples.rst

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

8585
Client transactions are supported for caches with
8686
:py:attr:`~pyignite.datatypes.cache_config.CacheAtomicityMode.TRANSACTIONAL` mode.
87+
**Supported only python 3.7+**
8788

8889
Let's create transactional cache:
8990

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

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

104105
.. literalinclude:: ../examples/transactions.py
105106
:language: python
106107
:dedent: 8
107-
:lines: 44-45
108+
:lines: 45-46
108109

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

111112
.. literalinclude:: ../examples/transactions.py
112113
:language: python
113114
:dedent: 8
114-
:lines: 48-59
115+
:lines: 49-61
115116

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

118119
.. literalinclude:: ../examples/transactions.py
119120
:language: python
120121
:dedent: 8
121-
:lines: 62-73
122+
:lines: 64-75
122123

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

docs/examples.rst

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

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

226226
.. literalinclude:: ../examples/transactions.py
227227
:language: python
228228
:dedent: 8
229-
:lines: 88-92
229+
:lines: 90-96
230230

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

233233
.. literalinclude:: ../examples/transactions.py
234234
:language: python
235235
:dedent: 8
236-
:lines: 94-95
236+
:lines: 98-99
237237

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

240240
.. literalinclude:: ../examples/transactions.py
241241
:language: python
242242
:dedent: 8
243-
:lines: 98-108
243+
:lines: 102-113
244244

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

247247
.. literalinclude:: ../examples/transactions.py
248248
:language: python
249249
:dedent: 8
250-
:lines: 111-121
250+
:lines: 116-126
251251

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

34.7 KB
Loading
30.5 KB
Loading

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Welcome to Apache Ignite binary client Python API documentation!
2222

2323
readme
2424
modules
25+
partition_awareness
2526
examples
2627
async_examples
2728

docs/partition_awareness.rst

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
.. Licensed to the Apache Software Foundation (ASF) under one or more
2+
contributor license agreements. See the NOTICE file distributed with
3+
this work for additional information regarding copyright ownership.
4+
The ASF licenses this file to You under the Apache License, Version 2.0
5+
(the "License"); you may not use this file except in compliance with
6+
the License. You may obtain a copy of the License at
7+
8+
.. http://www.apache.org/licenses/LICENSE-2.0
9+
10+
.. Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
===================
17+
Partition Awareness
18+
===================
19+
20+
Partition awareness allows the thin client to send query requests directly to the node that owns the queried data.
21+
22+
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.
23+
24+
.. image:: images/partitionawareness01.png
25+
:alt: Without partition awareness
26+
27+
Notice how queries must pass through the proxy server node, where they are routed to the correct node.
28+
29+
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.
30+
31+
.. image:: images/partitionawareness02.png
32+
:alt: With partition awareness
33+
34+
Partition awareness can be enabled or disabled by setting `partition_aware` parameter in
35+
:meth:`pyignite.client.Client.__init__` or :meth:`pyignite.aio_client.AioClient.__init__` to `True` (by default)
36+
or `False`.
37+
38+
Also, it is recommended to pass list of address and port pairs of all server nodes
39+
to :meth:`pyignite.client.Client.connect` or to :meth:`pyignite.aio_client.AioClient.connect`.
40+
41+
For example:
42+
43+
.. code-block:: python3
44+
45+
from pyignite import Client
46+
47+
client = Client(
48+
partition_awareness=True
49+
)
50+
nodes = [('10.128.0.1', 10800), ('10.128.0.2', 10800),...]
51+
with client.connect(nodes):
52+
....
53+
54+
.. code-block:: python3
55+
56+
from pyignite import AioClient
57+
58+
client = AioClient(
59+
partition_awareness=True
60+
)
61+
nodes = [('10.128.0.1', 10800), ('10.128.0.2', 10800),...]
62+
async with client.connect(nodes):
63+
....

docs/source/pyignite.transaction.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
limitations under the License.
1515
1616
pyignite.transaction module
17-
=========================
17+
===========================
1818

1919
.. automodule:: pyignite.transaction
2020
: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()

pyignite/aio_client.py

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

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

pyignite/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
Apache Ignite 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:`pyignite.client` module.

0 commit comments

Comments
 (0)