-
Notifications
You must be signed in to change notification settings - Fork 19
remove threads #1170
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
base: main
Are you sure you want to change the base?
remove threads #1170
Conversation
|
/run-security-scan |
alexcos20
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AI automated code review (Gemini 3).
Overall risk: low
Summary:
This pull request refactors the Indexer component from a multi-threaded worker_threads architecture to a single-threaded, non-blocking async/await model. This is a significant architectural shift, optimizing for I/O-bound operations prevalent in blockchain indexing. The crawlerThread.ts file is removed, and a new ChainIndexer.ts class handles per-chain indexing logic directly in the main thread. The OceanIndexer orchestrator now manages instances of ChainIndexer, and event communication is streamlined through a shared EventEmitter.
The changes are well-documented in docs/Arhitecture.md and the new module documentation. Tests have been updated to reflect the new event handling and ReindexTask structure. This refactoring is a positive step towards simplifying the codebase and leveraging Node.js's strengths for I/O concurrency.
Comments:
• [INFO][other] The reindexChain logic is guarded by !lockProcessing. Given that lockProcessing is acquired at the beginning of indexLoop, this means reindexChain will only execute if the main indexing logic is not currently running. This might cause delays in responding to triggerReindexChain commands if the indexer is constantly busy. Consider if reindexChain should be prioritized or allowed to interrupt/be scheduled more immediately.
• [INFO][other] In reindexChain, if updateLastIndexedBlockNumber returns -1 (indicating a failure to update the block in the DB), this.reindexBlock is reset to null. This effectively means a triggerReindexChain command is dropped if the initial DB update fails. This might be intended (requiring manual re-triggering by an admin), but it's worth clarifying if automatic retry logic or a more explicit error notification for the admin might be beneficial here.
• [INFO][other] The processReindexQueue uses this.reindexQueue.pop() to retrieve tasks. This makes it a LIFO (Last-In, First-Out) queue. Typically, reindex queues are FIFO (First-In, First-Out), which would use shift(). While not a critical bug, it might lead to unexpected processing order if multiple reindex tasks are added rapidly. Confirm if LIFO is the desired behavior for this queue.
• [INFO][performance] The stop() method uses a while (this.isRunning) { await sleep(100); } loop to wait for graceful shutdown. While it works, this is a busy-wait loop. A more efficient pattern for graceful shutdown might involve a promise that resolves when indexLoop naturally completes its current iteration or a more sophisticated signaling mechanism. For the current context, this might be acceptable given the sleep interval.
PR Description: Refactor Indexer from Worker Threads to Single-Threaded Architecture
Summary
Refactored the Ocean Node Indexer from a worker threads-based architecture to a single-threaded, non-blocking architecture using async/await.
Changes
New Files
src/components/Indexer/ChainIndexer.tscrawlerThread.tsstart(): Begins indexing (non-blocking)stop(): Graceful shutdownindexLoop(): Main continuous indexing loopaddReindexTask(): Queue transaction for reindexingtriggerReindexChain(): Full chain reindex from specific blockModified Files
src/components/Indexer/index.tsMajor refactoring:
Workerimports fromnode:worker_threadsworkers: Record<string, Worker>→indexers: Map<number, ChainIndexer>runningThreadsandglobalWorkersmapsstartThread(): CreatesChainIndexerinstead ofWorkerstopThread(): CallsChainIndexer.stop()instead ofpostMessagestopAllThreads()async for graceful shutdownsetupEventListeners(): Uses sharedINDEXER_CRAWLING_EVENT_EMITTERinstead of per-worker message handlersaddReindexTask(): Direct method call instead ofpostMessageresetCrawling(): Direct method call with proper async handlingsrc/test/integration/operationsDashboard.test.tsReindexTaskfromChainIndexer.jsinstead ofcrawlerThread.jsdocs/Arhitecture.mdRemoved
src/components/Indexer/crawlerThread.tsTechnical Details
How Concurrent Execution Works
Multiple chains process concurrently via Node.js event loop:
Event Communication Flow
Breaking Changes
None. This is a transparent internal refactoring:
Performance Impact
Memory Usage
CPU & I/O Performance
Concurrency
Testing
✅ TypeScript Compilation: No errors
✅ Linting: No errors
Tests to Run
src/test/integration/indexer.test.ts- Core indexer functionalitysrc/test/integration/operationsDashboard.test.ts- Admin operationsManual Testing Checklist
reindexTxadmin commandreindexChainadmin commandMigration Guide
For Node Operators
No action required. Update to this version and restart - indexing continues from last indexed block.
For Developers
Only if you directly imported from
crawlerThread.ts:All other code remains unchanged.
Checklist
Related Issues
Closes #1168