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

fix: add tests and log when there is an error when persisting anchors #1202

Merged
merged 2 commits into from
May 7, 2024
Merged
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
26 changes: 25 additions & 1 deletion src/services/__tests__/anchor-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,30 @@ describe('anchor service', () => {
expect(anchors.length).toEqual(numRequests)
})

test('Retry persisting anchors on serializable erorr', async () => {
const requests = await fake.multipleRequests(4)

const [candidates] = await anchorService._findCandidates(requests, 0)
const merkleTree = await anchorService._buildMerkleTree(candidates)
const ipfsProofCid = await ipfsService.storeRecord({})
const anchors = await anchorService._createAnchorCommits(ipfsProofCid, merkleTree)

// this update will lock the request rows for 1 second
// it is not awaited, so it will run in parallel with the persistAnchorResult
connection.transaction(async (trx) => {
await requestRepository
.withConnection(trx)
.updateRequests({ status: RequestStatus.REPLACED }, requests)

await Utils.delay(1000)
})
// an error will be thrown because of the lock from above, but will retry until it works
await anchorService._persistAnchorResult(anchors, candidates)

const readyRequests = await requestRepository.findByStatus(RequestStatus.READY)
expect(readyRequests.length).toEqual(0)
})

describe('Request pinning', () => {
async function anchorRequests(numRequests: number): Promise<Request[]> {
// Create Requests
Expand Down Expand Up @@ -711,7 +735,7 @@ describe('anchor service', () => {

const batch = new MockQueueMessage({
bid: uuidv4(),
rids: requests.map(({id}) => id),
rids: requests.map(({ id }) => id),
})
anchorBatchQueueService.receiveMessage.mockReturnValue(Promise.resolve(batch))

Expand Down
1 change: 1 addition & 0 deletions src/services/anchor-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ export class AnchorService {
{ isolationLevel: 'repeatable read' }
)
.catch(async (err) => {
logger.err(`Error persisting anchor results: ${err}`)
if (err?.code === REPEATED_READ_SERIALIZATION_ERROR) {
Metrics.count(METRIC_NAMES.DB_SERIALIZATION_ERROR, 1)
await Utils.delay(100)
Expand Down
Loading