Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -907,10 +907,11 @@ class Raft extends EventEmitter {
let raft = this;

if(raft.state !== Raft.LEADER) {
return fn({
message: 'NOTLEADER',
leaderAddress: raft.leader
});
const err = new Error('NOTLEADER');

err.leaderAddress = raft.leader;

throw err;
}

// about to send an append so don't send a heart beat
Expand Down
50 changes: 32 additions & 18 deletions log.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
const encode = require('encoding-down');
const levelup = require('levelup');
const PromiseQueue = require('promise-queue')

const keyEncoding = {
decode: function (raw) {
return parseInt(raw);
},
encode: function (key) {
return key.toString(16).padStart(8, '0');
}
};

/**
* @typedef Entry
Expand All @@ -21,7 +31,8 @@ class Log {
constructor (node, {adapter = require('leveldown'), path = ''}) {
this.node = node;
this.committedIndex = 0;
this.db = levelup(encode(adapter(path), { valueEncoding: 'json', keyEncoding: 'binary'}));
this.db = levelup(encode(adapter(path), { valueEncoding: 'json', keyEncoding }));
this.commandAckQueue = new PromiseQueue(1, Infinity);
}

/**
Expand Down Expand Up @@ -263,27 +274,30 @@ class Log {
* @return {Promise<Entry>}
*/
async commandAck (index, address) {
let entry;
try {
entry = await this.get(index);
} catch (err) {
return {
responses: []
return this.commandAckQueue.add(async () => {
let entry;
try {
entry = await this.get(index);
} catch (err) {
return {
responses: []
}
}
}

const entryIndex = await entry.responses.findIndex(resp => resp.address === address);
// node hasn't voted yet. Add response
if (entryIndex === -1) {
entry.responses.push({
address,
ack: true
});
}
const entryIndex = await entry.responses.findIndex(resp => resp.address === address);

await this.put(entry);
// node hasn't voted yet. Add response
if (entryIndex === -1) {
entry.responses.push({
address,
ack: true
});
}

return entry;
await this.put(entry);

return entry;
})
}

/**
Expand Down
Loading