Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closing #1255 #1281

Merged
merged 1 commit into from
Jun 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions kong/cli/services/serf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ echo $COMMAND | ]]..luajit_path..[[
return false, res
end

-- Create the unique identifier if it doesn't exist
local _, err = cluster_utils.create_node_identifier(self._configuration)
if err then
return false, err
end

return true
end

Expand Down Expand Up @@ -112,7 +118,7 @@ function Serf:_autojoin(current_node_name)
return false, tostring(err)
else
if #nodes == 0 then
logger:warn("Cannot auto-join the cluster because no nodes were found")
logger:info("No other Kong nodes were found in the cluster")
else
-- Sort by newest to oldest (although by TTL would be a better sort)
table.sort(nodes, function(a, b)
Expand Down Expand Up @@ -144,7 +150,7 @@ function Serf:_add_node()
return false, err
end

local name = cluster_utils.get_node_name(self._configuration)
local name = cluster_utils.get_node_identifier(self._configuration)
local addr
for _, member in ipairs(members) do
if member.name == name then
Expand Down Expand Up @@ -178,7 +184,7 @@ function Serf:start()
return nil, err
end

local node_name = cluster_utils.get_node_name(self._configuration)
local node_name = cluster_utils.get_node_identifier(self._configuration)

-- Prepare arguments
local cmd_args = {
Expand Down Expand Up @@ -269,7 +275,7 @@ function Serf:stop()
-- Remove the node from the datastore.
-- This is useful when this is the only node running in the cluster.
self._dao_factory.nodes:delete({
name = cluster_utils.get_node_name(self._configuration)
name = cluster_utils.get_node_identifier(self._configuration)
})

-- Finally stop Serf
Expand Down
4 changes: 2 additions & 2 deletions kong/core/cluster.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ local function async_autojoin(premature)
ngx.log(ngx.ERR, tostring(err))
elseif #members < 2 then
-- Trigger auto-join
local _, err = singletons.serf:_autojoin(cluster_utils.get_node_name(singletons.configuration))
local _, err = singletons.serf:_autojoin(cluster_utils.get_node_identifier(singletons.configuration))
if err then
ngx.log(ngx.ERR, tostring(err))
end
Expand Down Expand Up @@ -73,7 +73,7 @@ local function send_keepalive(premature)
local elapsed = lock:lock("keepalive")
if elapsed and elapsed == 0 then
-- Send keepalive
local node_name = cluster_utils.get_node_name(singletons.configuration)
local node_name = cluster_utils.get_node_identifier(singletons.configuration)
local nodes, err = singletons.dao.nodes:find_all {name = node_name}
if err then
ngx.log(ngx.ERR, tostring(err))
Expand Down
25 changes: 23 additions & 2 deletions kong/tools/cluster.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
local IO = require "kong.tools.io"
local utils = require "kong.tools.utils"
local singletons = require "kong.singletons"

local _M = {}

function _M.get_node_name(conf)
return utils.get_hostname().."_"..conf.cluster_listen
local IDENTIFIER = "serf.id"

function _M.get_node_identifier(conf)
local id = singletons.serf_id
if not id then
id = IO.read_file(IO.path:join(conf.nginx_working_dir, IDENTIFIER))
singletons.serf_id = id
end
return id
end

function _M.create_node_identifier(conf)
local path = IO.path:join(conf.nginx_working_dir, IDENTIFIER)
if not IO.file_exists(path) then
local id = utils.get_hostname().."_"..conf.cluster_listen.."_"..utils.random_string()
local _, err = IO.write_to_file(path, id)
if err then
return false, err
end
end
return true
end

return _M