Skip to content

Commit

Permalink
improvements to (VM's) shutdown logic
Browse files Browse the repository at this point in the history
- when shutting down corectld or halting multiple VMs those will
  always be halted in reverse boot order
- when hit by a signal corectld will now properly halt running VMs

Signed-off-by: António Meireles <[email protected]>
  • Loading branch information
AntonioMeireles committed Aug 3, 2016
1 parent 9055155 commit 2e040f4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 25 deletions.
33 changes: 15 additions & 18 deletions components/server/rpcservices.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type (
Meta *release.Info
VM *VMInfo
Images map[string]semver.Versions
Running map[string]*VMInfo
Running VMmap
WorkingNFS bool
}
)
Expand Down Expand Up @@ -272,6 +272,11 @@ func (s *RPCservice) Run(r *http.Request,
Daemon.Jobs.Add(1)
defer Daemon.Jobs.Done()
Daemon.Lock()
vm.exec.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
Setsid: false,
Pgid: 0,
}
err := vm.exec.Start()
Daemon.Unlock()
if err != nil {
Expand Down Expand Up @@ -307,6 +312,11 @@ func (s *RPCservice) Stop(r *http.Request,
}

log.Info("Sky must be falling. Shutting down...")
Daemon.Lock()
Daemon.AcceptingRequests = false
Daemon.Unlock()

Daemon.Active.array().halt()
Daemon.Oops <- nil
return
}
Expand All @@ -327,37 +337,24 @@ func (s *RPCservice) StopVMs(r *http.Request,
args *RPCquery, reply *RPCreply) (err error) {
log.Debug("vm:stop")

var toHalt []string
var targets VMs

if !Daemon.AcceptingRequests {
return ErrServerShuttingDown
}

if len(args.Input) == 0 {
for _, x := range Daemon.Active {
toHalt = append(toHalt, x.UUID)
}
targets = Daemon.Active.array()
} else {
for _, t := range args.Input {
for _, v := range Daemon.Active {
if v.Name == t || v.UUID == t {
toHalt = append(toHalt, v.UUID)
targets = append(targets, v)
}
}
}
}
for _, v := range toHalt {
Daemon.Active[v].halt()
for {
Daemon.Lock()
_, stillAlive := Daemon.Active[v]
Daemon.Unlock()
if !stillAlive {
break
}
time.Sleep(100 * time.Millisecond)
}
}
targets.halt()
return
}

Expand Down
41 changes: 41 additions & 0 deletions components/server/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/pem"
"fmt"
"os/exec"
"sort"
"syscall"

"net/http"
Expand Down Expand Up @@ -59,6 +60,10 @@ type (
isolationCheck, callBack sync.Once
cloudConfigContents []byte
}
//
VMmap map[string]*VMInfo
// Config ...
VMs []*VMInfo
// NetworkInterface ...
NetworkInterface struct {
Type int
Expand Down Expand Up @@ -333,6 +338,25 @@ func (vm *VMInfo) assembleBootPayload() (xArgs []string, err error) {
err
}

func (list VMs) halt() {
sort.Sort(sort.Reverse(VMs(list)))

for _, v := range list {
log.Info("shutting down %v...", v.Name)
Daemon.Active[v.UUID].halt()
for {
Daemon.Lock()
_, stillAlive := Daemon.Active[v.UUID]
Daemon.Unlock()
if !stillAlive {
break
}
time.Sleep(100 * time.Millisecond)
}
}
return
}

func (vm *VMInfo) halt() {
// Try to gracefully terminate the process.
if err := vm.exec.Process.Signal(syscall.SIGTERM); err != nil {
Expand Down Expand Up @@ -455,3 +479,20 @@ func (volumes *StorageAssets) PrettyPrint(root int) {
}
}
}

func (run VMs) Len() int {
return len(run)
}
func (run VMs) Swap(i, j int) {
run[i], run[j] = run[j], run[i]
}
func (run VMs) Less(i, j int) bool {
return run[i].CreationTime.Before(run[j].CreationTime)
}

func (in VMmap) array() (out VMs) {
for _, r := range in {
out = append(out, r)
}
return
}
10 changes: 3 additions & 7 deletions components/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type (
ServerContext struct {
Meta *release.Info
Media MediaAssets
Active map[string]*VMInfo
Active VMmap
APIserver *manners.GracefulServer
EtcdServer *EtcdServer
EtcdClient client.KeysAPI
Expand All @@ -64,7 +64,7 @@ func New() (cfg *ServerContext) {
AcceptingRequests: true,
WorkingNFS: false,
Oops: make(chan error, 1),
Active: make(map[string]*VMInfo),
Active: make(VMmap),
}
}

Expand Down Expand Up @@ -124,6 +124,7 @@ func Start() (err error) {
log.Info("Got '%v' signal, stopping server...", s)
signal.Stop(hades)
Daemon.Oops <- nil
Daemon.Active.array().halt()
}()

log.Info("server starting...")
Expand Down Expand Up @@ -154,11 +155,6 @@ func Start() (err error) {
}
}

Daemon.Lock()
for _, r := range Daemon.Active {
r.halt()
}
Daemon.Unlock()
Daemon.Jobs.Wait()
Daemon.APIserver.Close()
log.Info("gone!")
Expand Down

0 comments on commit 2e040f4

Please sign in to comment.