Repository navigation
WIP all: Add container types with disk overflow #4980
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
Changes from all commits
39cf085
057ec43
30fbf1f
9663af4
8f410f1
e87344b
9cb5f16
45e3e42
fad165d
c375f2e
63dda39
0e6af1e
b0cc232
c64c8b4
986af92
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 |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| // Copyright (C) 2016 The Syncthing Authors. | ||
| // | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
| // You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
|
||
| // Package diskoverflow provides several data container types which are limited | ||
| // in their memory usage. Once the total memory limit is reached, all new data | ||
| // is written to disk. | ||
| // Do not use any instances of these types concurrently! | ||
| package diskoverflow | ||
|
|
||
| import "github.com/syncthing/syncthing/lib/protocol" | ||
|
|
||
| // Value must be implemented by every type that is to be stored in a disk spilling container. | ||
| type Value interface { | ||
| Size() int64 | ||
| Marshal() []byte | ||
| Unmarshal([]byte) Value // The returned Value must not be a reference to the receiver. | ||
| } | ||
|
|
||
| // ValueFileInfo implements Value for protocol.FileInfo | ||
| type ValueFileInfo struct{ protocol.FileInfo } | ||
|
|
||
| func (s *ValueFileInfo) Size() int64 { | ||
| return int64(s.ProtoSize()) | ||
| } | ||
|
|
||
| func (s *ValueFileInfo) Marshal() []byte { | ||
| data, err := s.FileInfo.Marshal() | ||
| if err != nil { | ||
| panic("bug: marshalling FileInfo should never fail: " + err.Error()) | ||
| } | ||
| return data | ||
| } | ||
|
|
||
| func (s *ValueFileInfo) Unmarshal(v []byte) Value { | ||
| out := &ValueFileInfo{} | ||
| if err := out.FileInfo.Unmarshal(v); err != nil { | ||
| panic("unmarshal failed: " + err.Error()) | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| // Magical limit below which the underlying containers of slices/maps are never | ||
| // reset to save space. | ||
| // Variable for test, shouldn't ever be changed in code. | ||
| var minCompactionSize int64 = 10 << protocol.MiB | ||
|
|
||
| type common interface { | ||
| close() | ||
| length() int | ||
| } | ||
|
|
||
| type Iterator interface { | ||
| Release() | ||
| Next() bool | ||
| } | ||
|
|
||
| type ValueIterator interface { | ||
| Iterator | ||
| Value() Value | ||
| } | ||
|
|
||
| type SortValueIterator interface { | ||
| Iterator | ||
| Value() SortValue | ||
| } | ||
|
|
||
| type iteratorParent interface { | ||
| released() | ||
| value() interface{} | ||
| } | ||
|
|
||
| const ( | ||
| concurrencyMsg = "iteration in progress - don't modify or start a new iteration concurrently" | ||
| ) | ||
|
|
||
| type memIterator struct { | ||
| pos int | ||
| len int | ||
| reverse bool | ||
| parent iteratorParent | ||
| } | ||
|
|
||
| func newMemIterator(p iteratorParent, reverse bool, len int) *memIterator { | ||
| it := &memIterator{ | ||
| len: len, | ||
| reverse: reverse, | ||
| parent: p, | ||
| } | ||
| if reverse { | ||
| it.pos = len | ||
| } else { | ||
| it.pos = -1 | ||
| } | ||
| return it | ||
| } | ||
|
|
||
| func (si *memIterator) Next() bool { | ||
| if si.reverse { | ||
| if si.pos == 0 { | ||
| return false | ||
| } | ||
| si.pos-- | ||
| return true | ||
| } | ||
| if si.pos == si.len-1 { | ||
| return false | ||
| } | ||
| si.pos++ | ||
| return true | ||
| } | ||
|
|
||
| func (si *memIterator) Release() { | ||
| si.parent.released() | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Copyright (C) 2014 The Syncthing Authors. | ||
| // | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
| // You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
|
||
| package diskoverflow | ||
|
|
||
| import ( | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/syncthing/syncthing/lib/logger" | ||
| ) | ||
|
|
||
| var ( | ||
| l = logger.DefaultLogger.NewFacility("diskoverflow", "data containers that spill to disk if too big") | ||
| ) | ||
|
|
||
| func init() { | ||
| l.SetDebug("diskoverflow", strings.Contains(os.Getenv("STTRACE"), "diskoverflow") || os.Getenv("STTRACE") == "all") | ||
| } | ||
|
|
||
| func shouldDebug() bool { | ||
| return l.ShouldDebug("diskoverflow") | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| // Copyright (C) 2018 The Syncthing Authors. | ||
| // | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
| // You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
|
||
| package diskoverflow | ||
|
|
||
| import ( | ||
| "math/rand" | ||
|
|
||
| "github.com/syncthing/syncthing/lib/memsize" | ||
| "github.com/syncthing/syncthing/lib/protocol" | ||
| "github.com/syncthing/syncthing/lib/sync" | ||
| ) | ||
|
|
||
| var ( | ||
| availableMemory int64 | ||
| totalLimit1 int64 | ||
| totalLimit2 int64 | ||
| totalLimitHard int64 | ||
| limit1 int64 | ||
| limit2 int64 | ||
| ) | ||
|
|
||
| func init() { | ||
| if total, err := memsize.MemorySize(); err != nil { | ||
| // availableMemory = config.Options().FallBackMemoryLimit | ||
| availableMemory = 100 << protocol.MiB | ||
| } else { | ||
| availableMemory = total | ||
| } | ||
| setLimits() | ||
| } | ||
|
|
||
| func setLimits() { | ||
| totalLimit1 = availableMemory / 100 * 45 | ||
| totalLimit2 = availableMemory / 100 * 50 | ||
| totalLimitHard = availableMemory / 100 * 55 | ||
| limit1 = availableMemory / 100 * 10 | ||
| limit2 = availableMemory / 100 * 1 | ||
| } | ||
|
|
||
| // Tracks active disk overflow types, with information about how much memory | ||
| // they currently occupy and whether they were already told to spill to disk. | ||
| var lim = limiter{ | ||
| sizes: make(map[int]int64), | ||
| spilling: make(map[int]struct{}), | ||
| mut: sync.NewRWMutex(), | ||
| } | ||
|
Member
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 understand the noble idea here, but this thing adds a lot of complexity. Perhaps we should specify the size as we initialize these types, and like adults divide it in half if we know we are creating two of these containers.
Member
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. On the other hand scans happen in parallel, so being able to have a good grip on this might be hard. |
||
|
|
||
| type limiter struct { | ||
| totalSize int64 | ||
| sizes map[int]int64 | ||
| spilling map[int]struct{} | ||
| mut sync.RWMutex | ||
| } | ||
|
|
||
| func (li *limiter) register() int { | ||
| var key int | ||
| li.mut.Lock() | ||
| for ok := true; ok; _, ok = li.sizes[key] { | ||
| key = rand.Int() | ||
| } | ||
| li.sizes[key] = 0 | ||
| li.mut.Unlock() | ||
| return key | ||
| } | ||
|
|
||
| func (li *limiter) deregister(key int) { | ||
| li.mut.Lock() | ||
| li.totalSize -= li.sizes[key] | ||
| delete(li.sizes, key) | ||
| delete(li.spilling, key) | ||
| li.mut.Unlock() | ||
| } | ||
|
|
||
| // The returned bool is true, if the container can keep using memory | ||
| func (li *limiter) add(key int, size int64) bool { | ||
| li.mut.Lock() | ||
|
imsodin marked this conversation as resolved.
Resolved
|
||
| defer li.mut.Unlock() | ||
| if _, ok := li.spilling[key]; ok || li.shouldSpillLocked(key) { | ||
| return false | ||
| } | ||
| li.totalSize += size | ||
| li.sizes[key] += size | ||
| return true | ||
| } | ||
|
|
||
| func (li *limiter) size(key int) int64 { | ||
| li.mut.RLock() | ||
| defer li.mut.RUnlock() | ||
| return li.sizes[key] | ||
| } | ||
|
|
||
| func (li *limiter) remove(key int, size int64) { | ||
| li.mut.Lock() | ||
| li.totalSize -= size | ||
| li.sizes[key] -= size | ||
| li.mut.Unlock() | ||
| } | ||
|
|
||
| func (li *limiter) shouldSpillLocked(key int) bool { | ||
| switch s := li.sizes[key]; { | ||
| case li.totalSize > totalLimitHard: | ||
| return true | ||
| case li.totalSize > totalLimit2: | ||
| return s > limit2 | ||
| case li.totalSize > totalLimit1: | ||
| return s > limit1 | ||
| } | ||
| return false | ||
| } | ||
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.
Didn't see this used.