Skip to content

Commit

Permalink
Refactor full update
Browse files Browse the repository at this point in the history
  • Loading branch information
l0kix2 committed Apr 16, 2024
1 parent 2f777e3 commit b3f937f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 32 deletions.
23 changes: 5 additions & 18 deletions controllers/component_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ type ComponentManager struct {
type ComponentManagerStatus struct {
needSync bool
needInit bool
needFullUpdate bool
needLocalUpdate []components.Component
needUpdate []components.Component
allReadyOrUpdating bool
}

Expand Down Expand Up @@ -143,8 +142,7 @@ func NewComponentManager(
status := ComponentManagerStatus{
needInit: false,
needSync: false,
needFullUpdate: false,
needLocalUpdate: nil,
needUpdate: nil,
allReadyOrUpdating: true,
}
for _, c := range allComponents {
Expand All @@ -162,15 +160,8 @@ func NewComponentManager(
c.SetReadyCondition(componentStatus)
syncStatus := componentStatus.SyncStatus

if syncStatus == components.SyncStatusNeedFullUpdate {
status.needFullUpdate = true
}

if syncStatus == components.SyncStatusNeedLocalUpdate {
if status.needLocalUpdate == nil {
status.needLocalUpdate = make([]components.Component, 0)
}
status.needLocalUpdate = append(status.needLocalUpdate, c)
status.needUpdate = append(status.needUpdate, c)
}

if !components.IsRunningStatus(syncStatus) {
Expand Down Expand Up @@ -247,12 +238,8 @@ func (cm *ComponentManager) needInit() bool {
return cm.status.needInit
}

func (cm *ComponentManager) needFullUpdate() bool {
return cm.status.needFullUpdate
}

func (cm *ComponentManager) needLocalUpdate() []components.Component {
return cm.status.needLocalUpdate
func (cm *ComponentManager) needUpdate() []components.Component {
return cm.status.needUpdate
}

func (cm *ComponentManager) allReadyOrUpdating() bool {
Expand Down
46 changes: 36 additions & 10 deletions controllers/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,37 @@ func getComponentNames(components []components.Component) []string {
return names
}

// chooseUpdateStrategy considers spec decides if operator should proceed with update or block.
// Block is indicated with non-empty blockMsg.
// Component names which are chosen for update are return in names slice.
// Nil names splice means full update.
func chooseUpdateStrategy(spec ytv1.YtsaurusSpec, needUpdate []components.Component) (names []string, blockMsg string) {
isFullUpdateEnabled := spec.EnableFullUpdate

masterNeedsUpdate := false
tabletNodesNeedUpdate := false
for _, comp := range needUpdate {
if comp.GetType() == consts.MasterType {
masterNeedsUpdate = true
continue
}
if comp.GetType() == consts.TabletNodeType {
tabletNodesNeedUpdate = true
continue
}
}
statefulNeedUpdate := masterNeedsUpdate || tabletNodesNeedUpdate

if statefulNeedUpdate {
if isFullUpdateEnabled {
return nil, ""
} else {
return nil, "Full update is not allowed with enableFullUpdate field, ignoring it"
}
}
return getComponentNames(needUpdate), ""
}

func (r *YtsaurusReconciler) Sync(ctx context.Context, resource *ytv1.Ytsaurus) (ctrl.Result, error) {
logger := log.FromContext(ctx)

Expand Down Expand Up @@ -282,18 +313,13 @@ func (r *YtsaurusReconciler) Sync(ctx context.Context, resource *ytv1.Ytsaurus)
err := ytsaurus.SaveClusterState(ctx, ytv1.ClusterStateReconfiguration)
return ctrl.Result{Requeue: true}, err

case componentManager.needFullUpdate():
logger.Info("Ytsaurus needs full update")
if !ytsaurus.GetResource().Spec.EnableFullUpdate {
logger.Info("Full update isn't allowed, ignore it")
case componentManager.needUpdate() != nil:
componentNames, blockMsg := chooseUpdateStrategy(ytsaurus.GetResource().Spec, componentManager.needUpdate())
if blockMsg != "" {
logger.Info(blockMsg)
return ctrl.Result{}, nil
}
err := ytsaurus.SaveUpdatingClusterState(ctx, nil)
return ctrl.Result{Requeue: true}, err

case componentManager.needLocalUpdate() != nil:
componentNames := getComponentNames(componentManager.needLocalUpdate())
logger.Info("Ytsaurus needs local components update", "components", componentNames)
logger.Info("Ytsaurus needs components update", "components", componentNames)
err := ytsaurus.SaveUpdatingClusterState(ctx, componentNames)
return ctrl.Result{Requeue: true}, err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/components/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ type SyncStatus string

const (
SyncStatusBlocked SyncStatus = "Blocked"
SyncStatusNeedFullUpdate SyncStatus = "NeedFullUpdate"
SyncStatusNeedLocalUpdate SyncStatus = "NeedLocalUpdate"
SyncStatusPending SyncStatus = "Pending"
SyncStatusReady SyncStatus = "Ready"
SyncStatusUpdating SyncStatus = "Updating"
)

func IsRunningStatus(status SyncStatus) bool {
return status == SyncStatusReady || status == SyncStatusNeedLocalUpdate || status == SyncStatusNeedFullUpdate
return status == SyncStatusReady || status == SyncStatusNeedLocalUpdate
}

type ComponentStatus struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/components/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func (m *Master) doSync(ctx context.Context, dry bool) (ComponentStatus, error)
var err error

if ytv1.IsReadyToUpdateClusterState(m.ytsaurus.GetClusterState()) && m.server.needUpdate() {
return SimpleStatus(SyncStatusNeedFullUpdate), err
return SimpleStatus(SyncStatusNeedLocalUpdate), err
}

if m.ytsaurus.GetClusterState() == ytv1.ClusterStateUpdating {
Expand Down
2 changes: 1 addition & 1 deletion pkg/components/tablet_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (tn *TabletNode) doSync(ctx context.Context, dry bool) (ComponentStatus, er
var err error

if ytv1.IsReadyToUpdateClusterState(tn.ytsaurus.GetClusterState()) && tn.server.needUpdate() {
return SimpleStatus(SyncStatusNeedFullUpdate), err
return SimpleStatus(SyncStatusNeedLocalUpdate), err
}

if tn.ytsaurus.GetClusterState() == ytv1.ClusterStateUpdating {
Expand Down

0 comments on commit b3f937f

Please sign in to comment.