Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimal change to ensure that the bulky leaseEntry isn't kept in memory. #10726

Merged
merged 2 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions changelog/10726.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
core: reduce memory used by leases
```
23 changes: 12 additions & 11 deletions vault/expiration.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ type ExpirationManager struct {
testRegisterAuthFailure uberAtomic.Bool
}

type ExpireLeaseStrategy func(context.Context, *ExpirationManager, *leaseEntry)
type ExpireLeaseStrategy func(context.Context, *ExpirationManager, string, *namespace.Namespace)

// revokeIDFunc is invoked when a given ID is expired
func expireLeaseStrategyRevoke(ctx context.Context, m *ExpirationManager, le *leaseEntry) {
func expireLeaseStrategyRevoke(ctx context.Context, m *ExpirationManager, leaseID string, ns *namespace.Namespace) {
for attempt := uint(0); attempt < maxRevokeAttempts; attempt++ {
releasePermit := func() {}
if m.revokePermitPool != nil {
Expand All @@ -129,10 +129,10 @@ func expireLeaseStrategyRevoke(ctx context.Context, m *ExpirationManager, le *le
m.logger.Trace("expiring lease; got permit pool")
}

metrics.IncrCounterWithLabels([]string{"expire", "lease_expiration"}, 1, []metrics.Label{{"namespace", le.namespace.ID}})
metrics.IncrCounterWithLabels([]string{"expire", "lease_expiration"}, 1, []metrics.Label{{"namespace", ns.ID}})

revokeCtx, cancel := context.WithTimeout(ctx, DefaultMaxRequestDuration)
revokeCtx = namespace.ContextWithNamespace(revokeCtx, le.namespace)
revokeCtx = namespace.ContextWithNamespace(revokeCtx, ns)

go func() {
select {
Expand All @@ -145,33 +145,33 @@ func expireLeaseStrategyRevoke(ctx context.Context, m *ExpirationManager, le *le

select {
case <-m.quitCh:
m.logger.Error("shutting down, not attempting further revocation of lease", "lease_id", le.LeaseID)
m.logger.Error("shutting down, not attempting further revocation of lease", "lease_id", leaseID)
releasePermit()
cancel()
return
case <-m.quitContext.Done():
m.logger.Error("core context canceled, not attempting further revocation of lease", "lease_id", le.LeaseID)
m.logger.Error("core context canceled, not attempting further revocation of lease", "lease_id", leaseID)
releasePermit()
cancel()
return
default:
}

m.coreStateLock.RLock()
err := m.Revoke(revokeCtx, le.LeaseID)
err := m.Revoke(revokeCtx, leaseID)
m.coreStateLock.RUnlock()
releasePermit()
cancel()
if err == nil {
return
}

metrics.IncrCounterWithLabels([]string{"expire", "lease_expiration", "error"}, 1, []metrics.Label{{"namespace", le.namespace.ID}})
metrics.IncrCounterWithLabels([]string{"expire", "lease_expiration", "error"}, 1, []metrics.Label{{"namespace", ns.ID}})

m.logger.Error("failed to revoke lease", "lease_id", le.LeaseID, "error", err)
m.logger.Error("failed to revoke lease", "lease_id", leaseID, "error", err)
time.Sleep((1 << attempt) * revokeRetryBase)
}
m.logger.Error("maximum revoke attempts reached", "lease_id", le.LeaseID)
m.logger.Error("maximum revoke attempts reached", "lease_id", leaseID)
}

// NewExpirationManager creates a new ExpirationManager that is backed
Expand Down Expand Up @@ -1524,9 +1524,10 @@ func (m *ExpirationManager) updatePendingInternal(le *leaseEntry) {
pending.timer.Reset(leaseTotal)
// No change to lease count in this case
} else {
leaseID, namespace := le.LeaseID, le.namespace
// Extend the timer by the lease total
timer := time.AfterFunc(leaseTotal, func() {
m.expireFunc(m.quitContext, m, le)
m.expireFunc(m.quitContext, m, leaseID, namespace)
})
pending = pendingInfo{
timer: timer,
Expand Down