@@ -63,21 +63,39 @@ func (c *Calcium) doCreateContainer(ctx context.Context, opts *types.DeployOptio
63
63
for _ , nodeInfo := range nodesInfo {
64
64
go metrics .Client .SendDeployCount (nodeInfo .Deploy )
65
65
go func (nodeInfo types.NodeInfo , index int ) {
66
- defer func () {
67
- if err := c .store .DeleteProcessing (context .Background (), opts , nodeInfo ); err != nil {
68
- log .Errorf ("[doCreateContainer] remove processing status failed %v" , err )
69
- }
70
- wg .Done ()
71
- }()
72
-
73
- messages := c .doCreateContainerOnNode (ctx , nodeInfo , opts , index )
74
- for i , m := range messages {
75
- ch <- m
76
- // decr processing count
77
- if err := c .store .UpdateProcessing (context .Background (), opts , nodeInfo .Name , nodeInfo .Deploy - i - 1 ); err != nil {
78
- log .Warnf ("[doCreateContainer] Update processing count failed %v" , err )
79
- }
80
- }
66
+ _ = utils .Txn (
67
+ ctx ,
68
+ func (ctx context.Context ) error {
69
+ for i , m := range c .doCreateContainerOnNode (ctx , nodeInfo , opts , index ) {
70
+ _ = utils .Txn (
71
+ ctx ,
72
+ func (ctx context.Context ) error {
73
+ ch <- m
74
+ return nil
75
+ },
76
+ func (ctx context.Context ) error {
77
+ // decr processing count
78
+ if err := c .store .UpdateProcessing (ctx , opts , nodeInfo .Name , nodeInfo .Deploy - i - 1 ); err != nil {
79
+ log .Warnf ("[doCreateContainer] Update processing count failed %v" , err )
80
+ }
81
+ return nil
82
+ },
83
+ nil ,
84
+ c .config .GlobalTimeout ,
85
+ )
86
+ }
87
+ return nil
88
+ },
89
+ func (ctx context.Context ) error {
90
+ if err := c .store .DeleteProcessing (ctx , opts , nodeInfo ); err != nil {
91
+ log .Errorf ("[doCreateContainer] remove processing status failed %v" , err )
92
+ }
93
+ wg .Done ()
94
+ return nil
95
+ },
96
+ nil ,
97
+ c .config .GlobalTimeout ,
98
+ )
81
99
}(nodeInfo , index )
82
100
index += nodeInfo .Deploy
83
101
}
@@ -101,7 +119,7 @@ func (c *Calcium) doCreateContainerOnNode(ctx context.Context, nodeInfo types.No
101
119
}
102
120
103
121
node := & types.Node {}
104
- if err := c . Transaction (
122
+ if err := utils . Txn (
105
123
ctx ,
106
124
// if
107
125
func (ctx context.Context ) (err error ) {
@@ -121,17 +139,18 @@ func (c *Calcium) doCreateContainerOnNode(ctx context.Context, nodeInfo types.No
121
139
// rollback, will use background context
122
140
func (ctx context.Context ) (err error ) {
123
141
log .Errorf ("[doCreateContainerOnNode] Error when create and start a container, %v" , ms [i ].Error )
124
- if ms [i ].ContainerID == "" {
125
- if err = c .withNodeLocked (ctx , nodeInfo .Name , func (node * types.Node ) error {
126
- return c .store .UpdateNodeResource (ctx , node , cpu , opts .CPUQuota , opts .Memory , opts .Storage , volumePlan .IntoVolumeMap (), store .ActionIncr )
127
- }); err != nil {
128
- log .Errorf ("[doCreateContainer] Reset node %s failed %v" , nodeInfo .Name , err )
129
- }
130
- } else {
142
+ if ms [i ].ContainerID != "" {
131
143
log .Warnf ("[doCreateContainer] Create container failed %v, and container %s not removed" , ms [i ].Error , ms [i ].ContainerID )
144
+ return
145
+ }
146
+ if err = c .withNodeLocked (ctx , nodeInfo .Name , func (node * types.Node ) error {
147
+ return c .store .UpdateNodeResource (ctx , node , cpu , opts .CPUQuota , opts .Memory , opts .Storage , volumePlan .IntoVolumeMap (), store .ActionIncr )
148
+ }); err != nil {
149
+ log .Errorf ("[doCreateContainer] Reset node resource %s failed %v" , nodeInfo .Name , err )
132
150
}
133
151
return
134
152
},
153
+ c .config .GlobalTimeout ,
135
154
); err != nil {
136
155
continue
137
156
}
@@ -185,82 +204,91 @@ func (c *Calcium) doCreateAndStartContainer(
185
204
Publish : map [string ][]string {},
186
205
}
187
206
var err error
188
-
189
- defer func () {
190
- createContainerMessage .Error = err
191
- if err != nil && container .ID != "" {
192
- if err := c .doRemoveContainer (context .Background (), container , true ); err != nil {
193
- log .Errorf ("[doCreateAndStartContainer] create and start container failed, and remove it failed also, %s, %v" , container .ID , err )
194
- return
195
- }
196
- createContainerMessage .ContainerID = ""
197
- }
198
- }()
199
-
200
- // get config
201
- config := c .doMakeContainerOptions (no , cpu , volumePlan , opts , node )
202
- container .Name = config .Name
203
- container .Labels = config .Labels
204
- createContainerMessage .ContainerName = container .Name
205
-
206
- // create container
207
207
var containerCreated * enginetypes.VirtualizationCreated
208
- containerCreated , err = node .Engine .VirtualizationCreate (ctx , config )
209
- if err != nil {
210
- return createContainerMessage
211
- }
212
- container .ID = containerCreated .ID
213
208
214
- // Copy data to container
215
- if len (opts .Data ) > 0 {
216
- for dst , src := range opts .Data {
217
- if _ , err = src .Seek (0 , io .SeekStart ); err != nil {
218
- return createContainerMessage
219
- }
220
- if err = c .doSendFileToContainer (ctx , node .Engine , containerCreated .ID , dst , src , true , true ); err != nil {
221
- return createContainerMessage
209
+ _ = utils .Txn (
210
+ ctx ,
211
+ func (ctx context.Context ) error {
212
+ // get config
213
+ config := c .doMakeContainerOptions (no , cpu , volumePlan , opts , node )
214
+ container .Name = config .Name
215
+ container .Labels = config .Labels
216
+ createContainerMessage .ContainerName = container .Name
217
+
218
+ // create container
219
+ containerCreated , err = node .Engine .VirtualizationCreate (ctx , config )
220
+ if err != nil {
221
+ return err
222
222
}
223
- }
224
- }
223
+ container .ID = containerCreated .ID
225
224
226
- // deal with hook
227
- if len (opts .AfterCreate ) > 0 && container .Hook != nil {
228
- container .Hook = & types.Hook {
229
- AfterStart : append (opts .AfterCreate , container .Hook .AfterStart ... ),
230
- Force : container .Hook .Force ,
231
- }
232
- }
225
+ // Copy data to container
226
+ if len (opts .Data ) > 0 {
227
+ for dst , src := range opts .Data {
228
+ if _ , err = src .Seek (0 , io .SeekStart ); err != nil {
229
+ return err
230
+ }
231
+ if err = c .doSendFileToContainer (ctx , node .Engine , container .ID , dst , src , true , true ); err != nil {
232
+ return err
233
+ }
234
+ }
235
+ }
233
236
234
- // start first
235
- createContainerMessage .Hook , err = c .doStartContainer (ctx , container , opts .IgnoreHook )
236
- if err != nil {
237
- return createContainerMessage
238
- }
237
+ // deal with hook
238
+ if len (opts .AfterCreate ) > 0 && container .Hook != nil {
239
+ container .Hook = & types.Hook {
240
+ AfterStart : append (opts .AfterCreate , container .Hook .AfterStart ... ),
241
+ Force : container .Hook .Force ,
242
+ }
243
+ }
239
244
240
- // inspect real meta
241
- var containerInfo * enginetypes.VirtualizationInfo
242
- containerInfo , err = container .Inspect (ctx ) // 补充静态元数据
243
- if err != nil {
244
- return createContainerMessage
245
- }
245
+ // start first
246
+ createContainerMessage .Hook , err = c .doStartContainer (ctx , container , opts .IgnoreHook )
247
+ if err != nil {
248
+ return err
249
+ }
246
250
247
- // update meta
248
- if containerInfo .Networks != nil {
249
- createContainerMessage .Publish = utils .MakePublishInfo (containerInfo .Networks , opts .Entrypoint .Publish )
250
- }
251
- // reset users
252
- if containerInfo .User != container .User {
253
- container .User = containerInfo .User
254
- }
255
- // reset container.hook
256
- container .Hook = opts .Entrypoint .Hook
251
+ // inspect real meta
252
+ var containerInfo * enginetypes.VirtualizationInfo
253
+ containerInfo , err = container .Inspect (ctx ) // 补充静态元数据
254
+ if err != nil {
255
+ return err
256
+ }
257
257
258
- // store eru container
259
- if err = c .store .AddContainer (ctx , container ); err != nil {
260
- return createContainerMessage
261
- }
262
- // non-empty message.ContainerID signals that "core saves metadata of this container"
263
- createContainerMessage .ContainerID = containerCreated .ID
258
+ // update meta
259
+ if containerInfo .Networks != nil {
260
+ createContainerMessage .Publish = utils .MakePublishInfo (containerInfo .Networks , opts .Entrypoint .Publish )
261
+ }
262
+ // reset users
263
+ if containerInfo .User != container .User {
264
+ container .User = containerInfo .User
265
+ }
266
+ // reset container.hook
267
+ container .Hook = opts .Entrypoint .Hook
268
+ return nil
269
+ },
270
+ func (ctx context.Context ) error {
271
+ // store eru container
272
+ if err = c .store .AddContainer (ctx , container ); err != nil {
273
+ return err
274
+ }
275
+ // non-empty message.ContainerID means "core saves metadata of this container"
276
+ createContainerMessage .ContainerID = container .ID
277
+ return nil
278
+ },
279
+ func (ctx context.Context ) error {
280
+ createContainerMessage .Error = err
281
+ if err != nil && container .ID != "" {
282
+ if err := c .doRemoveContainer (ctx , container , true ); err != nil {
283
+ log .Errorf ("[doCreateAndStartContainer] create and start container failed, and remove it failed also, %s, %v" , container .ID , err )
284
+ return err
285
+ }
286
+ createContainerMessage .ContainerID = ""
287
+ }
288
+ return nil
289
+ },
290
+ c .config .GlobalTimeout ,
291
+ )
264
292
265
293
return createContainerMessage
266
294
}
0 commit comments