Skip to content

Commit

Permalink
feat: aggregator keeping oldest piece ts
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Nov 27, 2023
1 parent e8bffe2 commit a101b3d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
8 changes: 8 additions & 0 deletions packages/filecoin-api/src/aggregator/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ export interface AggregateRecord {
* Insertion date ISO string.
*/
insertedAt: string
/**
* ISO string date of oldest piece in the pipeline included into the aggregate.
*/
oldestPieceInsertedAt: string
}

// TODO: probably group should also be key!
Expand Down Expand Up @@ -314,6 +318,10 @@ export interface AggregateOfferMessage {
* Grouping information for submitted piece.
*/
group: string
/**
* ISO string date of oldest piece in the pipeline included into the aggregate.
*/
oldestPieceInsertedAt: string
}

export interface AggregateConfig {
Expand Down
10 changes: 10 additions & 0 deletions packages/filecoin-api/src/aggregator/buffer-reducing.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ export async function handleBufferReducingWithAggregate({
)
const aggregateBlock = await CBOR.write(aggregateReducedBuffer)

// Get timestamp of oldest piece in the pipeline included in the aggregate
const oldestPieceInsertedAtDate = new Date(
Math.min(
...aggregateInfo.addedBufferedPieces.map((bf) =>
new Date(bf.insertedAt).getTime()
)
)
)

// Store buffered pieces for aggregate
const bufferStoreAggregatePut = await bufferStore.put({
buffer: aggregateReducedBuffer,
Expand All @@ -65,6 +74,7 @@ export async function handleBufferReducingWithAggregate({
buffer: aggregateBlock.cid,
pieces: piecesBlock.cid,
group,
oldestPieceInsertedAt: oldestPieceInsertedAtDate.toISOString(),
})
if (aggregateOfferQueueAdd.error) {
return aggregateOfferQueueAdd
Expand Down
3 changes: 2 additions & 1 deletion packages/filecoin-api/src/aggregator/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,15 @@ export const handleBufferQueueMessage = async (context, records) => {
* @param {import('./api.js').AggregateOfferMessage} message
*/
export const handleAggregateOfferMessage = async (context, message) => {
const { pieces, aggregate, buffer, group } = message
const { pieces, aggregate, buffer, group, oldestPieceInsertedAt } = message

// Store aggregate information into the store. Store events MAY be used to propagate aggregate over
const putRes = await context.aggregateStore.put({
pieces,
aggregate,
buffer,
group,
oldestPieceInsertedAt,
insertedAt: new Date().toISOString(),
})

Expand Down
15 changes: 14 additions & 1 deletion packages/filecoin-api/test/events/aggregator.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,26 @@ export const test = {
/** @type {AggregateOfferMessage} */
// @ts-expect-error cannot infer buffer message
const message = context.queuedMessages.get('aggregateOfferQueue')?.[0]

const bufferGet = await context.bufferStore.get(message.buffer)
assert.ok(bufferGet.ok)
assert.ok(bufferGet.ok?.block.equals(message.buffer))
assert.equal(bufferGet.ok?.buffer.group, group)
assert.ok(message.aggregate.equals(bufferGet.ok?.buffer.aggregate))
assert.equal(bufferGet.ok?.buffer.pieces.length, totalPieces)
// Validate oldest piece date
assert.ok(message.oldestPieceInsertedAt)

const oldestPieceInsertedAtDate = new Date(
Math.min(
...(bufferGet.ok?.buffer.pieces?.map((bf) =>
new Date(bf.insertedAt).getTime()
) || [])
)
)
assert.equal(
oldestPieceInsertedAtDate.toISOString(),
message.oldestPieceInsertedAt
)
},
'handles buffer queue messages successfully to queue aggregate and remaining buffer':
async (assert, context) => {
Expand Down

0 comments on commit a101b3d

Please sign in to comment.