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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. rspamd_config:add_periodic(ev_base, 0.0, function(cfg, _)
  112. local function stat_redis_cb(err, data)
  113. lua_util.debugm(N, cfg, 'stat redis cb: %s, %s', err, data)
  114. if err then
  115. logger.warn(cfg, 'cannot get bayes statistics for %s: %s', symbol, err)
  116. else
  117. local new_cursor = data[1]
  118. current_data.users = current_data.users + data[2]
  119. current_data.revision = current_data.revision + data[3]
  120. if new_cursor == 0 then
  121. -- Done iteration
  122. final_data = lua_util.shallowcopy(current_data)
  123. current_data = {
  124. users = 0,
  125. revision = 0,
  126. }
  127. lua_util.debugm(N, cfg, 'final data: %s', final_data)
  128. stat_periodic_cb(cfg, final_data)
  129. end
  130. cursor = new_cursor
  131. end
  132. end
  133. lua_redis.exec_redis_script(stat_script_id,
  134. { ev_base = ev_base, cfg = cfg, is_write = false },
  135. stat_redis_cb, { tostring(cursor),
  136. symbol,
  137. is_spam and "learns_spam" or "learns_ham",
  138. tostring(max_users) })
  139. return statfile_ucl.monitor_timeout or classifier_ucl.monitor_timeout or 30.0
  140. end)
  141. return gen_classify_functor(redis_params, classify_script_id), gen_learn_functor(redis_params, learn_script_id)
  142. end
  143. local function gen_cache_check_functor(redis_params, check_script_id, conf)
  144. local packed_conf = ucl.to_format(conf, 'msgpack')
  145. return function(task, cache_id, callback)
  146. local function classify_redis_cb(err, data)
  147. lua_util.debugm(N, task, 'check cache redis cb: %s, %s (%s)', err, data, type(data))
  148. if err then
  149. callback(task, false, err)
  150. else
  151. if type(data) == 'number' then
  152. callback(task, true, data)
  153. else
  154. callback(task, false, 'not found')
  155. end
  156. end
  157. end
  158. lua_util.debugm(N, task, 'checking cache: %s', cache_id)
  159. lua_redis.exec_redis_script(check_script_id,
  160. { task = task, is_write = false, key = cache_id },
  161. classify_redis_cb, { cache_id, packed_conf })
  162. end
  163. end
  164. local function gen_cache_learn_functor(redis_params, learn_script_id, conf)
  165. local packed_conf = ucl.to_format(conf, 'msgpack')
  166. return function(task, cache_id, is_spam)
  167. local function learn_redis_cb(err, data)
  168. lua_util.debugm(N, task, 'learn_cache redis cb: %s, %s', err, data)
  169. end
  170. lua_util.debugm(N, task, 'try to learn cache: %s', cache_id)
  171. lua_redis.exec_redis_script(learn_script_id,
  172. { task = task, is_write = true, key = cache_id },
  173. learn_redis_cb,
  174. { cache_id, is_spam and "1" or "0", packed_conf })
  175. end
  176. end
  177. exports.lua_bayes_init_cache = function(classifier_ucl, statfile_ucl)
  178. local redis_params = load_redis_params(classifier_ucl, statfile_ucl)
  179. if not redis_params then
  180. return nil
  181. end
  182. local default_conf = {
  183. cache_prefix = "learned_ids",
  184. cache_max_elt = 10000, -- Maximum number of elements in the cache key
  185. cache_max_keys = 5, -- Maximum number of keys in the cache
  186. cache_elt_len = 32, -- Length of the element in the cache (will trim id to that value)
  187. }
  188. local conf = lua_util.override_defaults(default_conf, classifier_ucl)
  189. -- Clean all not known configurations
  190. for k, _ in pairs(conf) do
  191. if default_conf[k] == nil then
  192. conf[k] = nil
  193. end
  194. end
  195. local check_script_id = lua_redis.load_redis_script_from_file("bayes_cache_check.lua", redis_params)
  196. local learn_script_id = lua_redis.load_redis_script_from_file("bayes_cache_learn.lua", redis_params)
  197. return gen_cache_check_functor(redis_params, check_script_id, conf), gen_cache_learn_functor(redis_params,
  198. learn_script_id, conf)
  199. end
  200. return exports