Skip to content

Commit 2768bf9

Browse files
msiomkindenis-ignatenko
authored andcommitted
Change the result format of Lua function for getting nodes
1 parent 54224b5 commit 2768bf9

File tree

2 files changed

+33
-40
lines changed

2 files changed

+33
-40
lines changed

tarantool/mesh_connection.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ def getnext(self):
2626
self.pos = (self.pos + 1) % len(self.addrs)
2727
return self.addrs[tmp]
2828

29+
def parse_uri(uri_str):
30+
if not uri_str:
31+
return
32+
uri = uri_str.split(':')
33+
host = uri[0]
34+
port = int(uri[1])
35+
if host and port:
36+
return {'host': host, 'port': port}
2937

3038
class MeshConnection(Connection):
3139
'''
@@ -37,51 +45,44 @@ class MeshConnection(Connection):
3745
the list and switch nodes in case of unavailability of the current node.
3846
3947
'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:
48+
Lua function used to refresh the list of available nodes. The function takes
49+
no parameters and returns a list of strings in format 'host:port'. A generic
50+
function for getting the list of nodes looks like this:
4251
4352
.. code-block:: lua
4453
45-
function get_nodes()
54+
function get_cluster_nodes()
4655
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+
'192.168.0.1:3301',
57+
'192.168.0.2:3302',
5658
-- ...
5759
}
5860
end
5961
6062
You may put in this list whatever you need depending on your cluster
6163
topology. Chances are you'll want to make the list of nodes from nodes'
62-
replication config. So here is an example for it:
64+
replication config. Here is an example for it:
6365
6466
.. code-block:: lua
6567
6668
local uri_lib = require('uri')
6769
68-
function get_nodes()
70+
function get_cluster_nodes()
6971
local nodes = {}
7072
7173
local replicas = box.cfg.replication
7274
7375
for i = 1, #replicas do
7476
local uri = uri_lib.parse(replicas[i])
75-
local port = tonumber(uri.service)
7677
77-
if uri.host and port then
78-
table.insert(nodes, { host = uri.host, port = port })
78+
if uri.host and uri.service then
79+
table.insert(nodes, uri.host .. ':' .. uri.service)
7980
end
8081
end
8182
8283
-- if your replication config doesn't contain the current node
8384
-- you have to add it manually like this:
84-
table.insert(nodes, { host = '192.168.0.1', port = 3301 })
85+
table.insert(nodes, '192.168.0.1:3301')
8586
8687
return nodes
8788
end
@@ -150,8 +151,14 @@ def refresh_nodes(self, cur_time):
150151
if not (resp.data and resp.data[0]):
151152
return
152153

153-
addrs = resp.data[0]
154-
if type(addrs) is list:
154+
addrs_raw = resp.data[0]
155+
if type(addrs_raw) is list:
156+
addrs = []
157+
for uri_str in addrs_raw:
158+
addr = parse_uri(uri_str)
159+
if addr:
160+
addrs.append(addr)
161+
155162
self.strategy = self.strategy_class(addrs)
156163
self.last_nodes_refresh = cur_time
157164
if not {'host': self.host, 'port': self.port} in addrs:

unit/suites/test_reconnect.py

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,8 @@ def test_03_mesh(self):
8686

8787
# get_nodes function contains both servers' addresses
8888
get_nodes = " \
89-
function get_nodes() \
90-
return { \
91-
{ \
92-
host = '%s', \
93-
port = tonumber(%d) \
94-
}, \
95-
{ \
96-
host = '%s', \
97-
port = tonumber(%d) \
98-
} \
99-
} \
89+
function get_cluster_nodes() \
90+
return { '%s:%d', '%s:%d' } \
10091
end" % (self.srv.host, self.srv.args['primary'], self.srv2.host, self.srv2.args['primary'])
10192

10293
# Create get_nodes function on servers
@@ -112,7 +103,7 @@ def test_03_mesh(self):
112103
'host': self.srv.host, 'port': self.srv.args['primary']}],
113104
user='test',
114105
password='test',
115-
get_nodes_function_name='get_nodes',
106+
get_nodes_function_name='get_cluster_nodes',
116107
connect_now=True)
117108

118109
# Check we work with the first server
@@ -150,13 +141,8 @@ def test_04_mesh_exclude_node(self):
150141

151142
# get_nodes function contains only the second server address
152143
get_nodes = " \
153-
function get_nodes() \
154-
return { \
155-
{ \
156-
host = '%s', \
157-
port = tonumber(%d) \
158-
} \
159-
} \
144+
function get_cluster_nodes() \
145+
return { '%s:%d' } \
160146
end" % (self.srv2.host, self.srv2.args['primary'])
161147

162148
# Create get_nodes function on servers
@@ -172,7 +158,7 @@ def test_04_mesh_exclude_node(self):
172158
'host': self.srv.host, 'port': self.srv.args['primary']}],
173159
user='test',
174160
password='test',
175-
get_nodes_function_name='get_nodes',
161+
get_nodes_function_name='get_cluster_nodes',
176162
connect_now=True)
177163

178164
# Check we work with the second server

0 commit comments

Comments
 (0)