From d239caec385f2bac345698e5df02c23c288aa777 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Mon, 30 Nov 2026 17:39:35 +0300 Subject: [PATCH] mon: Do not rewrite unchanged last_clean_pgs save_last_clean() runs after every batch of PG state changes and put the whole last_clean_pgs map even when it did not change. While any PG of a pool is not active the value stays the same, so during mass peering every batch writes another copy of a map as large as /pg/config (about 177 KB for 2048 EC 8+2 PGs). Each copy stays in the etcd history until the next compaction, and its watch event makes the monitor run recheck_pgs again. Write last_clean_pgs only when its value changes. --- mon/mon.js | 19 ++++++++------ mon/test-last-clean.js | 57 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 mon/test-last-clean.js diff --git a/mon/mon.js b/mon/mon.js index 5e5edbc3..f42cb888 100644 --- a/mon/mon.js +++ b/mon/mon.js @@ -356,18 +356,23 @@ class Mon } new_clean_pgs.items[pool_id] = this.state.pg.config.items[pool_id]; } - this.state.history.last_clean_pgs = new_clean_pgs; this.all_pgs_clean = all_clean; if (all_clean) this.schedule_primary_rebalance(); else this.last_unclean_time = Date.now(); - await this.etcd.etcd_call('/kv/txn', { - success: [ { requestPut: { - key: b64(this.config.etcd_prefix+'/history/last_clean_pgs'), - value: b64(JSON.stringify(this.state.history.last_clean_pgs)) - } } ], - }, this.config.etcd_start_timeout, 0); + const value = JSON.stringify(new_clean_pgs); + // An unchanged rewrite costs a full map in etcd history and a recheck via our own watch + if (value !== JSON.stringify(this.state.history.last_clean_pgs)) + { + this.state.history.last_clean_pgs = new_clean_pgs; + await this.etcd.etcd_call('/kv/txn', { + success: [ { requestPut: { + key: b64(this.config.etcd_prefix+'/history/last_clean_pgs'), + value: b64(value) + } } ], + }, this.config.etcd_start_timeout, 0); + } this.save_last_clean_running = false; } diff --git a/mon/test-last-clean.js b/mon/test-last-clean.js new file mode 100644 index 00000000..3b5e82c0 --- /dev/null +++ b/mon/test-last-clean.js @@ -0,0 +1,57 @@ +// Copyright (c) Vitaliy Filippov, 2019+ +// License: VNPL-1.1 (see README.md for details) + +// save_last_clean() writes last_clean_pgs only when it changes. Run: node mon/test-last-clean.js + +const assert = require('assert'); +const Module = require('module'); + +const orig_load = Module._load; +Module._load = function(request, ...args) +{ + return request === 'ws' || request === 'antietcd' ? class {} : orig_load.call(this, request, ...args); +}; + +const Mon = require('./mon.js'); + +async function run() +{ + const writes = []; + const mon = Object.create(Mon.prototype); + mon.config = { etcd_prefix: '/vitastor', etcd_start_timeout: 5, placement_levels: {} }; + mon.etcd = { etcd_call: async (_, body) => writes.push(JSON.parse( + Buffer.from(body.success[0].requestPut.value, 'base64').toString())) }; + mon.schedule_primary_rebalance = () => {}; + mon.state = { + config: { pools: { 1: { name: 'p1', scheme: 'replicated', pg_size: 2, pg_minsize: 1, pg_count: 2 } } }, + history: { last_clean_pgs: { items: {} } }, + pg: { + config: { items: { 1: { 1: { osd_set: [ 1, 2 ], primary: 1 }, 2: { osd_set: [ 2, 1 ], primary: 2 } } } }, + state: { 1: { 1: { state: [ 'active' ] }, 2: { state: [ 'peering' ] } } }, + }, + }; + const set_state = async (pg, st) => + { + mon.state.pg.state[1][pg] = { state: [ st ] }; + await mon.save_last_clean(); + }; + + await mon.save_last_clean(); + assert.equal(writes.length, 0, 'a pool that was never clean must not rewrite the empty map'); + await set_state(2, 'active'); + assert.equal(writes.length, 1, 'a pool that became clean must be written'); + assert.deepEqual(writes[0].items[1], mon.state.pg.config.items[1]); + await set_state(2, 'peering'); + await set_state(1, 'peering'); + await set_state(1, 'active'); + assert.equal(writes.length, 1, 'state changes of an unclean pool must not rewrite the same map'); + await set_state(2, 'active'); + assert.equal(writes.length, 1, 'becoming clean again with the same PG map must not rewrite it'); + mon.state.pg.config = { items: { 1: { ...mon.state.pg.config.items[1], 2: { osd_set: [ 3, 1 ], primary: 3 } } } }; + await set_state(2, 'active'); + assert.equal(writes.length, 2, 'a clean pool with a new PG map must be written'); + assert.deepEqual(writes[1].items[1][2], { osd_set: [ 3, 1 ], primary: 3 }); + console.log('OK'); +} + +run().catch(e => { console.error(e); process.exit(1); }); -- 2.47.3