Skip to content

Commit

Permalink
Added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
diptanu committed Dec 20, 2016
1 parent 6143d8d commit 7ebe4a6
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 16 deletions.
10 changes: 2 additions & 8 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,19 +445,13 @@ func (c *Client) Stats() map[string]map[string]string {

// CollectAllocation garbage collects a single allocation
func (c *Client) CollectAllocation(allocID string) error {
if err := c.garbageCollector.Collect(allocID); err != nil {
return err
}
return nil
return c.garbageCollector.Collect(allocID)
}

// CollectAllAllocs garbage collects all allocations on a node in the terminal
// state
func (c *Client) CollectAllAllocs() error {
if err := c.garbageCollector.CollectAll(); err != nil {
return err
}
return nil
return c.garbageCollector.CollectAll()
}

// Node returns the locally registered node
Expand Down
8 changes: 5 additions & 3 deletions client/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const (
MB = 1024 * 1024
)

// GCAlloc wraps an allocation runner and an index enabling it to be used within
// a PQ
type GCAlloc struct {
timeStamp time.Time
allocRunner *AllocRunner
Expand Down Expand Up @@ -162,7 +164,7 @@ func (a *AllocGarbageCollector) run() {
select {
case <-ticker.C:
if err := a.keepUsageBelowThreshold(); err != nil {
a.logger.Printf("[ERR] client: error GCing allocation: %v", err)
a.logger.Printf("[ERR] client: error garbage collecting allocation: %v", err)
}
case <-a.shutdownCh:
ticker.Stop()
Expand Down Expand Up @@ -214,7 +216,7 @@ func (a *AllocGarbageCollector) keepUsageBelowThreshold() error {
}

func (a *AllocGarbageCollector) Stop() {
a.shutdownCh <- struct{}{}
close(a.shutdownCh)
}

// Collect garbage collects a single allocation on a node
Expand Down Expand Up @@ -281,7 +283,7 @@ func (a *AllocGarbageCollector) MakeRoomFor(allocations []*structs.Allocation) e
}

if allocDirStats != nil {
if allocDirStats.Available >= uint64(totalResource.DiskMB*1024*1024) {
if allocDirStats.Available >= uint64(totalResource.DiskMB*MB) {
break
}
} else {
Expand Down
22 changes: 22 additions & 0 deletions client/gc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func TestAllocGarbageCollector_MakeRoomForAllocations_EnoughSpace(t *testing.T)
statsCollector.inodePercents = []float64{0}

alloc := mock.Alloc()
alloc.Resources.DiskMB = 150
if err := gc.MakeRoomFor([]*structs.Allocation{alloc}); err != nil {
t.Fatalf("err: %v", err)
}
Expand Down Expand Up @@ -197,6 +198,7 @@ func TestAllocGarbageCollector_MakeRoomForAllocations_GC_Partial(t *testing.T) {
statsCollector.inodePercents = []float64{0, 0, 0}

alloc := mock.Alloc()
alloc.Resources.DiskMB = 150
if err := gc.MakeRoomFor([]*structs.Allocation{alloc}); err != nil {
t.Fatalf("err: %v", err)
}
Expand Down Expand Up @@ -233,6 +235,7 @@ func TestAllocGarbageCollector_MakeRoomForAllocations_GC_All(t *testing.T) {
statsCollector.inodePercents = []float64{0, 0, 0}

alloc := mock.Alloc()
alloc.Resources.DiskMB = 150
if err := gc.MakeRoomFor([]*structs.Allocation{alloc}); err != nil {
t.Fatalf("err: %v", err)
}
Expand Down Expand Up @@ -260,6 +263,7 @@ func TestAllocGarbageCollector_MakeRoomForAllocations_GC_Fallback(t *testing.T)
}

alloc := mock.Alloc()
alloc.Resources.DiskMB = 150
if err := gc.MakeRoomFor([]*structs.Allocation{alloc}); err != nil {
t.Fatalf("err: %v", err)
}
Expand Down Expand Up @@ -297,6 +301,14 @@ func TestAllocGarbageCollector_UsageBelowThreshold(t *testing.T) {
if err := gc.keepUsageBelowThreshold(); err != nil {
t.Fatalf("err: %v", err)
}

// We shouldn't GC any of the allocs since the used percent values are below
// threshold
for i := 0; i < 2; i++ {
if gcAlloc := gc.allocRunners.Pop(); gcAlloc == nil {
t.Fatalf("err: %v", gcAlloc)
}
}
}

func TestAllocGarbageCollector_UsedPercentThreshold(t *testing.T) {
Expand All @@ -322,4 +334,14 @@ func TestAllocGarbageCollector_UsedPercentThreshold(t *testing.T) {
if err := gc.keepUsageBelowThreshold(); err != nil {
t.Fatalf("err: %v", err)
}

// We should be GC-ing only one of the alloc runners since the second time
// used percent returns a number below threshold.
if gcAlloc := gc.allocRunners.Pop(); gcAlloc == nil {
t.Fatalf("err: %v", gcAlloc)
}

if gcAlloc := gc.allocRunners.Pop(); gcAlloc != nil {
t.Fatalf("gcAlloc: %v", gcAlloc)
}
}
7 changes: 6 additions & 1 deletion client/stats/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ type DiskStats struct {
InodesUsedPercent float64
}

// NodeStatsCollector is an interface which is used for the puproses of mocking
// the HostStatsCollector in the tests
type NodeStatsCollector interface {
Collect() error
Stats() *HostStats
Expand All @@ -70,7 +72,9 @@ type HostStatsCollector struct {
allocDir string
}

// NewHostStatsCollector returns a HostStatsCollector
// NewHostStatsCollector returns a HostStatsCollector. The allocDir is passed in
// so that we can present the disk related statistics for the mountpoint where
// the allocation directory lives
func NewHostStatsCollector(logger *log.Logger, allocDir string) *HostStatsCollector {
numCores := runtime.NumCPU()
statsCalculator := make(map[string]*HostCpuStatsCalculator)
Expand Down Expand Up @@ -138,6 +142,7 @@ func (h *HostStatsCollector) Collect() error {
}
hs.DiskStats = diskStats

// Getting the disk stats for the allocation directory
usage, err := disk.Usage(h.allocDir)
if err != nil {
return err
Expand Down
6 changes: 2 additions & 4 deletions command/agent/alloc_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,11 @@ func (s *HTTPServer) ClientGCRequest(resp http.ResponseWriter, req *http.Request
if s.agent.client == nil {
return nil, clientNotRunning
}
err := s.agent.Client().CollectAllAllocs()
return nil, err
return nil, s.agent.Client().CollectAllAllocs()
}

func (s *HTTPServer) allocGC(allocID string, resp http.ResponseWriter, req *http.Request) (interface{}, error) {
err := s.agent.Client().CollectAllocation(allocID)
return nil, err
return nil, s.agent.Client().CollectAllocation(allocID)
}

func (s *HTTPServer) allocSnapshot(allocID string, resp http.ResponseWriter, req *http.Request) (interface{}, error) {
Expand Down

0 comments on commit 7ebe4a6

Please sign in to comment.