Repository navigation
Conversation
…ot work on large d) Automated fix and test assertions. Signed-off-by: Ollie <olliemcgrath@icloud.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #59
/claim #59
Solution for [github/issues_59]: [outerbase/starbasedb] Database dumps do not work on large databases
Bounty: [outerbase/starbasedb] Database dumps do not work on large databases
Root Cause Analysis
The core problem stems from two limitations of Cloudflare Workers and Durable Objects:
The proposed solution in the issue description correctly identifies the need for asynchronous processing, external storage (R2), and a mechanism to resume interrupted operations.
Architecture Design
The solution will involve the following components and flow:
Asynchronous Dump Initiation:
/export/dumpendpoint will no longer directly return the dump file. Instead, it will initiate an asynchronous dump process within the Durable Object.dumpIdto track its status./export/dump/:dumpId/status) to check the progress or wait for a callback.Durable Object State Management:
dumpId: Unique identifier for the dump.status: (e.g.,PENDING,IN_PROGRESS,COMPLETED,FAILED).progress: Current position in the dump (e.g., last table processed, last row ID).r2Key: The key of the dump file in R2.callbackUrl: Optional URL to notify upon completion.startTime,endTime.state.storageto persist across restarts and allow for resuming.R2 for Dump Storage:
Durable Object Alarms for Resumption:
state.storage.alarm()handler will be invoked, which will resume the dump process from the saved progress, set a new alarm, and continue. This allows the DO to yield control and process other requests.Streaming to R2 (Chunking):
User Notification:
callbackUrlwas provided, the Durable Object will make an HTTP POST request to that URL with thedumpId,status, and a URL to download the dump file from R2./export/dump/:dumpId/downloadwill be provided to generate a signed URL for R2, allowing the user to download the completed dump.Error Handling and Cleanup:
Data Model for Dump State (within Durable Object
state.storage):High-Level Flow:
POST /export/dumpwithformat, optionalcallbackUrl.dumpId.DumpStateinstate.storage.status = PENDING.startDumpProcess()(async, non-await).202 AcceptedwithdumpId.status = IN_PROGRESS.now + CHUNK_DURATION_SECONDS.processDumpChunk().DumpStatefromstate.storage.currentTableIndex,currentRowOffset.CHUNK_DURATION_SECONDS.currentTableIndex,currentRowOffset,uploadedPartsinDumpState.DumpStatetostate.storage.status = COMPLETED.callbackUrl(if present).now + CHUNK_DURATION_SECONDS.processDumpChunk().GET /export/dump/:dumpId/statusDumpState(excluding sensitive info).GET /export/dump/:dumpId/downloadstatus == COMPLETED.r2Key.302 Redirectto the signed URL.Breathing Intervals: The alarm mechanism inherently provides breathing intervals. By setting an alarm for a short duration and processing only for a fraction of that duration, the Durable Object yields control, allowing other requests to be processed before the alarm triggers the next chunk of work.
Production-Ready Code/Patch Implementation
This implementation will focus on the SQL dump format as requested, but the structure will be extensible for CSV/JSON.
Assumptions:
starbasedbproject useswrangler.tomlfor configuration.DUMP_BUCKETis available.this.env.DB.prepare().all()) is accessible within the Durable Object.Databaseclass or similar abstraction exists to interact with the underlying data. For this example, I'll simulate it with a simplegetTablesandgetRowsmethod.DurableObjectStateandDurableObjectEnvtypes are available.Changes required:
wrangler.toml: Add R2 binding.src/index.ts(or main worker file):DurableObjectEnvto includeDUMP_BUCKET.src/durable_object.ts(or existing DO file):DumpStateinterface.handleRequestto support new dump endpoints.alarm()handler.startDumpProcess,processDumpChunk,generateSqlDumpChunkmethods.Databaseinterface and a mock implementation for testing.File: wrangler.toml
File: src/types.ts
File: src/durable_object.ts
File: src/index.ts
Testing Verification Steps
To verify this solution, you'll need a Cloudflare account,
wranglerCLI installed, and an R2 bucket configured.Setup Cloudflare Project:
wrangler.tomlis configured as shown above, with aDUMP_BUCKETbinding pointing to your R2 bucket.starbasedb-dumps(or whatever you configured inwrangler.toml).Deploy the Worker:
wrangler deployfrom your project root. This will deploy the Worker and the Durable Object.https://starbasedb.YOUR-ID-HERE.workers.dev).Initiate a Dump (POST /export/dump):
curlor a tool like Postman/Insomnia.YOUR-ID-HEREwith your worker's subdomain.Optionally, replace
callbackUrlwith a real webhook endpoint you control (e.g., a RequestBin URL) to test notifications.{ "dumpId": "a-uuid-string-like-this", "status": "PENDING", "message": "Dump process initiated. Use /export/dump/:dumpId/status to check progress." }dumpIdfrom the response.Monitor Dump Status (GET /export/dump/:dumpId/status):
curl 'https://starbasedb.YOUR-ID-HERE.workers.dev/export/dump/a-uuid-string-like-this/status'Payout & Settlement Details
0x66545035486C8B3b7966c45dc035560F4066aBb5