Skip to content

advertise transport encoding in http transfers to - #7712

Merged
edolstra merged 1 commit into
NixOS:masterfrom
Mic92:advertise-compressions
Feb 9, 2023
Merged

edolstra merged 1 commit into
NixOS:masterfrom
Mic92:advertise-compressions

Conversation

@Mic92

@Mic92 Mic92 commented Jan 29, 2023

Copy link
Copy Markdown
Member

tl;dr: With this 1 line change I was able to get a speedup of 1.5x on 1Gbit/s wan connections by enabling zstd compression in nginx.

Also nix already supported all common compression format for http transfer, webservers usually only enable them if they are advertised through the Accept-Encoding header.

This pull requests makes nix advertises content compression support for zstd, br, gzip and deflate.

It's particular useful to add transparent compression for binary caches that serve packages from the host nix store in particular nix-serve, nix-serve-ng and harmonia.

I tried so far gzip, brotli and zstd, whereas only zstd was able to bring me performance improvements for 1Gbit/s WAN connections.

The following nginx configuration was used in combination with the zstd module and harmonia

{
  services.nginx.virtualHosts."cache.yourhost.com" = {
    locations."/".extraConfig = ''
      proxy_pass http://127.0.0.1:5000;
      proxy_set_header Host $host;
      proxy_redirect http:// https://;
      proxy_http_version 1.1;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection $connection_upgrade;

      zstd on;
      zstd_types application/x-nix-archive;
    '';
  };
}

For testing I unpacked a linux kernel tarball to the nix store using this command nix-prefetch-url --unpack https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.1.8.tar.gz.

Before:

$ nix build && rm -rf /tmp/hello  && time ./result/bin/nix copy --no-check-sigs --from https://cache.thalheim.io --to 'file:///tmp/hello?compression=none' '/nix/store/j42mahch5f0jvfmayhzwbb88sw36fvah-linux-6.1.8.tar.gz'
warning: Git tree '/scratch/joerg/nix' is dirty

real    0m18,375s
user    0m2,889s
sys     0m1,558s

After:

$ nix build && rm -rf /tmp/hello  && time ./result/bin/nix copy --no-check-sigs --from https://cache.thalheim.io --to 'file:///tmp/hello?compression=none' '/nix/store/j42mahch5f0jvfmayhzwb
b88sw36fvah-linux-6.1.8.tar.gz'

real    0m11,884s
user    0m4,130s
sys     0m1,439s

Signed-off-by: Jörg Thalheim [email protected]

Motivation

Context

Checklist for maintainers

Maintainers: tick if completed or explain if not relevant

  • agreed on idea
  • agreed on implementation strategy
  • tests, as appropriate
    • functional tests - tests/**.sh
    • unit tests - src/*/tests
    • integration tests - tests/nixos/*
  • documentation in the manual
  • code and comments are self-explanatory
  • commit message explains why the change was made
  • new feature or bug fix: updated release notes

Comment thread src/libstore/filetransfer.cc Outdated
this->result.data.append(data);
})
{
requestHeaders = curl_slist_append(requestHeaders, "Accept-Encoding: zstd, br, gzip, deflate");

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.

We can't easily get this list from curl or at compile time?

@Mic92 Mic92 Jan 30, 2023 •

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.

@SuperSandro2000 this is actually not curl decompressing but nix has its own decompression sinks below. In fact my first version was just doing curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, ""); (see docs) and disable having nix doing the decompression afterwards (or else it would break). I choose this way because it made the diff smaller and therefore more likely to be merged as nix reviewers are usually quite busy.

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.

Except from brotli, the decompression is transparently handled by libarchive IIRC. So ideally we could just query libarchive for that, but after a quick search I couldn't find a way to do that.

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.

@thufschmitt the alternative would be to let curl handle all of the decompression. Just make a call which way do you prefer.

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.

Dunno, I have hardly ever touched these parts of the codebase, and I've no idea why things are this way 🤷‍♂️ I guess @edolstra would have a more informed opinion here.

What would be the cost of letting curl handle it? Should it just be a matter of setting CURLOPT_ACCEPT_ENCODING or is there something more involved?

@edolstra edolstra Jan 30, 2023 •

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.

From what I remember, curl doesn't support all the formats that Nix supports (e.g. bzip2 and xz, and I think the curl in nixpkgs didn't have brotli enabled at the time). And Nix has to have its own support for these formats anyway. So there isn't much advantage to letting curl handle the decompression.

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 think the curl in nixpkgs didn't have brotli enabled at the time

I changed that a good while ago and moved brotili support from curlFull to curl.

So we can just keep this as is and maybe need to update the values manual in the future due to the lack of better options?

@SuperSandro2000

Copy link
Copy Markdown
Member

webservers usually only enable them if they are advertised through the Accept-Encoding header.

They must only send what is written in Accept-Encoding. Thats why in addition to zstd supporting at least brotli and usually gzip, too is necessary for a normal webserver. Most browsers don't have adopted zstd yet or only behind a flag. Not that relevant for us here since nix support brotli for a while.

@NickCao

NickCao commented Jan 30, 2023

Copy link
Copy Markdown
Member

My earlier attempt at this: #6694

@Mic92

Mic92 commented Jan 31, 2023 •

Copy link
Copy Markdown
Member Author

Great @NickCao has implemented the other approach in #6694, where nix offloads decompressing to libcurl. This means both variants are implemented and you just need to merge the one that you prefer. I think both are fine.

@thufschmitt thufschmitt 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.

both variants are implemented and you just need to merge the one that you prefer. I think both are fine.

I actually think we can just merge that one since it's just a one-liner change and then think whether we want the full-curl approach.

So modulo a small suggestion, I think this is good to go

Comment thread src/libstore/filetransfer.cc Resolved
tl;dr: With this 1 line change I was able to get a speedup of 1.5x on 1Gbit/s
wan connections by enabling zstd compression in nginx.

Also nix already supported all common compression format for http
transfer, webservers usually only enable them if they are advertised
through the Accept-Encoding header.

This pull requests makes nix advertises content compression support for
zstd, br, gzip and deflate.

It's particular useful to add transparent compression for binary caches
that serve packages from the host nix store in particular nix-serve,
nix-serve-ng and harmonia.

I tried so far gzip, brotli and zstd, whereas only zstd was able to bring
me performance improvements for 1Gbit/s WAN connections.

The following nginx configuration was used in combination with the
[zstd module](https://github.com/tokers/zstd-nginx-module) and
[harmonia](https://github.com/nix-community/harmonia/)

```nix
{
  services.nginx.virtualHosts."cache.yourhost.com" = {
    locations."/".extraConfig = ''
      proxy_pass http://127.0.0.1:5000;
      proxy_set_header Host $host;
      proxy_redirect http:// https://;
      proxy_http_version 1.1;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection $connection_upgrade;

      zstd on;
      zstd_types application/x-nix-archive;
    '';
  };
}
```

For testing I unpacked a linux kernel tarball to the nix store using
this command `nix-prefetch-url --unpack https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.1.8.tar.gz`.

Before:

```console
$ nix build && rm -rf /tmp/hello  && time ./result/bin/nix copy --no-check-sigs --from https://cache.thalheim.io --to 'file:///tmp/hello?compression=none' '/nix/store/j42mahch5f0jvfmayhzwbb88sw36fvah-linux-6.1.8.tar.gz'
warning: Git tree '/scratch/joerg/nix' is dirty

real    0m18,375s
user    0m2,889s
sys     0m1,558s
```

After:

```console
$ nix build && rm -rf /tmp/hello  && time ./result/bin/nix copy --no-check-sigs --from https://cache.thalheim.io --to 'file:///tmp/hello?compression=none' '/nix/store/j42mahch5f0jvfmayhzwb
b88sw36fvah-linux-6.1.8.tar.gz'

real    0m11,884s
user    0m4,130s
sys     0m1,439s
```

Signed-off-by: Jörg Thalheim <[email protected]>

Update src/libstore/filetransfer.cc

Co-authored-by: Théophane Hufschmitt <[email protected]>
@Mic92
Mic92 force-pushed the advertise-compressions branch from cd27148 to f20d372 Compare February 3, 2023 12:36
@Mic92

Mic92 commented Feb 3, 2023

Copy link
Copy Markdown
Member Author

Squashed.

@edolstra
edolstra merged commit e46429f into NixOS:master Feb 9, 2023
@Mic92
Mic92 deleted the advertise-compressions branch May 2, 2023 18:56
@nixos-discourse

Copy link
Copy Markdown

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/2024-08-12-nix-team-meeting-minutes-168/50561/1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

7 participants