Skip to content

Commit c8d558b

Browse files
artur-barsegyanvasiliy-t
authored andcommitted
impl dns resolving via membership (#10)
1 parent 660cac0 commit c8d558b

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

examples/kv/key-value-store/init.lua

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,51 +22,53 @@ local memtx_memory = tonumber(os.getenv("TARANTOOL_MEMTX_MEMORY")) or (128 * 102
2222

2323
local http_port = os.getenv("TARANTOOL_HTTP_PORT") or 8081
2424

25-
local fiber = require('fiber')
2625
local log = require('log')
2726

28-
local http_client = require('http.client')
29-
local http_server = require('http.server')
27+
--- [HACK] Probing node via membership
28+
local function dns_resolver(opts)
29+
opts = opts or {}
30+
opts.timeout = opts.timeout or 100
3031

31-
local function resolve_uri(uri, timeout)
32-
if not uri then
33-
return nil, "Pass URI in the next format: uri:port"
34-
end
32+
local membership = require('membership')
3533

36-
timeout = timeout or 10
37-
-- local uri, port = uri:match("(^.*)%:(.*)")
38-
local resolved = false
39-
40-
local server_options = {
41-
log_errors = true,
42-
log_requests = log.debug
43-
}
34+
local hostname, port = advertise_uri:match("^(.*)%:(.*)")
35+
local ok, err = membership.init(hostname, tonumber(port))
36+
if not ok then
37+
log.error("[dns_resolver] Can't init a membership. Error: %s", err)
38+
os.exit(1)
39+
end
4440

45-
local srv = http_server.new("0.0.0.0", "3301", server_options)
46-
srv:route({ path = '/dns_resolver', method = 'GET' }, function(_) return { status = 200, text = 'Success' } end)
47-
srv:start()
41+
membership.set_encryption_key('test')
42+
membership.set_payload('alias', '__' .. advertise_uri)
4843

4944
local time = 0
50-
while time < timeout do
51-
local resp = http_client.get(uri .. '/dns_resolver')
52-
if resp.status ~= nil and resp.status == 200 then
45+
local resolved = false
46+
while time < opts.timeout do
47+
local ok = membership.probe_uri(membership.myself().uri)
48+
if ok then
5349
resolved = true
5450
break
55-
else
56-
print('Not resolved yet')
5751
end
5852

53+
log.info("DNS resolution has been failed. Trying to probe it again...")
5954
fiber.sleep(1)
6055
time = time + 1
6156
end
62-
srv:stop()
6357

64-
return resolved
58+
membership.leave()
59+
if not resolved then
60+
return false
61+
end
62+
63+
return true
6564
end
6665

67-
local t = resolve_uri(advertise_uri, 50)
68-
if not t then os.exit(1) end
69-
fiber.sleep(5)
66+
local resolved = dns_resolver({ timeout = 60 })
67+
if not resolved then
68+
log.error("[dns_resolver] Instance has not been resolved")
69+
os.exit(1)
70+
end
71+
--- End of hack
7072

7173
local ok, err = cartridge.cfg({
7274
alias = instance_name,

0 commit comments

Comments
 (0)