-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroup_policy_update.go
More file actions
293 lines (245 loc) · 11.1 KB
/
group_policy_update.go
File metadata and controls
293 lines (245 loc) · 11.1 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
Copyright 2026 The ARCORIS 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 bufferpool
import "time"
// group_policy_update.go owns live PoolGroup policy publication. The flow is
// validate the candidate, admit the group runtime gate, plan group-to-partition
// budget publication, admit and lock children top-down, publish the group
// runtime snapshot, and then apply only prevalidated partition budget plans.
const (
// groupPolicyPublicationNoBudgetTarget reports a group policy publication
// that changed group policy state but carried no retained parent target to
// publish into child partitions.
groupPolicyPublicationNoBudgetTarget = "no_budget_target"
)
// PoolGroupPolicyPublicationResult reports one group-level live policy
// publication.
//
// PoolGroup policy publication is a foreground group control operation. It is
// serialized with hard Close by runtimeMu, preserves the current pressure
// signal, may publish a new immutable group runtime policy snapshot, and may
// publish retained-budget targets into owned PoolPartitions. It never scans Pool
// shards, never executes Pool trim, never calls Pool.Get or Pool.Put, and never
// computes class EWMA. Scheduler changes are owner-local and are applied after
// publication releases locks that scheduled TickInto may need.
type PoolGroupPolicyPublicationResult struct {
// PreviousGeneration is the group runtime-policy generation observed before
// the publication attempt.
PreviousGeneration Generation
// Generation is the group runtime-policy generation published by this
// attempt. It remains NoGeneration when validation, feasibility, lifecycle,
// or child precheck rejects the publication before the runtime snapshot
// changes.
Generation Generation
// Published reports whether the group accepted the candidate policy and all
// required child partition budget targets were accepted.
Published bool
// RuntimePublished reports whether a new immutable group runtime snapshot was
// stored.
RuntimePublished bool
// Contracted reports whether group retained-budget limits became more
// restrictive.
//
// Contraction lowers future partition targets only. It does not scan shards,
// trim Pool storage, or force active leases.
Contracted bool
// FailureReason is empty on success and contains stable diagnostic text on
// rejection or observable partial publication.
FailureReason string
// SchedulerChange reports the owner-local scheduler reconfiguration planned
// by this publication attempt. Stable values are "none", "enable",
// "disable", and "retime".
SchedulerChange string
// SchedulerPreviousEnabled reports whether the previous runtime policy had
// the group-level coordinator scheduler enabled.
SchedulerPreviousEnabled bool
// SchedulerNextEnabled reports whether the candidate runtime policy enables
// the group-level coordinator scheduler.
SchedulerNextEnabled bool
// SchedulerPreviousInterval is the previous scheduler cadence after policy
// normalization.
SchedulerPreviousInterval time.Duration
// SchedulerNextInterval is the candidate scheduler cadence after policy
// normalization.
SchedulerNextInterval time.Duration
// SchedulerApplied reports whether PublishPolicy mutated the scheduler
// runtime. It is false for no-change attempts because no runtime scheduler
// mutation was needed.
SchedulerApplied bool
// SchedulerFailureReason is empty unless owner-local scheduler
// reconfiguration failed. It uses stable policy_update_* vocabulary and does
// not expose raw error text.
SchedulerFailureReason string
// Diff describes which group policy sections changed.
Diff PolicyUpdateDiffDiagnostics
// PreviousPolicy is the normalized group policy observed before publication.
PreviousPolicy PoolGroupPolicy
// Policy is the normalized candidate group policy.
Policy PoolGroupPolicy
// BudgetPublication reports group-to-partition retained-budget feasibility
// and publication status.
BudgetPublication PoolGroupBudgetPublicationReport
// SkippedPartitions records child partitions that could not accept target
// publication.
SkippedPartitions []PoolGroupSkippedPartition
// PressurePreserved reports whether the pressure signal observed before
// publication was preserved in the new group runtime snapshot.
PressurePreserved bool
}
// PublishPolicy publishes a live PoolGroup runtime policy update.
//
// The operation validates the candidate policy, computes group-to-partition
// retained targets when a retained parent target exists, admits every target
// partition, and asks each partition to build a complete no-mutation
// application plan before the group runtime snapshot is stored. Only after those
// child plans and their Pool control gates are admitted does the group publish
// its own immutable runtime snapshot. Applying the prevalidated partition plans
// is expected to be no-fail for normal policy, feasibility, and lifecycle
// reasons.
//
// This ordering is owner-level and report-driven, not a distributed
// transaction: no rollback is attempted, and internal corruption still remains
// an internal invariant failure. The guarantee is narrower and intentional:
// ordinary validation, infeasible budget, closed-child, and Pool-control
// admission failures are detected before group runtime publication. Live
// scheduler enable, disable, and retime changes are planned with the policy
// update and applied after runtimeMu is released. Scheduler start and retime
// prepare their ticker before runtime publication; the prepared ticker is either
// transferred to the scheduler runtime on success or stopped on every later
// error path. If a scheduler apply failure appears after runtime publication,
// the report keeps RuntimePublished accurate and records stable scheduler
// diagnostics instead of attempting rollback.
func (g *PoolGroup) PublishPolicy(policy PoolGroupPolicy) (PoolGroupPolicyPublicationResult, error) {
g.mustBeInitialized()
// Validate the candidate before taking the group write gate so malformed
// policy values do not block unrelated foreground group work.
normalized := policy.Normalize()
initial := g.currentRuntimeSnapshot()
result := newPoolGroupPolicyPublicationResult(initial, normalized)
if err := validateGroupPolicyPublicationCandidate(normalized, &result); err != nil {
return result, err
}
g.schedulerReconfigMu.Lock()
defer g.schedulerReconfigMu.Unlock()
result, schedulerChange, preparedScheduler, err := g.publishPolicyAndPlanSchedulerReconfiguration(normalized)
if err != nil {
return result, err
}
if err := g.applyPublishedCoordinatorSchedulerPolicyChange(&result, schedulerChange, preparedScheduler); err != nil {
return result, err
}
return result, nil
}
func (g *PoolGroup) publishPolicyAndPlanSchedulerReconfiguration(
normalized PoolGroupPolicy,
) (
PoolGroupPolicyPublicationResult,
schedulerPolicyChange,
controllerSchedulerPreparedStart,
error,
) {
result := newPoolGroupPolicyPublicationResult(g.currentRuntimeSnapshot(), normalized)
// Serialize live publication with group Close, group TickInto, and other
// group-level runtime snapshot mutations. Scheduler Stop is intentionally
// applied later, after runtimeMu is released.
g.runtimeMu.Lock()
defer g.runtimeMu.Unlock()
if !g.lifecycle.AllowsWork() {
result.FailureReason = policyUpdateFailureClosed
return result, schedulerPolicyChange{}, controllerSchedulerPreparedStart{}, newError(ErrClosed, errGroupClosed)
}
plan, err := g.planGroupPolicyPublicationLocked(normalized, &result)
if err != nil {
g.releaseGroupPolicyPublicationPlan(&plan)
return result, schedulerPolicyChange{}, controllerSchedulerPreparedStart{}, err
}
defer g.releaseGroupPolicyPublicationPlan(&plan)
var preparedScheduler controllerSchedulerPreparedStart
preparedSchedulerOwned := false
if plan.schedulerChange.needsPreparedStart() {
var err error
preparedScheduler, err = g.prepareCoordinatorSchedulerStart(normalized.Coordinator)
if err != nil {
result.markSchedulerPolicyChangeFailed(plan.schedulerChange, policyUpdateFailureReasonForSchedulerChange(plan.schedulerChange))
return result, schedulerPolicyChange{}, controllerSchedulerPreparedStart{}, err
}
preparedSchedulerOwned = true
defer func() {
if preparedSchedulerOwned {
preparedScheduler.stop()
}
}()
}
if err := g.commitGroupPolicyPublicationLocked(normalized, &result, &plan); err != nil {
return result, schedulerPolicyChange{}, controllerSchedulerPreparedStart{}, err
}
preparedSchedulerOwned = false
return result, plan.schedulerChange, preparedScheduler, nil
}
// UpdatePolicy publishes policy and returns only the operation error.
func (g *PoolGroup) UpdatePolicy(policy PoolGroupPolicy) error {
_, err := g.PublishPolicy(policy)
return err
}
// newPoolGroupPolicyPublicationResult builds a stable report skeleton shared by
// successful and rejected publication paths.
func newPoolGroupPolicyPublicationResult(
runtime *groupRuntimeSnapshot,
candidate PoolGroupPolicy,
) PoolGroupPolicyPublicationResult {
var previousPolicy PoolGroupPolicy
var previousGeneration Generation
if runtime != nil {
previousPolicy = runtime.Policy.Normalize()
previousGeneration = runtime.Generation
}
diff := classifyGroupPolicyUpdate(previousPolicy, candidate)
result := PoolGroupPolicyPublicationResult{
PreviousGeneration: previousGeneration,
Contracted: diff.RetentionContracted,
Diff: diff,
PreviousPolicy: previousPolicy,
Policy: candidate.Normalize(),
}
result.setSchedulerPolicyChangeDiagnostics(
classifyGroupSchedulerPolicyChange(previousPolicy.Coordinator, result.Policy.Coordinator),
)
return result
}
// validateGroupPolicyPublicationCandidate validates the candidate before any
// group runtime mutation is allowed.
func validateGroupPolicyPublicationCandidate(
policy PoolGroupPolicy,
result *PoolGroupPolicyPublicationResult,
) error {
if err := policy.Validate(); err != nil {
result.FailureReason = policyUpdateFailureInvalid
return err
}
return nil
}
// classifyGroupPolicyUpdate maps group policy differences into shared
// publication diagnostics.
func classifyGroupPolicyUpdate(previous PoolGroupPolicy, next PoolGroupPolicy) PolicyUpdateDiffDiagnostics {
previous = previous.Normalize()
next = next.Normalize()
return PolicyUpdateDiffDiagnostics{
RetentionChanged: previous.Budget != next.Budget,
RetentionContracted: partitionBudgetContracts(previous.Budget, next.Budget),
RetentionExpanded: partitionBudgetExpands(previous.Budget, next.Budget),
PressureChanged: previous.Pressure != next.Pressure,
}
}
func groupPolicyHasRetainedTarget(policy PoolGroupPolicy) bool {
return !policy.Budget.MaxRetainedBytes.IsZero()
}