Skip to content

Commit 21038e3

Browse files
committed
separate aggregate piece_ref, no pdp ref for aggregate
1 parent b2c4fc6 commit 21038e3

4 files changed

Lines changed: 50 additions & 23 deletions

File tree

cmd/curio/storacha-migration-aggregate.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
)
3535

3636
const storachaAggregateDefaultPieceSize = abi.PaddedPieceSize(1 << 30)
37+
const storachaMigrationAggregateDataURL = "storacha-migration-aggregate"
3738

3839
// storachaAggregatePieceSize is a variable only so tests can exercise the
3940
// recovery paths without writing 1 GiB aggregate files. Production uses the
@@ -1771,7 +1772,7 @@ func recoverStorachaAggregateFromFinal(ctx context.Context, db *harmonydb.DB, si
17711772
return false, err
17721773
}
17731774

1774-
imported, err := recoverFinalStorachaPiece(ctx, db, si, storageID, targetPath, row)
1775+
imported, err := recoverFinalStorachaPiece(ctx, db, si, storageID, targetPath, row, false)
17751776
if err != nil {
17761777
return false, err
17771778
}
@@ -2045,7 +2046,7 @@ func importStagedStorachaAggregateFile(ctx context.Context, db *harmonydb.DB, si
20452046
return xerrors.Errorf("aggregate staging file %s size mismatch: expected %d, got %d; cleanup manually before retrying", stagingPath, verified.RawSize, info.Size())
20462047
}
20472048

2048-
result, err := importStagedStorachaPiece(ctx, db, si, storageID, targetPath, stagingPath)
2049+
result, err := importStagedStorachaPiece(ctx, db, si, storageID, targetPath, stagingPath, false)
20492050
if err != nil {
20502051
return err
20512052
}

cmd/curio/storacha-migration-aggregate_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ func TestStorachaAggregateRecoversFromFinalFileAndFixesDB(t *testing.T) {
156156
tempPath, verified := createStorachaAggregateTempFileForTest(t, env, workDir, group)
157157
pieceID := insertAggregateParkedPiece(t, env, verified, false)
158158
assertStorachaRefs(t, env, pieceID, 0)
159+
assertStorachaRefsWithDataURL(t, env, pieceID, storachaMigrationAggregateDataURL, 0)
159160
assertSectorLocation(t, env, pieceID, 0)
160161

161162
finalPath := storachaFinalPiecePath(env.targetDir, pieceID)
@@ -166,8 +167,9 @@ func TestStorachaAggregateRecoversFromFinalFileAndFixesDB(t *testing.T) {
166167

167168
pp := parkedPieceByID(t, env, pieceID)
168169
require.True(t, pp.complete)
169-
assertStorachaRefs(t, env, pieceID, 1)
170-
assertPDPRefs(t, env, pieceID, group.PieceCIDV1, 1)
170+
assertStorachaRefs(t, env, pieceID, 0)
171+
assertStorachaRefsWithDataURL(t, env, pieceID, storachaMigrationAggregateDataURL, 1)
172+
assertPDPRefs(t, env, pieceID, group.PieceCIDV1, 0)
171173
assertSectorLocation(t, env, pieceID, 1)
172174

173175
complete := assertStorachaAggregateImported(t, env, workDir, group)
@@ -407,8 +409,9 @@ func assertStorachaAggregateImported(t *testing.T, env storachaMigrationTestEnv,
407409

408410
pp := parkedPieceByID(t, env, rows[0].ID)
409411
require.True(t, pp.complete)
410-
assertStorachaRefs(t, env, rows[0].ID, 1)
411-
assertPDPRefs(t, env, rows[0].ID, group.PieceCIDV1, 1)
412+
assertStorachaRefs(t, env, rows[0].ID, 0)
413+
assertStorachaRefsWithDataURL(t, env, rows[0].ID, storachaMigrationAggregateDataURL, 1)
414+
assertPDPRefs(t, env, rows[0].ID, group.PieceCIDV1, 0)
412415
assertSectorLocation(t, env, rows[0].ID, 1)
413416

414417
_, err = verifyStorachaAggregateFileForTest(storachaFinalPiecePath(env.targetDir, rows[0].ID), pcidV1, abi.PaddedPieceSize(group.PaddedSize))

cmd/curio/storacha-migration.go

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func runImportPieces(ctx context.Context, db *harmonydb.DB, si paths.SectorIndex
217217
continue
218218
}
219219

220-
result, err := importStagedStorachaPiece(ctx, db, si, storageID, targetPath, filepath.Join(staging, entry.Name()))
220+
result, err := importStagedStorachaPiece(ctx, db, si, storageID, targetPath, filepath.Join(staging, entry.Name()), true)
221221
if err != nil {
222222
return out, err
223223
}
@@ -276,7 +276,7 @@ func runImportPieces(ctx context.Context, db *harmonydb.DB, si paths.SectorIndex
276276
return out, xerrors.Errorf("renaming old path: %w", err)
277277
}
278278

279-
result, err := importStagedStorachaPiece(ctx, db, si, storageID, targetPath, stagingPath)
279+
result, err := importStagedStorachaPiece(ctx, db, si, storageID, targetPath, stagingPath, true)
280280
if err != nil {
281281
return out, err
282282
}
@@ -334,13 +334,15 @@ func storachaPieceInfoFromFileName(name string) (*mk20.PieceInfo, bool, error) {
334334

335335
// importStagedStorachaPiece finishes one target/storacha-staging/<cid>.car.
336336
// It first creates or reuses parked_pieces/refs in a committed DB transaction.
337+
// makePDPPieceRef keeps import-pieces behavior unchanged while letting aggregate
338+
// imports use the aggregate migration data_url and skip the public PDP piece ref.
337339
// If an existing incomplete parked_piece still has a live task, it returns
338340
// Imported=false and leaves the staged file untouched for a later retry.
339341
// Otherwise it consumes the staged file: remove it if the final
340342
// target/piece/s-t00-<id> already exists, or rename it to that final path. It
341343
// then declares the piece location and marks parked_pieces.complete unless the
342344
// row was already complete.
343-
func importStagedStorachaPiece(ctx context.Context, db *harmonydb.DB, si paths.SectorIndex, storageID storiface.ID, targetPath, stagingPath string) (storachaImportResult, error) {
345+
func importStagedStorachaPiece(ctx context.Context, db *harmonydb.DB, si paths.SectorIndex, storageID storiface.ID, targetPath, stagingPath string, makePDPPieceRef bool) (storachaImportResult, error) {
344346
info, err := os.Stat(stagingPath)
345347
if err != nil {
346348
return storachaImportResult{}, xerrors.Errorf("checking staging file: %w", err)
@@ -383,7 +385,7 @@ func importStagedStorachaPiece(ctx context.Context, db *harmonydb.DB, si paths.S
383385
return true, nil
384386
}
385387

386-
if err := ensureStorachaRefsTx(tx, parkedPieceID, pi); err != nil {
388+
if err := ensureStorachaRefsTx(tx, parkedPieceID, pi, makePDPPieceRef); err != nil {
387389
return false, err
388390
}
389391

@@ -474,7 +476,7 @@ func recoverFinalStorachaPieces(ctx context.Context, db *harmonydb.DB, si paths.
474476
break
475477
}
476478

477-
imported, err := recoverFinalStorachaPiece(ctx, db, si, storageID, targetPath, row)
479+
imported, err := recoverFinalStorachaPiece(ctx, db, si, storageID, targetPath, row, true)
478480
if err != nil {
479481
return err
480482
}
@@ -510,7 +512,7 @@ func storachaPieceCIDV2FromRow(row storachaFinalRecoveryRow) (string, error) {
510512
// DB row, reuses the same claim/ref checks as staged import, then declares the
511513
// final file and marks the parked piece complete. It returns false when the
512514
// final file is missing or a live task still owns the parked_piece.
513-
func recoverFinalStorachaPiece(ctx context.Context, db *harmonydb.DB, si paths.SectorIndex, storageID storiface.ID, targetPath string, row storachaFinalRecoveryRow) (bool, error) {
515+
func recoverFinalStorachaPiece(ctx context.Context, db *harmonydb.DB, si paths.SectorIndex, storageID storiface.ID, targetPath string, row storachaFinalRecoveryRow, makePDPPieceRef bool) (bool, error) {
514516
finalPath := storachaFinalPiecePath(targetPath, row.ID)
515517
info, err := os.Stat(finalPath)
516518
if os.IsNotExist(err) {
@@ -542,7 +544,7 @@ func recoverFinalStorachaPiece(ctx context.Context, db *harmonydb.DB, si paths.S
542544
if !state.UseStagedFile {
543545
return true, nil
544546
}
545-
if err := ensureStorachaRefsTx(tx, row.ID, pi); err != nil {
547+
if err := ensureStorachaRefsTx(tx, row.ID, pi, makePDPPieceRef); err != nil {
546548
return false, err
547549
}
548550
return true, nil
@@ -617,12 +619,15 @@ func claimExistingStorachaPieceTx(tx *harmonydb.Tx, parkedPieceID int64) (storac
617619
return state, nil
618620
}
619621

620-
// ensureStorachaRefsTx enforces the intended one-to-one shape for this import.
621-
// It locks storacha-migration parked_piece_refs for this parked piece, fails if
622-
// more than one exists, inserts the missing ref if needed, then does the same
623-
// for pdp_piecerefs through the unique piece_ref relation. Existing PDP refs
624-
// must already point at the same service and piece CID.
625-
func ensureStorachaRefsTx(tx *harmonydb.Tx, parkedPieceID int64, pi *mk20.PieceInfo) error {
622+
// ensureStorachaRefsTx enforces the intended one-to-one shape for this migration
623+
// mode. Direct imports create storacha-migration refs and PDP refs; aggregate
624+
// imports create storacha-migration-aggregate refs and intentionally skip PDP refs.
625+
func ensureStorachaRefsTx(tx *harmonydb.Tx, parkedPieceID int64, pi *mk20.PieceInfo, makePDPPieceRef bool) error {
626+
dataURL := storachaMigrationAggregateDataURL
627+
if makePDPPieceRef {
628+
dataURL = storachaMigrationDataURL
629+
}
630+
626631
var refs []struct {
627632
RefID int64 `db:"ref_id"`
628633
}
@@ -633,7 +638,7 @@ func ensureStorachaRefsTx(tx *harmonydb.Tx, parkedPieceID int64, pi *mk20.PieceI
633638
AND data_url = $2
634639
AND long_term = TRUE
635640
ORDER BY ref_id
636-
FOR UPDATE`, parkedPieceID, storachaMigrationDataURL)
641+
FOR UPDATE`, parkedPieceID, dataURL)
637642
if err != nil {
638643
return xerrors.Errorf("query storacha parked piece refs: %w", err)
639644
}
@@ -648,18 +653,26 @@ func ensureStorachaRefsTx(tx *harmonydb.Tx, parkedPieceID int64, pi *mk20.PieceI
648653
err = tx.QueryRow(`
649654
INSERT INTO parked_piece_refs (piece_id, data_url, long_term)
650655
VALUES ($1, $2, TRUE)
651-
RETURNING ref_id`, parkedPieceID, storachaMigrationDataURL).Scan(&pieceRef)
656+
RETURNING ref_id`, parkedPieceID, dataURL).Scan(&pieceRef)
652657
if err != nil {
653658
return xerrors.Errorf("insert storacha parked piece ref: %w", err)
654659
}
655660
}
656661

662+
if !makePDPPieceRef {
663+
return nil
664+
}
665+
666+
return ensurePDPPieceRef(tx, pieceRef, pi)
667+
}
668+
669+
func ensurePDPPieceRef(tx *harmonydb.Tx, pieceRef int64, pi *mk20.PieceInfo) error {
657670
var pdpRefs []struct {
658671
ID int64 `db:"id"`
659672
Service string `db:"service"`
660673
PieceCID string `db:"piece_cid"`
661674
}
662-
err = tx.Select(&pdpRefs, `
675+
err := tx.Select(&pdpRefs, `
663676
SELECT id, service, piece_cid
664677
FROM pdp_piecerefs
665678
WHERE piece_ref = $1

cmd/curio/storacha-migration_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,11 @@ func parkedPieceByID(t *testing.T, env storachaMigrationTestEnv, pieceID int64)
666666

667667
func storachaRefIDs(t *testing.T, env storachaMigrationTestEnv, pieceID int64) []int64 {
668668
t.Helper()
669+
return storachaRefIDsForDataURL(t, env, pieceID, storachaMigrationDataURL)
670+
}
671+
672+
func storachaRefIDsForDataURL(t *testing.T, env storachaMigrationTestEnv, pieceID int64, dataURL string) []int64 {
673+
t.Helper()
669674

670675
var rows []struct {
671676
RefID int64 `db:"ref_id"`
@@ -677,7 +682,7 @@ func storachaRefIDs(t *testing.T, env storachaMigrationTestEnv, pieceID int64) [
677682
AND data_url = $2
678683
AND long_term = TRUE
679684
ORDER BY ref_id
680-
`, pieceID, storachaMigrationDataURL)
685+
`, pieceID, dataURL)
681686
require.NoError(t, err)
682687

683688
out := make([]int64, 0, len(rows))
@@ -707,6 +712,11 @@ func assertStorachaRefs(t *testing.T, env storachaMigrationTestEnv, pieceID int6
707712
require.Len(t, storachaRefIDs(t, env, pieceID), expected)
708713
}
709714

715+
func assertStorachaRefsWithDataURL(t *testing.T, env storachaMigrationTestEnv, pieceID int64, dataURL string, expected int) {
716+
t.Helper()
717+
require.Len(t, storachaRefIDsForDataURL(t, env, pieceID, dataURL), expected)
718+
}
719+
710720
func assertPDPRefs(t *testing.T, env storachaMigrationTestEnv, pieceID int64, pieceCID string, expected int) {
711721
t.Helper()
712722
refs := pdpRefsForPiece(t, env, pieceID)

0 commit comments

Comments
 (0)