Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Check the workflow is not being deleted for Synchronization workflow #4935

Merged
merged 5 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion workflow/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ const (
workflowTemplateResyncPeriod = 20 * time.Minute
podResyncPeriod = 30 * time.Minute
clusterWorkflowTemplateResyncPeriod = 20 * time.Minute
workflowExistenceCheckPeriod = 1 * time.Minute
)

// NewWorkflowController instantiates a new WorkflowController
Expand Down Expand Up @@ -243,6 +244,8 @@ func (wfc *WorkflowController) Run(ctx context.Context, wfWorkers, workflowTTLWo
go wait.Until(wfc.syncWorkflowPhaseMetrics, 15*time.Second, ctx.Done())
go wait.Until(wfc.syncPodPhaseMetrics, 15*time.Second, ctx.Done())

go wait.Until(wfc.syncManager.CheckWorkflowExistence, workflowExistenceCheckPeriod, ctx.Done())

for i := 0; i < wfWorkers; i++ {
go wait.Until(wfc.runWorker, time.Second, ctx.Done())
}
Expand Down Expand Up @@ -297,7 +300,16 @@ func (wfc *WorkflowController) createSynchronizationManager(ctx context.Context)
wfc.wfQueue.AddRateLimited(key)
}

wfc.syncManager = sync.NewLockManager(getSyncLimit, nextWorkflow)
isWFDeleted := func(key string) bool {
_, exists, err := wfc.wfInformer.GetIndexer().GetByKey(key)
if err != nil {
log.WithFields(log.Fields{"key": key, "error": err}).Error("Failed to get workflow from informer")
return false
}
return exists
}

wfc.syncManager = sync.NewLockManager(getSyncLimit, nextWorkflow, isWFDeleted)

labelSelector := v1Label.NewSelector()
req, _ := v1Label.NewRequirement(common.LabelKeyPhase, selection.Equals, []string{string(wfv1.NodeRunning)})
Expand Down
16 changes: 10 additions & 6 deletions workflow/controller/operator_concurrency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ spec:
name: workflow-controller-configmap1
`

var workflowExistenceFunc = func(key string) bool {
return true
}

func GetSyncLimitFunc(ctx context.Context, kube kubernetes.Interface) func(string) (int, error) {
syncLimitConfig := func(lockName string) (int, error) {
items := strings.Split(lockName, "/")
Expand Down Expand Up @@ -128,7 +132,7 @@ func TestSemaphoreTmplLevel(t *testing.T) {
defer cancel()
ctx := context.Background()
controller.syncManager = sync.NewLockManager(GetSyncLimitFunc(ctx, controller.kubeclientset), func(key string) {
})
}, workflowExistenceFunc)
var cm v1.ConfigMap
err := yaml.Unmarshal([]byte(configMap), &cm)
assert.NoError(t, err)
Expand Down Expand Up @@ -191,7 +195,7 @@ func TestSemaphoreScriptTmplLevel(t *testing.T) {
defer cancel()
ctx := context.Background()
controller.syncManager = sync.NewLockManager(GetSyncLimitFunc(ctx, controller.kubeclientset), func(key string) {
})
}, workflowExistenceFunc)
var cm v1.ConfigMap
err := yaml.Unmarshal([]byte(configMap), &cm)
assert.NoError(t, err)
Expand Down Expand Up @@ -253,7 +257,7 @@ func TestSemaphoreResourceTmplLevel(t *testing.T) {
defer cancel()
ctx := context.Background()
controller.syncManager = sync.NewLockManager(GetSyncLimitFunc(ctx, controller.kubeclientset), func(key string) {
})
}, workflowExistenceFunc)
var cm v1.ConfigMap
err := yaml.Unmarshal([]byte(configMap), &cm)
assert.NoError(t, err)
Expand Down Expand Up @@ -316,7 +320,7 @@ func TestSemaphoreWithOutConfigMap(t *testing.T) {

ctx := context.Background()
controller.syncManager = sync.NewLockManager(GetSyncLimitFunc(ctx, controller.kubeclientset), func(key string) {
})
}, workflowExistenceFunc)

t.Run("SemaphoreRefWithOutConfigMap", func(t *testing.T) {
wf := unmarshalWF(wfWithSemaphore)
Expand Down Expand Up @@ -373,7 +377,7 @@ func TestMutexInDAG(t *testing.T) {
defer cancel()
ctx := context.Background()
controller.syncManager = sync.NewLockManager(GetSyncLimitFunc(ctx, controller.kubeclientset), func(key string) {
})
}, workflowExistenceFunc)
t.Run("MutexWithDAG", func(t *testing.T) {
wf := unmarshalWF(DAGWithMutex)
wf, err := controller.wfclientset.ArgoprojV1alpha1().Workflows(wf.Namespace).Create(ctx, wf, metav1.CreateOptions{})
Expand Down Expand Up @@ -437,7 +441,7 @@ func TestSynchronizationWithRetry(t *testing.T) {
defer cancel()
ctx := context.Background()
controller.syncManager = sync.NewLockManager(GetSyncLimitFunc(ctx, controller.kubeclientset), func(key string) {
})
}, workflowExistenceFunc)
var cm v1.ConfigMap
err := yaml.Unmarshal([]byte(configMap), &cm)
assert.NoError(err)
Expand Down
1 change: 1 addition & 0 deletions workflow/sync/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Semaphore interface {
addToQueue(holderKey string, priority int32, creationTime time.Time)
removeFromQueue(holderKey string)
getCurrentHolders() []string
getCurrentPending() []string
getName() string
getLimit() int
resize(n int) bool
Expand Down
4 changes: 4 additions & 0 deletions workflow/sync/mutex.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ type PriorityMutex struct {
lock *sync.Mutex
}

func (m *PriorityMutex) getCurrentPending() []string {
return m.mutex.getCurrentPending()
}

var _ Semaphore = &PriorityMutex{}

// NewMutex creates new mutex lock object
Expand Down
6 changes: 3 additions & 3 deletions workflow/sync/mutex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestMutexLock(t *testing.T) {
syncLimitFunc := GetSyncLimitFunc(kube)
t.Run("InitializeSynchronization", func(t *testing.T) {
concurrenyMgr := NewLockManager(syncLimitFunc, func(key string) {
})
}, WorkflowExistenceFunc)
wf := unmarshalWF(mutexwfstatus)
wfclientset := fakewfclientset.NewSimpleClientset(wf)

Expand All @@ -108,7 +108,7 @@ func TestMutexLock(t *testing.T) {
var nextWorkflow string
concurrenyMgr := NewLockManager(syncLimitFunc, func(key string) {
nextWorkflow = key
})
}, WorkflowExistenceFunc)
wf := unmarshalWF(mutexWf)
wf1 := wf.DeepCopy()
wf2 := wf.DeepCopy()
Expand Down Expand Up @@ -287,7 +287,7 @@ func TestMutexTmplLevel(t *testing.T) {
//var nextKey string
concurrenyMgr := NewLockManager(syncLimitFunc, func(key string) {
//nextKey = key
})
}, WorkflowExistenceFunc)
wf := unmarshalWF(mutexWfWithTmplLevel)
tmpl := wf.Spec.Templates[1]

Expand Down
8 changes: 8 additions & 0 deletions workflow/sync/semaphore.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ func (s *PrioritySemaphore) getLimit() int {
return s.limit
}

func (s *PrioritySemaphore) getCurrentPending() []string {
var keys []string
for _, item := range s.pending.items {
keys = append(keys, item.key)
}
return keys
}

func (s *PrioritySemaphore) getCurrentHolders() []string {
var keys []string
for k := range s.lockHolder {
Expand Down
35 changes: 34 additions & 1 deletion workflow/sync/sync_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sync

import (
"fmt"
"strings"
"sync"

log "github.com/sirupsen/logrus"
Expand All @@ -11,20 +12,52 @@ import (

type NextWorkflow func(string)
type GetSyncLimit func(string) (int, error)
type IsWorkflowDeleted func(string) bool

type Manager struct {
syncLockMap map[string]Semaphore
lock *sync.Mutex
nextWorkflow NextWorkflow
getSyncLimit GetSyncLimit
isWFDeleted IsWorkflowDeleted
}

func NewLockManager(getSyncLimit GetSyncLimit, nextWorkflow NextWorkflow) *Manager {
func NewLockManager(getSyncLimit GetSyncLimit, nextWorkflow NextWorkflow, isWFDeleted IsWorkflowDeleted) *Manager {
return &Manager{
syncLockMap: make(map[string]Semaphore),
lock: &sync.Mutex{},
nextWorkflow: nextWorkflow,
getSyncLimit: getSyncLimit,
isWFDeleted: isWFDeleted,
}
}

func (cm *Manager) getWorkflowKey(key string) (string, error) {
if key == "" {
return "", fmt.Errorf("holderkey is empty")
}
items := strings.Split(key, "/")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems redundant - you just split and then re-join - I think you can always assume namespace/name

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In template level synchronization scenario, Key will have 3 items (including node name)

if len(items) < 2 {
return "", fmt.Errorf("invalid holderkey format")
}
return fmt.Sprintf("%s/%s", items[0], items[1]), nil
}

func (cm *Manager) CheckWorkflowExistence() {
log.Infof("Check the workflow existence")
for _, lock := range cm.syncLockMap {
keys := lock.getCurrentHolders()
keys = append(keys, lock.getCurrentPending()...)
for _, holderKeys := range keys {
wfKey, err := cm.getWorkflowKey(holderKeys)
if err != nil {
continue
}
if !cm.isWFDeleted(wfKey) {
lock.release(holderKeys)
lock.removeFromQueue(holderKeys)
}
}
}
}

Expand Down
63 changes: 56 additions & 7 deletions workflow/sync/sync_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ spec:
args: ["hello world"]
`

var WorkflowExistenceFunc = func(s string) bool {
return false
}

func unmarshalWF(yamlStr string) *wfv1.Workflow {
var wf wfv1.Workflow
err := yaml.Unmarshal([]byte(yamlStr), &wf)
Expand Down Expand Up @@ -350,7 +354,7 @@ func TestSemaphoreWfLevel(t *testing.T) {
syncLimitFunc := GetSyncLimitFunc(kube)
t.Run("InitializeSynchronization", func(t *testing.T) {
concurrenyMgr := NewLockManager(syncLimitFunc, func(key string) {
})
}, WorkflowExistenceFunc)
wf := unmarshalWF(wfWithStatus)
wfclientset := fakewfclientset.NewSimpleClientset(wf)

Expand All @@ -362,7 +366,7 @@ func TestSemaphoreWfLevel(t *testing.T) {
t.Run("InitializeSynchronizationWithInvalid", func(t *testing.T) {
concurrenyMgr := NewLockManager(syncLimitFunc, func(key string) {

})
}, WorkflowExistenceFunc)
wf := unmarshalWF(wfWithStatus)
invalidSync := []wfv1.SemaphoreHolding{{Semaphore: "default/configmap/my-config1/workflow", Holders: []string{"hello-world-vcrg5"}}}
wf.Status.Synchronization.Semaphore.Holding = invalidSync
Expand All @@ -377,7 +381,7 @@ func TestSemaphoreWfLevel(t *testing.T) {
var nextKey string
concurrenyMgr := NewLockManager(syncLimitFunc, func(key string) {
nextKey = key
})
}, WorkflowExistenceFunc)
wf := unmarshalWF(wfWithSemaphore)
wf1 := wf.DeepCopy()
wf2 := wf.DeepCopy()
Expand Down Expand Up @@ -470,7 +474,7 @@ func TestResizeSemaphoreSize(t *testing.T) {
syncLimitFunc := GetSyncLimitFunc(kube)
t.Run("WfLevelAcquireAndRelease", func(t *testing.T) {
concurrenyMgr := NewLockManager(syncLimitFunc, func(key string) {
})
}, WorkflowExistenceFunc)
wf := unmarshalWF(wfWithSemaphore)
wf.CreationTimestamp = metav1.Time{Time: time.Now()}
wf1 := wf.DeepCopy()
Expand Down Expand Up @@ -541,7 +545,7 @@ func TestSemaphoreTmplLevel(t *testing.T) {
//var nextKey string
concurrenyMgr := NewLockManager(syncLimitFunc, func(key string) {
//nextKey = key
})
}, WorkflowExistenceFunc)
wf := unmarshalWF(wfWithTmplSemaphore)
tmpl := wf.Spec.Templates[2]

Expand Down Expand Up @@ -601,7 +605,7 @@ func TestTriggerWFWithAvailableLock(t *testing.T) {
triggerCount := 0
concurrenyMgr := NewLockManager(syncLimitFunc, func(key string) {
triggerCount++
})
}, WorkflowExistenceFunc)
var wfs []wfv1.Workflow
for i := 0; i < 3; i++ {
wf := unmarshalWF(wfWithSemaphore)
Expand Down Expand Up @@ -637,7 +641,7 @@ func TestMutexWfLevel(t *testing.T) {
//var nextKey string
concurrenyMgr := NewLockManager(syncLimitFunc, func(key string) {
//nextKey = key
})
}, WorkflowExistenceFunc)
wf := unmarshalWF(wfWithMutex)
wf1 := wf.DeepCopy()
wf2 := wf.DeepCopy()
Expand Down Expand Up @@ -674,3 +678,48 @@ func TestMutexWfLevel(t *testing.T) {
assert.Len(t, mutex.mutex.pending.items, 0)
})
}

func TestCheckWorkflowExistence(t *testing.T) {
assert := assert.New(t)
kube := fake.NewSimpleClientset()
var cm v1.ConfigMap
err := yaml.Unmarshal([]byte(configMap), &cm)
cm.Data["workflow"] = "1"
assert.NoError(err)

ctx := context.Background()
_, err = kube.CoreV1().ConfigMaps("default").Create(ctx, &cm, metav1.CreateOptions{})
assert.NoError(err)

syncLimitFunc := GetSyncLimitFunc(kube)
t.Run("WorkflowDeleted", func(t *testing.T) {
concurrenyMgr := NewLockManager(syncLimitFunc, func(key string) {
//nextKey = key
}, func(s string) bool {
return strings.Contains(s, "test1")
})
wfMutex := unmarshalWF(wfWithMutex)
wfMutex1 := wfMutex.DeepCopy()
wfMutex1.Name = "test1"
wfSema := unmarshalWF(wfWithSemaphore)
wfSema1 := wfSema.DeepCopy()
wfSema1.Name = "test2"
_, _, _, _ = concurrenyMgr.TryAcquire(wfMutex, "", wfMutex.Spec.Synchronization)
_, _, _, _ = concurrenyMgr.TryAcquire(wfMutex1, "", wfMutex.Spec.Synchronization)
_, _, _, _ = concurrenyMgr.TryAcquire(wfSema, "", wfSema.Spec.Synchronization)
_, _, _, _ = concurrenyMgr.TryAcquire(wfSema1, "", wfSema.Spec.Synchronization)
mutex := concurrenyMgr.syncLockMap["default/Mutex/my-mutex"].(*PriorityMutex)
semaphore := concurrenyMgr.syncLockMap["default/ConfigMap/my-config/workflow"]

assert.Len(mutex.getCurrentHolders(), 1)
assert.Len(mutex.getCurrentPending(), 1)
assert.Len(semaphore.getCurrentHolders(), 1)
assert.Len(semaphore.getCurrentPending(), 1)
concurrenyMgr.CheckWorkflowExistence()
assert.Len(mutex.getCurrentHolders(), 0)
assert.Len(mutex.getCurrentPending(), 1)
assert.Len(semaphore.getCurrentHolders(), 0)
assert.Len(semaphore.getCurrentPending(), 0)
})

}