NixOS / hydra

Hydra, the Nix-based continuous build system
http://nixos.org/hydra
GNU General Public License v3.0
1.1k stars 291 forks source link

Add build-redirect endpoint #1290

Open mkenigs opened 1 year ago

mkenigs commented 1 year ago

This adds a /build-redirect/$basename endpoint that redirects to the first build of /nix/store/$basename. The first build is determined by taking the minimum of build ID.

Currently, the only way to find the build for a store path is using the /search endpoint, but a request to that endpoint is so slow it will timeout:

$ time curl -H "Accept: application/json" https://hydra.nixos.org/search?query=/nix/store/v02pl5dhayp8jnz8ahdvg5vi71s8xc6g-hello-2.12.1
{"error":"DBIx::Class::Storage::DBI::_dbh_execute(): DBI Exception: DBD::Pg::st execute failed: ERROR:  canceling statement due to statement timeout [for Statement \"SELECT me.id, me.finished, me.timestamp, me.jobset_id, me.job, me.nixname, me.description, me.drvpath, me.system, me.license, me.homepage, me.maintainers, me.maxsilent, me.timeout, me.ischannel, me.iscurrent, me.priority, me.globalpriority, me.starttime, me.stoptime, me.iscachedbuild, me.buildstatus, me.size, me.closuresize, me.releasename, me.keep, me.notificationpendingsince FROM builds me LEFT JOIN buildoutputs buildoutputs ON buildoutputs.build = me.id WHERE ( buildoutputs.path ILIKE ? ) ORDER BY id desc LIMIT ?\" with ParamValues: 1='%/nix/store/v02pl5dhayp8jnz8ahdvg5vi71s8xc6g-hello-2.12.1%', 2='10'] at /nix/store/sw39pc6gwfwadr0f3nz6bzsf63qwpcwg-hydra-0.1.20221124.0118770/libexec/hydra/lib/Hydra/Controller/Root.pm line 486\n"}
real    0m22.935s
user    0m0.070s
sys 0m0.019s

The new endpoint uses an index on BuildOutputs.path. Here is an explain of the SQL query executed by the new endpoint:

hydra=# explain analyze SELECT MIN( me.build ) FROM buildoutputs me WHERE ( path = '...store path...' );
                                                             QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=12.50..12.51 rows=1 width=4) (actual time=0.024..0.025 rows=1 loops=1)
   ->  Bitmap Heap Scan on buildoutputs me  (cost=4.03..12.49 rows=4 width=4) (actual time=0.019..0.020 rows=2 loops=1)
         Recheck Cond: (path = '...store path...'::text)
         Heap Blocks: exact=1
         ->  Bitmap Index Scan on indexbuildoutputspath  (cost=0.00..4.03 rows=4 width=0) (actual time=0.010..0.010 rows=2 loops=1)
               Index Cond: (path = '...store path...'::text)
 Planning Time: 0.134 ms
 Execution Time: 0.060 ms
(8 rows)
mkenigs commented 11 months ago

@grahamc @edolstra does the general direction of this patch look like something that could be merged at some point?