| 1 |
package scheduler |
| 2 |
|
| 3 |
import ( |
| 4 |
log "github.com/golang/glog" |
| 5 |
mesos "github.com/mesos/mesos-go/api/v0/mesosproto" |
| 6 |
"github.com/mesos/mesos-go/api/v0/upid" |
| 7 |
"sync" |
| 8 |
) |
| 9 |
|
| 10 |
type cachedOffer struct { |
| 11 |
offer *mesos.Offer |
| 12 |
slavePid *upid.UPID |
| 13 |
} |
| 14 |
|
| 15 |
func newCachedOffer(offer *mesos.Offer, slavePid *upid.UPID) *cachedOffer { |
| 16 |
return &cachedOffer{offer: offer, slavePid: slavePid} |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
type schedCache struct { |
| 22 |
lock sync.RWMutex |
| 23 |
savedOffers map[string]*cachedOffer |
| 24 |
savedSlavePids map[string]*upid.UPID |
| 25 |
} |
| 26 |
|
| 27 |
func newSchedCache() *schedCache { |
| 28 |
return &schedCache{ |
| 29 |
savedOffers: make(map[string]*cachedOffer), |
| 30 |
savedSlavePids: make(map[string]*upid.UPID), |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
func (cache *schedCache) putOffer(offer *mesos.Offer, pid *upid.UPID) { |
| 36 |
if offer == nil || pid == nil { |
| 37 |
log.V(3).Infoln("WARN: Offer not cached. The offer or pid cannot be nil") |
| 38 |
return |
| 39 |
} |
| 40 |
log.V(3).Infoln("Caching offer ", offer.Id.GetValue(), " with slavePID ", pid.String()) |
| 41 |
cache.lock.Lock() |
| 42 |
cache.savedOffers[offer.Id.GetValue()] = &cachedOffer{offer: offer, slavePid: pid} |
| 43 |
cache.lock.Unlock() |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
func (cache *schedCache) getOffer(offerId *mesos.OfferID) *cachedOffer { |
| 48 |
if offerId == nil { |
| 49 |
log.V(3).Infoln("WARN: OfferId == nil, returning nil") |
| 50 |
return nil |
| 51 |
} |
| 52 |
cache.lock.RLock() |
| 53 |
defer cache.lock.RUnlock() |
| 54 |
return cache.savedOffers[offerId.GetValue()] |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
func (cache *schedCache) containsOffer(offerId *mesos.OfferID) bool { |
| 59 |
cache.lock.RLock() |
| 60 |
defer cache.lock.RUnlock() |
| 61 |
_, ok := cache.savedOffers[offerId.GetValue()] |
| 62 |
return ok |
| 63 |
} |
| 64 |
|
| 65 |
func (cache *schedCache) removeOffer(offerId *mesos.OfferID) { |
| 66 |
cache.lock.Lock() |
| 67 |
delete(cache.savedOffers, offerId.GetValue()) |
| 68 |
cache.lock.Unlock() |
| 69 |
} |
| 70 |
|
| 71 |
func (cache *schedCache) putSlavePid(slaveId *mesos.SlaveID, pid *upid.UPID) { |
| 72 |
cache.lock.Lock() |
| 73 |
cache.savedSlavePids[slaveId.GetValue()] = pid |
| 74 |
cache.lock.Unlock() |
| 75 |
} |
| 76 |
|
| 77 |
func (cache *schedCache) getSlavePid(slaveId *mesos.SlaveID) *upid.UPID { |
| 78 |
if slaveId == nil { |
| 79 |
log.V(3).Infoln("SlaveId == nil, returning empty UPID") |
| 80 |
return nil |
| 81 |
} |
| 82 |
return cache.savedSlavePids[slaveId.GetValue()] |
| 83 |
} |
| 84 |
|
| 85 |
func (cache *schedCache) containsSlavePid(slaveId *mesos.SlaveID) bool { |
| 86 |
cache.lock.RLock() |
| 87 |
defer cache.lock.RUnlock() |
| 88 |
_, ok := cache.savedSlavePids[slaveId.GetValue()] |
| 89 |
return ok |
| 90 |
} |
| 91 |
|
| 92 |
func (cache *schedCache) removeSlavePid(slaveId *mesos.SlaveID) { |
| 93 |
cache.lock.Lock() |
| 94 |
delete(cache.savedSlavePids, slaveId.GetValue()) |
| 95 |
cache.lock.Unlock() |
| 96 |
} |