@query-farm/vgi 0.50.1 → 0.52.0
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/README.md +45 -3
- package/dist/client-entry.js +833 -775
- package/dist/client-entry.js.map +10 -10
- package/dist/functions/aggregate.d.ts +25 -21
- package/dist/functions/execution-cache.d.ts +11 -0
- package/dist/functions/storage-blobs.d.ts +173 -0
- package/dist/functions/storage-cf-do.d.ts +50 -0
- package/dist/functions/storage-seal.d.ts +27 -0
- package/dist/functions/storage.d.ts +71 -1
- package/dist/functions/table-buffering.d.ts +30 -0
- package/dist/functions/table.d.ts +6 -1
- package/dist/generated/vgi-client.d.ts +7 -1
- package/dist/generated/vgi-protocol-schemas.d.ts +10 -9
- package/dist/generated/vgi-protocol-types.d.ts +244 -2
- package/dist/generated/vgi-service.d.ts +1 -1
- package/dist/hosted.d.ts +117 -0
- package/dist/http/blob-vendor.d.ts +33 -0
- package/dist/http/fetch.d.ts +60 -2
- package/dist/http/hosted-auth.d.ts +45 -0
- package/dist/index.core.d.ts +11 -3
- package/dist/index.js +8661 -6677
- package/dist/index.js.map +52 -34
- package/dist/protocol/arguments-ref.d.ts +107 -0
- package/dist/protocol/dispatch.d.ts +12 -2
- package/dist/protocol/handlers/aggregate.d.ts +1 -1
- package/dist/protocol/handlers/catalog/shared.d.ts +10 -0
- package/dist/protocol/handlers/function.d.ts +23 -0
- package/dist/protocol/handlers/table-buffering.d.ts +1 -1
- package/dist/protocol/serialize.d.ts +1 -1
- package/dist/protocol/serializers/bind.d.ts +27 -1
- package/dist/protocol/serializers/cardinality.d.ts +1 -1
- package/dist/protocol/serializers/splits.d.ts +7 -0
- package/dist/protocol/state-serializer.d.ts +8 -7
- package/dist/protocol/stream-init.d.ts +17 -0
- package/dist/protocol/types.d.ts +32 -0
- package/dist/secrets/lookups.d.ts +31 -0
- package/dist/serve-entry.d.ts +41 -6
- package/dist/serve-entry.js +8531 -6302
- package/dist/serve-entry.js.map +52 -35
- package/dist/worker-cf-entry.d.ts +5 -0
- package/dist/worker-cf-entry.js +7191 -4990
- package/dist/worker-cf-entry.js.map +59 -41
- package/package.json +5 -4
- package/src/functions/aggregate.ts +101 -152
- package/src/functions/copy-from.ts +2 -2
- package/src/functions/execution-cache.ts +59 -0
- package/src/functions/storage-blobs.ts +654 -0
- package/src/functions/storage-cf-do.ts +176 -22
- package/src/functions/storage-seal.ts +208 -0
- package/src/functions/storage.ts +174 -8
- package/src/functions/table-buffering.ts +104 -18
- package/src/functions/table-in-out.ts +3 -1
- package/src/functions/table.ts +12 -8
- package/src/generated/vgi-client.ts +9 -2
- package/src/generated/vgi-protocol-schemas.ts +115 -107
- package/src/generated/vgi-protocol-types.ts +640 -3
- package/src/generated/vgi-service.ts +2 -2
- package/src/hosted.ts +254 -0
- package/src/http/blob-vendor.ts +80 -0
- package/src/http/fetch.ts +179 -8
- package/src/http/hosted-auth.ts +326 -0
- package/src/index.core.ts +66 -1
- package/src/protocol/arguments-ref.ts +260 -0
- package/src/protocol/dispatch.ts +15 -4
- package/src/protocol/handlers/aggregate.ts +123 -82
- package/src/protocol/handlers/catalog/shared.ts +20 -0
- package/src/protocol/handlers/function.ts +135 -59
- package/src/protocol/handlers/table-buffering.ts +49 -32
- package/src/protocol/serialize.ts +3 -0
- package/src/protocol/serializers/bind.ts +96 -69
- package/src/protocol/serializers/cardinality.ts +4 -7
- package/src/protocol/serializers/init.ts +5 -11
- package/src/protocol/serializers/splits.ts +31 -4
- package/src/protocol/state-serializer.ts +27 -39
- package/src/protocol/stream-init.ts +87 -0
- package/src/protocol/types.ts +33 -0
- package/src/secrets/lookups.ts +53 -0
- package/src/serve-entry.ts +138 -11
- package/src/worker-cf-entry.ts +33 -0
- package/src/worker.ts +25 -0
- package/dist/protocol/carried-init-request.d.ts +0 -28
- package/dist/util/zstd-sync.d.ts +0 -7
- package/src/protocol/carried-init-request.ts +0 -139
- package/src/util/zstd-sync.ts +0 -50
package/src/protocol/dispatch.ts
CHANGED
|
@@ -41,6 +41,15 @@ export interface ProtocolConfig {
|
|
|
41
41
|
* receive a given attach must use the same key.
|
|
42
42
|
*/
|
|
43
43
|
attachSecretKey?: Uint8Array | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* Input key material for sealed framework storage payloads
|
|
46
|
+
* (src/functions/storage-seal.ts). The cross-SDK v1 rule is the UTF-8 bytes
|
|
47
|
+
* of `VGI_SIGNING_KEY` exactly as configured (not its hex-decoded form), so a
|
|
48
|
+
* deployment's one key derives the same payload key in every SDK. Defaults
|
|
49
|
+
* to `signingKey` (bytes passed as bytes are used unchanged); with neither,
|
|
50
|
+
* the host-local key file. The attach / state-token keys are unaffected.
|
|
51
|
+
*/
|
|
52
|
+
storageSealKey?: Uint8Array | undefined;
|
|
44
53
|
}
|
|
45
54
|
|
|
46
55
|
/**
|
|
@@ -80,9 +89,10 @@ export { VGI_PROTOCOL_NAME };
|
|
|
80
89
|
* hash covers method names, types and the params/result/header schemas only
|
|
81
90
|
* (WIRE_PROTOCOL.md §14).
|
|
82
91
|
*
|
|
83
|
-
* It changes only with vgi.v3's
|
|
92
|
+
* It changes only with vgi.v3's method surface (3.3 removed
|
|
84
93
|
* catalog_table_scan_function_get and gave catalog_table_create a `created`
|
|
85
|
-
* result
|
|
94
|
+
* result; 3.4.0 changed the transport's upload URLs, not the surface, so the
|
|
95
|
+
* hash is unchanged). Asserted
|
|
86
96
|
* by src/protocol/__tests__/vgi-v3-hash.test.ts; if that test fails, the
|
|
87
97
|
* surface drifted from the reference -- fix the schemas, do not bump this.
|
|
88
98
|
*/
|
|
@@ -100,9 +110,10 @@ export function buildVgiService(config: ProtocolConfig): VgiService {
|
|
|
100
110
|
signingKey: config.signingKey,
|
|
101
111
|
attachSecretKey: config.attachSecretKey,
|
|
102
112
|
catalogInterface: config.catalogInterface,
|
|
113
|
+
storageSealKey: config.storageSealKey ?? config.signingKey,
|
|
103
114
|
}),
|
|
104
|
-
...aggregateHandlers(config.registry),
|
|
105
|
-
...tableBufferingHandlers(config.registry, config.signingKey, config.attachSecretKey),
|
|
115
|
+
...aggregateHandlers(config.registry, config.signingKey, config.attachSecretKey, config.storageSealKey ?? config.signingKey),
|
|
116
|
+
...tableBufferingHandlers(config.registry, config.signingKey, config.attachSecretKey, config.storageSealKey ?? config.signingKey),
|
|
106
117
|
...catalogHandlers(config.catalogInterface, config.signingKey, config.attachSecretKey),
|
|
107
118
|
};
|
|
108
119
|
}
|
|
@@ -24,19 +24,30 @@ import {
|
|
|
24
24
|
AggregateFinalizeResultSchema,
|
|
25
25
|
} from "../../generated/vgi-protocol-schemas.js";
|
|
26
26
|
import {
|
|
27
|
-
getExecutionState,
|
|
28
|
-
setExecutionState,
|
|
29
|
-
persistGroupStates,
|
|
30
27
|
GROUP_COLUMN_NAME,
|
|
28
|
+
AGGREGATE_CONTEXT_KEY,
|
|
29
|
+
aggregateContextCache,
|
|
30
|
+
encodeAggregateContext,
|
|
31
|
+
decodeAggregateContext,
|
|
32
|
+
loadGroupStates,
|
|
33
|
+
storeGroupStates,
|
|
31
34
|
type AggregateFunctionConfig,
|
|
32
35
|
type AggregateBindParams,
|
|
33
36
|
} from "../../functions/aggregate.js";
|
|
37
|
+
import { BoundStorage, FrameworkNS, storage as defaultStorage } from "../../functions/storage.js";
|
|
38
|
+
import { openAttachWithShard } from "./catalog/shared.js";
|
|
39
|
+
import { currentRequestAuth } from "../../request-auth.js";
|
|
34
40
|
import { Arguments } from "../../arguments/arguments.js";
|
|
35
41
|
import { validateConstConstraints } from "../../arguments/argument-spec.js";
|
|
36
42
|
import { unwrapRequest, wrapResult } from "./shared.js";
|
|
37
43
|
|
|
38
44
|
/** The aggregate lifecycle's `vgi.v3` handlers. */
|
|
39
|
-
export function aggregateHandlers(
|
|
45
|
+
export function aggregateHandlers(
|
|
46
|
+
registry: FunctionRegistry,
|
|
47
|
+
signingKey?: Uint8Array,
|
|
48
|
+
attachSecretKey?: Uint8Array,
|
|
49
|
+
storageSealKey: Uint8Array | undefined = signingKey,
|
|
50
|
+
): Partial<VgiService> {
|
|
40
51
|
// `schemaPath` scopes resolution to the schema the caller named. Every
|
|
41
52
|
// aggregate RPC carries it (protocol 1.2.0), so an aggregate name declared in
|
|
42
53
|
// two schemas dispatches to the one the request names rather than colliding
|
|
@@ -57,52 +68,96 @@ export function aggregateHandlers(registry: FunctionRegistry): Partial<VgiServic
|
|
|
57
68
|
return func.aggregateConfig as AggregateFunctionConfig<any, any>;
|
|
58
69
|
}
|
|
59
70
|
|
|
71
|
+
// The execution's storage: its attach's shard (derived from the request's
|
|
72
|
+
// authenticated attach, as for every other function kind), scoped by the
|
|
73
|
+
// execution id.
|
|
74
|
+
async function boundFor(innerParams: Record<string, any>, ctx: any, executionId: Uint8Array): Promise<BoundStorage> {
|
|
75
|
+
const raw = innerParams.attach_opaque_data ? toUint8Array(innerParams.attach_opaque_data) : null;
|
|
76
|
+
const { shardKey } = await openAttachWithShard(raw, ctx?.auth ?? currentRequestAuth(), signingKey, attachSecretKey);
|
|
77
|
+
return new BoundStorage(defaultStorage, executionId, null, shardKey);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// The bind context of an execution: cached in-process, else the sealed
|
|
81
|
+
// storage copy (authoritative). Throws when the execution is unknown.
|
|
82
|
+
async function loadContext(
|
|
83
|
+
cfg: AggregateFunctionConfig<any, any>,
|
|
84
|
+
storage: BoundStorage,
|
|
85
|
+
rpc: string,
|
|
86
|
+
): Promise<AggregateBindParams<any>> {
|
|
87
|
+
const cached = aggregateContextCache.get(storage.shardKey, storage.executionId);
|
|
88
|
+
if (cached) return cached;
|
|
89
|
+
const payload = await storage.sealedGet(FrameworkNS.AGGREGATE_STATE, AGGREGATE_CONTEXT_KEY, storageSealKey);
|
|
90
|
+
if (payload == null) {
|
|
91
|
+
throw new Error(`${rpc}: unknown execution_id for '${cfg.name}' (bind missing or state dropped)`);
|
|
92
|
+
}
|
|
93
|
+
const stored = decodeAggregateContext(payload);
|
|
94
|
+
const args = deserializeArguments(stored.argumentsIpc);
|
|
95
|
+
const params: AggregateBindParams<any> = {
|
|
96
|
+
args: extractArgMap(cfg, args),
|
|
97
|
+
arguments: args,
|
|
98
|
+
inputSchema: stored.inputSchemaIpc ? deserializeSchema(stored.inputSchemaIpc) : null,
|
|
99
|
+
settings: {},
|
|
100
|
+
// Secret values are delivered on aggregate_bind only, and never stored.
|
|
101
|
+
secrets: {},
|
|
102
|
+
argumentNames: stored.argumentNames,
|
|
103
|
+
};
|
|
104
|
+
aggregateContextCache.set(storage.shardKey, storage.executionId, params);
|
|
105
|
+
return params;
|
|
106
|
+
}
|
|
107
|
+
|
|
60
108
|
// ------------------------------------------------------------------------
|
|
61
109
|
// aggregate_bind — allocate execution_id, return output schema
|
|
62
110
|
// ------------------------------------------------------------------------
|
|
63
|
-
const aggregateBind: VgiService["aggregateBind"] = async (params) => {
|
|
111
|
+
const aggregateBind: VgiService["aggregateBind"] = async (params, ctx?: any) => {
|
|
64
112
|
const innerParams = unwrapRequest(params.request);
|
|
65
113
|
const functionName: string = innerParams.function_name;
|
|
66
114
|
const cfg = resolveAggregate(functionName, innerParams.schema_path ?? null);
|
|
67
115
|
const argBytes = toUint8Array(innerParams.arguments);
|
|
68
116
|
const args = deserializeArguments(argBytes);
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
: null;
|
|
117
|
+
const inputSchemaIpc = innerParams.input_schema ? toUint8Array(innerParams.input_schema) : null;
|
|
118
|
+
const inputSchema = inputSchemaIpc && inputSchemaIpc.length > 0 ? deserializeSchema(inputSchemaIpc) : null;
|
|
72
119
|
// Decode the resolved-secrets batch the extension pre-resolves for
|
|
73
120
|
// aggregates that declared `requiredSecrets`. Mirrors the table-buffering
|
|
74
121
|
// handler (batchToSecretDict). The secret VALUES are delivered only here,
|
|
75
|
-
// at bind time — update/combine/finalize see no secrets by design
|
|
122
|
+
// at bind time — update/combine/finalize see no secrets by design, and
|
|
123
|
+
// they are never written to storage.
|
|
76
124
|
const secretsBytes = innerParams.secrets ? toUint8Array(innerParams.secrets) : null;
|
|
77
125
|
const secretsBatch =
|
|
78
126
|
secretsBytes && secretsBytes.length > 0 ? deserializeBatch(secretsBytes) : null;
|
|
79
127
|
const secrets = secretsBatch ? batchToSecretDict(secretsBatch) : {};
|
|
128
|
+
const argumentNames: (string | null)[] | null = innerParams.argument_names
|
|
129
|
+
? (Array.isArray(innerParams.argument_names)
|
|
130
|
+
? innerParams.argument_names
|
|
131
|
+
: [...innerParams.argument_names])
|
|
132
|
+
.map((value: unknown) => value == null ? null : String(value))
|
|
133
|
+
: null;
|
|
80
134
|
const bindParams: AggregateBindParams<any> = {
|
|
81
135
|
args: extractArgMap(cfg, args),
|
|
82
136
|
arguments: args,
|
|
83
137
|
inputSchema,
|
|
84
138
|
settings: {},
|
|
85
139
|
secrets,
|
|
86
|
-
argumentNames
|
|
87
|
-
? (Array.isArray(innerParams.argument_names)
|
|
88
|
-
? innerParams.argument_names
|
|
89
|
-
: [...innerParams.argument_names])
|
|
90
|
-
.map((value: unknown) => value == null ? null : String(value))
|
|
91
|
-
: null,
|
|
140
|
+
argumentNames,
|
|
92
141
|
};
|
|
93
|
-
const executionId = new Uint8Array(16);
|
|
94
|
-
crypto.getRandomValues(executionId);
|
|
95
|
-
setExecutionState(executionId, {
|
|
96
|
-
states: new Map(),
|
|
97
|
-
bindArgs: args,
|
|
98
|
-
bindParams,
|
|
99
|
-
});
|
|
100
142
|
// Output schema is a single "result" column. Type comes from the
|
|
101
143
|
// optional onBind hook (for aggregates whose return type depends on
|
|
102
144
|
// input — e.g. vgi_generic_sum takes ANY and returns the same type);
|
|
103
145
|
// otherwise the declared cfg.outputType.
|
|
104
146
|
const outType = cfg.onBind ? await cfg.onBind(bindParams) : cfg.outputType;
|
|
105
147
|
const outSchema = makeSchema([field("result", outType, true)]);
|
|
148
|
+
|
|
149
|
+
const executionId = new Uint8Array(16);
|
|
150
|
+
crypto.getRandomValues(executionId);
|
|
151
|
+
// Persist the bind context (sealed; no secrets) so any worker can serve
|
|
152
|
+
// this execution's update/combine/finalize RPCs.
|
|
153
|
+
const storage = await boundFor(innerParams, ctx, executionId);
|
|
154
|
+
await storage.sealedPut(
|
|
155
|
+
FrameworkNS.AGGREGATE_STATE,
|
|
156
|
+
AGGREGATE_CONTEXT_KEY,
|
|
157
|
+
encodeAggregateContext({ argumentsIpc: argBytes, inputSchemaIpc, argumentNames }),
|
|
158
|
+
storageSealKey,
|
|
159
|
+
);
|
|
160
|
+
aggregateContextCache.set(storage.shardKey, executionId, { ...bindParams, secrets: {} });
|
|
106
161
|
return wrapResult(
|
|
107
162
|
{ output_schema: serializeSchema(outSchema), execution_id: executionId },
|
|
108
163
|
AggregateBindResultSchema,
|
|
@@ -112,7 +167,7 @@ export function aggregateHandlers(registry: FunctionRegistry): Partial<VgiServic
|
|
|
112
167
|
// ------------------------------------------------------------------------
|
|
113
168
|
// aggregate_update — fold the incoming batch into per-group state
|
|
114
169
|
// ------------------------------------------------------------------------
|
|
115
|
-
const aggregateUpdate: VgiService["aggregateUpdate"] = (params) => {
|
|
170
|
+
const aggregateUpdate: VgiService["aggregateUpdate"] = async (params, ctx?: any) => {
|
|
116
171
|
const innerParams = unwrapRequest(params.request);
|
|
117
172
|
const functionName: string = innerParams.function_name;
|
|
118
173
|
const executionId = toUint8Array(innerParams.execution_id);
|
|
@@ -133,10 +188,13 @@ export function aggregateHandlers(registry: FunctionRegistry): Partial<VgiServic
|
|
|
133
188
|
groupIds.push(g);
|
|
134
189
|
uniqueGids.add(g);
|
|
135
190
|
}
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
191
|
+
const storage = await boundFor(innerParams, ctx, executionId);
|
|
192
|
+
const bindParams = await loadContext(cfg, storage, "aggregate_update");
|
|
193
|
+
// One batched read of every group this batch touches. Always from
|
|
194
|
+
// storage: a sibling worker may have just written a newer value, and a
|
|
195
|
+
// cached copy would overwrite it (read-modify-write race).
|
|
196
|
+
const gids = [...uniqueGids];
|
|
197
|
+
const states = await loadGroupStates(storage, gids);
|
|
140
198
|
|
|
141
199
|
// Build column-arg arrays — every non-__vgi_group_id column, in
|
|
142
200
|
// schema order — and pass to user's update(). We deliberately do NOT
|
|
@@ -149,33 +207,30 @@ export function aggregateHandlers(registry: FunctionRegistry): Partial<VgiServic
|
|
|
149
207
|
columns.push(batch.getChildAt(i));
|
|
150
208
|
}
|
|
151
209
|
const ensureState = (gid: bigint) => {
|
|
152
|
-
let s =
|
|
210
|
+
let s = states.get(gid);
|
|
153
211
|
if (s === undefined) {
|
|
154
|
-
s = cfg.initialState(
|
|
155
|
-
|
|
212
|
+
s = cfg.initialState(bindParams);
|
|
213
|
+
states.set(gid, s);
|
|
156
214
|
}
|
|
157
215
|
return s;
|
|
158
216
|
};
|
|
159
217
|
cfg.update({
|
|
160
|
-
states
|
|
218
|
+
states,
|
|
161
219
|
groupIds,
|
|
162
220
|
columns,
|
|
163
|
-
args:
|
|
221
|
+
args: bindParams.args,
|
|
164
222
|
ensureState,
|
|
165
223
|
});
|
|
166
|
-
//
|
|
167
|
-
//
|
|
168
|
-
|
|
169
|
-
// the pre-existing initial state; the idempotent rewrite is cheap
|
|
170
|
-
// relative to the RPC overhead.
|
|
171
|
-
persistGroupStates(executionId, uniqueGids);
|
|
224
|
+
// One batched write of every referenced group that has a state (groups
|
|
225
|
+
// skipped by NULL handling stay absent, so finalize sees null).
|
|
226
|
+
await storeGroupStates(storage, states, gids);
|
|
172
227
|
return wrapResult({}, makeSchema([]));
|
|
173
228
|
};
|
|
174
229
|
|
|
175
230
|
// ------------------------------------------------------------------------
|
|
176
231
|
// aggregate_combine — merge (source_group_id, target_group_id) pairs
|
|
177
232
|
// ------------------------------------------------------------------------
|
|
178
|
-
const aggregateCombine: VgiService["aggregateCombine"] = (params) => {
|
|
233
|
+
const aggregateCombine: VgiService["aggregateCombine"] = async (params, ctx?: any) => {
|
|
179
234
|
const innerParams = unwrapRequest(params.request);
|
|
180
235
|
const functionName: string = innerParams.function_name;
|
|
181
236
|
const executionId = toUint8Array(innerParams.execution_id);
|
|
@@ -194,33 +249,29 @@ export function aggregateHandlers(registry: FunctionRegistry): Partial<VgiServic
|
|
|
194
249
|
srcIds.push(src); tgtIds.push(tgt);
|
|
195
250
|
referencedGids.add(src); referencedGids.add(tgt);
|
|
196
251
|
}
|
|
197
|
-
const
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
`aggregate_combine: unknown execution_id for '${functionName}' ` +
|
|
201
|
-
`(context missing on disk — bind may have failed or been cleaned up)`,
|
|
202
|
-
);
|
|
203
|
-
}
|
|
252
|
+
const storage = await boundFor(innerParams, ctx, executionId);
|
|
253
|
+
const bindParams = await loadContext(cfg, storage, "aggregate_combine");
|
|
254
|
+
const states = await loadGroupStates(storage, [...referencedGids]);
|
|
204
255
|
const touchedTargets = new Set<bigint>();
|
|
205
256
|
for (let i = 0; i < batch.numRows; i++) {
|
|
206
257
|
const src = srcIds[i]!;
|
|
207
258
|
const tgt = tgtIds[i]!;
|
|
208
|
-
const srcHas =
|
|
209
|
-
const tgtHas =
|
|
259
|
+
const srcHas = states.has(src);
|
|
260
|
+
const tgtHas = states.has(tgt);
|
|
210
261
|
if (!srcHas && !tgtHas) continue;
|
|
211
|
-
const srcState = srcHas ?
|
|
212
|
-
const tgtState = tgtHas ?
|
|
213
|
-
|
|
262
|
+
const srcState = srcHas ? states.get(src)! : cfg.initialState(bindParams);
|
|
263
|
+
const tgtState = tgtHas ? states.get(tgt)! : cfg.initialState(bindParams);
|
|
264
|
+
states.set(tgt, cfg.combine(srcState, tgtState, bindParams));
|
|
214
265
|
touchedTargets.add(tgt);
|
|
215
266
|
}
|
|
216
|
-
|
|
267
|
+
await storeGroupStates(storage, states, touchedTargets);
|
|
217
268
|
return wrapResult({}, makeSchema([]));
|
|
218
269
|
};
|
|
219
270
|
|
|
220
271
|
// ------------------------------------------------------------------------
|
|
221
272
|
// aggregate_finalize — emit one result row per requested group_id
|
|
222
273
|
// ------------------------------------------------------------------------
|
|
223
|
-
const aggregateFinalize: VgiService["aggregateFinalize"] = (params) => {
|
|
274
|
+
const aggregateFinalize: VgiService["aggregateFinalize"] = async (params, ctx?: any) => {
|
|
224
275
|
const innerParams = unwrapRequest(params.request);
|
|
225
276
|
const functionName: string = innerParams.function_name;
|
|
226
277
|
const executionId = toUint8Array(innerParams.execution_id);
|
|
@@ -238,18 +289,17 @@ export function aggregateHandlers(registry: FunctionRegistry): Partial<VgiServic
|
|
|
238
289
|
uniqueGids.add(g);
|
|
239
290
|
}
|
|
240
291
|
}
|
|
241
|
-
const
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
}
|
|
292
|
+
const storage = await boundFor(innerParams, ctx, executionId);
|
|
293
|
+
const bindParams = await loadContext(cfg, storage, "aggregate_finalize");
|
|
294
|
+
const stored = await loadGroupStates(storage, [...uniqueGids]);
|
|
245
295
|
// Groups never updated pass state = null so finalize() can emit SQL NULL
|
|
246
296
|
// (matches Python semantics for SUM/AVG/MIN/MAX over zero rows).
|
|
247
297
|
const states = new Map<bigint, any | null>();
|
|
248
298
|
for (const gid of groupIds) {
|
|
249
|
-
states.set(gid,
|
|
299
|
+
states.set(gid, stored.has(gid) ? stored.get(gid) : null);
|
|
250
300
|
}
|
|
251
301
|
const resultBatch = cfg.finalize({
|
|
252
|
-
groupIds, states, outputSchema, args:
|
|
302
|
+
groupIds, states, outputSchema, args: bindParams.args,
|
|
253
303
|
});
|
|
254
304
|
return wrapResult(
|
|
255
305
|
{ result_batch: serializeBatchWithSchema(resultBatch) },
|
|
@@ -258,32 +308,23 @@ export function aggregateHandlers(registry: FunctionRegistry): Partial<VgiServic
|
|
|
258
308
|
};
|
|
259
309
|
|
|
260
310
|
// ------------------------------------------------------------------------
|
|
261
|
-
// aggregate_destructor — drop
|
|
311
|
+
// aggregate_destructor — drop the execution's state (best-effort)
|
|
262
312
|
//
|
|
263
|
-
//
|
|
264
|
-
//
|
|
265
|
-
//
|
|
266
|
-
//
|
|
267
|
-
// up when the worker process exits).
|
|
313
|
+
// The extension calls this once, when every group state of the execution
|
|
314
|
+
// has been destroyed (it counts destroys against allocated group ids), so
|
|
315
|
+
// it clears the whole execution -- group states and the sealed bind
|
|
316
|
+
// context -- and evicts the in-process cache, as vgi-python does.
|
|
268
317
|
// ------------------------------------------------------------------------
|
|
269
|
-
const aggregateDestructor: VgiService["aggregateDestructor"] = (params) => {
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
for (let i = 0; i < gidBatch.numRows; i++) {
|
|
279
|
-
const v = col.get(i);
|
|
280
|
-
const g = typeof v === "bigint" ? v : BigInt(v as any);
|
|
281
|
-
exec.states.delete(g);
|
|
282
|
-
toDrop.add(g);
|
|
318
|
+
const aggregateDestructor: VgiService["aggregateDestructor"] = async (params, ctx?: any) => {
|
|
319
|
+
try {
|
|
320
|
+
const innerParams = unwrapRequest(params.request);
|
|
321
|
+
const executionId = toUint8Array(innerParams.execution_id);
|
|
322
|
+
const storage = await boundFor(innerParams, ctx, executionId);
|
|
323
|
+
aggregateContextCache.delete(storage.shardKey, executionId);
|
|
324
|
+
await storage.executionClear();
|
|
325
|
+
} catch {
|
|
326
|
+
// Teardown path — best-effort.
|
|
283
327
|
}
|
|
284
|
-
// Persist deletions (unlinks the per-group file so sibling workers
|
|
285
|
-
// don't revive the state via getExecutionState's lazy disk load).
|
|
286
|
-
persistGroupStates(executionId, toDrop);
|
|
287
328
|
return wrapResult({}, makeSchema([]));
|
|
288
329
|
};
|
|
289
330
|
|
|
@@ -119,6 +119,26 @@ export async function openAttach(
|
|
|
119
119
|
return openFrameworkAttach(full, { explicit: attachSecretKey, signingKey });
|
|
120
120
|
}
|
|
121
121
|
|
|
122
|
+
/**
|
|
123
|
+
* Open an attach envelope once, returning both what the function body sees
|
|
124
|
+
* (the catalog's own bytes, UUID and options trailer stripped) and the
|
|
125
|
+
* FunctionStorage shard key of its framework UUID. An empty / absent attach
|
|
126
|
+
* yields `{ attach: <as given>, shardKey: "" }`.
|
|
127
|
+
*/
|
|
128
|
+
export async function openAttachWithShard(
|
|
129
|
+
envelope: Uint8Array | null | undefined,
|
|
130
|
+
auth: AuthContext | undefined,
|
|
131
|
+
signingKey: Uint8Array | undefined,
|
|
132
|
+
attachSecretKey?: Uint8Array,
|
|
133
|
+
): Promise<{ attach: Uint8Array | null; shardKey: string }> {
|
|
134
|
+
if (envelope == null) return { attach: null, shardKey: "" };
|
|
135
|
+
if (envelope.length === 0) return { attach: envelope, shardKey: "" };
|
|
136
|
+
const full = await openAttachFull(envelope, auth, signingKey);
|
|
137
|
+
const shardKey = full.length >= ATTACH_UUID_LEN ? deriveShardKey(full.subarray(0, ATTACH_UUID_LEN)) : "";
|
|
138
|
+
const attach = await openFrameworkAttach(full, { explicit: attachSecretKey, signingKey });
|
|
139
|
+
return { attach, shardKey };
|
|
140
|
+
}
|
|
141
|
+
|
|
122
142
|
/** Hex of a byte array (lowercase). */
|
|
123
143
|
function toHex(b: Uint8Array): string {
|
|
124
144
|
let s = "";
|