From 48499e5646ab780364e5f8ee7b328b86775cc590 Mon Sep 17 00:00:00 2001 From: Kenny Date: Tue, 22 Oct 2024 19:44:56 -0400 Subject: [PATCH] add logging per page in safe delete --- utils/migutils/migutils.go | 6 +++++- utils/migutils/migutils_test.go | 2 +- x/emissions/migrations/v3/migrate.go | 6 +++--- x/emissions/migrations/v4/migrate.go | 2 +- x/emissions/migrations/v5/migrate.go | 2 +- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/utils/migutils/migutils.go b/utils/migutils/migutils.go index a3aac7373..f106ab82f 100644 --- a/utils/migutils/migutils.go +++ b/utils/migutils/migutils.go @@ -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 @@ -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 } diff --git a/utils/migutils/migutils_test.go b/utils/migutils/migutils_test.go index 5dcad5d12..f0bcef5ee 100644 --- a/utils/migutils/migutils_test.go +++ b/utils/migutils/migutils_test.go @@ -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 { diff --git a/x/emissions/migrations/v3/migrate.go b/x/emissions/migrations/v3/migrate.go index 6ec39726d..9af12124b 100644 --- a/x/emissions/migrations/v3/migrate.go +++ b/x/emissions/migrations/v3/migrate.go @@ -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 @@ -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, @@ -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 } diff --git a/x/emissions/migrations/v4/migrate.go b/x/emissions/migrations/v4/migrate.go index 0a51b3fdc..1064918b8 100644 --- a/x/emissions/migrations/v4/migrate.go +++ b/x/emissions/migrations/v4/migrate.go @@ -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 } diff --git a/x/emissions/migrations/v5/migrate.go b/x/emissions/migrations/v5/migrate.go index c7c2e8ade..c99dea6c0 100644 --- a/x/emissions/migrations/v5/migrate.go +++ b/x/emissions/migrations/v5/migrate.go @@ -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 }