-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.js
More file actions
211 lines (194 loc) · 7.79 KB
/
Copy pathcontroller.js
File metadata and controls
211 lines (194 loc) · 7.79 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
const Conference = require('conference')
const { Queue } = require('avenue')
const { Future } = require('perhaps')
const Distributor = require('./distributor')
const Phaser = require('./phaser')
const fnv = require('./fnv')
class Controller {
constructor (destructible, application, { active = Number.MAX_SAFE_INTEGER, ratio = 3 } = {}) {
this.distributor = new Distributor({ active, ratio })
this.conference = new Conference
this.application = application
this.phasers = []
this.events = new Queue
this._appointments = {}
this._hops = []
this.request = new Queue
this.response = new Queue
this.outbox = new Queue
this.cookie = 0n
this._snapshots = {}
destructible.durable('events', async () => {
const shifter = this.distributor.events.shifter()
for await (const event of this.distributor.events.shifter()) {
switch (event.method) {
case 'appoint': {
const response = this._appointments[event.promise.replace(/\/0$/, '')]
if (response != null) {
response()
}
}
break
case 'entry': {
const entry = event.body
let result = await this.application.write(entry, this.distributor.leader)
if (typeof result == 'function') {
result = await result()
}
const future = this._hops[entry.cookie]
delete this._hops[entry.cookie]
future.resolve(result)
}
break
// TODO Determine if you need to enqueue the read messages into the
// internal write queue. Is it necessary to deal with pauses? When
// we resume to we enqueue or send out a message to 'rehash' or
// 'rehop'?
case 'read': {
}
break
case 'write': {
// TODO Do we have to rehash? Do we have to check for a rebalance?
const index = event.hash % this.phasers.length
this.phasers[index].enqueue(event)
}
break
case 'expand': {
while (this.phasers.length < event.length) {
this.phasers.push(new Phaser({ promise: this.promise, index: this.phasers.length }, this.distributor.events, this.outbox))
}
}
break
case 'paxos': {
// TODO
event.direction = 'map'
this.compassion.enqueue(event)
}
break
}
}
})
destructible.destruct(() => this.distributor.events.push(null))
destructible.durable('outbox', async () => {
for await (const message of this.outbox.shifter()) {
let request = message
while (request != null) {
const responses = {}
for (const to of request.to) {
if (to.promise == this.promise) {
// TODO Consider some sort of URL or otherwise parsed format for
// addresses used across application. i.e. `2/0?5`
// i.e. [ promise, index ] = address.split('?')
responses[`${to.promise}?${to.index}`] = this.phasers[to.index].request(JSON.parse(JSON.stringify(request)))
} else {
throw new Error
}
}
request = this.phasers[message.address.index].response(message, responses)
}
}
})
destructible.destruct(() => this.outbox.push(null))
}
initialize (compassion) {
this.compassion = compassion
}
hop (method, key, value) {
const hash = fnv(key)
const index = hash % this.distributor.buckets.length
const bucket = this.distributor.buckets[index]
const promise = bucket.majority[0]
if (promise != this.promise) {
return Promise.resolve(promise)
}
const cookie = this.promise + '/' + String(this.cookie++)
const future = this._hops[cookie] = new Future
this.distributor.events.push({ method, hash, key, value, cookie, future })
return future.promise
}
read (key, value) {
return this.hop('read', key, value)
}
write (key, value) {
return this.hop('write', key, value)
}
where (key) {
const index = fnv(key) % this.distributor.buckets.length
const bucket = this.distributor.buckets[index]
return bucket.majority[0]
}
async bootstrap ({ self }) {
this.promise = self.arrived
}
async snapshot ({ self, queue, promise }) {
this.promise = self.arrived
queue.push(this._snapshots[promise])
queue.push(null)
}
async join ({ shifter }) {
const snapshot = await shifter.shift()
this.conference.join(snapshot.conference)
this.distributor.join(snapshot.distributor)
await shifter.shift()
}
async arrive ({ arrival, government }) {
this.conference.arrive(arrival.promise)
this.distributor.arrive(arrival.promise, government.arrived.promise[government.majority])
}
async reduce (reductions) {
for (const reduction of reductions) {
this.distributor.response(reduction.map)
for (const response of reduction.map.response) {
for (const to of response.to) {
if (to.promise == '0/0' || to.promise == this.promise) {
switch (response.method) {
case 'majority': {
}
break
case 'resume': {
this.phasers[to.index].resume()
}
break
default: throw new Error
}
}
}
}
}
}
async entry ({ promise, self, entry, from }) {
this.events.push({ method: 'entry', entry })
switch (entry.direction) {
case 'map': {
this.conference.map(entry.cookie, entry)
const response = () => this.compassion.enqueue({ cookie: entry.cookie, direction: 'reduce' })
for (const request of entry.request) {
for (const to of request.to) {
if (self.arrived == to.promise) {
switch (request.method) {
case 'appoint': {
this._appointments[promise] = response
this.phasers[to.index].appoint(promise, request.majority)
}
break
}
}
}
}
if (this._appointments[promise] == null) {
response()
}
}
break
case 'reduce': {
this.reduce(this.conference.reduce(entry.cookie, self.arrived, null))
}
break
}
}
async acclimated ({ promise }) {
delete this._snapshots[promise]
this.events.push({ method: 'acclimated' })
}
}
module.exports = Controller