Skip to content

Commit

Permalink
feat: add scheduler host gc (#989)
Browse files Browse the repository at this point in the history
Signed-off-by: Gaius <[email protected]>
  • Loading branch information
gaius-qi committed Jun 28, 2023
1 parent 3b6966c commit 02913a5
Show file tree
Hide file tree
Showing 10 changed files with 273 additions and 18 deletions.
12 changes: 8 additions & 4 deletions docs/en/deployment/configuration/scheduler.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@ scheduler:
retryInterval: 1s
# gc metadata configuration
gc:
# peerGCInterval peer's gc interval
# peerGCInterval is peer's gc interval
peerGCInterval: 10m
# peerTTL peer's TTL duration
# peerTTL is peer's TTL duration
peerTTL: 24h
# taskGCInterval task's gc interval
# taskGCInterval is task's gc interval
taskGCInterval: 10m
# taskTTL task's TTL duration
# taskTTL is task's TTL duration
taskTTL: 24h
# hostGCInterval is host's gc interval
hostGCInterval: 30m
# hostTTL is host's TTL duration
hostTTL: 48h

# dynamic data configuration
dynConfig:
Expand Down
4 changes: 4 additions & 0 deletions docs/zh-CN/deployment/configuration/scheduler.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ scheduler:
taskGCInterval: 10m
# 不活跃的 task 的存活时间
taskTTL: 24h
# host 的回收间隔
hostGCInterval: 30m
# 不活跃的 host 的存活时间
hostTTL: 48h

# 动态数据配置
dynConfig:
Expand Down
8 changes: 8 additions & 0 deletions scheduler/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ func New() *Config {
PeerTTL: 24 * time.Hour,
TaskGCInterval: 10 * time.Minute,
TaskTTL: 24 * time.Hour,
HostGCInterval: 30 * time.Minute,
HostTTL: 48 * time.Hour,
},
},
DynConfig: &DynConfig{
Expand Down Expand Up @@ -254,6 +256,12 @@ type GCConfig struct {

// Task time to live
TaskTTL time.Duration `yaml:"taskTTL" mapstructure:"taskTTL"`

// Host gc interval
HostGCInterval time.Duration `yaml:"hostGCInterval" mapstructure:"hostGCInterval"`

// Host time to live
HostTTL time.Duration `yaml:"hostTTL" mapstructure:"hostTTL"`
}

type DynConfig struct {
Expand Down
2 changes: 2 additions & 0 deletions scheduler/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ func TestConfig_Load(t *testing.T) {
PeerTTL: 5 * time.Minute,
TaskGCInterval: 1 * time.Minute,
TaskTTL: 10 * time.Minute,
HostGCInterval: 1 * time.Minute,
HostTTL: 10 * time.Minute,
},
},
DynConfig: &DynConfig{
Expand Down
2 changes: 2 additions & 0 deletions scheduler/config/testdata/scheduler.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ scheduler:
peerTTL: 300000000000
taskGCInterval: 60000000000
taskTTL: 600000000000
hostGCInterval: 60000000000
hostTTL: 600000000000

dynconfig:
refreshInterval: 300000000000
Expand Down
49 changes: 47 additions & 2 deletions scheduler/resource/host_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ package resource

import (
"sync"
"time"

pkggc "d7y.io/dragonfly/v2/pkg/gc"
"d7y.io/dragonfly/v2/scheduler/config"
)

const (
// GC host id
GCHostID = "host"
)

type HostManager interface {
Expand All @@ -34,16 +43,36 @@ type HostManager interface {

// Delete deletes host for a key
Delete(string)

// Try to reclaim host
RunGC() error
}

type hostManager struct {
// Host sync map
*sync.Map

// Host time to live
ttl time.Duration
}

// New host manager interface
func newHostManager() HostManager {
return &hostManager{&sync.Map{}}
func newHostManager(cfg *config.GCConfig, gc pkggc.GC) (HostManager, error) {
h := &hostManager{
Map: &sync.Map{},
ttl: cfg.HostTTL,
}

if err := gc.Add(pkggc.Task{
ID: GCHostID,
Interval: cfg.HostGCInterval,
Timeout: cfg.HostGCInterval,
Runner: h,
}); err != nil {
return nil, err
}

return h, nil
}

func (h *hostManager) Load(key string) (*Host, bool) {
Expand All @@ -67,3 +96,19 @@ func (h *hostManager) LoadOrStore(host *Host) (*Host, bool) {
func (h *hostManager) Delete(key string) {
h.Map.Delete(key)
}

func (h *hostManager) RunGC() error {
h.Map.Range(func(_, value interface{}) bool {
host := value.(*Host)
elapsed := time.Since(host.UpdateAt.Load())

if elapsed > h.ttl && host.LenPeers() == 0 {
host.Log.Info("host has been reclaimed")
h.Delete(host.ID)
}

return true
})

return nil
}
14 changes: 14 additions & 0 deletions scheduler/resource/host_manager_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 02913a5

Please sign in to comment.