Having --jobs be per-repository does not make sense when serving a lot of repositories. Eg, with 10k repositories, even --jobs=2 means there can be 20k concurrent worker threads. Which is entirely too many to be any kind of resource limit. At the same time, only 2 jobs per repository is entirely too few. And using any reasonable number of --jobs, like 100, with so many repositories leads to high memory use.

If Annex state for repositories changes to be demand loaded and freed with a LRU cache, as seems likely for p2phttp startup time with many repositories, then this would change to --jobs times the size of the cache. That is better, but still too many. With the LRU cache sized to 100 repositories (which seems reasonable if you have 10k total), and --jobs=100, 10k worker threads is still too many.

A plan to fix this:

  • Demand load repository states, with an LRU cache. (done)

  • Add --activerepositories=N which is the total number of repository states that can be in memory at one time. (done)

  • For fairness, when a request comes in for a repository, and it's not in the LRU cache, and the cache is full, the oldest repository state in the LRU needs to be removed and freed.

    So, there is a fast path and a slow path in getting the repository state for a request. The fast path looks it up in the LRU cache, and can be parallized. The slow path is serialized across all requests using it. It removes the oldest one from the LRU cache, and cleanly shuts it down (including waiting for any running worker threads and quienscing all its WorkerPool). Then the new repository state is loaded, and is added to the LRU cache. (done)

  • Need to block perRepoServerStateShutdown until all Annex actions run in that repo have stopped. Currently Annex actions can be left running, leading to resource leaks. (done)

  • openLocks can need to persist for a while (up to 10 minutes). To avoid losing it when freeing a repo state, make all repo states share the same openLocks TMVar. (done)

  • Have allocateAnnexWorker use a quantity semaphore shared across all repositories, to limit the total number of active worker threads to --jobs. (done)

  • Keep the WorkerPool as-is, but change it to be sized via --workers=N. Each active repository in the LRU cache will have its own WorkerPool. So the memory used by p2phttp will be --activerepositories times --workers. (done)

    (This is not necessarily optimal, but it's expedient, since the WorkerPool management is complicated. It might be possible to have a counter for the total number of IdleWorkers in the WorkerPools across all repositories, and when there are too many, remove one from the WorkerPool of the oldest repository in the LRU cache. That would allow a busy repository to have a larger WorkerPool, and less busy, but still recently used repository to have a smaller WorkerPool. So memory use would be more optimal, and the --workers switch would not be needed (--jobs would be enough).)

  • The ProxyRunnerMap gets lost when a repo state is freed, which will probably leak resources. It should be ok to just shut down all proxies for a repo when freeing its state. TODO


Work underway in p2phttp_jobs_scaling branch. --Joey

Update: At this point, I have addressed all of the above except for proxies. It still needs to be tested fully.

Known problems:

  • When --activeconnections is low and activity is high, there is a race that leads to "internal server error".

    getPerRepoServerState can return a PerRepoServerState and then it gets shutdown before the request calls acquireP2PConnection or handleRequestAnnex.

    For this to happen, the server has to be under so much load that the state, having been put to the front of the LRU cache by getPerRepoServerState, becomes the oldest thing in the LRU cache before the request handler gets a chance to run at all. So --activeconnections=1 is a good way to see it, but with more load, it could happen with more active connections.