Skip to content

Commit eba1917

Browse files
authored
Metadata in compute jobs (#976)
* metadata * remove editor spaces * lint fix * test and readme
1 parent eb432d3 commit eba1917

9 files changed

Lines changed: 68 additions & 27 deletions

File tree

docs/API.md

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,17 +1275,18 @@ starts a free compute job and returns jobId if succesfull
12751275

12761276
#### Parameters
12771277

1278-
| name | type | required | description |
1279-
| --------------- | ------ | -------- | ----------------------------------------- |
1280-
| command | string | v | command name |
1281-
| node | string | | if not present it means current node |
1282-
| consumerAddress | string | v | consumer address |
1283-
| signature | string | v | signature (msg=String(nonce) ) |
1284-
| nonce | string | v | nonce for the request |
1285-
| datasets | object | | list of ComputeAsset to be used as inputs |
1286-
| algorithm | object | | ComputeAlgorithm definition |
1287-
| environment | string | v | compute environment to use |
1288-
| resources | object | | optional list of required resources |
1278+
| name | type | required | description |
1279+
| --------------- | ------ | -------- | -------------------------------------------------------- |
1280+
| command | string | v | command name |
1281+
| node | string | | if not present it means current node |
1282+
| consumerAddress | string | v | consumer address |
1283+
| signature | string | v | signature (msg=String(nonce) ) |
1284+
| nonce | string | v | nonce for the request |
1285+
| datasets | object | | list of ComputeAsset to be used as inputs |
1286+
| algorithm | object | | ComputeAlgorithm definition |
1287+
| environment | string | v | compute environment to use |
1288+
| resources | object | | optional list of required resources |
1289+
| metadata | object | | optional metadata for the job, data provided by the user |
12891290

12901291
#### Request
12911292

@@ -1300,7 +1301,8 @@ starts a free compute job and returns jobId if succesfull
13001301
"signature": "123",
13011302
"nonce": 1,
13021303
"environment": "0x7d187e4c751367be694497ead35e2937ece3c7f3b325dcb4f7571e5972d092bd-0xbeaf12703d708f39ef98c3d8939ce458553254176dbb69fe83d535883c4cee38",
1303-
"resources": [{ "id": "cpu", "amount": 1 }]
1304+
"resources": [{ "id": "cpu", "amount": 1 }],
1305+
"metadata": { "key": "value" }
13041306
}
13051307
```
13061308

@@ -1324,7 +1326,8 @@ starts a free compute job and returns jobId if succesfull
13241326
{ "id": "ram", "amount": 1000000000 },
13251327
{ "id": "disk", "amount": 0 }
13261328
],
1327-
"isFree": true
1329+
"isFree": true,
1330+
"metadata": { "key": "value" }
13281331
}
13291332
]
13301333
```
@@ -1338,6 +1341,7 @@ starts a free compute job and returns jobId if succesfull
13381341
returns job status
13391342

13401343
#### Parameters
1344+
13411345
Required at least one of the following parameters:
13421346

13431347
| name | type | required | description |
@@ -1378,7 +1382,8 @@ Required at least one of the following parameters:
13781382
"amount": 1000000000
13791383
}
13801384
],
1381-
"isFree": true
1385+
"isFree": true,
1386+
"metadata": { "key": "value" }
13821387
}
13831388
]
13841389
```

src/@types/C2D/C2D.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ export interface ComputeResult {
141141
index?: number
142142
}
143143

144+
export type DBComputeJobMetadata = {
145+
[key: string]: string | number | boolean
146+
}
144147
export interface ComputeJob {
145148
owner: string
146149
did?: string
@@ -155,6 +158,7 @@ export interface ComputeJob {
155158
maxJobDuration?: number
156159
agreementId?: string
157160
environment?: string
161+
metadata?: DBComputeJobMetadata
158162
}
159163

160164
export interface ComputeOutput {
@@ -198,6 +202,7 @@ export interface DBComputeJobPayment {
198202
lockTx: string
199203
claimTx: string
200204
}
205+
201206
// this is the internal structure
202207
export interface DBComputeJob extends ComputeJob {
203208
clusterHash: string
@@ -216,6 +221,7 @@ export interface DBComputeJob extends ComputeJob {
216221
algoStopTimestamp: string
217222
resources: ComputeResourceRequestWithPrice[]
218223
payment?: DBComputeJobPayment
224+
metadata?: DBComputeJobMetadata
219225
}
220226

221227
// make sure we keep them both in sync

src/@types/commands.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import type {
55
ComputeAsset,
66
ComputeAlgorithm,
77
ComputeOutput,
8-
ComputeResourceRequest
8+
ComputeResourceRequest,
9+
DBComputeJobMetadata
910
} from './C2D/C2D.js'
1011
import {
1112
ArweaveFileObject,
@@ -200,6 +201,7 @@ export interface FreeComputeStartCommand extends Command {
200201
output?: ComputeOutput
201202
resources?: ComputeResourceRequest[]
202203
maxJobDuration?: number
204+
metadata?: DBComputeJobMetadata
203205
}
204206
export interface PaidComputeStartCommand extends FreeComputeStartCommand {
205207
payment: ComputePayment

src/components/c2d/compute_engine_base.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import type {
1313
ComputeResourcesPricingInfo,
1414
DBComputeJobPayment,
1515
DBComputeJob,
16-
dockerDeviceRequest
16+
dockerDeviceRequest,
17+
DBComputeJobMetadata
1718
} from '../../@types/C2D/C2D.js'
1819
import { C2DClusterType } from '../../@types/C2D/C2D.js'
1920
import { C2DDatabase } from '../database/C2DDatabase.js'
@@ -61,7 +62,8 @@ export abstract class C2DEngine {
6162
maxJobDuration: number,
6263
resources: ComputeResourceRequest[],
6364
payment: DBComputeJobPayment,
64-
jobId: string
65+
jobId: string,
66+
metadata?: DBComputeJobMetadata
6567
): Promise<ComputeJob[]>
6668

6769
public abstract stopComputeJob(

src/components/c2d/compute_engine_docker.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
/* eslint-disable security/detect-non-literal-fs-filename */
22
import { Readable } from 'stream'
3-
import { C2DStatusNumber, C2DStatusText } from '../../@types/C2D/C2D.js'
3+
import {
4+
C2DStatusNumber,
5+
C2DStatusText,
6+
DBComputeJobMetadata
7+
} from '../../@types/C2D/C2D.js'
48
import type {
59
C2DClusterInfo,
610
ComputeEnvironment,
@@ -354,7 +358,8 @@ export class C2DEngineDocker extends C2DEngine {
354358
maxJobDuration: number,
355359
resources: ComputeResourceRequest[],
356360
payment: DBComputeJobPayment,
357-
jobId: string
361+
jobId: string,
362+
metadata?: DBComputeJobMetadata
358363
): Promise<ComputeJob[]> {
359364
if (!this.docker) return []
360365
// TO DO - iterate over resources and get default runtime
@@ -371,6 +376,14 @@ export class C2DEngineDocker extends C2DEngine {
371376
)}`
372377
)
373378
}
379+
380+
if (metadata && Object.keys(metadata).length > 0) {
381+
const metadataSize = JSON.stringify(metadata).length
382+
if (metadataSize > 1024) {
383+
throw new Error('Metadata size is too large')
384+
}
385+
}
386+
374387
const envIdWithHash = environment && environment.indexOf('-') > -1
375388
const env = await this.getComputeEnvironment(
376389
payment && payment.chainId ? payment.chainId : null,
@@ -408,7 +421,8 @@ export class C2DEngineDocker extends C2DEngine {
408421
isFree,
409422
algoStartTimestamp: '0',
410423
algoStopTimestamp: '0',
411-
payment
424+
payment,
425+
metadata
412426
}
413427
await this.makeJobFolders(job)
414428
// make sure we actually were able to insert on DB

src/components/core/compute/startCompute.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,8 @@ export class PaidComputeStartHandler extends CommandHandler {
366366
maxJobDuration: task.maxJobDuration,
367367
chainId: task.payment.chainId,
368368
agreementId: '',
369-
resources
369+
resources,
370+
metadata: task.metadata
370371
}
371372
// job ID unicity
372373
const jobId = generateUniqueID(s)
@@ -412,7 +413,8 @@ export class PaidComputeStartHandler extends CommandHandler {
412413
lockTx: agreementId,
413414
claimTx: null
414415
},
415-
jobId
416+
jobId,
417+
task.metadata
416418
)
417419
CORE_LOGGER.logMessage(
418420
'ComputeStartCommand Response: ' + JSON.stringify(response, null, 2),
@@ -566,7 +568,8 @@ export class FreeComputeStartHandler extends CommandHandler {
566568
environment: task.environment,
567569
owner: task.consumerAddress,
568570
maxJobDuration: task.maxJobDuration,
569-
resources: task.resources
571+
resources: task.resources,
572+
metadata: task.metadata
570573
}
571574
const jobId = generateUniqueID(s)
572575
const response = await engine.startComputeJob(
@@ -578,7 +581,8 @@ export class FreeComputeStartHandler extends CommandHandler {
578581
task.maxJobDuration,
579582
task.resources,
580583
null,
581-
jobId
584+
jobId,
585+
task.metadata
582586
)
583587

584588
CORE_LOGGER.logMessage(

src/components/database/sqliteCompute.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ function getInternalStructure(job: DBComputeJob): any {
4444
resources: job.resources,
4545
isFree: job.isFree,
4646
algoStartTimestamp: job.algoStartTimestamp,
47-
algoStopTimestamp: job.algoStopTimestamp
47+
algoStopTimestamp: job.algoStopTimestamp,
48+
metadata: job.metadata
4849
}
4950
return internalBlob
5051
}
@@ -155,7 +156,8 @@ export class SQLiteCompute implements ComputeDatabaseProvider {
155156
maxJobDuration: job.maxJobDuration,
156157
chainId: job.payment?.chainId || null,
157158
agreementId: job.agreementId,
158-
resources: job.resources
159+
resources: job.resources,
160+
metadata: job.metadata
159161
}
160162
jobId = generateUniqueID(jobStructure)
161163
job.jobId = jobId

src/components/httpRoutes/compute.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ computeRoutes.post(`${SERVICES_API_BASE_PATH}/compute`, async (req, res) => {
7777
datasets: (req.body.datasets as unknown as ComputeAsset[]) || null,
7878
payment: (req.body.payment as unknown as ComputePayment) || null,
7979
resources: (req.body.resources as unknown as ComputeResourceRequest[]) || null,
80+
metadata: req.body.metadata || null,
8081
authorization: req.headers?.authorization
8182
}
8283
if (req.body.output) {
@@ -119,6 +120,7 @@ computeRoutes.post(`${SERVICES_API_BASE_PATH}/freeCompute`, async (req, res) =>
119120
datasets: (req.body.datasets as unknown as ComputeAsset[]) || null,
120121
resources: (req.body.resources as unknown as ComputeResourceRequest[]) || null,
121122
maxJobDuration: req.body.maxJobDuration || null,
123+
metadata: req.body.metadata || null,
122124
authorization: req.headers?.authorization
123125
}
124126
if (req.body.output) {

src/test/integration/compute.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,10 @@ describe('Compute', () => {
884884
transferTxId: algoOrderTxId,
885885
meta: publishedAlgoDataset.ddo.metadata.algorithm
886886
},
887-
output: {}
887+
output: {},
888+
metadata: {
889+
key: 'value'
890+
}
888891
// additionalDatasets?: ComputeAsset[]
889892
// output?: ComputeOutput
890893
}
@@ -916,6 +919,7 @@ describe('Compute', () => {
916919
assert(response.stream, 'Failed to get stream')
917920
expect(response.stream).to.be.instanceOf(Readable)
918921
const jobs = await streamToObject(response.stream as Readable)
922+
expect(jobs[0].metadata).to.deep.equal({ key: 'value' })
919923
console.log(jobs)
920924
})
921925

0 commit comments

Comments
 (0)