Skip to content

Commit 561a3ac

Browse files
authored
fix: publish empty batches to avoid staled rounds (#692)
* fix: publish empty batches to avoid staled rounds After we changed the 0k checker node to run all task as quickly as possible, we are hitting a deadlock where no measurements are reported towards the end of the round, and as a result, the round is never advanced. In this patch, I am changing the publisher to commit a CID for an empty file (bafkqaaa) when no measurements were recorded. Signed-off-by: Miroslav Bajtoš <[email protected]> * Update publish/test/test.js --------- Signed-off-by: Miroslav Bajtoš <[email protected]>
1 parent cbdc080 commit 561a3ac

File tree

2 files changed

+53
-14
lines changed

2 files changed

+53
-14
lines changed

publish/index.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,35 @@ export const publish = async ({
4444
maxMeasurements
4545
])
4646
if (measurements.length === 0) {
47-
logger.log('No measurements to publish.')
48-
return
47+
// IMPORTANT: We still need to publish an empty batch,
48+
// otherwise the current round will never advance.
49+
logger.log('WARNING: No measurements to publish. Publishing an empty batch.')
4950
}
5051

5152
// Fetch the count of all unpublished measurements - we need this for monitoring
5253
// Note: this number will be higher than `measurements.length` because spark-api adds more
5354
// measurements in between the previous and the next query.
5455
const totalCount = (await pgPool.query(
5556
'SELECT COUNT(*) FROM measurements'
56-
)).rows[0].count
57+
)).rows[0]?.count || 0
5758

5859
logger.log(`Publishing ${measurements.length} measurements. Total unpublished: ${totalCount}. Batch size: ${maxMeasurements}.`)
5960

6061
// Share measurements
6162
const start = new Date()
62-
const file = new File(
63-
[measurements.map(m => JSON.stringify(m)).join('\n')],
64-
'measurements.ndjson',
65-
{ type: 'application/json' }
66-
)
67-
const cid = await web3Storage.uploadFile(file)
63+
64+
let cid
65+
if (measurements.length) {
66+
const file = new File(
67+
[measurements.map(m => JSON.stringify(m)).join('\n')],
68+
'measurements.ndjson',
69+
{ type: 'application/json' }
70+
)
71+
cid = await web3Storage.uploadFile(file)
72+
} else {
73+
// bafkqaaa is a CID for an empty file
74+
cid = 'bafkqaaa'
75+
}
6876
const uploadMeasurementsDuration = new Date().getTime() - start.getTime()
6977
logger.log(`Measurements packaged in ${cid}`)
7078

publish/test/test.js

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ describe('unit', () => {
109109
assert.strictEqual(removeConfirmedCalls.length, 1)
110110
})
111111

112-
it('noops when measurements empty', async () => {
112+
it('publishes an empty batch with CID bafkqaaa when measurements empty', async () => {
113113
const clientStatements = []
114114
const client = {
115115
connect () {
@@ -122,9 +122,40 @@ describe('unit', () => {
122122
}
123123
}
124124

125-
const web3Storage = {}
126-
const ieContract = {}
127-
const stuckTransactionsCanceller = {}
125+
const web3Storage = {
126+
async uploadFile (file) {
127+
throw new Error('web3Storage.uploadFile should not be called')
128+
}
129+
}
130+
131+
const ieContractMeasurementCIDs = []
132+
const ieContract = {
133+
async addMeasurements (cid) {
134+
ieContractMeasurementCIDs.push(cid)
135+
return {
136+
async wait () {
137+
return {
138+
logs: [{}]
139+
}
140+
}
141+
}
142+
},
143+
interface: {
144+
parseLog () {
145+
return {
146+
args: [
147+
null,
148+
1
149+
]
150+
}
151+
}
152+
}
153+
}
154+
155+
const stuckTransactionsCanceller = {
156+
async addPending (tx) { },
157+
async removeConfirmed (tx) { }
158+
}
128159

129160
const { recordTelemetry } = createTelemetryRecorderStub()
130161

@@ -138,7 +169,7 @@ describe('unit', () => {
138169
stuckTransactionsCanceller
139170
})
140171

141-
assert.strictEqual(clientStatements.length, 1)
172+
assert.deepStrictEqual(ieContractMeasurementCIDs, ['bafkqaaa'])
142173
})
143174
})
144175

0 commit comments

Comments
 (0)