Skip to content

Commit

Permalink
migration: re-define the migration registration process
Browse files Browse the repository at this point in the history
We make it a bit more ergonomic (this revision was originally
prototyped in cockroachdb#57445).

Release note: None
  • Loading branch information
irfansharif committed Dec 11, 2020
1 parent c725f7b commit 932a815
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
13 changes: 10 additions & 3 deletions pkg/migration/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,19 @@ func (m *Manager) Migrate(ctx context.Context, from, to clusterversion.ClusterVe
}
}

// TODO(irfansharif): We'll want to retrieve the right migration off of
// our registry of migrations, if any, and execute it.
// TODO(irfansharif): We'll want to be able to override which migration
// is retrieved here within tests. We could make the registry be a part
// of the manager, and all tests to provide their own.
_ = Registry[clusterVersion]

// Finally, run the actual migration.
migration, ok := registry[clusterVersion]
if !ok {
log.Infof(ctx, "no migration registered for %s, skipping", clusterVersion)
continue
}
if err := migration.Run(ctx, h); err != nil {
return err
}
}

return nil
Expand Down
9 changes: 4 additions & 5 deletions pkg/migration/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ import (
"github.com/cockroachdb/cockroach/pkg/clusterversion"
)

// Registry defines the global mapping between a cluster version, and the
// registry defines the global mapping between a cluster version and the
// associated migration. The migration is only executed after a cluster-wide
// bump of the version gate.
var Registry = make(map[clusterversion.ClusterVersion]Migration)
// bump of the corresponding version gate.
var registry = make(map[clusterversion.ClusterVersion]Migration)

func init() {
// TODO(irfansharif): We'll want to register individual migrations with
// specific internal cluster versions here.
//
// Registry[clusterversion.ByKey(clusterversion.VersionWhatever)] = WhateverMigration
_ = register // register(clusterversion.WhateverMigration, WhateverMigration, "whatever migration")
}

// Migration defines a program to be executed once every node in the cluster is
Expand Down
10 changes: 10 additions & 0 deletions pkg/migration/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,13 @@ func fenceVersionFor(
fenceCV.Internal--
return fenceCV
}

// register is a short hand to register a given migration within the global
// registry.
func register(key clusterversion.Key, fn migrationFn, desc string) {
cv := clusterversion.ClusterVersion{Version: clusterversion.ByKey(key)}
if _, ok := registry[cv]; ok {
log.Fatalf(context.Background(), "doubly registering migration for %s", cv)
}
registry[cv] = Migration{cv: cv, fn: fn, desc: desc}
}

0 comments on commit 932a815

Please sign in to comment.