-
Notifications
You must be signed in to change notification settings - Fork 32
/
Proposer.js
184 lines (162 loc) · 5.01 KB
/
Proposer.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
class ProposerError extends Error {
static ConcurrentRequestError() {
return new ProposerError("ConcurrentRequestError");
}
static PrepareError() {
return new ProposerError("PrepareError")
}
static CommitError() {
return new ProposerError("CommitError")
}
static UpdateError(err) {
const error = new ProposerError("UpdateError")
error.err = err;
return error;
}
constructor(code, ...args) {
super(...args)
this.code = code;
Error.captureStackTrace(this, ProposerError)
}
}
class InsufficientQuorumError extends Error {
constructor(all, ...args) {
super(...args)
this.all = all;
Error.captureStackTrace(this, InsufficientQuorumError)
}
}
class Proposer {
constructor(ballot, prepare, accept) {
this.ballot = ballot;
this.prepare = prepare;
this.accept = accept;
this.cache = new Map();
this.locks = new Set();
}
async change(key, update, extra) {
if (!this.tryLock(key)) {
throw ProposerError.ConcurrentRequestError();
}
try {
const [ballot, curr] = await this.guessValue(key, extra);
let next = curr;
let error = null;
try {
next = update(curr);
} catch (e) {
error = e;
}
const promise = ballot.next();
await this.commitValue(key, ballot, next, promise, extra);
this.cache.set(key, [promise, next]);
if (error != null) {
throw ProposerError.UpdateError(error);
}
return next;
} finally {
this.unlock(key);
}
}
async guessValue(key, extra) {
if (!this.cache.has(key)) {
const tick = this.ballot.inc();
let ok = null;
try {
[ok] = await waitFor(
this.prepare.nodes.map(x => x.prepare(key, tick, extra)),
x => x.isPrepared,
this.prepare.quorum
);
} catch (e) {
if (e instanceof InsufficientQuorumError) {
for (const x of e.all.filter(x => x.isConflict)) {
this.ballot.fastforwardAfter(x.ballot);
}
throw ProposerError.PrepareError();
} else {
throw e;
}
}
const value = max(ok, x => x.ballot).value;
this.cache.set(key, [tick, value]);
}
return this.cache.get(key);
}
async commitValue(key, ballot, value, promise, extra) {
let ok = null;
let all = [];
try {
[ok, all] = await waitFor(
this.accept.nodes.map(x => x.accept(key, ballot, value, promise, extra)),
x => x.isOk,
this.accept.quorum
);
} catch (e) {
if (e instanceof InsufficientQuorumError) {
all = e.all;
throw ProposerError.CommitError();
} else {
throw e;
}
} finally {
for (const x of all.filter(x => x.isConflict)) {
this.cache.delete(key);
this.ballot.fastforwardAfter(x.ballot);
}
}
}
tryLock(key) {
if (this.locks.has(key)) {
return false;
}
this.locks.add(key);
return true;
}
unlock(key) {
this.locks.delete(key);
}
}
function max(iterable, selector) {
return iterable.reduce((acc,e) => {
return selector(acc).compareTo(selector(e)) < 0 ? e : acc
}, iterable[0]);
}
function waitFor(promises, cond, count) {
return new Promise((resolve, reject) => {
const result = [];
const all = [];
let isResolved = false;
let failed = 0;
for (let promise of promises) {
(async function() {
let value = null;
let error = false;
try {
value = await promise;
if (isResolved) return;
all.push(value);
if (!cond(value)) error = true;
} catch(e) {
if (isResolved) return;
error = true;
}
if (error) {
failed += 1;
if (promises.length - failed < count) {
isResolved = true;
reject(new InsufficientQuorumError(all));
}
} else {
result.push(value);
if (result.length == count) {
isResolved = true;
resolve([result, all]);
}
}
})()
}
});
}
exports.Proposer = Proposer;
exports.ProposerError = ProposerError;