From 9937099b1dfa3562a367edd482d158c274a0e225 Mon Sep 17 00:00:00 2001 From: Thibault Charbonnier Date: Fri, 18 Dec 2015 14:38:14 -0800 Subject: [PATCH] fix(dao) do not spawn cluster when outside of ngx_lua - Cluster is only spawned when inside if ngx_lua (when starting Kong). - Ensure LuaSocket is used by the driver by changing the return value of `ngx_stub.get_phase()` to `init`. - Ensure lua-cassandra does not output logs when in the `init` phase (which is the case because of change #2) and inside of the specs. Which is why `ngx.stub` is a new property. --- kong/dao/cassandra/factory.lua | 12 +++++++----- kong/kong.lua | 2 +- kong/tools/dao_loader.lua | 4 ++-- kong/tools/ngx_stub.lua | 3 ++- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/kong/dao/cassandra/factory.lua b/kong/dao/cassandra/factory.lua index 66cc83b7a90d..65ea48e5328c 100644 --- a/kong/dao/cassandra/factory.lua +++ b/kong/dao/cassandra/factory.lua @@ -11,7 +11,7 @@ local stringy = require "stringy" local Object = require "classic" local utils = require "kong.tools.utils" -if ngx ~= nil and type(ngx.get_phase) == "function" and ngx.get_phase() == "init" then +if ngx ~= nil and type(ngx.get_phase) == "function" and ngx.get_phase() == "init" and not ngx.stub then cassandra.set_log_level("INFO") else cassandra.set_log_level("QUIET") @@ -30,14 +30,16 @@ end -- Instantiate a Cassandra Factory and all its DAOs for various entities -- @param `properties` Cassandra properties -function CassandraFactory:new(properties, plugins) +function CassandraFactory:new(properties, plugins, spawn_cluster) self.properties = properties self.type = "cassandra" self.daos = {} - local ok, err = cassandra.spawn_cluster(self:get_session_options()) - if not ok then - error(err) + if spawn_cluster then + local ok, err = cassandra.spawn_cluster(self:get_session_options()) + if not ok then + error(err) + end end -- Load core entities DAOs diff --git a/kong/kong.lua b/kong/kong.lua index f95462192f77..7bcb0c4af7d9 100644 --- a/kong/kong.lua +++ b/kong/kong.lua @@ -107,7 +107,7 @@ local Kong = {} -- it will be thrown and needs to be catched in `init_by_lua`. function Kong.init() configuration = config_loader.load(os.getenv("KONG_CONF")) - dao = dao_loader.load(configuration) + dao = dao_loader.load(configuration, true) loaded_plugins = load_node_plugins(configuration) process_id = utils.random_string() ngx.update_time() diff --git a/kong/tools/dao_loader.lua b/kong/tools/dao_loader.lua index 30cb18979540..ea576ac8a926 100644 --- a/kong/tools/dao_loader.lua +++ b/kong/tools/dao_loader.lua @@ -1,8 +1,8 @@ local _M = {} -function _M.load(config) +function _M.load(config, spawn_cluster) local DaoFactory = require("kong.dao."..config.database..".factory") - return DaoFactory(config.dao_config, config.plugins_available) + return DaoFactory(config.dao_config, config.plugins_available, spawn_cluster) end return _M diff --git a/kong/tools/ngx_stub.lua b/kong/tools/ngx_stub.lua index ba67851af997..3e2141e05ba8 100644 --- a/kong/tools/ngx_stub.lua +++ b/kong/tools/ngx_stub.lua @@ -103,10 +103,11 @@ local shared_mt = { } _G.ngx = { + stub = true, req = {}, ctx = {}, header = {}, - get_phase = function() return "not_ngx_lua" end, + get_phase = function() return "init" end, exit = function() end, say = function() end, log = function() end,