Skip to content

Commit

Permalink
catalog/lease: avoid using context.Background
Browse files Browse the repository at this point in the history
- `(*AmbientContext).AnnotateCtx()` - takes care of connecting the
  context to the tracer
- `logtags.FromContext` / `logtags.WithTags` - reproduces the logging
  tags on the child context.

Release note: None
  • Loading branch information
knz committed Nov 11, 2021
1 parent 46dcf39 commit b2dc1f7
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/server/server_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,7 @@ func (s *SQLServer) preStart(

// Delete all orphaned table leases created by a prior instance of this
// node. This also uses SQL.
s.leaseMgr.DeleteOrphanedLeases(orphanedLeasesTimeThresholdNanos)
s.leaseMgr.DeleteOrphanedLeases(ctx, orphanedLeasesTimeThresholdNanos)

// Start scheduled jobs daemon.
jobs.StartJobSchedulerDaemon(
Expand Down
5 changes: 4 additions & 1 deletion pkg/sql/catalog/lease/descriptor_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/logtags"
"github.com/cockroachdb/redact"
)

Expand Down Expand Up @@ -273,7 +274,9 @@ func (t *descriptorState) maybeQueueLeaseRenewal(
}

// Start the renewal. When it finishes, it will reset t.renewalInProgress.
return t.stopper.RunAsyncTask(context.Background(),
newCtx := m.ambientCtx.AnnotateCtx(context.Background())
newCtx = logtags.WithTags(newCtx, logtags.FromContext(ctx))
return t.stopper.RunAsyncTask(newCtx,
"lease renewal", func(ctx context.Context) {
t.startLeaseRenewal(ctx, m, id, name)
})
Expand Down
10 changes: 7 additions & 3 deletions pkg/sql/catalog/lease/lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,9 @@ func acquireNodeLease(ctx context.Context, m *Manager, id descpb.ID) (bool, erro
// of the first context cancels other callers to the `acquireNodeLease()` method,
// because of its use of `singleflight.Group`. See issue #41780 for how this has
// happened.
newCtx, cancel := m.stopper.WithCancelOnQuiesce(logtags.WithTags(context.Background(), logtags.FromContext(ctx)))
baseCtx := m.ambientCtx.AnnotateCtx(context.Background())
baseCtx = logtags.WithTags(baseCtx, logtags.FromContext(ctx))
newCtx, cancel := m.stopper.WithCancelOnQuiesce(baseCtx)
defer cancel()
if m.isDraining() {
return nil, errors.New("cannot acquire lease when draining")
Expand Down Expand Up @@ -1166,7 +1168,7 @@ func (m *Manager) refreshSomeLeases(ctx context.Context) {
// DeleteOrphanedLeases releases all orphaned leases created by a prior
// instance of this node. timeThreshold is a walltime lower than the
// lowest hlc timestamp that the current instance of the node can use.
func (m *Manager) DeleteOrphanedLeases(timeThreshold int64) {
func (m *Manager) DeleteOrphanedLeases(ctx context.Context, timeThreshold int64) {
if m.testingKnobs.DisableDeleteOrphanedLeases {
return
}
Expand All @@ -1179,7 +1181,9 @@ func (m *Manager) DeleteOrphanedLeases(timeThreshold int64) {

// Run as async worker to prevent blocking the main server Start method.
// Exit after releasing all the orphaned leases.
_ = m.stopper.RunAsyncTask(context.Background(), "del-orphaned-leases", func(ctx context.Context) {
newCtx := m.ambientCtx.AnnotateCtx(context.Background())
newCtx = logtags.WithTags(newCtx, logtags.FromContext(ctx))
_ = m.stopper.RunAsyncTask(newCtx, "del-orphaned-leases", func(ctx context.Context) {
// This could have been implemented using DELETE WHERE, but DELETE WHERE
// doesn't implement AS OF SYSTEM TIME.

Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/catalog/lease/lease_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2007,7 +2007,7 @@ CREATE TABLE t.after (k CHAR PRIMARY KEY, v CHAR);
t.expectLeases(afterDesc.GetID(), "/1/1")

// Call DeleteOrphanedLeases() with the server startup time.
t.node(1).DeleteOrphanedLeases(now)
t.node(1).DeleteOrphanedLeases(ctx, now)
// Orphaned lease is gone.
t.expectLeases(beforeDesc.GetID(), "")
t.expectLeases(afterDesc.GetID(), "/1/1")
Expand Down

0 comments on commit b2dc1f7

Please sign in to comment.