Skip to content

Commit

Permalink
Minimal change to ensure that the bulky leaseEntry isn't kept in memo…
Browse files Browse the repository at this point in the history
…ry. (#10726) (#10732)
  • Loading branch information
ncabatoff authored Jan 19, 2021
1 parent 6e6834c commit fc8a31c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
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 @@ -115,10 +115,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 @@ -128,10 +128,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 @@ -144,33 +144,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 @@ -1523,9 +1523,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

0 comments on commit fc8a31c

Please sign in to comment.