forked from hyperhq/runv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm_states.go
322 lines (296 loc) · 8.3 KB
/
vm_states.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package hypervisor
import (
"errors"
"fmt"
"io"
"strings"
"sync"
"time"
"github.com/hyperhq/hypercontainer-utils/hlog"
"github.com/hyperhq/runv/hypervisor/types"
)
// states
const (
StateRunning = "RUNNING"
StateTerminating = "TERMINATING"
StateNone = "NONE"
)
func (ctx *VmContext) timedKill(seconds int) {
ctx.timer = time.AfterFunc(time.Duration(seconds)*time.Second, func() {
if ctx != nil && ctx.handler != nil {
ctx.DCtx.Kill(ctx)
}
})
}
func (ctx *VmContext) newContainer(id string) error {
ctx.lock.Lock()
defer ctx.lock.Unlock()
if ctx.current != StateRunning {
ctx.Log(DEBUG, "start container %s during %v", id, ctx.current)
return NewNotReadyError(ctx.Id)
}
c, ok := ctx.containers[id]
if ok {
ctx.Log(TRACE, "start sending INIT_NEWCONTAINER")
var err error
c.stdinPipe, c.stdoutPipe, c.stderrPipe, err = ctx.hyperstart.NewContainer(c.VmSpec())
if err == nil && c.tty != nil {
go streamCopy(c.tty, c.stdinPipe, c.stdoutPipe, c.stderrPipe)
}
ctx.Log(TRACE, "sent INIT_NEWCONTAINER")
go func() {
status := ctx.hyperstart.WaitProcess(id, "init")
ctx.reportProcessFinished(types.E_CONTAINER_FINISHED, &types.ProcessFinished{
Id: id, Code: uint8(status), Ack: make(chan bool, 1),
})
ctx.lock.Lock()
if c, ok := ctx.containers[id]; ok {
c.Log(TRACE, "container finished, unset iostream pipes")
c.stdinPipe = nil
c.stdoutPipe = nil
c.stderrPipe = nil
c.tty = nil
}
ctx.lock.Unlock()
}()
return err
} else {
return fmt.Errorf("container %s not exist", id)
}
}
func (ctx *VmContext) restoreContainer(id string) (alive bool, err error) {
ctx.lock.Lock()
defer ctx.lock.Unlock()
if ctx.current != StateRunning {
ctx.Log(DEBUG, "start container %s during %v", id, ctx.current)
return false, NewNotReadyError(ctx.Id)
}
c, ok := ctx.containers[id]
if !ok {
return false, fmt.Errorf("try to associate a container not exist in sandbox")
}
// FIXME do we need filter some error type? error=stopped isn't always true.
c.stdinPipe, c.stdoutPipe, c.stderrPipe, err = ctx.hyperstart.RestoreContainer(c.VmSpec())
if err != nil {
ctx.Log(ERROR, "restore conatiner failed in hyperstart, mark as stopped: %v", err)
if strings.Contains(err.Error(), "hyperstart closed") {
return false, err
}
return false, nil
}
go func() {
status := ctx.hyperstart.WaitProcess(id, "init")
ctx.reportProcessFinished(types.E_CONTAINER_FINISHED, &types.ProcessFinished{
Id: id, Code: uint8(status), Ack: make(chan bool, 1),
})
ctx.lock.Lock()
if c, ok := ctx.containers[id]; ok {
c.Log(TRACE, "container finished, unset iostream pipes")
c.stdinPipe = nil
c.stdoutPipe = nil
c.stderrPipe = nil
c.tty = nil
}
ctx.lock.Unlock()
}()
return true, nil
}
func (ctx *VmContext) updateInterface(id string) error {
if inf := ctx.networks.getInterface(id); inf == nil {
return fmt.Errorf("can't find interface whose ID is %s", id)
} else {
return ctx.hyperstart.UpdateInterface(inf.DeviceName, inf.IpAddr, inf.NetMask)
}
}
// TODO remove attachCmd and move streamCopy to hyperd
func (ctx *VmContext) attachCmd(cmd *AttachCommand) error {
ctx.lock.Lock()
defer ctx.lock.Unlock()
if ctx.current != StateRunning {
ctx.Log(DEBUG, "attach container %s during %v", cmd.Container, ctx.current)
return NewNotReadyError(ctx.Id)
}
c, ok := ctx.containers[cmd.Container]
if !ok {
estr := fmt.Sprintf("cannot find container %s to attach", cmd.Container)
ctx.Log(ERROR, estr)
return errors.New(estr)
}
if c.tty != nil {
return fmt.Errorf("we can attach only once")
}
c.tty = cmd.Streams
if c.stdinPipe != nil {
go streamCopy(c.tty, c.stdinPipe, c.stdoutPipe, c.stderrPipe)
}
return nil
}
// TODO move this logic to hyperd
type TtyIO struct {
Stdin io.ReadCloser
Stdout io.Writer
Stderr io.Writer
}
func (tty *TtyIO) Close() {
hlog.Log(TRACE, "Close tty")
if tty.Stdin != nil {
tty.Stdin.Close()
}
cf := func(w io.Writer) {
if w == nil {
return
}
if c, ok := w.(io.WriteCloser); ok {
c.Close()
}
}
cf(tty.Stdout)
cf(tty.Stderr)
}
// TODO move this logic to hyperd
func streamCopy(tty *TtyIO, stdinPipe io.WriteCloser, stdoutPipe, stderrPipe io.ReadCloser) {
var wg sync.WaitGroup
// old way cleanup all(expect stdinPipe) no matter what kinds of fails, TODO: change it
var once sync.Once
// cleanup closes tty.Stdin and thus terminates the first go routine
cleanup := func() {
tty.Close()
// stdinPipe is directly closed in the first go routine
stdoutPipe.Close()
if stderrPipe != nil {
stderrPipe.Close()
}
}
if tty.Stdin != nil {
go func() {
_, err := io.Copy(stdinPipe, tty.Stdin)
stdinPipe.Close()
if err != nil {
// we should not call cleanup when tty.Stdin reaches EOF
once.Do(cleanup)
}
}()
}
if tty.Stdout != nil {
wg.Add(1)
go func() {
_, err := io.Copy(tty.Stdout, stdoutPipe)
if err != nil {
once.Do(cleanup)
}
wg.Done()
}()
}
if tty.Stderr != nil && stderrPipe != nil {
wg.Add(1)
go func() {
_, err := io.Copy(tty.Stderr, stderrPipe)
if err != nil {
once.Do(cleanup)
}
wg.Done()
}()
}
wg.Wait()
once.Do(cleanup)
}
func (ctx *VmContext) startPod() error {
err := ctx.hyperstart.StartSandbox(ctx.networks.sandboxInfo())
if err == nil {
ctx.Log(INFO, "pod start successfully")
ctx.reportSuccess("Start POD success", []byte{})
} else {
reason := fmt.Sprintf("Start POD failed: %s", err.Error())
ctx.reportVmFault(reason)
ctx.Log(ERROR, reason)
}
return err
}
func (ctx *VmContext) shutdownVM() {
ctx.setTimeout(10)
err := ctx.hyperstart.DestroySandbox()
if err == nil {
ctx.Log(DEBUG, "POD destroyed")
ctx.poweroffVM(false, "")
} else {
ctx.Log(WARNING, "Destroy pod failed")
ctx.poweroffVM(true, "Destroy pod failed")
ctx.Close()
}
}
func (ctx *VmContext) poweroffVM(err bool, msg string) {
if err {
ctx.Log(ERROR, "Shutting down because of an exception: ", msg)
}
//REFACTOR: kill directly instead of DCtx.Shutdown() and send shutdown information
ctx.Log(INFO, "poweroff vm based on command: %s", msg)
if ctx != nil && ctx.handler != nil {
ctx.DCtx.Kill(ctx)
}
}
// state machine
func unexpectedEventHandler(ctx *VmContext, ev VmEvent, state string) {
switch ev.Event() {
case COMMAND_SHUTDOWN,
COMMAND_RELEASE,
COMMAND_PAUSEVM:
ctx.reportUnexpectedRequest(ev, state)
default:
ctx.Log(WARNING, "got unexpected event during ", state)
}
}
func stateRunning(ctx *VmContext, ev VmEvent) {
switch ev.Event() {
case COMMAND_SHUTDOWN:
ctx.Log(TRACE, "got shutdown command, shutting down")
go ctx.shutdownVM()
ctx.Become(stateTerminating, StateTerminating)
case COMMAND_RELEASE:
ctx.Log(TRACE, "pod is running, got release command, let VM fly")
ctx.Become(nil, StateNone)
ctx.reportSuccess("", nil)
case EVENT_INIT_CONNECTED:
ctx.Log(TRACE, "hyperstart is ready to accept vm commands")
ctx.reportVmRun()
case EVENT_VM_EXIT, ERROR_VM_START_FAILED:
ctx.Log(TRACE, "VM has exit, or not started at all (%d)", ev.Event())
ctx.reportVmShutdown()
ctx.Close()
case EVENT_VM_TIMEOUT: // REFACTOR: we do not set timeout for prepare devices after the refactor, then we do not need wait this event any more
ctx.Log(ERROR, "REFACTOR: should be no time in running state at all")
case ERROR_INIT_FAIL: // VM connection Failure
reason := ev.(*InitFailedEvent).Reason
ctx.Log(ERROR, reason)
ctx.poweroffVM(true, "connection to vm broken")
ctx.Close()
case ERROR_INTERRUPTED:
ctx.Log(TRACE, "Connection interrupted, quit...")
ctx.poweroffVM(true, "connection to vm broken")
ctx.Close()
default:
unexpectedEventHandler(ctx, ev, "pod running")
}
}
func stateTerminating(ctx *VmContext, ev VmEvent) {
switch ev.Event() {
case EVENT_VM_EXIT:
ctx.Log(TRACE, "Got VM shutdown event while terminating, go to cleaning up")
ctx.reportVmShutdown()
ctx.Close()
case EVENT_VM_KILL:
ctx.Log(TRACE, "Got VM force killed message, go to cleaning up")
ctx.reportVmShutdown()
ctx.Close()
case COMMAND_RELEASE:
ctx.Log(TRACE, "vm terminating, got release")
case EVENT_VM_TIMEOUT:
ctx.Log(WARNING, "VM did not exit in time, try to stop it")
ctx.poweroffVM(true, "vm terminating timeout")
ctx.Close()
case ERROR_INTERRUPTED:
interruptEv := ev.(*Interrupted)
ctx.Log(TRACE, "Connection interrupted while terminating: %s", interruptEv.Reason)
default:
unexpectedEventHandler(ctx, ev, "terminating")
}
}