Skip to content

Commit 4788f09

Browse files
committed
sql: refactor lease helper for single-descriptor lookup
Adds a helper for single-descriptor use. Epic: CRDB-49398 Release note: None
1 parent df1fb6a commit 4788f09

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

pkg/sql/catalog/lease/lease.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ func (m *Manager) WaitForNoVersion(
144144
return nil
145145
}
146146

147-
// maybeGetDescriptorsWithoutValidation gets a descriptor without validating
148-
// from the KV layer.
147+
// maybeGetDescriptorsWithoutValidation gets descriptors without validating from
148+
// the KV layer.
149149
func (m *Manager) maybeGetDescriptorsWithoutValidation(
150150
ctx context.Context, ids descpb.IDs, existenceExpected bool,
151151
) (catalog.Descriptors, error) {
@@ -158,6 +158,7 @@ func (m *Manager) maybeGetDescriptorsWithoutValidation(
158158
if err != nil {
159159
return err
160160
}
161+
161162
for _, id := range ids {
162163
desc := c.LookupDescriptor(id)
163164
if desc == nil {
@@ -177,6 +178,19 @@ func (m *Manager) maybeGetDescriptorsWithoutValidation(
177178
return descs, nil
178179
}
179180

181+
// maybeGetDescriptorWithoutValidation gets a descriptor without validating
182+
// from the KV layer.
183+
func (m *Manager) maybeGetDescriptorWithoutValidation(
184+
ctx context.Context, id descpb.ID, existenceExpected bool,
185+
) (catalog.Descriptor, error) {
186+
descArr, err := m.maybeGetDescriptorsWithoutValidation(ctx, descpb.IDs{id}, existenceExpected)
187+
if err != nil {
188+
return nil, err
189+
}
190+
191+
return descArr[0], nil
192+
}
193+
180194
// countDescriptorsHeldBySessionIDs can be used to make sure certain nodes
181195
// (sessions) observe the existence of a given set of descriptors. Assuming the given
182196
// sessions are still alive.
@@ -453,27 +467,28 @@ func (m *Manager) WaitForInitialVersion(
453467
// invariant that no new leases for desc.Version-1 will be granted once
454468
// desc.Version exists.
455469
//
456-
// If the descriptor is not found, an error will be returned. The error
457-
// can be detected by using errors.Is(err, catalog.ErrDescriptorNotFound).
470+
// If the descriptor is not found, an error will be returned.
458471
func (m *Manager) WaitForOneVersion(
459472
ctx context.Context,
460473
id descpb.ID,
461474
regions regionliveness.CachedDatabaseRegions,
462475
retryOpts retry.Options,
463-
) (desc catalog.Descriptor, _ error) {
476+
) (catalog.Descriptor, error) {
464477
// Increment the long wait gauge for wait for one version, if this function
465478
// takes longer than the lease duration.
466479
decAfterWait := m.IncGaugeAfterLeaseDuration(GaugeWaitForOneVersion)
467480
defer decAfterWait()
468481
wsTracker := startWaitStatsTracker(ctx)
469482
defer wsTracker.end()
483+
484+
var desc catalog.Descriptor
470485
for lastCount, r := 0, retry.Start(retryOpts); r.Next(); {
471486
var err error
472-
var descArr catalog.Descriptors
473-
if descArr, err = m.maybeGetDescriptorsWithoutValidation(ctx, descpb.IDs{id}, true); err != nil {
487+
desc, err = m.maybeGetDescriptorWithoutValidation(ctx, id, true)
488+
if err != nil {
474489
return nil, err
475490
}
476-
desc = descArr[0]
491+
477492
// Check to see if there are any leases that still exist on the previous
478493
// version of the descriptor.
479494
now := m.storage.clock.Now()
@@ -492,6 +507,7 @@ func (m *Manager) WaitForOneVersion(
492507
log.Infof(ctx, "waiting for %d leases to expire: desc=%v", detail.count, descs)
493508
}
494509
}
510+
495511
return desc, nil
496512
}
497513

0 commit comments

Comments
 (0)