@@ -3319,15 +3319,17 @@ func (sm *TenantsStorageMetrics) acquireTenant(tenantID roachpb.TenantID) *tenan
3319
3319
for {
3320
3320
if m , ok := sm .tenants .Load (tenantID ); ok {
3321
3321
if alreadyDestroyed := incRef (m ); ! alreadyDestroyed {
3322
- return & tenantMetricsRef {
3323
- _tenantID : tenantID ,
3324
- }
3322
+ return & m .ref
3325
3323
}
3326
3324
// Somebody else concurrently took the reference count to zero, go back
3327
3325
// around. Because of the locking in releaseTenant, we know that we'll
3328
3326
// find a different value or no value at all on the next iteration.
3329
3327
} else {
3330
- m := & tenantStorageMetrics {}
3328
+ m := & tenantStorageMetrics {
3329
+ ref : tenantMetricsRef {
3330
+ _tenantID : tenantID ,
3331
+ },
3332
+ }
3331
3333
m .mu .Lock ()
3332
3334
_ , loaded := sm .tenants .LoadOrStore (tenantID , m )
3333
3335
if loaded {
@@ -3359,9 +3361,7 @@ func (sm *TenantsStorageMetrics) acquireTenant(tenantID roachpb.TenantID) *tenan
3359
3361
m .SysCount = sm .SysCount .AddChild (tenantIDStr )
3360
3362
m .AbortSpanBytes = sm .AbortSpanBytes .AddChild (tenantIDStr )
3361
3363
m .mu .Unlock ()
3362
- return & tenantMetricsRef {
3363
- _tenantID : tenantID ,
3364
- }
3364
+ return & m .ref
3365
3365
}
3366
3366
}
3367
3367
}
@@ -3370,11 +3370,8 @@ func (sm *TenantsStorageMetrics) acquireTenant(tenantID roachpb.TenantID) *tenan
3370
3370
// acquired with acquireTenant. It will fatally log if no entry exists for this
3371
3371
// tenant.
3372
3372
func (sm * TenantsStorageMetrics ) releaseTenant (ctx context.Context , ref * tenantMetricsRef ) {
3373
- m := sm .getTenant (ctx , ref ) // NB: asserts against use-after-release
3374
- if atomic .SwapInt32 (& ref ._state , 1 ) != 0 {
3375
- ref .assert (ctx ) // this will fatal
3376
- return // unreachable
3377
- }
3373
+ m := sm .getTenant (ctx , ref )
3374
+
3378
3375
ref ._stack .Lock ()
3379
3376
ref ._stack .SafeStack = debugutil .Stack ()
3380
3377
ref ._stack .Unlock ()
@@ -3387,6 +3384,11 @@ func (sm *TenantsStorageMetrics) releaseTenant(ctx context.Context, ref *tenantM
3387
3384
return
3388
3385
}
3389
3386
3387
+ if atomic .SwapInt32 (& ref ._state , 1 ) != 0 {
3388
+ log .FatalfDepth (ctx , 1 , "tenant metrics already released in:\n %s" , ref ._stack .SafeStack )
3389
+ return // unreachable
3390
+ }
3391
+
3390
3392
// The refCount is zero, delete this instance after destroying its metrics.
3391
3393
// Note that concurrent attempts to create an instance will detect the zero
3392
3394
// refCount value and construct a new instance.
@@ -3434,7 +3436,8 @@ func (sm *TenantsStorageMetrics) getTenant(
3434
3436
}
3435
3437
3436
3438
type tenantStorageMetrics struct {
3437
- mu struct {
3439
+ ref tenantMetricsRef
3440
+ mu struct {
3438
3441
syncutil.Mutex
3439
3442
refCount int
3440
3443
}
0 commit comments