Skip to content
This repository was archived by the owner on Mar 14, 2025. It is now read-only.

Commit a1636a9

Browse files
Restoring previous version of pruning (without left join) (#1544)
## Motivation Outer joins are putting too much pressure on the database. There were some fixes done to improve that, but in 2.18 release. In the meantime, I'm bringing back the previous implementation until we merge 2.18 to CCIP repo. The only difference is that, we not gonna drop the orphaned logs (it would work as the currently released version) ### Using outer join <img width="945" alt="image" src="https://github.com/user-attachments/assets/622c2877-f0fe-4b7b-a9c2-7d785902f88a"> ### Restoring inner join <img width="941" alt="image" src="https://github.com/user-attachments/assets/074176d1-93f1-4fab-a6a5-2a665da2093f">
1 parent 2a76297 commit a1636a9

File tree

4 files changed

+25
-31
lines changed

4 files changed

+25
-31
lines changed

core/chains/evm/logpoller/observability_test.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,7 @@ func TestCountersAreProperlyPopulatedForWrites(t *testing.T) {
119119
assert.Equal(t, float64(20), testutil.ToFloat64(orm.logsInserted.WithLabelValues("420")))
120120
assert.Equal(t, float64(2), testutil.ToFloat64(orm.blocksInserted.WithLabelValues("420")))
121121

122-
rowsAffected, err := orm.DeleteExpiredLogs(ctx, 3)
123-
require.NoError(t, err)
124-
require.Equal(t, int64(3), rowsAffected)
125-
assert.Equal(t, 3, counterFromGaugeByLabels(orm.datasetSize, "420", "DeleteExpiredLogs", "delete"))
126-
127-
rowsAffected, err = orm.DeleteBlocksBefore(ctx, 30, 0)
122+
rowsAffected, err := orm.DeleteBlocksBefore(ctx, 30, 0)
128123
require.NoError(t, err)
129124
require.Equal(t, int64(2), rowsAffected)
130125
assert.Equal(t, 2, counterFromGaugeByLabels(orm.datasetSize, "420", "DeleteBlocksBefore", "delete"))

core/chains/evm/logpoller/orm.go

+18-14
Original file line numberDiff line numberDiff line change
@@ -314,30 +314,34 @@ type Exp struct {
314314
ShouldDelete bool
315315
}
316316

317-
// DeleteExpiredLogs removes any logs which either:
318-
// - don't match any currently registered filters, or
319-
// - have a timestamp older than any matching filter's retention, UNLESS there is at
320-
// least one matching filter with retention=0
321317
func (o *DSORM) DeleteExpiredLogs(ctx context.Context, limit int64) (int64, error) {
322318
var err error
323319
var result sql.Result
324-
query := `DELETE FROM evm.logs
320+
if limit > 0 {
321+
result, err = o.ds.ExecContext(ctx, `
322+
DELETE FROM evm.logs
325323
WHERE (evm_chain_id, address, event_sig, block_number) IN (
326324
SELECT l.evm_chain_id, l.address, l.event_sig, l.block_number
327325
FROM evm.logs l
328-
LEFT JOIN (
329-
SELECT address, event, CASE WHEN MIN(retention) = 0 THEN 0 ELSE MAX(retention) END AS retention
326+
INNER JOIN (
327+
SELECT address, event, MAX(retention) AS retention
330328
FROM evm.log_poller_filters
331329
WHERE evm_chain_id = $1
332330
GROUP BY evm_chain_id, address, event
333-
) r ON l.address = r.address AND l.event_sig = r.event
334-
WHERE l.evm_chain_id = $1 AND -- Must be WHERE rather than ON due to LEFT JOIN
335-
r.retention IS NULL OR (r.retention != 0 AND l.block_timestamp <= STATEMENT_TIMESTAMP() - (r.retention / 10^9 * interval '1 second')) %s)`
336-
337-
if limit > 0 {
338-
result, err = o.ds.ExecContext(ctx, fmt.Sprintf(query, "LIMIT $2"), ubig.New(o.chainID), limit)
331+
HAVING NOT 0 = ANY(ARRAY_AGG(retention))
332+
) r ON l.evm_chain_id = $1 AND l.address = r.address AND l.event_sig = r.event
333+
AND l.block_timestamp <= STATEMENT_TIMESTAMP() - (r.retention / 10^9 * interval '1 second')
334+
LIMIT $2
335+
)`, ubig.New(o.chainID), limit)
339336
} else {
340-
result, err = o.ds.ExecContext(ctx, fmt.Sprintf(query, ""), ubig.New(o.chainID))
337+
result, err = o.ds.ExecContext(ctx, `WITH r AS
338+
( SELECT address, event, MAX(retention) AS retention
339+
FROM evm.log_poller_filters WHERE evm_chain_id=$1
340+
GROUP BY evm_chain_id,address, event HAVING NOT 0 = ANY(ARRAY_AGG(retention))
341+
) DELETE FROM evm.logs l USING r
342+
WHERE l.evm_chain_id = $1 AND l.address=r.address AND l.event_sig=r.event
343+
AND l.block_timestamp <= STATEMENT_TIMESTAMP() - (r.retention / 10^9 * interval '1 second')`, // retention is in nanoseconds (time.Duration aka BIGINT)
344+
ubig.New(o.chainID))
341345
}
342346

343347
if err != nil {

core/chains/evm/logpoller/orm_test.go

+5-10
Original file line numberDiff line numberDiff line change
@@ -489,12 +489,7 @@ func TestORM(t *testing.T) {
489489
time.Sleep(2 * time.Millisecond) // just in case we haven't reached the end of the 1ms retention period
490490
deleted, err := o1.DeleteExpiredLogs(ctx, 2)
491491
require.NoError(t, err)
492-
assert.Equal(t, int64(2), deleted)
493-
494-
// Delete expired logs without page limit
495-
deleted, err = o1.DeleteExpiredLogs(ctx, 0)
496-
require.NoError(t, err)
497-
assert.Equal(t, int64(2), deleted)
492+
assert.Equal(t, int64(1), deleted)
498493

499494
// Ensure that both of the logs from the second chain are still there
500495
logs, err = o2.SelectLogs(ctx, 0, 100, common.HexToAddress("0x1236"), topic2)
@@ -506,10 +501,10 @@ func TestORM(t *testing.T) {
506501

507502
logs, err = o1.SelectLogsByBlockRange(ctx, 1, latest.BlockNumber)
508503
require.NoError(t, err)
509-
// It should have retained the log matching filter0 (due to ret=0 meaning permanent retention) as well as all
510-
// 3 logs matching filter12 (ret=1 hour). It should have deleted 3 logs not matching any filter, as well as 1
511-
// of the 2 logs matching filter1 (ret=1ms)--the one that doesn't also match filter12.
512-
assert.Len(t, logs, 4)
504+
// The only log which should be deleted is the one which matches filter1 (ret=1ms) but not filter12 (ret=1 hour)
505+
// Importantly, it shouldn't delete any logs matching only filter0 (ret=0 meaning permanent retention). Anything
506+
// matching filter12 should be kept regardless of what other filters it matches.
507+
assert.Len(t, logs, 7)
513508

514509
// Delete logs after should delete all logs.
515510
err = o1.DeleteLogsAndBlocksAfter(ctx, 1)

integration-tests/ccip-tests/testconfig/tomls/ccip1.4-stress/baseline.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# If you want to use a specific commit or a branch you need to switch to the internal ECR in `~/.testsecrets`
1616
# E2E_TEST_CHAINLINK_IMAGE="<aws account number>.dkr.ecr.<aws region>.amazonaws.com/chainlink-ccip"
1717
[CCIP.Env.NewCLCluster.Common.ChainlinkImage]
18-
version = "2.14.0-ccip1.5.0"
18+
version = "2.17.0-ccip1.5.11-beta.0"
1919

2020
[CCIP]
2121
[CCIP.ContractVersions]

0 commit comments

Comments
 (0)