Skip to content

WIP all: Add container types with disk overflow - #4980

Closed
imsodin wants to merge 15 commits into
syncthing:masterfrom
imsodin:diskOverflow
Closed

imsodin wants to merge 15 commits into
syncthing:masterfrom
imsodin:diskOverflow

Conversation

@imsodin

@imsodin imsodin commented Jun 1, 2018

Copy link
Copy Markdown
Member

Purpose

Initial implementation of what I talked about in #4976. The idea is to restrict memory used in big slices/maps based on total available memory.

Todo

  • Testing
  • Total memory on freebsd, openbsd and dragonfly is new, does it work?
  • Fallback if getting total memory fails.
  • Actually think about where to use these types instead of dropping it all over scanner and puller - which might be totally fine though :P

Testing

Only existing ones so far, so only in-memory operations are tested, disk-spilling is untested.

Restrict memory used in big slices/maps based on total available memory.
@calmh

calmh commented Jun 1, 2018

Copy link
Copy Markdown
Member

Is this your code, or where does it come from?

@imsodin

imsodin commented Jun 1, 2018

Copy link
Copy Markdown
Member Author

Very originally it was your index sorting code, but it became quite unrelated (I rewrote almost the entire thing a few times...). However https://github.com/syncthing/syncthing/pull/4980/files#diff-dbf2b1d1fa6344048d9bc7c2ce21be8aR167 is probably still the same (or I copied it from some lib/db file, don't remember), in which case I should keep 2016 as copyright date (?).

@calmh

calmh commented Jun 1, 2018

Copy link
Copy Markdown
Member

Oh alright I misunderstood your comment on the issue that was not original code, but then I get it.

Comment thread lib/diskoverflow/map.go Outdated
if m.spilling {
m.inactive.iter(fn)
}
m.iter(fn)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not respect fn returning false, and calls iteration function again with the second container

Comment thread lib/diskoverflow/common.go Outdated
}

func (s ValueFileInfoSlice) FromByte(v []byte) {
var f protocol.FileInfo

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These protocol specific types should not be in this package.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better in bep_extensions.go? It was a coin toss for me, because it's both diskoverflow specific (implemented interface) and protocol specific (underlying type).

Comment thread lib/diskoverflow/common.go Outdated
type Value interface {
Bytes() int64 // estimated size in memory in bytes
ToByte() []byte
FromByte([]byte)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely:

encoding.BinaryMarshaller
encoding.BinaryUnmarshaller
Size() int64

Makes more sense?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These encoding interfaces allow for failure, i.e. return an error. I really don't know how to handle an error gracefully and littering err!=nil->panic all over the place is ugly. Naming is definitely shit, Marshal, Unmarshal and Size is much better.

Comment thread lib/diskoverflow/limiter.go Resolved
Comment thread lib/diskoverflow/debug.go
}

func shouldDebug() bool {
return l.ShouldDebug("diskoverflow")

Copy link
Copy Markdown
Member

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.

Comment thread lib/diskoverflow/slice.go Outdated
Iter(fn func(v Value) bool, rev bool)
IterAndClose(fn func(v Value) bool, rev bool)
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably want indexing

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only implemented what's already needed, and indexing isn't. Can definitely be added.

Comment thread lib/diskoverflow/slice.go Outdated
if !fn(s.values[i]) {
return
}
if closing {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the use case for this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it would make sense if we were somehow appending as we iterate, but we never do that, so doing all of this stuff potentially slows things down, and doesn't alleviate memory pressure as I suspect it's unlikely for GC to be invoked while while we are dereferencing things.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[...] as I suspect it's unlikely for GC to be invoked while while we are dereferencing things.

E.g. scanning can take a long time, where I'd expect GC to kick in. Also huge sync operations (e.g. resolving conflicts on initial sync) do take a lot of time on a "normal" laptop. And all of this only happens when the container is bigger than 10MB.

Comment thread lib/diskoverflow/sorted.go Resolved
Comment thread lib/diskoverflow/sorted.go Outdated
func (s *sorted) iterImpl(fn func(v Value) bool, rev, closing bool) {
if closing {
defer s.Close()
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WHy is this needed if IterAndClose calls s.Close() anyway?

Comment thread lib/diskoverflow/sorted.go Outdated
comp = 1
}
for aok && iok {
if bytes.Compare(av.Key(), iv.Key()) == comp {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be insertion sort I guess. Also, this has the caveat that the key has to be unique, otherwise you lose values.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Insertion sort is just a lot slower than the current insert sequentially and sort when a value is queried for the first time. And I indeed did overlook the uniqueness problem, but that's easily solvable by adding an insertion counter at the end of the key.

@calmh calmh left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some general comments...

  • There's a lot of moving parts and channels and async and routines and stuff in the Sorted.iterImpl. I didn't understand it all and would like some comments. But more importantly it looks like most of it is to manage the fact that there may be several storage backends in play that need to be merged? This could be simplified by just moving everything from in-memory to on-disk when the first spill happens and always just having one backend?

  • I've grown less fond of the function based iterator and would prefer something like the iterators in leveldb - something you create, call Next() and Value() and so forth on in a loop, and then have a deferred Close() on. I think this makes for better code at the call site, maybe something to look into?

  • I don't think these types should concern themselves with memory sizing, instead just taking a maximum size at construction time. That size could potentially be something auto generated, but mostly I think it's good enough to have a configurable that defaults to something like 8 or 16 megs - just to avoid disk spilling for trivial operations.

@imsodin

imsodin commented Oct 19, 2018

Copy link
Copy Markdown
Member Author
  • I've grown less fond of the function based iterator and would prefer something like the iterators in leveldb - something you create, call Next() and Value() and so forth on in a loop, and then have a deferred Close() on. I think this makes for better code at the call site, maybe something to look into?

I implemented this "object style" iterators. Personally I don't even like the code on the call site. On the implementation side it's anyway pretty ugly (maybe I just don't know how to make it pretty). And I didn't implement the full functionality of e.g. the leveldb iterator (that would be a huge pain to do). What do you think? 986af92

  • I don't think these types should concern themselves with memory sizing, instead just taking a maximum size at construction time. That size could potentially be something auto generated, but mostly I think it's good enough to have a configurable that defaults to something like 8 or 16 megs - just to avoid disk spilling for trivial operations.

I'd like to avoid disk spilling at all times. In my personal experience disk io is the weakest point of syncthing's performance - especially when huge sync operations happen. That's exactly when disk spilling of these types might also happen. So I really only want it to happen, if OOM is a danger. With a per object limit or with a limit that does not depend on system RAM, even on a system with plenty of RAM Syncthing will do more disk trashing.

@st-review

Copy link
Copy Markdown

🤖 beep boop

I'm going to close this pull request as it has been idle for more than 90 days.

This is not a rejection, merely a removal from the list of active pull requests that is periodically reviewed by humans. The pull request can be reopened when there is new activity, and merged once any remaining issues are resolved.

@st-review st-review closed this Jan 18, 2019
@st-review st-review added the frozen-due-to-age Issues closed and untouched for a long time, together with being locked for discussion label Jan 18, 2020
@syncthing syncthing locked and limited conversation to collaborators Jan 18, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

frozen-due-to-age Issues closed and untouched for a long time, together with being locked for discussion

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants