From 81bc782c84a2f7c6b27c041c63c06baa9d466195 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Wed, 25 Nov 2026 20:20:30 +0300 Subject: [PATCH 1/2] mon: Never decrease total op counters on OSD restart or master change vitastor_stat_count/usec/bytes are exported as Prometheus counters, but they were a sum of absolute OSD counters. The sum went down when an OSD was down or restarted (its counters start from zero), and a new monitor master published an empty and then a partial sum until every OSD had reported twice. rate() takes every such drop for a counter reset and shows a spike of the whole accumulated value. Sum the growth of OSD counters instead, starting from the totals saved in /stats, so that a new master continues them. Operations done between the old master's last update and the new master's first look at an OSD are not counted. --- mon/mon.js | 2 +- mon/stats.js | 49 +++++++++++++++++++++++++++++++++++++++++++++-- mon/test-stats.js | 45 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 mon/test-stats.js diff --git a/mon/mon.js b/mon/mon.js index f8cd907b..98c131f9 100644 --- a/mon/mon.js +++ b/mon/mon.js @@ -758,7 +758,7 @@ class Mon { const txn = []; const { object_counts, object_bytes, backfillfull_pools, backfillfull_osds } = sum_object_counts(this.state, this.config); - let stats = sum_op_stats(this.state.osd, this.prev_stats); + let stats = sum_op_stats(this.state.osd, this.prev_stats, this.state.stats); let { inode_stats, seen_pools } = sum_inode_stats(this.state, this.prev_stats); stats.object_counts = object_counts; stats.object_bytes = object_bytes; diff --git a/mon/stats.js b/mon/stats.js index 9d516bd7..9a53e75a 100644 --- a/mon/stats.js +++ b/mon/stats.js @@ -61,12 +61,50 @@ function derive_osd_stats(st, prev, prev_diff) return diff; } -// sum_op_stats(this.state.osd, this.prev_stats) -function sum_op_stats(all_osd, prev_stats) +const counter_keys = { op_stats: [ 'count', 'usec', 'bytes' ], subop_stats: [ 'count', 'usec' ], recovery_stats: [ 'count', 'bytes' ] }; + +// Add the growth of an OSD counter since to +function add_growth(total, cur, prev, keys) { + for (const k of keys) + { + const c = BigInt(cur[k]||0), p = BigInt(prev && prev[k]||0); + // A counter that went down means that the OSD restarted + total[k] = (total[k]||0n) + (c >= p ? c-p : c); + } +} + +// sum_op_stats(this.state.osd, this.prev_stats, this.state.stats) +function sum_op_stats(all_osd, prev_stats, saved_stats) +{ + // Total counters are exported as Prometheus counters, so they must never go down. + // OSD counters reset on restart, down OSDs and OSDs not yet seen by a new master + // are missing, so sum the growth of OSD counters starting from the saved totals + if (!prev_stats.totals) + { + prev_stats.totals = {}; + for (const type in counter_keys) + { + prev_stats.totals[type] = {}; + for (const op in (saved_stats||{})[type]||{}) + { + prev_stats.totals[type][op] = {}; + add_growth(prev_stats.totals[type][op], saved_stats[type][op], null, counter_keys[type]); + } + } + } for (const osd in all_osd.stats) { const cur = { ...all_osd.stats[osd], inode_stats: all_osd.inodestats[osd]||{} }; + const prev = prev_stats.osd_stats[osd]; + for (const type in counter_keys) + { + for (const op in prev && cur[type] || {}) + { + prev_stats.totals[type][op] = prev_stats.totals[type][op] || {}; + add_growth(prev_stats.totals[type][op], cur[type][op], prev[type] && prev[type][op], counter_keys[type]); + } + } prev_stats.osd_diff[osd] = derive_osd_stats( cur, prev_stats.osd_stats[osd], prev_stats.osd_diff[osd] ); @@ -107,6 +145,13 @@ function sum_op_stats(all_osd, prev_stats) } } } + for (const type in prev_stats.totals) + { + for (const op in prev_stats.totals[type]) + { + sum_diff[type][op] = { ...sum_diff[type][op], ...prev_stats.totals[type][op] }; + } + } return sum_diff; } diff --git a/mon/test-stats.js b/mon/test-stats.js new file mode 100644 index 00000000..a96c897c --- /dev/null +++ b/mon/test-stats.js @@ -0,0 +1,45 @@ +// Copyright (c) Vitaliy Filippov, 2019+ +// License: VNPL-1.1 (see README.md for details) + +// Total op counters must never go down: on OSD down, OSD restart and monitor master change +// Run: node test-stats.js + +const assert = require('assert'); +const { sum_op_stats, serialize_bigints } = require('./stats.js'); + +const state = { + osd: { state: { 1: { state: 'up' }, 2: { state: 'up' } }, stats: {}, inodestats: {} }, + stats: {}, +}; +let prev_stats = { osd_stats: {}, osd_diff: {} }; + +let last = [ 0n, 0n, 0n ]; + +function step(time, counts, growth) +{ + for (const osd in counts) + { + state.osd.stats[osd] = { time, op_stats: { primary_write: { count: counts[osd], usec: counts[osd]*10, bytes: counts[osd]*4096 } } }; + } + // Round-trip through JSON like /stats in etcd + state.stats = JSON.parse(JSON.stringify(serialize_bigints(sum_op_stats(state.osd, prev_stats, state.stats)))); + const st = state.stats.op_stats.primary_write || {}; + const cur = [ BigInt(st.count||0), BigInt(st.usec||0), BigInt(st.bytes||0) ]; + if (growth !== null) + { + assert.deepStrictEqual(cur.map((c, i) => c-last[i]), [ BigInt(growth), BigInt(growth*10), BigInt(growth*4096) ]); + } + last = cur; +} + +step(1, { 1: 1000, 2: 2000 }, null); // OSDs are seen for the first time +step(2, { 1: 1100, 2: 2100 }, null); +delete state.osd.state[2]; // OSD 2 is down, its last stats stay in etcd +step(3, { 1: 1200 }, 100); +state.osd.state[2] = { state: 'up' }; // OSD 2 restarted, its counters start from zero +step(4, { 1: 1300, 2: 50 }, 150); +prev_stats = { osd_stats: {}, osd_diff: {} }; // Another monitor becomes the master +step(4, {}, 0); +step(5, { 1: 1400, 2: 150 }, 200); + +console.log('OK'); -- 2.47.3 From d3b65be392e299df4da9e7d6966e71fa5385e0cc Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Wed, 25 Nov 2026 20:22:02 +0300 Subject: [PATCH 2/2] mon: Never decrease per-inode op counters on OSD restart or master change vitastor_image_stat_count/usec/bytes were a sum of absolute OSD inode counters over up OSDs, so they went down when an OSD was down or restarted, and rate() showed a false spike on image and pool graphs. Sum the growth of OSD inode counters instead, like the total op counters, starting from /inode/stats saved by the previous master. Forget the totals of an inode together with its stats when it is deleted. --- mon/stats.js | 56 ++++++++++++++++++++++++++++++++++++----------- mon/test-stats.js | 27 +++++++++++++++-------- 2 files changed, 61 insertions(+), 22 deletions(-) diff --git a/mon/stats.js b/mon/stats.js index 9a53e75a..694e279a 100644 --- a/mon/stats.js +++ b/mon/stats.js @@ -291,28 +291,57 @@ function sum_inode_stats(state, prev_stats) const used = state.pool.stats[pool_id].used_raw_tb; state.pool.stats[pool_id].used_raw_tb = Number(used)/1024/1024/1024/1024; } - for (const osd_num in state.osd.state) + // Per-inode counters must never go down too, see sum_op_stats() + if (!prev_stats.inode_totals) { - const ist = state.osd.inodestats[osd_num]; - if (!ist || !state.osd.state[osd_num]) + prev_stats.inode_totals = {}; + prev_stats.osd_inodestats = {}; + for (const pool_id in state.inode.stats) { - continue; - } - for (const pool_id in ist) - { - inode_stats[pool_id] = inode_stats[pool_id] || {}; - for (const inode_num in ist[pool_id]) + prev_stats.inode_totals[pool_id] = {}; + for (const inode_num in state.inode.stats[pool_id]) { - inode_stats[pool_id][inode_num] = inode_stats[pool_id][inode_num] || inode_stub(); + const t = prev_stats.inode_totals[pool_id][inode_num] = {}; for (const op of [ 'read', 'write', 'delete' ]) { - inode_stats[pool_id][inode_num][op].count += BigInt(ist[pool_id][inode_num][op].count||0); - inode_stats[pool_id][inode_num][op].usec += BigInt(ist[pool_id][inode_num][op].usec||0); - inode_stats[pool_id][inode_num][op].bytes += BigInt(ist[pool_id][inode_num][op].bytes||0); + t[op] = {}; + add_growth(t[op], state.inode.stats[pool_id][inode_num][op]||{}, null, counter_keys.op_stats); } } } } + for (const osd_num in state.osd.inodestats) + { + const ist = state.osd.inodestats[osd_num]||{}; + const prev = prev_stats.osd_inodestats[osd_num]; + prev_stats.osd_inodestats[osd_num] = ist; + for (const pool_id in prev && ist || {}) + { + prev_stats.inode_totals[pool_id] = prev_stats.inode_totals[pool_id] || {}; + for (const inode_num in ist[pool_id]) + { + const t = prev_stats.inode_totals[pool_id][inode_num] = + prev_stats.inode_totals[pool_id][inode_num] || { read: {}, write: {}, delete: {} }; + const pr = prev[pool_id] && prev[pool_id][inode_num]; + for (const op of [ 'read', 'write', 'delete' ]) + { + add_growth(t[op], ist[pool_id][inode_num][op]||{}, pr && pr[op], counter_keys.op_stats); + } + } + } + } + for (const pool_id in prev_stats.inode_totals) + { + inode_stats[pool_id] = inode_stats[pool_id] || {}; + for (const inode_num in prev_stats.inode_totals[pool_id]) + { + inode_stats[pool_id][inode_num] = inode_stats[pool_id][inode_num] || inode_stub(); + for (const op of [ 'read', 'write', 'delete' ]) + { + Object.assign(inode_stats[pool_id][inode_num][op], prev_stats.inode_totals[pool_id][inode_num][op]); + } + } + } for (const osd in state.osd.state) { const osd_diff = prev_stats.osd_diff[osd]; @@ -357,6 +386,7 @@ function sum_inode_stats(state, prev_stats) { // Deleted inode (no data, no I/O, no config) delete inode_stats[pool_id][inode_num]; + delete (prev_stats.inode_totals[pool_id]||{})[inode_num]; } } } diff --git a/mon/test-stats.js b/mon/test-stats.js index a96c897c..e583fe41 100644 --- a/mon/test-stats.js +++ b/mon/test-stats.js @@ -1,33 +1,42 @@ // Copyright (c) Vitaliy Filippov, 2019+ // License: VNPL-1.1 (see README.md for details) -// Total op counters must never go down: on OSD down, OSD restart and monitor master change +// Total and per-inode op counters must never go down: on OSD down, OSD restart and monitor master change // Run: node test-stats.js const assert = require('assert'); -const { sum_op_stats, serialize_bigints } = require('./stats.js'); +const { sum_op_stats, sum_inode_stats, serialize_bigints } = require('./stats.js'); const state = { - osd: { state: { 1: { state: 'up' }, 2: { state: 'up' } }, stats: {}, inodestats: {} }, + config: { pools: { 1: {} }, inode: { 1: { 5: {} } } }, + osd: { state: { 1: { state: 'up' }, 2: { state: 'up' } }, stats: {}, inodestats: {}, space: {} }, + pool: { stats: {} }, + inode: { stats: {} }, stats: {}, }; let prev_stats = { osd_stats: {}, osd_diff: {} }; -let last = [ 0n, 0n, 0n ]; +let last = [ 0n, 0n, 0n, 0n, 0n, 0n ]; function step(time, counts, growth) { for (const osd in counts) { - state.osd.stats[osd] = { time, op_stats: { primary_write: { count: counts[osd], usec: counts[osd]*10, bytes: counts[osd]*4096 } } }; + const c = { count: counts[osd], usec: counts[osd]*10, bytes: counts[osd]*4096 }; + state.osd.stats[osd] = { time, op_stats: { primary_write: c } }; + state.osd.inodestats[osd] = { 1: { 5: { read: c, write: c, delete: c } } }; } - // Round-trip through JSON like /stats in etcd - state.stats = JSON.parse(JSON.stringify(serialize_bigints(sum_op_stats(state.osd, prev_stats, state.stats)))); + // Round-trip through JSON like /stats and /inode/stats in etcd + const json = (o) => JSON.parse(JSON.stringify(serialize_bigints(o))); + state.stats = json(sum_op_stats(state.osd, prev_stats, state.stats)); + state.inode.stats = json(sum_inode_stats(state, prev_stats).inode_stats); const st = state.stats.op_stats.primary_write || {}; - const cur = [ BigInt(st.count||0), BigInt(st.usec||0), BigInt(st.bytes||0) ]; + const ist = state.inode.stats[1] && state.inode.stats[1][5] && state.inode.stats[1][5].write || {}; + const cur = [ st.count, st.usec, st.bytes, ist.count, ist.usec, ist.bytes ].map(v => BigInt(v||0)); if (growth !== null) { - assert.deepStrictEqual(cur.map((c, i) => c-last[i]), [ BigInt(growth), BigInt(growth*10), BigInt(growth*4096) ]); + const g = [ growth, growth*10, growth*4096 ].map(BigInt); + assert.deepStrictEqual(cur.map((c, i) => c-last[i]), [ ...g, ...g ]); } last = cur; } -- 2.47.3