Fix persistence

master
Vitaliy Filippov 2024-05-09 13:50:11 +03:00
parent 2e89aa8b17
commit 0c75cd1d63
1 changed files with 7 additions and 7 deletions

View File

@ -2,6 +2,7 @@ const fs = require('fs');
const fsp = require('fs').promises;
const zlib = require('zlib');
const stableStringify = require('./stable-stringify.js');
const EtcTree = require('./etctree.js');
const { de64, runCallbacks } = require('./common.js');
@ -52,10 +53,10 @@ class AntiPersistence
let changed = false;
for (const ev of msg.events)
{
if (ev.kv.lease)
if (ev.lease)
{
// Values with lease are never persisted
const key = de64(ev.kv.key);
const key = de64(ev.key);
if (this.prev_value[key] !== undefined)
{
delete this.prev_value[key];
@ -64,15 +65,14 @@ class AntiPersistence
}
else
{
const key = de64(ev.kv.key);
const filtered = this.cfg.persist_filter(key, ev.type === 'DELETE' ? undefined : de64(ev.kv.value));
const key = de64(ev.key);
const filtered = this.cfg.persist_filter(key, ev.value == null ? undefined : de64(ev.value));
if (!EtcTree.eq(filtered, this.prev_value[key]))
{
this.prev_value[key] = filtered;
changed = true;
}
}
changed = true;
}
if (!changed)
{
@ -114,7 +114,7 @@ class AntiPersistence
{
let dump = this.antietcd.etctree.dump(true);
dump['term'] = this.antietcd.stored_term;
dump = JSON.stringify(dump);
dump = stableStringify(dump);
dump = await new Promise((ok, no) => zlib.gzip(dump, (err, res) => err ? no(err) : ok(res)));
const fh = await fsp.open(this.cfg.data+'.tmp', 'w');
await fh.writeFile(dump);
@ -124,7 +124,7 @@ class AntiPersistence
}
catch (e)
{
console.error(e);
console.error('Error persisting data to disk: '+e);
process.exit(1);
}
runCallbacks(this, 'wait_persist', null);