-
Notifications
You must be signed in to change notification settings - Fork 11
/
acceptor.js
66 lines (58 loc) · 1.85 KB
/
acceptor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const Recorder = require('./recorder')
const Monotonic = require('./monotonic')
class Acceptor {
constructor (paxos) {
this.register = paxos._writer.register || {
body: paxos.top.body,
previous: null
}
this.promise = paxos.top.body.promise
this._paxos = paxos
}
request (now, message) {
switch (message.method) {
case 'prepare':
if (Monotonic.compare(this.promise, message.promise) < 0) {
this.promise = message.promise
return {
method: 'promise',
promise: this.promise,
register: this.register
}
}
break
case 'accept':
if (Monotonic.compare(this.promise, message.body.promise) == 0) {
const register = {
body: message.body,
previous: message.previous
}
this.register = register
this.promise = register.body.promise
return { method: 'accepted', promise: this.promise }
}
break
}
return { method: 'reject', promise: this.promise }
}
createRecorder (promise) {
const entries = []
let register = this.register
while (register) {
entries.push(register.body)
register = register.previous
}
entries.reverse()
for (let i = 1, I = entries.length - 1; i < I; i++) {
if (entries[i].promise == promise) {
return this
}
}
// TODO Does it matter if the promise is off? Or only register contents?
return new Recorder(this._paxos)
}
inspect () {
return { type: 'Acceptor', register: this.register }
}
}
module.exports = Acceptor