graph/db: move sanity check out of insertChannel
We do this so that this lookup is only done in the situation it is actually needed. During a migration, we dont need to special case this AlreadyExists error since we will terminate the transaction either way. So there is no need for the extra lookup during the migration. A timing analysis showed that this query was significantly impacting the performance of the migration when run with a postgres backend.
This commit is contained in:
1 file changed
+19
-23
+19
-23
@@ -591,15 +591,29 @@ func (s *SQLStore) AddChannelEdge(ctx context.Context,
|
|||||||
alreadyExists = false
|
alreadyExists = false
|
||||||
},
|
},
|
||||||
Do: func(tx SQLQueries) error {
|
Do: func(tx SQLQueries) error {
|
||||||
_, err := insertChannel(ctx, tx, edge)
|
chanIDB := channelIDToBytes(edge.ChannelID)
|
||||||
|
|
||||||
// Silence ErrEdgeAlreadyExist so that the batch can
|
// Make sure that the channel doesn't already exist. We
|
||||||
// succeed, but propagate the error via local state.
|
// do this explicitly instead of relying on catching a
|
||||||
if errors.Is(err, ErrEdgeAlreadyExist) {
|
// unique constraint error because relying on SQL to
|
||||||
|
// throw that error would abort the entire batch of
|
||||||
|
// transactions.
|
||||||
|
_, err := tx.GetChannelBySCID(
|
||||||
|
ctx, sqlc.GetChannelBySCIDParams{
|
||||||
|
Scid: chanIDB,
|
||||||
|
Version: int16(ProtocolV1),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if err == nil {
|
||||||
alreadyExists = true
|
alreadyExists = true
|
||||||
return nil
|
return nil
|
||||||
|
} else if !errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return fmt.Errorf("unable to fetch channel: %w",
|
||||||
|
err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_, err = insertChannel(ctx, tx, edge)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
},
|
},
|
||||||
OnCommit: func(err error) error {
|
OnCommit: func(err error) error {
|
||||||
@@ -3767,24 +3781,6 @@ type dbChanInfo struct {
|
|||||||
func insertChannel(ctx context.Context, db SQLQueries,
|
func insertChannel(ctx context.Context, db SQLQueries,
|
||||||
edge *models.ChannelEdgeInfo) (*dbChanInfo, error) {
|
edge *models.ChannelEdgeInfo) (*dbChanInfo, error) {
|
||||||
|
|
||||||
chanIDB := channelIDToBytes(edge.ChannelID)
|
|
||||||
|
|
||||||
// Make sure that the channel doesn't already exist. We do this
|
|
||||||
// explicitly instead of relying on catching a unique constraint error
|
|
||||||
// because relying on SQL to throw that error would abort the entire
|
|
||||||
// batch of transactions.
|
|
||||||
_, err := db.GetChannelBySCID(
|
|
||||||
ctx, sqlc.GetChannelBySCIDParams{
|
|
||||||
Scid: chanIDB,
|
|
||||||
Version: int16(ProtocolV1),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err == nil {
|
|
||||||
return nil, ErrEdgeAlreadyExist
|
|
||||||
} else if !errors.Is(err, sql.ErrNoRows) {
|
|
||||||
return nil, fmt.Errorf("unable to fetch channel: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make sure that at least a "shell" entry for each node is present in
|
// Make sure that at least a "shell" entry for each node is present in
|
||||||
// the nodes table.
|
// the nodes table.
|
||||||
node1DBID, err := maybeCreateShellNode(ctx, db, edge.NodeKey1Bytes)
|
node1DBID, err := maybeCreateShellNode(ctx, db, edge.NodeKey1Bytes)
|
||||||
@@ -3804,7 +3800,7 @@ func insertChannel(ctx context.Context, db SQLQueries,
|
|||||||
|
|
||||||
createParams := sqlc.CreateChannelParams{
|
createParams := sqlc.CreateChannelParams{
|
||||||
Version: int16(ProtocolV1),
|
Version: int16(ProtocolV1),
|
||||||
Scid: chanIDB,
|
Scid: channelIDToBytes(edge.ChannelID),
|
||||||
NodeID1: node1DBID,
|
NodeID1: node1DBID,
|
||||||
NodeID2: node2DBID,
|
NodeID2: node2DBID,
|
||||||
Outpoint: edge.ChannelPoint.String(),
|
Outpoint: edge.ChannelPoint.String(),
|
||||||
|
|||||||
Reference in new issue
Block a user