Software /
code /
prosody
Comparison
plugins/mod_storage_sql.lua @ 10993:b5e7f4d533e2
mod_storage_sql: Measure hits/misses on the item count cache
A cache miss can be expensive so having numbers on how often this occurs
may be valuable.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 02 Jul 2020 19:03:59 +0200 |
parent | 10925:73e95ecec733 |
child | 11271:41a962b72a6e |
comparison
equal
deleted
inserted
replaced
10992:df3ee12acd8c | 10993:b5e7f4d533e2 |
---|---|
151 --- Archive store API | 151 --- Archive store API |
152 | 152 |
153 local archive_item_limit = module:get_option_number("storage_archive_item_limit"); | 153 local archive_item_limit = module:get_option_number("storage_archive_item_limit"); |
154 local archive_item_count_cache = cache.new(module:get_option("storage_archive_item_limit_cache_size", 1000)); | 154 local archive_item_count_cache = cache.new(module:get_option("storage_archive_item_limit_cache_size", 1000)); |
155 | 155 |
156 local item_count_cache_hit = module:measure("item_count_cache_hit", "rate"); | |
157 local item_count_cache_miss = module:measure("item_count_cache_miss", "rate") | |
158 | |
156 -- luacheck: ignore 512 431/user 431/store 431/err | 159 -- luacheck: ignore 512 431/user 431/store 431/err |
157 local map_store = {}; | 160 local map_store = {}; |
158 map_store.__index = map_store; | 161 map_store.__index = map_store; |
159 map_store.remove = {}; | 162 map_store.remove = {}; |
160 function map_store:get(username, key) | 163 function map_store:get(username, key) |
284 function archive_store:append(username, key, value, when, with) | 287 function archive_store:append(username, key, value, when, with) |
285 local user,store = username,self.store; | 288 local user,store = username,self.store; |
286 local cache_key = jid_join(username, host, store); | 289 local cache_key = jid_join(username, host, store); |
287 local item_count = archive_item_count_cache:get(cache_key); | 290 local item_count = archive_item_count_cache:get(cache_key); |
288 if not item_count then | 291 if not item_count then |
292 item_count_cache_miss(); | |
289 local ok, ret = engine:transaction(function() | 293 local ok, ret = engine:transaction(function() |
290 local count_sql = [[ | 294 local count_sql = [[ |
291 SELECT COUNT(*) FROM "prosodyarchive" | 295 SELECT COUNT(*) FROM "prosodyarchive" |
292 WHERE "host"=? AND "user"=? AND "store"=?; | 296 WHERE "host"=? AND "user"=? AND "store"=?; |
293 ]]; | 297 ]]; |
301 if not ok or not item_count then | 305 if not ok or not item_count then |
302 module:log("error", "Failed while checking quota for %s: %s", username, ret); | 306 module:log("error", "Failed while checking quota for %s: %s", username, ret); |
303 return nil, "Failure while checking quota"; | 307 return nil, "Failure while checking quota"; |
304 end | 308 end |
305 archive_item_count_cache:set(cache_key, item_count); | 309 archive_item_count_cache:set(cache_key, item_count); |
310 else | |
311 item_count_cache_hit(); | |
306 end | 312 end |
307 | 313 |
308 if archive_item_limit then | 314 if archive_item_limit then |
309 module:log("debug", "%s has %d items out of %d limit", username, item_count, archive_item_limit); | 315 module:log("debug", "%s has %d items out of %d limit", username, item_count, archive_item_limit); |
310 if item_count >= archive_item_limit then | 316 if item_count >= archive_item_limit then |
406 function archive_store:find(username, query) | 412 function archive_store:find(username, query) |
407 query = query or {}; | 413 query = query or {}; |
408 local user,store = username,self.store; | 414 local user,store = username,self.store; |
409 local cache_key = jid_join(username, host, self.store); | 415 local cache_key = jid_join(username, host, self.store); |
410 local total = archive_item_count_cache:get(cache_key); | 416 local total = archive_item_count_cache:get(cache_key); |
417 (total and item_count_cache_hit or item_count_cache_miss)(); | |
411 if total ~= nil and query.limit == 0 and query.start == nil and query.with == nil and query["end"] == nil and query.key == nil then | 418 if total ~= nil and query.limit == 0 and query.start == nil and query.with == nil and query["end"] == nil and query.key == nil then |
412 return noop, total; | 419 return noop, total; |
413 end | 420 end |
414 local ok, result, err = engine:transaction(function() | 421 local ok, result, err = engine:transaction(function() |
415 local sql_query = [[ | 422 local sql_query = [[ |