17244: Refactor crunchstat to use cgroup unified/hybrid modes.
[arvados.git] / lib / crunchrun / docker.go
index bb635265862fff8a4bf36127000297bbad16e051..4adc680d9bf7c3402e43048576f917fb3c2ade09 100644 (file)
@@ -4,6 +4,7 @@
 package crunchrun
 
 import (
+       "context"
        "fmt"
        "io"
        "io/ioutil"
@@ -17,7 +18,6 @@ import (
        dockertypes "github.com/docker/docker/api/types"
        dockercontainer "github.com/docker/docker/api/types/container"
        dockerclient "github.com/docker/docker/client"
-       "golang.org/x/net/context"
 )
 
 // Docker daemon won't let you set a limit less than ~10 MiB
@@ -34,7 +34,7 @@ const DockerAPIVersion = "1.35"
 // Number of consecutive "inspect container" failures before
 // concluding Docker is unresponsive, giving up, and cancelling the
 // container.
-const dockerWatchdogThreshold = 3
+const dockerWatchdogThreshold = 5
 
 type dockerExecutor struct {
        containerUUID    string
@@ -52,7 +52,7 @@ func newDockerExecutor(containerUUID string, logf func(string, ...interface{}),
        // currently the minimum version we want to support.
        client, err := dockerclient.NewClient(dockerclient.DefaultDockerHost, DockerAPIVersion, nil, nil)
        if watchdogInterval < 1 {
-               watchdogInterval = time.Minute
+               watchdogInterval = time.Minute * 2
        }
        return &dockerExecutor{
                containerUUID:    containerUUID,
@@ -187,7 +187,7 @@ func (e *dockerExecutor) config(spec containerSpec) (dockercontainer.Config, doc
 
 func (e *dockerExecutor) Create(spec containerSpec) error {
        cfg, hostCfg := e.config(spec)
-       created, err := e.dockerclient.ContainerCreate(context.TODO(), &cfg, &hostCfg, nil, e.containerUUID)
+       created, err := e.dockerclient.ContainerCreate(context.TODO(), &cfg, &hostCfg, nil, nil, e.containerUUID)
        if err != nil {
                return fmt.Errorf("While creating container: %v", err)
        }
@@ -195,8 +195,15 @@ func (e *dockerExecutor) Create(spec containerSpec) error {
        return e.startIO(spec.Stdin, spec.Stdout, spec.Stderr)
 }
 
-func (e *dockerExecutor) CgroupID() string {
-       return e.containerID
+func (e *dockerExecutor) Pid() int {
+       ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(10*time.Second))
+       defer cancel()
+       ctr, err := e.dockerclient.ContainerInspect(ctx, e.containerID)
+       if err != nil && ctr.State != nil {
+               return ctr.State.Pid
+       } else {
+               return -1
+       }
 }
 
 func (e *dockerExecutor) Start() error {