-
Notifications
You must be signed in to change notification settings - Fork 2
/
discover.js
65 lines (52 loc) · 1.7 KB
/
discover.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
const Paxos = require('paxos')
const { coalesce } = require('extant')
module.exports = function (id, members, complete) {
// Never mind.
if (members.length == 0) {
return { action: 'retry' }
}
// Find ourselves.
const selves = members.filter(function (member) {
return member.id == id
})
// If we cannot reach ourselves, let's not bother.
if (selves.length != 1) {
return { action: 'retry' }
}
members.sort(function (a, b) {
return coalesce(b.government.republic, 0) - coalesce(a.government.republic, 0)
})
if (members[0].government.republic == null) {
if (!complete) {
return { action: 'retry' }
}
const self = selves.shift()
members.sort(function (a, b) {
return a.createdAt - b.createdAt
})
if (self.createdAt == members[0].createdAt) {
return { action: 'bootstrap', url: self.url }
}
return { action: 'retry' }
}
const unsplit = members.filter(function (member) {
return member.government.republic == members[0].government.republic
})
unsplit.sort(function (a, b) {
return -Paxos.compare(a.government.promise, b.government.promise)
})
const government = unsplit[0].government
const ids = unsplit.map(function (member) { return member.id })
const synod = government.majority.concat(government.minority)
if (synod.filter(function (id) {
return !! ~ids.indexOf(id)
}).length != synod.length) {
return { action: 'retry' }
}
const self = selves.shift()
return {
action: 'join',
url: self.url,
republic: members[0].government.republic
}
}