Compare commits
3 Commits
76fe5ea44e
...
7ea356170f
Author | SHA1 | Date |
---|---|---|
Vitaliy Filippov | 7ea356170f | |
Vitaliy Filippov | fa29c11a01 | |
Vitaliy Filippov | 4689076bc3 |
|
@ -105,6 +105,9 @@ Specify <ca> = <cert> if your certificate is self-signed.</dd>
|
|||
<dt>--ws_keepalive_interval 30000</dt>
|
||||
<dd>Client websocket ping (keepalive) interval in milliseconds</dd>
|
||||
|
||||
<dt>--use_base64 1</dt>
|
||||
<dd>Use base64 encoding of keys and values, like in etcd (enabled by default).</dd>
|
||||
|
||||
</dl>
|
||||
|
||||
### Persistence
|
||||
|
|
|
@ -226,7 +226,10 @@ class AntiCluster
|
|||
}
|
||||
else
|
||||
{
|
||||
this._log('Got dump from '+client.raft_node_id+' with stored term '+res.term);
|
||||
this._log(
|
||||
'Got dump from '+client.raft_node_id+' with stored term '+res.term+
|
||||
', mod_revision '+res.mod_revision+', compact_revision '+res.compact_revision
|
||||
);
|
||||
}
|
||||
this.resync_state.dumps[client.raft_node_id] = res.error ? null : res;
|
||||
this._continueResync();
|
||||
|
@ -325,7 +328,10 @@ class AntiCluster
|
|||
this.antietcd.stored_term = this.raft.term;
|
||||
this.synced = true;
|
||||
runCallbacks(this, 'wait_sync', []);
|
||||
this._log('Synchronized with followers, new term is '+this.raft.term);
|
||||
this._log(
|
||||
'Synchronized with followers, new term is '+this.raft.term+
|
||||
', mod_revision '+this.antietcd.etctree.mod_revision+', compact_revision '+this.antietcd.etctree.compact_revision
|
||||
);
|
||||
}
|
||||
|
||||
_isWrite(path, data)
|
||||
|
@ -452,7 +458,10 @@ class AntiCluster
|
|||
this.antietcd.stored_term = msg.term;
|
||||
this.synced = true;
|
||||
runCallbacks(this, 'wait_sync', []);
|
||||
this._log('Synchronized with leader, new term is '+msg.term);
|
||||
this._log(
|
||||
'Synchronized with leader, new term is '+this.raft.term+
|
||||
', mod_revision '+this.antietcd.etctree.mod_revision+', compact_revision '+this.antietcd.etctree.compact_revision
|
||||
);
|
||||
client.socket.send(JSON.stringify({ request_id: msg.request_id, reply: {} }));
|
||||
}
|
||||
else
|
||||
|
|
|
@ -46,6 +46,8 @@ HTTP:
|
|||
--merge_watches 1
|
||||
Antietcd merges all watcher events into a single websocket message to provide
|
||||
more ordering/transaction guarantees. Set to 0 to disable this behaviour.
|
||||
--use_base64 1
|
||||
Use base64 encoding of keys and values, like in etcd (enabled by default).
|
||||
|
||||
Persistence:
|
||||
|
||||
|
|
86
antietcd.js
86
antietcd.js
|
@ -15,7 +15,7 @@ const ws = require('ws');
|
|||
const EtcTree = require('./etctree.js');
|
||||
const AntiPersistence = require('./antipersistence.js');
|
||||
const AntiCluster = require('./anticluster.js');
|
||||
const { runCallbacks, RequestError } = require('./common.js');
|
||||
const { runCallbacks, de64, b64, RequestError } = require('./common.js');
|
||||
|
||||
const VERSION = '1.0.9';
|
||||
|
||||
|
@ -26,6 +26,7 @@ class AntiEtcd extends EventEmitter
|
|||
super();
|
||||
cfg['merge_watches'] = !('merge_watches' in cfg) || is_true(cfg['merge_watches']);
|
||||
cfg['stale_read'] = !('stale_read' in cfg) || is_true(cfg['stale_read']);
|
||||
cfg['use_base64'] = !('use_base64' in cfg) || is_true(cfg['use_base64']);
|
||||
this.clients = {};
|
||||
this.client_id = 1;
|
||||
this.etctree = new EtcTree(true);
|
||||
|
@ -415,7 +416,7 @@ class AntiEtcd extends EventEmitter
|
|||
// public watch API
|
||||
async create_watch(params, callback, stream_id)
|
||||
{
|
||||
const watch = this.etctree.api_create_watch({ ...params, watch_id: null }, callback, stream_id);
|
||||
const watch = this.etctree.api_create_watch(this._encodeWatch(params), (msg) => callback(this._encodeMsg(msg)), stream_id);
|
||||
if (!watch.created)
|
||||
{
|
||||
throw new RequestError(400, 'Requested watch revision is compacted', { compact_revision: watch.compact_revision });
|
||||
|
@ -439,24 +440,64 @@ class AntiEtcd extends EventEmitter
|
|||
// internal handlers
|
||||
async _handle_kv_txn(data)
|
||||
{
|
||||
return await this.etctree.api_txn(data);
|
||||
if (this.cfg.use_base64)
|
||||
{
|
||||
for (const item of data.compare||[])
|
||||
{
|
||||
if (item.key != null)
|
||||
item.key = de64(item.key);
|
||||
}
|
||||
for (const items of [ data.success, data.failure ])
|
||||
{
|
||||
for (const item of items||[])
|
||||
{
|
||||
const req = item.request_range || item.requestRange ||
|
||||
item.request_put || item.requestPut ||
|
||||
item.request_delete_range || item.requestDeleteRange;
|
||||
if (req.key != null)
|
||||
req.key = de64(req.key);
|
||||
if (req.range_end != null)
|
||||
req.range_end = de64(req.range_end);
|
||||
if (req.value != null)
|
||||
req.value = de64(req.value);
|
||||
}
|
||||
}
|
||||
}
|
||||
const result = await this.etctree.api_txn(data);
|
||||
if (this.cfg.use_base64)
|
||||
{
|
||||
for (const item of result.responses||[])
|
||||
{
|
||||
if (item.response_range)
|
||||
{
|
||||
for (const kv of item.response_range.kvs)
|
||||
{
|
||||
if (kv.key != null)
|
||||
kv.key = b64(kv.key);
|
||||
if (kv.value != null)
|
||||
kv.value = b64(kv.value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
async _handle_kv_range(data)
|
||||
{
|
||||
const r = await this.etctree.api_txn({ success: [ { request_range: data } ] });
|
||||
const r = await this._handle_kv_txn({ success: [ { request_range: data } ] });
|
||||
return { header: r.header, ...r.responses[0].response_range };
|
||||
}
|
||||
|
||||
async _handle_kv_put(data)
|
||||
{
|
||||
const r = await this.etctree.api_txn({ success: [ { request_put: data } ] });
|
||||
const r = await this._handle_kv_txn({ success: [ { request_put: data } ] });
|
||||
return { header: r.header, ...r.responses[0].response_put };
|
||||
}
|
||||
|
||||
async _handle_kv_deleterange(data)
|
||||
{
|
||||
const r = await this.etctree.api_txn({ success: [ { request_delete_range: data } ] });
|
||||
const r = await this._handle_kv_txn({ success: [ { request_delete_range: data } ] });
|
||||
return { header: r.header, ...r.responses[0].response_delete_range };
|
||||
}
|
||||
|
||||
|
@ -518,9 +559,9 @@ class AntiEtcd extends EventEmitter
|
|||
const create_request = msg.create_request;
|
||||
if (!create_request.watch_id || !client.watches[create_request.watch_id])
|
||||
{
|
||||
client.send_cb = client.send_cb || (msg => socket.send(JSON.stringify(msg)));
|
||||
client.send_cb = client.send_cb || (msg => socket.send(JSON.stringify(this._encodeMsg(msg))));
|
||||
const watch = this.etctree.api_create_watch(
|
||||
{ ...create_request, watch_id: null }, client.send_cb, (this.cfg.merge_watches ? 'C'+client_id : null)
|
||||
this._encodeWatch(create_request), client.send_cb, (this.cfg.merge_watches ? 'C'+client_id : null)
|
||||
);
|
||||
if (!watch.created)
|
||||
{
|
||||
|
@ -558,6 +599,35 @@ class AntiEtcd extends EventEmitter
|
|||
}
|
||||
}
|
||||
|
||||
_encodeWatch(create_request)
|
||||
{
|
||||
const req = { ...create_request, watch_id: null };
|
||||
if (this.cfg.use_base64)
|
||||
{
|
||||
if (req.key != null)
|
||||
req.key = de64(req.key);
|
||||
if (req.range_end != null)
|
||||
req.range_end = de64(req.range_end);
|
||||
}
|
||||
return req;
|
||||
}
|
||||
|
||||
_encodeMsg(msg)
|
||||
{
|
||||
if (this.cfg.use_base64 && msg.result && msg.result.events)
|
||||
{
|
||||
return { ...msg, result: { ...msg.result, events: msg.result.events.map(ev => ({
|
||||
...ev,
|
||||
kv: !ev.kv ? ev.kv : {
|
||||
...ev.kv,
|
||||
key: b64(ev.kv.key),
|
||||
value: b64(ev.kv.value),
|
||||
},
|
||||
})) } };
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
_unsubscribeClient(client_id)
|
||||
{
|
||||
if (!this.clients[client_id])
|
||||
|
|
|
@ -8,7 +8,7 @@ const zlib = require('zlib');
|
|||
|
||||
const stableStringify = require('./stable-stringify.js');
|
||||
const EtcTree = require('./etctree.js');
|
||||
const { de64, runCallbacks } = require('./common.js');
|
||||
const { runCallbacks } = require('./common.js');
|
||||
|
||||
class AntiPersistence
|
||||
{
|
||||
|
@ -60,20 +60,18 @@ class AntiPersistence
|
|||
if (ev.lease)
|
||||
{
|
||||
// Values with lease are never persisted
|
||||
const key = de64(ev.key);
|
||||
if (this.prev_value[key] !== undefined)
|
||||
if (this.prev_value[ev.key] !== undefined)
|
||||
{
|
||||
delete this.prev_value[key];
|
||||
delete this.prev_value[ev.key];
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
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]))
|
||||
const filtered = this.cfg.persist_filter(ev.key, ev.value == null ? undefined : ev.value);
|
||||
if (!EtcTree.eq(filtered, this.prev_value[ev.key]))
|
||||
{
|
||||
this.prev_value[key] = filtered;
|
||||
this.prev_value[ev.key] = filtered;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
@ -116,7 +114,7 @@ class AntiPersistence
|
|||
this.wait_persist = [];
|
||||
try
|
||||
{
|
||||
let dump = this.antietcd.etctree.dump(true);
|
||||
let dump = this.antietcd.etctree.dump(true, this.cfg.persist_filter);
|
||||
dump['term'] = this.antietcd.stored_term;
|
||||
dump = stableStringify(dump);
|
||||
dump = await new Promise((ok, no) => zlib.gzip(dump, (err, res) => err ? no(err) : ok(res)));
|
||||
|
|
|
@ -19,6 +19,13 @@ function de64(k)
|
|||
return Buffer.from(k, 'base64').toString();
|
||||
}
|
||||
|
||||
function b64(k)
|
||||
{
|
||||
if (k == null) // null or undefined
|
||||
return k;
|
||||
return Buffer.from(k).toString('base64');
|
||||
}
|
||||
|
||||
function runCallbacks(obj, key, new_value)
|
||||
{
|
||||
const cbs = obj[key];
|
||||
|
@ -35,5 +42,6 @@ function runCallbacks(obj, key, new_value)
|
|||
module.exports = {
|
||||
RequestError,
|
||||
de64,
|
||||
b64,
|
||||
runCallbacks,
|
||||
};
|
||||
|
|
53
etctree.js
53
etctree.js
|
@ -19,7 +19,7 @@ const { RequestError } = require('./common.js');
|
|||
|
||||
class EtcTree
|
||||
{
|
||||
constructor(use_base64)
|
||||
constructor()
|
||||
{
|
||||
this.state = {};
|
||||
this.leases = {};
|
||||
|
@ -27,7 +27,6 @@ class EtcTree
|
|||
this.watcher_id = 0;
|
||||
this.mod_revision = 0;
|
||||
this.compact_revision = 0;
|
||||
this.use_base64 = use_base64;
|
||||
this.replicate = null;
|
||||
this.paused = false;
|
||||
this.active_immediate = [];
|
||||
|
@ -51,23 +50,9 @@ class EtcTree
|
|||
this.replicate = replicate;
|
||||
}
|
||||
|
||||
de64(k)
|
||||
{
|
||||
if (k == null) // null or undefined
|
||||
return k;
|
||||
return this.use_base64 ? Buffer.from(k, 'base64').toString() : k;
|
||||
}
|
||||
|
||||
b64(k)
|
||||
{
|
||||
if (k == null) // null or undefined
|
||||
return k;
|
||||
return this.use_base64 ? Buffer.from(k).toString('base64') : k;
|
||||
}
|
||||
|
||||
_check(chk)
|
||||
{
|
||||
const parts = this._key_parts(this.de64(chk.key));
|
||||
const parts = this._key_parts(chk.key);
|
||||
const { cur } = this._get_subtree(parts, false, false);
|
||||
let check_value, ref_value;
|
||||
if (chk.target === 'MOD')
|
||||
|
@ -118,8 +103,8 @@ class EtcTree
|
|||
|
||||
_get_range(req)
|
||||
{
|
||||
const key = this.de64(req.key);
|
||||
const end = this.de64(req.range_end);
|
||||
const key = req.key;
|
||||
const end = req.range_end;
|
||||
if (end != null && (key !== '' && end !== '') && (key[key.length-1] != '/' || end[end.length-1] != '0' ||
|
||||
end.substr(0, end.length-1) !== key.substr(0, key.length-1)))
|
||||
{
|
||||
|
@ -168,7 +153,7 @@ class EtcTree
|
|||
dump(persistent_only, value_filter)
|
||||
{
|
||||
const snapshot = {
|
||||
state: this._copy_tree(this.state, persistent_only, value_filter) || {},
|
||||
state: this._copy_tree(this.state, null, persistent_only, value_filter) || {},
|
||||
mod_revision: this.mod_revision,
|
||||
compact_revision: this.compact_revision,
|
||||
};
|
||||
|
@ -184,13 +169,13 @@ class EtcTree
|
|||
return snapshot;
|
||||
}
|
||||
|
||||
_copy_tree(cur, no_lease, value_filter)
|
||||
_copy_tree(cur, key, no_lease, value_filter)
|
||||
{
|
||||
let nonempty = cur.value != null && (!no_lease || !cur.lease);
|
||||
let filtered;
|
||||
if (nonempty && value_filter)
|
||||
{
|
||||
filtered = value_filter(cur.value);
|
||||
filtered = value_filter(key === null ? '' : key, cur.value);
|
||||
nonempty = nonempty && filtered != null;
|
||||
}
|
||||
const copy = (nonempty ? { ...cur } : {});
|
||||
|
@ -204,7 +189,7 @@ class EtcTree
|
|||
let has_children = false;
|
||||
for (const k in cur.children)
|
||||
{
|
||||
const child = this._copy_tree(cur.children[k], no_lease, value_filter);
|
||||
const child = this._copy_tree(cur.children[k], key === null ? k : key+'/'+k, no_lease, value_filter);
|
||||
if (child)
|
||||
{
|
||||
copy.children[k] = child;
|
||||
|
@ -279,7 +264,7 @@ class EtcTree
|
|||
{
|
||||
cur_old.value = cur_new.value;
|
||||
const key_watchers = (cur_old.key_watchers ? [ ...watchers, ...(cur_old.key_watchers||[]) ] : watchers);
|
||||
const notify = { watchers: key_watchers, key: this.b64(key), value: this.b64(cur_new.value), mod_revision: cur_new.mod_revision };
|
||||
const notify = { watchers: key_watchers, key, value: cur_new.value, mod_revision: cur_new.mod_revision };
|
||||
if (cur_new.lease)
|
||||
{
|
||||
notify.lease = cur_new.lease;
|
||||
|
@ -444,7 +429,7 @@ class EtcTree
|
|||
}
|
||||
for (const key in this.leases[id].keys)
|
||||
{
|
||||
this._delete_range({ key: this.use_base64 ? this.b64(key) : key }, next_revision, notifications);
|
||||
this._delete_range({ key }, next_revision, notifications);
|
||||
}
|
||||
if (this.leases[id].timer_id)
|
||||
{
|
||||
|
@ -603,9 +588,9 @@ class EtcTree
|
|||
{
|
||||
const ev = {
|
||||
type: cur.value == null ? 'DELETE' : 'PUT',
|
||||
kv: cur.value == null ? { key: this.b64(prefix === null ? '' : prefix) } : {
|
||||
key: this.b64(prefix),
|
||||
value: this.b64(cur.value),
|
||||
kv: cur.value == null ? { key: (prefix === null ? '' : prefix) } : {
|
||||
key: prefix,
|
||||
value: cur.value,
|
||||
mod_revision: cur.mod_revision,
|
||||
},
|
||||
};
|
||||
|
@ -733,9 +718,9 @@ class EtcTree
|
|||
_put(request_put, cur_revision, notifications)
|
||||
{
|
||||
// FIXME: prev_kv, ignore_value(?), ignore_lease(?)
|
||||
const parts = this._key_parts(this.de64(request_put.key));
|
||||
const parts = this._key_parts(request_put.key);
|
||||
const key = parts.join('/');
|
||||
const value = this.de64(request_put.value);
|
||||
const value = request_put.value;
|
||||
const { cur, watchers } = this._get_subtree(parts, true, true);
|
||||
if (cur.key_watchers)
|
||||
{
|
||||
|
@ -768,7 +753,7 @@ class EtcTree
|
|||
cur.create_revision = cur_revision;
|
||||
}
|
||||
cur.value = value;
|
||||
const notify = { watchers, key: this.b64(key), value: this.b64(value), mod_revision: cur.mod_revision };
|
||||
const notify = { watchers, key, value, mod_revision: cur.mod_revision };
|
||||
if (cur.lease)
|
||||
{
|
||||
notify.lease = cur.lease;
|
||||
|
@ -799,10 +784,10 @@ class EtcTree
|
|||
}
|
||||
if (cur.value != null)
|
||||
{
|
||||
const item = { key: this.b64(prefix === null ? '' : prefix) };
|
||||
const item = { key: (prefix === null ? '' : prefix) };
|
||||
if (!req.keys_only)
|
||||
{
|
||||
item.value = this.b64(cur.value);
|
||||
item.value = cur.value;
|
||||
item.mod_revision = cur.mod_revision;
|
||||
//item.create_revision = cur.create_revision;
|
||||
//item.version = cur.version;
|
||||
|
@ -839,7 +824,7 @@ class EtcTree
|
|||
this.mod_revision = cur_revision;
|
||||
notifications.push({
|
||||
watchers: cur.key_watchers ? [ ...watchers, ...cur.key_watchers ] : watchers,
|
||||
key: this.b64(prefix === null ? '' : prefix),
|
||||
key: (prefix === null ? '' : prefix),
|
||||
mod_revision: cur_revision,
|
||||
});
|
||||
}
|
||||
|
|
|
@ -184,6 +184,25 @@ tests['update'] = async () =>
|
|||
t2.destroy();
|
||||
};
|
||||
|
||||
tests['dump filter'] = async () =>
|
||||
{
|
||||
const t1 = new EtcTree();
|
||||
const leaseID = (await t1.api_grant_lease({ TTL: 0.5 })).ID;
|
||||
expect(leaseID != null, true);
|
||||
expect(
|
||||
await t1.api_txn({ success: [
|
||||
{ request_put: { key: '/vitastor/osd/state/1', lease: leaseID, value: { ip: '1.2.3.6' } } },
|
||||
{ request_put: { key: '/vitastor/osd/stats/1', value: { time: 123, read: [ 10, 1000, 10459 ] } } },
|
||||
] }),
|
||||
{ header: { revision: 2 }, succeeded: true, responses: [ { response_put: {} }, { response_put: {} } ] }
|
||||
);
|
||||
expect(
|
||||
t1.dump(true, (key, value) => (key.substr(0, '/vitastor/osd/stats'.length) === '/vitastor/osd/stats' ? { time: value.time } : value)),
|
||||
{"state":{"children":{"":{"children":{"vitastor":{"children":{"osd":{"children":{"stats":{"children":{"1":{"version":1,"mod_revision":2,"create_revision":2,"value":{"time":123}}}}}}}}}}}},"mod_revision":2,"compact_revision":0}
|
||||
);
|
||||
t1.destroy();
|
||||
};
|
||||
|
||||
tests['replicate watcher'] = async () =>
|
||||
{
|
||||
const t = new EtcTree();
|
||||
|
|
Loading…
Reference in New Issue