From f2b7e9f1446dd2825c5004e313448aee2a684f0e Mon Sep 17 00:00:00 2001 From: Steven Danna Date: Wed, 22 Nov 2023 10:41:29 +0000 Subject: [PATCH] Revert "kv: store span by pointer in latch struct" This reverts commit 831c61ded84ba2d5887736f7046a45c3cac3cd4c. --- pkg/kv/kvserver/spanlatch/manager.go | 49 ++++++----------------- pkg/kv/kvserver/spanlatch/manager_test.go | 8 ---- 2 files changed, 13 insertions(+), 44 deletions(-) diff --git a/pkg/kv/kvserver/spanlatch/manager.go b/pkg/kv/kvserver/spanlatch/manager.go index a227e4a3aeb7..32fe84e42081 100644 --- a/pkg/kv/kvserver/spanlatch/manager.go +++ b/pkg/kv/kvserver/spanlatch/manager.go @@ -87,7 +87,7 @@ func Make(stopper *stop.Stopper, slowReqs *metric.Gauge) Manager { type latch struct { *signals id uint64 - span *roachpb.Span + span roachpb.Span ts hlc.Timestamp next, prev *latch // readSet linked-list. } @@ -109,36 +109,13 @@ func (la *latch) SafeFormat(w redact.SafePrinter, _ rune) { //go:generate ../../../util/interval/generic/gen.sh *latch spanlatch // Methods required by util/interval/generic type contract. -func (la *latch) New() *latch { return new(latch) } -func (la *latch) ID() uint64 { return la.id } -func (la *latch) SetID(v uint64) { la.id = v } -func (la *latch) Key() []byte { - if la.span == nil { - return nil - } - return la.span.Key -} -func (la *latch) EndKey() []byte { - if la.span == nil { - return nil - } - return la.span.EndKey -} -func (la *latch) SetKey(v []byte) { - la.initSpan() - la.span.Key = v -} -func (la *latch) SetEndKey(v []byte) { - la.initSpan() - la.span.EndKey = v -} - -// initSpan lazily initializes the latch's span field. Only used in tests. -func (la *latch) initSpan() { - if la.span == nil { - la.span = new(roachpb.Span) - } -} +func (la *latch) ID() uint64 { return la.id } +func (la *latch) Key() []byte { return la.span.Key } +func (la *latch) EndKey() []byte { return la.span.EndKey } +func (la *latch) New() *latch { return new(latch) } +func (la *latch) SetID(v uint64) { la.id = v } +func (la *latch) SetKey(v []byte) { la.span.Key = v } +func (la *latch) SetEndKey(v []byte) { la.span.EndKey = v } type signals struct { done signal @@ -221,7 +198,7 @@ func newGuard(spans *spanset.SpanSet, pp poison.Policy) *Guard { ssLatches := latches[:n] for i := range ssLatches { latch := &latches[i] - latch.span = &ss[i].Span + latch.span = ss[i].Span latch.signals = &guard.signals latch.ts = ss[i].Timestamp // latch.setID() in Manager.insert, under lock. @@ -310,9 +287,9 @@ func (m *Manager) CheckOptimisticNoConflicts(lg *Guard, spans *spanset.SpanSet) tr := &snap.trees[s] for a := spanset.SpanAccess(0); a < spanset.NumSpanAccess; a++ { ss := spans.GetSpans(a, s) - for i := range ss { - search.span = &ss[i].Span - search.ts = ss[i].Timestamp + for _, sp := range ss { + search.span = sp.Span + search.ts = sp.Timestamp switch a { case spanset.SpanReadOnly: // Search for writes at equal or lower timestamps. @@ -584,7 +561,7 @@ func (m *Manager) waitForSignal( // ourselves anyway, so we don't need to self-poison. switch pp { case poison.Policy_Error: - return poison.NewPoisonedError(*held.span, held.ts) + return poison.NewPoisonedError(held.span, held.ts) case poison.Policy_Wait: log.Eventf(ctx, "encountered poisoned latch; continuing to wait") wait.poison.signal() diff --git a/pkg/kv/kvserver/spanlatch/manager_test.go b/pkg/kv/kvserver/spanlatch/manager_test.go index f34050536850..be8cc806cce1 100644 --- a/pkg/kv/kvserver/spanlatch/manager_test.go +++ b/pkg/kv/kvserver/spanlatch/manager_test.go @@ -18,7 +18,6 @@ import ( "strings" "testing" "time" - "unsafe" "github.com/cockroachdb/cockroach/pkg/keys" "github.com/cockroachdb/cockroach/pkg/kv/kvserver/concurrency/poison" @@ -682,13 +681,6 @@ func TestLatchManagerWaitFor(t *testing.T) { m.Release(lg3) } -// TestSizeOfLatch tests the size of the latch struct. -func TestSizeOfLatch(t *testing.T) { - var la latch - size := int(unsafe.Sizeof(la)) - require.Equal(t, 56, size) -} - func BenchmarkLatchManagerReadOnlyMix(b *testing.B) { for _, size := range []int{1, 4, 16, 64, 128, 256} { b.Run(fmt.Sprintf("size=%d", size), func(b *testing.B) {