github.com/weaviate/weaviate@v1.24.6/adapters/repos/db/lsmkv/memtable_size_advisor.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package lsmkv 13 14 import "time" 15 16 // if not enough config is provided we can fall back to this reasonable default 17 // value 18 const reasonableMemtableDefault = 10 * 1024 * 1024 19 20 type memtableSizeAdvisorCfg struct { 21 initial int 22 stepSize int 23 maxSize int 24 minDuration time.Duration 25 maxDuration time.Duration 26 } 27 28 type memtableSizeAdvisor struct { 29 cfg memtableSizeAdvisorCfg 30 active bool 31 } 32 33 func newMemtableSizeAdvisor(cfg memtableSizeAdvisorCfg) *memtableSizeAdvisor { 34 a := &memtableSizeAdvisor{ 35 cfg: cfg, 36 } 37 38 // only activate if initial size, step size, max size, and max duration are 39 // given 40 if a.cfg.maxSize > 0 && a.cfg.initial > 0 && a.cfg.stepSize > 0 && a.cfg.maxDuration > 0 { 41 a.active = true 42 } 43 44 return a 45 } 46 47 func (m memtableSizeAdvisor) Initial() int { 48 if m.active { 49 return m.cfg.initial 50 } else { 51 return reasonableMemtableDefault 52 } 53 } 54 55 func (m memtableSizeAdvisor) NextTarget(previousTarget int, 56 timeSinceFlush time.Duration, 57 ) (int, bool) { 58 if !m.active { 59 return reasonableMemtableDefault, false 60 } 61 62 if timeSinceFlush < m.cfg.minDuration { 63 next := min(previousTarget+m.cfg.stepSize, m.cfg.maxSize) 64 return next, next != previousTarget 65 } 66 if timeSinceFlush > m.cfg.maxDuration { 67 next := max(previousTarget-m.cfg.stepSize, m.cfg.initial) 68 return next, next != previousTarget 69 } 70 return previousTarget, false 71 } 72 73 func min(a, b int) int { 74 if a <= b { 75 return a 76 } 77 78 return b 79 } 80 81 func max(a, b int) int { 82 if a >= b { 83 return a 84 } 85 86 return b 87 }