Skip to content
This repository has been archived by the owner on Feb 1, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1637 from jimenez/launch_bad_timeout
Browse files Browse the repository at this point in the history
Launch bad timeout
  • Loading branch information
vieux committed Jan 14, 2016
2 parents 08839f6 + a99ceeb commit f883a6d
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 16 deletions.
9 changes: 5 additions & 4 deletions cluster/mesos/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mesos

import (
"testing"
"time"

"github.com/docker/swarm/cluster"
"github.com/docker/swarm/cluster/mesos/task"
Expand Down Expand Up @@ -42,11 +43,11 @@ func TestAddTask(t *testing.T) {
assert.Empty(t, s.tasks)
assert.True(t, s.empty())

t1, err := task.NewTask(cluster.BuildContainerConfig(dockerclient.ContainerConfig{}), "task1")
t1, err := task.NewTask(cluster.BuildContainerConfig(dockerclient.ContainerConfig{}), "task1", 5*time.Second)
assert.NoError(t, err)
s.addTask(t1)

t2, err := task.NewTask(cluster.BuildContainerConfig(dockerclient.ContainerConfig{}), "task1")
t2, err := task.NewTask(cluster.BuildContainerConfig(dockerclient.ContainerConfig{}), "task1", 5*time.Second)
assert.NoError(t, err)
s.addTask(t2)
assert.Equal(t, len(s.tasks), 2)
Expand Down Expand Up @@ -80,11 +81,11 @@ func TestRemoveTask(t *testing.T) {

assert.Empty(t, s.tasks)

t1, err := task.NewTask(cluster.BuildContainerConfig(dockerclient.ContainerConfig{}), "task1")
t1, err := task.NewTask(cluster.BuildContainerConfig(dockerclient.ContainerConfig{}), "task1", 5*time.Second)
assert.NoError(t, err)
s.addTask(t1)

t2, err := task.NewTask(cluster.BuildContainerConfig(dockerclient.ContainerConfig{}), "task1")
t2, err := task.NewTask(cluster.BuildContainerConfig(dockerclient.ContainerConfig{}), "task1", 5*time.Second)
assert.NoError(t, err)
s.addTask(t2)
assert.Equal(t, len(s.tasks), 2)
Expand Down
6 changes: 2 additions & 4 deletions cluster/mesos/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (c *Cluster) CreateContainer(config *cluster.ContainerConfig, name string,
return nil, errResourcesNeeded
}

task, err := task.NewTask(config, name)
task, err := task.NewTask(config, name, c.taskCreationTimeout)
if err != nil {
return nil, err
}
Expand All @@ -194,10 +194,8 @@ func (c *Cluster) CreateContainer(config *cluster.ContainerConfig, name string,
case container := <-task.GetContainer():
return formatContainer(container), nil
case err := <-task.Error:
return nil, err
case <-time.After(c.taskCreationTimeout):
c.pendingTasks.Remove(task)
return nil, fmt.Errorf("container failed to start after %s", c.taskCreationTimeout)
return nil, err
}
}

Expand Down
13 changes: 12 additions & 1 deletion cluster/mesos/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strconv"
"strings"
"time"

log "github.com/Sirupsen/logrus"
"github.com/docker/docker/pkg/stringid"
Expand Down Expand Up @@ -162,7 +163,7 @@ func (t *Task) Build(slaveID string, offers map[string]*mesosproto.Offer) {
}

// NewTask fucntion creates a task
func NewTask(config *cluster.ContainerConfig, name string) (*Task, error) {
func NewTask(config *cluster.ContainerConfig, name string, timeout time.Duration) (*Task, error) {
id := stringid.TruncateID(stringid.GenerateRandomID())

if name != "" {
Expand All @@ -183,9 +184,19 @@ func NewTask(config *cluster.ContainerConfig, name string) (*Task, error) {
task.Name = &name
task.TaskId = &mesosproto.TaskID{Value: &id}
task.Labels = &mesosproto.Labels{Labels: []*mesosproto.Label{{Key: proto.String("SWARM_CONTAINER_NAME"), Value: &name}}}

go task.suicide(timeout)

return task, nil
}

func (t *Task) suicide(timeout time.Duration) {
<-time.After(timeout)
if !t.Stopped() && t.SlaveId == nil {
t.Error <- fmt.Errorf("container failed to start after %s", timeout)
}
}

// SendStatus method writes the task status in the updates channel
func (t *Task) SendStatus(status *mesosproto.TaskStatus) {
t.updates <- status
Expand Down
7 changes: 4 additions & 3 deletions cluster/mesos/task/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"sort"
"strings"
"testing"
"time"

"github.com/docker/swarm/cluster"
"github.com/mesos/mesos-go/mesosproto"
Expand All @@ -20,7 +21,7 @@ func TestBuild(t *testing.T) {
CpuShares: 42,
Memory: 2097152,
Cmd: []string{"ls", "foo", "bar"},
}), name)
}), name, 5*time.Second)
assert.NoError(t, err)

task.Build("slave-id", nil)
Expand All @@ -47,7 +48,7 @@ func TestBuild(t *testing.T) {
}

func TestNewTask(t *testing.T) {
task, err := NewTask(cluster.BuildContainerConfig(dockerclient.ContainerConfig{}), name)
task, err := NewTask(cluster.BuildContainerConfig(dockerclient.ContainerConfig{}), name, 5*time.Second)
assert.NoError(t, err)

assert.Equal(t, *task.Name, name)
Expand All @@ -56,7 +57,7 @@ func TestNewTask(t *testing.T) {
}

func TestSendGetStatus(t *testing.T) {
task, err := NewTask(cluster.BuildContainerConfig(dockerclient.ContainerConfig{}), "")
task, err := NewTask(cluster.BuildContainerConfig(dockerclient.ContainerConfig{}), "", 5*time.Second)
assert.NoError(t, err)

status := mesosutil.NewTaskStatus(nil, mesosproto.TaskState_TASK_RUNNING)
Expand Down
9 changes: 5 additions & 4 deletions cluster/mesos/task/tasks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package task

import (
"testing"
"time"

"github.com/docker/swarm/cluster"
"github.com/samalba/dockerclient"
Expand All @@ -25,14 +26,14 @@ func TestAdd(t *testing.T) {
CpuShares: 42,
Memory: 2097152,
Cmd: []string{"ls", "foo", "bar"},
}), "name1")
}), "name1", 5*time.Second)

task2, _ := NewTask(cluster.BuildContainerConfig(dockerclient.ContainerConfig{
Image: "test-image",
CpuShares: 42,
Memory: 2097152,
Cmd: []string{"ls", "foo", "bar"},
}), "name2")
}), "name2", 5*time.Second)
q.Add(task1)
assert.Equal(t, len(q.Tasks), 0)

Expand All @@ -48,7 +49,7 @@ func TestRemove(t *testing.T) {
CpuShares: 42,
Memory: 2097152,
Cmd: []string{"ls", "foo", "bar"},
}), "name1")
}), "name1", 5*time.Second)

q.Add(task1)
assert.Equal(t, len(q.Tasks), 1)
Expand All @@ -64,7 +65,7 @@ func TestProcess(t *testing.T) {
CpuShares: 42,
Memory: 2097152,
Cmd: []string{"ls", "foo", "bar"},
}), "name1")
}), "name1", 5*time.Second)

q.Add(task1)
assert.Equal(t, len(q.Tasks), 1)
Expand Down
16 changes: 16 additions & 0 deletions test/integration/mesos/api/run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,19 @@ function teardown() {
[ "$status" -ne 0 ]
[[ "${output}" == *'resources constraints (-c and/or -m) are required by mesos'* ]]
}

@test "mesos - docker run with long pull" {
start_docker 2
start_mesos
swarm_manage --cluster-driver mesos-experimental --cluster-opt mesos.tasktimeout=1s 127.0.0.1:$MESOS_MASTER_PORT

# make sure no container exist
run docker_swarm ps -qa
[ "${#lines[@]}" -eq 0 ]

# run
docker_swarm run -m 20m -d --name test_container busybox sleep 100

# verify, container is running
[ -n $(docker_swarm ps -q --filter=name=test_container --filter=status=running) ]
}

0 comments on commit f883a6d

Please sign in to comment.