1
0
mirror of https://github.com/newnius/YAO-scheduler.git synced 2025-06-07 22:31:55 +00:00
YAO-scheduler/src/resource_pool.go

886 lines
21 KiB
Go
Raw Normal View History

2019-03-04 09:19:55 +00:00
package main
import (
"sync"
2019-04-29 09:05:15 +00:00
"time"
2019-06-05 09:09:22 +00:00
"net/url"
"strings"
2019-07-10 12:40:43 +00:00
log "github.com/sirupsen/logrus"
2019-06-05 09:09:22 +00:00
"math/rand"
"strconv"
2020-05-01 04:48:06 +00:00
"sort"
2020-05-03 15:32:38 +00:00
"hash/fnv"
2019-10-24 12:25:59 +00:00
)
2019-03-04 09:19:55 +00:00
2020-05-24 13:07:02 +00:00
var resourcePoolInstance *ResourcePool
var resourcePoolInstanceLock sync.Mutex
func InstanceOfResourcePool() *ResourcePool {
defer resourcePoolInstanceLock.Unlock()
resourcePoolInstanceLock.Lock()
if resourcePoolInstance == nil {
resourcePoolInstance = &ResourcePool{}
}
return resourcePoolInstance
}
2019-03-04 09:19:55 +00:00
type ResourcePool struct {
2020-04-13 14:35:17 +00:00
poolsCount int
2020-05-03 16:19:50 +00:00
pools []PoolSeg
2020-05-03 15:32:38 +00:00
poolsMu sync.Mutex
2019-04-29 09:05:15 +00:00
2019-04-29 12:57:32 +00:00
history []PoolStatus
2019-06-04 03:08:49 +00:00
2020-05-25 12:50:41 +00:00
heartBeat map[string]time.Time
heartBeatMu sync.Mutex
versions map[string]float64
versionsMu sync.Mutex
counter int
counterTotal int
subscriptions map[string]map[string]int
subscriptionsMu sync.Mutex
2019-06-05 09:09:22 +00:00
networks map[string]bool
networksFree map[string]bool
networkMu sync.Mutex
2020-03-29 13:12:44 +00:00
2020-04-30 10:39:47 +00:00
bindings map[string]map[string]int
2020-04-13 12:29:58 +00:00
bindingsMu sync.Mutex
2020-04-30 15:06:12 +00:00
utils map[string][]UtilGPUTimeSeries
2020-04-30 09:52:52 +00:00
2020-05-03 16:59:20 +00:00
TotalGPU int
TotalGPUMu sync.Mutex
2020-05-24 13:07:02 +00:00
UsingGPU int
UsingGPUMu sync.Mutex
enableShare bool
enableShareRatio float64
enablePreSchedule bool
enablePreScheduleRatio float64
2019-04-29 09:05:15 +00:00
}
2020-05-24 13:07:02 +00:00
func (pool *ResourcePool) init(conf Configuration) {
2020-05-04 05:59:01 +00:00
log.Info("RM started ")
2019-06-05 09:09:22 +00:00
pool.networks = map[string]bool{}
pool.networksFree = map[string]bool{}
2020-04-30 10:39:47 +00:00
pool.bindings = map[string]map[string]int{}
2020-04-30 15:06:12 +00:00
pool.utils = map[string][]UtilGPUTimeSeries{}
2020-04-11 03:38:04 +00:00
2020-04-30 09:52:52 +00:00
pool.TotalGPU = 0
2020-05-24 13:07:02 +00:00
pool.UsingGPU = 0
pool.enableShare = true
pool.enableShareRatio = 0.75
pool.enablePreSchedule = true
pool.enablePreScheduleRatio = 0.95
2020-04-30 09:52:52 +00:00
2020-05-03 15:32:38 +00:00
/* init pools */
pool.poolsCount = 300
2020-04-13 14:35:17 +00:00
for i := 0; i < pool.poolsCount; i++ {
2020-05-25 11:29:35 +00:00
pool.pools = append(pool.pools, PoolSeg{Lock: sync.Mutex{}, ID: i})
2020-05-03 15:32:38 +00:00
}
2020-05-23 18:22:05 +00:00
/* generate working segs */
2020-05-03 15:32:38 +00:00
for i := 0; i < 10; i++ {
pool.pools[rand.Intn(pool.poolsCount)].Nodes = map[string]*NodeStatus{}
}
/* init Next pointer */
var pre *PoolSeg
for i := pool.poolsCount*2 - 1; ; i-- {
if pool.pools[i%pool.poolsCount].Next != nil {
break
}
pool.pools[i%pool.poolsCount].Next = pre
if pool.pools[i%pool.poolsCount].Nodes != nil {
pre = &pool.pools[i%pool.poolsCount]
}
2020-04-13 14:35:17 +00:00
}
2020-05-25 12:50:41 +00:00
pool.versions = map[string]float64{}
pool.subscriptions = map[string]map[string]int{}
2020-05-03 15:32:38 +00:00
pool.heartBeat = map[string]time.Time{}
2019-06-04 03:15:12 +00:00
go func() {
2020-05-03 15:32:38 +00:00
pool.checkDeadNodes()
2019-06-04 03:15:12 +00:00
}()
2019-06-04 03:08:49 +00:00
2020-05-03 15:32:38 +00:00
pool.history = []PoolStatus{}
2019-04-29 09:05:15 +00:00
go func() {
2020-05-03 15:32:38 +00:00
pool.saveStatusHistory()
}()
}
/* check dead nodes periodically */
func (pool *ResourcePool) checkDeadNodes() {
for {
pool.heartBeatMu.Lock()
2020-05-04 10:19:39 +00:00
var nodesToDel []string
2020-05-03 15:32:38 +00:00
for k, v := range pool.heartBeat {
if v.Add(time.Second * 30).Before(time.Now()) {
2020-05-04 10:19:39 +00:00
segID := pool.getNodePool(k)
seg := &pool.pools[segID]
2020-05-03 15:32:38 +00:00
if seg.Nodes == nil {
seg = seg.Next
2019-04-29 09:05:15 +00:00
}
2020-05-03 16:59:20 +00:00
2020-05-04 10:05:11 +00:00
seg.Lock.Lock()
2020-05-14 12:52:39 +00:00
pool.TotalGPUMu.Lock()
2020-05-03 16:59:20 +00:00
if _, ok := seg.Nodes[k]; ok {
pool.TotalGPU -= len(seg.Nodes[k].Status)
}
pool.TotalGPUMu.Unlock()
2020-05-03 15:32:38 +00:00
delete(seg.Nodes, k)
seg.Lock.Unlock()
pool.versionsMu.Lock()
delete(pool.versions, k)
pool.versionsMu.Unlock()
2020-05-04 10:19:39 +00:00
nodesToDel = append(nodesToDel, k)
2020-05-03 15:32:38 +00:00
log.Info(" node ", k, " is offline")
2019-04-29 09:05:15 +00:00
}
2020-05-03 15:32:38 +00:00
}
2020-05-04 10:19:39 +00:00
for _, v := range nodesToDel {
segID := pool.getNodePool(v)
seg := &pool.pools[segID]
if seg.Nodes == nil {
seg = seg.Next
}
2020-05-14 12:52:39 +00:00
seg.Lock.Lock()
if seg.Nodes == nil {
seg = seg.Next
}
2020-05-04 10:19:39 +00:00
delete(seg.Nodes, v)
2020-05-14 12:52:39 +00:00
seg.Lock.Unlock()
2020-05-24 13:07:02 +00:00
delete(pool.heartBeat, v)
2020-05-04 10:19:39 +00:00
}
2020-05-03 15:32:38 +00:00
pool.heartBeatMu.Unlock()
time.Sleep(time.Second * 10)
}
}
2019-04-29 09:05:15 +00:00
2020-05-03 15:32:38 +00:00
func (pool *ResourcePool) GPUModelToPower(model string) int {
mapper := map[string]int{
"K40": 1, "Tesla K40": 1,
"K80": 2, "Tesla K80": 2,
"P100": 3, "Tesla P100": 3,
}
if power, err := mapper[model]; !err {
return power
}
return 0
}
2019-04-29 09:05:15 +00:00
2020-05-03 15:32:38 +00:00
func (pool *ResourcePool) getNodePool(name string) int {
h := fnv.New32a()
h.Write([]byte(name))
return int(h.Sum32()) % pool.poolsCount
}
/* save pool status periodically */
func (pool *ResourcePool) saveStatusHistory() {
2020-05-03 16:59:20 +00:00
/* waiting for nodes */
time.Sleep(time.Second * 30)
2020-05-03 15:32:38 +00:00
for {
summary := PoolStatus{}
UtilCPU := 0.0
TotalCPU := 0
TotalMem := 0
AvailableMem := 0
TotalGPU := 0
UtilGPU := 0
TotalMemGPU := 0
AvailableMemGPU := 0
nodesCount := 0
2020-05-25 11:29:35 +00:00
start := pool.pools[0]
if start.Nodes == nil {
start = *start.Next
}
2020-05-03 15:32:38 +00:00
for cur := start; ; {
cur.Lock.Lock()
for _, node := range cur.Nodes {
UtilCPU += node.UtilCPU
TotalCPU += node.NumCPU
TotalMem += node.MemTotal
AvailableMem += node.MemAvailable
for _, GPU := range node.Status {
UtilGPU += GPU.UtilizationGPU
TotalGPU ++
TotalMemGPU += GPU.MemoryTotal
AvailableMemGPU += GPU.MemoryFree
}
}
nodesCount += len(cur.Nodes)
cur.Lock.Unlock()
2020-05-25 11:29:35 +00:00
cur = *cur.Next
2020-05-03 16:17:59 +00:00
if cur.ID == start.ID {
2020-05-03 15:32:38 +00:00
break
2019-04-29 09:05:15 +00:00
}
2020-05-03 15:32:38 +00:00
}
summary.TimeStamp = time.Now().Format("2006-01-02 15:04:05")
summary.UtilCPU = UtilCPU / (float64(nodesCount) + 0.001)
summary.TotalCPU = TotalCPU
summary.TotalMem = TotalMem
summary.AvailableMem = AvailableMem
summary.TotalGPU = TotalGPU
if TotalGPU == 0 {
summary.UtilGPU = 0.0
} else {
summary.UtilGPU = UtilGPU / TotalGPU
}
summary.TotalMemGPU = TotalMemGPU
summary.AvailableMemGPU = AvailableMemGPU
pool.history = append(pool.history, summary)
2020-04-30 09:52:52 +00:00
2020-05-03 15:32:38 +00:00
if len(pool.history) > 60 {
pool.history = pool.history[len(pool.history)-60:]
2019-04-29 09:05:15 +00:00
}
2020-05-03 15:32:38 +00:00
2020-05-03 16:59:20 +00:00
pool.TotalGPUMu.Lock()
2020-05-03 15:32:38 +00:00
pool.TotalGPU = TotalGPU
2020-05-03 16:59:20 +00:00
pool.TotalGPUMu.Unlock()
2020-05-03 15:32:38 +00:00
time.Sleep(time.Second * 60)
}
2019-03-04 09:19:55 +00:00
}
2020-05-03 15:32:38 +00:00
/* update node info */
2019-04-16 08:59:19 +00:00
func (pool *ResourcePool) update(node NodeStatus) {
2020-05-25 13:41:39 +00:00
pool.poolsMu.Lock()
2020-05-25 13:54:38 +00:00
defer pool.poolsMu.Unlock()
2020-05-03 15:32:38 +00:00
segID := pool.getNodePool(node.ClientID)
seg := &pool.pools[segID]
if seg.Nodes == nil {
seg = seg.Next
}
seg.Lock.Lock()
defer seg.Lock.Unlock()
2019-03-04 09:19:55 +00:00
2020-05-03 15:32:38 +00:00
/* init bindings */
2020-04-12 03:13:23 +00:00
go func(node NodeStatus) {
2020-05-23 13:06:31 +00:00
pool.subscriptionsMu.Lock()
defer pool.subscriptionsMu.Unlock()
2020-04-13 12:29:58 +00:00
pool.bindingsMu.Lock()
defer pool.bindingsMu.Unlock()
2020-04-12 03:13:23 +00:00
for _, gpu := range node.Status {
if _, ok := pool.bindings[gpu.UUID]; ok {
2020-05-04 05:59:01 +00:00
if _, ok2 := pool.utils[gpu.UUID]; ok2 {
2020-04-30 15:06:12 +00:00
pool.utils[gpu.UUID] = append(pool.utils[gpu.UUID],
UtilGPUTimeSeries{Time: (int)(time.Now().Unix()), Util: gpu.UtilizationGPU})
2020-04-12 03:13:23 +00:00
}
}
2020-05-23 13:06:31 +00:00
if _, ok := pool.subscriptions[gpu.UUID]; ok {
for jobName := range pool.subscriptions[gpu.UUID] {
2020-05-23 17:41:19 +00:00
go func(name string) {
2020-05-24 13:07:02 +00:00
/* ask to update job status */
2020-05-23 17:41:19 +00:00
scheduler.QueryState(name)
}(jobName)
2020-05-23 13:06:31 +00:00
}
}
2020-04-12 03:13:23 +00:00
}
2020-04-13 12:29:58 +00:00
pool.heartBeatMu.Lock()
pool.heartBeat[node.ClientID] = time.Now()
pool.heartBeatMu.Unlock()
2020-04-12 03:13:23 +00:00
}(node)
2020-03-29 13:12:44 +00:00
pool.counterTotal++
2020-05-03 15:32:38 +00:00
pool.versionsMu.Lock()
2020-03-29 13:12:44 +00:00
if version, ok := pool.versions[node.ClientID]; ok && version == node.Version {
2020-05-25 11:29:35 +00:00
//pool.versionsMu.Unlock()
//return
2020-03-29 13:12:44 +00:00
}
2020-05-03 15:32:38 +00:00
pool.versionsMu.Unlock()
pool.counter++
2020-04-12 02:44:32 +00:00
log.Debug(node.Version, "!=", pool.versions[node.ClientID])
2020-04-11 03:38:04 +00:00
2020-05-03 15:32:38 +00:00
status, ok := seg.Nodes[node.ClientID]
2019-03-20 03:14:07 +00:00
if ok {
2020-05-04 05:59:01 +00:00
/* keep allocation info */
2019-04-16 08:59:19 +00:00
for i, GPU := range status.Status {
if GPU.UUID == node.Status[i].UUID {
node.Status[i].MemoryAllocated = GPU.MemoryAllocated
2019-03-20 03:14:07 +00:00
}
}
2020-05-03 16:59:20 +00:00
} else {
2020-05-25 13:41:39 +00:00
/* TODO: double check node do belong to this seg */
2020-05-03 16:59:20 +00:00
pool.TotalGPUMu.Lock()
pool.TotalGPU += len(node.Status)
pool.TotalGPUMu.Unlock()
2020-05-25 12:50:41 +00:00
log.Info("node ", node.ClientID, " is online")
2019-03-20 03:14:07 +00:00
}
2020-05-03 15:32:38 +00:00
seg.Nodes[node.ClientID] = &node
if len(seg.Nodes) > 10 {
2020-05-25 13:41:39 +00:00
go func() {
pool.scaleSeg(seg)
}()
2020-05-03 15:32:38 +00:00
}
2020-03-29 13:12:44 +00:00
pool.versions[node.ClientID] = node.Version
2019-03-04 09:19:55 +00:00
}
2020-05-03 15:32:38 +00:00
/* spilt seg */
func (pool *ResourcePool) scaleSeg(seg *PoolSeg) {
2020-05-03 16:17:59 +00:00
log.Info("Scaling seg ", seg.ID)
2020-05-03 15:32:38 +00:00
2020-05-25 13:41:39 +00:00
pool.poolsMu.Lock()
defer pool.poolsMu.Unlock()
2020-05-03 15:32:38 +00:00
2020-05-25 13:41:39 +00:00
var segIDs []int
segIDs = append(segIDs, seg.ID)
2020-05-03 15:32:38 +00:00
2020-05-25 13:41:39 +00:00
/* find previous seg */
var pre *PoolSeg
for i := seg.ID + pool.poolsCount - 1; i >= 0; i-- {
segIDs = append(segIDs, i%pool.poolsCount)
2020-05-25 15:56:49 +00:00
if pool.pools[i%pool.poolsCount].Next.ID != seg.ID {
2020-05-25 13:41:39 +00:00
break
2020-05-03 15:32:38 +00:00
}
2020-05-25 15:56:49 +00:00
pre = &pool.pools[i%pool.poolsCount]
2020-05-25 13:41:39 +00:00
}
2020-05-03 15:32:38 +00:00
2020-05-25 13:41:39 +00:00
distance := seg.ID - pre.ID
if distance < 0 {
distance += pool.poolsCount
}
if distance <= 1 {
2020-05-25 15:56:49 +00:00
log.Warn("Unable to scale, ", seg.ID, ", already full")
2020-05-25 13:41:39 +00:00
return
}
2020-05-25 11:29:35 +00:00
2020-05-25 13:41:39 +00:00
candidate := pre
/* walk to the nearest middle */
2020-05-25 15:56:49 +00:00
if pre.ID < seg.ID {
candidate = &pool.pools[(pre.ID+seg.ID)/2]
} else {
candidate = &pool.pools[(pre.ID+seg.ID+pool.poolsCount)/2%pool.poolsCount]
2020-05-25 13:41:39 +00:00
}
2020-05-25 15:56:49 +00:00
candidate.Next = seg
candidate.Nodes = map[string]*NodeStatus{}
2020-05-03 15:32:38 +00:00
2020-05-25 13:41:39 +00:00
/* lock in asc sequence to avoid deadlock */
sort.Ints(segIDs)
for _, id := range segIDs {
pool.pools[id].Lock.Lock()
}
2020-05-25 13:53:20 +00:00
log.Println(segIDs)
2020-05-25 13:41:39 +00:00
/* update Next */
2020-05-25 15:56:49 +00:00
for i := 0; ; i++ {
id := (pre.ID + i) % pool.poolsCount
if id == candidate.ID {
break
}
pool.pools[id].Next = candidate
2020-05-25 13:41:39 +00:00
}
/* move nodes */
nodesToMove := map[string]*NodeStatus{}
for _, node := range seg.Nodes {
seg2ID := pool.getNodePool(node.ClientID)
seg2 := &pool.pools[seg2ID]
if seg2.Nodes == nil {
seg2 = seg2.Next
2020-05-03 15:32:38 +00:00
}
2020-05-25 15:56:49 +00:00
if seg2.ID != seg.ID {
2020-05-25 13:41:39 +00:00
nodesToMove[node.ClientID] = node
}
}
for _, node := range nodesToMove {
delete(seg.Nodes, node.ClientID)
}
candidate.Nodes = nodesToMove
2020-05-25 15:56:49 +00:00
//log.Info("pre=", pre.ID, " active=", candidate.ID, " seg=", seg.ID)
2020-05-25 13:41:39 +00:00
for _, id := range segIDs {
pool.pools[id].Lock.Unlock()
}
2020-05-03 15:32:38 +00:00
}
/* get node by ClientID */
2019-04-18 09:25:37 +00:00
func (pool *ResourcePool) getByID(id string) NodeStatus {
2020-04-13 14:35:17 +00:00
poolID := pool.getNodePool(id)
2020-05-03 15:32:38 +00:00
seg := &pool.pools[poolID]
if seg.Nodes == nil {
seg = seg.Next
}
seg.Lock.Lock()
defer seg.Lock.Unlock()
2020-04-13 14:35:17 +00:00
2020-05-03 15:32:38 +00:00
status, ok := seg.Nodes[id]
2019-03-04 09:19:55 +00:00
if ok {
2020-05-03 15:32:38 +00:00
return *status
2019-03-04 09:19:55 +00:00
}
2019-04-16 08:59:19 +00:00
return NodeStatus{}
2019-03-04 09:19:55 +00:00
}
2019-04-29 09:05:15 +00:00
2020-05-03 15:32:38 +00:00
/* get all nodes */
2019-04-29 09:05:15 +00:00
func (pool *ResourcePool) list() MsgResource {
2020-04-13 14:35:17 +00:00
nodes := map[string]NodeStatus{}
2020-05-03 15:32:38 +00:00
2020-05-25 11:29:35 +00:00
start := pool.pools[0]
if start.Nodes == nil {
start = *start.Next
}
2020-05-03 15:32:38 +00:00
for cur := start; ; {
cur.Lock.Lock()
for k, node := range cur.Nodes {
nodes[k] = *node
}
2020-05-03 15:43:47 +00:00
cur.Lock.Unlock()
2020-05-25 11:29:35 +00:00
cur = *cur.Next
if cur.ID == start.ID {
2020-05-03 15:32:38 +00:00
break
2020-04-13 14:35:17 +00:00
}
}
return MsgResource{Code: 0, Resource: nodes}
2019-04-29 09:05:15 +00:00
}
func (pool *ResourcePool) statusHistory() MsgPoolStatusHistory {
return MsgPoolStatusHistory{Code: 0, Data: pool.history}
}
2019-06-05 09:09:22 +00:00
2020-03-29 13:12:44 +00:00
func (pool *ResourcePool) getCounter() map[string]int {
return map[string]int{"counter": pool.counter, "counterTotal": pool.counterTotal}
}
2019-06-05 09:09:22 +00:00
func (pool *ResourcePool) acquireNetwork() string {
2019-06-13 02:53:00 +00:00
pool.networkMu.Lock()
defer pool.networkMu.Unlock()
2019-06-05 09:09:22 +00:00
var network string
2020-04-13 11:41:28 +00:00
log.Debug(pool.networksFree)
2019-06-05 09:09:22 +00:00
if len(pool.networksFree) == 0 {
for {
2019-06-13 02:53:00 +00:00
for {
network = "yao-net-" + strconv.Itoa(rand.Intn(999999))
if _, ok := pool.networks[network]; !ok {
break
}
2019-06-05 09:09:22 +00:00
}
2019-06-13 02:53:00 +00:00
v := url.Values{}
v.Set("name", network)
2019-06-13 03:30:55 +00:00
resp, err := doRequest("POST", "http://yao-agent-master:8000/create", strings.NewReader(v.Encode()), "application/x-www-form-urlencoded", "")
2019-06-13 02:53:00 +00:00
if err != nil {
log.Println(err.Error())
continue
}
2020-05-24 13:07:02 +00:00
resp.Body.Close()
2019-06-13 02:53:00 +00:00
pool.networksFree[network] = true
pool.networks[network] = true
break
2019-06-05 09:09:22 +00:00
}
}
2019-06-13 02:53:00 +00:00
2019-06-05 09:09:22 +00:00
for k := range pool.networksFree {
network = k
delete(pool.networksFree, k)
2020-05-03 07:19:21 +00:00
break
2019-06-05 09:09:22 +00:00
}
return network
}
func (pool *ResourcePool) releaseNetwork(network string) {
pool.networkMu.Lock()
pool.networksFree[network] = true
pool.networkMu.Unlock()
}
2020-04-11 03:38:04 +00:00
func (pool *ResourcePool) attach(GPU string, job string) {
2020-05-23 13:06:31 +00:00
pool.subscriptionsMu.Lock()
defer pool.subscriptionsMu.Unlock()
2020-04-13 12:29:58 +00:00
pool.bindingsMu.Lock()
defer pool.bindingsMu.Unlock()
2020-05-23 13:06:31 +00:00
if _, ok := pool.subscriptions[GPU]; !ok {
pool.subscriptions[GPU] = map[string]int{}
}
pool.subscriptions[GPU][job] = int(time.Now().Unix())
2020-04-12 03:13:23 +00:00
if _, ok := pool.bindings[GPU]; !ok {
2020-04-30 10:39:47 +00:00
pool.bindings[GPU] = map[string]int{}
2020-04-12 03:13:23 +00:00
}
2020-04-30 10:39:47 +00:00
pool.bindings[GPU][job] = int(time.Now().Unix())
2020-04-12 03:13:23 +00:00
if _, ok := pool.utils[GPU]; !ok {
2020-04-30 15:06:12 +00:00
pool.utils[GPU] = []UtilGPUTimeSeries{}
2020-04-11 03:38:04 +00:00
}
2020-05-04 05:59:01 +00:00
if len(pool.bindings[GPU]) > 1 {
delete(pool.utils, GPU)
}
2020-04-11 03:38:04 +00:00
}
2020-05-04 10:05:11 +00:00
func (pool *ResourcePool) detach(GPU string, job Job) {
2020-05-23 13:06:31 +00:00
pool.subscriptionsMu.Lock()
defer pool.subscriptionsMu.Unlock()
2020-04-13 12:29:58 +00:00
pool.bindingsMu.Lock()
defer pool.bindingsMu.Unlock()
2020-05-23 13:06:31 +00:00
if _, ok := pool.subscriptions[GPU]; ok {
delete(pool.subscriptions[GPU], job.Name)
}
2020-04-12 03:13:23 +00:00
if _, ok := pool.bindings[GPU]; ok {
2020-05-04 05:59:01 +00:00
if _, ok2 := pool.utils[GPU]; ok2 {
2020-05-25 12:50:41 +00:00
if len(pool.bindings[GPU]) == 1 && job.Status == Finished {
2020-05-04 10:05:11 +00:00
InstanceOfOptimizer().feed(job.Name, pool.utils[GPU])
2020-05-04 05:59:01 +00:00
}
delete(pool.utils, GPU)
2020-04-12 03:13:23 +00:00
}
}
2020-04-11 03:38:04 +00:00
if list, ok := pool.bindings[GPU]; ok {
2020-05-04 10:05:11 +00:00
delete(list, job.Name)
2020-04-11 03:38:04 +00:00
}
}
2020-04-12 02:44:32 +00:00
2020-05-24 13:07:02 +00:00
func (pool *ResourcePool) countGPU() (int, int) {
2020-05-25 12:50:41 +00:00
return pool.TotalGPU - pool.UsingGPU, pool.TotalGPU
2020-05-24 13:07:02 +00:00
}
2020-04-30 10:39:47 +00:00
func (pool *ResourcePool) getBindings() map[string]map[string]int {
2020-04-12 02:44:32 +00:00
return pool.bindings
2020-04-13 12:29:58 +00:00
}
2020-04-30 13:22:21 +00:00
2020-05-01 04:48:06 +00:00
func (pool *ResourcePool) pickNode(candidates []*NodeStatus, availableGPUs map[string][]GPUStatus, task Task, job Job, nodes []NodeStatus) *NodeStatus {
2020-04-30 13:22:21 +00:00
/* shuffle */
r := rand.New(rand.NewSource(time.Now().Unix()))
2020-05-01 04:48:06 +00:00
for n := len(candidates); n > 0; n-- {
2020-04-30 13:22:21 +00:00
randIndex := r.Intn(n)
2020-05-01 04:48:06 +00:00
candidates[n-1], candidates[randIndex] = candidates[randIndex], candidates[n-1]
2020-04-30 13:22:21 +00:00
}
/* sort */
2020-05-01 04:48:06 +00:00
// single node, single GPU
sort.Slice(candidates, func(a, b int) bool {
diffA := pool.GPUModelToPower(candidates[a].Status[0].ProductName) - pool.GPUModelToPower(task.ModelGPU)
diffB := pool.GPUModelToPower(candidates[b].Status[0].ProductName) - pool.GPUModelToPower(task.ModelGPU)
2020-04-30 13:22:21 +00:00
2020-05-01 04:48:06 +00:00
if diffA > 0 && diffB >= 0 && diffA > diffB {
return false //b
}
if diffA < 0 && diffB < 0 && diffA > diffB {
return false
}
if diffA < 0 && diffB >= 0 {
return false
}
if diffA == diffB {
if len(availableGPUs[candidates[a].ClientID]) == len(availableGPUs[candidates[b].ClientID]) {
return candidates[a].UtilCPU > candidates[b].UtilCPU
}
return len(availableGPUs[candidates[a].ClientID]) < len(availableGPUs[candidates[b].ClientID])
}
return true //a
})
var t []*NodeStatus
bestGPU := candidates[0].Status[0].ProductName
for _, node := range candidates {
if node.Status[0].ProductName != bestGPU {
break
}
t = append(t, node)
}
candidates = t
if (len(job.Tasks) == 1) && task.NumberGPU > 1 { //single node, multi GPUs
sort.Slice(candidates, func(a, b int) bool {
if len(availableGPUs[candidates[a].ClientID]) == len(availableGPUs[candidates[b].ClientID]) {
return candidates[a].UtilCPU > candidates[b].UtilCPU
}
return len(availableGPUs[candidates[a].ClientID]) < len(availableGPUs[candidates[b].ClientID])
})
}
if len(job.Tasks) > 1 { //multi nodes, multi GPUs
sort.Slice(candidates, func(a, b int) bool {
distanceA := 0
distanceB := 0
for _, node := range nodes {
if node.Rack != candidates[a].Rack {
distanceA += 10
}
if node.ClientID != candidates[a].ClientID {
distanceA += 1
}
if node.Rack != candidates[b].Rack {
distanceB += 10
}
if node.ClientID != candidates[b].ClientID {
distanceB += 1
}
}
if distanceA == distanceB {
return len(availableGPUs[candidates[a].ClientID]) > len(availableGPUs[candidates[b].ClientID])
}
return distanceA*job.Locality < distanceB*job.Locality
})
}
return candidates[0]
2020-04-30 13:22:21 +00:00
}
2020-05-24 13:07:02 +00:00
func (pool *ResourcePool) acquireResource(job Job) []NodeStatus {
if len(job.Tasks) == 0 {
return []NodeStatus{}
}
task := job.Tasks[0]
segID := rand.Intn(pool.poolsCount)
if pool.TotalGPU < 100 {
segID = 0
}
start := &pool.pools[segID]
if start.Nodes == nil {
start = start.Next
}
locks := map[int]*sync.Mutex{}
allocationType := 0
availableGPUs := map[string][]GPUStatus{}
var candidates []*NodeStatus
/* first, choose sharable GPUs */
if pool.enableShare && (pool.TotalGPU != 0 && len(job.Tasks) == 1 && task.NumberGPU == 1 && float64(pool.UsingGPU)/float64(pool.TotalGPU) >= pool.enableShareRatio) {
// check sharable
allocationType = 1
if util, valid := InstanceOfOptimizer().predictUtilGPU(job.Name); valid {
2020-05-25 11:29:35 +00:00
for cur := start; ; {
2020-05-24 13:07:02 +00:00
if _, ok := locks[cur.ID]; !ok {
cur.Lock.Lock()
locks[cur.ID] = &cur.Lock
}
for _, node := range cur.Nodes {
var available []GPUStatus
for _, status := range node.Status {
if status.MemoryAllocated > 0 && status.MemoryTotal > task.MemoryGPU+status.MemoryAllocated {
if jobs, ok := pool.bindings[status.UUID]; ok {
totalUtil := util
for job := range jobs {
if utilT, ok := InstanceOfOptimizer().predictUtilGPU(job); ok {
totalUtil += utilT
} else {
totalUtil += 100
}
}
if totalUtil < 100 {
available = append(available, status)
availableGPUs[node.ClientID] = available
}
}
}
}
if len(available) >= task.NumberGPU {
candidates = append(candidates, node)
if len(candidates) >= len(job.Tasks)*3+5 {
break
}
}
}
if len(candidates) >= len(job.Tasks)*3+5 {
break
}
2020-05-25 11:29:35 +00:00
if cur.ID > cur.Next.ID {
2020-05-24 13:07:02 +00:00
break
}
2020-05-25 11:29:35 +00:00
cur = cur.Next
2020-05-24 13:07:02 +00:00
}
}
//log.Info(candidates)
}
/* second round, find vacant gpu */
if len(candidates) == 0 {
allocationType = 2
2020-05-25 11:29:35 +00:00
for cur := start; ; {
2020-05-24 13:07:02 +00:00
if _, ok := locks[cur.ID]; !ok {
cur.Lock.Lock()
locks[cur.ID] = &cur.Lock
}
for _, node := range cur.Nodes {
var available []GPUStatus
for _, status := range node.Status {
if status.MemoryAllocated == 0 && status.MemoryUsed < 10 {
available = append(available, status)
}
}
if len(available) >= task.NumberGPU {
candidates = append(candidates, node)
availableGPUs[node.ClientID] = available
if len(candidates) >= len(job.Tasks)*3+5 {
break
}
}
}
if len(candidates) >= len(job.Tasks)*3+5 {
break
}
2020-05-25 11:29:35 +00:00
if cur.ID > cur.Next.ID {
2020-05-24 13:07:02 +00:00
break
}
2020-05-25 11:29:35 +00:00
cur = cur.Next
2020-05-24 13:07:02 +00:00
}
//log.Info(candidates)
}
/* third round, find gpu to be released */
if len(candidates) == 0 && len(job.Tasks) == 1 && task.NumberGPU == 1 && pool.enablePreSchedule {
estimate, valid := InstanceOfOptimizer().predictTime(job.Name)
//log.Info(pool.TotalGPU)
//log.Info(estimate, valid)
//log.Info(scheduler.UsingGPU)
if pool.TotalGPU != 0 && float64(pool.UsingGPU)/float64(pool.TotalGPU) >= pool.enablePreScheduleRatio && valid {
allocationType = 3
2020-05-25 11:29:35 +00:00
for cur := start; ; {
2020-05-24 13:07:02 +00:00
if _, ok := locks[cur.ID]; !ok {
cur.Lock.Lock()
locks[cur.ID] = &cur.Lock
}
for _, node := range cur.Nodes {
var available []GPUStatus
for _, status := range node.Status {
bindings := pool.getBindings()
if tasks, ok := bindings[status.UUID]; ok {
if len(tasks) > 1 || status.MemoryAllocated == 0 {
continue
}
for taskT, s := range tasks {
est, valid2 := InstanceOfOptimizer().predictTime(taskT)
if valid2 {
now := (int)(time.Now().Unix())
log.Info(s, now, estimate, est)
if now-s > est.Total-est.Post-estimate.Pre-15 {
available = append(available, status)
}
}
}
}
}
if len(available) >= task.NumberGPU {
candidates = append(candidates, node)
availableGPUs[node.ClientID] = available
if len(candidates) >= len(job.Tasks)*3+5 {
break
}
}
}
if len(candidates) >= len(job.Tasks)*3+5 {
break
}
2020-05-25 11:29:35 +00:00
if cur.ID > cur.Next.ID {
break
}
cur = cur.Next
2020-05-24 13:07:02 +00:00
}
//log.Info(candidates)
}
}
if len(candidates) > 0 {
log.Info("allocationType is ", allocationType)
//log.Info(candidates)
}
/* assign */
var ress []NodeStatus
if len(candidates) > 0 {
2020-05-25 03:35:44 +00:00
/*
for range job.Tasks { //append would cause uncertain order
resources = append(resources, NodeStatus{ClientID: "null"})
}
*/
2020-05-24 13:07:02 +00:00
var nodes []NodeStatus
if len(job.Tasks) == 1 {
node := pool.pickNode(candidates, availableGPUs, task, job, []NodeStatus{})
nodes = append(nodes, *node)
}
for _, node := range nodes {
res := NodeStatus{}
res.ClientID = node.ClientID
res.ClientHost = node.ClientHost
res.Status = availableGPUs[node.ClientID][0:task.NumberGPU]
res.NumCPU = task.NumberCPU
res.MemTotal = task.Memory
for i := range res.Status {
for j := range node.Status {
if res.Status[i].UUID == node.Status[j].UUID {
if node.Status[j].MemoryAllocated == 0 {
pool.UsingGPUMu.Lock()
pool.UsingGPU ++
pool.UsingGPUMu.Unlock()
}
node.Status[j].MemoryAllocated += task.MemoryGPU
res.Status[i].MemoryTotal = task.MemoryGPU
}
}
}
for _, t := range res.Status {
pool.attach(t.UUID, job.Name)
}
ress = append(ress, res)
}
}
for segID, lock := range locks {
log.Debug("Unlock ", segID)
lock.Unlock()
}
return ress
}
func (pool *ResourcePool) releaseResource(job Job, agent NodeStatus) {
segID := pool.getNodePool(agent.ClientID)
seg := pool.pools[segID]
if seg.Nodes == nil {
seg = *seg.Next
}
seg.Lock.Lock()
defer seg.Lock.Unlock()
node := seg.Nodes[agent.ClientID]
for _, gpu := range agent.Status {
for j := range node.Status {
if gpu.UUID == node.Status[j].UUID {
node.Status[j].MemoryAllocated -= gpu.MemoryTotal
if node.Status[j].MemoryAllocated < 0 {
// in case of error
log.Warn(node.ClientID, "More Memory Allocated")
node.Status[j].MemoryAllocated = 0
}
if node.Status[j].MemoryAllocated == 0 {
pool.UsingGPUMu.Lock()
pool.UsingGPU--
pool.UsingGPUMu.Unlock()
log.Info(node.Status[j].UUID, " is released")
}
//log.Info(node.Status[j].MemoryAllocated)
}
}
}
}
func (pool *ResourcePool) SetShareRatio(ratio float64) bool {
pool.enableShareRatio = ratio
log.Info("enableShareRatio is updated to ", ratio)
return true
}
func (pool *ResourcePool) SetPreScheduleRatio(ratio float64) bool {
pool.enablePreScheduleRatio = ratio
log.Info("enablePreScheduleRatio is updated to ", ratio)
return true
}