2016-12-12 06:33:12 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/heap"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2016-12-12 07:08:13 +00:00
|
|
|
"github.com/hashicorp/nomad/client/stats"
|
2016-12-12 06:33:12 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2016-12-16 07:54:54 +00:00
|
|
|
const (
|
|
|
|
// diskUsageThreshold is the percent of used disk space beyond which Nomad
|
|
|
|
// GCs terminated allocations
|
|
|
|
diskUsageThreshold = 80
|
|
|
|
|
|
|
|
// gcInterval is the interval at which Nomad runs the garbage collector
|
|
|
|
gcInterval = 1 * time.Minute
|
|
|
|
|
|
|
|
// inodeUsageThreshold is the percent of inode usage that Nomad tries to
|
|
|
|
// maintain, whenever we are over it we will attempt to GC terminal
|
|
|
|
// allocations
|
|
|
|
inodeUsageThreshold = 70
|
2016-12-19 18:51:57 +00:00
|
|
|
|
|
|
|
// MB is a constant which converts values in bytes to MB
|
|
|
|
MB = 1024 * 1024
|
2016-12-16 07:54:54 +00:00
|
|
|
)
|
|
|
|
|
2016-12-20 01:53:11 +00:00
|
|
|
// GCAlloc wraps an allocation runner and an index enabling it to be used within
|
|
|
|
// a PQ
|
2016-12-12 06:33:12 +00:00
|
|
|
type GCAlloc struct {
|
2016-12-12 06:40:11 +00:00
|
|
|
timeStamp time.Time
|
|
|
|
allocRunner *AllocRunner
|
|
|
|
index int
|
2016-12-12 06:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type GCAllocPQImpl []*GCAlloc
|
|
|
|
|
|
|
|
func (pq GCAllocPQImpl) Len() int {
|
|
|
|
return len(pq)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pq GCAllocPQImpl) Less(i, j int) bool {
|
|
|
|
return pq[i].timeStamp.Before(pq[j].timeStamp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pq GCAllocPQImpl) Swap(i, j int) {
|
|
|
|
pq[i], pq[j] = pq[j], pq[i]
|
|
|
|
pq[i].index = i
|
|
|
|
pq[j].index = j
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pq *GCAllocPQImpl) Push(x interface{}) {
|
|
|
|
n := len(*pq)
|
|
|
|
item := x.(*GCAlloc)
|
|
|
|
item.index = n
|
|
|
|
*pq = append(*pq, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pq *GCAllocPQImpl) Pop() interface{} {
|
|
|
|
old := *pq
|
|
|
|
n := len(old)
|
|
|
|
item := old[n-1]
|
|
|
|
item.index = -1 // for safety
|
|
|
|
*pq = old[0 : n-1]
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
|
|
|
// IndexedGCAllocPQ is an indexed PQ which maintains a list of allocation runner
|
|
|
|
// based on their termination time.
|
|
|
|
type IndexedGCAllocPQ struct {
|
|
|
|
index map[string]*GCAlloc
|
|
|
|
heap GCAllocPQImpl
|
2016-12-16 07:54:54 +00:00
|
|
|
|
|
|
|
pqLock sync.Mutex
|
2016-12-12 06:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIndexedGCAllocPQ() *IndexedGCAllocPQ {
|
|
|
|
return &IndexedGCAllocPQ{
|
|
|
|
index: make(map[string]*GCAlloc),
|
|
|
|
heap: make(GCAllocPQImpl, 0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IndexedGCAllocPQ) Push(ar *AllocRunner) error {
|
2016-12-16 07:54:54 +00:00
|
|
|
i.pqLock.Lock()
|
|
|
|
defer i.pqLock.Unlock()
|
|
|
|
|
2016-12-12 06:33:12 +00:00
|
|
|
alloc := ar.Alloc()
|
|
|
|
if _, ok := i.index[alloc.ID]; ok {
|
|
|
|
return fmt.Errorf("alloc %v already being tracked for GC", alloc.ID)
|
|
|
|
}
|
|
|
|
gcAlloc := &GCAlloc{
|
2016-12-12 06:40:11 +00:00
|
|
|
timeStamp: time.Now(),
|
|
|
|
allocRunner: ar,
|
2016-12-12 06:33:12 +00:00
|
|
|
}
|
|
|
|
i.index[alloc.ID] = gcAlloc
|
|
|
|
heap.Push(&i.heap, gcAlloc)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IndexedGCAllocPQ) Pop() *GCAlloc {
|
2016-12-16 07:54:54 +00:00
|
|
|
i.pqLock.Lock()
|
|
|
|
defer i.pqLock.Unlock()
|
|
|
|
|
2016-12-12 06:33:12 +00:00
|
|
|
if len(i.heap) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
gcAlloc := heap.Pop(&i.heap).(*GCAlloc)
|
2016-12-12 06:40:11 +00:00
|
|
|
delete(i.index, gcAlloc.allocRunner.Alloc().ID)
|
2016-12-12 06:33:12 +00:00
|
|
|
return gcAlloc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IndexedGCAllocPQ) Remove(allocID string) (*GCAlloc, error) {
|
2016-12-16 07:54:54 +00:00
|
|
|
i.pqLock.Lock()
|
|
|
|
defer i.pqLock.Unlock()
|
|
|
|
|
2016-12-12 06:33:12 +00:00
|
|
|
if gcAlloc, ok := i.index[allocID]; ok {
|
|
|
|
heap.Remove(&i.heap, gcAlloc.index)
|
|
|
|
delete(i.index, allocID)
|
|
|
|
return gcAlloc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("alloc %q not present", allocID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IndexedGCAllocPQ) Length() int {
|
2016-12-16 07:54:54 +00:00
|
|
|
i.pqLock.Lock()
|
|
|
|
defer i.pqLock.Unlock()
|
|
|
|
|
2016-12-12 06:33:12 +00:00
|
|
|
return len(i.heap)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllocGarbageCollector garbage collects terminated allocations on a node
|
|
|
|
type AllocGarbageCollector struct {
|
2016-12-12 07:08:13 +00:00
|
|
|
allocRunners *IndexedGCAllocPQ
|
2016-12-16 07:54:54 +00:00
|
|
|
statsCollector stats.NodeStatsCollector
|
|
|
|
reservedDiskMB int
|
2016-12-12 07:08:13 +00:00
|
|
|
logger *log.Logger
|
2016-12-16 07:54:54 +00:00
|
|
|
shutdownCh chan struct{}
|
2016-12-12 06:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAllocGarbageCollector returns a garbage collector for terminated
|
|
|
|
// allocations on a node.
|
2016-12-16 07:54:54 +00:00
|
|
|
func NewAllocGarbageCollector(logger *log.Logger, statsCollector stats.NodeStatsCollector, reservedDiskMB int) *AllocGarbageCollector {
|
|
|
|
gc := &AllocGarbageCollector{
|
2016-12-12 07:08:13 +00:00
|
|
|
allocRunners: NewIndexedGCAllocPQ(),
|
|
|
|
statsCollector: statsCollector,
|
2016-12-16 07:54:54 +00:00
|
|
|
reservedDiskMB: reservedDiskMB,
|
2016-12-12 07:08:13 +00:00
|
|
|
logger: logger,
|
2016-12-16 07:54:54 +00:00
|
|
|
shutdownCh: make(chan struct{}),
|
2016-12-12 06:33:12 +00:00
|
|
|
}
|
2016-12-16 07:54:54 +00:00
|
|
|
go gc.run()
|
|
|
|
|
|
|
|
return gc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AllocGarbageCollector) run() {
|
|
|
|
ticker := time.NewTicker(gcInterval)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
if err := a.keepUsageBelowThreshold(); err != nil {
|
2016-12-20 01:53:11 +00:00
|
|
|
a.logger.Printf("[ERR] client: error garbage collecting allocation: %v", err)
|
2016-12-16 07:54:54 +00:00
|
|
|
}
|
|
|
|
case <-a.shutdownCh:
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// keepUsageBelowThreshold collects disk usage information and garbage collects
|
|
|
|
// allocations to make disk space available.
|
|
|
|
func (a *AllocGarbageCollector) keepUsageBelowThreshold() error {
|
|
|
|
for {
|
|
|
|
// Check if we have enough free space
|
|
|
|
err := a.statsCollector.Collect()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if we are below thresholds for used disk space and inode usage
|
|
|
|
diskStats := a.statsCollector.Stats().AllocDirStats
|
2016-12-19 18:51:57 +00:00
|
|
|
|
|
|
|
if diskStats == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2016-12-16 07:54:54 +00:00
|
|
|
if diskStats.UsedPercent <= diskUsageThreshold &&
|
|
|
|
diskStats.InodesUsedPercent <= inodeUsageThreshold {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect an allocation
|
|
|
|
gcAlloc := a.allocRunners.Pop()
|
|
|
|
if gcAlloc == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
ar := gcAlloc.allocRunner
|
|
|
|
alloc := ar.Alloc()
|
|
|
|
a.logger.Printf("[INFO] client: garbage collecting allocation %v", alloc.ID)
|
|
|
|
|
|
|
|
// Destroy the alloc runner and wait until it exits
|
|
|
|
ar.Destroy()
|
|
|
|
select {
|
|
|
|
case <-ar.WaitCh():
|
|
|
|
case <-a.shutdownCh:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AllocGarbageCollector) Stop() {
|
2016-12-20 01:53:11 +00:00
|
|
|
close(a.shutdownCh)
|
2016-12-12 06:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Collect garbage collects a single allocation on a node
|
|
|
|
func (a *AllocGarbageCollector) Collect(allocID string) error {
|
|
|
|
gcAlloc, err := a.allocRunners.Remove(allocID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to collect allocation %q: %v", allocID, err)
|
|
|
|
}
|
|
|
|
|
2016-12-12 06:40:11 +00:00
|
|
|
ar := gcAlloc.allocRunner
|
2016-12-12 06:33:12 +00:00
|
|
|
a.logger.Printf("[INFO] client: garbage collecting allocation %q", ar.Alloc().ID)
|
|
|
|
ar.Destroy()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CollectAll garbage collects all termianated allocations on a node
|
|
|
|
func (a *AllocGarbageCollector) CollectAll() error {
|
|
|
|
for {
|
|
|
|
gcAlloc := a.allocRunners.Pop()
|
|
|
|
if gcAlloc == nil {
|
|
|
|
break
|
|
|
|
}
|
2016-12-12 06:40:11 +00:00
|
|
|
ar := gcAlloc.allocRunner
|
2016-12-12 06:33:12 +00:00
|
|
|
a.logger.Printf("[INFO] client: garbage collecting alloc runner for alloc %q", ar.Alloc().ID)
|
|
|
|
ar.Destroy()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeRoomFor garbage collects enough number of allocations in the terminal
|
|
|
|
// state to make room for new allocations
|
|
|
|
func (a *AllocGarbageCollector) MakeRoomFor(allocations []*structs.Allocation) error {
|
|
|
|
totalResource := &structs.Resources{}
|
|
|
|
for _, alloc := range allocations {
|
|
|
|
if err := totalResource.Add(alloc.Resources); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-16 07:54:54 +00:00
|
|
|
// If the host has enough free space to accomodate the new allocations then
|
|
|
|
// we don't need to garbage collect terminated allocations
|
2016-12-19 18:51:57 +00:00
|
|
|
if hostStats := a.statsCollector.Stats(); hostStats != nil {
|
|
|
|
var availableForAllocations uint64
|
|
|
|
if hostStats.AllocDirStats.Available < uint64(a.reservedDiskMB*MB) {
|
|
|
|
availableForAllocations = 0
|
|
|
|
} else {
|
|
|
|
availableForAllocations = hostStats.AllocDirStats.Available - uint64(a.reservedDiskMB*MB)
|
|
|
|
}
|
|
|
|
if uint64(totalResource.DiskMB*MB) < availableForAllocations {
|
|
|
|
return nil
|
|
|
|
}
|
2016-12-16 07:54:54 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 06:33:12 +00:00
|
|
|
var diskCleared int
|
|
|
|
for {
|
2016-12-16 07:54:54 +00:00
|
|
|
// Collect host stats and see if we still need to remove older
|
|
|
|
// allocations
|
2016-12-19 18:51:57 +00:00
|
|
|
var allocDirStats *stats.DiskStats
|
2016-12-16 07:54:54 +00:00
|
|
|
if err := a.statsCollector.Collect(); err == nil {
|
2016-12-19 18:51:57 +00:00
|
|
|
if hostStats := a.statsCollector.Stats(); hostStats != nil {
|
|
|
|
allocDirStats = hostStats.AllocDirStats
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if allocDirStats != nil {
|
2016-12-20 01:53:11 +00:00
|
|
|
if allocDirStats.Available >= uint64(totalResource.DiskMB*MB) {
|
2016-12-16 07:54:54 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Falling back to a simpler model to know if we have enough disk
|
|
|
|
// space if stats collection fails
|
|
|
|
if diskCleared >= totalResource.DiskMB {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-12 06:33:12 +00:00
|
|
|
gcAlloc := a.allocRunners.Pop()
|
|
|
|
if gcAlloc == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2016-12-12 06:40:11 +00:00
|
|
|
ar := gcAlloc.allocRunner
|
2016-12-12 06:33:12 +00:00
|
|
|
alloc := ar.Alloc()
|
2016-12-12 06:58:28 +00:00
|
|
|
a.logger.Printf("[INFO] client: garbage collecting allocation %v", alloc.ID)
|
2016-12-16 07:54:54 +00:00
|
|
|
|
|
|
|
// Destroy the alloc runner and wait until it exits
|
2016-12-12 06:33:12 +00:00
|
|
|
ar.Destroy()
|
2016-12-16 07:54:54 +00:00
|
|
|
select {
|
|
|
|
case <-ar.WaitCh():
|
|
|
|
case <-a.shutdownCh:
|
2016-12-12 06:33:12 +00:00
|
|
|
}
|
2016-12-16 07:54:54 +00:00
|
|
|
|
|
|
|
// Call stats collect again
|
|
|
|
diskCleared += alloc.Resources.DiskMB
|
2016-12-12 06:33:12 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarkForCollection starts tracking an allocation for Garbage Collection
|
|
|
|
func (a *AllocGarbageCollector) MarkForCollection(ar *AllocRunner) error {
|
|
|
|
if ar == nil {
|
|
|
|
return fmt.Errorf("nil allocation runner inserted for garbage collection")
|
|
|
|
}
|
|
|
|
if ar.Alloc() == nil {
|
|
|
|
a.logger.Printf("[INFO] client: alloc is nil, so garbage collecting")
|
|
|
|
ar.Destroy()
|
|
|
|
}
|
|
|
|
|
|
|
|
a.logger.Printf("[INFO] client: marking allocation %v for GC", ar.Alloc().ID)
|
|
|
|
return a.allocRunners.Push(ar)
|
|
|
|
}
|
2016-12-20 19:14:22 +00:00
|
|
|
|
|
|
|
// Remove removes an alloc runner without garbage collecting it
|
|
|
|
func (a *AllocGarbageCollector) Remove(ar *AllocRunner) {
|
|
|
|
if ar == nil || ar.Alloc() == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
alloc := ar.Alloc()
|
|
|
|
if _, err := a.allocRunners.Remove(alloc.ID); err == nil {
|
|
|
|
a.logger.Printf("[INFO] client: removed alloc runner %v from garbage collector", alloc.ID)
|
|
|
|
}
|
|
|
|
}
|