Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server: set multiple concurrentReadTx instances share one txReadBuffer. Approach #2 #12935

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions server/mvcc/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ func newBackend(bcfg BackendConfig) *backend {
readTx: &readTx{
baseReadTx: baseReadTx{
buf: txReadBuffer{
txBuffer: txBuffer{make(map[string]*bucketBuffer)},
txBuffer: txBuffer{make(map[string]*bucketBuffer)},
cachedReadBuffer: nil,
},
buckets: make(map[string]*bolt.Bucket),
txWg: new(sync.WaitGroup),
Expand Down Expand Up @@ -222,9 +223,15 @@ func (b *backend) ConcurrentReadTx() ReadTx {
// prevent boltdb read Tx from been rolled back until store read Tx is done. Needs to be called when holding readTx.RLock().
b.readTx.txWg.Add(1)
// TODO: might want to copy the read buffer lazily - create copy when A) end of a write transaction B) end of a batch interval.
var buf txReadBuffer
if b.readTx.buf.cachedReadBuffer != nil {
buf = *b.readTx.buf.cachedReadBuffer
} else {
buf = b.readTx.buf.unsafeCopy()
}
return &concurrentReadTx{
baseReadTx: baseReadTx{
buf: b.readTx.buf.unsafeCopy(),
buf: buf,
txMu: b.readTx.txMu,
tx: b.readTx.tx,
buckets: b.readTx.buckets,
Expand Down
14 changes: 13 additions & 1 deletion server/mvcc/backend/tx_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,15 @@ func (txw *txWriteBuffer) writeback(txr *txReadBuffer) {
rb.merge(wb)
}
txw.reset()
// save one copy of the latest modified buffer
txr.unsafeGenerateNewCachedTxReadBuffer()
}

// txReadBuffer accesses buffered updates.
type txReadBuffer struct{ txBuffer }
type txReadBuffer struct {
txBuffer
cachedReadBuffer *txReadBuffer
}

func (txr *txReadBuffer) Range(bucketName, key, endKey []byte, limit int64) ([][]byte, [][]byte) {
if b := txr.buckets[string(bucketName)]; b != nil {
Expand All @@ -88,12 +93,19 @@ func (txr *txReadBuffer) ForEach(bucketName []byte, visitor func(k, v []byte) er
return nil
}

// generate a new cached txReadBuffer, caller should acquire backend.readTx.RLock()
wilsonwang371 marked this conversation as resolved.
Show resolved Hide resolved
func (txr *txReadBuffer) unsafeGenerateNewCachedTxReadBuffer() {
tmp := txr.unsafeCopy()
txr.cachedReadBuffer = &tmp
}

// unsafeCopy returns a copy of txReadBuffer, caller should acquire backend.readTx.RLock()
func (txr *txReadBuffer) unsafeCopy() txReadBuffer {
txrCopy := txReadBuffer{
txBuffer: txBuffer{
buckets: make(map[string]*bucketBuffer, len(txr.txBuffer.buckets)),
},
cachedReadBuffer: nil,
}
for bucketName, bucket := range txr.txBuffer.buckets {
txrCopy.txBuffer.buckets[bucketName] = bucket.Copy()
Expand Down