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

[YUNIKORN-469]Implement recycling service in PlaceholderManager that cleans up orphan placeholders #208

Merged
merged 4 commits into from
Dec 9, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions pkg/cache/placeholder_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package cache

import (
"sync"
"sync/atomic"
"time"

"go.uber.org/zap"
v1 "k8s.io/api/core/v1"
Expand All @@ -33,6 +35,8 @@ import (
type PlaceholderManager struct {
clients *client.Clients
orphanPod map[string]*v1.Pod
stopChan chan struct{}
running atomic.Value
sync.RWMutex
}

Expand Down Expand Up @@ -97,3 +101,62 @@ func (mgr *PlaceholderManager) setMockedClients(mockedClients *client.Clients) {
defer mgr.Unlock()
mgr.clients = mockedClients
}

func (mgr *PlaceholderManager) cleanOrphanPlaceholders() {
yangwwei marked this conversation as resolved.
Show resolved Hide resolved
mgr.Lock()
defer mgr.Unlock()
for taskID, pod := range mgr.orphanPod {
log.Logger().Debug("start to clean up orphan pod",
zap.String("taskID", taskID),
zap.String("podName", pod.Name))
err := mgr.clients.KubeClient.Delete(pod)
yangwwei marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Logger().Warn("failed to clean up orphan pod", zap.Error(err))
} else {
delete(mgr.orphanPod, taskID)
}
}
}

func (mgr *PlaceholderManager) Start() {
yangwwei marked this conversation as resolved.
Show resolved Hide resolved
log.Logger().Info("starting the Placeholder Manager")
mgr.stopChan = make(chan struct{})
if mgr.isRunning() {
log.Logger().Info("The placeholder manager has been started")
return
}
mgr.setRunning(true)
go func() {
for {
select {
case <-mgr.stopChan:
log.Logger().Info("PlaceholderManager has been stopped")
mgr.setRunning(false)
return
default:
// clean orphan placeholders every 5 seconds
log.Logger().Info("clean up orphan pod")
mgr.cleanOrphanPlaceholders()
time.Sleep(5 * time.Second)
}
}
}()
}

func (mgr *PlaceholderManager) Stop() {
if !mgr.isRunning() {
log.Logger().Info("The placeholder manager has been stopped")
return
}
log.Logger().Info("stopping the Placeholder Manager")
mgr.stopChan <- struct{}{}
time.Sleep(3 * time.Second)
}

func (mgr *PlaceholderManager) isRunning() bool {
return mgr.running.Load().(bool)
}

func (mgr *PlaceholderManager) setRunning(flag bool) {
mgr.running.Store(flag)
}
42 changes: 42 additions & 0 deletions pkg/cache/placeholder_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package cache
import (
"fmt"
"sync"
"sync/atomic"
"testing"

"gotest.tools/assert"
Expand Down Expand Up @@ -173,3 +174,44 @@ func TestCleanUp(t *testing.T) {
assert.Equal(t, exist, false)
assert.Equal(t, len(placeholderMgr.orphanPod), 0)
}

yangwwei marked this conversation as resolved.
Show resolved Hide resolved
func TestCleanOrphanPlaceholders(t *testing.T) {
mockedAPIProvider := client.NewMockedAPIProvider()
placeholderMgr := &PlaceholderManager{
clients: mockedAPIProvider.GetAPIs(),
orphanPod: make(map[string]*v1.Pod),
RWMutex: sync.RWMutex{},
}
pod1 := &v1.Pod{
TypeMeta: apis.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: apis.ObjectMeta{
Name: "pod-01",
UID: "UID-01",
},
}
placeholderMgr.orphanPod["task01"] = pod1
assert.Equal(t, len(placeholderMgr.orphanPod), 1)
placeholderMgr.cleanOrphanPlaceholders()
assert.Equal(t, len(placeholderMgr.orphanPod), 0)
}

func TestPlaceholderManagerStartStop(t *testing.T) {
mockedAPIProvider := client.NewMockedAPIProvider()
placeholderMgr := &PlaceholderManager{
clients: mockedAPIProvider.GetAPIs(),
orphanPod: make(map[string]*v1.Pod),
running: atomic.Value{},
RWMutex: sync.RWMutex{},
}
placeholderMgr.setRunning(false)
// start clean up goroutine
placeholderMgr.Start()
assert.Equal(t, placeholderMgr.isRunning(), true)

placeholderMgr.Stop()
// check orphan pod map is empty
assert.Equal(t, placeholderMgr.isRunning(), false)
}