diff --git a/kong/cli/services/serf.lua b/kong/cli/services/serf.lua index 5db39e99562..e54460ff5e2 100644 --- a/kong/cli/services/serf.lua +++ b/kong/cli/services/serf.lua @@ -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 @@ -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) @@ -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 @@ -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 = { @@ -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 diff --git a/kong/core/cluster.lua b/kong/core/cluster.lua index a87918af983..fd91689d80c 100644 --- a/kong/core/cluster.lua +++ b/kong/core/cluster.lua @@ -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 @@ -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)) diff --git a/kong/tools/cluster.lua b/kong/tools/cluster.lua index 613067ca7db..5d4a5afc884 100644 --- a/kong/tools/cluster.lua +++ b/kong/tools/cluster.lua @@ -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 \ No newline at end of file