You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lua_bayes_redis.lua 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. --[[
  2. Copyright (c) 2022, Vsevolod Stakhov <vsevolod@rspamd.com>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ]]
  13. -- This file contains functions to support Bayes statistics in Redis
  14. local exports = {}
  15. local lua_redis = require "lua_redis"
  16. local logger = require "rspamd_logger"
  17. local lua_util = require "lua_util"
  18. local ucl = require "ucl"
  19. local N = "bayes"
  20. local function gen_classify_functor(redis_params, classify_script_id)
  21. return function(task, expanded_key, id, is_spam, stat_tokens, callback)
  22. local function classify_redis_cb(err, data)
  23. lua_util.debugm(N, task, 'classify redis cb: %s, %s', err, data)
  24. if err then
  25. callback(task, false, err)
  26. else
  27. callback(task, true, data[1], data[2], data[3], data[4])
  28. end
  29. end
  30. lua_redis.exec_redis_script(classify_script_id,
  31. { task = task, is_write = false, key = expanded_key },
  32. classify_redis_cb, { expanded_key, stat_tokens })
  33. end
  34. end
  35. local function gen_learn_functor(redis_params, learn_script_id)
  36. return function(task, expanded_key, id, is_spam, symbol, is_unlearn, stat_tokens, callback, maybe_text_tokens)
  37. local function learn_redis_cb(err, data)
  38. lua_util.debugm(N, task, 'learn redis cb: %s, %s', err, data)
  39. if err then
  40. callback(task, false, err)
  41. else
  42. callback(task, true)
  43. end
  44. end
  45. if maybe_text_tokens then
  46. lua_redis.exec_redis_script(learn_script_id,
  47. { task = task, is_write = true, key = expanded_key },
  48. learn_redis_cb,
  49. { expanded_key, tostring(is_spam), symbol, tostring(is_unlearn), stat_tokens, maybe_text_tokens })
  50. else
  51. lua_redis.exec_redis_script(learn_script_id,
  52. { task = task, is_write = true, key = expanded_key },
  53. learn_redis_cb, { expanded_key, tostring(is_spam), symbol, tostring(is_unlearn), stat_tokens })
  54. end
  55. end
  56. end
  57. local function load_redis_params(classifier_ucl, statfile_ucl)
  58. local redis_params
  59. -- Try load from statfile options
  60. if statfile_ucl.redis then
  61. redis_params = lua_redis.try_load_redis_servers(statfile_ucl.redis, rspamd_config, true)
  62. end
  63. if not redis_params then
  64. if statfile_ucl then
  65. redis_params = lua_redis.try_load_redis_servers(statfile_ucl, rspamd_config, true)
  66. end
  67. end
  68. -- Try load from classifier config
  69. if not redis_params and classifier_ucl.backend then
  70. redis_params = lua_redis.try_load_redis_servers(classifier_ucl.backend, rspamd_config, true)
  71. end
  72. if not redis_params and classifier_ucl.redis then
  73. redis_params = lua_redis.try_load_redis_servers(classifier_ucl.redis, rspamd_config, true)
  74. end
  75. if not redis_params then
  76. redis_params = lua_redis.try_load_redis_servers(classifier_ucl, rspamd_config, true)
  77. end
  78. -- Try load global options
  79. if not redis_params then
  80. redis_params = lua_redis.try_load_redis_servers(rspamd_config:get_all_opt('redis'), rspamd_config, true)
  81. end
  82. if not redis_params then
  83. logger.err(rspamd_config, "cannot load Redis parameters for the classifier")
  84. return nil
  85. end
  86. return redis_params
  87. end
  88. ---
  89. --- Init bayes classifier
  90. --- @param classifier_ucl ucl of the classifier config
  91. --- @param statfile_ucl ucl of the statfile config
  92. --- @return a pair of (classify_functor, learn_functor) or `nil` in case of error
  93. exports.lua_bayes_init_statfile = function(classifier_ucl, statfile_ucl, symbol, is_spam, ev_base, stat_periodic_cb)
  94. local redis_params = load_redis_params(classifier_ucl, statfile_ucl)
  95. if not redis_params then
  96. return nil
  97. end
  98. local classify_script_id = lua_redis.load_redis_script_from_file("bayes_classify.lua", redis_params)
  99. local learn_script_id = lua_redis.load_redis_script_from_file("bayes_learn.lua", redis_params)
  100. local stat_script_id = lua_redis.load_redis_script_from_file("bayes_stat.lua", redis_params)
  101. local max_users = classifier_ucl.max_users or 1000
  102. local current_data = {
  103. users = 0,
  104. revision = 0,
  105. }
  106. local final_data = {
  107. users = 0,
  108. revision = 0, -- number of learns
  109. }
  110. local cursor = 0
  111. if ev_base then
  112. rspamd_config:add_periodic(ev_base, 0.0, function(cfg, _)
  113. local function stat_redis_cb(err, data)
  114. lua_util.debugm(N, cfg, 'stat redis cb: %s, %s', err, data)
  115. if err then
  116. logger.warn(cfg, 'cannot get bayes statistics for %s: %s', symbol, err)
  117. else
  118. local new_cursor = data[1]
  119. current_data.users = current_data.users + data[2]
  120. current_data.revision = current_data.revision + data[3]
  121. if new_cursor == 0 then
  122. -- Done iteration
  123. final_data = lua_util.shallowcopy(current_data)
  124. current_data = {
  125. users = 0,
  126. revision = 0,
  127. }
  128. lua_util.debugm(N, cfg, 'final data: %s', final_data)
  129. stat_periodic_cb(cfg, final_data)
  130. end
  131. cursor = new_cursor
  132. end
  133. end
  134. lua_redis.exec_redis_script(stat_script_id,
  135. { ev_base = ev_base, cfg = cfg, is_write = false },
  136. stat_redis_cb, { tostring(cursor),
  137. symbol,
  138. is_spam and "learns_spam" or "learns_ham",
  139. tostring(max_users) })
  140. return statfile_ucl.monitor_timeout or classifier_ucl.monitor_timeout or 30.0
  141. end)
  142. end
  143. return gen_classify_functor(redis_params, classify_script_id), gen_learn_functor(redis_params, learn_script_id)
  144. end
  145. local function gen_cache_check_functor(redis_params, check_script_id, conf)
  146. local packed_conf = ucl.to_format(conf, 'msgpack')
  147. return function(task, cache_id, callback)
  148. local function classify_redis_cb(err, data)
  149. lua_util.debugm(N, task, 'check cache redis cb: %s, %s (%s)', err, data, type(data))
  150. if err then
  151. callback(task, false, err)
  152. else
  153. if type(data) == 'number' then
  154. callback(task, true, data)
  155. else
  156. callback(task, false, 'not found')
  157. end
  158. end
  159. end
  160. lua_util.debugm(N, task, 'checking cache: %s', cache_id)
  161. lua_redis.exec_redis_script(check_script_id,
  162. { task = task, is_write = false, key = cache_id },
  163. classify_redis_cb, { cache_id, packed_conf })
  164. end
  165. end
  166. local function gen_cache_learn_functor(redis_params, learn_script_id, conf)
  167. local packed_conf = ucl.to_format(conf, 'msgpack')
  168. return function(task, cache_id, is_spam)
  169. local function learn_redis_cb(err, data)
  170. lua_util.debugm(N, task, 'learn_cache redis cb: %s, %s', err, data)
  171. end
  172. lua_util.debugm(N, task, 'try to learn cache: %s', cache_id)
  173. lua_redis.exec_redis_script(learn_script_id,
  174. { task = task, is_write = true, key = cache_id },
  175. learn_redis_cb,
  176. { cache_id, is_spam and "1" or "0", packed_conf })
  177. end
  178. end
  179. exports.lua_bayes_init_cache = function(classifier_ucl, statfile_ucl)
  180. local redis_params = load_redis_params(classifier_ucl, statfile_ucl)
  181. if not redis_params then
  182. return nil
  183. end
  184. local default_conf = {
  185. cache_prefix = "learned_ids",
  186. cache_max_elt = 10000, -- Maximum number of elements in the cache key
  187. cache_max_keys = 5, -- Maximum number of keys in the cache
  188. cache_elt_len = 32, -- Length of the element in the cache (will trim id to that value)
  189. }
  190. local conf = lua_util.override_defaults(default_conf, classifier_ucl)
  191. -- Clean all not known configurations
  192. for k, _ in pairs(conf) do
  193. if default_conf[k] == nil then
  194. conf[k] = nil
  195. end
  196. end
  197. local check_script_id = lua_redis.load_redis_script_from_file("bayes_cache_check.lua", redis_params)
  198. local learn_script_id = lua_redis.load_redis_script_from_file("bayes_cache_learn.lua", redis_params)
  199. return gen_cache_check_functor(redis_params, check_script_id, conf), gen_cache_learn_functor(redis_params,
  200. learn_script_id, conf)
  201. end
  202. return exports