-
Notifications
You must be signed in to change notification settings - Fork 375
/
engine.go
164 lines (140 loc) · 4.48 KB
/
engine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (c) 2021 Terminus, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pipengine
import (
"context"
"math/rand"
"sync"
"time"
"github.com/sirupsen/logrus"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/modules/pipeline/conf"
"github.com/erda-project/erda/modules/pipeline/dbclient"
"github.com/erda-project/erda/modules/pipeline/pipengine/actionexecutor"
"github.com/erda-project/erda/modules/pipeline/pipengine/reconciler"
)
type Engine struct {
mgr *actionexecutor.Manager
dbClient *dbclient.Client
reconciler *reconciler.Reconciler
}
const logPrefixContinueLoading = "continue load running pipelines"
var once sync.Once
var e Engine
func New(
dbClient *dbclient.Client,
) *Engine {
e = Engine{
dbClient: dbClient,
}
return &e
}
func (engine *Engine) OnceDo(
r *reconciler.Reconciler,
) error {
var onceErr error
once.Do(func() {
//执行engine的一些初始化过程
_, cfgChan, err := engine.dbClient.ListPipelineConfigsOfActionExecutor()
if err != nil {
onceErr = err
}
mgr := actionexecutor.GetManager()
if err := mgr.Initialize(cfgChan); err != nil {
onceErr = err
return
}
engine.reconciler = r
engine.mgr = mgr
})
return onceErr
}
func (engine *Engine) StartReconciler(ctx context.Context) {
go engine.reconciler.Listen(ctx)
// 开始 Listen 后再开始加载已经在处理中的流水线,否则组件还未准备好,包括 eventManger(阻塞)
go func() {
time.Sleep(time.Second * 2)
if err := engine.loadRunningPipelines(); err != nil {
logrus.Errorf("engine: failed to load running pipelines, err: %v", err)
}
}()
}
func (engine *Engine) StartGC(ctx context.Context) {
go engine.reconciler.ListenGC(ctx)
go engine.reconciler.PipelineDatabaseGC(ctx)
go engine.reconciler.CompensateGCNamespaces(ctx)
}
func (engine *Engine) Send(pipelineID uint64) {
engine.reconciler.Add(pipelineID)
}
func (engine *Engine) WaitDBGC(pipelineID uint64, ttl uint64, needArchive bool) {
engine.reconciler.WaitDBGC(pipelineID, ttl, needArchive)
}
// loadRunningPipelines load running pipeline from db.
func (engine *Engine) loadRunningPipelines() error {
logrus.Infof("%s: begin load running pipelines", logPrefixContinueLoading)
pipelineIDs, err := engine.dbClient.ListPipelineIDsByStatuses(apistructs.ReconcilerRunningStatuses()...)
if err != nil {
return err
}
// send pipeline id by interval time instead of at once
total := len(pipelineIDs)
intervalSec := time.Duration(conf.InitializeSendRunningIntervalSec())
intervalNum := conf.InitializeSendRunningIntervalNum()
maxTimes := total / int(intervalNum)
for i := 0; i <= maxTimes; i++ {
time.Sleep(intervalSec)
end := (i + 1) * int(intervalNum)
if end > total {
end = total
}
for _, id := range pipelineIDs[i*int(intervalNum) : end] {
go func(pipelineID uint64) {
engine.Send(pipelineID)
logrus.Debugf("%s: load running pipeline success, pipelineID: %d", logPrefixContinueLoading, pipelineID)
}(id)
}
}
logrus.Infof("%s: pipengine end load running pipelines", logPrefixContinueLoading)
return nil
}
func (engine *Engine) continueLoadRunningPipelines() {
// 多实例,先等待随机时间
rand.Seed(time.Now().UnixNano())
randN := rand.Intn(60)
logrus.Debugf("%s: random sleep %d seconds...", logPrefixContinueLoading, randN)
time.Sleep(time.Duration(randN) * time.Second)
done := make(chan struct{})
errDone := make(chan error)
for {
go func() {
// 执行 loading
if err := engine.loadRunningPipelines(); err != nil {
errDone <- err
return
}
done <- struct{}{}
}()
select {
// 正常结束,等待 30min 后开始下一次处理
case <-done:
logrus.Infof("%s: sleep 30min for next loading...", logPrefixContinueLoading)
time.Sleep(time.Minute * 30)
// 异常结束,等待 2min 后尽快开始下一次处理
case err := <-errDone:
logrus.Errorf("%s: failed to load, wait 2min for next loading, err: %v", logPrefixContinueLoading, err)
time.Sleep(time.Minute * 2)
}
}
}