@platformatic/watt-extra 1.22.0-alpha.1 → 1.22.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/durable-objects.js +31 -1
- package/package.json +1 -1
- package/plugins/durable-objects.js +13 -3
- package/plugins/update.js +17 -6
- package/test/durable-objects.test.js +36 -0
package/lib/durable-objects.js
CHANGED
|
@@ -10,7 +10,8 @@ export const LAVORATORE_TYPES = ['@platformatic/lavoratore', 'lavoratore']
|
|
|
10
10
|
// The ITC commands handled by the Lavoratore applications, with the same names and payloads as their runtime messages
|
|
11
11
|
export const COMMANDS = {
|
|
12
12
|
'durable-objects-promote': 'plt:lavoratore:promote',
|
|
13
|
-
'durable-objects-drain': 'plt:lavoratore:drain'
|
|
13
|
+
'durable-objects-drain': 'plt:lavoratore:drain',
|
|
14
|
+
'durable-objects-list': 'plt:lavoratore:objects'
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
// The names a runtime gives to the configuration file of an application which declares none
|
|
@@ -129,3 +130,32 @@ export function aggregateDrain (results) {
|
|
|
129
130
|
|
|
130
131
|
return { status, applications: results }
|
|
131
132
|
}
|
|
133
|
+
|
|
134
|
+
// The objects the Lavoratore applications of the pod hold, each with its application: active, or hibernated. An
|
|
135
|
+
// application without Durable Objects answers { status: 'unsupported' } and has none. Truncated when an application
|
|
136
|
+
// holds more objects than it lists: total counts them all. applications are those which listed their objects, even
|
|
137
|
+
// none: ICC tells from them a pod which holds no object from one which cannot list, and from the skipped ones a list
|
|
138
|
+
// which misses the objects of some applications.
|
|
139
|
+
export function aggregateObjects (results) {
|
|
140
|
+
const objects = []
|
|
141
|
+
const applications = []
|
|
142
|
+
let total = 0
|
|
143
|
+
let truncated = false
|
|
144
|
+
|
|
145
|
+
for (const [application, result] of Object.entries(results)) {
|
|
146
|
+
if (!Array.isArray(result.objects)) {
|
|
147
|
+
continue
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
applications.push(application)
|
|
151
|
+
|
|
152
|
+
for (const object of result.objects) {
|
|
153
|
+
objects.push({ application, ...object })
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
total += result.total ?? result.objects.length
|
|
157
|
+
truncated ||= result.truncated === true
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return { total, truncated, applications, objects }
|
|
161
|
+
}
|
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { aggregateDrain, aggregatePromotion, COMMANDS, isLavoratoreApplication } from '../lib/durable-objects.js'
|
|
1
|
+
import { aggregateDrain, aggregateObjects, aggregatePromotion, COMMANDS, isLavoratoreApplication } from '../lib/durable-objects.js'
|
|
2
2
|
|
|
3
3
|
// Forwards the Durable Objects commands of ICC to the Lavoratore applications of the runtime, see lib/durable-objects.js
|
|
4
4
|
async function durableObjects (app, _opts) {
|
|
@@ -37,7 +37,8 @@ async function durableObjects (app, _opts) {
|
|
|
37
37
|
// repetition joins the one still running instead of starting another
|
|
38
38
|
const inflight = new Map()
|
|
39
39
|
|
|
40
|
-
// Runs a command on every Lavoratore application: a promotion with { generation }
|
|
40
|
+
// Runs a command on every Lavoratore application: a promotion with { generation }, a drain, or the list of the
|
|
41
|
+
// objects the pod holds. Returns the aggregate
|
|
41
42
|
// of their answers, see lib/durable-objects.js. An application which has no handler for the command, i.e. without
|
|
42
43
|
// Durable Objects placed across pods, or with a version of Lavoratore which predates the commands, is skipped: it has
|
|
43
44
|
// nothing to promote or drain, and ICC must not wait for it. The applications are all asked even when one fails, and
|
|
@@ -86,7 +87,16 @@ async function durableObjects (app, _opts) {
|
|
|
86
87
|
throw error
|
|
87
88
|
}
|
|
88
89
|
|
|
89
|
-
|
|
90
|
+
let aggregate
|
|
91
|
+
|
|
92
|
+
if (command === 'durable-objects-promote') {
|
|
93
|
+
aggregate = aggregatePromotion(results)
|
|
94
|
+
} else if (command === 'durable-objects-list') {
|
|
95
|
+
aggregate = aggregateObjects(results)
|
|
96
|
+
} else {
|
|
97
|
+
aggregate = aggregateDrain(results)
|
|
98
|
+
}
|
|
99
|
+
|
|
90
100
|
return skipped.length > 0 ? { ...aggregate, skipped } : aggregate
|
|
91
101
|
}
|
|
92
102
|
|
package/plugins/update.js
CHANGED
|
@@ -114,14 +114,25 @@ async function updatePlugin (app) {
|
|
|
114
114
|
}
|
|
115
115
|
|
|
116
116
|
// Handle the Durable Objects commands of ICC: the promotion of a staged version, with the generation of the
|
|
117
|
-
// approval,
|
|
118
|
-
// with their result, so that ICC knows what happened (see
|
|
119
|
-
|
|
120
|
-
|
|
117
|
+
// approval, the drain of a retiring one, and the list of the objects the pod holds. They are forwarded to the
|
|
118
|
+
// Lavoratore applications and answered with their result, so that ICC knows what happened (see
|
|
119
|
+
// lib/durable-objects.js).
|
|
120
|
+
if (command === 'durable-objects-promote' || command === 'durable-objects-drain' || command === 'durable-objects-list') {
|
|
121
|
+
// The list is read again and again while somebody looks at it, and changes nothing: it is not logged
|
|
122
|
+
const logged = command !== 'durable-objects-list'
|
|
123
|
+
|
|
124
|
+
if (logged) {
|
|
125
|
+
app.log.info({ command, params: message.params }, 'Received Durable Objects command from ICC')
|
|
126
|
+
}
|
|
127
|
+
|
|
121
128
|
try {
|
|
122
129
|
const result = await app.sendDurableObjectsCommand(command, message.params ?? {})
|
|
123
|
-
|
|
124
|
-
|
|
130
|
+
|
|
131
|
+
if (logged) {
|
|
132
|
+
// What the applications answered: a promotion or a drain which did nothing is found here
|
|
133
|
+
app.log.info({ command, params: message.params, result }, 'Answered the Durable Objects command of ICC')
|
|
134
|
+
}
|
|
135
|
+
|
|
125
136
|
if (message.requestId) {
|
|
126
137
|
socket.send(JSON.stringify({
|
|
127
138
|
requestId: message.requestId,
|
|
@@ -315,3 +315,39 @@ test('the Durable Objects commands of ICC are answered over the websocket with t
|
|
|
315
315
|
{ command: 'durable-objects-drain', params: {} }
|
|
316
316
|
])
|
|
317
317
|
})
|
|
318
|
+
|
|
319
|
+
test('the objects of the pod are listed over its Lavoratore applications', async () => {
|
|
320
|
+
const counter = { className: 'Counter', id: 'a1', name: 'a', state: 'active', storageBytes: 4096 }
|
|
321
|
+
const room = { className: 'Room', id: 'b2', name: null, state: 'hibernated', storageBytes: null }
|
|
322
|
+
const runtime = createMockRuntime([
|
|
323
|
+
{ id: 'objects', type: '@platformatic/lavoratore' },
|
|
324
|
+
{ id: 'rooms', type: '@platformatic/lavoratore' },
|
|
325
|
+
{ id: 'front', type: '@platformatic/lavoratore' },
|
|
326
|
+
{ id: 'legacy', type: '@platformatic/lavoratore' }
|
|
327
|
+
], {
|
|
328
|
+
objects: { status: 'ok', memberId: 'pod-1', total: 5, truncated: true, objects: [counter] },
|
|
329
|
+
rooms: { status: 'ok', memberId: 'pod-1', total: 1, truncated: false, objects: [room] },
|
|
330
|
+
// Without Durable Objects
|
|
331
|
+
front: { status: 'unsupported' }
|
|
332
|
+
// legacy has no handler: a Lavoratore which predates the list
|
|
333
|
+
})
|
|
334
|
+
const app = await createApp(runtime)
|
|
335
|
+
|
|
336
|
+
assert.deepStrictEqual(await app.sendDurableObjectsCommand('durable-objects-list', { limit: 1 }), {
|
|
337
|
+
total: 6,
|
|
338
|
+
truncated: true,
|
|
339
|
+
applications: ['objects', 'rooms'],
|
|
340
|
+
objects: [{ application: 'objects', ...counter }, { application: 'rooms', ...room }],
|
|
341
|
+
skipped: ['legacy']
|
|
342
|
+
})
|
|
343
|
+
assert.deepStrictEqual(runtime.commands[0], { id: 'objects', name: 'plt:lavoratore:objects', params: { limit: 1 } })
|
|
344
|
+
|
|
345
|
+
// An application which lists no object listed: the pod holds none, which is not a pod which cannot list
|
|
346
|
+
const empty = await createApp(createMockRuntime([{ id: 'objects', type: '@platformatic/lavoratore' }, { id: 'legacy', type: '@platformatic/lavoratore' }], {
|
|
347
|
+
objects: { status: 'ok', memberId: 'pod-1', total: 0, truncated: false, objects: [] }
|
|
348
|
+
}))
|
|
349
|
+
assert.deepStrictEqual(await empty.sendDurableObjectsCommand('durable-objects-list'), { total: 0, truncated: false, applications: ['objects'], objects: [], skipped: ['legacy'] })
|
|
350
|
+
|
|
351
|
+
const legacy = await createApp(createMockRuntime([{ id: 'legacy', type: '@platformatic/lavoratore' }]))
|
|
352
|
+
assert.deepStrictEqual(await legacy.sendDurableObjectsCommand('durable-objects-list'), { total: 0, truncated: false, applications: [], objects: [], skipped: ['legacy'] })
|
|
353
|
+
})
|