Skip to content

Commit

Permalink
lmrpc: Don't add refs to pieces which are currently getting removed
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k committed Aug 27, 2024
1 parent 5370f49 commit 594c26b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
3 changes: 2 additions & 1 deletion harmony/harmonydb/sql/20240228-piece-park.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ create table parked_pieces (
foreign key (task_id) references harmony_task (id) on delete set null, -- dropped
foreign key (cleanup_task_id) references harmony_task (id) on delete set null, -- dropped

unique (piece_cid)
unique (piece_cid) -- dropped
-- unique (piece_cid, cleanup_task_id) -- Added in 20240827-piecepark-uniq-cleanup.sql
);

/*
Expand Down
4 changes: 4 additions & 0 deletions harmony/harmonydb/sql/20240827-piecepark-uniq-cleanup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- this migration fixes piecepark unique constraint issue to allow multiple pieceparks with same name when cleanup task is set

ALTER TABLE parked_pieces DROP CONSTRAINT IF EXISTS parked_pieces_piece_cid_key;
ALTER TABLE parked_pieces ADD CONSTRAINT parked_pieces_piece_cid_cleanup_task_id_key UNIQUE (piece_cid, cleanup_task_id);
3 changes: 1 addition & 2 deletions market/lmrpc/piecerefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,14 @@ func (rt *refTracker) addPieceEntry(ctx context.Context, db *harmonydb.DB, conf

var pieceID int64
// Attempt to select the piece ID first
err = tx.QueryRow(`SELECT id FROM parked_pieces WHERE piece_cid = $1`, deal.PieceCID().String()).Scan(&pieceID)
err = tx.QueryRow(`SELECT id FROM parked_pieces WHERE piece_cid = $1 AND cleanup_task_id IS NULL`, deal.PieceCID().String()).Scan(&pieceID)

if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
// Piece does not exist, attempt to insert
err = tx.QueryRow(`
INSERT INTO parked_pieces (piece_cid, piece_padded_size, piece_raw_size)
VALUES ($1, $2, $3)
ON CONFLICT (piece_cid) DO NOTHING
RETURNING id`, deal.PieceCID().String(), int64(pieceSize.Padded()), int64(pieceSize)).Scan(&pieceID)
if err != nil {
return false, xerrors.Errorf("inserting new parked piece and getting id: %w", err)
Expand Down

0 comments on commit 594c26b

Please sign in to comment.