2016-12-12 06:33:12 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/heap"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2018-08-29 22:05:03 +00:00
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
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 (
|
2016-12-19 18:51:57 +00:00
|
|
|
// MB is a constant which converts values in bytes to MB
|
|
|
|
MB = 1024 * 1024
|
2017-03-11 00:27:00 +00:00
|
|
|
)
|
2016-12-12 06:33:12 +00:00
|
|
|
|
2017-01-31 23:32:20 +00:00
|
|
|
// GCConfig allows changing the behaviour of the garbage collector
|
|
|
|
type GCConfig struct {
|
2017-05-11 00:39:45 +00:00
|
|
|
// MaxAllocs is the maximum number of allocations to track before a GC
|
|
|
|
// is triggered.
|
|
|
|
MaxAllocs int
|
2017-01-31 23:32:20 +00:00
|
|
|
DiskUsageThreshold float64
|
|
|
|
InodeUsageThreshold float64
|
|
|
|
Interval time.Duration
|
|
|
|
ReservedDiskMB int
|
2017-03-11 00:27:00 +00:00
|
|
|
ParallelDestroys int
|
2017-01-31 23:32:20 +00:00
|
|
|
}
|
|
|
|
|
2017-10-19 00:06:46 +00:00
|
|
|
// AllocCounter is used by AllocGarbageCollector to discover how many un-GC'd
|
|
|
|
// allocations a client has and is generally fulfilled by the Client.
|
2017-05-11 00:39:45 +00:00
|
|
|
type AllocCounter interface {
|
|
|
|
NumAllocs() int
|
|
|
|
}
|
|
|
|
|
2016-12-12 06:33:12 +00:00
|
|
|
// AllocGarbageCollector garbage collects terminated allocations on a node
|
|
|
|
type AllocGarbageCollector struct {
|
2017-10-26 21:03:51 +00:00
|
|
|
config *GCConfig
|
|
|
|
|
|
|
|
// allocRunners marked for GC
|
|
|
|
allocRunners *IndexedGCAllocPQ
|
|
|
|
|
|
|
|
// statsCollector for node based thresholds (eg disk)
|
2016-12-16 07:54:54 +00:00
|
|
|
statsCollector stats.NodeStatsCollector
|
2017-10-26 21:03:51 +00:00
|
|
|
|
|
|
|
// allocCounter return the number of un-GC'd allocs on this node
|
|
|
|
allocCounter AllocCounter
|
|
|
|
|
|
|
|
// destroyCh is a semaphore for rate limiting concurrent garbage
|
|
|
|
// collections
|
|
|
|
destroyCh chan struct{}
|
|
|
|
|
|
|
|
// shutdownCh is closed when the GC's run method should exit
|
|
|
|
shutdownCh chan struct{}
|
|
|
|
|
|
|
|
// triggerCh is ticked by the Trigger method to cause a GC
|
|
|
|
triggerCh chan struct{}
|
|
|
|
|
2018-08-29 22:05:03 +00:00
|
|
|
logger hclog.Logger
|
2016-12-12 06:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAllocGarbageCollector returns a garbage collector for terminated
|
2017-05-11 00:39:45 +00:00
|
|
|
// allocations on a node. Must call Run() in a goroutine enable periodic
|
|
|
|
// garbage collection.
|
2018-08-29 22:05:03 +00:00
|
|
|
func NewAllocGarbageCollector(logger hclog.Logger, statsCollector stats.NodeStatsCollector, ac AllocCounter, config *GCConfig) *AllocGarbageCollector {
|
|
|
|
logger = logger.Named("gc")
|
2017-03-11 00:27:00 +00:00
|
|
|
// Require at least 1 to make progress
|
|
|
|
if config.ParallelDestroys <= 0 {
|
2018-08-29 22:05:03 +00:00
|
|
|
logger.Warn("garbage collector defaulting parallelism to 1 due to invalid input value", "gc_parallel_destroys", config.ParallelDestroys)
|
2017-03-11 00:27:00 +00:00
|
|
|
config.ParallelDestroys = 1
|
|
|
|
}
|
|
|
|
|
2016-12-16 07:54:54 +00:00
|
|
|
gc := &AllocGarbageCollector{
|
2016-12-12 07:08:13 +00:00
|
|
|
allocRunners: NewIndexedGCAllocPQ(),
|
|
|
|
statsCollector: statsCollector,
|
2017-05-11 00:39:45 +00:00
|
|
|
allocCounter: ac,
|
2017-01-31 23:32:20 +00:00
|
|
|
config: config,
|
2016-12-12 07:08:13 +00:00
|
|
|
logger: logger,
|
2017-03-11 00:27:00 +00:00
|
|
|
destroyCh: make(chan struct{}, config.ParallelDestroys),
|
2016-12-16 07:54:54 +00:00
|
|
|
shutdownCh: make(chan struct{}),
|
2017-10-26 21:03:51 +00:00
|
|
|
triggerCh: make(chan struct{}, 1),
|
2016-12-12 06:33:12 +00:00
|
|
|
}
|
2016-12-16 07:54:54 +00:00
|
|
|
|
|
|
|
return gc
|
|
|
|
}
|
|
|
|
|
2017-05-11 00:39:45 +00:00
|
|
|
// Run the periodic garbage collector.
|
|
|
|
func (a *AllocGarbageCollector) Run() {
|
2017-01-31 23:32:20 +00:00
|
|
|
ticker := time.NewTicker(a.config.Interval)
|
2016-12-16 07:54:54 +00:00
|
|
|
for {
|
|
|
|
select {
|
2017-10-26 21:03:51 +00:00
|
|
|
case <-a.triggerCh:
|
2016-12-16 07:54:54 +00:00
|
|
|
case <-ticker.C:
|
|
|
|
case <-a.shutdownCh:
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
}
|
2017-10-26 21:03:51 +00:00
|
|
|
|
|
|
|
if err := a.keepUsageBelowThreshold(); err != nil {
|
2018-08-29 22:05:03 +00:00
|
|
|
a.logger.Error("error garbage collecting allocations", "error", err)
|
2017-10-26 21:03:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force the garbage collector to run.
|
|
|
|
func (a *AllocGarbageCollector) Trigger() {
|
|
|
|
select {
|
|
|
|
case a.triggerCh <- struct{}{}:
|
|
|
|
default:
|
|
|
|
// already triggered
|
2016-12-16 07:54:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// keepUsageBelowThreshold collects disk usage information and garbage collects
|
|
|
|
// allocations to make disk space available.
|
|
|
|
func (a *AllocGarbageCollector) keepUsageBelowThreshold() error {
|
|
|
|
for {
|
2017-03-11 00:27:00 +00:00
|
|
|
select {
|
|
|
|
case <-a.shutdownCh:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2016-12-16 07:54:54 +00:00
|
|
|
// Check if we have enough free space
|
2017-10-19 00:06:46 +00:00
|
|
|
if err := a.statsCollector.Collect(); err != nil {
|
2016-12-16 07:54:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if we are below thresholds for used disk space and inode usage
|
2017-10-19 00:06:46 +00:00
|
|
|
diskStats := a.statsCollector.Stats().AllocDirStats
|
2017-05-11 00:39:45 +00:00
|
|
|
reason := ""
|
2018-08-29 22:05:03 +00:00
|
|
|
logf := a.logger.Warn
|
2017-05-11 00:39:45 +00:00
|
|
|
|
2017-10-19 00:06:46 +00:00
|
|
|
liveAllocs := a.allocCounter.NumAllocs()
|
|
|
|
|
2017-05-11 00:39:45 +00:00
|
|
|
switch {
|
|
|
|
case diskStats.UsedPercent > a.config.DiskUsageThreshold:
|
|
|
|
reason = fmt.Sprintf("disk usage of %.0f is over gc threshold of %.0f",
|
|
|
|
diskStats.UsedPercent, a.config.DiskUsageThreshold)
|
|
|
|
case diskStats.InodesUsedPercent > a.config.InodeUsageThreshold:
|
|
|
|
reason = fmt.Sprintf("inode usage of %.0f is over gc threshold of %.0f",
|
|
|
|
diskStats.InodesUsedPercent, a.config.InodeUsageThreshold)
|
2017-10-19 00:06:46 +00:00
|
|
|
case liveAllocs > a.config.MaxAllocs:
|
2017-11-02 17:55:10 +00:00
|
|
|
// if we're unable to gc, don't WARN until at least 2x over limit
|
|
|
|
if liveAllocs < (a.config.MaxAllocs * 2) {
|
2018-08-29 22:05:03 +00:00
|
|
|
logf = a.logger.Info
|
2017-11-02 17:55:10 +00:00
|
|
|
}
|
2017-10-19 00:06:46 +00:00
|
|
|
reason = fmt.Sprintf("number of allocations (%d) is over the limit (%d)", liveAllocs, a.config.MaxAllocs)
|
2017-05-11 00:39:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if reason == "" {
|
2017-10-26 21:03:51 +00:00
|
|
|
// No reason to gc, exit
|
2016-12-16 07:54:54 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect an allocation
|
|
|
|
gcAlloc := a.allocRunners.Pop()
|
|
|
|
if gcAlloc == nil {
|
2018-08-29 22:05:03 +00:00
|
|
|
logf("garbage collection skipped because no terminal allocations", "reason", reason)
|
2016-12-16 07:54:54 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy the alloc runner and wait until it exits
|
2018-06-29 00:01:05 +00:00
|
|
|
a.destroyAllocRunner(gcAlloc.allocID, gcAlloc.allocRunner, reason)
|
2017-03-11 00:27:00 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// destroyAllocRunner is used to destroy an allocation runner. It will acquire a
|
|
|
|
// lock to restrict parallelism and then destroy the alloc runner, returning
|
|
|
|
// once the allocation has been destroyed.
|
2018-06-29 00:01:05 +00:00
|
|
|
func (a *AllocGarbageCollector) destroyAllocRunner(allocID string, ar AllocRunner, reason string) {
|
2018-08-29 22:05:03 +00:00
|
|
|
a.logger.Info("garbage collecting allocation", "alloc_id", allocID, "reason", reason)
|
2017-05-11 00:39:45 +00:00
|
|
|
|
2017-03-11 00:27:00 +00:00
|
|
|
// Acquire the destroy lock
|
|
|
|
select {
|
|
|
|
case <-a.shutdownCh:
|
|
|
|
return
|
2017-03-14 17:45:15 +00:00
|
|
|
case a.destroyCh <- struct{}{}:
|
2017-03-11 00:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ar.Destroy()
|
|
|
|
|
|
|
|
select {
|
2018-12-14 15:05:16 +00:00
|
|
|
case <-ar.DestroyCh():
|
2017-03-11 00:27:00 +00:00
|
|
|
case <-a.shutdownCh:
|
|
|
|
}
|
|
|
|
|
2018-08-29 22:05:03 +00:00
|
|
|
a.logger.Debug("garbage collected %s", "alloc_id", allocID)
|
2017-03-11 00:27:00 +00:00
|
|
|
|
2017-03-14 17:45:15 +00:00
|
|
|
// Release the lock
|
|
|
|
<-a.destroyCh
|
2016-12-16 07:54:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AllocGarbageCollector) Stop() {
|
2016-12-20 01:53:11 +00:00
|
|
|
close(a.shutdownCh)
|
2016-12-12 06:33:12 +00:00
|
|
|
}
|
|
|
|
|
2017-10-28 00:00:11 +00:00
|
|
|
// Collect garbage collects a single allocation on a node. Returns true if
|
|
|
|
// alloc was found and garbage collected; otherwise false.
|
|
|
|
func (a *AllocGarbageCollector) Collect(allocID string) bool {
|
2018-08-29 22:05:03 +00:00
|
|
|
gcAlloc := a.allocRunners.Remove(allocID)
|
|
|
|
if gcAlloc == nil {
|
|
|
|
a.logger.Debug("alloc was already garbage collected", "alloc_id", allocID)
|
|
|
|
return false
|
2016-12-12 06:33:12 +00:00
|
|
|
}
|
2017-10-19 00:06:46 +00:00
|
|
|
|
2018-08-29 22:05:03 +00:00
|
|
|
a.destroyAllocRunner(allocID, gcAlloc.allocRunner, "forced collection")
|
|
|
|
return true
|
2016-12-12 06:33:12 +00:00
|
|
|
}
|
|
|
|
|
2018-03-11 19:01:49 +00:00
|
|
|
// CollectAll garbage collects all terminated allocations on a node
|
2017-10-19 00:06:46 +00:00
|
|
|
func (a *AllocGarbageCollector) CollectAll() {
|
2016-12-12 06:33:12 +00:00
|
|
|
for {
|
2017-03-11 00:27:00 +00:00
|
|
|
select {
|
|
|
|
case <-a.shutdownCh:
|
2017-10-19 00:06:46 +00:00
|
|
|
return
|
2017-03-11 00:27:00 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2016-12-12 06:33:12 +00:00
|
|
|
gcAlloc := a.allocRunners.Pop()
|
|
|
|
if gcAlloc == nil {
|
2017-10-19 00:06:46 +00:00
|
|
|
return
|
2016-12-12 06:33:12 +00:00
|
|
|
}
|
2017-03-11 00:27:00 +00:00
|
|
|
|
2018-06-29 00:01:05 +00:00
|
|
|
go a.destroyAllocRunner(gcAlloc.allocID, gcAlloc.allocRunner, "forced full node collection")
|
2016-12-12 06:33:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2017-10-26 21:03:51 +00:00
|
|
|
if len(allocations) == 0 {
|
|
|
|
// Nothing to make room for!
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-11 00:39:45 +00:00
|
|
|
// GC allocs until below the max limit + the new allocations
|
|
|
|
max := a.config.MaxAllocs - len(allocations)
|
2017-10-19 00:06:46 +00:00
|
|
|
for a.allocCounter.NumAllocs() > max {
|
2017-05-11 00:39:45 +00:00
|
|
|
select {
|
|
|
|
case <-a.shutdownCh:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
gcAlloc := a.allocRunners.Pop()
|
|
|
|
if gcAlloc == nil {
|
|
|
|
// It's fine if we can't lower below the limit here as
|
|
|
|
// we'll keep trying to drop below the limit with each
|
|
|
|
// periodic gc
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy the alloc runner and wait until it exits
|
2018-06-29 00:01:05 +00:00
|
|
|
a.destroyAllocRunner(gcAlloc.allocID, gcAlloc.allocRunner, fmt.Sprintf("new allocations and over max (%d)", a.config.MaxAllocs))
|
2017-05-11 00:39:45 +00:00
|
|
|
}
|
2017-10-19 00:06:46 +00:00
|
|
|
|
2018-10-03 16:47:18 +00:00
|
|
|
totalResource := &structs.AllocatedSharedResources{}
|
2016-12-12 06:33:12 +00:00
|
|
|
for _, alloc := range allocations {
|
2018-10-03 16:47:18 +00:00
|
|
|
// COMPAT(0.11): Remove in 0.11
|
|
|
|
if alloc.AllocatedResources != nil {
|
|
|
|
totalResource.Add(&alloc.AllocatedResources.Shared)
|
|
|
|
} else {
|
2018-10-16 22:34:32 +00:00
|
|
|
totalResource.DiskMB += int64(alloc.Resources.DiskMB)
|
2016-12-12 06:33:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-07 21:13:05 +00:00
|
|
|
// If the host has enough free space to accommodate the new allocations then
|
2016-12-16 07:54:54 +00:00
|
|
|
// 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
|
2017-01-31 23:32:20 +00:00
|
|
|
if hostStats.AllocDirStats.Available < uint64(a.config.ReservedDiskMB*MB) {
|
2016-12-19 18:51:57 +00:00
|
|
|
availableForAllocations = 0
|
|
|
|
} else {
|
2017-01-31 23:32:20 +00:00
|
|
|
availableForAllocations = hostStats.AllocDirStats.Available - uint64(a.config.ReservedDiskMB*MB)
|
2016-12-19 18:51:57 +00:00
|
|
|
}
|
2018-10-16 21:21:42 +00:00
|
|
|
if uint64(totalResource.DiskMB*MB) < availableForAllocations {
|
2016-12-19 18:51:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-12-16 07:54:54 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 22:34:32 +00:00
|
|
|
var diskCleared int64
|
2016-12-12 06:33:12 +00:00
|
|
|
for {
|
2017-03-11 00:27:00 +00:00
|
|
|
select {
|
|
|
|
case <-a.shutdownCh:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-10-16 21:21:42 +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-16 07:54:54 +00:00
|
|
|
|
2018-10-03 16:47:18 +00:00
|
|
|
// COMPAT(0.11): Remove in 0.11
|
2018-10-16 22:34:32 +00:00
|
|
|
var allocDiskMB int64
|
2018-10-03 16:47:18 +00:00
|
|
|
if alloc.AllocatedResources != nil {
|
|
|
|
allocDiskMB = alloc.AllocatedResources.Shared.DiskMB
|
|
|
|
} else {
|
2018-10-16 22:34:32 +00:00
|
|
|
allocDiskMB = int64(alloc.Resources.DiskMB)
|
2018-10-03 16:47:18 +00:00
|
|
|
}
|
|
|
|
|
2016-12-16 07:54:54 +00:00
|
|
|
// Destroy the alloc runner and wait until it exits
|
2018-06-29 00:01:05 +00:00
|
|
|
a.destroyAllocRunner(gcAlloc.allocID, ar, fmt.Sprintf("freeing %d MB for new allocations", allocDiskMB))
|
2016-12-16 07:54:54 +00:00
|
|
|
|
2018-10-03 16:47:18 +00:00
|
|
|
diskCleared += allocDiskMB
|
2016-12-12 06:33:12 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarkForCollection starts tracking an allocation for Garbage Collection
|
2018-06-29 00:01:05 +00:00
|
|
|
func (a *AllocGarbageCollector) MarkForCollection(allocID string, ar AllocRunner) {
|
|
|
|
if a.allocRunners.Push(allocID, ar) {
|
2018-08-29 22:05:03 +00:00
|
|
|
a.logger.Info("marking allocation for GC", "alloc_id", allocID)
|
2016-12-20 19:14:22 +00:00
|
|
|
}
|
2017-05-11 00:39:45 +00:00
|
|
|
}
|
|
|
|
|
2017-03-11 00:27:00 +00:00
|
|
|
// GCAlloc wraps an allocation runner and an index enabling it to be used within
|
|
|
|
// a PQ
|
|
|
|
type GCAlloc struct {
|
|
|
|
timeStamp time.Time
|
2018-06-29 00:01:05 +00:00
|
|
|
allocID string
|
|
|
|
allocRunner AllocRunner
|
2017-03-11 00:27:00 +00:00
|
|
|
index int
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
pqLock sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIndexedGCAllocPQ() *IndexedGCAllocPQ {
|
|
|
|
return &IndexedGCAllocPQ{
|
|
|
|
index: make(map[string]*GCAlloc),
|
|
|
|
heap: make(GCAllocPQImpl, 0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-19 00:06:46 +00:00
|
|
|
// Push an alloc runner into the GC queue. Returns true if alloc was added,
|
|
|
|
// false if the alloc already existed.
|
2018-06-29 00:01:05 +00:00
|
|
|
func (i *IndexedGCAllocPQ) Push(allocID string, ar AllocRunner) bool {
|
2017-03-11 00:27:00 +00:00
|
|
|
i.pqLock.Lock()
|
|
|
|
defer i.pqLock.Unlock()
|
|
|
|
|
2018-06-29 00:01:05 +00:00
|
|
|
if _, ok := i.index[allocID]; ok {
|
2017-03-11 00:27:00 +00:00
|
|
|
// No work to do
|
2017-10-19 00:06:46 +00:00
|
|
|
return false
|
2017-03-11 00:27:00 +00:00
|
|
|
}
|
|
|
|
gcAlloc := &GCAlloc{
|
|
|
|
timeStamp: time.Now(),
|
2018-06-29 00:01:05 +00:00
|
|
|
allocID: allocID,
|
2017-03-11 00:27:00 +00:00
|
|
|
allocRunner: ar,
|
|
|
|
}
|
2018-06-29 00:01:05 +00:00
|
|
|
i.index[allocID] = gcAlloc
|
2017-03-11 00:27:00 +00:00
|
|
|
heap.Push(&i.heap, gcAlloc)
|
2017-10-19 00:06:46 +00:00
|
|
|
return true
|
2017-03-11 00:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IndexedGCAllocPQ) Pop() *GCAlloc {
|
|
|
|
i.pqLock.Lock()
|
|
|
|
defer i.pqLock.Unlock()
|
|
|
|
|
|
|
|
if len(i.heap) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
gcAlloc := heap.Pop(&i.heap).(*GCAlloc)
|
|
|
|
delete(i.index, gcAlloc.allocRunner.Alloc().ID)
|
|
|
|
return gcAlloc
|
|
|
|
}
|
|
|
|
|
2017-10-19 00:06:46 +00:00
|
|
|
// Remove alloc from GC. Returns nil if alloc doesn't exist.
|
|
|
|
func (i *IndexedGCAllocPQ) Remove(allocID string) *GCAlloc {
|
2017-03-11 00:27:00 +00:00
|
|
|
i.pqLock.Lock()
|
|
|
|
defer i.pqLock.Unlock()
|
|
|
|
|
|
|
|
if gcAlloc, ok := i.index[allocID]; ok {
|
|
|
|
heap.Remove(&i.heap, gcAlloc.index)
|
|
|
|
delete(i.index, allocID)
|
2017-10-19 00:06:46 +00:00
|
|
|
return gcAlloc
|
2017-03-11 00:27:00 +00:00
|
|
|
}
|
|
|
|
|
2017-10-19 00:06:46 +00:00
|
|
|
return nil
|
2017-03-11 00:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IndexedGCAllocPQ) Length() int {
|
|
|
|
i.pqLock.Lock()
|
|
|
|
defer i.pqLock.Unlock()
|
|
|
|
|
|
|
|
return len(i.heap)
|
|
|
|
}
|