-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
CCIP-2971 Optimize token/gas prices database interactions #14074
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8f9bf8e
Simplify codebase and improve performance by switching to upsert
mateusz-sekara 027a65e
Update core/services/ccip/orm.go
mateusz-sekara 0cb98b5
Post review fixes
mateusz-sekara 850a7d0
Merge branch 'develop' into optimize-token-prices
mateusz-sekara 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
This file contains 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,5 @@ | ||
--- | ||
"chainlink": patch | ||
--- | ||
|
||
Simplify how token and gas prices are stored in the database - user upsert instead of insert/delete flow #db_update |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,115 @@ | ||
package ccip | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/sqlutil" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
|
||
"github.com/smartcontractkit/chainlink/v2/core/logger" | ||
) | ||
|
||
var ( | ||
sqlLatencyBuckets = []float64{ | ||
float64(10 * time.Millisecond), | ||
float64(20 * time.Millisecond), | ||
float64(30 * time.Millisecond), | ||
float64(40 * time.Millisecond), | ||
float64(50 * time.Millisecond), | ||
float64(70 * time.Millisecond), | ||
float64(90 * time.Millisecond), | ||
float64(100 * time.Millisecond), | ||
float64(200 * time.Millisecond), | ||
float64(300 * time.Millisecond), | ||
float64(400 * time.Millisecond), | ||
float64(500 * time.Millisecond), | ||
float64(750 * time.Millisecond), | ||
float64(1 * time.Second), | ||
} | ||
ccipQueryDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{ | ||
Name: "ccip_orm_query_duration", | ||
Buckets: sqlLatencyBuckets, | ||
}, []string{"query", "destChainSelector"}) | ||
ccipQueryDatasets = promauto.NewGaugeVec(prometheus.GaugeOpts{ | ||
Name: "ccip_orm_dataset_size", | ||
}, []string{"query", "destChainSelector"}) | ||
) | ||
|
||
type observedORM struct { | ||
ORM | ||
queryDuration *prometheus.HistogramVec | ||
datasetSize *prometheus.GaugeVec | ||
} | ||
|
||
var _ ORM = (*observedORM)(nil) | ||
|
||
func NewObservedORM(ds sqlutil.DataSource, lggr logger.Logger) (*observedORM, error) { | ||
delegate, err := NewORM(ds, lggr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &observedORM{ | ||
ORM: delegate, | ||
queryDuration: ccipQueryDuration, | ||
datasetSize: ccipQueryDatasets, | ||
}, nil | ||
} | ||
|
||
func (o *observedORM) GetGasPricesByDestChain(ctx context.Context, destChainSelector uint64) ([]GasPrice, error) { | ||
return withObservedQueryAndResults(o, "GetGasPricesByDestChain", destChainSelector, func() ([]GasPrice, error) { | ||
return o.ORM.GetGasPricesByDestChain(ctx, destChainSelector) | ||
}) | ||
} | ||
|
||
func (o *observedORM) GetTokenPricesByDestChain(ctx context.Context, destChainSelector uint64) ([]TokenPrice, error) { | ||
return withObservedQueryAndResults(o, "GetTokenPricesByDestChain", destChainSelector, func() ([]TokenPrice, error) { | ||
return o.ORM.GetTokenPricesByDestChain(ctx, destChainSelector) | ||
}) | ||
} | ||
|
||
func (o *observedORM) UpsertGasPricesForDestChain(ctx context.Context, destChainSelector uint64, gasPrices []GasPrice) (int64, error) { | ||
return withObservedQueryAndRowsAffected(o, "UpsertGasPricesForDestChain", destChainSelector, func() (int64, error) { | ||
return o.ORM.UpsertGasPricesForDestChain(ctx, destChainSelector, gasPrices) | ||
}) | ||
} | ||
|
||
func (o *observedORM) UpsertTokenPricesForDestChain(ctx context.Context, destChainSelector uint64, tokenPrices []TokenPrice, interval time.Duration) (int64, error) { | ||
return withObservedQueryAndRowsAffected(o, "UpsertTokenPricesForDestChain", destChainSelector, func() (int64, error) { | ||
return o.ORM.UpsertTokenPricesForDestChain(ctx, destChainSelector, tokenPrices, interval) | ||
}) | ||
} | ||
|
||
func withObservedQueryAndRowsAffected(o *observedORM, queryName string, chainSelector uint64, query func() (int64, error)) (int64, error) { | ||
rowsAffected, err := withObservedQuery(o, queryName, chainSelector, query) | ||
if err == nil { | ||
o.datasetSize. | ||
WithLabelValues(queryName, strconv.FormatUint(chainSelector, 10)). | ||
Set(float64(rowsAffected)) | ||
} | ||
return rowsAffected, err | ||
} | ||
|
||
func withObservedQueryAndResults[T any](o *observedORM, queryName string, chainSelector uint64, query func() ([]T, error)) ([]T, error) { | ||
results, err := withObservedQuery(o, queryName, chainSelector, query) | ||
if err == nil { | ||
o.datasetSize. | ||
WithLabelValues(queryName, strconv.FormatUint(chainSelector, 10)). | ||
Set(float64(len(results))) | ||
} | ||
return results, err | ||
} | ||
|
||
func withObservedQuery[T any](o *observedORM, queryName string, chainSelector uint64, query func() (T, error)) (T, error) { | ||
queryStarted := time.Now() | ||
defer func() { | ||
o.queryDuration. | ||
WithLabelValues(queryName, strconv.FormatUint(chainSelector, 10)). | ||
Observe(float64(time.Since(queryStarted))) | ||
}() | ||
return query() | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way I understand these buckets is that if the latency is above 1s you wouldn't really know how high it is, are we confident that 1s is the max possible for our use case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything above 1s for such small queries is super bad, we don't need more buckets above 1s