mon: Do not rewrite unchanged last_clean_pgs #212

Open
koolkhel wants to merge 1 commits from koolkhel/vitastor:mon-last-clean-unchanged into master
2 changed files with 69 additions and 7 deletions

View File

@@ -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;
}

57
mon/test-last-clean.js Normal file
View File

@@ -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); });