@@ -33,21 +33,32 @@ func (c *Calcium) RemoveWorkload(ctx context.Context, ids []string, force bool,
33
33
ret := & types.RemoveWorkloadMessage {WorkloadID : id , Success : false , Hook : []* bytes.Buffer {}}
34
34
if err := c .withWorkloadLocked (ctx , id , func (ctx context.Context , workload * types.Workload ) error {
35
35
return c .withNodeLocked (ctx , workload .Nodename , func (ctx context.Context , node * types.Node ) (err error ) {
36
- return utils .Txn (
36
+ if err = utils .Txn (
37
37
ctx ,
38
38
// if
39
39
func (ctx context.Context ) error {
40
- return errors .WithStack (c .doRemoveWorkload (ctx , workload , force ))
40
+ return errors .WithStack (c .store . UpdateNodeResource (ctx , node , & workload . ResourceMeta , store . ActionIncr ))
41
41
},
42
42
// then
43
43
func (ctx context.Context ) error {
44
- log .Infof ("[RemoveWorkload] Workload %s removed" , workload .ID )
45
- return errors .WithStack (c .store .UpdateNodeResource (ctx , node , & workload .ResourceMeta , store .ActionIncr ))
44
+ err := errors .WithStack (c .doRemoveWorkload (ctx , workload , force ))
45
+ if err != nil {
46
+ log .Infof ("[RemoveWorkload] Workload %s removed" , workload .ID )
47
+ }
48
+ return err
46
49
},
47
50
// rollback
48
- nil ,
51
+ func (ctx context.Context , _ bool ) error {
52
+ return errors .WithStack (c .store .UpdateNodeResource (ctx , node , & workload .ResourceMeta , store .ActionDecr ))
53
+ },
49
54
c .config .GlobalTimeout ,
50
- )
55
+ ); err != nil {
56
+ return
57
+ }
58
+
59
+ // TODO@zc: 优化一下, 先按照 node 聚合 ids
60
+ c .doRemapResourceAndLog (ctx , logger , node )
61
+ return
51
62
})
52
63
}); err != nil {
53
64
logger .Errorf ("[RemoveWorkload] Remove workload %s failed, err: %+v" , id , err )
@@ -66,19 +77,22 @@ func (c *Calcium) RemoveWorkload(ctx context.Context, ids []string, force bool,
66
77
return ch , nil
67
78
}
68
79
80
+ // semantic: instance removed on err == nil, instance remained on err != nil
69
81
func (c * Calcium ) doRemoveWorkload (ctx context.Context , workload * types.Workload , force bool ) error {
70
82
return utils .Txn (
71
83
ctx ,
72
84
// if
73
85
func (ctx context.Context ) error {
74
- return errors .WithStack (workload . Remove (ctx , force ))
86
+ return errors .WithStack (c . store . RemoveWorkload (ctx , workload ))
75
87
},
76
88
// then
77
89
func (ctx context.Context ) error {
78
- return errors .WithStack (c . store . RemoveWorkload (ctx , workload ))
90
+ return errors .WithStack (workload . Remove (ctx , force ))
79
91
},
80
92
// rollback
81
- nil ,
93
+ func (ctx context.Context , _ bool ) error {
94
+ return errors .WithStack (c .store .AddWorkload (ctx , workload ))
95
+ },
82
96
c .config .GlobalTimeout ,
83
97
)
84
98
0 commit comments