Skip to content

Commit 9dce8ff

Browse files
committed
Change the result format of Lua function for getting nodes
1 parent 766cac7 commit 9dce8ff

File tree

2 files changed

+27
-34
lines changed

2 files changed

+27
-34
lines changed

tarantool/mesh_connection.py

Lines changed: 25 additions & 18 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,22 +45,16 @@ 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
@@ -72,16 +74,15 @@ class MeshConnection(Connection):
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 and not addr in addrs:
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: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,7 @@ def test_03_mesh(self):
8787
# get_nodes function contains both servers' addresses
8888
get_nodes = " \
8989
function get_nodes() \
90-
return { \
91-
{ \
92-
host = '%s', \
93-
port = tonumber(%d) \
94-
}, \
95-
{ \
96-
host = '%s', \
97-
port = tonumber(%d) \
98-
} \
99-
} \
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
@@ -151,12 +142,7 @@ def test_04_mesh_exclude_node(self):
151142
# get_nodes function contains only the second server address
152143
get_nodes = " \
153144
function get_nodes() \
154-
return { \
155-
{ \
156-
host = '%s', \
157-
port = tonumber(%d) \
158-
} \
159-
} \
145+
return { '%s:%d' } \
160146
end" % (self.srv2.host, self.srv2.args['primary'])
161147

162148
# Create get_nodes function on servers

0 commit comments

Comments
 (0)