mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #39105 from cpuguy83/optimize_test_daemon_startup
Optimize test daemon startup
This commit is contained in:
commit
f0fe0d6c4e
3 changed files with 41 additions and 25 deletions
|
@ -91,6 +91,8 @@ func (cli *DaemonCli) start(opts *daemonOptions) (err error) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logrus.Info("Starting up")
|
||||||
|
|
||||||
cli.configFile = &opts.configFile
|
cli.configFile = &opts.configFile
|
||||||
cli.flags = opts.flags
|
cli.flags = opts.flags
|
||||||
|
|
||||||
|
@ -266,6 +268,7 @@ func (cli *DaemonCli) start(opts *daemonOptions) (err error) {
|
||||||
return errors.Wrap(errAPI, "shutting down due to ServeAPI error")
|
return errors.Wrap(errAPI, "shutting down due to ServeAPI error")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logrus.Info("Daemon shutdown complete")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"github.com/docker/docker/pkg/homedir"
|
"github.com/docker/docker/pkg/homedir"
|
||||||
"github.com/docker/libnetwork/portallocator"
|
"github.com/docker/libnetwork/portallocator"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -152,6 +153,7 @@ func (cli *DaemonCli) initContainerD(ctx context.Context) (func(time.Duration) e
|
||||||
return nil, errors.Wrap(err, "could not determine whether the system containerd is running")
|
return nil, errors.Wrap(err, "could not determine whether the system containerd is running")
|
||||||
}
|
}
|
||||||
if !ok {
|
if !ok {
|
||||||
|
logrus.Debug("Containerd not running, starting daemon managed containerd")
|
||||||
opts, err := cli.getContainerdDaemonOpts()
|
opts, err := cli.getContainerdDaemonOpts()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "failed to generate containerd options")
|
return nil, errors.Wrap(err, "failed to generate containerd options")
|
||||||
|
@ -161,6 +163,7 @@ func (cli *DaemonCli) initContainerD(ctx context.Context) (func(time.Duration) e
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "failed to start containerd")
|
return nil, errors.Wrap(err, "failed to start containerd")
|
||||||
}
|
}
|
||||||
|
logrus.Debug("Started daemon managed containerd")
|
||||||
cli.Config.ContainerdAddr = r.Address()
|
cli.Config.ContainerdAddr = r.Address()
|
||||||
|
|
||||||
// Try to wait for containerd to shutdown
|
// Try to wait for containerd to shutdown
|
||||||
|
|
|
@ -290,27 +290,10 @@ func (d *Daemon) StartWithLogFile(out *os.File, providedArgs ...string) error {
|
||||||
|
|
||||||
d.Wait = wait
|
d.Wait = wait
|
||||||
|
|
||||||
ticker := time.NewTicker(500 * time.Millisecond)
|
|
||||||
defer ticker.Stop()
|
|
||||||
tick := ticker.C
|
|
||||||
|
|
||||||
// make sure daemon is ready to receive requests
|
|
||||||
startTime := time.Now().Unix()
|
|
||||||
for {
|
|
||||||
d.log.Logf("[%s] waiting for daemon to start", d.id)
|
|
||||||
if time.Now().Unix()-startTime > 5 {
|
|
||||||
// After 5 seconds, give up
|
|
||||||
return errors.Errorf("[%s] Daemon exited and never started", d.id)
|
|
||||||
}
|
|
||||||
select {
|
|
||||||
case <-time.After(2 * time.Second):
|
|
||||||
return errors.Errorf("[%s] timeout: daemon does not respond", d.id)
|
|
||||||
case <-tick:
|
|
||||||
clientConfig, err := d.getClientConfig()
|
clientConfig, err := d.getClientConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Transport: clientConfig.transport,
|
Transport: clientConfig.transport,
|
||||||
}
|
}
|
||||||
|
@ -321,10 +304,30 @@ func (d *Daemon) StartWithLogFile(out *os.File, providedArgs ...string) error {
|
||||||
}
|
}
|
||||||
req.URL.Host = clientConfig.addr
|
req.URL.Host = clientConfig.addr
|
||||||
req.URL.Scheme = clientConfig.scheme
|
req.URL.Scheme = clientConfig.scheme
|
||||||
resp, err := client.Do(req)
|
|
||||||
|
ticker := time.NewTicker(500 * time.Millisecond)
|
||||||
|
defer ticker.Stop()
|
||||||
|
tick := ticker.C
|
||||||
|
|
||||||
|
timeout := time.NewTimer(60 * time.Second) // timeout for the whole loop
|
||||||
|
defer timeout.Stop()
|
||||||
|
|
||||||
|
// make sure daemon is ready to receive requests
|
||||||
|
for {
|
||||||
|
d.log.Logf("[%s] waiting for daemon to start", d.id)
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-timeout.C:
|
||||||
|
return errors.Errorf("[%s] Daemon exited and never started", d.id)
|
||||||
|
case <-tick:
|
||||||
|
ctx, cancel := context.WithTimeout(context.TODO(), 2*time.Second)
|
||||||
|
resp, err := client.Do(req.WithContext(ctx))
|
||||||
|
cancel()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
d.log.Logf("[%s] error pinging daemon on start: %v", d.id, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
d.log.Logf("[%s] received status != 200 OK: %s\n", d.id, resp.Status)
|
d.log.Logf("[%s] received status != 200 OK: %s\n", d.id, resp.Status)
|
||||||
|
@ -420,12 +423,16 @@ func (d *Daemon) Stop(t testingT) {
|
||||||
// If it timeouts, a SIGKILL is sent.
|
// If it timeouts, a SIGKILL is sent.
|
||||||
// Stop will not delete the daemon directory. If a purged daemon is needed,
|
// Stop will not delete the daemon directory. If a purged daemon is needed,
|
||||||
// instantiate a new one with NewDaemon.
|
// instantiate a new one with NewDaemon.
|
||||||
func (d *Daemon) StopWithError() error {
|
func (d *Daemon) StopWithError() (err error) {
|
||||||
if d.cmd == nil || d.Wait == nil {
|
if d.cmd == nil || d.Wait == nil {
|
||||||
return errDaemonNotStarted
|
return errDaemonNotStarted
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
|
if err == nil {
|
||||||
|
d.log.Logf("[%s] Daemon stopped", d.id)
|
||||||
|
} else {
|
||||||
|
d.log.Logf("[%s] Error when stopping daemon: %v", d.id, err)
|
||||||
|
}
|
||||||
d.logFile.Close()
|
d.logFile.Close()
|
||||||
d.cmd = nil
|
d.cmd = nil
|
||||||
}()
|
}()
|
||||||
|
@ -435,12 +442,15 @@ func (d *Daemon) StopWithError() error {
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
tick := ticker.C
|
tick := ticker.C
|
||||||
|
|
||||||
|
d.log.Logf("[%s] Stopping daemon", d.id)
|
||||||
|
|
||||||
if err := d.cmd.Process.Signal(os.Interrupt); err != nil {
|
if err := d.cmd.Process.Signal(os.Interrupt); err != nil {
|
||||||
if strings.Contains(err.Error(), "os: process already finished") {
|
if strings.Contains(err.Error(), "os: process already finished") {
|
||||||
return errDaemonNotStarted
|
return errDaemonNotStarted
|
||||||
}
|
}
|
||||||
return errors.Errorf("could not send signal: %v", err)
|
return errors.Errorf("could not send signal: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
out1:
|
out1:
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
|
Loading…
Reference in a new issue