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(whale-api): add reindex for failed to indexed block #2169

Merged
merged 5 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
44 changes: 41 additions & 3 deletions apps/whale-api/src/module.indexer/rpc.block.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { IndexStatusMapper, Status } from './status'
import { waitForCondition } from '@defichain/testcontainers/dist/utils'
import { blockchain as defid, RpcApiError } from '@defichain/jellyfish-api-core'
import { NotFoundIndexerError } from './error'

@Injectable()
export class RPCBlockProvider {
Expand Down Expand Up @@ -44,6 +45,15 @@
}

try {
const status = await this.statusMapper.get()
lykalabrada marked this conversation as resolved.
Show resolved Hide resolved
this.logger.log(`[Start Indexing] - status: ${status?.status ?? ''}`)
if (status?.status === Status.REINDEX) {
const currentBlock = await this.client.blockchain.getBlock(status.hash, 2)
const prevBlock = await this.client.blockchain.getBlock(currentBlock.previousblockhash, 2)
this.logger.log(`Reindexing previous block ${prevBlock.height} ...`)
await this.statusMapper.put(prevBlock.hash, prevBlock.height, Status.INDEXING)

Check warning on line 54 in apps/whale-api/src/module.indexer/rpc.block.provider.ts

View check run for this annotation

Codecov / codecov/patch

apps/whale-api/src/module.indexer/rpc.block.provider.ts#L51-L54

Added lines #L51 - L54 were not covered by tests
}

await this.cleanup()
this.indexing = await this.synchronize()
} catch (err) {
Expand Down Expand Up @@ -88,9 +98,32 @@

const nextBlock = await this.client.blockchain.getBlock(nextHash, 2)
if (await RPCBlockProvider.isBestChain(indexed, nextBlock)) {
this.logger.log(`[Synchronize - best chain] Indexing next block ${nextBlock.height}...`)
await this.index(nextBlock)
} else {
await this.invalidate(indexed.hash, indexed.height)
this.logger.log(`[Synchronize - not the best chain] Indexing prev block ${indexed.height}...`)

Check warning on line 104 in apps/whale-api/src/module.indexer/rpc.block.provider.ts

View check run for this annotation

Codecov / codecov/patch

apps/whale-api/src/module.indexer/rpc.block.provider.ts#L104

Added line #L104 was not covered by tests
// Retry indexing the previous block before invalidating it
const MAX_RETRIES = 3
Copy link
Contributor

@canonbrother canonbrother Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we do not need this ady after REINDEX..
all reindex/pre-index will be auto handling in synchronize
one retry pattern is sufficient

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, then we will not be reindexing the previous block, just the current block thats failing. Also fine with me, we just have to test on any stucked ocean, to see if fix still works the same :)
cc: @pierregee

let prevBlockIndexedSuccessfully = false
let prevBlockRetryCount = 0

Check warning on line 108 in apps/whale-api/src/module.indexer/rpc.block.provider.ts

View check run for this annotation

Codecov / codecov/patch

apps/whale-api/src/module.indexer/rpc.block.provider.ts#L106-L108

Added lines #L106 - L108 were not covered by tests

while (!prevBlockIndexedSuccessfully && prevBlockRetryCount < MAX_RETRIES) {
try {
this.logger.log(`Retrying indexing block ${indexed.height} - Attempt ${prevBlockRetryCount + 1}`)
const previousBlock = await this.client.blockchain.getBlock(indexed.hash, 2)
await this.index(previousBlock)
prevBlockIndexedSuccessfully = true

Check warning on line 115 in apps/whale-api/src/module.indexer/rpc.block.provider.ts

View check run for this annotation

Codecov / codecov/patch

apps/whale-api/src/module.indexer/rpc.block.provider.ts#L111-L115

Added lines #L111 - L115 were not covered by tests
} catch (error) {
this.logger.error(`Error indexing the previous block - ${indexed.height}`)
prevBlockRetryCount++

Check warning on line 118 in apps/whale-api/src/module.indexer/rpc.block.provider.ts

View check run for this annotation

Codecov / codecov/patch

apps/whale-api/src/module.indexer/rpc.block.provider.ts#L117-L118

Added lines #L117 - L118 were not covered by tests
}
}

if (!prevBlockIndexedSuccessfully) {
// If all retries for indexing the previous block fail, invalidate it
await this.invalidate(indexed.hash, indexed.height)
this.logger.error('All retries for indexing the previous block have failed. The block has been invalidated.')

Check warning on line 125 in apps/whale-api/src/module.indexer/rpc.block.provider.ts

View check run for this annotation

Codecov / codecov/patch

apps/whale-api/src/module.indexer/rpc.block.provider.ts#L124-L125

Added lines #L124 - L125 were not covered by tests
}
}
return true
}
Expand All @@ -100,7 +133,7 @@
* @param {defid.Block<Transaction>} nextBlock to check previous block hash
*/
private static async isBestChain (indexed: Block, nextBlock: defid.Block<defid.Transaction>): Promise<boolean> {
return nextBlock.previousblockhash === indexed.hash
return indexed.hash === nextBlock.previousblockhash
Copy link
Contributor

@canonbrother canonbrother Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice 🤓 !! (lesser brain consuming)

}

public async indexGenesis (): Promise<boolean> {
Expand All @@ -121,6 +154,7 @@
switch (status.status) {
case Status.INVALIDATED:
case Status.INDEXED:
case Status.REINDEX:
return
}

Expand Down Expand Up @@ -149,7 +183,11 @@
await this.indexer.invalidate(hash)
await this.statusMapper.put(hash, height, Status.INVALIDATED)
} catch (err) {
await this.statusMapper.put(hash, height, Status.ERROR)
if (err instanceof NotFoundIndexerError) {
await this.statusMapper.put(hash, height, Status.REINDEX)
} else {
await this.statusMapper.put(hash, height, Status.ERROR)

Check warning on line 189 in apps/whale-api/src/module.indexer/rpc.block.provider.ts

View check run for this annotation

Codecov / codecov/patch

apps/whale-api/src/module.indexer/rpc.block.provider.ts#L187-L189

Added lines #L187 - L189 were not covered by tests
}
throw err
}
}
Expand Down
3 changes: 2 additions & 1 deletion apps/whale-api/src/module.indexer/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export enum Status {
INDEXED = 'INDEXED',
INVALIDATING = 'INVALIDATING',
INVALIDATED = 'INVALIDATED',
ERROR = 'ERROR'
ERROR = 'ERROR',
REINDEX = 'REINDEX'
}

export interface IndexStatus extends Model {
Expand Down
Loading