aboutsummaryrefslogtreecommitdiffstats
path: root/openresty-ext/src/assembly/resources/openresty/nginx/luaext/dao/dao.lua
blob: 2adc3fcae6bf31021c59e47329fbe121ffd7a7df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
--[[

    Copyright (C) 2017-2018 ZTE, Inc. and others. All rights reserved. (ZTE)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

--]]

--This module integrates the shcache with an abstraction for various databases(redis and so on)
local _M = {}
_M._VERSION = '1.0.0'

local shcache = require("vendor.shcache")
local cjson_safe =  require "cjson.safe"
local msbConf = require('conf.msbinit')

local DB      = require('dao.redis_db')
local options = msbConf.redisConf

local cacheConf = msbConf.cacheConf
local positive_ttl = cacheConf.positive_ttl or 60
local negative_ttl = cacheConf.negative_ttl or 2
local actualize_ttl = cacheConf.actualize_ttl or 120
local stats_prefix = "monitor:stats:"

local svc_shcache = ngx.shared.svc_cache

local function load_serviceinfo(key)

	-- closure to perform external lookup to redis
	local fetch_svcinfo_from_db = function ()
		local _db = DB:new(options)
		return _db:getserviceinfo(key)
	end

	local svcinfo_cache_table = shcache:new(
		svc_shcache,
		{ external_lookup = fetch_svcinfo_from_db,
			--encode = cmsgpack.pack,
			--encode = cjson_safe.encode,
			--decode = cmsgpack.unpack
			--decode = cjson_safe.decode
		},
		{   positive_ttl = positive_ttl,           -- default cache good data for 60s 
			negative_ttl = negative_ttl,           -- default cache failed lookup for 5s
			actualize_ttl = actualize_ttl,
			name = 'svcinfo_cache'       -- "named" cache, useful for debug / report
		}
	)

	local serviceinfo, from_cache = svcinfo_cache_table:load(key)

	return serviceinfo
end
_M.load_serviceinfo = load_serviceinfo

local function load_backservers(keypattern)

	-- closure to perform external lookup to redis
	local fetch_servers_from_db = function ()
		local _db = DB:new(options)
		return _db:getbackservers(keypattern)
	end

	local servers_cache_table = shcache:new(
		svc_shcache,
		{ external_lookup = fetch_servers_from_db,
			--encode = cmsgpack.pack,
			encode = cjson_safe.encode,
			--decode = cmsgpack.unpack
			decode = cjson_safe.decode
		},
		{   positive_ttl = positive_ttl,           -- default cache good data for 60s 
			negative_ttl = negative_ttl,           -- default cache failed lookup for 5s
			name = 'servers_cache'           -- "named" cache, useful for debug / report
		}
	)

	local servers_table, from_cache = servers_cache_table:load(keypattern)

	return servers_table
end
_M.load_backservers = load_backservers


local function load_customsvcnames(keypattern)
	-- closure to perform external lookup to redis
	local fetch_svcnames_from_db = function ()
		local _db = DB:new(options)
		return _db:getcustomsvcnames(keypattern)
	end

	local svcnames_cache_table = shcache:new(
		svc_shcache,
		{ external_lookup = fetch_svcnames_from_db,
			--encode = cmsgpack.pack,
			encode = cjson_safe.encode,
			--decode = cmsgpack.unpack
			decode = cjson_safe.decode
		},
		{   positive_ttl = positive_ttl,           -- default cache good data for 60s 
			negative_ttl = negative_ttl,           -- default cache failed lookup for 5s
			actualize_ttl = actualize_ttl,
			name = 'svcnames_cache'      -- "named" cache, useful for debug / report
		}
	)

	local service_names, from_cache = svcnames_cache_table:load(keypattern)

	return service_names
end
_M.load_customsvcnames = load_customsvcnames


function _M.save_reqnum_stats(date,info)
	local _db = DB:new(options)
	local c, err = _db:connectdb()
	if not c then
		return nil, err
	end

	local red   = _db.redis
	local resp,err =  red:rpush(stats_prefix..date,info)
	_db:keepalivedb()
	if not resp then
		return nil, "save_reqnum_stats failed! key:"..date.." value:"..info
	else
		return resp,nil
	end
end

function _M.delete_reqnum_stats(date)
	local _db = DB:new(options)
	local c, err = _db:connectdb()
	if not c then
		return nil, err
	end
	local red   = _db.redis
	red:del(stats_prefix..date)
	_db:keepalivedb()
end

function _M.get_reqnum_stats(date,latest_num)
	if(type(latest_num)~="number") then
		return nil,"the input num is illegal!"
	end
	local _db = DB:new(options)
	local c, err = _db:connectdb()
	if not c then
		return nil, err
	end
	local red   = _db.redis
	local resp,err =  red:lrange(stats_prefix..date,0-latest_num,-1)
	_db:keepalivedb()
	return resp,nil
end

return _M