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

feat(core) upstreams #1735

Merged
merged 81 commits into from
Dec 28, 2016
Merged
Show file tree
Hide file tree
Changes from 71 commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
2b9486d
initial dns commit
Tieske Sep 1, 2016
42c73b0
fixed unit tests after extracting feature/dns2 branch
Tieske Sep 2, 2016
6c3ab98
updated all connections to use local Kong dns resolver, except cli + db
Tieske Sep 2, 2016
67431fd
patch the global tcp.connect function to use the internal dns resolver
Tieske Sep 5, 2016
03693df
Merge pull request #1598 from Mashape/fix/dns2-globalpatch
Tieske Sep 5, 2016
5f60bf0
Merge branch 'next' of https://github.com/Mashape/kong into feature/dns2
Tieske Sep 5, 2016
2927eda
implements the api property `retries`
Tieske Sep 8, 2016
9916ef4
Merge branch 'next' of https://github.com/Mashape/kong into feature/dns2
Tieske Sep 8, 2016
1a4d03b
fix double drop-column statement and added commenst regarding 2 step …
Tieske Sep 12, 2016
ee654dd
fix a remnant from merging `next`. The config file parsing now delive…
Tieske Sep 12, 2016
42277d1
fix linter errors
Tieske Sep 12, 2016
cc1c446
added some debug stuff, failing all over...
Tieske Sep 13, 2016
3fc057f
wtf
Tieske Sep 13, 2016
19ba990
added patches to make the cli work with co-sockets instead of luasocket
Tieske Sep 14, 2016
ddc7230
Merge branch 'next' of https://github.com/Mashape/kong into fix/dns2-…
Tieske Sep 15, 2016
e089725
fix Kong starting errors, due to yield across c-boundary error
Tieske Sep 15, 2016
04d1489
fixes the cassandra tests
Tieske Sep 16, 2016
4771514
Merge pull request #1647 from Mashape/fix/dns2-cli-tcp-override
Tieske Sep 16, 2016
39e646c
cherry picked remains of original `upstreams` branch (after extractin…
Tieske Sep 16, 2016
c5f51e0
update validation and routes tests for upstreams and targets
Tieske Sep 19, 2016
50d1636
implemented caching logic/database access for upstreams and targets
Tieske Sep 19, 2016
b014ab3
added cache-only option
Tieske Sep 21, 2016
3c59cb7
Merge branch 'next' of https://github.com/Mashape/kong into feature/dns2
Tieske Sep 23, 2016
5d38d4c
fix linter error
Tieske Sep 26, 2016
913665d
Merge branch 'feature/dns2' of https://github.com/Mashape/kong into f…
Tieske Sep 26, 2016
b745277
balancer updates
Tieske Sep 28, 2016
f633cac
bugfixes. Initial manual tests are now working for upstreams and the …
Tieske Sep 30, 2016
153a028
removed debug statements
Tieske Oct 11, 2016
f06576b
make keepalive setting configurable
Tieske Oct 12, 2016
6e3e3d7
removed connections setting from shebang
Tieske Oct 12, 2016
85c0c43
replaced `ngx.exit` calls with the proper `kong.tools.responses` equi…
Tieske Oct 12, 2016
f2469c7
Merge branch 'next' of https://github.com/Mashape/kong into feature/dns2
Tieske Oct 12, 2016
4d93ede
updated comments
Tieske Oct 15, 2016
6565d3f
updated dns.lua dependency to release 0.2.0
Tieske Oct 18, 2016
b138dc2
update dns.lua dependency to 0.2.1
Tieske Oct 24, 2016
c085643
Merge branch 'next' of https://github.com/Mashape/kong into feature/dns2
Tieske Oct 24, 2016
ece41c7
Merge branch 'next' of https://github.com/Mashape/kong into feature/dns2
Tieske Oct 27, 2016
773afac
fix merge issues
Tieske Oct 27, 2016
6d9194f
multiple small updates after reviewing and a bug fix
Tieske Oct 27, 2016
e6ebd98
fix typo
Tieske Oct 27, 2016
fa11a9c
fix cassandra-abstraction failing to parse version string correctly
Tieske Oct 27, 2016
4d9921e
fix tests, implemented pure lua semaphore only to be used for testing…
Tieske Oct 28, 2016
2ce14ae
Merge branch 'feature/dns2' of https://github.com/Mashape/kong into f…
Tieske Oct 28, 2016
03f6f36
added some unit tests for core.balancer
Tieske Oct 31, 2016
1750a4e
added cleanup of `targets` table
Tieske Oct 31, 2016
aa2012d
update ldoc settings to allow multi-line parameter comments
Tieske Nov 4, 2016
cefdd66
Merge branch 'next' of https://github.com/Mashape/kong into feature/u…
Tieske Nov 4, 2016
cd44bb7
fix simplify structure with `dns_cache_only`
Tieske Nov 7, 2016
6ae91a5
Merge branch 'next' of https://github.com/Mashape/kong into feature/u…
Tieske Nov 8, 2016
85f42d0
Merge branch 'next' of https://github.com/Mashape/kong into feature/u…
Tieske Nov 8, 2016
6641dde
removed an orphaned file
Tieske Nov 8, 2016
5fd062a
implemented the proper events and hooks for target updates. Also fix …
Tieske Nov 15, 2016
67892b8
Merge branch 'next' of https://github.com/Mashape/kong into feature/u…
Tieske Nov 15, 2016
b4c622d
fix random seeding
Tieske Nov 15, 2016
b5e2c92
Merge branch 'next' of https://github.com/Mashape/kong into feature/u…
Tieske Nov 16, 2016
e3161ce
rewrote the caching for upstreams and targets. The existing one only …
Tieske Nov 17, 2016
bd6eda9
missed an invalidation
Tieske Nov 17, 2016
5ef2106
added test for failed dns resolver upstream
Tieske Nov 18, 2016
5c7c730
fixed upstream resolve failure test
Tieske Nov 18, 2016
c71123a
refactored cache invalidation tests
Tieske Nov 18, 2016
3aced2d
added tests, incomplete, wip
Tieske Nov 18, 2016
8364662
fixed test after change of caching strategy
Tieske Nov 25, 2016
8f75266
implemented the invalidation/hooks integration tests
Tieske Nov 27, 2016
c62aa12
fix final tests and some test helpers
Tieske Nov 29, 2016
603ba29
fix linter errors
Tieske Nov 29, 2016
b2e3fbc
Merge branch 'next' of https://github.com/Mashape/kong into feature/u…
Tieske Dec 1, 2016
8861044
fix merge issues
Tieske Dec 2, 2016
c0434bd
update precision to milliseconds, everything for Cassandra, for Postg…
Tieske Dec 2, 2016
850e1fd
more millisec-timestamp updates, and running tests against both cassa…
Tieske Dec 2, 2016
14525e7
fix Cassandra for targets, accidental left-over 'unique' requirement …
Tieske Dec 2, 2016
cc8d6f4
update to existing dao tester
Tieske Dec 2, 2016
27b87d2
updates based on review comments
Tieske Dec 14, 2016
47c640c
revert style change
Tieske Dec 14, 2016
aa0139d
Merge branch 'next' of https://github.com/Mashape/kong into feature/u…
Tieske Dec 22, 2016
d6a8664
Merge branch 'next' of https://github.com/Mashape/kong into feature/u…
Tieske Dec 22, 2016
0d642fd
Merge branch 'next' of https://github.com/Mashape/kong into feature/u…
Tieske Dec 22, 2016
7363532
fix caching problem
Tieske Dec 23, 2016
32ffda4
Merge branch 'next' into feature/upstreams2
subnetmarco Dec 23, 2016
fa0d43e
Merge branch 'next' of https://github.com/Mashape/kong into feature/u…
Tieske Dec 23, 2016
25ef72b
Merge branch 'feature/upstreams2' of https://github.com/Mashape/kong …
Tieske Dec 23, 2016
4f19d2a
fixed error when there were NO targets, added a test for this case
Tieske Dec 23, 2016
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
2 changes: 1 addition & 1 deletion config.ld
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ dir='doc'
--readme='readme.md'
sort=true
sort_modules=true
not_luadoc=true
--not_luadoc=true
all=false
3 changes: 3 additions & 0 deletions kong-0.9.6-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ build = {
["kong.api.routes.plugins"] = "kong/api/routes/plugins.lua",
["kong.api.routes.cache"] = "kong/api/routes/cache.lua",
["kong.api.routes.cluster"] = "kong/api/routes/cluster.lua",
["kong.api.routes.upstreams"] = "kong/api/routes/upstreams.lua",

["kong.tools.dns"] = "kong/tools/dns.lua",
["kong.tools.utils"] = "kong/tools/utils.lua",
Expand All @@ -101,6 +102,8 @@ build = {
["kong.dao.schemas.nodes"] = "kong/dao/schemas/nodes.lua",
["kong.dao.schemas.consumers"] = "kong/dao/schemas/consumers.lua",
["kong.dao.schemas.plugins"] = "kong/dao/schemas/plugins.lua",
["kong.dao.schemas.upstreams"] = "kong/dao/schemas/upstreams.lua",
["kong.dao.schemas.targets"] = "kong/dao/schemas/targets.lua",
["kong.dao.db"] = "kong/dao/db/init.lua",
["kong.dao.db.cassandra"] = "kong/dao/db/cassandra.lua",
["kong.dao.db.postgres"] = "kong/dao/db/postgres.lua",
Expand Down
18 changes: 18 additions & 0 deletions kong/api/crud_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ function _M.find_consumer_by_username_or_id(self, dao_factory, helpers)
end
end

function _M.find_upstream_by_name_or_id(self, dao_factory, helpers)
local filter_keys = {
[utils.is_valid_uuid(self.params.name_or_id) and "id" or "name"] = self.params.name_or_id
}
self.params.name_or_id = nil

local rows, err = dao_factory.upstreams:find_all(filter_keys)
if err then
return helpers.yield_error(err)
end

-- We know name and id are unique, so if we have a row, it must be the only one
self.upstream = rows[1]
if not self.upstream then
return helpers.responses.send_HTTP_NOT_FOUND()
end
end

function _M.paginated_set(self, dao_collection)
local size = self.params.size and tonumber(self.params.size) or 100
local offset = self.params.offset and ngx.decode_base64(self.params.offset) or nil
Expand Down
2 changes: 1 addition & 1 deletion kong/api/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ end
ngx.log(ngx.DEBUG, "Loading Admin API endpoints")

-- Load core routes
for _, v in ipairs({"kong", "apis", "consumers", "plugins", "cache", "cluster" }) do
for _, v in ipairs({"kong", "apis", "consumers", "plugins", "cache", "cluster", "upstreams" }) do
local routes = require("kong.api.routes."..v)
attach_routes(insert_405(routes))
end
Expand Down
101 changes: 101 additions & 0 deletions kong/api/routes/upstreams.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
local crud = require "kong.api.crud_helpers"

return {
["/upstreams/"] = {
GET = function(self, dao_factory)
crud.paginated_set(self, dao_factory.upstreams)
end,

PUT = function(self, dao_factory)
crud.put(self.params, dao_factory.upstreams)
end,

POST = function(self, dao_factory, helpers)
crud.post(self.params, dao_factory.upstreams)
end
},

["/upstreams/:name_or_id"] = {
before = function(self, dao_factory, helpers)
crud.find_upstream_by_name_or_id(self, dao_factory, helpers)
end,

GET = function(self, dao_factory, helpers)
return helpers.responses.send_HTTP_OK(self.upstream)
end,

PATCH = function(self, dao_factory)
crud.patch(self.params, dao_factory.upstreams, self.upstream)
end,

DELETE = function(self, dao_factory)
crud.delete(self.upstream, dao_factory.upstreams)
end
},

["/upstreams/:name_or_id/targets/"] = {
before = function(self, dao_factory, helpers)
crud.find_upstream_by_name_or_id(self, dao_factory, helpers)
self.params.upstream_id = self.upstream.id
end,

GET = function(self, dao_factory)
crud.paginated_set(self, dao_factory.targets)
end,

POST = function(self, dao_factory, helpers)
local cleanup_factor = 10 -- when to cleanup; invalid-entries > (valid-ones * cleanup_factor)
Copy link
Member

Choose a reason for hiding this comment

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

nitpick: in your comments, it is very difficult to understand what you mean at the first glance at it because you use semi-colons ; instead of colons :.

Semi-colons are meant to separate two independent clauses in a sentence. Colons, on the other side, are used to present an explanation after what could stand as an independent clause. That is, the later should be used when you make a statement, and present the explanation for that statement. Not the former.

I know this can sound like a nitpick but believe me: many times did I found myself re-reading your comments because I did not understand the second clause was the explanation for the first one.


--cleaning up history, check if it's necessary...
local target_history = dao_factory.targets:find_all(
{ upstream_id = self.params.upstream_id })
if target_history then --ignoring errors here, will be caught when posting below
-- sort the targets
for _,target in ipairs(target_history) do
target.order = target.created_at..":"..target.id
end
-- sort table in reverse order
table.sort(target_history, function(a,b) return a.order>b.order end)
-- do clean up
local cleaned = {}
local delete = {}
for _, entry in ipairs(target_history) do
if cleaned[entry.target] then
-- we got a newer entry for this target than this, so this one can go
delete[#delete+1] = entry
else
-- haven't got this one, so this is the last one for this target
cleaned[entry.target] = true
cleaned[#cleaned+1] = entry
if entry.weight == 0 then
Copy link
Member

Choose a reason for hiding this comment

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

question: so setting an upstream's wight to 0 would be another way of - eventually - deleting a target, is that correct?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes. On the balancer object it gets deleted. But because we need individual Kong nodes to retain the same order of slots in the balancer, we must still record setting it to 0. This way we can replay changes on each node and end up with the same balancer config.

delete[#delete+1] = entry
end
end
end
Copy link
Member

Choose a reason for hiding this comment

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

style/nitpick: feel free to use more space to make the code more readable. Line jumps, for example, could be inserted at many places in this snippet. A good practice I have recently found myself to be kind of is inserting a blank lines before elseif and else statements as well. It makes the whole code greatly more readable.

Copy link
Member

Choose a reason for hiding this comment

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

At some point, I believe we should formalize the code-style used in this project.


-- do we need to cleanup?
-- either nothing left, or when 10x more outdated than active entries
if (#cleaned == 0 and #delete > 0) or
(#delete >= (math.max(#cleaned,1)*cleanup_factor)) then
Copy link
Member

Choose a reason for hiding this comment

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

style: could use some spacing as well...

ngx.log(ngx.WARN, "Starting cleanup of target table for upstream "..tostring(self.params.upstream_id))
Copy link
Member

Choose a reason for hiding this comment

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

  • should be logged at the INFO log level.
  • better to respect good practices and use the variadic arguments form of ngx.log: Use , instead of .. to avoid Lua-land string concatenations.
  • avoid +80 chars columns
  • prefix with the component logging this message: [admin API]

local cnt = 0
for _, entry in ipairs(delete) do
-- not sending update events, one event at the end, based on the
-- post of the new entry should suffice to reload only once
dao_factory.targets:delete(
{ id = entry.id },
{ quiet = true }
)
-- ignoring errors here, deleted by id, so should not matter
-- in case another kong-node does the same cleanup simultaneously
cnt = cnt + 1
end
ngx.log(ngx.WARN, "Finished cleanup of target table for upstream "..
Copy link
Member

Choose a reason for hiding this comment

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

ditto:

  • prefix message with component
  • INFO log level
  • avoid Lua-land string concatenations

tostring(self.params.upstream_id).." removed "..tostring(cnt).." target entries")
end
end

crud.post(self.params, dao_factory.targets)
end,
},
}
Loading