-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
refactor: use epochs to gc eth tx hashes from chain indexer #12516
Changes from 3 commits
b04162b
6587808
29c1566
efe4582
da05e17
da1e02b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ package index | |
|
||
import ( | ||
"context" | ||
"strconv" | ||
"time" | ||
|
||
logging "github.com/ipfs/go-log/v2" | ||
|
@@ -75,17 +74,17 @@ func (si *SqliteIndexer) gc(ctx context.Context) { | |
// ------------------------------------------------------------------------------------------------- | ||
// Also GC eth hashes | ||
|
||
// Convert gcRetentionEpochs to number of days | ||
gcRetentionDays := si.gcRetentionEpochs / (builtin.EpochsInDay) | ||
if gcRetentionDays < 1 { | ||
log.Infof("skipping gc of eth hashes as retention days is less than 1") | ||
return | ||
} | ||
currHeadTime := time.Unix(int64(head.MinTimestamp()), 0) | ||
retentionDuration := time.Duration(si.gcRetentionEpochs*builtin.EpochDurationSeconds) * time.Second | ||
|
||
// gcTime is the time that is gcRetentionEpochs before currHeadTime | ||
gcTime := currHeadTime.Add(-retentionDuration) | ||
|
||
log.Infof("gc'ing eth hashes before time %s", gcTime.UTC().String()) | ||
|
||
log.Infof("gc'ing eth hashes older than %d days", gcRetentionDays) | ||
res, err = si.stmts.removeEthHashesOlderThanStmt.ExecContext(ctx, "-"+strconv.Itoa(int(gcRetentionDays))+" day") | ||
res, err = si.stmts.removeEthHashesBeforeTimeStmt.ExecContext(ctx, gcTime.Unix()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need to do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just wanted to provide as accurate time as possible, so I choose unix. |
||
if err != nil { | ||
log.Errorf("failed to gc eth hashes older than %d days: %w", gcRetentionDays, err) | ||
log.Errorf("failed to gc eth hashes before time %s: %w", gcTime.String(), err) | ||
return | ||
} | ||
|
||
|
@@ -95,5 +94,5 @@ func (si *SqliteIndexer) gc(ctx context.Context) { | |
return | ||
} | ||
|
||
log.Infof("gc'd %d eth hashes older than %d days", rows, gcRetentionDays) | ||
log.Infof("gc'd %d eth hashes before time %s", rows, gcTime.String()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,57 +11,66 @@ import ( | |
|
||
func TestGC(t *testing.T) { | ||
ctx := context.Background() | ||
rng := pseudo.New(pseudo.NewSource(time.Now().UnixNano())) | ||
genesisTime := time.Now() | ||
rng := pseudo.New(pseudo.NewSource(genesisTime.UnixNano())) | ||
|
||
// head at height 60 | ||
// insert tipsets at heigh 1,10,50. | ||
// retention epochs is 20 | ||
// tipset at height 60 will be in the future | ||
si, _, _ := setupWithHeadIndexed(t, 60, rng) | ||
si.gcRetentionEpochs = 20 | ||
defer func() { _ = si.Close() }() | ||
|
||
tsCid1 := randomCid(t, rng) | ||
tsCid10 := randomCid(t, rng) | ||
tsCid50 := randomCid(t, rng) | ||
|
||
insertTipsetMessage(t, si, tipsetMessage{ | ||
tipsetKeyCid: tsCid1.Bytes(), | ||
height: 1, | ||
reverted: false, | ||
messageCid: randomCid(t, rng).Bytes(), | ||
messageIndex: 0, | ||
}) | ||
|
||
insertTipsetMessage(t, si, tipsetMessage{ | ||
tipsetKeyCid: tsCid10.Bytes(), | ||
height: 10, | ||
reverted: false, | ||
messageCid: randomCid(t, rng).Bytes(), | ||
messageIndex: 0, | ||
}) | ||
|
||
insertTipsetMessage(t, si, tipsetMessage{ | ||
tipsetKeyCid: tsCid50.Bytes(), | ||
height: 50, | ||
reverted: false, | ||
messageCid: randomCid(t, rng).Bytes(), | ||
messageIndex: 0, | ||
}) | ||
// all tipsets will be in future | ||
tsKeyCid1, _, _ := insertRandomTipsetAtHeight(t, si, 1, false, genesisTime) | ||
tsKeyCid2, _, _ := insertRandomTipsetAtHeight(t, si, 10, false, genesisTime) | ||
tsKeyCid3, _, _ := insertRandomTipsetAtHeight(t, si, 50, false, genesisTime) | ||
|
||
si.gc(ctx) | ||
|
||
// tipset at height 1 and 10 should be removed | ||
// tipset at height 1 data should be removed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dont understand this comment. what's "height 1 data" ? |
||
var count int | ||
err := si.db.QueryRow("SELECT COUNT(*) FROM tipset_message WHERE height = 1").Scan(&count) | ||
require.NoError(t, err) | ||
require.Equal(t, 0, count) | ||
|
||
err = si.stmts.getNonRevertedTipsetEventCountStmt.QueryRow(tsKeyCid1.Bytes()).Scan(&count) | ||
require.NoError(t, err) | ||
require.Equal(t, 0, count) | ||
|
||
err = si.stmts.getNonRevertedTipsetEventEntriesCountStmt.QueryRow(tsKeyCid1.Bytes()).Scan(&count) | ||
require.NoError(t, err) | ||
require.Equal(t, 0, count) | ||
|
||
// tipset at height 10 data should be removed | ||
err = si.db.QueryRow("SELECT COUNT(*) FROM tipset_message WHERE height = 10").Scan(&count) | ||
require.NoError(t, err) | ||
require.Equal(t, 0, count) | ||
|
||
err = si.stmts.getNonRevertedTipsetEventCountStmt.QueryRow(tsKeyCid2.Bytes()).Scan(&count) | ||
require.NoError(t, err) | ||
require.Equal(t, 0, count) | ||
|
||
err = si.stmts.getNonRevertedTipsetEventEntriesCountStmt.QueryRow(tsKeyCid2.Bytes()).Scan(&count) | ||
require.NoError(t, err) | ||
require.Equal(t, 0, count) | ||
|
||
// tipset at height 50 should not be removed | ||
err = si.db.QueryRow("SELECT COUNT(*) FROM tipset_message WHERE height = 50").Scan(&count) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lot of duplication here. Can we wrap this and the two sql statements below into a function and re-use it everywhere ? |
||
require.NoError(t, err) | ||
require.Equal(t, 1, count) | ||
|
||
err = si.stmts.getNonRevertedTipsetEventCountStmt.QueryRow(tsKeyCid3.Bytes()).Scan(&count) | ||
require.NoError(t, err) | ||
require.Equal(t, 1, count) | ||
|
||
err = si.stmts.getNonRevertedTipsetEventEntriesCountStmt.QueryRow(tsKeyCid3.Bytes()).Scan(&count) | ||
require.NoError(t, err) | ||
require.Equal(t, 1, count) | ||
|
||
err = si.db.QueryRow("SELECT COUNT(*) FROM eth_tx_hash").Scan(&count) | ||
require.NoError(t, err) | ||
// eth_tx_hash for tipset at height 50 timestamp should not be removed | ||
require.Equal(t, 1, count) | ||
} |
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.
Can you add a comment explaining this calculation here ?
Why do we need time.Duration(si.gcRetentionEpochs*builtin.EpochDurationSeconds) * time.Second ?