Skip to content

Commit

Permalink
Analyze everything
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz-sekara committed Aug 6, 2024
1 parent 1006a65 commit 5aaff4f
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion core/chains/evm/logpoller/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/smartcontractkit/chainlink-common/pkg/types/query"

"github.com/smartcontractkit/chainlink-common/pkg/sqlutil"

evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types"

"github.com/smartcontractkit/chainlink/v2/core/chains/evm/types"
Expand Down Expand Up @@ -431,6 +432,8 @@ func (o *DSORM) SelectLogsByBlockRange(ctx context.Context, start, end int64) ([
AND block_number <= :end_block
ORDER BY block_number, log_index`

o.withAnalyze(ctx, "SelectLogsByBlockRange", query, args)

var logs []Log
query, sqlArgs, err := o.ds.BindNamed(query, args)
if err != nil {
Expand Down Expand Up @@ -462,6 +465,8 @@ func (o *DSORM) SelectLogs(ctx context.Context, start, end int64, address common
AND block_number <= :end_block
ORDER BY block_number, log_index`

o.withAnalyze(ctx, "SelectLogs", query, args)

var logs []Log
query, sqlArgs, err := o.ds.BindNamed(query, args)
if err != nil {
Expand Down Expand Up @@ -494,6 +499,8 @@ func (o *DSORM) SelectLogsCreatedAfter(ctx context.Context, address common.Addre
AND block_number <= %s
ORDER BY block_number, log_index`, nestedBlockNumberQuery(confs))

o.withAnalyze(ctx, "SelectLogsCreatedAfter", query, args)

var logs []Log
query, sqlArgs, err := o.ds.BindNamed(query, args)
if err != nil {
Expand Down Expand Up @@ -526,6 +533,8 @@ func (o *DSORM) SelectLogsWithSigs(ctx context.Context, start, end int64, addres
AND block_number BETWEEN :start_block AND :end_block
ORDER BY block_number, log_index`

o.withAnalyze(ctx, "SelectLogsWithSigs", query, args)

query, sqlArgs, err := o.ds.BindNamed(query, args)
if err != nil {
return nil, err
Expand Down Expand Up @@ -553,6 +562,8 @@ func (o *DSORM) GetBlocksRange(ctx context.Context, start int64, end int64) ([]L
AND evm_chain_id = :evm_chain_id
ORDER BY block_number ASC`

o.withAnalyze(ctx, "GetBlocksRange", query, args)

var blocks []LogPollerBlock
query, sqlArgs, err := o.ds.BindNamed(query, args)
if err != nil {
Expand Down Expand Up @@ -590,6 +601,8 @@ func (o *DSORM) SelectLatestLogEventSigsAddrsWithConfs(ctx context.Context, from
)
ORDER BY block_number ASC`, nestedBlockNumberQuery(confs))

o.withAnalyze(ctx, "SelectLatestLogEventSigsAddrsWithConfs", query, args)

var logs []Log
query, sqlArgs, err := o.ds.BindNamed(query, args)
if err != nil {
Expand Down Expand Up @@ -621,6 +634,8 @@ func (o *DSORM) SelectLatestBlockByEventSigsAddrsWithConfs(ctx context.Context,
AND block_number > :start_block
AND block_number <= %s`, nestedBlockNumberQuery(confs))

o.withAnalyze(ctx, "SelectLatestBlockByEventSigsAddrsWithConfs", query, args)

var blockNumber int64
query, sqlArgs, err := o.ds.BindNamed(query, args)
if err != nil {
Expand Down Expand Up @@ -653,6 +668,8 @@ func (o *DSORM) SelectLogsDataWordRange(ctx context.Context, address common.Addr
AND block_number <= %s
ORDER BY block_number, log_index`, nestedBlockNumberQuery(confs))

o.withAnalyze(ctx, "SelectLogsDataWordRange", query, args)

var logs []Log
query, sqlArgs, err := o.ds.BindNamed(query, args)
if err != nil {
Expand Down Expand Up @@ -684,6 +701,8 @@ func (o *DSORM) SelectLogsDataWordGreaterThan(ctx context.Context, address commo
AND block_number <= %s
ORDER BY block_number, log_index`, nestedBlockNumberQuery(confs))

o.withAnalyze(ctx, "SelectLogsDataWordGreaterThan", query, args)

var logs []Log
query, sqlArgs, err := o.ds.BindNamed(query, args)
if err != nil {
Expand Down Expand Up @@ -715,6 +734,7 @@ func (o *DSORM) SelectLogsDataWordBetween(ctx context.Context, address common.Ad
AND substring(data from 32*:word_index_max+1 for 32) >= :word_value
AND block_number <= %s
ORDER BY block_number, log_index`, nestedBlockNumberQuery(confs))
o.withAnalyze(ctx, "SelectLogsDataWordBetween", query, args)

var logs []Log
query, sqlArgs, err := o.ds.BindNamed(query, args)
Expand Down Expand Up @@ -747,6 +767,8 @@ func (o *DSORM) SelectIndexedLogsTopicGreaterThan(ctx context.Context, address c
AND block_number <= %s
ORDER BY block_number, log_index`, nestedBlockNumberQuery(confs))

o.withAnalyze(ctx, "SelectIndexedLogsTopicGreaterThan", query, args)

var logs []Log
query, sqlArgs, err := o.ds.BindNamed(query, args)
if err != nil {
Expand Down Expand Up @@ -780,6 +802,8 @@ func (o *DSORM) SelectIndexedLogsTopicRange(ctx context.Context, address common.
AND block_number <= %s
ORDER BY block_number, log_index`, nestedBlockNumberQuery(confs))

o.withAnalyze(ctx, "SelectIndexedLogsTopicRange", query, args)

var logs []Log
query, sqlArgs, err := o.ds.BindNamed(query, args)
if err != nil {
Expand Down Expand Up @@ -811,6 +835,8 @@ func (o *DSORM) SelectIndexedLogs(ctx context.Context, address common.Address, e
AND block_number <= %s
ORDER BY block_number, log_index`, nestedBlockNumberQuery(confs))

o.withAnalyze(ctx, "SelectIndexedLogs", query, args)

var logs []Log
query, sqlArgs, err := o.ds.BindNamed(query, args)
if err != nil {
Expand Down Expand Up @@ -844,6 +870,8 @@ func (o *DSORM) SelectIndexedLogsByBlockRange(ctx context.Context, start, end in
AND block_number <= :end_block
ORDER BY block_number, log_index`

o.withAnalyze(ctx, "SelectIndexedLogsByBlockRange", query, args)

var logs []Log
query, sqlArgs, err := o.ds.BindNamed(query, args)
if err != nil {
Expand Down Expand Up @@ -879,6 +907,8 @@ func (o *DSORM) SelectIndexedLogsCreatedAfter(ctx context.Context, address commo
ORDER BY block_number, log_index
`, nestedBlockNumberQuery(confs))

o.withAnalyze(ctx, "SelectIndexedLogsCreatedAfter", query, args)

var logs []Log
query, sqlArgs, err := o.ds.BindNamed(query, args)
if err != nil {
Expand Down Expand Up @@ -908,6 +938,8 @@ func (o *DSORM) SelectIndexedLogsByTxHash(ctx context.Context, address common.Ad
AND tx_hash = :tx_hash
ORDER BY block_number, log_index`

o.withAnalyze(ctx, "SelectIndexedLogsByTxHash", query, args)

var logs []Log
query, sqlArgs, err := o.ds.BindNamed(query, args)
if err != nil {
Expand Down Expand Up @@ -969,7 +1001,7 @@ func (o *DSORM) SelectIndexedLogsWithSigsExcluding(ctx context.Context, sigA, si
}

// TODO flaky BCF-3258
func (o *DSORM) FilteredLogs(ctx context.Context, filter query.KeyFilter, limitAndSort query.LimitAndSort, _ string) ([]Log, error) {
func (o *DSORM) FilteredLogs(ctx context.Context, filter query.KeyFilter, limitAndSort query.LimitAndSort, queryName string) ([]Log, error) {
qs, args, err := (&pgDSLParser{}).buildQuery(o.chainID, filter.Expressions, limitAndSort)
if err != nil {
return nil, err
Expand All @@ -980,6 +1012,8 @@ func (o *DSORM) FilteredLogs(ctx context.Context, filter query.KeyFilter, limitA
return nil, err
}

o.withAnalyze(ctx, queryName, qs, values)

query, sqlArgs, err := o.ds.BindNamed(qs, values)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1009,3 +1043,19 @@ func nestedBlockNumberQuery(confs evmtypes.Confirmations) string {
WHERE evm_chain_id = :evm_chain_id
ORDER BY block_number DESC LIMIT 1) `
}

func (o *DSORM) withAnalyze(ctx context.Context, queryName string, query string, args map[string]any) {
query, sqlArgs, err := o.ds.BindNamed("EXPLAIN (ANALYZE, BUFFERS) "+query, args)
if err != nil {
return
}

var response []string
err = o.ds.SelectContext(ctx, &response, query, sqlArgs...)
if err != nil {
return
}
if len(response) > 0 {
o.lggr.Infow("Analyze query", "query", queryName, "response", strings.Join(response, "\n"))
}
}

0 comments on commit 5aaff4f

Please sign in to comment.