Skip to content

Commit ab7e2a8

Browse files
committed
Txn rollback func can know which part fails
1 parent b6461c9 commit ab7e2a8

File tree

12 files changed

+201
-295
lines changed

12 files changed

+201
-295
lines changed

cluster/calcium/create.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (c *Calcium) doCreateWorkloads(ctx context.Context, opts *types.DeployOptio
9797
},
9898

9999
// rollback: give back resources
100-
func(ctx context.Context) (err error) {
100+
func(ctx context.Context, _ bool) (err error) {
101101
for nodename, rollbackIndices := range rollbackMap {
102102
if e := c.withNodeLocked(ctx, nodename, func(node *types.Node) error {
103103
for _, plan := range plans {
@@ -170,7 +170,7 @@ func (c *Calcium) doDeployWorkloadsOnNode(ctx context.Context, ch chan *types.Cr
170170
ch <- createMsg
171171
}()
172172

173-
var r *types.ResourceMeta
173+
r := &types.ResourceMeta{}
174174
o := resourcetypes.DispenseOptions{
175175
Node: node,
176176
Index: idx,
@@ -182,8 +182,7 @@ func (c *Calcium) doDeployWorkloadsOnNode(ctx context.Context, ch chan *types.Cr
182182
}
183183

184184
createMsg.ResourceMeta = *r
185-
e = c.doDeployOneWorkload(ctx, node, opts, createMsg, seq+idx, deploy-1-idx)
186-
return e
185+
return c.doDeployOneWorkload(ctx, node, opts, createMsg, seq+idx, deploy-1-idx)
187186
}
188187
_ = do(idx)
189188
}
@@ -307,7 +306,7 @@ func (c *Calcium) doDeployOneWorkload(
307306
},
308307

309308
// remove container
310-
func(ctx context.Context) error {
309+
func(ctx context.Context, _ bool) error {
311310
return errors.WithStack(c.doRemoveContainer(ctx, container, true))
312311
},
313312
c.config.GlobalTimeout,

cluster/calcium/realloc.go

+29-25
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,22 @@ func (c *Calcium) doReallocOnNode(ctx context.Context, nodename string, containe
4949
return utils.Txn(
5050
ctx,
5151

52-
// if commit changes
52+
// if update workload resources
5353
func(ctx context.Context) (err error) {
54+
return c.doReallocContainersOnInstance(ctx, node, plans, container)
55+
},
56+
// then commit changes
57+
func(ctx context.Context) error {
5458
for _, plan := range plans {
5559
plan.ApplyChangesOnNode(node, 1)
5660
}
5761
return c.store.UpdateNodes(ctx, node)
5862
},
59-
60-
// then update workload resources
61-
func(ctx context.Context) error {
62-
return c.doReallocContainersOnInstance(ctx, node, plans, container)
63-
},
64-
6563
// rollback to origin
66-
func(ctx context.Context) error {
64+
func(ctx context.Context, failureByCond bool) error {
65+
if failureByCond {
66+
return nil
67+
}
6768
for _, plan := range plans {
6869
plan.RollbackChangesOnNode(node, 1)
6970
}
@@ -94,7 +95,22 @@ func (c *Calcium) doReallocContainersOnInstance(ctx context.Context, node *types
9495
return utils.Txn(
9596
ctx,
9697

97-
// if: update container meta
98+
// if: update container resources
99+
func(ctx context.Context) error {
100+
r := &enginetypes.VirtualizationResource{
101+
CPU: r.CPU,
102+
Quota: r.CPUQuotaLimit,
103+
NUMANode: r.NUMANode,
104+
Memory: r.MemoryLimit,
105+
Volumes: r.VolumeLimit.ToStringSlice(false, false),
106+
VolumePlan: r.VolumePlanLimit.ToLiteral(),
107+
VolumeChanged: r.VolumeChanged,
108+
Storage: r.StorageLimit,
109+
}
110+
return errors.WithStack(node.Engine.VirtualizationUpdateResource(ctx, container.ID, r))
111+
},
112+
113+
// then: update container meta
98114
func(ctx context.Context) error {
99115
container.CPUQuotaRequest = r.CPUQuotaRequest
100116
container.CPUQuotaLimit = r.CPUQuotaLimit
@@ -110,23 +126,11 @@ func (c *Calcium) doReallocContainersOnInstance(ctx context.Context, node *types
110126
return errors.WithStack(c.store.UpdateContainer(ctx, container))
111127
},
112128

113-
// then: update container resources
114-
func(ctx context.Context) error {
115-
r := &enginetypes.VirtualizationResource{
116-
CPU: r.CPU,
117-
Quota: r.CPUQuotaLimit,
118-
NUMANode: r.NUMANode,
119-
Memory: r.MemoryLimit,
120-
Volumes: r.VolumeLimit.ToStringSlice(false, false),
121-
VolumePlan: r.VolumePlanLimit.ToLiteral(),
122-
VolumeChanged: r.VolumeChanged,
123-
Storage: r.StorageLimit,
124-
}
125-
return errors.WithStack(node.Engine.VirtualizationUpdateResource(ctx, container.ID, r))
126-
},
127-
128129
// rollback: container meta
129-
func(ctx context.Context) error {
130+
func(ctx context.Context, failureByCond bool) error {
131+
if failureByCond {
132+
return nil
133+
}
130134
return errors.WithStack(c.store.UpdateContainer(ctx, &originalContainer))
131135
},
132136

0 commit comments

Comments
 (0)