Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #22

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Fix #22

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
21 changes: 14 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,15 @@ class Raft extends EventEmitter {
// When a server starts, it's always started as Follower and it will remain in
// this state until receive a message from a Leader or Candidate.
//
raft.state = options.state || Raft.FOLLOWER; // Our current state.
raft.leader = ''; // Leader in our cluster.
raft.term = 0; // Our current term.

// Our current state.
raft.state = Number.isInteger(options.state) ? options.state : Raft.FOLLOWER;

// Leader in our cluster.
raft.leader = '';

// Our current term.
raft.term = 0;

raft._initialize(options);
}
Expand Down Expand Up @@ -907,10 +913,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