Skip to content

Commit bb1e51d

Browse files
committed
Merge branch 'refactor/timeout' into 'master'
[skip ci] completely remove timeout from config See merge request !141
2 parents 74208e1 + 31ebc2e commit bb1e51d

16 files changed

+57
-172
lines changed

cluster/calcium/build_image.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,7 @@ func (c *calcium) BuildImage(repository, version, uid, artifact string) (chan *t
176176

177177
log.Infof("Building image %v with artifact %v at %v:%v", tag, artifact, buildPodname, node.Name)
178178

179-
ctx, cancel := context.WithTimeout(context.Background(), c.config.Timeout.BuildImage)
180-
defer cancel()
181-
resp, err := node.Engine.ImageBuild(ctx, buildContext, buildOptions)
179+
resp, err := node.Engine.ImageBuild(context.Background(), buildContext, buildOptions)
182180
if err != nil {
183181
return ch, err
184182
}
@@ -200,11 +198,9 @@ func (c *calcium) BuildImage(repository, version, uid, artifact string) (chan *t
200198
ch <- message
201199
}
202200

203-
ctx, cancel := context.WithTimeout(context.Background(), c.config.Timeout.BuildImage)
204-
defer cancel()
205201
// About this "Khadgar", https://github.com/docker/docker/issues/10983#issuecomment-85892396
206202
// Just because Ben Schnetzer's cute Khadgar...
207-
rc, err := node.Engine.ImagePush(ctx, tag, enginetypes.ImagePushOptions{RegistryAuth: "Khadgar"})
203+
rc, err := node.Engine.ImagePush(context.Background(), tag, enginetypes.ImagePushOptions{RegistryAuth: "Khadgar"})
208204
if err != nil {
209205
ch <- makeErrorBuildImageMessage(err)
210206
return
@@ -229,9 +225,7 @@ func (c *calcium) BuildImage(repository, version, uid, artifact string) (chan *t
229225
// 事实上他不会跟cached pod一样
230226
// 一样就砍死
231227
go func() {
232-
ctx, cancel := context.WithTimeout(context.Background(), c.config.Timeout.BuildImage)
233-
defer cancel()
234-
_, err := node.Engine.ImageRemove(ctx, tag, enginetypes.ImageRemoveOptions{
228+
_, err := node.Engine.ImageRemove(context.Background(), tag, enginetypes.ImageRemoveOptions{
235229
Force: false,
236230
PruneChildren: true,
237231
})

cluster/calcium/build_image_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
func TestGetRandomNode(t *testing.T) {
1818
store := &mockstore.MockStore{}
1919
config := types.Config{}
20-
c := &calcium{store: store, config: config, scheduler: simplescheduler.New(), network: calico.New(config), source: gitlab.New(config)}
20+
c := &calcium{store: store, config: config, scheduler: simplescheduler.New(), network: calico.New(), source: gitlab.New(config)}
2121

2222
n1 := &types.Node{Name: "node1", Podname: "podname", Endpoint: "tcp://10.0.0.1:2376", CPU: types.CPUMap{"0": 10, "1": 10}, Available: true}
2323
n2 := &types.Node{Name: "node2", Podname: "podname", Endpoint: "tcp://10.0.0.2:2376", CPU: types.CPUMap{"0": 10, "1": 10}, Available: true}
@@ -34,7 +34,7 @@ func TestGetRandomNode(t *testing.T) {
3434
func TestGetRandomNodeFail(t *testing.T) {
3535
store := &mockstore.MockStore{}
3636
config := types.Config{}
37-
c := &calcium{store: store, config: config, scheduler: simplescheduler.New(), network: calico.New(config), source: gitlab.New(config)}
37+
c := &calcium{store: store, config: config, scheduler: simplescheduler.New(), network: calico.New(), source: gitlab.New(config)}
3838

3939
n1 := &types.Node{Name: "node1", Podname: "podname", Endpoint: "tcp://10.0.0.1:2376", CPU: types.CPUMap{"0": 10, "1": 10}, Available: false}
4040
n2 := &types.Node{Name: "node2", Podname: "podname", Endpoint: "tcp://10.0.0.2:2376", CPU: types.CPUMap{"0": 10, "1": 10}, Available: false}

cluster/calcium/cluster.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func New(config types.Config) (*calcium, error) {
4646
}
4747

4848
// set network
49-
titanium := calico.New(config)
49+
titanium := calico.New()
5050

5151
// set scm
5252
var scm source.Source

cluster/calcium/create_container.go

+10-26
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ func (c *calcium) createContainerWithMemoryPrior(specs types.Specs, opts *types.
8383

8484
func (c *calcium) removeMemoryPodFailedContainer(id string, node *types.Node, nodeInfo types.NodeInfo, opts *types.DeployOptions) {
8585
defer c.store.UpdateNodeMem(opts.Podname, nodeInfo.Name, opts.Memory, "+")
86-
ctx, cancel := context.WithTimeout(context.Background(), c.config.Timeout.CreateContainer)
87-
defer cancel()
88-
if err := node.Engine.ContainerRemove(ctx, id, enginetypes.ContainerRemoveOptions{}); err != nil {
86+
if err := node.Engine.ContainerRemove(context.Background(), id, enginetypes.ContainerRemoveOptions{}); err != nil {
8987
log.Errorf("[RemoveMemoryPodFailedContainer] Error during remove failed container %v", err)
9088
}
9189
}
@@ -127,9 +125,7 @@ func (c *calcium) doCreateContainerWithMemoryPrior(nodeInfo types.NodeInfo, spec
127125
}
128126

129127
//create container
130-
ctx, cancel := context.WithTimeout(context.Background(), c.config.Timeout.CreateContainer)
131-
defer cancel()
132-
container, err := node.Engine.ContainerCreate(ctx, config, hostConfig, networkConfig, containerName)
128+
container, err := node.Engine.ContainerCreate(context.Background(), config, hostConfig, networkConfig, containerName)
133129
if err != nil {
134130
log.Errorf("[CreateContainerWithMemoryPrior] Error during ContainerCreate, %v", err)
135131
ms[i].Error = err.Error()
@@ -165,9 +161,7 @@ func (c *calcium) doCreateContainerWithMemoryPrior(nodeInfo types.NodeInfo, spec
165161
continue
166162
}
167163
}
168-
ctxStart, cancelStart := context.WithTimeout(context.Background(), c.config.Timeout.CreateContainer)
169-
defer cancelStart()
170-
err = node.Engine.ContainerStart(ctxStart, container.ID, enginetypes.ContainerStartOptions{})
164+
err = node.Engine.ContainerStart(context.Background(), container.ID, enginetypes.ContainerStartOptions{})
171165
if err != nil {
172166
log.Errorf("[CreateContainerWithMemoryPrior] Error during ContainerStart, %v", err)
173167
ms[i].Error = err.Error()
@@ -178,9 +172,7 @@ func (c *calcium) doCreateContainerWithMemoryPrior(nodeInfo types.NodeInfo, spec
178172
// TODO
179173
// if network manager uses our own, then connect must be called after container starts
180174
// here
181-
ctxInspect, cancelInspect := context.WithTimeout(context.Background(), c.config.Timeout.CreateContainer)
182-
defer cancelInspect()
183-
info, err := node.Engine.ContainerInspect(ctxInspect, container.ID)
175+
info, err := node.Engine.ContainerInspect(context.Background(), container.ID)
184176
if err != nil {
185177
log.Errorf("[CreateContainerWithMemoryPrior] Error during ContainerInspect, %v", err)
186178
ms[i].Error = err.Error()
@@ -190,7 +182,7 @@ func (c *calcium) doCreateContainerWithMemoryPrior(nodeInfo types.NodeInfo, spec
190182
ms[i].ContainerID = info.ID
191183

192184
// after start
193-
if err := runExec(node.Engine, info, AFTER_START, c.config.Timeout.CreateContainer); err != nil {
185+
if err := runExec(node.Engine, info, AFTER_START); err != nil {
194186
log.Errorf("[CreateContainerWithMemoryPrior] Run exec at %s error: %v", AFTER_START, err)
195187
}
196188

@@ -259,9 +251,7 @@ func (c *calcium) createContainerWithCPUPrior(specs types.Specs, opts *types.Dep
259251

260252
func (c *calcium) removeCPUPodFailedContainer(id string, node *types.Node, quota types.CPUMap) {
261253
defer c.releaseQuota(node, quota)
262-
ctx, cancel := context.WithTimeout(context.Background(), c.config.Timeout.CreateContainer)
263-
defer cancel()
264-
if err := node.Engine.ContainerRemove(ctx, id, enginetypes.ContainerRemoveOptions{}); err != nil {
254+
if err := node.Engine.ContainerRemove(context.Background(), id, enginetypes.ContainerRemoveOptions{}); err != nil {
265255
log.Errorf("[RemoveCPUPodFailedContainer] Error during remove failed container %v", err)
266256
}
267257
}
@@ -301,9 +291,7 @@ func (c *calcium) doCreateContainerWithCPUPrior(nodeName string, cpuMap []types.
301291
}
302292

303293
// create container
304-
ctxCreate, cancelCreate := context.WithTimeout(context.Background(), c.config.Timeout.CreateContainer)
305-
defer cancelCreate()
306-
container, err := node.Engine.ContainerCreate(ctxCreate, config, hostConfig, networkConfig, containerName)
294+
container, err := node.Engine.ContainerCreate(context.Background(), config, hostConfig, networkConfig, containerName)
307295
if err != nil {
308296
log.Errorf("[CreateContainerWithCPUPrior] Error when creating container, %v", err)
309297
ms[i].Error = err.Error()
@@ -341,9 +329,7 @@ func (c *calcium) doCreateContainerWithCPUPrior(nodeName string, cpuMap []types.
341329
continue
342330
}
343331
}
344-
ctxStart, cancelStart := context.WithTimeout(context.Background(), c.config.Timeout.CreateContainer)
345-
defer cancelStart()
346-
err = node.Engine.ContainerStart(ctxStart, container.ID, enginetypes.ContainerStartOptions{})
332+
err = node.Engine.ContainerStart(context.Background(), container.ID, enginetypes.ContainerStartOptions{})
347333
if err != nil {
348334
log.Errorf("[CreateContainerWithCPUPrior] Error when starting container, %v", err)
349335
ms[i].Error = err.Error()
@@ -354,9 +340,7 @@ func (c *calcium) doCreateContainerWithCPUPrior(nodeName string, cpuMap []types.
354340
// TODO
355341
// if network manager uses our own, then connect must be called after container starts
356342
// here
357-
ctxInspect, cancelInspect := context.WithTimeout(context.Background(), c.config.Timeout.CreateContainer)
358-
defer cancelInspect()
359-
info, err := node.Engine.ContainerInspect(ctxInspect, container.ID)
343+
info, err := node.Engine.ContainerInspect(context.Background(), container.ID)
360344
if err != nil {
361345
log.Errorf("[CreateContainerWithCPUPrior] Error when inspecting container, %v", err)
362346
ms[i].Error = err.Error()
@@ -366,7 +350,7 @@ func (c *calcium) doCreateContainerWithCPUPrior(nodeName string, cpuMap []types.
366350
ms[i].ContainerID = info.ID
367351

368352
// after start
369-
if err := runExec(node.Engine, info, AFTER_START, c.config.Timeout.CreateContainer); err != nil {
353+
if err := runExec(node.Engine, info, AFTER_START); err != nil {
370354
log.Errorf("[CreateContainerWithCPUPrior] Run exec at %s error: %v", AFTER_START, err)
371355
}
372356

cluster/calcium/helper.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"os"
99
"path/filepath"
1010
"strings"
11-
"time"
1211

1312
log "github.com/Sirupsen/logrus"
1413
enginetypes "github.com/docker/docker/api/types"
@@ -188,7 +187,7 @@ func makeMountPaths(specs types.Specs, config types.Config) ([]string, map[strin
188187

189188
// 跑存在labels里的exec
190189
// 为什么要存labels呢, 因为下线容器的时候根本不知道entrypoint是啥
191-
func runExec(client *engineapi.Client, container enginetypes.ContainerJSON, label string, timeout time.Duration) error {
190+
func runExec(client *engineapi.Client, container enginetypes.ContainerJSON, label string) error {
192191
cmd, ok := container.Config.Labels[label]
193192
if !ok || cmd == "" {
194193
log.Debugf("No %s found in container %s", label, container.ID)
@@ -197,14 +196,10 @@ func runExec(client *engineapi.Client, container enginetypes.ContainerJSON, labe
197196

198197
cmds := utils.MakeCommandLineArgs(cmd)
199198
execConfig := enginetypes.ExecConfig{User: container.Config.User, Cmd: cmds}
200-
ctxExec, cancelCreate := context.WithTimeout(context.Background(), timeout)
201-
defer cancelCreate()
202-
resp, err := client.ContainerExecCreate(ctxExec, container.ID, execConfig)
199+
resp, err := client.ContainerExecCreate(context.Background(), container.ID, execConfig)
203200
if err != nil {
204201
log.Errorf("Error during runExec: %v", err)
205202
return err
206203
}
207-
ctxStart, cancel := context.WithTimeout(context.Background(), timeout)
208-
defer cancel()
209-
return client.ContainerExecStart(ctxStart, resp.ID, enginetypes.ExecStartCheck{})
204+
return client.ContainerExecStart(context.Background(), resp.ID, enginetypes.ExecStartCheck{})
210205
}

cluster/calcium/image.go

+4-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"strings"
66
"sync"
7-
"time"
87

98
log "github.com/Sirupsen/logrus"
109
enginetypes "github.com/docker/docker/api/types"
@@ -63,7 +62,7 @@ func (c *calcium) cleanImage(podname, image string) error {
6362
wg.Add(1)
6463
go func(node *types.Node) {
6564
defer wg.Done()
66-
if err := cleanImageOnNode(node, image, c.config.ImageCache, c.config.Timeout.RemoveImage); err != nil {
65+
if err := cleanImageOnNode(node, image, c.config.ImageCache); err != nil {
6766
log.Errorf("cleanImageOnNode error: %s", err)
6867
}
6968
}(node)
@@ -90,14 +89,12 @@ func (x imageList) Less(i, j int) bool { return x[i].Created > x[j].Created }
9089
// 清理一个node上的这个image
9190
// 只清理同名字不同tag的
9291
// 并且保留最新的两个
93-
func cleanImageOnNode(node *types.Node, image string, count int, timeout time.Duration) error {
92+
func cleanImageOnNode(node *types.Node, image string, count int) error {
9493
log.Debugf("[cleanImageOnNode] node: %s, image: %s", node.Name, strings.Split(image, ":")[0])
9594
imgListFilter := filters.NewArgs()
9695
image = normalizeImage(image)
9796
imgListFilter.Add("reference", image) // 相同repo的image
98-
ctxList, cancelList := context.WithTimeout(context.Background(), timeout)
99-
defer cancelList()
100-
images, err := node.Engine.ImageList(ctxList, enginetypes.ImageListOptions{Filters: imgListFilter})
97+
images, err := node.Engine.ImageList(context.Background(), enginetypes.ImageListOptions{Filters: imgListFilter})
10198
if err != nil {
10299
return err
103100
}
@@ -110,15 +107,13 @@ func cleanImageOnNode(node *types.Node, image string, count int, timeout time.Du
110107
log.Debugf("Delete Images: %v", images)
111108

112109
for _, image := range images {
113-
ctx, cancel := context.WithTimeout(context.Background(), timeout)
114-
_, err := node.Engine.ImageRemove(ctx, image.ID, enginetypes.ImageRemoveOptions{
110+
_, err := node.Engine.ImageRemove(context.Background(), image.ID, enginetypes.ImageRemoveOptions{
115111
Force: false,
116112
PruneChildren: true,
117113
})
118114
if err != nil {
119115
log.Errorf("[cleanImageOnNode] Node %s ImageRemove error: %s, imageID: %s", node.Name, err, image.ID)
120116
}
121-
cancel()
122117
}
123118
return nil
124119
}

cluster/calcium/meta_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
func TestListPods(t *testing.T) {
1616
store := &mockstore.MockStore{}
1717
config := types.Config{}
18-
c := &calcium{store: store, config: config, scheduler: simplescheduler.New(), network: calico.New(config), source: gitlab.New(config)}
18+
c := &calcium{store: store, config: config, scheduler: simplescheduler.New(), network: calico.New(), source: gitlab.New(config)}
1919

2020
store.On("GetAllPods").Return([]*types.Pod{
2121
&types.Pod{Name: "pod1", Desc: "desc1"},
@@ -36,7 +36,7 @@ func TestListPods(t *testing.T) {
3636
func TestAddPod(t *testing.T) {
3737
store := &mockstore.MockStore{}
3838
config := types.Config{}
39-
c := &calcium{store: store, config: config, scheduler: simplescheduler.New(), network: calico.New(config), source: gitlab.New(config)}
39+
c := &calcium{store: store, config: config, scheduler: simplescheduler.New(), network: calico.New(), source: gitlab.New(config)}
4040

4141
store.On("AddPod", "pod1", "", "desc1").Return(&types.Pod{Name: "pod1", Favor: "MEM", Desc: "desc1"}, nil)
4242
store.On("AddPod", "pod2", "", "desc2").Return(nil, fmt.Errorf("Etcd Error"))
@@ -55,7 +55,7 @@ func TestAddPod(t *testing.T) {
5555
func TestGetPods(t *testing.T) {
5656
store := &mockstore.MockStore{}
5757
config := types.Config{}
58-
c := &calcium{store: store, config: config, scheduler: simplescheduler.New(), network: calico.New(config), source: gitlab.New(config)}
58+
c := &calcium{store: store, config: config, scheduler: simplescheduler.New(), network: calico.New(), source: gitlab.New(config)}
5959

6060
store.On("GetPod", "pod1").Return(&types.Pod{Name: "pod1", Desc: "desc1"}, nil).Once()
6161
store.On("GetPod", "pod2").Return(nil, fmt.Errorf("Not found")).Once()

cluster/calcium/mock_test.go

+5-9
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,15 @@ var (
4141
Hub: "hub.testhub.com",
4242
HubPrefix: "apps",
4343
},
44-
Timeout: coretypes.TimeoutConfig{
45-
Common: 3,
46-
},
4744
Scheduler: coretypes.SchedConfig{
4845
ShareBase: 10,
4946
MaxShare: -1,
5047
},
5148
}
52-
mockc *calcium
53-
mockStore *mockstore.MockStore
54-
err error
55-
mockTimeoutError bool
56-
specs = coretypes.Specs{
49+
mockc *calcium
50+
mockStore *mockstore.MockStore
51+
err error
52+
specs = coretypes.Specs{
5753
Appname: "root",
5854
Entrypoints: map[string]coretypes.Entrypoint{
5955
"test": coretypes.Entrypoint{
@@ -291,7 +287,7 @@ func (tf transportFunc) RoundTrip(req *http.Request) (*http.Response, error) {
291287
}
292288

293289
func initMockConfig() {
294-
mockc, err = New(utils.SetTimeout(config))
290+
mockc, err = New(config)
295291
if err != nil {
296292
panic(err)
297293
}

cluster/calcium/realloc.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"sync"
7-
"time"
87

98
log "github.com/Sirupsen/logrus"
109
enginecontainer "github.com/docker/docker/api/types/container"
@@ -149,7 +148,7 @@ func (c *calcium) doUpdateContainerWithMemoryPrior(
149148
// CPUQuota not cpu
150149
newResource := c.makeMemoryPriorSetting(newMemory, float64(newCPUQuota)/float64(utils.CpuPeriodBase))
151150
updateConfig := enginecontainer.UpdateConfig{Resources: newResource}
152-
if err := reSetContainer(containerJSON.ID, node, updateConfig, c.config.Timeout.Realloc); err != nil {
151+
if err := reSetContainer(containerJSON.ID, node, updateConfig); err != nil {
153152
log.Errorf("[realloc] update container failed %v, %s", err, containerJSON.ID)
154153
ch <- &types.ReallocResourceMessage{ContainerID: containerJSON.ID, Success: false}
155154
// 如果是增加内存,失败的时候应该把内存还回去
@@ -258,7 +257,7 @@ func (c *calcium) doReallocContainersWithCPUPrior(
258257
for index, container := range containers {
259258
resource := c.makeCPUPriorSetting(cpuset[index])
260259
updateConfig := enginecontainer.UpdateConfig{Resources: resource}
261-
if err := reSetContainer(container.ID, node, updateConfig, c.config.Timeout.Realloc); err != nil {
260+
if err := reSetContainer(container.ID, node, updateConfig); err != nil {
262261
log.Errorf("[realloc] update container failed %v", err)
263262
// TODO 这里理论上是可以恢复 CPU 占用表的,一来我们知道新的占用是怎样,二来我们也晓得老的占用是啥样
264263
ch <- &types.ReallocResourceMessage{ContainerID: container.ID, Success: false}
@@ -268,9 +267,7 @@ func (c *calcium) doReallocContainersWithCPUPrior(
268267
}
269268
}
270269

271-
func reSetContainer(ID string, node *types.Node, config enginecontainer.UpdateConfig, timeout time.Duration) error {
272-
ctx, cancel := context.WithTimeout(context.Background(), timeout)
273-
defer cancel()
274-
_, err := node.Engine.ContainerUpdate(ctx, ID, config)
270+
func reSetContainer(ID string, node *types.Node, config enginecontainer.UpdateConfig) error {
271+
_, err := node.Engine.ContainerUpdate(context.Background(), ID, config)
275272
return err
276273
}

cluster/calcium/remove_container.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"sync"
7+
"time"
78

89
log "github.com/Sirupsen/logrus"
910
enginetypes "github.com/docker/docker/api/types"
@@ -141,12 +142,13 @@ func (c *calcium) removeOneContainer(container *types.Container, info enginetype
141142
}()
142143

143144
// before stop
144-
if err := runExec(container.Engine, info, BEFORE_STOP, c.config.Timeout.RemoveContainer); err != nil {
145+
if err := runExec(container.Engine, info, BEFORE_STOP); err != nil {
145146
log.Errorf("Run exec at %s error: %s", BEFORE_STOP, err.Error())
146147
}
147148

148-
// FUXK docker
149-
err = container.Engine.ContainerStop(context.Background(), info.ID, &c.config.Timeout.RemoveContainer)
149+
// 一分钟还停不下来的话, 就好自为之吧
150+
timeout := time.Minute * 2
151+
err = container.Engine.ContainerStop(context.Background(), info.ID, &timeout)
150152
if err != nil {
151153
log.Errorf("Error during ContainerStop: %s", err.Error())
152154
return err
@@ -156,9 +158,7 @@ func (c *calcium) removeOneContainer(container *types.Container, info enginetype
156158
RemoveVolumes: true,
157159
Force: true,
158160
}
159-
ctxRemove, cancelRemove := context.WithTimeout(context.Background(), c.config.Timeout.RemoveContainer)
160-
defer cancelRemove()
161-
err = container.Engine.ContainerRemove(ctxRemove, info.ID, rmOpts)
161+
err = container.Engine.ContainerRemove(context.Background(), info.ID, rmOpts)
162162
if err != nil {
163163
log.Errorf("Error during ContainerRemove: %s", err.Error())
164164
return err

0 commit comments

Comments
 (0)