Skip to content

Commit

Permalink
Merge pull request #3459 from multani/docker-oom-notification
Browse files Browse the repository at this point in the history
docker: log that a container has been killed by the OOM killer
  • Loading branch information
dadgar authored Nov 3, 2017
2 parents 927b10e + 15114e4 commit 11184c7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
7 changes: 7 additions & 0 deletions client/driver/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1609,6 +1609,13 @@ func (h *DockerHandle) run() {
werr = fmt.Errorf("Docker container exited with non-zero exit code: %d", exitCode)
}

container, ierr := h.waitClient.InspectContainer(h.containerID)
if ierr != nil {
h.logger.Printf("[ERR] driver.docker: failed to inspect container %s: %v", h.containerID, ierr)
} else if container.State.OOMKilled {
werr = fmt.Errorf("OOM Killed")
}

close(h.doneCh)

// Shutdown the syslog collector
Expand Down
50 changes: 50 additions & 0 deletions client/driver/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1753,3 +1753,53 @@ func TestDockerDriver_AuthConfiguration(t *testing.T) {
}
}
}

func TestDockerDriver_OOMKilled(t *testing.T) {
if !tu.IsTravis() {
t.Parallel()
}
if !testutil.DockerIsConnected(t) {
t.Skip("Docker not connected")
}

task := &structs.Task{
Name: "oom-killed",
Driver: "docker",
Config: map[string]interface{}{
"image": "busybox",
"load": "busybox.tar",
"command": "sh",
// Incrementally creates a bigger and bigger variable.
"args": []string{"-c", "x=a; while true; do eval x='$x$x'; done"},
},
LogConfig: &structs.LogConfig{
MaxFiles: 10,
MaxFileSizeMB: 10,
},
Resources: &structs.Resources{
CPU: 250,
MemoryMB: 10,
DiskMB: 20,
Networks: []*structs.NetworkResource{},
},
}

_, handle, cleanup := dockerSetup(t, task)
defer cleanup()

select {
case res := <-handle.WaitCh():
if res.Successful() {
t.Fatalf("expected error, but container exited successfull")
}

if res.Err.Error() != "OOM Killed" {
t.Fatalf("not killed by OOM killer: %s", res.Err)
}

t.Logf("Successfully killed by OOM killer")

case <-time.After(time.Duration(tu.TestMultiplier()*5) * time.Second):
t.Fatalf("timeout")
}
}

0 comments on commit 11184c7

Please sign in to comment.