-
Notifications
You must be signed in to change notification settings - Fork 222
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
membuffer: support staging & checkpoint for ART #1465
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
package art | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
|
||
tikverr "github.com/tikv/client-go/v2/error" | ||
|
@@ -76,7 +77,7 @@ func (t *ART) GetFlags(key []byte) (kv.KeyFlags, error) { | |
if leaf.vAddr.IsNull() && leaf.isDeleted() { | ||
return 0, tikverr.ErrNotExist | ||
} | ||
return leaf.getKeyFlags(), nil | ||
return leaf.GetKeyFlags(), nil | ||
} | ||
|
||
func (t *ART) Set(key artKey, value []byte, ops ...kv.FlagsOp) error { | ||
|
@@ -324,8 +325,8 @@ func (t *ART) newLeaf(key artKey) (artNode, *artLeaf) { | |
} | ||
|
||
func (t *ART) setValue(addr arena.MemdbArenaAddr, l *artLeaf, value []byte, ops []kv.FlagsOp) { | ||
flags := l.getKeyFlags() | ||
if flags == 0 && l.vAddr.IsNull() { | ||
flags := l.GetKeyFlags() | ||
if flags == 0 && l.vAddr.IsNull() || l.isDeleted() { | ||
t.len++ | ||
t.size += int(l.klen) | ||
} | ||
|
@@ -373,12 +374,12 @@ func (t *ART) trySwapValue(addr arena.MemdbArenaAddr, value []byte) (int, bool) | |
} | ||
|
||
func (t *ART) Dirty() bool { | ||
panic("unimplemented") | ||
return t.dirty | ||
} | ||
|
||
// Mem returns the memory usage of MemBuffer. | ||
func (t *ART) Mem() uint64 { | ||
panic("unimplemented") | ||
return t.allocator.nodeAllocator.Capacity() + t.allocator.vlogAllocator.Capacity() | ||
} | ||
|
||
// Len returns the count of entries in the MemBuffer. | ||
|
@@ -392,51 +393,89 @@ func (t *ART) Size() int { | |
} | ||
|
||
func (t *ART) checkpoint() arena.MemDBCheckpoint { | ||
panic("unimplemented") | ||
return t.allocator.vlogAllocator.Checkpoint() | ||
} | ||
|
||
func (t *ART) RevertNode(hdr *arena.MemdbVlogHdr) { | ||
panic("unimplemented") | ||
lf := t.allocator.getLeaf(hdr.NodeAddr) | ||
cfzjywxk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if lf == nil { | ||
panic("revert an invalid node") | ||
} | ||
lf.vAddr = hdr.OldValue | ||
t.size -= int(hdr.ValueLen) | ||
if hdr.OldValue.IsNull() { | ||
keptFlags := lf.GetKeyFlags() | ||
keptFlags = keptFlags.AndPersistent() | ||
if keptFlags == 0 { | ||
lf.markDelete() | ||
t.len-- | ||
t.size -= int(lf.klen) | ||
} else { | ||
lf.setKeyFlags(keptFlags) | ||
} | ||
} else { | ||
t.size += len(t.allocator.vlogAllocator.GetValue(hdr.OldValue)) | ||
} | ||
} | ||
|
||
func (t *ART) InspectNode(addr arena.MemdbArenaAddr) (*artLeaf, arena.MemdbArenaAddr) { | ||
panic("unimplemented") | ||
lf := t.allocator.getLeaf(addr) | ||
return lf, lf.vAddr | ||
} | ||
|
||
// Checkpoint returns a checkpoint of ART. | ||
func (t *ART) Checkpoint() *arena.MemDBCheckpoint { | ||
panic("unimplemented") | ||
cp := t.allocator.vlogAllocator.Checkpoint() | ||
return &cp | ||
} | ||
|
||
// RevertToCheckpoint reverts the ART to the checkpoint. | ||
func (t *ART) RevertToCheckpoint(cp *arena.MemDBCheckpoint) { | ||
panic("unimplemented") | ||
t.allocator.vlogAllocator.RevertToCheckpoint(t, cp) | ||
t.allocator.vlogAllocator.Truncate(cp) | ||
t.allocator.vlogAllocator.OnMemChange() | ||
} | ||
|
||
func (t *ART) Stages() []arena.MemDBCheckpoint { | ||
panic("unimplemented") | ||
return t.stages | ||
} | ||
|
||
func (t *ART) Staging() int { | ||
return 0 | ||
t.stages = append(t.stages, t.checkpoint()) | ||
return len(t.stages) | ||
} | ||
|
||
func (t *ART) Release(h int) { | ||
if h != len(t.stages) { | ||
panic("cannot release staging buffer") | ||
} | ||
if h == 1 { | ||
tail := t.checkpoint() | ||
if !t.stages[0].IsSamePosition(&tail) { | ||
t.dirty = true | ||
} | ||
} | ||
t.stages = t.stages[:h-1] | ||
} | ||
|
||
func (t *ART) Cleanup(h int) { | ||
} | ||
|
||
func (t *ART) revertToCheckpoint(cp *arena.MemDBCheckpoint) { | ||
panic("unimplemented") | ||
} | ||
|
||
func (t *ART) moveBackCursor(cursor *arena.MemDBCheckpoint, hdr *arena.MemdbVlogHdr) { | ||
panic("unimplemented") | ||
} | ||
if h > len(t.stages) { | ||
return | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not directly related to this change, but I wonder whether there are such usages... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TiDB always call func addRecord() {
sh := memBuffer.Staging() // sh == 2, len(stages) == 2
defer memBuffer.Cleanup(sh) // sh == 2, len(stages) == 1
...
memBuffer.Release(sh) // len(stages) == 1 after this call
} |
||
} | ||
if h < len(t.stages) { | ||
panic(fmt.Sprintf("cannot cleanup staging buffer, h=%v, len(db.stages)=%v", h, len(t.stages))) | ||
} | ||
|
||
func (t *ART) truncate(snap *arena.MemDBCheckpoint) { | ||
panic("unimplemented") | ||
cp := &t.stages[h-1] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'll add test for it. |
||
if !t.vlogInvalid { | ||
curr := t.checkpoint() | ||
if !curr.IsSamePosition(cp) { | ||
t.allocator.vlogAllocator.RevertToCheckpoint(t, cp) | ||
t.allocator.vlogAllocator.Truncate(cp) | ||
} | ||
} | ||
t.stages = t.stages[:h-1] | ||
t.allocator.vlogAllocator.OnMemChange() | ||
} | ||
|
||
// Reset resets the MemBuffer to initial states. | ||
|
@@ -459,7 +498,10 @@ func (t *ART) DiscardValues() { | |
|
||
// InspectStage used to inspect the value updates in the given stage. | ||
func (t *ART) InspectStage(handle int, f func([]byte, kv.KeyFlags, []byte)) { | ||
panic("unimplemented") | ||
idx := handle - 1 | ||
tail := t.allocator.vlogAllocator.Checkpoint() | ||
head := t.stages[idx] | ||
t.allocator.vlogAllocator.InspectKVInLog(t, &head, &tail, f) | ||
} | ||
|
||
// SelectValueHistory select the latest value which makes `predicate` returns true from the modification history. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is "RevertVLogEntry" or something similar a more appropriate name?
RevertNode
looks like we are deleting the node.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename it to
RevertVAddr
.