Skip to content

Commit

Permalink
sql: add option to enable/disable txn id cache
Browse files Browse the repository at this point in the history
Resolves #76329

Release note (sql change): when `sql.contention.txn_id_cache.max_size`
is set to 0, it would effectively turn off transaction ID cache.
  • Loading branch information
Azhng committed Feb 16, 2022
1 parent dc2302d commit 13b80bb
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/generated/settings/settings-for-tenants.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ server.web_session.purge.max_deletions_per_cycle integer 10 the maximum number o
server.web_session.purge.period duration 1h0m0s the time until old sessions are deleted
server.web_session.purge.ttl duration 1h0m0s if nonzero, entries in system.web_sessions older than this duration are periodically purged
server.web_session_timeout duration 168h0m0s the duration that a newly created web session will be valid
sql.contention.txn_id_cache.max_size byte size 64 MiB the maximum byte size TxnID cache will use
sql.contention.txn_id_cache.max_size byte size 64 MiB the maximum byte size TxnID cache will use (set to 0 to disable)
sql.cross_db_fks.enabled boolean false if true, creating foreign key references across databases is allowed
sql.cross_db_sequence_owners.enabled boolean false if true, creating sequences owned by tables from other databases is allowed
sql.cross_db_sequence_references.enabled boolean false if true, sequences referenced by tables from other databases are allowed
Expand Down
2 changes: 1 addition & 1 deletion docs/generated/settings/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
<tr><td><code>server.web_session.purge.period</code></td><td>duration</td><td><code>1h0m0s</code></td><td>the time until old sessions are deleted</td></tr>
<tr><td><code>server.web_session.purge.ttl</code></td><td>duration</td><td><code>1h0m0s</code></td><td>if nonzero, entries in system.web_sessions older than this duration are periodically purged</td></tr>
<tr><td><code>server.web_session_timeout</code></td><td>duration</td><td><code>168h0m0s</code></td><td>the duration that a newly created web session will be valid</td></tr>
<tr><td><code>sql.contention.txn_id_cache.max_size</code></td><td>byte size</td><td><code>64 MiB</code></td><td>the maximum byte size TxnID cache will use</td></tr>
<tr><td><code>sql.contention.txn_id_cache.max_size</code></td><td>byte size</td><td><code>64 MiB</code></td><td>the maximum byte size TxnID cache will use (set to 0 to disable)</td></tr>
<tr><td><code>sql.cross_db_fks.enabled</code></td><td>boolean</td><td><code>false</code></td><td>if true, creating foreign key references across databases is allowed</td></tr>
<tr><td><code>sql.cross_db_sequence_owners.enabled</code></td><td>boolean</td><td><code>false</code></td><td>if true, creating sequences owned by tables from other databases is allowed</td></tr>
<tr><td><code>sql.cross_db_sequence_references.enabled</code></td><td>boolean</td><td><code>false</code></td><td>if true, sequences referenced by tables from other databases are allowed</td></tr>
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/contention/txnidcache/cluster_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ import "github.com/cockroachdb/cockroach/pkg/settings"
var MaxSize = settings.RegisterByteSizeSetting(
settings.TenantWritable,
`sql.contention.txn_id_cache.max_size`,
"the maximum byte size TxnID cache will use",
"the maximum byte size TxnID cache will use (set to 0 to disable)",
64*1024*1024, // 64 MB
).WithPublic()
2 changes: 1 addition & 1 deletion pkg/sql/contention/txnidcache/txn_id_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func NewTxnIDCache(st *cluster.Settings, metrics *Metrics) *Cache {
return MaxSize.Get(&st.SV) / entrySize
} /* capacity */)

t.writer = newWriter(t)
t.writer = newWriter(st, t)
return t
}

Expand Down
9 changes: 8 additions & 1 deletion pkg/sql/contention/txnidcache/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package txnidcache

import (
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
)
Expand All @@ -20,15 +21,18 @@ import (
const shardCount = 16

type writer struct {
st *cluster.Settings

shards [shardCount]*concurrentWriteBuffer

sink blockSink
}

var _ Writer = &writer{}

func newWriter(sink blockSink) *writer {
func newWriter(st *cluster.Settings, sink blockSink) *writer {
w := &writer{
st: st,
sink: sink,
}

Expand All @@ -41,6 +45,9 @@ func newWriter(sink blockSink) *writer {

// Record implements the Writer interface.
func (w *writer) Record(resolvedTxnID ResolvedTxnID) {
if MaxSize.Get(&w.st.SV) == 0 {
return
}
shardIdx := hashTxnID(resolvedTxnID.TxnID)
buffer := w.shards[shardIdx]
buffer.Record(resolvedTxnID)
Expand Down
51 changes: 50 additions & 1 deletion pkg/sql/contention/txnidcache/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/stretchr/testify/require"
)

type blackHoleSink struct {
Expand Down Expand Up @@ -71,11 +72,12 @@ func BenchmarkWriter(b *testing.B) {
defer log.Scope(b).Close(b)

ctx := context.Background()
st := cluster.MakeTestingClusterSettings()

run := func(b *testing.B, sink blockSink, numOfConcurrentWriter int) {
starter := make(chan struct{})

w := newWriter(sink)
w := newWriter(st, sink)

b.ResetTimer()
b.SetBytes(blockSize * entrySize)
Expand Down Expand Up @@ -149,3 +151,50 @@ func BenchmarkWriter(b *testing.B) {
})
}
}

type counterSink struct {
numOfRecord int
}

var _ blockSink = &counterSink{}

func (c *counterSink) push(block *block) {
for i := 0; i < blockSize; i++ {
if !block[i].valid() {
break
}
c.numOfRecord++
}
}

func TestTxnIDCacheCanBeDisabledViaClusterSetting(t *testing.T) {
st := cluster.MakeTestingClusterSettings()
ctx := context.Background()

sink := &counterSink{}
w := newWriter(st, sink)
w.Record(ResolvedTxnID{
TxnID: uuid.FastMakeV4(),
})

w.Flush()
require.Equal(t, 1, sink.numOfRecord)

// This should disable txn id cache.
MaxSize.Override(ctx, &st.SV, 0)

w.Record(ResolvedTxnID{
TxnID: uuid.FastMakeV4(),
})
w.Flush()
require.Equal(t, 1, sink.numOfRecord)

// This should re-enable txn id cache.
MaxSize.Override(ctx, &st.SV, MaxSize.Default())

w.Record(ResolvedTxnID{
TxnID: uuid.FastMakeV4(),
})
w.Flush()
require.Equal(t, 2, sink.numOfRecord)
}

0 comments on commit 13b80bb

Please sign in to comment.