-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
txn_id_cache.go
178 lines (156 loc) · 5.11 KB
/
txn_id_cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package txnidcache
import (
"context"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
)
const (
shardCount = 256
channelSize = 128
)
// Cache stores the mapping from the Transaction IDs (UUID) of recently
// executed transactions to their corresponding Transaction Fingerprint ID (uint64).
// The size of Cache is controlled via sql.contention.txn_id_cache.max_size
// cluster setting, and it follows FIFO eviction policy once the cache size
// reaches the limit defined by the cluster setting.
//
// Cache's overall architecture is as follows:
// +------------------------------------+
// | connExecutor ---------* |
// | +-------------+ | writes to |
// | | Writer |<-* |
// | +-----+-------+ |
// +------------|-----------------------+
// |
// when full, Writer flushes into a channel.
// |
// v
// channel
// ^
// |
// Cache runs a goroutine that polls the channel --*
// | and sends the message to its |
// | corresponding shard |
// | *-----------------------------*
// +----------------------------|---+
// | Cache | |
// | | +-------------v--------------------------------------*
// | *----> | shard1 [writeBuffer] ----------- when full ---* |
// | | | | |
// | | | [cache.UnorderedCache] <---- flush ----* |
// | | +----------------------------------------------------*
// | *----> shard2 |
// | | |
// | *----> shard2 |
// | | |
// | ....... |
// | | |
// | *----> shard256 |
// +--------------------------------+
type Cache struct {
st *cluster.Settings
msgChan chan messageBlock
store storage
pool writerPool
metrics *Metrics
}
var (
entrySize = int64(uuid.UUID{}.Size()) +
roachpb.TransactionFingerprintID(0).Size() + 8 // the size of a hash.
_ Provider = &Cache{}
)
// NewTxnIDCache creates a new instance of Cache.
func NewTxnIDCache(st *cluster.Settings, metrics *Metrics) *Cache {
t := &Cache{
st: st,
metrics: metrics,
msgChan: make(chan messageBlock, channelSize),
}
t.pool = newWriterPoolImpl(func() Writer {
return t.createNewWriteBuffer()
})
t.store = newShardedCache(func() storage {
return newTxnIDCacheShard(t)
})
return t
}
// Start implements the Provider interface.
func (t *Cache) Start(ctx context.Context, stopper *stop.Stopper) {
_ = stopper.RunAsyncTask(ctx, "txn-id-cache-ingest", func(ctx context.Context) {
for {
select {
case msgBlock := <-t.msgChan:
for i := range msgBlock.data {
t.store.Record(msgBlock.data[i])
}
if msgBlock.forceImmediateFlushIntoCache {
t.store.Flush()
}
case <-stopper.ShouldQuiesce():
return
}
}
})
}
// Lookup implements the reader interface.
func (t *Cache) Lookup(txnID uuid.UUID) (result roachpb.TransactionFingerprintID, found bool) {
return t.store.Lookup(txnID)
}
// GetWriter implements the writerPool interface.
func (t *Cache) GetWriter() Writer {
return t.pool.GetWriter()
}
// push implements the messageSink interface.
func (t *Cache) push(msg messageBlock) {
select {
case t.msgChan <- msg:
// noop.
default:
t.metrics.DiscardedResolvedTxnIDCount.Inc(messageBlockSize)
}
}
// disconnect implements the messageSink interface.
func (t *Cache) disconnect(w Writer) {
t.pool.disconnect(w)
}
func (t *Cache) createNewWriteBuffer() *ConcurrentWriteBuffer {
writeBuffer := &ConcurrentWriteBuffer{
sink: t,
}
writeBuffer.flushDone.L = writeBuffer.flushSyncLock.RLocker()
return writeBuffer
}
// FlushActiveWritersForTest is only used in test to flush all writers so the
// content of the Cache can be inspected.
func (t *Cache) FlushActiveWritersForTest() {
poolImpl := t.pool.(*writerPoolImpl)
poolImpl.mu.Lock()
defer poolImpl.mu.Unlock()
for txnIDCacheWriteBuffer := range poolImpl.mu.activeWriters {
txnIDCacheWriteBuffer.(*ConcurrentWriteBuffer).flushForTest()
}
}
func hashTxnID(txnID uuid.UUID) int {
b := txnID.GetBytes()
b, val1, err := encoding.DecodeUint64Descending(b)
if err != nil {
panic(err)
}
_, val2, err := encoding.DecodeUint64Descending(b)
if err != nil {
panic(err)
}
return int((val1 ^ val2) % shardCount)
}