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

Adding node on start, and not on hook #1008

Merged
merged 1 commit into from
Feb 19, 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
4 changes: 1 addition & 3 deletions kong/api/routes/cluster.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local responses = require "kong.tools.responses"
local cjson = require "cjson"
local Serf = require "kong.cli.services.serf"

local pairs = pairs
Expand All @@ -9,12 +8,11 @@ local string_upper = string.upper
return {
["/cluster/"] = {
GET = function(self, dao_factory, helpers)
local res, err = Serf(configuration):invoke_signal("members", {["-format"] = "json"})
local members, err = Serf(configuration):_members()
if err then
return responses.send_HTTP_INTERNAL_SERVER_ERROR(err)
end

local members = cjson.decode(res).members
local result = {data = {}}
for _, v in pairs(members) do
if not self.params.status or (self.params.status and v.status == self.params.status) then
Expand Down
47 changes: 46 additions & 1 deletion kong/cli/services/serf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ function Serf:_join_node(address)
return true
end

function Serf:_members()
local res, err = self:invoke_signal("members", {["-format"] = "json"})
if err then
return false, err
end

return cjson.decode(res).members
end

function Serf:_autojoin(current_node_name)
if self._configuration.cluster["auto-join"] then
logger:info("Trying to auto-join Kong nodes, please wait..")
Expand Down Expand Up @@ -129,6 +138,36 @@ function Serf:_autojoin(current_node_name)
return true
end

function Serf:_add_node()
local members, err = self:_members()
if err then
return false, err
end

local name = cluster_utils.get_node_name(self._configuration)
local addr
for _, member in ipairs(members) do
if member.name == name then
addr = member.addr
break
end
end

if not addr then
return false, "Can't find current member address"
end

local _, err = self._dao_factory.nodes:insert({
name = name,
cluster_listening_address = stringy.strip(addr)
})
if err then
return false, err
end

return true
end

function Serf:start()
if self:is_running() then
return nil, SERVICE_NAME.." is already running"
Expand Down Expand Up @@ -168,7 +207,13 @@ function Serf:start()
logger:info(string.format([[serf ..............%s]], str_cmd_args))

-- Auto-Join nodes
return self:_autojoin(node_name)
local ok, err = self:_autojoin(node_name)
if not ok then
return nil, err
end

-- Adding node to nodes table
return self:_add_node()
else
-- Get last error message
local parts = stringy.split(IO.read_file(self._log_path), "\n")
Expand Down
9 changes: 5 additions & 4 deletions kong/cli/utils/services.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ _M.STATUSES = {
-- Services ordered by priority
local services = {
require "kong.cli.services.dnsmasq",
require "kong.cli.services.nginx",
require "kong.cli.services.serf"
require "kong.cli.services.serf",
require "kong.cli.services.nginx"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you will like this @mars - although terminating the foreground process will not terminate all the other services (like < v0.6.x was erroneously doing). As opposed to #1007 which terminates the other process when the main one is terminated (but not really an issue if terminating means shutting down the whole Docker container).

Basically this would allow the older behavior to work again. Still, not a replacement of supervisor (or similar solutions).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would allow the older behavior to work again.

The old behavior being "run kong w/ Nginx daemon off" to get stderr/stdout streams?

It's an improvement indeed, as log spelunking on Docker containers makes ops difficult. Stream all the logs!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. Although, like the old behavior, if you terminate nginx it will not terminate the other services, but it doesn't matter if terminating nginx means shutting down the entire container.

}

local function prepare_database(configuration)
Expand Down Expand Up @@ -116,8 +116,9 @@ function _M.check_status(configuration, configuration_path)
end

function _M.stop_all(configuration, configuration_path)
for _, service in ipairs(services) do
service(configuration, configuration_path):stop()
-- Backwards
for i=#services, 1, -1 do
services[i](configuration, configuration_path):stop()
end
end

Expand Down
4 changes: 1 addition & 3 deletions kong/core/cluster.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local cluster_utils = require "kong.tools.cluster"
local Serf = require "kong.cli.services.serf"
local cache = require "kong.tools.database_cache"
local cjson = require "cjson"

local resty_lock
local status, res = pcall(require, "resty.lock")
Expand Down Expand Up @@ -40,12 +39,11 @@ local function async_autojoin(premature)
ngx.log(ngx.ERR, tostring(err))
elseif count > 1 then
local serf = Serf(configuration)
local res, err = serf:invoke_signal("members", {["-format"] = "json"})
local members, err = serf:_members()
if err then
ngx.log(ngx.ERR, tostring(err))
end

local members = cjson.decode(res).members
if #members < 2 then
-- Trigger auto-join
local _, err = serf:_autojoin(cluster_utils.get_node_name(configuration))
Expand Down
18 changes: 4 additions & 14 deletions kong/core/hooks.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local events = require "kong.core.events"
local cache = require "kong.tools.database_cache"
local stringy = require "stringy"
local cjson = require "cjson"
local Serf = require "kong.cli.services.serf"

local function invalidate_plugin(entity)
Expand All @@ -24,11 +23,11 @@ end

local function get_cluster_members()
local serf = require("kong.cli.services.serf")(configuration)
local res, err = serf:invoke_signal("members", { ["-format"] = "json" })
local members, err = serf:_members()
if err then
ngx.log(ngx.ERR, err)
else
return cjson.decode(res).members
return members
end
end

Expand Down Expand Up @@ -106,18 +105,9 @@ local function member_join(message_t)
return
end

if #nodes == 0 then -- Insert
local _, err = dao.nodes:insert({
name = stringy.strip(member.name),
cluster_listening_address = stringy.strip(member.cluster_listening_address)
})
if err then
ngx.log(ngx.ERR, tostring(err))
return
end
elseif #nodes == 1 then -- Update
if #nodes == 1 then -- Update
member_update(message_t)
else
elseif #nodes > 1 then
error("Inconsistency error. More than one node found with name "..member.name)
end

Expand Down