5 ms·
Each Gitaly server instance was fork/exec'ing Git processes about 20 times per second so we seemed to finally have a very promising lead. What's really wrong h
by Animats 9y ago
Each Gitaly server instance was fork/exec'ing Git processes about 20 times per second so we seemed to finally have a very promising lead.
What's really wrong here is that they're apparently spawning processes like crazy. Do they spawn a new process for each API call? That's like running CGI programs under Apache, like it's 1995.
- dchest 9y agoNothing wrong with it. In fact, I wish spawning processes was more common. It's beneficial for security.
- mschuster91 9y ago> It's beneficial for security ... and for RAM usage. Java applications all have a tendency to bloat the longer you keep them running.
- paulfurtado 9y agoIt's not bloat or memory leaks per se, the JVM just does not return memory to the OS after it is freed. To limit its memory usage, tune the heap size. To fully allocate the heap on startup for consistent usage, use -XX:+AlwaysPreTouch
- axaxs 9y agoJava has always been 'use memory to increase speed...sometimes'. You can tune it some, sure, but that's what it's known to do.
- chinhodado 9y agoLast time I checked, I still couldn't control the max heap free ratio, because apparently that option/flag just doesn't work with Java 8's default GC.
- nurettin 9y agoBack when I tried running a jruby application on 800mb ram, it bloated, then started throwing "OutOfMemoryError"s and "Insufficient Class Space" or something similar. Apparently jruby was generating too many new types at runtime to accommodate rails framework. Garbage collector was pretty garbage at it's job back in 2011.
- readittwice 9y agoAFAIR JRuby creates a Java class for each Ruby method. Classes were part of the Permanent Generation, so sometimes this generation was too small escpecially when using Rails. It was enough to just increase the size of this generation with -XX:MaxPermSize (-Xmx doesn't increase the permanent generation's size). The Permanent Generation was named that way since objects in there were never collected. For most applications this isn't really a problem. Running JRuby+Rails just allocated a lot of classes in this generation, so the default size was too small. But still, the permanent generation was quite small compared to the heap size. I wouldn't really call the GC bad because of this, IMHO they were already quite good back then. And in Java 8 the permanent generation was replaced with the Metaspace, objects in there can be free'd and the Metaspace can be expanded at runtime so it's less likely to get these OOM errors for the permanent generation.
- nurettin 9y agoThat was my experience as well. Increase maxpermsize and reboot every night.
- arghwhat 9y agoThat's mostly just a Java thing, though. Java has a weird relationship with memory.
- kjksf 9y agoProcess isolation is good for security. Parsing text data in ad-hoc, non-standardized, not documented, not defined format is really bad for security. Just spawning a process creates as many security problems as it solves. If it was done right, it would look like Chrome architecture, where untrusted, isolated processes can do dangerous work but communicate with trusted process via well defined IPC protocol.
- pjmlp 9y agoYes, but there is no need to spawn them all the time. A parser service daemon, or a pool of them can be used instead, getting requests from the main application process.
- sreque 9y agoSpawning a process, even if its just vfork() followed by exec(), is very very expensive compared to spawning a thread, and even then most apps created threads on startup and then just cache them for the lifetime of the application. Spawning a process 20 times a second honestly isn't terrible, but even hundreds per second can be noticeably slow IIRC.
- azinman2 9y agoI thought Linux treated forks and processes the same, where as the former is just a special case?
- saghm 9y ago`fork` is how you create a new process on Linux. Threads, which GP was talking about, are different, especially Go threads, which are not the same as OS threads.
- ereyes01 9y agoI believe GP was referring to the `clone` system call, which both fork and pthread_create use under the covers. The difference is what happens to the memory-resident data- the forked/child program shares the address space of the parent until either writes to a page, in which case the page is copied such that each process gets its own unique copy. This is known as copy-on-write (COW). Pthreads, on the other hand, outright share the process's address space and must implement their own synchronization via locks or whatever. Obviously, the performance of fork vs. pthread_create could differ dramatically depending on what the program does. Goroutines are a layer of abstraction above this. They might run on different threads concurrently- the Go runtime controls what happens here and may differ on various architectures / OSes. If you break into a running Go program on Linux with gdb, there's definitely a bunch of pthreads running, maybe for goroutines, and probably for garbage collection, and other stuff. (If you want to actually debug go code, you should of course use something like delve). http://man7.org/linux/man-pages/man2/clone.2.html http://man7.org/linux/man-pages/man2/clone.2.html
- CapacitorSet 9y agoI wonder how much it would speed up if they were using libgit2 directly.
- sytse 9y agoI'm at GitLab but not on the Gitaly team. I think we are using libgit2 but that it doesn't contain all the calls we need.
- CapacitorSet 9y agoReading through an old issue (can't link right now, am on mobile), it seems that the main reason for not using libgit2 in Gitaly is performance, since it would read too many unused files.
- zegerjan 9y agoThe Gitaly server has a Ruby component, but also a Go component. The Ruby server uses Rugged[1] and Gollum-lib[2] which both use libgit2. The Go component doesn't have libgit2 binding yet, although we're looking into adding that later. That or maybe go-git[3]. But for now Gitaly is mainly focussed at migrating all git calls from the Rails monolith. Not introducing a new component now reduces the risks this project has. [1]: https://github.com/libgit2/rugged/ https://github.com/libgit2/rugged/ [2]: https://gitlab.slack.com/archives/C027N716H/p1516954304000263 https://gitlab.slack.com/archives/C027N716H/p151695430400026... [3]: https://github.com/src-d/go-git/ https://github.com/src-d/go-git/
- chriscool 9y agolibgit2 has had bugs related to file locking (for example when you make some ref changes while garbage collecting) and libgit2 does not implement all the git features, so you cannot do everything using libgit2 anyway.
- masklinn 9y agoAssuming they're currently pure git, binding to libgit2 would require using cgo, which could have far-ranging (negative) consequences.
- deleted 9y ago[deleted]
- revelation 9y agoIt's even worse. Gitaly is a program that takes loosely-validated externally-triggered requests and turns them into Git command lines to be exec()ed. So every API request transmutes its input into one or more Git command lines that are exec()ed, each one invoking fork() on the main massively-parallel Gitaly process (well, used to anyway). It's like a terrible China router firmware, without the C. Bonus points for every straightforward way of running a throwaway command on Linux invoking fork(). I guess it's a good thing because it sets us up for another blog post once they learn of the latency gains to be had when you are not creating new processes on API requests. Hell, when someone starts looking into how this Git thing works, we might be in for a whole series.
- Animats 9y agoDo they just do that for commands that make changes, or do they do it for pure read commands as well? Most of the volume is in reads, especially since so many build systems now read directly from Github.
- revelation 9y agoHere is the "reference exists" that the blog post alludes to: https://gitlab.com/gitlab-org/gitaly/blob/master/internal/service/ref/refexists.go https://gitlab.com/gitlab-org/gitaly/blob/master/internal/se... I'm not an expert in this abomination, but it looks all the world like invoking "git show-ref --verify". (Of course a Git ref is usually just a file in .git with a SHA1 in it. They don't care about the SHA1, so really they are launching a Git process for a file exists operation. This used to take them 400ms, but now it's only 100ms!)
- lbotos 9y agoCurious, how would you do it? (disclaimer: I work at GitLab, not on this project though)
- revelation 9y agoSomeone mentioned libgit2, and that is a good first step fix. There is no need to launch a process that calls libgit2 when you can call high-level libgit2 functions yourself. That already eliminates this problem entirely, along with all the other ickiness that comes with running command line tools programmatically. But ultimately this is the main Git interface for the remainder of the site, and it is apparently already sharded so only has to deal with a limited number of repositories. You can use libgit2 on a low-enough level that you can just keep and mutate repository state in memory. Something like a ref exists should be just a hash table lookup, and there are a bunch of other commands where gains can be had when you are not starting from scratch on every API call. (This is what github is doing. They started out with grit, which was some parts of Git reimplemented in Ruby and then launching git for heavy-weight stuff. Nowadays they use rugged, the ruby bindings for libgit2.)
- paulddraper 9y agoI am tired of this "processes are expensive" bullcrap. (At least for Linux.) $ time seq 1000 | while read; do sleep 0 & done real 0m0.185s user 0m0.546s sys 0m0.265s That's less than .2ms to start a process. Processes give you operational control (CPU, memory, permissions, isolation, monitoring) that other constructs simple cannot. Decades ago when we had far slower computers, people were doing process-oriented development and forking as if it was okay (CGI, make, git). Somehow, separate processes came to be avoided like the plague, when in reality, they are probably the smallest resource "waste" in 99% of systems.
- arghwhat 9y agoThis is a terrible microbenchmark. First of all, you're only benchmarking the time it takes for fork(2) to return in the parent subshell, nothing else. The new processes don't exist yet at this point, and certainly hasn't exec'd (which tends to be why you're forking). Second, you're not measuring the cost at all. The forked children will, at some point, start executing on other CPUs, which includes finishing configuration and running exec, which takes time. The cost is the total cycles it takes before the child is executing the intended code. Fork is damn expensive, but whether they're too expensive depends on the usecase, and the cost of expanding hardware. Fork time scales with the virtual memory of the forking process, and you're forking from a fresh subshell that hardly has anything allocated. It's even mentioned in the linked post that their issue stemmed from this (specifically fork lock contention spiking as fork time increased).
- paulddraper 9y ago(1) The benchmark measured the point of discussion. (2) Even not using asynchronity (which Go is heralded for), processes take <2ms to start and stop. Not nothing, but certainly something you could do hundreds of times a second. $ time seq 1000 | while read; do sleep 0; done real 0m1.644s user 0m1.065s sys 0m0.672s
- arghwhat 9y ago