-
Notifications
You must be signed in to change notification settings - Fork 42
/
nodemap.go
145 lines (118 loc) · 2.96 KB
/
nodemap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package containerstore
import (
"sync"
"time"
"code.cloudfoundry.org/executor"
"code.cloudfoundry.org/lager/v3"
)
type nodeMap struct {
nodes map[string]*storeNode
lock *sync.RWMutex
remainingResources *executor.ExecutorResources
}
func newNodeMap(totalCapacity *executor.ExecutorResources) *nodeMap {
capacity := totalCapacity.Copy()
return &nodeMap{
nodes: make(map[string]*storeNode),
lock: &sync.RWMutex{},
remainingResources: &capacity,
}
}
func (n *nodeMap) Contains(guid string) bool {
n.lock.RLock()
defer n.lock.RUnlock()
_, ok := n.nodes[guid]
return ok
}
func (n *nodeMap) RemainingResources() executor.ExecutorResources {
n.lock.RLock()
defer n.lock.RUnlock()
return n.remainingResources.Copy()
}
func (n *nodeMap) Add(node *storeNode) error {
n.lock.Lock()
defer n.lock.Unlock()
info := node.Info()
if _, ok := n.nodes[info.Guid]; ok {
return executor.ErrContainerGuidNotAvailable
}
ok := n.remainingResources.Subtract(&info.Resource)
if !ok {
return executor.ErrInsufficientResourcesAvailable
}
n.nodes[info.Guid] = node
return nil
}
func (n *nodeMap) Remove(guid string) {
n.lock.Lock()
defer n.lock.Unlock()
node, ok := n.nodes[guid]
if !ok {
return
}
n.remove(node)
}
func (n *nodeMap) remove(node *storeNode) {
info := node.Info()
n.remainingResources.Add(&info.Resource)
delete(n.nodes, info.Guid)
}
func (n *nodeMap) Get(guid string) (*storeNode, error) {
n.lock.RLock()
defer n.lock.RUnlock()
node, ok := n.nodes[guid]
if !ok {
return nil, executor.ErrContainerNotFound
}
return node, nil
}
func (n *nodeMap) List() []*storeNode {
n.lock.RLock()
defer n.lock.RUnlock()
list := make([]*storeNode, 0, len(n.nodes))
for _, node := range n.nodes {
list = append(list, node)
}
return list
}
func (n *nodeMap) CompleteExpired(logger lager.Logger, traceID string, now time.Time) {
n.lock.Lock()
logger.Debug("lock-acquired")
defer n.lock.Unlock()
defer logger.Debug("lock-released")
for i := range n.nodes {
node := n.nodes[i]
expired := node.Expire(logger, traceID, now)
if expired {
logger.Info("container-expired", lager.Data{"guid": node.Info().Guid})
}
}
}
func (n *nodeMap) CompleteMissing(logger lager.Logger, traceID string, snapshotGuids map[string]struct{}, existingHandles map[string]struct{}) {
n.lock.Lock()
logger.Debug("lock-acquired")
defer n.lock.Unlock()
defer logger.Debug("lock-released")
for guid := range snapshotGuids {
if _, exist := existingHandles[guid]; !exist {
node, ok := n.nodes[guid]
if ok {
reaped := node.Reap(logger, traceID)
if reaped {
logger.Info("reaped-missing-container", lager.Data{"guid": guid})
}
}
}
}
}
func (n *nodeMap) containerGuids(logger lager.Logger) map[string]struct{} {
n.lock.Lock()
logger.Debug("lock-acquired")
defer n.lock.Unlock()
defer logger.Debug("lock-released")
guids := make(map[string]struct{})
for guid := range n.nodes {
guids[guid] = struct{}{}
}
return guids
}