Skip to content

Commit f2ed556

Browse files
committed
graph/db+sqldb: improve performance of chan update sql migration
This commit simplifies insertChanEdgePolicyMig. Much of the logic can be removed given that this method is only used in the context of the graph SQL migration. This should improve the performance of the migration quite a lot since it removes the extra GetChannelAndNodesBySCID call.
1 parent 22bf88e commit f2ed556

File tree

5 files changed

+66
-79
lines changed

5 files changed

+66
-79
lines changed

graph/db/sql_migration.go

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,9 @@ func migrateChannelsAndPolicies(ctx context.Context, cfg *SQLStoreConfig,
440440
Interval: 10 * time.Second,
441441
}
442442
)
443-
migChanPolicy := func(policy *models.ChannelEdgePolicy) error {
443+
migChanPolicy := func(dbChanInfo *dbChanInfo,
444+
policy *models.ChannelEdgePolicy) error {
445+
444446
// If the policy is nil, we can skip it.
445447
if policy == nil {
446448
return nil
@@ -454,7 +456,7 @@ func migrateChannelsAndPolicies(ctx context.Context, cfg *SQLStoreConfig,
454456

455457
policyCount++
456458

457-
_, _, _, err := insertChanEdgePolicyMig(ctx, sqlDB, policy)
459+
err := insertChanEdgePolicyMig(ctx, sqlDB, dbChanInfo, policy)
458460
if err != nil {
459461
return fmt.Errorf("could not migrate channel "+
460462
"policy %d: %w", policy.ChannelID, err)
@@ -531,12 +533,12 @@ func migrateChannelsAndPolicies(ctx context.Context, cfg *SQLStoreConfig,
531533
}
532534

533535
// Now, migrate the two channel policies for the channel.
534-
err = migChanPolicy(policy1)
536+
err = migChanPolicy(dbChanInfo, policy1)
535537
if err != nil {
536538
return fmt.Errorf("could not migrate policy1(%d): %w",
537539
scid, err)
538540
}
539-
err = migChanPolicy(policy2)
541+
err = migChanPolicy(dbChanInfo, policy2)
540542
if err != nil {
541543
return fmt.Errorf("could not migrate policy2(%d): %w",
542544
scid, err)
@@ -1564,45 +1566,14 @@ func insertChannelMig(ctx context.Context, db SQLQueries,
15641566
// insertChanEdgePolicyMig inserts the channel policy info we have stored for
15651567
// a channel we already know of. This is used during the SQL migration
15661568
// process to insert channel policies.
1567-
//
1568-
// TODO(elle): update this function to be more performant in the migration
1569-
// setting. For the sake of keeping the commit that introduced this function
1570-
// simple, this is for now mostly the same as updateChanEdgePolicy.
15711569
func insertChanEdgePolicyMig(ctx context.Context, tx SQLQueries,
1572-
edge *models.ChannelEdgePolicy) (route.Vertex, route.Vertex, bool,
1573-
error) {
1574-
1575-
var (
1576-
node1Pub, node2Pub route.Vertex
1577-
isNode1 bool
1578-
chanIDB = channelIDToBytes(edge.ChannelID)
1579-
)
1580-
1581-
// Check that this edge policy refers to a channel that we already
1582-
// know of. We do this explicitly so that we can return the appropriate
1583-
// ErrEdgeNotFound error if the channel doesn't exist, rather than
1584-
// abort the transaction which would abort the entire batch.
1585-
dbChan, err := tx.GetChannelAndNodesBySCID(
1586-
ctx, sqlc.GetChannelAndNodesBySCIDParams{
1587-
Scid: chanIDB,
1588-
Version: int16(ProtocolV1),
1589-
},
1590-
)
1591-
if errors.Is(err, sql.ErrNoRows) {
1592-
return node1Pub, node2Pub, false, ErrEdgeNotFound
1593-
} else if err != nil {
1594-
return node1Pub, node2Pub, false, fmt.Errorf("unable to "+
1595-
"fetch channel(%v): %w", edge.ChannelID, err)
1596-
}
1597-
1598-
copy(node1Pub[:], dbChan.Node1PubKey)
1599-
copy(node2Pub[:], dbChan.Node2PubKey)
1570+
dbChan *dbChanInfo, edge *models.ChannelEdgePolicy) error {
16001571

16011572
// Figure out which node this edge is from.
1602-
isNode1 = edge.ChannelFlags&lnwire.ChanUpdateDirection == 0
1603-
nodeID := dbChan.NodeID1
1573+
isNode1 := edge.ChannelFlags&lnwire.ChanUpdateDirection == 0
1574+
nodeID := dbChan.node1ID
16041575
if !isNode1 {
1605-
nodeID = dbChan.NodeID2
1576+
nodeID = dbChan.node2ID
16061577
}
16071578

16081579
var (
@@ -1616,7 +1587,7 @@ func insertChanEdgePolicyMig(ctx context.Context, tx SQLQueries,
16161587

16171588
id, err := tx.InsertEdgePolicyMig(ctx, sqlc.InsertEdgePolicyMigParams{
16181589
Version: int16(ProtocolV1),
1619-
ChannelID: dbChan.ID,
1590+
ChannelID: dbChan.channelID,
16201591
NodeID: nodeID,
16211592
Timelock: int32(edge.TimeLockDelta),
16221593
FeePpm: int64(edge.FeeProportionalMillionths),
@@ -1638,24 +1609,32 @@ func insertChanEdgePolicyMig(ctx context.Context, tx SQLQueries,
16381609
Signature: edge.SigBytes,
16391610
})
16401611
if err != nil {
1641-
return node1Pub, node2Pub, isNode1,
1642-
fmt.Errorf("unable to upsert edge policy: %w", err)
1612+
return fmt.Errorf("unable to upsert edge policy: %w", err)
16431613
}
16441614

16451615
// Convert the flat extra opaque data into a map of TLV types to
16461616
// values.
16471617
extra, err := marshalExtraOpaqueData(edge.ExtraOpaqueData)
16481618
if err != nil {
1649-
return node1Pub, node2Pub, false, fmt.Errorf("unable to "+
1650-
"marshal extra opaque data: %w", err)
1619+
return fmt.Errorf("unable to marshal extra opaque data: %w",
1620+
err)
16511621
}
16521622

1653-
// Update the channel policy's extra signed fields.
1654-
err = upsertChanPolicyExtraSignedFields(ctx, tx, id, extra)
1655-
if err != nil {
1656-
return node1Pub, node2Pub, false, fmt.Errorf("inserting chan "+
1657-
"policy extra TLVs: %w", err)
1623+
// Insert all new extra signed fields for the channel policy.
1624+
for tlvType, value := range extra {
1625+
err = tx.UpsertChanPolicyExtraType(
1626+
ctx, sqlc.UpsertChanPolicyExtraTypeParams{
1627+
ChannelPolicyID: id,
1628+
Type: int64(tlvType),
1629+
Value: value,
1630+
},
1631+
)
1632+
if err != nil {
1633+
return fmt.Errorf("unable to insert "+
1634+
"channel_policy(%d) extra signed field(%v): %w",
1635+
id, tlvType, err)
1636+
}
16581637
}
16591638

1660-
return node1Pub, node2Pub, isNode1, nil
1639+
return nil
16611640
}

graph/db/sql_store.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ type SQLQueries interface {
124124
GetChannelPolicyByChannelAndNode(ctx context.Context, arg sqlc.GetChannelPolicyByChannelAndNodeParams) (sqlc.GraphChannelPolicy, error)
125125
GetV1DisabledSCIDs(ctx context.Context) ([][]byte, error)
126126

127-
InsertChanPolicyExtraType(ctx context.Context, arg sqlc.InsertChanPolicyExtraTypeParams) error
127+
UpsertChanPolicyExtraType(ctx context.Context, arg sqlc.UpsertChanPolicyExtraTypeParams) error
128128
GetChannelPolicyExtraTypesBatch(ctx context.Context, policyIds []int64) ([]sqlc.GetChannelPolicyExtraTypesBatchRow, error)
129129
DeleteChannelPolicyExtraTypes(ctx context.Context, channelPolicyID int64) error
130130

@@ -3934,8 +3934,8 @@ func upsertChanPolicyExtraSignedFields(ctx context.Context, db SQLQueries,
39343934

39353935
// Insert all new extra signed fields for the channel policy.
39363936
for tlvType, value := range extraFields {
3937-
err = db.InsertChanPolicyExtraType(
3938-
ctx, sqlc.InsertChanPolicyExtraTypeParams{
3937+
err = db.UpsertChanPolicyExtraType(
3938+
ctx, sqlc.UpsertChanPolicyExtraTypeParams{
39393939
ChannelPolicyID: chanPolicyID,
39403940
Type: int64(tlvType),
39413941
Value: value,

sqldb/sqlc/graph.sql.go

Lines changed: 27 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sqldb/sqlc/querier.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sqldb/sqlc/queries/graph.sql

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -869,11 +869,15 @@ WHERE c.scid = @scid
869869
─────────────────────────────────────────────
870870
*/
871871

872-
-- name: InsertChanPolicyExtraType :exec
872+
-- name: UpsertChanPolicyExtraType :exec
873873
INSERT INTO graph_channel_policy_extra_types (
874874
channel_policy_id, type, value
875875
)
876-
VALUES ($1, $2, $3);
876+
VALUES ($1, $2, $3)
877+
ON CONFLICT (channel_policy_id, type)
878+
-- If a conflict occurs on channel_policy_id and type, then we update the
879+
-- value.
880+
DO UPDATE SET value = EXCLUDED.value;
877881

878882
-- name: GetChannelPolicyExtraTypesBatch :many
879883
SELECT

0 commit comments

Comments
 (0)