Skip to content

Commit 766cac7

Browse files
committed
Add documentation on MeshConnection
1 parent 0da1242 commit 766cac7

File tree

6 files changed

+103
-6
lines changed

6 files changed

+103
-6
lines changed

README.rst

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Features
6565
* MsgPack data format and MsgPack based client-server protocol
6666
* Two data engines:
6767
* 100% in-memory with optional persistence
68-
* 2-level disk-based B-tree, to use with large data sets (powered by `Sophia`_)
68+
* LSM-tree based disk storage to use with large data sets
6969
* secondary key and index iterators support (can be non-unique and composite)
7070
* multiple index types: HASH, BITSET, TREE
7171
* asynchronous master-master replication
@@ -79,6 +79,26 @@ See More
7979
* `Tarantool User Guide`_
8080
* `Client-server Protocol Specification`_
8181

82+
How to use the connector
83+
^^^^^^^^^^^^^^^^^^^^^^^^
84+
85+
.. code-block:: lua
86+
87+
import tarantool
88+
con = tarantool.Connection(host='localhost',
89+
port=3301,
90+
user='test',
91+
password='test',
92+
connect_now=True)
93+
94+
95+
resp = con.eval('return "Hello Wrold!"')
96+
if resp.data[0] == "Hello World!":
97+
print('Ok')
98+
99+
This will print 'Ok' assuming that there's a running Tarantool instance on port
100+
3301 on local machine and user test has execute privilege on universe.
101+
82102
NOTE
83103
^^^^
84104

doc/api/class-mesh-connection.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
.. currentmodule:: tarantool.mesh_connection
3+
4+
class :class:`MeshConnection`
5+
-----------------------------
6+
7+
.. autoclass:: MeshConnection
8+
9+
.. automethod:: refresh_nodes(self, cur_time)

doc/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ API Reference
4040

4141
api/module-tarantool.rst
4242
api/class-connection.rst
43+
api/class-mesh-connection.rst
4344
api/class-space.rst
4445
api/class-response.rst
4546

doc/index.ru.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
api/module-tarantool.rst
4242
api/class-connection.rst
43+
api/class-mesh-connection.rst
4344
api/class-space.rst
4445
api/class-response.rst
4546

tarantool/mesh_connection.py

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,65 @@ def getnext(self):
2828

2929

3030
class MeshConnection(Connection):
31+
'''
32+
Represents a connection to a cluster of Tarantool servers.
33+
34+
This class uses Connection to connect to one of the nodes of the cluster.
35+
The initial list of nodes is passed to the constructor in 'addrs' parameter.
36+
The class set in 'strategy_class' parameter is used to select a node from
37+
the list and switch nodes in case of unavailability of the current node.
38+
39+
'get_nodes_function_name' param of the constructor sets the name of a stored
40+
Lua function used to refresh the list of available nodes. A generic function
41+
for getting the list of nodes looks like this:
42+
43+
.. code-block:: lua
44+
45+
function get_nodes()
46+
return {
47+
{
48+
host = '192.168.0.1',
49+
port = 3301
50+
},
51+
{
52+
host = '192.168.0.2',
53+
port = 3302
54+
},
55+
56+
-- ...
57+
}
58+
end
59+
60+
You may put in this list whatever you need depending on your cluster
61+
topology. Chances are you'll want to make the list of nodes from nodes'
62+
replication config. So here is an example for it:
63+
64+
.. code-block:: lua
65+
66+
local uri_lib = require('uri')
67+
68+
function get_nodes()
69+
local nodes = {}
70+
71+
local replicas = box.cfg.replication
72+
73+
for i = 1, #replicas do
74+
local uri = uri_lib.parse(replicas[i])
75+
local port = tonumber(uri.service)
76+
77+
if uri.host and port then
78+
table.insert(nodes, { host = uri.host, port = port })
79+
end
80+
end
81+
82+
-- if your replication config doesn't contain the current node
83+
-- you have to add it manually like this:
84+
table.insert(nodes, { host = '192.168.0.1', port = 3301 })
85+
86+
return nodes
87+
end
88+
'''
89+
3190
def __init__(self, addrs,
3291
user=None,
3392
password=None,
@@ -46,7 +105,7 @@ def __init__(self, addrs,
46105
host = addr['host']
47106
port = addr['port']
48107
self.get_nodes_function_name = get_nodes_function_name
49-
self.nodes_refresh_interval = nodes_refresh_interval
108+
self.nodes_refresh_interval = nodes_refresh_interval >= 30 and nodes_refresh_interval or 30
50109
self.last_nodes_refresh = 0
51110
super(MeshConnection, self).__init__(host=host,
52111
port=port,
@@ -78,6 +137,13 @@ def _opt_reconnect(self):
78137
self.refresh_nodes(now)
79138

80139
def refresh_nodes(self, cur_time):
140+
'''
141+
Refreshes nodes list by calling Lua function with name
142+
self.get_nodes_function_name on the current node. If this field is None
143+
no refresh occurs. Usually you don't need to call this function manually
144+
since it's called automatically during reconnect every
145+
self.nodes_refresh_interval seconds.
146+
'''
81147
resp = super(MeshConnection, self).call_ex(self.get_nodes_function_name,
82148
[], reconnect=False)
83149

unit/suites/test_reconnect.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ def test_03_mesh(self):
7676
# Start two servers
7777
self.srv.start()
7878
self.srv.admin("box.schema.user.create('test', { password = 'test', if_not_exists = true })")
79-
self.srv.admin("box.schema.user.grant('test', 'read,write,execute', 'universe')")
79+
self.srv.admin("box.schema.user.grant('test', 'execute', 'universe')")
8080

8181
self.srv2 = TarantoolServer()
8282
self.srv2.script = 'unit/suites/box.lua'
8383
self.srv2.start()
8484
self.srv2.admin("box.schema.user.create('test', { password = 'test', if_not_exists = true })")
85-
self.srv2.admin("box.schema.user.grant('test', 'read,write,execute', 'universe')")
85+
self.srv2.admin("box.schema.user.grant('test', 'execute', 'universe')")
8686

8787
# get_nodes function contains both servers' addresses
8888
get_nodes = " \
@@ -140,13 +140,13 @@ def test_04_mesh_exclude_node(self):
140140
# Start two servers
141141
self.srv.start()
142142
self.srv.admin("box.schema.user.create('test', { password = 'test', if_not_exists = true })")
143-
self.srv.admin("box.schema.user.grant('test', 'read,write,execute', 'universe')")
143+
self.srv.admin("box.schema.user.grant('test', 'execute', 'universe')")
144144

145145
self.srv2 = TarantoolServer()
146146
self.srv2.script = 'unit/suites/box.lua'
147147
self.srv2.start()
148148
self.srv2.admin("box.schema.user.create('test', { password = 'test', if_not_exists = true })")
149-
self.srv2.admin("box.schema.user.grant('test', 'read,write,execute', 'universe')")
149+
self.srv2.admin("box.schema.user.grant('test', 'execute', 'universe')")
150150

151151
# get_nodes function contains only the second server address
152152
get_nodes = " \

0 commit comments

Comments
 (0)