Skip to content

Commit

Permalink
add logging per page in safe delete
Browse files Browse the repository at this point in the history
  • Loading branch information
kpeluso committed Oct 22, 2024
1 parent 377d2f9 commit 48499e5
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 7 deletions.
6 changes: 5 additions & 1 deletion utils/migutils/migutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/store/prefix"
storetypes "cosmossdk.io/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// Deletes all keys in the store with the given keyPrefix `maxPageSize` keys at a time
func SafelyClearWholeMap(store storetypes.KVStore, keyPrefix []byte, maxPageSize uint64) error {
func SafelyClearWholeMap(ctx sdk.Context, store storetypes.KVStore, keyPrefix []byte, maxPageSize uint64) error {
s := prefix.NewStore(store, keyPrefix)

// `clearPage` deletes `maxPageSize` keys at a time
Expand Down Expand Up @@ -44,13 +45,16 @@ func SafelyClearWholeMap(store storetypes.KVStore, keyPrefix []byte, maxPageSize
// Loop until all keys are deleted.
// Unbounded not best practice but we are sure that the number of keys will be limited
// and not deleting all keys means "poison" will remain in the store.
count := uint64(0)
for {
ctx.Logger().Info("MIGRATION: DELETING keys in store with prefix", "prefix", keyPrefix, "page", count)
more, err := clearPage()
if err != nil {
return err
} else if !more {
break
}
count++
}
return nil
}
2 changes: 1 addition & 1 deletion utils/migutils/migutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func runSafelyClearWholeMapCase[K, V any](
keys = append(keys, key)
}

err := migutils.SafelyClearWholeMap(testDB.Store, prefix, maxPageSize)
err := migutils.SafelyClearWholeMap(testDB.TestCtx.Ctx, testDB.Store, prefix, maxPageSize)
require.NoError(t, err)

for _, key := range keys {
Expand Down
6 changes: 3 additions & 3 deletions x/emissions/migrations/v3/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func MigrateStore(ctx sdk.Context, emissionsKeeper keeper.Keeper) error {
}

ctx.Logger().Info("INVOKING MIGRATION HANDLER ResetMapsWithNonNumericValues() FROM VERSION 2 TO VERSION 3")
err := ResetMapsWithNonNumericValues(store, cdc)
err := ResetMapsWithNonNumericValues(ctx, store, cdc)
if err != nil {
ctx.Logger().Error("ERROR RESETTING MAPS WITH NON NUMERIC VALUES: %v", err)
return err
Expand Down Expand Up @@ -297,7 +297,7 @@ func getNewTopic(oldMsg oldtypes.Topic) types.Topic {
}
}

func ResetMapsWithNonNumericValues(store storetypes.KVStore, cdc codec.BinaryCodec) error {
func ResetMapsWithNonNumericValues(ctx sdk.Context, store storetypes.KVStore, cdc codec.BinaryCodec) error {
prefixes := []collections.Prefix{
types.InferenceScoresKey,
types.ForecastScoresKey,
Expand All @@ -317,7 +317,7 @@ func ResetMapsWithNonNumericValues(store storetypes.KVStore, cdc codec.BinaryCod
types.LatestOneOutForecasterForecasterNetworkRegretsKey,
}
for _, prefix := range prefixes {
err := migutils.SafelyClearWholeMap(store, prefix, maxPageSize)
err := migutils.SafelyClearWholeMap(ctx, store, prefix, maxPageSize)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion x/emissions/migrations/v4/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func ResetMapsWithNonNumericValues(ctx sdk.Context, store storetypes.KVStore, cd

for _, prefix := range prefixes {
ctx.Logger().Info("MIGRATION V4: RESETTING %v MAP", prefix.name)
err := migutils.SafelyClearWholeMap(store, prefix.prefix, maxPageSize)
err := migutils.SafelyClearWholeMap(ctx, store, prefix.prefix, maxPageSize)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion x/emissions/migrations/v5/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func ResetMapsWithNonNumericValues(ctx sdk.Context, store storetypes.KVStore, cd

for _, prefix := range prefixes {
ctx.Logger().Info(fmt.Sprintf("MIGRATION V5: RESETTING %v MAP", prefix.name))
err := migutils.SafelyClearWholeMap(store, prefix.prefix, maxPageSize)
err := migutils.SafelyClearWholeMap(ctx, store, prefix.prefix, maxPageSize)
if err != nil {
return err
}
Expand Down

0 comments on commit 48499e5

Please sign in to comment.