Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add round buffer. Closes #3 #342

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ primary_region = "cdg"

[deploy]
strategy = "rolling"

[mount]
source = "spark-evaluate-data"
destination = "/var/lib/spark-evaluate/"
15 changes: 15 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { evaluate } from './lib/evaluate.js'
import { RoundData } from './lib/round.js'
import { refreshDatabase } from './lib/platform-stats.js'
import timers from 'node:timers/promises'
import { recoverRound, clearRoundBuffer } from './lib/round-buffer.js'

// Tweak this value to improve the chances of the data being available
const PREPROCESS_DELAY = 60_000
Expand All @@ -30,6 +31,13 @@ export const startEvaluate = async ({
const roundsSeen = []
let lastNewEventSeenAt = null

try {
rounds.current = await recoverRound()
} catch (err) {
console.error('CANNOT RECOVER ROUND:', err)
Sentry.captureException(err)
}

const onMeasurementsAdded = async (cid, _roundIndex) => {
const roundIndex = BigInt(_roundIndex)
if (cidsSeen.includes(cid)) return
Expand Down Expand Up @@ -89,6 +97,13 @@ export const startEvaluate = async ({

console.log('Event: RoundStart', { roundIndex })

try {
await clearRoundBuffer()
} catch (err) {
console.error('CANNOT CLEAR ROUND BUFFER:', err)
Sentry.captureException(err)
}

if (!rounds.current) {
console.error('No current round data available, skipping evaluation')
return
Expand Down
2 changes: 2 additions & 0 deletions lib/preprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { validateBlock } from '@web3-storage/car-block-validator'
import { recursive as exporter } from 'ipfs-unixfs-exporter'
import createDebug from 'debug'
import pRetry from 'p-retry'
import { appendToRoundBuffer } from './round-buffer.js'

const debug = createDebug('spark:preprocess')

Expand Down Expand Up @@ -116,6 +117,7 @@ export const preprocess = async ({
logger.log('Retrieval Success Rate: %s%s (%s of %s)', Math.round(100 * okCount / total), '%', okCount, total)

round.measurements.push(...validMeasurements)
await appendToRoundBuffer(validMeasurements)

recordTelemetry('preprocess', point => {
point.intField('round_index', roundIndex)
Expand Down
44 changes: 44 additions & 0 deletions lib/round-buffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import fs from 'node:fs/promises'
import { RoundData } from './round.js'

const ROUND_BUFFER_PATH = '/var/lib/spark-evaluate/round-buffer.ndjson'

// TODO: Handle when it's from the wrong round
export const recoverRound = async () => {
let roundBuffer
try {
roundBuffer = await fs.readFile(ROUND_BUFFER_PATH, 'utf8')
} catch (err) {
if (err.code !== 'ENOENT') {
throw err
}
}
if (roundBuffer) {
const lines = roundBuffer.split('\n').filter(Boolean)
if (lines.length > 1) {
const round = new RoundData(JSON.parse(lines[0]))
for (const line of lines.slice(1)) {
round.measurements.push(JSON.parse(line))
}
return round
}
}
}

export const appendToRoundBuffer = async validMeasurements => {
await fs.appendFile(
ROUND_BUFFER_PATH,
validMeasurements.map(m => JSON.stringify(m)).join('\n') + '\n'
)
}

// TODO: Write round index as first line
export const clearRoundBuffer = async () => {
try {
await fs.writeFile(ROUND_BUFFER_PATH, '')
} catch (err) {
if (err.code !== 'ENOENT') {
throw err
}
}
}
Loading