-
Notifications
You must be signed in to change notification settings - Fork 141
/
controller.go
173 lines (151 loc) · 4.98 KB
/
controller.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
165
166
167
168
169
170
171
172
173
// Copyright 2021 The PipeCD Authors.
//
// 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 deploymentchaincontroller
import (
"context"
"fmt"
"sync"
"time"
"go.uber.org/zap"
"github.com/pipe-cd/pipecd/pkg/datastore"
"github.com/pipe-cd/pipecd/pkg/model"
)
const (
// syncDeploymentChainsInterval represents time to sync state for all controlling deployment chains.
syncDeploymentChainsInterval = 15 * time.Second
// syncUpdatersInterval represents time to update list of controlling deployment chain updaters.
syncUpdatersInterval = time.Minute
// updaterWorkerNum represents the maximum number of updaters which can be
// run at the same time.
maxUpdaterWorkerNum = 10
)
type DeploymentChainController struct {
applicationStore datastore.ApplicationStore
deploymentStore datastore.DeploymentStore
deploymentChainStore datastore.DeploymentChainStore
// Map from deployment chain ID to the updater
// who in charge for the deployment chain updating.
updaters map[string]*updater
logger *zap.Logger
}
func NewDeploymentChainController(
ds datastore.DataStore,
logger *zap.Logger,
) *DeploymentChainController {
return &DeploymentChainController{
applicationStore: datastore.NewApplicationStore(ds),
deploymentStore: datastore.NewDeploymentStore(ds),
deploymentChainStore: datastore.NewDeploymentChainStore(ds),
updaters: make(map[string]*updater),
logger: logger.Named("deployment-chain-controller"),
}
}
func (d *DeploymentChainController) Run(ctx context.Context) error {
syncUpdatersTicker := time.NewTicker(syncUpdatersInterval)
defer syncUpdatersTicker.Stop()
syncDeploymentChainsTicker := time.NewTicker(syncDeploymentChainsInterval)
defer syncDeploymentChainsTicker.Stop()
d.logger.Info("start running deployment chain controller")
defer d.logger.Info("deployment chain controller has been stopped")
for {
select {
case <-ctx.Done():
return nil
case <-syncUpdatersTicker.C:
if err := d.syncUpdaters(ctx); err != nil {
d.logger.Error("failed while sync controller updaters", zap.Error(err))
}
case <-syncDeploymentChainsTicker.C:
if err := d.syncDeploymentChains(ctx); err != nil {
d.logger.Error("failed while sync deployment chains", zap.Error(err))
}
}
}
}
func (d *DeploymentChainController) syncUpdaters(ctx context.Context) error {
// Remove done updater of completed deployment chain.
for id, u := range d.updaters {
if !u.IsDone() {
continue
}
d.logger.Info("remove done updater of deployment chain",
zap.String("id", id),
zap.Time("completed_at", u.doneTimestamp),
)
delete(d.updaters, id)
}
// Find all not completed deployment chains and create updater if does not exist.
notCompletedChains, err := listNotCompletedDeploymentChain(ctx, d.deploymentChainStore)
if err != nil {
d.logger.Error("failed to fetch all not completed deployment chain", zap.Error(err))
return err
}
for _, c := range notCompletedChains {
// Ignore in case there is updater for that deployment chain existed.
if _, ok := d.updaters[c.Id]; ok {
continue
}
d.updaters[c.Id] = newUpdater(
c,
d.applicationStore,
d.deploymentStore,
d.deploymentChainStore,
d.logger,
)
}
return nil
}
func (d *DeploymentChainController) syncDeploymentChains(ctx context.Context) error {
var (
updatersNum = len(d.updaters)
updaterCh = make(chan *updater, updatersNum)
wg sync.WaitGroup
)
updaterWorkerNum := maxUpdaterWorkerNum
if updaterWorkerNum > updatersNum {
updaterWorkerNum = updatersNum
}
d.logger.Info(fmt.Sprintf("there are %d running deployment chain updaters", updatersNum))
for w := 0; w < updaterWorkerNum; w++ {
wg.Add(1)
go func(wid int) {
d.logger.Info(fmt.Sprintf("worker id (%d) is handling deployment chain updaters", wid))
defer wg.Done()
for updater := range updaterCh {
updater.Run(ctx)
}
d.logger.Info(fmt.Sprintf("worker id (%d) has stopped", wid))
}(w)
}
for chainID := range d.updaters {
updaterCh <- d.updaters[chainID]
}
close(updaterCh)
d.logger.Info("waiting for all updaters to finish")
wg.Wait()
return nil
}
func listNotCompletedDeploymentChain(ctx context.Context, dcs datastore.DeploymentChainStore) ([]*model.DeploymentChain, error) {
opts := datastore.ListOptions{
Filters: []datastore.ListFilter{
{
Field: "Status",
Operator: datastore.OperatorIn,
Value: model.GetNotCompletedDeploymentChainStatuses(),
},
},
}
chains, _, err := dcs.ListDeploymentChains(ctx, opts)
return chains, err
}