Skip to content

Commit

Permalink
add background migration
Browse files Browse the repository at this point in the history
  • Loading branch information
BeryJu committed Nov 25, 2024
1 parent bc62216 commit 17f6cdb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
8 changes: 1 addition & 7 deletions pkg/instance/api_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,7 @@ type APIInstanceInfo struct {

func (i *Instance) APIInstanceInfo() usecase.Interactor {
u := usecase.NewInteractor(func(ctx context.Context, input struct{}, output *APIInstanceInfo) error {
ri := i.ForRole("api", ctx)
m := migrate.New(ri)
cv, err := m.GetClusterVersion(ctx)
if err != nil {
return status.Internal
}
output.Version = cv.String()
output.Version = extconfig.Version
output.BuildHash = extconfig.BuildHash
output.Dirs = extconfig.Get().Dirs()
output.CurrentInstanceIP = extconfig.Get().Instance.IP
Expand Down
48 changes: 33 additions & 15 deletions pkg/roles/dhcp/role_migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,38 @@ func (r *Role) migrateMoveInitial(ctx context.Context) {
}
}

// func (r *Role) migrateMoveBackground() {
// watchChan := r.i.KV().Watch(
// r.ctx,
// r.i.KV().Key(types.KeyRole, types.KeyLegacyLeases).Prefix(true).String(),
// clientv3.WithPrefix(),
// )
// for watchResp := range watchChan {
// for _, event := range watchResp.Events {
// switch event.Type {
// case clientv3.EventTypeDelete:
// r.i.KV().Delete(r.ctx)
// }
// }
// }
// }
func (r *Role) migrateMoveBackground(ctx context.Context) {
watchChan := r.i.KV().Watch(
ctx,
r.i.KV().Key(types.KeyRole, types.KeyLegacyLeases).Prefix(true).String(),
clientv3.WithPrefix(),
)
type partialLease struct {
ScopeKey string `json:"scopeKey"`
}
for watchResp := range watchChan {
for _, event := range watchResp.Events {
pl := partialLease{}
err := json.Unmarshal(event.Kv.Value, &pl)
if err != nil {
r.log.Warn("failed to parse partial lease", zap.Error(err))
continue

Check warning on line 59 in pkg/roles/dhcp/role_migrations.go

View check run for this annotation

Codecov / codecov/patch

pkg/roles/dhcp/role_migrations.go#L44-L59

Added lines #L44 - L59 were not covered by tests
}
ident := strings.Split(string(event.Kv.Key), "/")[2]
newKey := r.i.KV().Key(types.KeyRole, types.KeyScopes, pl.ScopeKey, ident).String()
switch event.Type {
case clientv3.EventTypePut:
_, err = r.i.KV().Put(ctx, newKey, string(event.Kv.Value))
case clientv3.EventTypeDelete:
_, err = r.i.KV().Delete(ctx, newKey)

Check warning on line 67 in pkg/roles/dhcp/role_migrations.go

View check run for this annotation

Codecov / codecov/patch

pkg/roles/dhcp/role_migrations.go#L61-L67

Added lines #L61 - L67 were not covered by tests
}
if err != nil {
r.log.Warn("failed to mirror legacy lease operation", zap.Error(err))
continue

Check warning on line 71 in pkg/roles/dhcp/role_migrations.go

View check run for this annotation

Codecov / codecov/patch

pkg/roles/dhcp/role_migrations.go#L69-L71

Added lines #L69 - L71 were not covered by tests
}
}
}
}

func (r *Role) RegisterMigrations() {
r.i.Migrator().AddMigration(&migrate.InlineMigration{
Expand All @@ -76,6 +93,7 @@ func (r *Role) RegisterMigrations() {
leasePrefix := r.i.KV().Key(types.KeyRole, types.KeyScopes).Prefix(true).String()

r.migrateMoveInitial(ctx)
go r.migrateMoveBackground(ctx)

return r.i.KV().WithHooks(storage.StorageHook{
PutPost: func(ctx context.Context, key, val string, res *clientv3.PutResponse, opts ...clientv3.OpOption) (*clientv3.PutResponse, error) {
Expand Down

0 comments on commit 17f6cdb

Please sign in to comment.