Skip to content

Commit cf98810

Browse files
committed
kvserver: nest tenant metrics ref inside of tenant metrics
In a follow-up step, we will absorb the struct into its parent, thus removing a confusing layer of indirection. The Replica will hold on to a `*tenantMetrics` directly.
1 parent 7cc372f commit cf98810

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

pkg/kv/kvserver/metrics.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3319,15 +3319,17 @@ func (sm *TenantsStorageMetrics) acquireTenant(tenantID roachpb.TenantID) *tenan
33193319
for {
33203320
if m, ok := sm.tenants.Load(tenantID); ok {
33213321
if alreadyDestroyed := incRef(m); !alreadyDestroyed {
3322-
return &tenantMetricsRef{
3323-
_tenantID: tenantID,
3324-
}
3322+
return &m.ref
33253323
}
33263324
// Somebody else concurrently took the reference count to zero, go back
33273325
// around. Because of the locking in releaseTenant, we know that we'll
33283326
// find a different value or no value at all on the next iteration.
33293327
} else {
3330-
m := &tenantStorageMetrics{}
3328+
m := &tenantStorageMetrics{
3329+
ref: tenantMetricsRef{
3330+
_tenantID: tenantID,
3331+
},
3332+
}
33313333
m.mu.Lock()
33323334
_, loaded := sm.tenants.LoadOrStore(tenantID, m)
33333335
if loaded {
@@ -3359,9 +3361,7 @@ func (sm *TenantsStorageMetrics) acquireTenant(tenantID roachpb.TenantID) *tenan
33593361
m.SysCount = sm.SysCount.AddChild(tenantIDStr)
33603362
m.AbortSpanBytes = sm.AbortSpanBytes.AddChild(tenantIDStr)
33613363
m.mu.Unlock()
3362-
return &tenantMetricsRef{
3363-
_tenantID: tenantID,
3364-
}
3364+
return &m.ref
33653365
}
33663366
}
33673367
}
@@ -3370,11 +3370,8 @@ func (sm *TenantsStorageMetrics) acquireTenant(tenantID roachpb.TenantID) *tenan
33703370
// acquired with acquireTenant. It will fatally log if no entry exists for this
33713371
// tenant.
33723372
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+
33783375
ref._stack.Lock()
33793376
ref._stack.SafeStack = debugutil.Stack()
33803377
ref._stack.Unlock()
@@ -3387,6 +3384,11 @@ func (sm *TenantsStorageMetrics) releaseTenant(ctx context.Context, ref *tenantM
33873384
return
33883385
}
33893386

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+
33903392
// The refCount is zero, delete this instance after destroying its metrics.
33913393
// Note that concurrent attempts to create an instance will detect the zero
33923394
// refCount value and construct a new instance.
@@ -3434,7 +3436,8 @@ func (sm *TenantsStorageMetrics) getTenant(
34343436
}
34353437

34363438
type tenantStorageMetrics struct {
3437-
mu struct {
3439+
ref tenantMetricsRef
3440+
mu struct {
34383441
syncutil.Mutex
34393442
refCount int
34403443
}

0 commit comments

Comments
 (0)