-
Notifications
You must be signed in to change notification settings - Fork 0
repair cli #22
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
Merged
Merged
repair cli #22
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
15a65c6
chore: cli setup
hugomrdias 0e9755e
chore: repair commands
hugomrdias 46786fc
chore: update repair-cli package and commands
hugomrdias 04f44a1
chore: lock
hugomrdias 0a70d3d
chore: update dependencies and improve repair-cli functionality
hugomrdias e098865
feat: enhance repair-cli with new filtering and upsert functionalities
hugomrdias 2a339ba
refactor: update CLI versioning and enhance dataset commands
hugomrdias 67174ff
refactor: streamline repair dataset handling and enhance CLI function…
hugomrdias 5c1532c
fix: progress and new synpase-core
hugomrdias acb45c6
refactor: enhance repair CLI functionality and streamline dataset man…
hugomrdias 8d01480
chore: update dependencies in pnpm-lock and package.json
hugomrdias 977b2bc
chore: update TypeScript configuration for repair-cli
hugomrdias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| # Early repair CLI (`@filoz/repair-cli`) | ||
|
|
||
| CLI to migrate pieces from a faulty Filecoin storage provider to an alternate provider. Built with [incur](https://www.npmjs.com/package/incur), Drizzle ORM, and `@filoz/synapse-core` for on-chain SP calls. | ||
|
|
||
| ## Architecture | ||
|
|
||
| Two databases: | ||
|
|
||
| | DB | Engine | Schema | Role | | ||
| | ----------- | ------------------------------ | ------------------------ | ---------------------------------------------- | | ||
| | **Indexer** | Postgres (`indexer-schema.ts`) | `early-repair` pg schema | Read-only catalog: providers, datasets, pieces | | ||
| | **Local** | SQLite (`local-schema.ts`) | `repairs`, `operations` | Repair job queue and execution state | | ||
|
|
||
| Commands get both via `contextMiddleware` (`middleware.ts`): wallet client from config, indexer URL by `chainId` (314 = mainnet, else calibration). | ||
|
|
||
| ## Piece groups | ||
|
|
||
| Pieces are grouped by dataset flags (`withCdn`, `withIpfsIndexing`). Groups are **mutually exclusive**: | ||
|
|
||
| - `both`: `withCdn = true`, `withIpfsIndexing = true` | ||
| - `cdn`: `withCdn = true`, `withIpfsIndexing = false` | ||
| - `ipfs`: `withCdn = false`, `withIpfsIndexing = true` | ||
| - `none`: `withCdn = false`, `withIpfsIndexing = false` | ||
|
|
||
| Same CID may appear on multiple datasets in one group; dedupe per group when listing or paginating pieces. | ||
|
|
||
| Target datasets are looked up with payer + `EARLY_REPAIR_SOURCE` (`utils.ts`, value `early-repair2`). | ||
|
|
||
| ## Repair pipeline | ||
|
|
||
| ### 1. `repair create --provider-id <id>` | ||
|
|
||
| `createRepair` (`db/create-repair.ts`): | ||
|
|
||
| 1. **Target provider** — if `--target-provider-id` is set, **`getRepairProvider`** loads that active provider; otherwise **`selectAlternateRepairProvider`** picks one with tier-matched fallback (endorsed → approved → none). Throws if none found. | ||
| 2. **`getDataSetsByGroup`** — target provider datasets per group. | ||
| 3. Insert **`repairs`** row (`repairProviderId`, `targetProviderId`). | ||
| 4. **`forEachPiecesPage`** — paginated `add_piece` operations (`db/get-pieces.ts`, page size 500). | ||
|
hugomrdias marked this conversation as resolved.
Outdated
|
||
| 5. **`getRepairGroups`** — distinct groups from pending `add_piece` ops; saved on the repair row. | ||
| 6. For each group with pending pieces but no target dataset → insert **`create_dataset`** operation (`pending`). | ||
|
|
||
| ### 2. `repair run <repairId>` | ||
|
|
||
| 1. Run **`create_dataset`** phase (`pipeline/create-datasets.ts`) via fastq — `SP.createDataSet`, then `updateOperation`; returns created dataset IDs indexed by group. | ||
| 2. Run **`add_piece`** pull phase (`pipeline/pull.ts`): pending ops are fetched in same-group pages and fed into fastq as workers free up (`createPullPiecesWorker` — mock logs CIDs per batch). | ||
|
|
||
| `--reset` retries `pending` and `failed` `create_dataset` ops only; `add_piece` always runs `pending` (failed pieces are skipped). `--batch-size` (default 50) caps pieces per pull job; each batch is one repair group only. | ||
|
|
||
| ### Operation types | ||
|
|
||
| - `create_dataset`: `pending` → `committing` → `completed` | `failed`; data has `serviceUrl`, `payee`. | ||
| - `add_piece`: `pending` → `pulling` → `committing` → `completed` | `failed`; data has `cid`, `serviceUrl`, `metadata`, `alternateProviders`. | ||
|
|
||
| `add_piece` without alternate providers (other replicas) is created as **`failed`** with error `"No alternate providers found"`. `getProvidersByCid` excludes the source `providerId`. | ||
|
hugomrdias marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## Source layout | ||
|
|
||
| ```text | ||
| src/ | ||
| cli.ts # incur root: setup, wallet, repair, datasets, providers | ||
| commands/ | ||
| repair.ts # create | list | delete | run | ||
| datasets.ts | ||
| providers.ts # list | ||
| setup.ts | ||
| wallet.ts | ||
| db/ | ||
| create-repair.ts # createRepair orchestration | ||
| get-repair-groups.ts # source groups that need repair | ||
| get-datasets-by-group.ts # target datasets per group | ||
| get-providers-by-cid.ts # alternate providers per CID | ||
| select-alternate-repair-provider.ts # automatic target provider selection | ||
| get-repair-provider.ts # load explicit target provider by ID | ||
| update-operation.ts # patch local operation status/result/error | ||
| delete-repair.ts # delete repair and its operations | ||
| get-pieces.ts # getPiecesPage, forEachPiecesPage | ||
| pipeline/ | ||
| create-datasets.ts # create_dataset operation queue | ||
| pull.ts # paginated add_piece pull queue + mock worker | ||
| local-schema.ts # SQLite repairs/operations | ||
| indexer-schema.ts # Postgres early-repair schema | ||
| middleware.ts # DB + wallet context | ||
| types.ts # Group, PIECE_GROUPS, DB types | ||
| error.ts # NoAlternateProviderError, RepairCreationError | ||
| utils.ts # config, client, metadata helpers | ||
| ``` | ||
|
|
||
| ## Conventions | ||
|
|
||
| - Indexer helpers take `IndexerQueryOptions` (`indexerDb`, `indexerSchema`). | ||
| - Extract DB helpers under `src/db/` (indexer queries and local operation updates). | ||
| - Add JSDoc on exported functions/types; inline comments only for non-obvious logic (dedupe, pagination, tier fallback). | ||
| - Use `PIECE_GROUPS` instead of `Object.keys` for group iteration. | ||
|
|
||
| ## Indexer API (`src/db/`) | ||
|
|
||
| | Function | Module | Purpose | | ||
| | ------------------------------- | ----------------------------------- | ------------------------------------------------------- | | ||
| | `getRepairGroups` | `get-repair-groups.ts` | Repair groups from pending local `add_piece` operations | | ||
| | `getDataSetsByGroup` | `get-datasets-by-group.ts` | One dataset per group for payer + `EARLY_REPAIR_SOURCE` | | ||
| | `getProvidersByCid` | `get-providers-by-cid.ts` | Alternate providers per CID; empty array if none | | ||
| | `selectAlternateRepairProvider` | `select-alternate-repair-provider.ts` | Automatic target provider selection | | ||
| | `getRepairProvider` | `get-repair-provider.ts` | Load explicit target provider by ID | | ||
| | `filterPullPiecesNotInDataset` | `filter-pull-pieces-not-in-dataset.ts` | Exclude CIDs already indexed in a dataset | | ||
| | `updateOperation` | `update-operation.ts` | Patch local operation status/result/error | | ||
|
|
||
| ## Local schema | ||
|
|
||
| - **`repairs`**: `repairProviderId`, `targetProviderId`, `repairGroups`, `status` (`pending` \| `running` \| `completed` \| `failed`). | ||
| - **`operations`**: `type`, `group`, `status`, `data` (JSON), `result`, `error`. | ||
|
|
||
| ## Commands | ||
|
|
||
| | Command | Notes | | ||
| | ------- | ----- | | ||
| | `repair setup` | Interactive config: private key, indexer URLs, chain, local DB path; migrates SQLite | | ||
| | `repair wallet fund` | Fund calibration wallet from faucet | | ||
| | `repair wallet balance` | Wallet FIL/USDFC balances and pay account summary | | ||
| | `repair wallet deposit <amount>` | Deposit USDFC to pay account | | ||
| | `repair wallet withdraw <amount>` | Withdraw USDFC from pay account | | ||
| | `repair repair create --provider-id <id>` | Plan repair; optional `--target-provider-id`; returns `repairId` | | ||
| | `repair repair list` | List repairs with operation counts | | ||
| | `repair repair delete <repairId>` | Delete a repair and its operations | | ||
| | `repair repair run <repairId>` | Execute workers; `--concurrency`, `--batch-size`, `--reset` | | ||
| | `repair datasets list` | List payer datasets from indexer with piece counts; optional `--provider-id` | | ||
| | `repair providers list` | List active providers from indexer (`providerActive` + `pdpProductActive`) | | ||
|
|
||
| ## Build & test | ||
|
|
||
| ```bash | ||
| pnpm --filter @filoz/repair-cli build | ||
| pnpm --filter @filoz/repair-cli lint | ||
| pnpm --filter @filoz/repair-cli test | ||
| ``` | ||
|
|
||
| ## Not yet implemented | ||
|
|
||
| - Real `add_piece` pull/commit (worker only logs batches). | ||
| - Repair-level status transitions after run completes. | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,19 @@ | ||
| // TODO | ||
| #!/usr/bin/env node | ||
| import { Cli } from 'incur' | ||
| import { datasets } from './commands/datasets.ts' | ||
| import { providers } from './commands/providers.ts' | ||
| import { repair } from './commands/repair.ts' | ||
| import { setup } from './commands/setup.ts' | ||
| import { wallet } from './commands/wallet.ts' | ||
|
|
||
| const cli = Cli.create('repair', { | ||
| version: '0.0.0', | ||
| description: 'Early repair for faulty service providers and datasets', | ||
| }) | ||
|
hugomrdias marked this conversation as resolved.
|
||
|
|
||
| cli.command(setup) | ||
| cli.command(wallet) | ||
| cli.command(repair) | ||
| cli.command(datasets) | ||
| cli.command(providers) | ||
| cli.serve() | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { and, eq } from 'drizzle-orm' | ||
| import { Cli, z } from 'incur' | ||
| import { contextMiddleware, contextSchema } from '../middleware.ts' | ||
| import { globalOptions } from '../utils.ts' | ||
| export const datasets = Cli.create('datasets', { | ||
| description: 'Dataset commands', | ||
| options: globalOptions, | ||
| vars: contextSchema, | ||
| }) | ||
|
|
||
| datasets.command('list', { | ||
| description: 'List all datasets', | ||
|
hugomrdias marked this conversation as resolved.
Outdated
|
||
| options: globalOptions.extend({ | ||
| providerId: z.coerce.bigint().optional().describe('Filter datasets by provider ID'), | ||
| }), | ||
| middleware: [contextMiddleware], | ||
| run: async (c) => { | ||
| try { | ||
| const schema = c.var.indexerDb._.fullSchema | ||
| const conditions = [ | ||
| eq(schema.dataSets.deleted, false), | ||
| eq(schema.dataSets.payer, c.var.client.account.address.toLowerCase()), | ||
| ] | ||
| if (c.options.providerId != null) { | ||
| conditions.push(eq(schema.dataSets.providerId, c.options.providerId)) | ||
| } | ||
|
|
||
| const datasets = await c.var.indexerDb.query.dataSets.findMany({ | ||
| where: and(...conditions), | ||
| with: { | ||
| provider: true, | ||
| pieces: true, | ||
| }, | ||
| }) | ||
|
|
||
| const datasetsFlattened = datasets.map((dataset) => { | ||
| const { provider, pieces } = dataset | ||
| return { | ||
| id: dataset.dataSetId, | ||
| withCdn: dataset.withCdn, | ||
| withIpfsIndexing: dataset.withIpfsIndexing, | ||
| source: dataset.source, | ||
| provider: provider.serviceUrl, | ||
| pdpEndEpoch: dataset.pdpEndEpoch, | ||
| pieces: pieces.length, | ||
| // metadata: JSON.stringify(dataset.metadata), | ||
| } | ||
| }) | ||
|
|
||
| return c.ok({ | ||
| datasets: datasetsFlattened, | ||
| }) | ||
| } catch (error) { | ||
| console.error(error) | ||
| return c.error({ | ||
| code: 'DATASETS_FAILED', | ||
| message: error instanceof Error ? error.message : 'Failed to list datasets', | ||
| retryable: true, | ||
| }) | ||
| } | ||
| }, | ||
| }) | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { and, asc, eq } from 'drizzle-orm' | ||
| import { Cli, z } from 'incur' | ||
| import { contextMiddleware, contextSchema } from '../middleware.ts' | ||
| import { globalOptions } from '../utils.ts' | ||
|
|
||
| export const providers = Cli.create('providers', { | ||
| description: 'Provider commands', | ||
| options: globalOptions, | ||
| vars: contextSchema, | ||
| }) | ||
|
|
||
| providers.command('list', { | ||
| description: 'List all providers from the indexer', | ||
| options: globalOptions, | ||
| middleware: [contextMiddleware], | ||
| run: async (c) => { | ||
| try { | ||
| const schema = c.var.indexerDb._.fullSchema | ||
| const rows = await c.var.indexerDb.query.providers.findMany({ | ||
| orderBy: [asc(schema.providers.providerId)], | ||
| where: and(eq(schema.providers.providerActive, true), eq(schema.providers.pdpProductActive, true)), | ||
| }) | ||
|
|
||
| const providersFlattened = rows.map((provider) => ({ | ||
| id: provider.providerId, | ||
| name: provider.name, | ||
| approved: provider.approved, | ||
| endorsed: provider.endorsed, | ||
| })) | ||
|
|
||
| return c.ok({ | ||
| providers: providersFlattened, | ||
| }) | ||
| } catch (error) { | ||
| console.error(error) | ||
| return c.error({ | ||
| code: 'PROVIDERS_FAILED', | ||
| message: error instanceof Error ? error.message : 'Failed to list providers', | ||
| retryable: true, | ||
| }) | ||
| } | ||
| }, | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.