Skip to content

Commit

Permalink
Merge pull request #5988 from onflow/supun/migration-stats
Browse files Browse the repository at this point in the history
Report migration metrics
  • Loading branch information
SupunS authored May 29, 2024
2 parents 7d80c9d + 4baceb2 commit 09a9b0e
Show file tree
Hide file tree
Showing 6 changed files with 604 additions and 6 deletions.
5 changes: 5 additions & 0 deletions cmd/util/cmd/execution-state-extract/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ var (
flagFixSlabsWithBrokenReferences bool
flagFilterUnreferencedSlabs bool
flagCPUProfile string
flagReportMetrics bool
)

var Cmd = &cobra.Command{
Expand Down Expand Up @@ -163,6 +164,9 @@ func init() {

Cmd.Flags().StringVar(&flagCPUProfile, "cpu-profile", "",
"enable CPU profiling")

Cmd.Flags().BoolVar(&flagReportMetrics, "report-metrics", false,
"report migration metrics")
}

func run(*cobra.Command, []string) {
Expand Down Expand Up @@ -375,6 +379,7 @@ func run(*cobra.Command, []string) {
VerboseErrorOutput: flagVerboseErrorOutput,
FixSlabsWithBrokenReferences: chainID == flow.Testnet && flagFixSlabsWithBrokenReferences,
FilterUnreferencedSlabs: flagFilterUnreferencedSlabs,
ReportMetrics: flagReportMetrics,
}

if len(flagInputPayloadFileName) > 0 {
Expand Down
18 changes: 18 additions & 0 deletions cmd/util/ledger/migrations/cadence.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,23 @@ func NewCadence1ValueMigrations(
)
}

if opts.ReportMetrics {
migs = append(migs, NamedMigration{
Name: metricsCollectingMigrationName,
Migrate: NewAccountBasedMigration(
log,
opts.NWorker,
[]AccountBasedMigration{
NewMetricsCollectingMigration(
opts.ChainID,
rwf,
programs,
),
},
),
})
}

return
}

Expand Down Expand Up @@ -425,6 +442,7 @@ type Options struct {
MaxAccountSize uint64
FixSlabsWithBrokenReferences bool
FilterUnreferencedSlabs bool
ReportMetrics bool
}

func NewCadence1Migrations(
Expand Down
12 changes: 6 additions & 6 deletions cmd/util/ledger/migrations/cadence_values_migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,12 @@ func checkMigratedState(
{
storageKey: interpreter.StorageKey{Key: "storage", Address: address},
storageMapKey: interpreter.StringStorageMapKey("flowTokenVault"),
value: `11240984669916758018`,
value: `8791026472627208194`,
},
{
storageKey: interpreter.StorageKey{Key: "storage", Address: address},
storageMapKey: interpreter.StringStorageMapKey("flowTokenVault"),
value: `A.0ae53cb6e3f42a79.FlowToken.Vault(balance: 0.00100000, uuid: 11240984669916758018)`,
value: `A.0ae53cb6e3f42a79.FlowToken.Vault(balance: 0.00100000, uuid: 8791026472627208194)`,
},
{
storageKey: interpreter.StorageKey{Key: "storage", Address: address},
Expand Down Expand Up @@ -384,12 +384,12 @@ func checkMigratedState(
{
storageKey: interpreter.StorageKey{Key: "storage", Address: address},
storageMapKey: interpreter.StringStorageMapKey("r"),
value: `360287970189639680`,
value: `11457157452030541824`,
},
{
storageKey: interpreter.StorageKey{Key: "storage", Address: address},
storageMapKey: interpreter.StringStorageMapKey("r"),
value: "A.01cf0e2f2f715450.Test.R(uuid: 360287970189639680)",
value: "A.01cf0e2f2f715450.Test.R(uuid: 11457157452030541824)",
},
{
storageKey: interpreter.StorageKey{Key: "storage", Address: address},
Expand Down Expand Up @@ -573,7 +573,7 @@ func checkMigratedPayloads(
Name: "balance",
},
{
Value: interpreter.NewUnmeteredUInt64Value(11240984669916758018),
Value: interpreter.NewUnmeteredUInt64Value(8791026472627208194),
Name: "uuid",
},
},
Expand Down Expand Up @@ -666,7 +666,7 @@ func checkMigratedPayloads(
common.CompositeKindResource,
[]interpreter.CompositeField{
{
Value: interpreter.NewUnmeteredUInt64Value(360287970189639680),
Value: interpreter.NewUnmeteredUInt64Value(11457157452030541824),
Name: "uuid",
},
},
Expand Down
194 changes: 194 additions & 0 deletions cmd/util/ledger/migrations/migration_matrics_collector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package migrations

import (
"testing"

"github.com/rs/zerolog"

"github.com/stretchr/testify/require"

"github.com/onflow/cadence/runtime/common"

"github.com/onflow/flow-go/cmd/util/ledger/util"
"github.com/onflow/flow-go/cmd/util/ledger/util/registers"
"github.com/onflow/flow-go/model/flow"
)

func TestMigrationMetricsCollection(t *testing.T) {

t.Parallel()

t.Run("contract not staged", func(t *testing.T) {

t.Parallel()

// Get the old payloads
payloads, err := util.PayloadsFromEmulatorSnapshot(snapshotPath)
require.NoError(t, err)

registersByAccount, err := registers.NewByAccountFromPayloads(payloads)
require.NoError(t, err)

rwf := &testReportWriterFactory{}

logWriter := &writer{}
logger := zerolog.New(logWriter).Level(zerolog.InfoLevel)

const nWorker = 2

const chainID = flow.Emulator
const evmContractChange = EVMContractChangeNone
const burnerContractChange = BurnerContractChangeDeploy

migrations := NewCadence1Migrations(
logger,
t.TempDir(),
rwf,
Options{
NWorker: nWorker,
ChainID: chainID,
EVMContractChange: evmContractChange,
BurnerContractChange: burnerContractChange,
VerboseErrorOutput: true,
ReportMetrics: true,

// Important: 'Test' contract is NOT staged intentionally.
// So values belongs to types from 'Test' contract should be
// identified as un-migrated values.
},
)

for _, migration := range migrations {
err = migration.Migrate(registersByAccount)
require.NoError(
t,
err,
"migration `%s` failed, logs: %v",
migration.Name,
logWriter.logs,
)
}

require.NoError(t, err)

reportWriter := rwf.reportWriters["metrics-collecting-migration"]
require.Len(t, reportWriter.entries, 1)

entry := reportWriter.entries[0]
require.IsType(t, Metrics{}, entry)

require.Equal(
t,
Metrics{
TotalValues: 752,
TotalErrors: 6,
ErrorsPerContract: map[string]int{
"A.01cf0e2f2f715450.Test": 6,
},
ValuesPerContract: map[string]int{
"A.01cf0e2f2f715450.Test": 6,
"A.0ae53cb6e3f42a79.FlowToken": 20,
"A.f8d6e0586b0a20c7.FlowClusterQC": 6,
"A.f8d6e0586b0a20c7.FlowDKG": 4,
"A.f8d6e0586b0a20c7.FlowEpoch": 1,
"A.f8d6e0586b0a20c7.FlowIDTableStaking": 5,
"A.f8d6e0586b0a20c7.LockedTokens": 3,
"A.f8d6e0586b0a20c7.NodeVersionBeacon": 1,
},
},
entry,
)
})

t.Run("staged contract with errors", func(t *testing.T) {

t.Parallel()

address, err := common.HexToAddress(testAccountAddress)
require.NoError(t, err)

// Get the old payloads
payloads, err := util.PayloadsFromEmulatorSnapshot(snapshotPath)
require.NoError(t, err)

registersByAccount, err := registers.NewByAccountFromPayloads(payloads)
require.NoError(t, err)

rwf := &testReportWriterFactory{}

logWriter := &writer{}
logger := zerolog.New(logWriter).Level(zerolog.InfoLevel)

const nWorker = 2

const chainID = flow.Emulator
const evmContractChange = EVMContractChangeNone
const burnerContractChange = BurnerContractChangeDeploy

stagedContracts := []StagedContract{
{
Contract: Contract{
Name: "Test",
Code: []byte(`access(all) contract Test {}`),
},
Address: address,
},
}

migrations := NewCadence1Migrations(
logger,
t.TempDir(),
rwf,
Options{
NWorker: nWorker,
ChainID: chainID,
EVMContractChange: evmContractChange,
BurnerContractChange: burnerContractChange,
VerboseErrorOutput: true,
ReportMetrics: true,
StagedContracts: stagedContracts,
},
)

for _, migration := range migrations {
err = migration.Migrate(registersByAccount)
require.NoError(
t,
err,
"migration `%s` failed, logs: %v",
migration.Name,
logWriter.logs,
)
}

require.NoError(t, err)

reportWriter := rwf.reportWriters["metrics-collecting-migration"]
require.Len(t, reportWriter.entries, 1)

entry := reportWriter.entries[0]
require.IsType(t, Metrics{}, entry)

require.Equal(
t,
Metrics{
TotalValues: 752,
TotalErrors: 6,
ErrorsPerContract: map[string]int{
"A.01cf0e2f2f715450.Test": 6,
},
ValuesPerContract: map[string]int{
"A.01cf0e2f2f715450.Test": 6,
"A.0ae53cb6e3f42a79.FlowToken": 20,
"A.f8d6e0586b0a20c7.FlowClusterQC": 6,
"A.f8d6e0586b0a20c7.FlowDKG": 4,
"A.f8d6e0586b0a20c7.FlowEpoch": 1,
"A.f8d6e0586b0a20c7.FlowIDTableStaking": 5,
"A.f8d6e0586b0a20c7.LockedTokens": 3,
"A.f8d6e0586b0a20c7.NodeVersionBeacon": 1,
},
},
entry,
)
})
}
Loading

0 comments on commit 09a9b0e

Please sign in to comment.