Skip to content

Commit 1f31358

Browse files
committed
handle created WAL by distinguishing status
1 parent 607336a commit 1f31358

File tree

8 files changed

+58
-58
lines changed

8 files changed

+58
-58
lines changed

cluster/calcium/wal.go

+24-43
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package calcium
33
import (
44
"context"
55
"encoding/json"
6-
"strings"
76
"time"
87

98
"github.com/projecteru2/core/log"
@@ -49,13 +48,6 @@ func (w *WAL) registerHandlers() {
4948
w.Register(newProcessingCreatedHandler(w.calcium))
5049
}
5150

52-
func (w *WAL) logCreateWorkload(workloadID, nodename string) (wal.Commit, error) {
53-
return w.Log(eventCreateWorkload, &types.Workload{
54-
ID: workloadID,
55-
Nodename: nodename,
56-
})
57-
}
58-
5951
func (w *WAL) logCreateLambda(opts *types.CreateWorkloadMessage) (wal.Commit, error) {
6052
return w.Log(eventCreateLambda, opts.WorkloadID)
6153
}
@@ -79,30 +71,12 @@ func (h *CreateWorkloadHandler) Event() string {
7971
}
8072

8173
// Check .
82-
func (h *CreateWorkloadHandler) Check(ctx context.Context, raw interface{}) (bool, error) {
83-
wrk, ok := raw.(*types.Workload)
74+
func (h *CreateWorkloadHandler) Check(ctx context.Context, raw interface{}) (handle bool, err error) {
75+
_, ok := raw.(*types.Workload)
8476
if !ok {
8577
return false, types.NewDetailedErr(types.ErrInvalidType, raw)
8678
}
87-
logger := log.WithField("WAL.Check", "CreateWorkload").WithField("ID", wrk.ID)
88-
89-
ctx, cancel := getReplayContext(ctx)
90-
defer cancel()
91-
92-
_, err := h.calcium.GetWorkload(ctx, wrk.ID)
93-
switch {
94-
// there has been an exact workload metadata.
95-
case err == nil:
96-
return false, nil
97-
98-
case strings.HasPrefix(err.Error(), types.ErrBadCount.Error()):
99-
logger.Errorf(ctx, "No such workload")
100-
return true, nil
101-
102-
default:
103-
logger.Errorf(ctx, "Unexpected error: %v", err)
104-
return false, err
105-
}
79+
return true, nil
10680
}
10781

10882
// Encode .
@@ -121,31 +95,38 @@ func (h *CreateWorkloadHandler) Decode(bs []byte) (interface{}, error) {
12195
return wrk, err
12296
}
12397

124-
// Handle .
98+
// Handle: remove instance, remove meta, restore resource
12599
func (h *CreateWorkloadHandler) Handle(ctx context.Context, raw interface{}) (err error) {
126-
wrk, ok := raw.(*types.Workload)
127-
if !ok {
128-
return types.NewDetailedErr(types.ErrInvalidType, raw)
129-
}
100+
wrk, _ := raw.(*types.Workload)
130101
logger := log.WithField("WAL.Handle", "CreateWorkload").WithField("ID", wrk.ID).WithField("nodename", wrk.Nodename)
131102

132103
ctx, cancel := getReplayContext(ctx)
133104
defer cancel()
134105

135-
ch, err := h.calcium.RemoveWorkload(ctx, []string{wrk.ID}, true, 0)
106+
if _, err = h.calcium.GetWorkload(ctx, wrk.ID); err == nil {
107+
// workload meta exists
108+
ch, err := h.calcium.RemoveWorkload(ctx, []string{wrk.ID}, true, 0)
109+
if err != nil {
110+
return logger.Err(ctx, err)
111+
}
112+
for msg := range ch {
113+
if !msg.Success {
114+
logger.Errorf(ctx, "failed to remove workload")
115+
}
116+
}
117+
return nil
118+
}
119+
120+
// workload meta doesn't exist
121+
node, err := h.calcium.GetNode(ctx, wrk.Nodename)
136122
if err != nil {
137-
logger.Errorf(ctx, "failed to remove workload")
138-
return
123+
return logger.Err(ctx, err)
139124
}
140-
for msg := range ch {
141-
if !msg.Success {
142-
logger.Errorf(ctx, "failed to remove workload")
143-
return nil
144-
}
125+
if _, err = node.Engine.VirtualizationRemove(ctx, wrk.ID, true, true); err != nil {
126+
return logger.Err(ctx, err)
145127
}
146128

147129
logger.Infof(ctx, "workload removed")
148-
149130
return nil
150131
}
151132

engine/docker/container.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,14 @@ func (e *Engine) VirtualizationStop(ctx context.Context, ID string, gracefulTime
346346
}
347347

348348
// VirtualizationRemove remove virtualization
349-
func (e *Engine) VirtualizationRemove(ctx context.Context, ID string, removeVolumes, force bool) error {
350-
return e.client.ContainerRemove(ctx, ID, dockertypes.ContainerRemoveOptions{RemoveVolumes: removeVolumes, Force: force})
349+
func (e *Engine) VirtualizationRemove(ctx context.Context, ID string, removeVolumes, force bool) (removed int, err error) {
350+
if err = e.client.ContainerRemove(ctx, ID, dockertypes.ContainerRemoveOptions{RemoveVolumes: removeVolumes, Force: force}); err == nil {
351+
return 1, nil
352+
}
353+
if strings.Contains(err.Error(), "No such container") {
354+
return 0, nil
355+
}
356+
return
351357
}
352358

353359
// VirtualizationInspect get virtualization info

engine/engine.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type API interface {
4141
VirtualizationCopyTo(ctx context.Context, ID, target string, content []byte, uid, gid int, mode int64) error
4242
VirtualizationStart(ctx context.Context, ID string) error
4343
VirtualizationStop(ctx context.Context, ID string, gracefulTimeout time.Duration) error
44-
VirtualizationRemove(ctx context.Context, ID string, volumes, force bool) error
44+
VirtualizationRemove(ctx context.Context, ID string, volumes, force bool) (int, error)
4545
VirtualizationInspect(ctx context.Context, ID string) (*enginetypes.VirtualizationInfo, error)
4646
VirtualizationLogs(ctx context.Context, opts *enginetypes.VirtualizationLogStreamOptions) (stdout, stderr io.ReadCloser, err error)
4747
VirtualizationAttach(ctx context.Context, ID string, stream, openStdin bool) (stdout, stderr io.ReadCloser, stdin io.WriteCloser, err error)

engine/fake/fake.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ func (f *Engine) VirtualizationStop(ctx context.Context, ID string, gracefulTime
139139
}
140140

141141
// VirtualizationRemove .
142-
func (f *Engine) VirtualizationRemove(ctx context.Context, ID string, volumes, force bool) error {
143-
return types.ErrNilEngine
142+
func (f *Engine) VirtualizationRemove(ctx context.Context, ID string, volumes, force bool) (int, error) {
143+
return 0, types.ErrNilEngine
144144
}
145145

146146
// VirtualizationInspect .

engine/mocks/API.go

+13-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

engine/mocks/fakeengine/mock.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func MakeClient(ctx context.Context, config coretypes.Config, nodename, endpoint
9797
e.On("VirtualizationCopyTo", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
9898
e.On("VirtualizationStart", mock.Anything, mock.Anything).Return(nil)
9999
e.On("VirtualizationStop", mock.Anything, mock.Anything, mock.Anything).Return(nil)
100-
e.On("VirtualizationRemove", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
100+
e.On("VirtualizationRemove", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(1, nil)
101101
vcJSON := &enginetypes.VirtualizationInfo{ID: ID, Image: "mock-image", Running: true, Networks: map[string]string{"mock-network": "1.1.1.1"}}
102102
e.On("VirtualizationInspect", mock.Anything, mock.Anything).Return(vcJSON, nil)
103103
logs := ioutil.NopCloser(bytes.NewBufferString("logs1...\nlogs2...\n"))

engine/virt/virt.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,13 @@ func (v *Virt) VirtualizationStop(ctx context.Context, ID string, gracefulTimeou
228228
}
229229

230230
// VirtualizationRemove removes a guest.
231-
func (v *Virt) VirtualizationRemove(ctx context.Context, ID string, volumes, force bool) (err error) {
232-
_, err = v.client.DestroyGuest(ctx, ID, force)
231+
func (v *Virt) VirtualizationRemove(ctx context.Context, ID string, volumes, force bool) (removed int, err error) {
232+
if _, err = v.client.DestroyGuest(ctx, ID, force); err == nil {
233+
return 1, nil
234+
}
235+
if strings.Contains(err.Error(), "key not exists") {
236+
return 0, nil
237+
}
233238
return
234239
}
235240

types/workload.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ func (c *Workload) Remove(ctx context.Context, force bool) error {
8585
if c.Engine == nil {
8686
return errors.WithStack(ErrNilEngine)
8787
}
88-
return errors.WithStack(c.Engine.VirtualizationRemove(ctx, c.ID, true, force))
88+
_, err := c.Engine.VirtualizationRemove(ctx, c.ID, true, force)
89+
return errors.WithStack(err)
8990
}
9091

9192
// WorkloadStatus store deploy status

0 commit comments

Comments
 (0)