@@ -11,7 +11,9 @@ import (
11
11
"github.com/strabox/caravela/node/common/resources"
12
12
"github.com/strabox/caravela/node/external"
13
13
"github.com/strabox/caravela/util"
14
+ "github.com/strabox/caravela/util/debug"
14
15
"sync"
16
+ "unsafe"
15
17
)
16
18
17
19
// Containers manager responsible for interacting with the Docker daemon and managing all the interaction with the
@@ -25,7 +27,7 @@ type Manager struct {
25
27
supplier supplierLocal // Local Supplier component.
26
28
27
29
quitChan chan bool // Channel to alert that the node is stopping.
28
- containersMutex * sync.Mutex // Mutex to control access to containers map.
30
+ containersMutex sync.Mutex // Mutex to control access to containers map.
29
31
containersMap map [string ]map [string ]* localContainer // Collection of deployed containers (buyerIP->(containerID->Container)).
30
32
}
31
33
@@ -38,21 +40,21 @@ func NewManager(config *configuration.Configuration, dockerClient external.Docke
38
40
supplier : supplier ,
39
41
40
42
quitChan : make (chan bool ),
41
- containersMutex : & sync.Mutex {},
43
+ containersMutex : sync.Mutex {},
42
44
containersMap : make (map [string ]map [string ]* localContainer ),
43
45
}
44
46
}
45
47
46
48
// receiveDockerEvents
47
- func (man * Manager ) receiveDockerEvents (eventsChan <- chan * events.Event ) {
49
+ func (m * Manager ) receiveDockerEvents (eventsChan <- chan * events.Event ) {
48
50
go func () {
49
51
for {
50
52
select {
51
53
case event := <- eventsChan :
52
54
if event .Type == events .ContainerDied {
53
- man .StopContainer (event .Value )
55
+ m .StopContainer (event .Value )
54
56
}
55
- case quit := <- man .quitChan : // Stopping the containers management
57
+ case quit := <- m .quitChan : // Stopping the containers management
56
58
if quit {
57
59
log .Infof (util .LogTag ("CONTAINER" ) + "STOPPED" )
58
60
return
@@ -63,18 +65,18 @@ func (man *Manager) receiveDockerEvents(eventsChan <-chan *events.Event) {
63
65
}
64
66
65
67
// Verify if the offer is valid and alert the supplier and after that start the container in the Docker engine.
66
- func (man * Manager ) StartContainer (fromBuyer * types.Node , offer * types.Offer , containersConfigs []types.ContainerConfig ,
68
+ func (m * Manager ) StartContainer (fromBuyer * types.Node , offer * types.Offer , containersConfigs []types.ContainerConfig ,
67
69
totalResourcesNecessary resources.Resources ) ([]types.ContainerStatus , error ) {
68
- if ! man .IsWorking () {
70
+ if ! m .IsWorking () {
69
71
panic (fmt .Errorf ("can't start container, container manager not working" ))
70
72
}
71
73
72
- man .containersMutex .Lock ()
73
- defer man .containersMutex .Unlock ()
74
+ m .containersMutex .Lock ()
75
+ defer m .containersMutex .Unlock ()
74
76
75
77
// =================== Obtain the resources from the offer ==================
76
78
77
- obtained := man .supplier .ObtainResources (offer .ID , totalResourcesNecessary , len (containersConfigs ))
79
+ obtained := m .supplier .ObtainResources (offer .ID , totalResourcesNecessary , len (containersConfigs ))
78
80
if ! obtained {
79
81
log .Debugf (util .LogTag ("CONTAINER" )+ "Container NOT RUNNING, invalid offer: %d" , offer .ID )
80
82
return nil , fmt .Errorf ("can't start container, invalid offer: %d" , offer .ID )
@@ -85,11 +87,11 @@ func (man *Manager) StartContainer(fromBuyer *types.Node, offer *types.Offer, co
85
87
deployedContStatus := make ([]types.ContainerStatus , 0 )
86
88
87
89
for _ , contConfig := range containersConfigs {
88
- containerStatus , err := man .dockerClient .RunContainer (contConfig )
90
+ containerStatus , err := m .dockerClient .RunContainer (contConfig )
89
91
if err != nil { // If can't deploy a container remove all the other containers.
90
- man .supplier .ReturnResources (totalResourcesNecessary , len (containersConfigs ))
92
+ m .supplier .ReturnResources (totalResourcesNecessary , len (containersConfigs ))
91
93
for _ , contStatus := range deployedContStatus {
92
- man .StopContainer (contStatus .ContainerID )
94
+ m .StopContainer (contStatus .ContainerID )
93
95
}
94
96
return nil , err
95
97
}
@@ -104,15 +106,15 @@ func (man *Manager) StartContainer(fromBuyer *types.Node, offer *types.Offer, co
104
106
newContainer := newContainer (contConfig .Name , contConfig .ImageKey , contConfig .Args , contConfig .PortMappings ,
105
107
* contResources , containerID , fromBuyer .IP )
106
108
107
- if _ , ok := man .containersMap [fromBuyer .IP ]; ! ok {
109
+ if _ , ok := m .containersMap [fromBuyer .IP ]; ! ok {
108
110
userContainersMap := make (map [string ]* localContainer )
109
111
userContainersMap [containerID ] = newContainer
110
- man .containersMap [fromBuyer .IP ] = userContainersMap
112
+ m .containersMap [fromBuyer .IP ] = userContainersMap
111
113
} else {
112
- man .containersMap [fromBuyer.IP ][containerID ] = newContainer
114
+ m .containersMap [fromBuyer.IP ][containerID ] = newContainer
113
115
}
114
116
115
- deployedContStatus [i ].SupplierIP = man .config .HostIP () // Set the container's supplier's IP!
117
+ deployedContStatus [i ].SupplierIP = m .config .HostIP () // Set the container's supplier's IP!
116
118
117
119
log .Debugf (util .LogTag ("CONTAINER" )+ "[%d] Container %s RUNNING, Img: %s, Args: %v, Res: <%d,%d>" ,
118
120
i , containerID [0 :12 ], contConfig .ImageKey , contConfig .Args , contResources .CPUs (),
@@ -123,21 +125,21 @@ func (man *Manager) StartContainer(fromBuyer *types.Node, offer *types.Offer, co
123
125
}
124
126
125
127
// StopContainer stop a local container in the Docker engine and remove it.
126
- func (man * Manager ) StopContainer (containerIDToStop string ) error {
127
- man .containersMutex .Lock ()
128
- defer man .containersMutex .Unlock ()
128
+ func (m * Manager ) StopContainer (containerIDToStop string ) error {
129
+ m .containersMutex .Lock ()
130
+ defer m .containersMutex .Unlock ()
129
131
130
- for buyerIP , containersMap := range man .containersMap {
132
+ for buyerIP , containersMap := range m .containersMap {
131
133
for containerID , container := range containersMap {
132
134
if containerID == containerIDToStop {
133
- man .dockerClient .RemoveContainer (containerIDToStop )
134
- man .supplier .ReturnResources (container .Resources (), 1 )
135
+ m .dockerClient .RemoveContainer (containerIDToStop )
136
+ m .supplier .ReturnResources (container .Resources (), 1 )
135
137
delete (containersMap , containerID )
136
138
return nil
137
139
}
138
140
}
139
141
if containersMap == nil || len (containersMap ) == 0 {
140
- delete (man .containersMap , buyerIP )
142
+ delete (m .containersMap , buyerIP )
141
143
}
142
144
}
143
145
@@ -148,32 +150,67 @@ func (man *Manager) StopContainer(containerIDToStop string) error {
148
150
// = SubComponent Interface =
149
151
// ===============================================================================
150
152
151
- func (man * Manager ) Start () {
152
- man .Started (man .config .Simulation (), func () {
153
- if ! man .config .Simulation () {
154
- eventsChan := man .dockerClient .Start ()
155
- man .receiveDockerEvents (eventsChan )
153
+ func (m * Manager ) Start () {
154
+ m .Started (m .config .Simulation (), func () {
155
+ if ! m .config .Simulation () {
156
+ eventsChan := m .dockerClient .Start ()
157
+ m .receiveDockerEvents (eventsChan )
156
158
}
157
159
})
158
160
}
159
161
160
- func (man * Manager ) Stop () {
161
- man .Stopped (func () {
162
- man .containersMutex .Lock ()
163
- defer man .containersMutex .Unlock ()
162
+ func (m * Manager ) Stop () {
163
+ m .Stopped (func () {
164
+ m .containersMutex .Lock ()
165
+ defer m .containersMutex .Unlock ()
164
166
165
167
// Stop and remove all the running containers from the docker engine
166
- for _ , containers := range man .containersMap {
168
+ for _ , containers := range m .containersMap {
167
169
for containerID := range containers {
168
- man .dockerClient .RemoveContainer (containerID )
170
+ m .dockerClient .RemoveContainer (containerID )
169
171
log .Debugf (util .LogTag ("CONTAINER" )+ "Container, %s STOPPED and REMOVED" , containerID )
170
172
}
171
173
}
172
174
173
- man .quitChan <- true
175
+ m .quitChan <- true
174
176
})
175
177
}
176
178
177
- func (man * Manager ) IsWorking () bool {
178
- return man .Working ()
179
+ func (m * Manager ) IsWorking () bool {
180
+ return m .Working ()
181
+ }
182
+
183
+ // ===============================================================================
184
+ // = Debug Methods =
185
+ // ===============================================================================
186
+
187
+ func (m * Manager ) DebugSizeBytes () int {
188
+ localContainerSize := func (container * localContainer ) uintptr {
189
+ contSizeBytes := unsafe .Sizeof (* container )
190
+ contSizeBytes += debug .DebugSizeofString (container .buyerIP )
191
+ // common.Container
192
+ contSizeBytes += unsafe .Sizeof (* container .Container )
193
+ contSizeBytes += debug .DebugSizeofString (container .Name ())
194
+ contSizeBytes += debug .DebugSizeofString (container .ImageKey ())
195
+ contSizeBytes += debug .DebugSizeofString (container .ID ())
196
+ contSizeBytes += debug .DebugSizeofStringSlice (container .Args ())
197
+ contSizeBytes += debug .DebugSizeofPortMappings (container .PortMappings ())
198
+ return contSizeBytes
199
+ }
200
+
201
+ contManagerSizeBytes := unsafe .Sizeof (* m )
202
+ for k , v := range m .containersMap {
203
+ contManagerSizeBytes += unsafe .Sizeof (k )
204
+ contManagerSizeBytes += debug .DebugSizeofString (k )
205
+ contManagerSizeBytes += unsafe .Sizeof (v )
206
+ if v != nil {
207
+ for k2 , v2 := range v {
208
+ contManagerSizeBytes += unsafe .Sizeof (k2 )
209
+ contManagerSizeBytes += debug .DebugSizeofString (k2 )
210
+ contManagerSizeBytes += unsafe .Sizeof (v2 )
211
+ contManagerSizeBytes += localContainerSize (v2 )
212
+ }
213
+ }
214
+ }
215
+ return int (contManagerSizeBytes )
179
216
}
0 commit comments