Skip to content

Commit

Permalink
add ut for query node mode changer
Browse files Browse the repository at this point in the history
Signed-off-by: shaoyue.chen <[email protected]>
  • Loading branch information
haorenfsa committed Feb 5, 2024
1 parent e422b58 commit 4cb6221
Show file tree
Hide file tree
Showing 3 changed files with 404 additions and 34 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ test: manifests generate fmt vet test-only ## Run tests.

code-check: go-generate fmt vet

test-only:
CGO_ENABLED=0 go test -race ./... -coverprofile tmp.out; cat tmp.out | sed '/zz_generated.deepcopy.go/d' | sed '/_mock.go/d' > cover.out
test-only: ## -race requires cgo
CGO_ENABLED=1 go test -race ./... -coverprofile tmp.out; cat tmp.out | sed '/zz_generated.deepcopy.go/d' | sed '/_mock.go/d' > cover.out

##@ Build
VERSION_PATH=github.com/milvus-io/milvus-operator/apis/milvus.io/v1beta1
Expand Down
68 changes: 36 additions & 32 deletions pkg/controllers/query_node_mode_changer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,29 @@ type DeployModeChanger interface {
var _ DeployModeChanger = &DeployModeChangerImpl{}

type DeployModeChangerImpl struct {
cli client.Client
util QueryNodeControllerBizUtil
cli client.Client
util QueryNodeControllerBizUtil
changeModeToV2Steps []step
}

func NewDeployModeChanger(cli client.Client, util QueryNodeControllerBizUtil) *DeployModeChangerImpl {
return &DeployModeChangerImpl{
c := &DeployModeChangerImpl{
cli: cli,
util: util,
}
c.changeModeToV2Steps = []step{
newStep("save and delete old deploy", c.SaveDeleteOldDeploy),
newStep("save and delete old replica set", c.SaveDeleteOldReplicaSet),
newStep("update old pod labels", c.UpdateOldPodLabels),
newStep("recover replica sets", c.RecoverReplicaSets),
newStep("recover deploy", c.RecoverDeploy),
newStep("update status mode to v2", c.UpdateStatusToV2),
}
return c
}

func (c *DeployModeChangerImpl) MarkDeployModeChanging(ctx context.Context, mc v1beta1.Milvus, changing bool) error {
return c.markChangingDeployMode(ctx, mc, changing)
}

type step struct {
Expand All @@ -48,15 +62,7 @@ func newStep(name string, f func(context.Context, v1beta1.Milvus) error) step {
}

func (c *DeployModeChangerImpl) ChangeRollingModeToV2(ctx context.Context, mc v1beta1.Milvus) error {
steps := []step{
newStep("save and delete old deploy", c.SaveDeleteOldDeploy),
newStep("save and delete old replica set", c.SaveDeleteOldReplicaSet),
newStep("update old pod labels", c.UpdateOldPodLabels),
newStep("recover replica sets", c.RecoverReplicaSets),
newStep("recover deploy", c.RecoverDeploy),
newStep("update status mode to v2", c.UpdateStatusToV2),
}
for i, step := range steps {
for i, step := range c.changeModeToV2Steps {
err := step.Func(ctx, mc)
if err != nil {
return errors.Wrapf(err, "step[no.%d][%s]", i, step.Name)
Expand All @@ -65,32 +71,15 @@ func (c *DeployModeChangerImpl) ChangeRollingModeToV2(ctx context.Context, mc v1
return nil
}

func (c *DeployModeChangerImpl) MarkDeployModeChanging(ctx context.Context, mc v1beta1.Milvus, changing bool) error {
return c.markChangingDeployMode(ctx, mc, changing)
}

func (c *DeployModeChangerImpl) UpdateStatusToV2(ctx context.Context, mc v1beta1.Milvus) error {
mc.Status.RollingMode = v1beta1.RollingModeV2
err := c.cli.Status().Update(ctx, &mc)
if err != nil {
return errors.Wrap(err, "update status rolling mode")
}
return errors.Wrap(ErrRequeue, "update status rolling mode")
}

func (c *DeployModeChangerImpl) markChangingDeployMode(ctx context.Context, mc v1beta1.Milvus, changing bool) error {
if v1beta1.Labels().IsChangeQueryNodeMode(mc) == changing {
return nil
}
logger := ctrl.LoggerFrom(ctx)
logger.Info("marking changing deploy mode", "changing", changing)
v1beta1.Labels().SetChangingQueryNodeMode(&mc, changing)
err := c.cli.Update(ctx, &mc)
if err != nil {
return errors.Wrap(err, "update milvus spec")
}
// return error to abort this reconcile, and trigger next
return errors.Wrap(ErrRequeue, "update deploy mode changing anotation")
err := c.util.UpdateAndRequeue(ctx, &mc)
return errors.Wrap(err, "marking changing deploy mode")
}

func (c *DeployModeChangerImpl) SaveDeleteOldDeploy(ctx context.Context, mc v1beta1.Milvus) error {
Expand All @@ -116,7 +105,10 @@ func (c *DeployModeChangerImpl) SaveDeleteOldReplicaSet(ctx context.Context, mc
if err != nil {
return errors.Wrap(err, "list old replica sets")
}
c.util.SaveObject(ctx, mc, formatSaveOldReplicaSetListName(mc), &replicasetList)
err = c.util.SaveObject(ctx, mc, formatSaveOldReplicaSetListName(mc), &replicasetList)
if err != nil {
return errors.Wrap(err, "save old replicaset list")
}
for _, rs := range replicasetList.Items {
err = c.util.OrphanDelete(ctx, &rs)
if err != nil {
Expand Down Expand Up @@ -159,6 +151,9 @@ func (c *DeployModeChangerImpl) RecoverReplicaSets(ctx context.Context, mc v1bet
rs.UID = ""
rs.ResourceVersion = ""
splitedName := strings.Split(rs.Name, "-")
if len(splitedName) < 2 {
return errors.Errorf("invalid old replica set name: %s", rs.Name)
}
rsHash := splitedName[len(splitedName)-1]
rs.Name = fmt.Sprintf("%s-%s", formatQnDeployName(mc, 0), rsHash)
err = c.util.CreateObject(ctx, &rs)
Expand Down Expand Up @@ -193,6 +188,15 @@ func (c *DeployModeChangerImpl) RecoverDeploy(ctx context.Context, mc v1beta1.Mi
return nil
}

func (c *DeployModeChangerImpl) UpdateStatusToV2(ctx context.Context, mc v1beta1.Milvus) error {
mc.Status.RollingMode = v1beta1.RollingModeV2
err := c.cli.Status().Update(ctx, &mc)
if err != nil {
return errors.Wrap(err, "update status rolling mode")
}
return errors.Wrap(ErrRequeue, "update status rolling mode")
}

func formatSaveOldDeployName(mc v1beta1.Milvus) string {
return fmt.Sprintf("%s-old-deploy", mc.Name)
}
Expand Down
Loading

0 comments on commit 4cb6221

Please sign in to comment.