Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
6cd4f07
Create a maximum revision GC setting
whaught Jul 13, 2020
c08705a
update yaml
whaught Jul 13, 2020
c6fc366
Use the new GC settings
whaught Jul 14, 2020
d09b112
Merge remote-tracking branch 'upstream/master' into use-gc-settings
whaught Jul 14, 2020
0a65256
actually use the new settings
whaught Jul 14, 2020
aab20cf
simplify
whaught Jul 14, 2020
2a7afbb
now it compiles
whaught Jul 14, 2020
3aebf24
Merge remote-tracking branch 'upstream/master' into use-gc-settings
whaught Jul 14, 2020
16d6668
fix config references
whaught Jul 14, 2020
562512a
test with min settings
whaught Jul 15, 2020
639a4fc
max tests
whaught Jul 15, 2020
b2969c6
Merge remote-tracking branch 'upstream/master' into use-gc-settings
whaught Jul 15, 2020
668dd00
change name of max to max-non-active
whaught Jul 15, 2020
80d7b90
comments and validate positive
whaught Jul 15, 2020
ab2ea1c
parse forever constant
whaught Jul 15, 2020
6066241
fixing the unit test, better examples
whaught Jul 15, 2020
e3343a2
include disabled setinel
whaught Jul 15, 2020
2c1334f
merge in settings
whaught Jul 15, 2020
4393e7e
Merge remote-tracking branch 'upstream/master' into use-gc-settings
whaught Jul 15, 2020
2ac77b3
disabled logic
whaught Jul 15, 2020
a3da2ff
update comments
whaught Jul 15, 2020
53302c5
review suggestions
whaught Jul 16, 2020
0259af4
fix const
whaught Jul 17, 2020
9f8da0f
update with logging changes
whaught Jul 17, 2020
645743f
Merge remote-tracking branch 'upstream/master' into use-gc-settings
whaught Jul 17, 2020
70d3b34
fix unit test
whaught Jul 17, 2020
c838773
comment settings
whaught Jul 17, 2020
e4e0d7a
unit test update
whaught Jul 17, 2020
b3b262a
Merge remote-tracking branch 'upstream/master' into use-gc-settings
whaught Jul 17, 2020
65aad63
nits
whaught Jul 17, 2020
de048b5
fix unit test names
whaught Jul 17, 2020
d7297ec
more consistent name
whaught Jul 17, 2020
e994502
fix max boundary
whaught Jul 17, 2020
b1d34df
nit
whaught Jul 18, 2020
5f1fc4c
remove ref to lastpinned
whaught Jul 18, 2020
220ca35
Merge remote-tracking branch 'upstream/master' into use-gc-settings
whaught Jul 20, 2020
0085ef8
review suggestions
whaught Jul 20, 2020
ec96b4b
fix sign
whaught Jul 20, 2020
4331669
use nonactive as min
whaught Jul 21, 2020
f29b422
first filter out active
whaught Jul 21, 2020
f0264d7
comment nit on config
whaught Jul 21, 2020
5a7c70b
swap if/else ordering
whaught Jul 21, 2020
345e7ec
simplify
whaught Jul 21, 2020
0204045
whitespace
whaught Jul 21, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/gc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type Config struct {
// and exempt from GC.Note that GCMaxStaleRevision may override this if set.
// Set Disabled (-1) to disable/ignore duration and always consider active.
RetainSinceLastActiveTime time.Duration
// Minimum number of stale revisions to keep before considering for GC.
// Minimum number of non-active revisions to keep before considering for GC.
MinNonActiveRevisions int64
// Maximum number of non-active revisions to keep before considering for GC.
// regardless of creation or staleness time-bounds.
Expand Down
20 changes: 14 additions & 6 deletions pkg/reconciler/gc/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
v1 "knative.dev/serving/pkg/apis/serving/v1"
servingclient "knative.dev/serving/pkg/client/injection/client/fake"
configreconciler "knative.dev/serving/pkg/client/injection/reconciler/serving/v1/configuration"
"knative.dev/serving/pkg/gc"
gcconfig "knative.dev/serving/pkg/gc"
"knative.dev/serving/pkg/reconciler/configuration/resources"
"knative.dev/serving/pkg/reconciler/gc/config"
Expand Down Expand Up @@ -69,9 +70,16 @@ func TestGCReconcile(t *testing.T) {
ConfigStore: &testConfigStore{
config: &config.Config{
RevisionGC: &gcconfig.Config{
// v1 settings
StaleRevisionCreateDelay: 5 * time.Minute,
StaleRevisionTimeout: 5 * time.Minute,
StaleRevisionMinimumGenerations: 2,

// v2 settings
RetainSinceCreateTime: 5 * time.Minute,
RetainSinceLastActiveTime: 5 * time.Minute,
MinNonActiveRevisions: 1,
MaxNonActiveRevisions: gc.Disabled,
},
},
}}
Expand All @@ -86,16 +94,16 @@ func TestGCReconcile(t *testing.T) {
WithConfigObservedGen),
rev("keep-two", "foo", 5554, MarkRevisionReady,
WithRevName("5554"),
WithCreationTimestamp(oldest),
WithLastPinned(oldest)),
WithRoutingState(v1.RoutingStateReserve),
WithRoutingStateModified(oldest)),
rev("keep-two", "foo", 5555, MarkRevisionReady,
WithRevName("5555"),
WithCreationTimestamp(older),
WithLastPinned(older)),
WithRoutingState(v1.RoutingStateReserve),
WithRoutingStateModified(older)),
rev("keep-two", "foo", 5556, MarkRevisionReady,
WithRevName("5556"),
WithCreationTimestamp(old),
WithLastPinned(old)),
WithRoutingState(v1.RoutingStateActive),
WithRoutingStateModified(old)),
},
WantDeletes: []clientgotesting.DeleteActionImpl{{
ActionImpl: clientgotesting.ActionImpl{
Expand Down
104 changes: 70 additions & 34 deletions pkg/reconciler/gc/v2/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
v1 "knative.dev/serving/pkg/apis/serving/v1"
clientset "knative.dev/serving/pkg/client/clientset/versioned"
listers "knative.dev/serving/pkg/client/listers/serving/v1"
"knative.dev/serving/pkg/gc"
configns "knative.dev/serving/pkg/reconciler/gc/config"
)

Expand All @@ -49,63 +50,98 @@ func Collect(
return err
}

gcSkipOffset := cfg.StaleRevisionMinimumGenerations

if gcSkipOffset >= int64(len(revs)) {
min, max := int(cfg.MinNonActiveRevisions), int(cfg.MaxNonActiveRevisions)
if len(revs) <= min ||
max == gc.Disabled && cfg.RetainSinceCreateTime == gc.Disabled && cfg.RetainSinceLastActiveTime == gc.Disabled {
return nil
}

// Sort by last active descending
// Filter out active revs
revs = nonactiveRevisions(revs, config)

// Sort by last active ascending (oldest first)
sort.Slice(revs, func(i, j int) bool {
a, b := revisionLastActiveTime(revs[i]), revisionLastActiveTime(revs[j])
return a.After(b)
return a.Before(b)
})

for _, rev := range revs[gcSkipOffset:] {
if strings.EqualFold(rev.ObjectMeta.Annotations[serving.RevisionPreservedAnnotationKey], "true") {
// Delete stale revisions while more than min remain
remaining := len(revs)
nonstale := make([]*v1.Revision, 0, remaining)
for _, rev := range revs {
switch {
case remaining <= min:
return nil

case !isRevisionStale(cfg, rev, logger):
nonstale = append(nonstale, rev)
continue
}
if isRevisionStale(ctx, rev, config) {
err := client.ServingV1().Revisions(rev.Namespace).Delete(rev.Name, &metav1.DeleteOptions{})
if err != nil {
logger.With(zap.Error(err)).Errorf("Failed to delete stale revision %q", rev.Name)
continue

default:
remaining--
logger.Info("Deleting stale revision: ", rev.ObjectMeta.Name)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should tweak the name of MinNonActiveRevisions and MaxNonActiveRevisions config options for more clarity

serving/pkg/gc/config.go

Lines 58 to 63 in d86ab4a

// Minimum number of stale revisions to keep before considering for GC.
MinNonActiveRevisions int64
// Maximum number of non-active revisions to keep before considering for GC.
// regardless of creation or staleness time-bounds.
// Set Disabled (-1) to disable/ignore max.
MaxNonActiveRevisions int64

Since depending on how the revisions are processes you'd get different gc results

For example (with min 20 & max 1000):
A list composed of 20 stale revisions and then 100 non-active would cause all non-active revisions to be deleted
compared to
A list composed of 100 non-active revisions and then 20 stale revisions would delete nothing

Unsure what we want

Copy link
Copy Markdown
Contributor Author

@whaught whaught Jul 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I've documented that better here:

# ---------------------------------------

and need to carry that across to the golang comment. Note that all stale revisions are also non-active. In your example I think we'd expect 20 to be deleted and the remaining to stick around until they also go stale. However if you disable the time bounds for staleness, we keep growing the revision list to the max. That allows you to either have a hard-count of how many we keep around or a less deterministic time-based approach that can still have a ceiling. Or disable everything!

Copy link
Copy Markdown
Contributor Author

@whaught whaught Jul 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I misunderstood what you were saying. The former scenario would be unexpected because the revisions are first sorted by their last active time, but we do want the latter scenario to delete the 20 stale revisions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the code to consider the latter scenario regardless of the sort assumption. I think it's a little more readable too.

if err := client.ServingV1().Revisions(rev.Namespace).Delete(rev.Name, &metav1.DeleteOptions{}); err != nil {
logger.With(zap.Error(err)).Error("Failed to GC revision: ", rev.Name)
}
}
}

if max == gc.Disabled || len(nonstale) <= max {
return nil
}

// Delete extra revisions past max
for _, rev := range nonstale[:len(nonstale)-max] {
logger.Infof("Maximum(%d) reached. Deleting oldest non-active revision %q", max, rev.ObjectMeta.Name)
if err := client.ServingV1().Revisions(rev.Namespace).Delete(rev.Name, &metav1.DeleteOptions{}); err != nil {
logger.With(zap.Error(err)).Error("Failed to GC revision: ", rev.Name)
}
}
return nil
}

func isRevisionStale(ctx context.Context, rev *v1.Revision, config *v1.Configuration) bool {
func nonactiveRevisions(revs []*v1.Revision, config *v1.Configuration) []*v1.Revision {
nonactive := make([]*v1.Revision, 0, len(revs))
for _, rev := range revs {
if !isRevisionActive(rev, config) {
nonactive = append(nonactive, rev)
}
}
return nonactive
}

func isRevisionActive(rev *v1.Revision, config *v1.Configuration) bool {
if config.Status.LatestReadyRevisionName == rev.Name {
return false
return true // never delete latest ready, even if config is not active.
}
cfg := configns.FromContext(ctx).RevisionGC
logger := logging.FromContext(ctx)
curTime := time.Now()
createTime := rev.ObjectMeta.CreationTimestamp

if createTime.Add(cfg.StaleRevisionCreateDelay).After(curTime) {
// Revision was created sooner than staleRevisionCreateDelay. Ignore it.
return false
if strings.EqualFold(rev.Annotations[serving.RevisionPreservedAnnotationKey], "true") {
return true
}
// Anything that the labeler hasn't explicitly labelled as inactive.
// Revisions which do not yet have any annotation are not eligible for deletion.
return rev.GetRoutingState() != v1.RoutingStateReserve
}

lastActive := revisionLastActiveTime(rev)
func isRevisionStale(cfg *gc.Config, rev *v1.Revision, logger *zap.SugaredLogger) bool {
sinceCreate, sinceActive := cfg.RetainSinceCreateTime, cfg.RetainSinceLastActiveTime
if sinceCreate == gc.Disabled && sinceActive == gc.Disabled {
return false // Time checks are both disabled. Not stale.
}

// TODO(whaught): this is carried over from v1, but I'm not sure why we can't delete a ready revision
// that isn't referenced? Maybe because of labeler failure - can we replace this with 'pending' routing state check?
if lastActive.Equal(createTime.Time) {
// Revision was never active and it's not ready after staleRevisionCreateDelay.
// It usually happens when ksvc was deployed with wrong configuration.
return !rev.Status.GetCondition(v1.RevisionConditionReady).IsTrue()
createTime := rev.ObjectMeta.CreationTimestamp.Time
if sinceCreate != gc.Disabled && time.Since(createTime) < sinceCreate {
return false // Revision was created sooner than RetainSinceCreateTime. Not stale.
}

ret := lastActive.Add(cfg.StaleRevisionTimeout).Before(curTime)
if ret {
logger.Infof("Detected stale revision %v with creation time %v and last active time %v.",
rev.ObjectMeta.Name, rev.ObjectMeta.CreationTimestamp, lastActive)
active := revisionLastActiveTime(rev)
if sinceActive != gc.Disabled && time.Since(active) < sinceActive {
return false // Revision was recently active. Not stale.
}
return ret

logger.Infof("Detected stale revision %q with creation time %v and last active time %v.",
rev.ObjectMeta.Name, createTime, active)
return true
}

// revisionLastActiveTime returns if present:
Expand Down
Loading