mirrored from https://chromium.googlesource.com/infra/luci/luci-go
/
cls.go
228 lines (211 loc) · 6.92 KB
/
cls.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright 2021 The LUCI 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 triager
import (
"fmt"
"time"
"go.chromium.org/luci/cv/internal/changelist"
"go.chromium.org/luci/cv/internal/prjmanager/prjpb"
)
// triageCLs decides whether individual CLs ought to be acted upon.
func triageCLs(c *prjpb.Component, pm pmState) map[int64]*clInfo {
cls := make(map[int64]*clInfo, len(c.GetClids()))
for _, clid := range c.GetClids() {
cls[clid] = &clInfo{
pcl: pm.MustPCL(clid),
purgingCL: pm.PurgingCL(clid), // may be nil
}
}
for index, r := range c.GetPruns() {
for _, clid := range r.GetClids() {
info := cls[clid]
info.runIndexes = append(info.runIndexes, int32(index))
}
}
for _, info := range cls {
info.triage(pm)
}
return cls
}
// clInfo represents a CL in the PM component of CLs.
type clInfo struct {
pcl *prjpb.PCL
// runIndexes are indexes of Component.PRuns which references this CL.
runIndexes []int32
// purgingCL is set if CL is already being purged.
purgingCL *prjpb.PurgingCL
triagedCL
}
// lastTriggered returns the last triggered time among this CL and its triggered
// deps. Can be zero time.Time if neither are triggered.
func (c *clInfo) lastTriggered() time.Time {
thisPB := c.pcl.GetTrigger().GetTime()
switch {
case thisPB == nil && c.deps == nil:
return time.Time{}
case thisPB == nil:
return c.deps.lastTriggered
case c.deps == nil || c.deps.lastTriggered.IsZero():
return thisPB.AsTime()
default:
this := thisPB.AsTime()
if c.deps.lastTriggered.Before(this) {
return this
}
return c.deps.lastTriggered
}
}
// triagedCL is the result of CL triage (see clInfo.triage()).
//
// Note: doesn't take into account `combine_cls.stabilization_delay`,
// Thus a CL may be ready or with purgeReason but due to stabilization delay,
// it shouldn't be acted upon *yet*.
type triagedCL struct {
// deps are triaged deps, set only if CL is watched by exactly 1 config group.
// of the current project.
deps *triagedDeps
// purgeReasons is set if the CL ought to be purged.
//
// Not set is CL is .purgingCL is non-nil since CL is already being purged.
purgeReasons []*changelist.CLError
// ready is true if it can be used in creation of new Runs.
//
// If true, purgeReason must be nil, and deps must be OK though they may contain
// not not yet loaded deps.
ready bool
}
// triage sets triagedCL part of clInfo.
//
// Expects non-triagedCL part of clInfo to be arleady set.
// panics iff component is not in a valid state.
func (info *clInfo) triage(pm pmState) {
switch {
case len(info.runIndexes) > 0:
// Once CV supports API based triggering, a CL may be at the same time be in
// purged state && have an incomplete Run. The presence in a Run is more
// important, so treat as such.
info.triageInRun(pm)
case info.purgingCL != nil:
info.triageInPurge(pm)
default:
info.triageNew(pm)
}
}
func (info *clInfo) triageInRun(pm pmState) {
pcl := info.pcl
switch s := pcl.GetStatus(); {
case (false || // false is a noop for ease of reading
s == prjpb.PCL_DELETED ||
s == prjpb.PCL_UNWATCHED ||
s == prjpb.PCL_UNKNOWN ||
pcl.GetSubmitted() ||
pcl.GetTrigger() == nil):
// This is expected while Run is being finalized iff PM sees updates to a
// CL before OnRunFinished event.
return
case len(pcl.GetConfigGroupIndexes()) != 1:
// This is expected if project config has changed, but Run's reaction to it via
// OnRunFinished event hasn't yet reached PM.
return
case s != prjpb.PCL_OK:
panic(fmt.Errorf("PCL has unrecognized status %s", s))
}
cgIndex := pcl.GetConfigGroupIndexes()[0]
info.deps = triageDeps(pcl, cgIndex, pm)
// A purging CL must not be "ready" to avoid creating new Runs with them.
if info.deps.OK() && info.purgingCL == nil {
info.ready = true
}
}
func (info *clInfo) triageInPurge(pm pmState) {
// The PM hasn't noticed yet the completion of the async purge.
// The result of purging is modified CL, which may be observed by PM earlier
// than completion of purge.
//
// Thus, consider these CLs in potential Run Creation, but don't mark them
// ready in oder to avoid creating new Runs.
pcl := info.pcl
switch s := pcl.GetStatus(); s {
case prjpb.PCL_DELETED, prjpb.PCL_UNWATCHED, prjpb.PCL_UNKNOWN:
// Likely purging effect is already visible.
return
case prjpb.PCL_OK:
// OK.
default:
panic(fmt.Errorf("PCL has unrecognized status %s", s))
}
switch {
case pcl.GetSubmitted():
// Someone already submitted CL while purging was ongoing.
return
case pcl.GetTrigger() == nil:
// Likely purging effect is already visible.
return
}
cgIndexes := pcl.GetConfigGroupIndexes()
switch len(cgIndexes) {
case 0:
panic(fmt.Errorf("PCL %d without ConfigGroup index not possible for CL not referenced by any Runs (partitioning bug?)", pcl.GetClid()))
case 1:
info.deps = triageDeps(pcl, cgIndexes[0], pm)
// info.deps.OK() may be true, for example if user has already corrected the
// mistake that previously reulted in purging op. However, don't mark CL
// ready until purging op completes or expires.
}
}
func (info *clInfo) triageNew(pm pmState) {
pcl := info.pcl
clid := pcl.GetClid()
assumption := "not possible for CL not referenced by any Runs (partitioning bug?)"
switch s := pcl.GetStatus(); s {
case prjpb.PCL_DELETED, prjpb.PCL_UNWATCHED, prjpb.PCL_UNKNOWN:
panic(fmt.Errorf("PCL %d status %s %s", clid, s, assumption))
case prjpb.PCL_OK:
// OK.
default:
panic(fmt.Errorf("PCL has unrecognized status %s", s))
}
switch {
case pcl.GetSubmitted():
panic(fmt.Errorf("PCL %d submitted %s", clid, assumption))
case pcl.GetTrigger() == nil:
panic(fmt.Errorf("PCL %d not triggered %s", clid, assumption))
case len(pcl.GetErrors()) > 0:
info.purgeReasons = pcl.GetErrors()
return
}
cgIndexes := pcl.GetConfigGroupIndexes()
switch len(cgIndexes) {
case 0:
panic(fmt.Errorf("PCL %d without ConfigGroup index %s", clid, assumption))
case 1:
if info.deps = triageDeps(pcl, cgIndexes[0], pm); info.deps.OK() {
info.ready = true
} else {
info.purgeReasons = append(info.purgeReasons, info.deps.makePurgeReason())
}
default:
cgNames := make([]string, len(cgIndexes))
for i, idx := range cgIndexes {
cgNames[i] = pm.ConfigGroup(idx).ID.Name()
}
info.purgeReasons = append(info.purgeReasons, &changelist.CLError{
Kind: &changelist.CLError_WatchedByManyConfigGroups_{
WatchedByManyConfigGroups: &changelist.CLError_WatchedByManyConfigGroups{
ConfigGroups: cgNames,
},
},
})
}
}