Skip to content

Commit

Permalink
feat(db) implement lua-cassandra 1.0.0 support
Browse files Browse the repository at this point in the history
This makes the DAO functional with lua-cassandra 1.0.0. Only Cassandra
2.x is still supported (more updates required for 3.x).

We also retrieve the cluster release_version to avoid nodes with
different major versions, and thus prevent migration errors (due to
different queries being performed for different major versions)

* move DB implementations to `kong.dao.db`
* updated rockspec tests to properly evaluate `init` modules (typical
Lua idiom)
* Get rid of classic.lua and use our own DB implementation helper
module, it's pretty cool
* instanciating a DAO Factory now returns `factory, err`
* instanciating a Cassandra DB in the CLI triggers a cluster refresh
(testing the connection and retrieving cluster info at the same time)
* as long as openresty/resty-cli#12 is not
included in an OpenResty release, this implements a mocked
`ngx.shared.DICT` object in `kong.globalpatches`.
  • Loading branch information
thibaultcha committed Oct 17, 2016
1 parent 38752f0 commit 18ebfa0
Show file tree
Hide file tree
Showing 29 changed files with 502 additions and 267 deletions.
2 changes: 2 additions & 0 deletions bin/busted
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ require("kong.core.globalpatches")({
rbusted = true
})

package.path = "?/init.lua;"..package.path

-- Busted command-line runner
require 'busted.runner'({ standalone = false })
2 changes: 2 additions & 0 deletions bin/kong
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ require("kong.core.globalpatches")({
cli = true
})

package.path = "?/init.lua;"..package.path

require("kong.cmd.init")(arg)
12 changes: 6 additions & 6 deletions kong-0.9.3-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dependencies = {
"multipart == 0.4",
"version == 0.2",
"lapis == 1.5.1",
"lua-cassandra == 0.5.4",
"lua-cassandra == 1.0.0",
"pgmoon-mashape == 2.0.1",
"luatz == 0.3",
"lua_system_constants == 0.1.1",
Expand All @@ -45,8 +45,8 @@ build = {

["kong.vendor.classic"] = "kong/vendor/classic.lua",

["kong.cmd"] = "kong/cmd/init.lua",
["kong.cmd.roar"] = "kong/cmd/roar.lua",
["kong.cmd.init"] = "kong/cmd/init.lua",
["kong.cmd.stop"] = "kong/cmd/stop.lua",
["kong.cmd.quit"] = "kong/cmd/quit.lua",
["kong.cmd.start"] = "kong/cmd/start.lua",
Expand All @@ -65,7 +65,7 @@ build = {
["kong.cmd.utils.prefix_handler"] = "kong/cmd/utils/prefix_handler.lua",
["kong.cmd.utils.dnsmasq_signals"] = "kong/cmd/utils/dnsmasq_signals.lua",

["kong.api.init"] = "kong/api/init.lua",
["kong.api"] = "kong/api/init.lua",
["kong.api.api_helpers"] = "kong/api/api_helpers.lua",
["kong.api.crud_helpers"] = "kong/api/crud_helpers.lua",
["kong.api.routes.kong"] = "kong/api/routes/kong.lua",
Expand Down Expand Up @@ -98,9 +98,9 @@ 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.base_db"] = "kong/dao/base_db.lua",
["kong.dao.cassandra_db"] = "kong/dao/cassandra_db.lua",
["kong.dao.postgres_db"] = "kong/dao/postgres_db.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",
["kong.dao.dao"] = "kong/dao/dao.lua",
["kong.dao.factory"] = "kong/dao/factory.lua",
["kong.dao.model_factory"] = "kong/dao/model_factory.lua",
Expand Down
4 changes: 2 additions & 2 deletions kong/cmd/cluster.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ local conf_loader = require "kong.conf_loader"
local function execute(args)
if args.command == "keygen" then
local conf = assert(conf_loader(args.conf))
local dao = DAOFactory(conf)
local dao = assert(DAOFactory(conf))
local serf = Serf.new(conf, dao)
print(assert(serf:keygen()))
return
Expand All @@ -21,7 +21,7 @@ local function execute(args)
assert(pl_path.exists(default_conf.prefix),
"no such prefix: "..default_conf.prefix)
local conf = assert(conf_loader(default_conf.kong_conf))
local dao = DAOFactory(conf)
local dao = assert(DAOFactory.new(conf))
local serf = Serf.new(conf, dao)

if args.command == "members" then
Expand Down
2 changes: 1 addition & 1 deletion kong/cmd/migrations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ end

local function execute(args)
local conf = assert(conf_loader(args.conf))
local dao = DAOFactory(conf, conf.plugins)
local dao = assert(DAOFactory.new(conf, conf.plugins))

if args.command == "up" then
assert(dao:run_migrations())
Expand Down
3 changes: 2 additions & 1 deletion kong/cmd/quit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ local function execute(args)
assert(nginx_signals.stop(conf))
end

assert(serf_signals.stop(conf, DAOFactory(conf)))
local dao = assert(DAOFactory.new(conf))
assert(serf_signals.stop(conf, dao))

if conf.dnsmasq then
assert(dnsmasq_signals.stop(conf))
Expand Down
4 changes: 3 additions & 1 deletion kong/cmd/reload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ local function execute(args)
if conf.dnsmasq then
assert(dnsmasq_signals.start(conf))
end
assert(serf_signals.start(conf, DAOFactory(conf)))

local dao = assert(DAOFactory.new(conf))
assert(serf_signals.start(conf, dao))
assert(nginx_signals.reload(conf))
log("Kong reloaded")
end
Expand Down
2 changes: 1 addition & 1 deletion kong/cmd/start.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ local function execute(args)
assert(not kill.is_running(conf.nginx_pid),
"Kong is already running in "..conf.prefix)

local dao = DAOFactory(conf)
local err
local dao = assert(DAOFactory.new(conf))
xpcall(function()
assert(prefix_handler.prepare_prefix(conf, args.nginx_conf))
assert(dao:run_migrations())
Expand Down
3 changes: 2 additions & 1 deletion kong/cmd/stop.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ local function execute(args)

-- load <PREFIX>/kong.conf containing running node's config
local conf = assert(conf_loader(default_conf.kong_conf))
local dao = assert(DAOFactory.new(conf))
assert(nginx_signals.stop(conf))
assert(serf_signals.stop(conf, DAOFactory(conf)))
assert(serf_signals.stop(conf, dao))
if conf.dnsmasq then
assert(dnsmasq_signals.stop(conf))
end
Expand Down
108 changes: 107 additions & 1 deletion kong/core/globalpatches.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,113 @@ return function(options)
local socket = require(namespace .. ".socket")
socket.force_luasocket(ngx.get_phase(), true)
end

do
-- ngx.shared.DICT proxy
-- https://github.com/bsm/fakengx/blob/master/fakengx.lua
-- with minor fixes and addtions such as exptime
--
-- See https://github.com/openresty/resty-cli/pull/12
-- for a definitive solution ot using shms in CLI
local SharedDict = {}
local function set(data, key, value)
data[key] = {
value = value,
info = {expired = false}
}
end
function SharedDict:new()
return setmetatable({data = {}}, {__index = self})
end
function SharedDict:get(key)
return self.data[key] and self.data[key].value, nil
end
function SharedDict:set(key, value)
set(self.data, key, value)
return true, nil, false
end
SharedDict.safe_set = SharedDict.set
function SharedDict:add(key, value, exptime)
if self.data[key] ~= nil then
return false, "exists", false
end

if exptime then
ngx.timer.at(exptime, function()
self.data[key] = nil
end)
end

set(self.data, key, value)
return true, nil, false
end
function SharedDict:replace(key, value)
if self.data[key] == nil then
return false, "not found", false
end
set(self.data, key, value)
return true, nil, false
end
function SharedDict:delete(key)
self.data[key] = nil
return true
end
function SharedDict:incr(key, value)
if not self.data[key] then
return nil, "not found"
elseif type(self.data[key].value) ~= "number" then
return nil, "not a number"
end
self.data[key].value = self.data[key].value + value
return self.data[key].value, nil
end
function SharedDict:flush_all()
for _, item in pairs(self.data) do
item.info.expired = true
end
end
function SharedDict:flush_expired(n)
local data = self.data
local flushed = 0

for key, item in pairs(self.data) do
if item.info.expired then
data[key] = nil
flushed = flushed + 1
if n and flushed == n then
break
end
end
end
self.data = data
return flushed
end
function SharedDict:get_keys(n)
n = n or 1024
local i = 0
local keys = {}
for k in pairs(self.data) do
keys[#keys+1] = k
i = i + 1
if n ~= 0 and i == n then
break
end
end
return keys
end

-- hack
_G.ngx.shared = setmetatable({}, {
__index = function(self, key)
local shm = rawget(self, key)
if not shm then
shm = SharedDict:new()
rawset(self, key, SharedDict:new())
end
return shm
end
})
end
end

if options.rbusted then
Expand Down Expand Up @@ -93,6 +200,5 @@ return function(options)
return seed
end
end

end
end
48 changes: 0 additions & 48 deletions kong/dao/base_db.lua

This file was deleted.

Loading

0 comments on commit 18ebfa0

Please sign in to comment.