Skip to content

Commit

Permalink
Solve the problem of index creation failure on newer versions of mong…
Browse files Browse the repository at this point in the history
…odb.

Error message: `create_coll_index(): create index [date_1_key_1] for [xxxx] failed! err:cannot write to 'xxxx.system.indexes' , context: ngx.timer`
  • Loading branch information
jie123108 committed Nov 28, 2020
1 parent 1836261 commit b9ae6f2
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 24 deletions.
2 changes: 1 addition & 1 deletion dist.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name=lua-resty-stats
abstract=A statistical module for nginx base on ngx_lua, Statistical key and values are configurable, can use the nginx core's variables and this module's variables. The statistical result store in mongodb.(github.com/jie123108/lua-resty-stats)
version=1.0.2
version=1.0.3
author[email protected]
is_original=yes
license=2bsd
Expand Down
4 changes: 2 additions & 2 deletions docs/stats_simple_demo.conf
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ log_by_lua_block {
local stats = require("resty.stats")
stats.log("stats_uri")
stats.log("stats_host")
}
}
server {
listen 2000;
server_name localhost;

location /stats {
set $template_root /path/to/lua-resty-stats/view;
content_by_lua_file '/path/to/lua-resty-stats/view/main.lua';
Expand Down
2 changes: 1 addition & 1 deletion lib/resty/stats/coll_util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function _M.create_coll_index(mongo_cfg, collection, indexes)
options[k] = v
end
end
ok, err, idx_name = dao:ensure_index(keys,options)
ok, err, idx_name = dao:ensure_index(keys,options, collection)
if ok then
ngx.log(ngx.INFO, "create index [",tostring(idx_name), "] for [", collection, "] success! ")
else
Expand Down
4 changes: 2 additions & 2 deletions lib/resty/stats/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ local function minute() return string.sub(ngx.localtime(), 15,16) end
local function second() return string.sub(ngx.localtime(), 18,19) end

local def_vars = {
now = ngx.time,
now = ngx.time,
date = date,
time = time,
year = year,
Expand Down Expand Up @@ -62,7 +62,7 @@ end

local _M = {}

_M._VERSION = '1.0.2'
_M._VERSION = '1.0.3'

_M.def_mongo_cfg = { host = "127.0.0.1",port = 27017, dbname = "ngx_stats"}

Expand Down
37 changes: 19 additions & 18 deletions lib/resty/stats/mongo_dao.lua
Original file line number Diff line number Diff line change
Expand Up @@ -120,37 +120,38 @@ function _M:upsert(selector, update)
--return self.coll:update(selector, update, 1, 0, 0)
end

-- 参数类型及说明,参见:https://docs.mongodb.org/manual/reference/method/db.collection.createIndex/#db.collection.createIndex
function _M:ensure_index(keys,options, collname)
-- For parameter types and descriptions, reference:https://docs.mongodb.org/manual/reference/method/db.collection.createIndex/#db.collection.createIndex
function _M:ensure_index(keys, options, collname)
local ok, err = self:init()
if not ok then
return false, err
end
options = options or {}
local ns = self.ns
if collname then
ns = self.dbname .. "." .. collname
end
local doc = t_ordered({"ns",ns})

local index = t_ordered({})
local _keys = t_ordered():merge(keys)
doc.key = _keys
doc.name = options.name or t_concat(_keys,'_')

index.key = _keys
index.name = options.name or t_concat(_keys,'_')
for i,v in ipairs({"unique","background", "sparse"}) do
if options[v] ~= nil then
doc[v] = options[v] and true or false
--options[v] = nil
index[v] = options[v] and true or false
end
end

local sys_idx_coll_name = "system.indexes"
local sys_idx_coll = self.db:get_col(sys_idx_coll_name)
local doc = t_ordered()
-- used $cmd: https://www.bookstack.cn/read/mongodb-4.2-manual/662e17e4e7c25f48.md#dbcmd.createIndexes
doc:merge({createIndexes = collname})
doc:merge({indexes = {index}})

local retinfo, errmsg = self.db:cmd(doc)

local n, err = sys_idx_coll:insert({doc},0, true)
self:uninit()
local ok = (n==0)

return ok, err, doc.name
local ok = false
if retinfo then
ok = retinfo.ok == 1
end

return ok, errmsg, index.name
end

return _M

0 comments on commit b9ae6f2

Please sign in to comment.