aboutsummaryrefslogtreecommitdiff
path: root/guix-data-service/jobs/load-new-guix-revision.scm
blob: 2d6af349fd6e9397cbb5307a42c4839138d36aec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
;;; Guix Data Service -- Information about Guix over time
;;; Copyright © 2019 Christopher Baines <mail@cbaines.net>
;;;
;;; This program is free software: you can redistribute it and/or
;;; modify it under the terms of the GNU Affero General Public License
;;; as published by the Free Software Foundation, either version 3 of
;;; the License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; Affero General Public License for more details.
;;;
;;; You should have received a copy of the GNU Affero General Public
;;; License along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

(define-module (guix-data-service jobs load-new-guix-revision)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-11)
  #:use-module (srfi srfi-43)
  #:use-module (srfi srfi-71)
  #:use-module (ice-9 match)
  #:use-module (ice-9 threads)
  #:use-module (ice-9 exceptions)
  #:use-module (ice-9 textual-ports)
  #:use-module (ice-9 hash-table)
  #:use-module (ice-9 suspendable-ports)
  #:use-module (ice-9 binary-ports)
  #:use-module ((ice-9 ports internal) #:select (port-poll))
  #:use-module (rnrs bytevectors)
  #:use-module (rnrs exceptions)
  #:use-module (lzlib)
  #:use-module (json)
  #:use-module (squee)
  #:use-module (gcrypt hash)
  #:use-module (fibers)
  #:use-module (fibers channels)
  #:use-module (guix monads)
  #:use-module (guix base32)
  #:use-module (guix store)
  #:use-module (guix channels)
  #:use-module (guix inferior)
  #:use-module (guix profiles)
  #:use-module (guix utils)
  #:use-module (guix i18n)
  #:use-module (guix progress)
  #:use-module (guix packages)
  #:use-module (guix derivations)
  #:use-module (guix serialization)
  #:use-module (guix build utils)
  #:use-module ((guix build syscalls)
                #:select (set-thread-name))
  #:use-module (guix-data-service config)
  #:use-module (guix-data-service database)
  #:use-module (guix-data-service utils)
  #:use-module (guix-data-service model utils)
  #:use-module (guix-data-service model build)
  #:use-module (guix-data-service model system)
  #:use-module (guix-data-service model channel-instance)
  #:use-module (guix-data-service model channel-news)
  #:use-module (guix-data-service model package)
  #:use-module (guix-data-service model package-derivation-by-guix-revision-range)
  #:use-module (guix-data-service model git-repository)
  #:use-module (guix-data-service model guix-revision)
  #:use-module (guix-data-service model package-derivation)
  #:use-module (guix-data-service model guix-revision-package-derivation)
  #:use-module (guix-data-service model license)
  #:use-module (guix-data-service model license-set)
  #:use-module (guix-data-service model lint-checker)
  #:use-module (guix-data-service model lint-warning)
  #:use-module (guix-data-service model lint-warning-message)
  #:use-module (guix-data-service model location)
  #:use-module (guix-data-service model package-metadata)
  #:use-module (guix-data-service model derivation)
  #:use-module (guix-data-service model system-test)
  #:export (fetch-unlocked-jobs
            process-load-new-guix-revision-job
            select-load-new-guix-revision-job-metrics
            select-job-for-commit
            select-jobs-and-events
            select-recent-job-events
            select-unprocessed-jobs-and-events
            select-jobs-and-events-for-commit
            guix-revision-loaded-successfully?
            record-job-event
            enqueue-load-new-guix-revision-job
            most-recent-n-load-new-guix-revision-jobs))

(define inferior-package-id
  (@@ (guix inferior) inferior-package-id))

(define (record-start-time action)
  (simple-format #t "debug: Starting ~A\n" action)
  (cons action
        (current-time)))

(define record-end-time
  (match-lambda
    ((action . start-time)
     (let ((time-taken (- (current-time) start-time)))
       (simple-format #t "debug: Finished ~A, took ~A seconds\n"
                      action time-taken)))))

(define-exception-type &missing-store-item-error &error
  make-missing-store-item-error
  missing-store-item-error?
  (item missing-store-item-error-item))

(define (retry-on-missing-store-item thunk)
  (with-exception-handler
      (lambda (exn)
        (if (missing-store-item-error? exn)
            (begin
              (simple-format (current-error-port)
                             "missing store item ~A, retrying ~A\n"
                             (missing-store-item-error-item exn)
                             thunk)
              (retry-on-missing-store-item thunk))
            (raise-exception exn)))
    thunk
    #:unwind? #t))

(define (inferior-guix-systems inf)
  ;; The order shouldn't matter here, but bugs in Guix can lead to different
  ;; results depending on the order, so sort the systems to try and provide
  ;; deterministic behaviour
  (sort
   (cond
    ((inferior-eval
      '(defined? 'systems
         (resolve-module '(guix platform)))
      inf)

     (remove
      (lambda (system)
        ;; There aren't currently bootstrap binaries for s390x-linux, so this
        ;; just leads to lots of errors
        (string=? system "s390x-linux"))
      (inferior-eval
       '((@ (guix platform) systems))
       inf)))

    (else
     (inferior-eval
      '(@ (guix packages) %supported-systems)
      inf)))
   string<?))

(define (all-inferior-system-tests inf store guix-source guix-commit)
  (define inf-systems
    (inferior-guix-systems inf))

  (define extract
    `(lambda (store)
       (parameterize ((current-guix-package
                       (channel-source->package ,guix-source
                                                #:commit ,guix-commit)))
         (map
          (lambda (system-test)
            (let ((stats (gc-stats)))
              (simple-format
               (current-error-port)
               "inferior heap: ~a MiB used (~a MiB heap)~%"
               (round
                (/ (- (assoc-ref stats 'heap-size)
                      (assoc-ref stats 'heap-free-size))
                   (expt 2. 20)))
               (round
                (/ (assoc-ref stats 'heap-size)
                   (expt 2. 20)))))

            (list (system-test-name system-test)
                  (system-test-description system-test)
                  (filter-map
                   (lambda (system)
                     (simple-format
                      (current-error-port)
                      "guix-data-service: computing derivation for ~A system test (on ~A)\n"
                      (system-test-name system-test)
                      system)
                     (catch
                       #t
                       (lambda ()
                         (cons
                          system
                          (parameterize ((%current-system system))
                            (derivation-file-name
                             (run-with-store store
                               (mbegin %store-monad
                                 (system-test-value system-test)))))))
                       (lambda (key . args)
                         (simple-format
                          (current-error-port)
                          "guix-data-service: error computing derivation for system test ~A (~A): ~A: ~A\n"
                          (system-test-name system-test)
                          system
                          key args)
                         #f)))
                   (list ,@inf-systems))
                  (match (system-test-location system-test)
                    (($ <location> file line column)
                     (list file
                           line
                           column)))))
          (all-system-tests)))))

  (catch
    #t
    (lambda ()
      (inferior-eval
       ;; For channel-source->package
       '(use-modules (gnu packages package-management))
       inf)

      (let ((system-test-data
             (with-time-logging "getting system tests"
               (inferior-eval-with-store/non-blocking inf store extract))))
        system-test-data))
    (lambda (key . args)
      (display (backtrace) (current-error-port))
      (display "\n" (current-error-port))
      (simple-format
       (current-error-port)
       "error: all-inferior-system-tests: ~A: ~A\n"
       key args)

      #f)))

(define locales
  '("cs_CZ.UTF-8"
    "da_DK.UTF-8"
    "de_DE.UTF-8"
    "eo_EO.UTF-8"
    "es_ES.UTF-8"
    "fr_FR.UTF-8"
    "hu_HU.UTF-8"
    "nl_NL.UTF-8"
    "pl_PL.UTF-8"
    "pt_BR.UTF-8"
    ;;"sr_SR.UTF-8"
    "sv_SE.UTF-8"
    "vi_VN.UTF-8"
    "zh_CN.UTF-8"))

(define (inferior-lint-checkers inf)
  (and
   (or (inferior-eval '(and (resolve-module '(guix lint) #:ensure #f)
                            (use-modules (guix lint))
                            #t)
                      inf)
       (begin
         (simple-format (current-error-port)
                        "warning: no (guix lint) module found\n")
         #f))
   (inferior-eval
    `(begin
       (define (lint-descriptions-by-locale checker)
         (let* ((source-locale "en_US.UTF-8")
                (source-description
                 (begin
                   (setlocale LC_MESSAGES source-locale)
                   (G_ (lint-checker-description checker))))
                (descriptions-by-locale
                 (filter-map
                  (lambda (locale)
                    (catch 'system-error
                      (lambda ()
                        (setlocale LC_MESSAGES locale))
                      (lambda (key . args)
                        (error
                         (simple-format
                          #f
                          "error changing locale to ~A: ~A ~A"
                          locale key args))))
                    (let ((description
                           (G_ (lint-checker-description checker))))
                      (setlocale LC_MESSAGES source-locale)
                      (if (string=? description source-description)
                          #f
                          (cons locale description))))
                  (list ,@locales))))
           (cons (cons source-locale source-description)
                 descriptions-by-locale)))

       (map (lambda (checker)
              (list (lint-checker-name checker)
                    (lint-descriptions-by-locale checker)
                    (if (memq checker %network-dependent-checkers)
                        #t
                        #f)))
            %all-checkers))
    inf)))

(define (inferior-lint-warnings inf store checker-name)
  (define lint-warnings-for-checker
    `(lambda (store)
       (let* ((checker-name (quote ,checker-name))
              (checker (find (lambda (checker)
                               (eq? (lint-checker-name checker)
                                    checker-name))
                             %local-checkers))
              (check (lint-checker-check checker)))

         (define lint-checker-requires-store?-defined?
           (defined? 'lint-checker-requires-store?
             (resolve-module '(guix lint))))

         (define (process-lint-warning lint-warning)
           (list
            (match (lint-warning-location lint-warning)
              (($ <location> file line column)
               (list (if (string-prefix? "/gnu/store/" file)
                         ;; Convert a string like
                         ;; /gnu/store/53xh0mpigin2rffg31s52x5dc08y0qmr-guix-module-union/share/guile/site/2.2/gnu/packages/xdisorg.scm
                         ;;
                         ;; This happens when the checker uses
                         ;; package-field-location.
                         (string-join (drop (string-split file #\/) 8) "/")
                         file)
                     line
                     column)))
            (let* ((source-locale "en_US.UTF-8")
                   (source-message
                    (begin
                      (setlocale LC_MESSAGES source-locale)
                      (lint-warning-message lint-warning)))
                   (messages-by-locale
                    (filter-map
                     (lambda (locale)
                       (catch 'system-error
                         (lambda ()
                           (setlocale LC_MESSAGES locale))
                         (lambda (key . args)
                           (error
                            (simple-format
                             #f
                             "error changing locale to ~A: ~A ~A"
                             locale key args))))
                       (let ((message
                              (lint-warning-message lint-warning)))
                         (setlocale LC_MESSAGES source-locale)
                         (if (string=? message source-message)
                             #f
                             (cons locale message))))
                     (list ,@locales))))
              (cons (cons source-locale source-message)
                    messages-by-locale))))

         (vector-map
          (lambda (_ package)
            (map process-lint-warning
                 (with-exception-handler
                     (lambda (exn)
                       (simple-format (current-error-port)
                                      "exception checking ~A with ~A checker: ~A\n"
                                      package checker-name exn)
                       (raise-exception exn))
                   (lambda ()
                     (if (and lint-checker-requires-store?-defined?
                              (lint-checker-requires-store? checker))

                         (check package #:store store)
                         (check package)))
                   #:unwind? #t)))
          gds-inferior-packages))))

  (ensure-gds-inferior-packages-defined! inf)

  (inferior-eval '(and (resolve-module '(guix lint) #:ensure #f)
                       (use-modules (guix lint))
                       #t)
                 inf)

  (with-time-logging (simple-format #f "getting ~A lint warnings"
                                    checker-name)
    (inferior-eval-with-store/non-blocking
     inf
     store
     lint-warnings-for-checker)))

(define (inferior-fetch-system-target-pairs inf)
  (define inf-systems
    (inferior-guix-systems inf))

  (define inf-targets
    (cond
     ((inferior-eval
       '(defined? 'targets
          (resolve-module '(guix platform)))
       inf)
      (sort
       (inferior-eval
        '((@ (guix platform) targets))
        inf)
       string<?))

     (else
      '("arm-linux-gnueabihf"
        "aarch64-linux-gnu"
        "mips64el-linux-gnu"
        "powerpc-linux-gnu"
        "powerpc64le-linux-gnu"
        "riscv64-linux-gnu"
        "i586-pc-gnu"
        "i686-w64-mingw32"
        "x86_64-w64-mingw32"))))

  (define cross-derivations
    `(("x86_64-linux" . ,(remove
                          (lambda (target)
                            ;; Remove targets that don't make much sense
                            (member target
                                    '("x86_64-linux-gnu"
                                      "i686-linux-gnu")))
                          inf-targets))))

  (define supported-system-pairs
    (map (lambda (system)
           (cons system #f))
         inf-systems))

  (define supported-system-cross-build-pairs
    (append-map
     (match-lambda
       ((system . targets)
        (map (lambda (target)
               (cons system target))
             targets)))
     cross-derivations))

  (append supported-system-pairs
          supported-system-cross-build-pairs))

(define (inferior-package-derivations store inf system target start-index count)
  (define proc
    `(lambda (store)
       (define system-target-pair
         (cons ,system ,target))

       (define target-system-alist
         (if (defined? 'platforms (resolve-module '(guix platform)))
             (filter-map
              (lambda (platform)
                (and
                 (platform-target platform)
                 (cons (platform-target platform)
                       (platform-system platform))))
              (platforms))

             '(("arm-linux-gnueabihf"   . "armhf-linux")
               ("aarch64-linux-gnu"     . "aarch64-linux")
               ("mips64el-linux-gnu"    . "mips64el-linux")
               ("powerpc-linux-gnu"     . "powerpc-linux")
               ("powerpc64le-linux-gnu" . "powerpc64le-linux")
               ("riscv64-linux-gnu"     . "riscv64-linux")
               ("i586-pc-gnu"           . "i586-gnu"))))

       (define package-transitive-supported-systems-supports-multiple-arguments? #t)
       (define (get-supported-systems package system)
         (or (and package-transitive-supported-systems-supports-multiple-arguments?
                  (catch
                    'wrong-number-of-args
                    (lambda ()
                      (package-transitive-supported-systems package system))
                    (lambda (key . args)
                      ;; Older Guix revisions don't support two
                      ;; arguments to
                      ;; package-transitive-supported-systems
                      (simple-format
                       (current-error-port)
                       "info: package-transitive-supported-systems doesn't support two arguments, falling back to package-supported-systems\n")
                      (set! package-transitive-supported-systems-supports-multiple-arguments? #f)
                      #f)))
             (catch
               #t
               (lambda ()
                 (package-supported-systems package))
               (lambda (key . args)
                 (simple-format
                  (current-error-port)
                  "error: while processing ~A, unable to compute supported systems\n"
                  (package-name package))
                 (simple-format
                  (current-error-port)
                  "error ~A: ~A\n" key args)
                 #f))))

       (define (derivation-for-system-and-target package system target)
         (catch
           'misc-error
           (lambda ()
             (guard (c ((package-cross-build-system-error? c)
                        #f)
                       ((package-unsupported-target-error? c)
                        #f)
                       ((unsupported-cross-compilation-target-error? c)
                        #f))
               (let ((derivation
                      (if target
                          (package-cross-derivation store package
                                                    target
                                                    system)
                          (package-derivation store package system))))
                 ;; You don't always get what you ask for, so check
                 (if (string=? system (derivation-system derivation))
                     (derivation-file-name derivation)
                     (begin
                       (simple-format
                        (current-error-port)
                        "warning: request for ~A derivation for ~A produced a derivation for system ~A\n"
                        system
                        (package-name package)
                        (derivation-system derivation))
                       #f)))))
           (lambda args
             (simple-format
              (current-error-port)
              "warning: error when computing ~A derivation for system ~A (~A): ~A\n"
              (package-name package)
              system
              (or target "no target")
              args)
             #f)))

       (let ((stats (gc-stats)))
         (simple-format
          (current-error-port)
          "inferior heap: ~a MiB used (~a MiB heap)~%"
          (round
           (/ (- (assoc-ref stats 'heap-size)
                 (assoc-ref stats 'heap-free-size))
              (expt 2. 20)))
          (round
           (/ (assoc-ref stats 'heap-size)
              (expt 2. 20)))))

       (let ((vec (list->vector
                   (iota ,count ,start-index))))
         (vector-map!
          (lambda (_ index)
            (define package (vector-ref gds-inferior-packages index))

            (catch
              #t
              (lambda ()
                (let* ((system (car system-target-pair))
                       (target (cdr system-target-pair))
                       (supported-systems (get-supported-systems package system))
                       (system-supported?
                        (and supported-systems
                             (->bool (member system supported-systems))))
                       (target-supported?
                        (or (not target)
                            (let ((system-for-target
                                   (assoc-ref target-system-alist
                                              target)))
                              (or (not system-for-target)
                                  (->bool
                                   (member system-for-target
                                           (package-supported-systems package)
                                           string=?)))))))

                  (when (string=? (package-name package) "guix")
                    (simple-format
                     (current-error-port)
                     "looking at guix package (supported systems: ~A, system supported: ~A, target supported: ~A\n"
                     supported-systems
                     system-supported?
                     target-supported?))

                  (if system-supported?
                      (if target-supported?
                          (derivation-for-system-and-target package
                                                            system
                                                            target)
                          #f)
                      #f)))
              (lambda (key . args)
                (if (and (eq? key 'system-error)
                         (eq? (car args) 'fport_write))
                    (begin
                      (simple-format
                       (current-error-port)
                       "error: while processing ~A, exiting: ~A: ~A\n"
                       (package-name package)
                       key
                       args)
                      (exit 1))
                    (begin
                      (simple-format
                       (current-error-port)
                       "error: while processing ~A ignoring error: ~A: ~A\n"
                       (package-name package)
                       key
                       args)
                      #f)))))
          vec)
         vec)))

  (inferior-eval
   '(when (defined? 'systems (resolve-module '(guix platform)))
      (use-modules (guix platform)))
   inf)

  (unless (inferior-eval
           '(defined? 'package-unsupported-target-error?
              (resolve-module '(guix packages)))
           inf)
    (inferior-eval
     '(define package-unsupported-target-error? (const #f))
     inf)
    (inferior-eval
     '(define unsupported-cross-compilation-target-error? (const #f))
     inf))

  (format (current-error-port)
          "heap size: ~a MiB~%"
          (round
           (/ (assoc-ref (gc-stats) 'heap-size)
              (expt 2. 20))))

  (catch
    'match-error
    (lambda ()
      (inferior-eval '(invalidate-derivation-caches!) inf))
    (lambda (key . args)
      (simple-format
       (current-error-port)
       "warning: ignoring match-error from calling inferior invalidate-derivation-caches!\n")))

  ;; Clean the cached store connections, as there are caches associated
  ;; with these that take up lots of memory
  (inferior-eval '(when (defined? '%store-table) (hash-clear! %store-table)) inf)

  (inferior-eval-with-store/non-blocking
   inf
   store
   proc))

(define (sort-and-deduplicate-inferior-packages packages
                                                pkg-to-replacement-hash-table)
  (pair-fold
   (lambda (pair result)
     (if (null? (cdr pair))
         (cons (first pair) result)
         (let* ((a (first pair))
                (b (second pair))
                (a-name (inferior-package-name a))
                (b-name (inferior-package-name b))
                (a-version (inferior-package-version a))
                (b-version (inferior-package-version b))
                (a-replacement (hashq-ref pkg-to-replacement-hash-table a))
                (b-replacement (hashq-ref pkg-to-replacement-hash-table b)))
           (if (and (string=? a-name b-name)
                    (string=? a-version b-version)
                    (eq? a-replacement b-replacement))
               (begin
                 (simple-format (current-error-port)
                                "warning: ignoring duplicate package: ~A (~A)\n"
                                a-name
                                a-version)
                 result)
               (cons a result)))))
   '()
   (sort packages
         (lambda (a b)
           (let ((a-name (inferior-package-name a))
                 (b-name (inferior-package-name b)))
             (if (string=? a-name b-name)
                 (let ((a-version (inferior-package-version a))
                       (b-version (inferior-package-version b)))
                   (if (string=? a-version b-version)
                       ;; The name and version are the same, so try and pick
                       ;; the same package each time, by looking at the
                       ;; location.
                       (let ((a-location (inferior-package-location a))
                             (b-location (inferior-package-location b)))
                         (> (location-line a-location)
                            (location-line b-location)))
                       (string<? a-version
                                 b-version)))
                 (string<? a-name
                           b-name)))))))

(define (inferior-packages-plus-replacements inf)
  (let* ((packages
          ;; The use of force in (guix inferior) introduces a continuation
          ;; barrier
          (with-time-logging "calling inferior-packages"
            (call-with-temporary-thread
             (lambda ()
               (inferior-packages inf)))))
         (replacements
          (with-time-logging "getting inferior package replacements"
            (map
             (lambda (inf-pkg has-replacement?)
               (and has-replacement?
                    (inferior-package-replacement inf-pkg)))
             packages
             (inferior-eval
              `(map (lambda (id)
                      (->bool (package-replacement
                               (hash-ref %package-table id))))
                    (list ,@(map inferior-package-id packages)))
              inf))))
         (pkg-to-replacement-hash-table
          (let ((ht (make-hash-table)))
            (for-each
             (lambda (pkg replacement)
               (when replacement
                 (hashq-set! ht
                             pkg
                             replacement)))
             packages
             replacements)
            ht))
         (non-exported-replacements
          (let ((package-id-hash-table (make-hash-table)))
            (for-each (lambda (pkg)
                        (hash-set! package-id-hash-table
                                   (inferior-package-id pkg)
                                   #t))
                      packages)

            (filter
             (lambda (pkg)
               (and pkg
                    (eq? #f
                         (hash-ref package-id-hash-table
                                   (inferior-package-id pkg)))))
             replacements)))

         (deduplicated-packages
          ;; This isn't perfect, sometimes there can be two packages with the
          ;; same name and version, but different derivations.  Guix will warn
          ;; about this case though, generally this means only one of the
          ;; packages should be exported.
          (with-time-logging "deduplicating inferior packages"
            (call-with-temporary-thread
             (lambda ()
               ;; TODO Sort introduces a continuation barrier
               (sort-and-deduplicate-inferior-packages
                (append! packages non-exported-replacements)
                pkg-to-replacement-hash-table)))))

         (deduplicated-packages-length
          (length deduplicated-packages)))

    (inferior-eval
     `(define gds-inferior-packages
        (make-vector ,deduplicated-packages-length))
     inf)

    (inferior-eval
     `(for-each
       (lambda (index id)
         (vector-set! gds-inferior-packages
                      index
                      (or (hashv-ref %package-table id)
                          (error "missing package id"))))
       (iota ,deduplicated-packages-length)
       (list ,@(map inferior-package-id deduplicated-packages)))
     inf)

    (values (list->vector deduplicated-packages)
            pkg-to-replacement-hash-table)))

(define (ensure-gds-inferior-packages-defined! inf)
  (unless (inferior-eval '(defined? 'gds-inferior-packages) inf)
    (with-time-logging "ensuring gds-inferior-packages is defined in inferior"
      (inferior-packages-plus-replacements inf))))

(define* (all-inferior-packages-data inf packages pkg-to-replacement-hash-table)
  (define inferior-package-id->packages-index-hash-table
    (let ((hash-table (make-hash-table)))
      (vector-for-each
       (lambda (i pkg)
         (hash-set! hash-table
                    (inferior-package-id pkg)
                    i))
       packages)
      hash-table))

  (let* ((package-license-data
          (with-time-logging "fetching inferior package license metadata"
            (inferior-packages->license-data inf)))
         (package-metadata
          (with-time-logging "fetching inferior package metadata"
            (vector-map
             (lambda (_ package)
               (let ((translated-package-descriptions-and-synopsis
                      (inferior-packages->translated-package-descriptions-and-synopsis
                       inf package)))
                 (list (non-empty-string-or-false
                        (inferior-package-home-page package))
                       (inferior-package-location package)
                       (car translated-package-descriptions-and-synopsis)
                       (cdr translated-package-descriptions-and-synopsis))))
             packages)))
         (package-replacement-data
          (vector-map
           (lambda (_ pkg)
             (let ((replacement (hashq-ref pkg-to-replacement-hash-table pkg)))
               (if replacement
                   ;; I'm not sure if replacements can themselves be
                   ;; replaced, but I do know for sure that there are
                   ;; infinite chains of replacements (python(2)-urllib3
                   ;; in 7c4c781aa40c42d4cd10b8d9482199f3db345e1b for
                   ;; example).
                   ;;
                   ;; So this might be #f in these cases
                   (let ((index
                          (hash-ref inferior-package-id->packages-index-hash-table
                                    (inferior-package-id replacement))))
                     (unless index
                       (simple-format
                        (current-error-port)
                        "warning: replacement for ~A (~A) is unknown\n"
                        pkg
                        replacement))
                     index)
                   #f)))
           packages)))

    `((names        . ,(vector-map (lambda (_ pkg) (inferior-package-name pkg))
                                   packages))
      (versions     . ,(vector-map (lambda (_ pkg) (inferior-package-version pkg))
                                   packages))
      (license-data . ,package-license-data)
      (metadata     . ,package-metadata)
      (replacements . ,package-replacement-data))))

(define (insert-packages conn inferior-packages-data)
  (let* ((names (assq-ref inferior-packages-data 'names))
         (versions (assq-ref inferior-packages-data 'versions))
         (package-license-set-ids
          (with-time-logging "inserting package license sets"
            (inferior-packages->license-set-ids
             conn
             (inferior-packages->license-id-lists
              conn
              ;; TODO Don't needlessly convert
              (vector->list
               (assq-ref inferior-packages-data 'license-data))))))
         (all-package-metadata-ids
          new-package-metadata-ids
          (with-time-logging "inserting package metadata entries"
            (inferior-packages->package-metadata-ids
             conn
             ;; TODO Don't needlessly convert
             (vector->list
              (assq-ref inferior-packages-data 'metadata))
             package-license-set-ids)))
         (replacement-package-ids
          (vector-map
           (lambda (_ package-index-or-false)
             (if package-index-or-false
                 (first
                  (inferior-packages->package-ids
                   conn
                   (list (list (vector-ref names package-index-or-false)
                               (vector-ref versions package-index-or-false)
                               (list-ref all-package-metadata-ids
                                         package-index-or-false)
                               (cons "integer" NULL)))))
                 (cons "integer" NULL)))
           (assq-ref inferior-packages-data 'replacements))))

    (unless (null? new-package-metadata-ids)
      (with-time-logging "inserting package metadata tsvector entries"
        (insert-package-metadata-tsvector-entries
         conn new-package-metadata-ids)))

    (with-time-logging "getting package-ids (without replacements)"
      (list->vector
       (inferior-packages->package-ids
        conn
        ;; TODO Do this more efficiently
        (zip (vector->list names)
             (vector->list versions)
             all-package-metadata-ids
             (vector->list replacement-package-ids)))))))

(define (insert-lint-warnings conn
                              package-ids
                              lint-checker-ids
                              lint-warnings-data)
  (lint-warnings-data->lint-warning-ids
   conn
   (append-map!
    (lambda (lint-checker-id warnings-per-package)
      (if warnings-per-package
          (vector-fold
           (lambda (_ result package-id warnings)
             (append!
              result
              (map
               (match-lambda
                 ((location-data messages-by-locale)
                  (let ((location-id
                         (location->location-id
                          conn
                          (apply location location-data)))
                        (lint-warning-message-set-id
                         (lint-warning-message-data->lint-warning-message-set-id
                          conn
                          messages-by-locale)))
                    (list lint-checker-id
                          package-id
                          location-id
                          lint-warning-message-set-id))))
               (fold (lambda (location-and-messages result)
                       ;; TODO Sort to delete duplicates, rather than use member
                       (if (member location-and-messages result)
                           (begin
                             (apply
                              simple-format
                              (current-error-port)
                              "warning: skipping duplicate lint warning ~A ~A\n"
                              location-and-messages)
                             result)
                           (append! result
                                    (list location-and-messages))))
                     '()
                     warnings))))
           '()
           package-ids
           warnings-per-package)
          '()))
    lint-checker-ids
    lint-warnings-data)))

(define (update-derivation-ids-hash-table! conn
                                           derivation-ids-hash-table
                                           file-names)
  (define file-names-count (vector-length file-names))

  (simple-format #t "debug: update-derivation-ids-hash-table!: ~A file-names\n"
                 file-names-count)
  (let ((missing-file-names
         (vector-fold
          (lambda (_ result file-name)
            (if (and file-name
                     (hash-ref derivation-ids-hash-table
                               file-name))
                result
                (cons file-name result)))
          '()
          file-names)))

    (simple-format
     #t "debug: update-derivation-ids-hash-table!: lookup ~A file-names, ~A not cached\n"
     file-names-count (length missing-file-names))

    (unless (null? missing-file-names)
      (for-each
       (lambda (chunk)
         (for-each
          (match-lambda
            ((id file-name)
             (hash-set! derivation-ids-hash-table
                        file-name
                        (string->number id))))
          (exec-query conn (select-existing-derivations chunk))))
       (chunk! missing-file-names 1000)))))

(define (insert-missing-derivations postgresql-connection-pool
                                    utility-thread-channel
                                    derivation-ids-hash-table
                                    derivations)

  (define (ensure-input-derivations-exist input-derivation-file-names)
    (unless (null? input-derivation-file-names)
      (simple-format
       #t "debug: ensure-input-derivations-exist: processing ~A derivations\n"
       (length input-derivation-file-names))

      (with-resource-from-pool postgresql-connection-pool conn
        (update-derivation-ids-hash-table! conn
                                           derivation-ids-hash-table
                                           (list->vector
                                            input-derivation-file-names)))
      (simple-format
       #t
       "debug: ensure-input-derivations-exist: checking for missing input derivations\n")
      (let ((missing-derivations-filenames
             (remove (lambda (derivation-file-name)
                       (hash-ref derivation-ids-hash-table
                                 derivation-file-name))
                     input-derivation-file-names)))

        (unless (null? missing-derivations-filenames)
          (simple-format
           #f
           "debug: ensure-input-derivations-exist: inserting missing input derivations\n")
          ;; Ensure all the input derivations exist
          (insert-missing-derivations
           postgresql-connection-pool
           utility-thread-channel
           derivation-ids-hash-table
           (call-with-worker-thread
            utility-thread-channel
            (lambda ()
              (map read-derivation-from-file
                   missing-derivations-filenames))))))))

  (define (insert-into-derivations conn drvs)
    (string-append
     "INSERT INTO derivations "
     "(file_name, builder, args, env_vars, system_id) VALUES "
     (string-join
      (map (match-lambda
             (($ <derivation> outputs inputs sources
                              system builder args env-vars file-name)
              (simple-format
               #f "('~A', '~A', ARRAY[~A]::varchar[], ARRAY[~A], '~A')"
               file-name
               builder
               (string-join (map quote-string args) ",")
               (string-join (map (match-lambda
                                   ((key . value)
                                    (string-append
                                     "['" key '"', $$"
                                     value "$$ ]")))
                                 env-vars)
                            ",")
               (system->system-id conn system))))
           drvs)
      ",")
     " RETURNING id"
     ";"))

  (with-time-logging
      (simple-format
       #f "insert-missing-derivations: inserting ~A derivations"
       (length derivations))
    (let* ((chunks (chunk derivations 500))
           (derivation-ids
            (with-resource-from-pool postgresql-connection-pool conn
              (append-map!
               (lambda (chunk)
                 (map (lambda (result)
                        (string->number (car result)))
                      (exec-query conn (insert-into-derivations conn chunk))))
               chunks))))

      (with-time-logging
          "insert-missing-derivations: updating hash table"
        (for-each (lambda (derivation derivation-id)
                    (hash-set! derivation-ids-hash-table
                               (derivation-file-name derivation)
                               derivation-id))
                  derivations
                  derivation-ids))

      (with-time-logging
          "insert-missing-derivations: inserting sources"
        (for-each
         (lambda (derivation-id derivation)
           (let ((sources (derivation-sources derivation)))
             (unless (null? sources)
               (let ((sources-ids
                      (with-resource-from-pool postgresql-connection-pool conn
                        (insert-derivation-sources conn
                                                   derivation-id
                                                   sources))))
                 (par-map&
                  (lambda (id source-file)
                    (match
                        (with-resource-from-pool postgresql-connection-pool conn
                          (exec-query
                           conn
                           "
SELECT 1 FROM derivation_source_file_nars WHERE derivation_source_file_id = $1"
                           (list (number->string id))))
                      (()
                       (let ((nar-bytevector
                              (call-with-worker-thread
                               utility-thread-channel
                               (lambda ()
                                 (call-with-values
                                     (lambda ()
                                       (open-bytevector-output-port))
                                   (lambda (port get-bytevector)
                                     (unless (file-exists? source-file)
                                       (raise-exception
                                        (make-missing-store-item-error
                                         source-file)))
                                     (write-file source-file port)
                                     (get-bytevector)))))))
                         (letpar&
                             ((compressed-nar-bytevector
                               (call-with-worker-thread
                                utility-thread-channel
                                (lambda ()
                                  (call-with-values
                                      (lambda ()
                                        (open-bytevector-output-port))
                                    (lambda (port get-bytevector)
                                      (call-with-lzip-output-port port
                                        (lambda (port)
                                          (put-bytevector port nar-bytevector))
                                        #:level 9)
                                      (get-bytevector))))))
                              (hash
                               (call-with-worker-thread
                                utility-thread-channel
                                (lambda ()
                                  (bytevector->nix-base32-string
                                   (sha256 nar-bytevector)))))
                              (uncompressed-size (bytevector-length nar-bytevector)))

                           (with-resource-from-pool postgresql-connection-pool conn
                             (insert-derivation-source-file-nar
                              conn
                              id
                              hash
                              compressed-nar-bytevector
                              uncompressed-size)))))
                      (_ #f)))
                  sources-ids
                  sources)))))
         derivation-ids
         derivations))

      (with-resource-from-pool postgresql-connection-pool conn
        (with-time-logging
            "insert-missing-derivations: inserting outputs"
          (for-each (lambda (derivation-id derivation)
                      (insert-derivation-outputs conn
                                                 derivation-id
                                                 (derivation-outputs derivation)))
                    derivation-ids
                    derivations)))

      (with-time-logging
          "insert-missing-derivations: ensure-input-derivations-exist"
        (ensure-input-derivations-exist (deduplicate-strings
                                         (map derivation-input-path
                                              (append-map derivation-inputs
                                                          derivations)))))

      (with-resource-from-pool postgresql-connection-pool conn
        (with-time-logging
            (simple-format
             #f "insert-missing-derivations: inserting inputs for ~A derivations"
             (length derivations))
          (insert-derivation-inputs conn
                                    derivation-ids
                                    derivations))))))

(define (derivation-file-names->derivation-ids postgresql-connection-pool
                                               utility-thread-channel
                                               derivation-file-names)
  (define derivations-count
    (vector-length derivation-file-names))

  (if (= 0 derivations-count)
      #()
      (let* ((derivation-ids-hash-table (make-hash-table
                                         ;; Account for more derivations in
                                         ;; the graph
                                         (* 2 derivations-count))))
        (simple-format
         #t "debug: derivation-file-names->derivation-ids: processing ~A derivations\n"
         derivations-count)

        (with-resource-from-pool postgresql-connection-pool conn
          (update-derivation-ids-hash-table! conn
                                             derivation-ids-hash-table
                                             derivation-file-names))

        (let* ((missing-derivation-filenames
                (deduplicate-strings
                 (vector-fold
                  (lambda (_ result derivation-file-name)
                    (if (not derivation-file-name)
                        result
                        (if (hash-ref derivation-ids-hash-table
                                      derivation-file-name)
                            result
                            (cons derivation-file-name result))))
                  '()
                  derivation-file-names)))
               (missing-derivations-chunked-promises
                (map
                 (lambda (chunk)
                   (fibers-delay
                    (lambda ()
                      (map (lambda (filename)
                             (if (file-exists? filename)
                                 (read-derivation-from-file filename)
                                 (raise-exception
                                  (make-missing-store-item-error
                                   filename))))
                           chunk))))
                 (chunk! missing-derivation-filenames 1000))))

          (for-each
           (lambda (missing-derivation-filenames-chunk)
             (let ((missing-derivations-chunk
                    ;; Do the filter again, since processing the last chunk
                    ;; might have inserted some of the derivations in this
                    ;; chunk
                    (remove! (lambda (derivation)
                               (hash-ref derivation-ids-hash-table
                                         (derivation-file-name
                                          derivation)))
                             (fibers-force
                              missing-derivation-filenames-chunk))))

               (unless (null? missing-derivations-chunk)
                 (insert-missing-derivations postgresql-connection-pool
                                             utility-thread-channel
                                             derivation-ids-hash-table
                                             missing-derivations-chunk))))
           missing-derivations-chunked-promises))

        (let ((all-ids
               (vector-map
                (lambda (_ derivation-file-name)
                  (if derivation-file-name
                      (or (hash-ref derivation-ids-hash-table
                                    derivation-file-name)
                          (error "missing derivation id"))
                      #f))
                derivation-file-names)))

          all-ids))))

(prevent-inlining-for-tests derivation-file-names->derivation-ids)

(define guix-store-path
  (let ((store-path #f))
    (lambda (store)
      (if (and store-path
               (file-exists? store-path))
          store-path
          (let ((config-guix (%config 'guix)))
            (if (and (file-exists? config-guix)
                     (string-prefix? "/gnu/store/" config-guix))
                (begin
                  (set! store-path
                    (dirname
                     (dirname
                      (%config 'guix))))
                  store-path)
                (begin
                  (invalidate-derivation-caches!)
                  (hash-clear! (@@ (guix packages) %derivation-cache))
                  (let* ((guix-package (@ (gnu packages package-management)
                                          guix))
                         (derivation (package-derivation store guix-package)))
                    (with-time-logging "building the guix derivation"
                      (build-derivations store (list derivation)))

                    (let ((new-store-path
                           (derivation->output-path derivation)))
                      (set! store-path new-store-path)
                      (simple-format (current-error-port)
                                     "debug: guix-store-path: ~A\n"
                                     new-store-path)
                      new-store-path)))))))))

(define (nss-certs-store-path store)
  (let* ((nss-certs-package (@ (gnu packages certs)
                               nss-certs))
         (derivation (package-derivation store nss-certs-package)))
    (with-time-logging "building the nss-certs derivation"
      (build-derivations store (list derivation)))
    (derivation->output-path derivation)))

(define (non-blocking-port port)
  "Make PORT non-blocking and return it."
  (let ((flags (fcntl port F_GETFL)))
    (when (zero? (logand O_NONBLOCK flags))
      (fcntl port F_SETFL (logior O_NONBLOCK flags)))
    port))

(define (ensure-non-blocking-store-connection store)
  (match (store-connection-socket store)
    ((? file-port? port)
     (non-blocking-port port))
    (_ #f)))

(define (call-with-temporary-blocking-store store proc)
  (let* ((port (store-connection-socket store))
         (flags (fcntl port F_GETFL)))
    (unless (zero? (logand O_NONBLOCK flags))
      (fcntl port F_SETFL (logxor O_NONBLOCK flags)))
    (call-with-values
        (lambda ()
          (proc store))
      (lambda vals
        (fcntl port F_SETFL (logior O_NONBLOCK flags))
        (apply values vals)))))

(define (make-inferior-non-blocking! inferior)
  (non-blocking-port
   ((@@ (guix inferior) inferior-socket) inferior)))

(define (call-with-temporary-thread thunk)
  (let ((channel (make-channel)))
    (call-with-new-thread
     (lambda ()
       (parameterize
           ((current-read-waiter (lambda (port) (port-poll port "r")))
            (current-write-waiter (lambda (port) (port-poll port "w"))))

         (with-exception-handler
             (lambda (exn)
               (put-message channel `(exception . ,exn)))
           (lambda ()
             (with-throw-handler #t
               (lambda ()
                 (call-with-values thunk
                   (lambda values
                     (put-message channel `(values ,@values)))))
               (lambda _
                 (backtrace))))
           #:unwind? #t))))

    (match (get-message channel)
      (('values . results)
       (apply values results))
      (('exception . exn)
       (raise-exception exn)))))

(define (inferior-eval-with-store/non-blocking inferior store proc)
  (call-with-temporary-thread
   (lambda ()
     (inferior-eval-with-store inferior store proc))))

(define* (channel->source-and-derivation-file-names-by-system
          conn channel
          fetch-with-authentication?
          #:key parallelism)

  (define use-container? (defined?
                           'open-inferior/container
                           (resolve-module '(guix inferior))))

  (define (inferior-code channel-instance system)
    `(lambda (store)
       (let* ((system ,system)
              (instances
               (list
                (channel-instance
                 (channel (name ',(channel-name channel))
                          (url    ,(channel-url channel))
                          (branch ,(channel-branch channel))
                          (commit ,(channel-commit channel)))
                 ,(channel-instance-commit channel-instance)
                 ,(channel-instance-checkout channel-instance)))))
         (simple-format
          (current-error-port)
          "guix-data-service: computing the derivation-file-name for ~A\n"
          system)

         (let ((manifest
                (catch #t
                  (lambda ()
                    ((channel-instances->manifest instances #:system system) store))
                  (lambda (key . args)
                    (simple-format
                     (current-error-port)
                     "error: while computing manifest entry derivation for ~A\n"
                     system)
                    (simple-format
                     (current-error-port)
                     "error ~A: ~A\n" key args)
                    #f))))
           (define (add-tmp-root-and-return-drv drv)
             (add-temp-root store drv)
             drv)

           (simple-format
            (current-error-port)
            "computed the manifest for ~A\n" system)

           `((manifest-entry-item
              . ,(and manifest
                      (add-tmp-root-and-return-drv
                       (derivation-file-name
                        (manifest-entry-item
                         (first
                          (manifest-entries manifest)))))))
             (profile
              . ,(catch #t
                   (lambda ()
                     (and manifest
                          (add-tmp-root-and-return-drv
                           (derivation-file-name
                            (parameterize ((%current-system system))
                              (run-with-store store
                                (profile-derivation
                                 manifest
                                 #:hooks %channel-profile-hooks)))))))
                   (lambda (key . args)
                     (simple-format
                      (current-error-port)
                      "error: while computing profile derivation for ~A\n"
                      system)
                     (simple-format
                      (current-error-port)
                      "error ~A: ~A\n" key args)
                     #f))))))))

  (define (start-inferior inferior-store)
    (let ((inferior
           (if use-container?
               (open-inferior/container
                inferior-store
                (guix-store-path inferior-store)
                #:extra-shared-directories
                '("/gnu/store")
                #:extra-environment-variables
                (list (string-append
                       "SSL_CERT_DIR=" (nss-certs-store-path inferior-store))))
               (begin
                 (simple-format #t "debug: using open-inferior\n")
                 (open-inferior (guix-store-path inferior-store)
                                #:error-port (current-error-port))))))

      ;; /etc is only missing if open-inferior/container has been used
      (when use-container?
        (inferior-eval
         '(begin
            ;; Create /etc/pass, as %known-shorthand-profiles in (guix
            ;; profiles) tries to read from this file. Because the environment
            ;; is cleaned in build-self.scm, xdg-directory in (guix utils)
            ;; falls back to accessing /etc/passwd.
            (mkdir "/etc")
            (call-with-output-file "/etc/passwd"
              (lambda (port)
                (display "root:x:0:0::/root:/bin/bash" port))))
         inferior))

      (inferior-eval '(use-modules (srfi srfi-1)
                                   (ice-9 history)
                                   (guix channels)
                                   (guix grafts)
                                   (guix profiles))
                     inferior)
      (inferior-eval '(%graft? #f)
                     inferior)
      (inferior-eval '(disable-value-history!)
                     inferior)
      (inferior-eval '(define channel-instance
                        (@@ (guix channels) channel-instance))
                     inferior)

      inferior))

  (let* ((channel-instance
          ;; Obtain a session level lock here, to avoid conflicts with
          ;; other jobs over the Git repository.
          (with-advisory-session-lock/log-time
           conn
           'latest-channel-instances
           (lambda ()
             (with-store-connection
              (lambda (store)
                ;; TODO (guix serialization) uses dynamic-wind
                (call-with-temporary-thread
                 (lambda ()
                   (first
                    (latest-channel-instances store
                                              (list channel)
                                              #:authenticate?
                                              fetch-with-authentication?)))))))))
         (pool-store-connections '())
         (inferior-and-store-pool
          (make-resource-pool
           (lambda ()
             (let* ((inferior-store (open-store-connection))
                    (inferior (start-inferior inferior-store)))
               (ensure-non-blocking-store-connection inferior-store)
               (set-build-options inferior-store #:fallback? #t)
               (make-inferior-non-blocking! inferior)
               (call-with-blocked-asyncs
                (lambda ()
                  (set! pool-store-connections
                        (cons inferior-store pool-store-connections))))
               (cons inferior inferior-store)))
           parallelism
           #:min-size 0
           #:idle-seconds 30
           #:destructor (match-lambda
                          ((inferior . store)
                           (close-inferior inferior)
                           (close-connection store)))))
         (systems
          (with-resource-from-pool inferior-and-store-pool res
            (match res
              ((inferior . inferior-store)
               (inferior-eval '(@ (guix packages) %supported-systems)
                              inferior)))))
         (result
          (par-map&
           (lambda (system)
             (with-resource-from-pool inferior-and-store-pool res
               (match res
                 ((inferior . inferior-store)
                  (with-exception-handler
                      (lambda (exn)
                        (if (inferior-protocol-error? exn)
                            (begin
                              (simple-format (current-error-port)
                                             "ignoring ~A for ~A\n"
                                             exn
                                             system)
                              (cons system #f))
                            (raise-exception exn)))
                    (lambda ()
                      (with-throw-handler #t
                        (lambda ()
                          (cons system
                                (inferior-eval-with-store/non-blocking
                                 inferior
                                 inferior-store
                                 (inferior-code channel-instance system))))
                        (lambda _
                          (simple-format
                           (current-error-port)
                           "failed to compute channel instance derivation for ~A\n"
                           system))))
                    #:unwind? #t)))))
           systems)))

    (cons
     (channel-instance-checkout channel-instance)
     result)))

(define* (channel->source-and-derivations-by-system conn channel
                                                    fetch-with-authentication?
                                                    #:key parallelism)
  (match (with-time-logging "computing the channel derivation"
           (channel->source-and-derivation-file-names-by-system
            conn
            channel
            fetch-with-authentication?
            #:parallelism parallelism))
    ((source . derivation-file-names-by-system)
     (for-each
      (match-lambda
        ((system . derivation-file-name)
         (simple-format (current-error-port)
                        "debug: ~A: channel dervation: ~A\n"
                        system
                        derivation-file-name)))
      derivation-file-names-by-system)

     (values source derivation-file-names-by-system))))

(prevent-inlining-for-tests channel->source-and-derivations-by-system)

(define (channel-derivations-by-system->guix-store-item
         channel-derivations-by-system)

  (define (store-item->guix-store-item filename)
    (dirname
     (readlink
      (string-append filename "/bin"))))

  (let ((derivation-file-name-for-current-system
         (assoc-ref
          (assoc-ref channel-derivations-by-system
                     (%current-system))
          'profile)))
    (if derivation-file-name-for-current-system
        (let ((derivation-for-current-system
               (read-derivation-from-file derivation-file-name-for-current-system)))
          (with-time-logging "building the channel derivation"
            (with-store-connection
             (lambda (store)
               (build-derivations store (list derivation-for-current-system)))))

          (values
           (store-item->guix-store-item
            (derivation->output-path derivation-for-current-system))
           derivation-file-name-for-current-system))
        #f)))

(prevent-inlining-for-tests channel-derivations-by-system->guix-store-item)

(define (glibc-locales-for-guix-store-path store store-path)
  (let ((inf (if (defined?
                   'open-inferior/container
                   (resolve-module '(guix inferior)))
                 (open-inferior/container store store-path
                                          #:extra-shared-directories
                                          '("/gnu/store"))
                 (begin
                   (simple-format #t "debug: using open-inferior\n")
                   (open-inferior store-path
                                  #:error-port (current-error-port))))))
    (inferior-eval '(use-modules (srfi srfi-1)
                                 (srfi srfi-34)
                                 (guix grafts)
                                 (guix derivations))
                   inf)
    (inferior-eval '(when (defined? '%graft?) (%graft? #f)) inf)

    (let* ((derivation
            (or
             (and=>
              (inferior-eval-with-store/non-blocking
               inf
               store
               '(lambda (store)
                  (and (defined?
                         'libc-locales-for-target
                         (resolve-module '(gnu packages base)))
                       (derivation-file-name
                        (package-derivation
                         store
                         ((@ (gnu packages base) libc-locales-for-target)))))))
              read-derivation-from-file)
             (inferior-package-derivation
              store
              (first
               (lookup-inferior-packages inf "glibc-locales")))))
           (output (derivation->output-path derivation)))
      (close-inferior inf)
      (with-time-logging "building the glibc-locales derivation"
        (build-derivations store (list derivation)))

      output)))

(define (start-inferior-for-data-extration store store-path guix-locpath
                                           extra-inferior-environment-variables)
  (call-with-blocked-asyncs
   (lambda ()
     (let* ((original-guix-locpath (getenv "GUIX_LOCPATH"))
            (original-extra-env-vars-values
             (map (match-lambda
                    ((key . _)
                     (getenv key)))
                  extra-inferior-environment-variables))
            (inf (begin
                   ;; Unset the GUILE_LOAD_PATH and GUILE_LOAD_COMPILED_PATH to
                   ;; avoid the values for these being used in the
                   ;; inferior. Even though the inferior %load-path and
                   ;; %load-compiled-path has the inferior modules first, this
                   ;; can cause issues when there are modules present outside
                   ;; of the inferior Guix which aren't present in the inferior
                   ;; Guix (like the new (guix lint) module
                   (unsetenv "GUILE_LOAD_PATH")
                   (unsetenv "GUILE_LOAD_COMPILED_PATH")
                   (simple-format (current-error-port) "debug: set GUIX_LOCPATH to ~A\n"
                                  guix-locpath)
                   (for-each
                    (match-lambda
                      ((key . val)
                       (simple-format (current-error-port)
                                      "debug: set ~A to ~A\n"
                                      key val)
                       (setenv key val)))
                    extra-inferior-environment-variables)

                   (if (defined?
                         'open-inferior/container
                         (resolve-module '(guix inferior)))
                       (open-inferior/container store store-path
                                                #:extra-shared-directories
                                                '("/gnu/store")
                                                #:extra-environment-variables
                                                (list (string-append
                                                       "GUIX_LOCPATH="
                                                       guix-locpath)))
                       (begin
                         (setenv "GUIX_LOCPATH" guix-locpath)
                         (simple-format #t "debug: using open-inferior\n")
                         (open-inferior store-path
                                        #:error-port (current-error-port)))))))
       (setenv "GUIX_LOCPATH" original-guix-locpath) ; restore GUIX_LOCPATH
       (for-each
        (lambda (key val)
          (setenv key val))
        (map car extra-inferior-environment-variables)
        original-extra-env-vars-values)

       (when (eq? inf #f)
         (error "error: inferior is #f"))

       ;; Normalise the locale for the inferior process
       (with-exception-handler
           (lambda (key . args)
             (simple-format
              (current-error-port)
              "warning: failed to set locale to en_US.UTF-8: ~A ~A\n"
              key args))
         (lambda ()
           (inferior-eval '(setlocale LC_ALL "en_US.UTF-8") inf)))

       (inferior-eval '(use-modules (srfi srfi-1)
                                    (srfi srfi-34)
                                    (srfi srfi-43)
                                    (ice-9 history)
                                    (guix grafts)
                                    (guix derivations)
                                    (gnu tests))
                      inf)

       (inferior-eval '(disable-value-history!)
                      inf)

       ;; For G_ and P_
       (or (inferior-eval '(and (resolve-module '(guix i18n) #:ensure #f)
                                (use-modules (guix i18n))
                                #t)
                          inf)
           (inferior-eval '(use-modules (guix ui))
                          inf))

       (inferior-eval '(when (defined? '%graft?) (%graft? #f)) inf)

       ;; TODO Have Guix make this easier
       ((@@ (guix inferior) ensure-store-bridge!) inf)
       (non-blocking-port ((@@ (guix inferior) inferior-bridge-socket) inf))

       inf))))

(define* (extract-information-from db-conn guix-revision-id commit
                                   guix-source store-item
                                   guix-derivation
                                   utility-thread-channel
                                   #:key skip-system-tests?
                                   extra-inferior-environment-variables
                                   parallelism)

  (define guix-locpath
    ;; Augment the GUIX_LOCPATH to include glibc-locales from
    ;; the Guix at store-path, this should mean that the
    ;; inferior Guix works, even if it's build using a different
    ;; glibc version
    (string-append
     (with-store-connection
      (lambda (store)
        (glibc-locales-for-guix-store-path store store-item)))
     "/lib/locale"
     ":" (getenv "GUIX_LOCPATH")))

  (define inf-and-store-pool
    (make-resource-pool
     (lambda ()
       (let* ((inferior-store (open-store-connection)))
         (unless (valid-path? inferior-store store-item)
           (simple-format #t "warning: store item missing (~A)\n"
                          store-item)
           (unless (valid-path? inferior-store guix-derivation)
             (simple-format #t "warning: attempting to substitute guix derivation (~A)\n"
                            guix-derivation)
             (ensure-path inferior-store guix-derivation))
           (simple-format #t "warning: building (~A)\n"
                          guix-derivation)
           (build-derivations inferior-store
                              (list (read-derivation-from-file
                                     guix-derivation))))
         ;; Use this more to keep the store-path alive so long as there's a
         ;; inferior operating
         (add-temp-root inferior-store store-item)

         (let ((inferior (start-inferior-for-data-extration
                          inferior-store
                          store-item
                          guix-locpath
                          extra-inferior-environment-variables)))
           (ensure-non-blocking-store-connection inferior-store)
           (make-inferior-non-blocking! inferior)
           (simple-format #t "debug: started new inferior and store connection\n")

           (cons inferior inferior-store))))
     parallelism
     #:min-size 0
     #:idle-seconds 2
     #:destructor
     (match-lambda
       ((inferior . store)
        (simple-format
         #t "debug: closing inferior and associated store connection\n")

        (close-connection store)
        (close-inferior inferior)))))

  (define postgresql-connection-pool
    (make-resource-pool
     (lambda ()
       (with-time-logging
           "acquiring advisory transaction lock: load-new-guix-revision-inserts"
         ;; Wait until this is the only transaction inserting data, to
         ;; avoid any concurrency issues
         (obtain-advisory-transaction-lock db-conn
                                           'load-new-guix-revision-inserts))
       db-conn)
     1
     #:min-size 0))

  (define derivation-file-names->derivation-ids/fiberized
    (fiberize
     (lambda (derivation-file-names)
       (derivation-file-names->derivation-ids
        postgresql-connection-pool
        utility-thread-channel
        derivation-file-names))))

  (define package-ids-promise
    (fibers-delay
     (lambda ()
       (let ((packages-data
              (with-resource-from-pool inf-and-store-pool res
                (match res
                  ((inferior . inferior-store)
                   (with-time-logging "getting all inferior package data"
                     (let ((packages
                            pkg-to-replacement-hash-table
                            (inferior-packages-plus-replacements inferior)))
                       (all-inferior-packages-data
                        inferior
                        packages
                        pkg-to-replacement-hash-table))))))))
         (with-resource-from-pool postgresql-connection-pool conn
           (insert-packages conn packages-data))))))

  (define (extract-and-store-lint-checkers-and-warnings)
    (define inferior-lint-checkers-data
      (with-resource-from-pool inf-and-store-pool res
        (match res
          ((inferior . inferior-store)
           (inferior-lint-checkers inferior)))))

    (when inferior-lint-checkers-data
      (letpar& ((lint-checker-ids
                 (with-resource-from-pool postgresql-connection-pool conn
                   (lint-checkers->lint-checker-ids
                    conn
                    (map (match-lambda
                           ((name descriptions-by-locale network-dependent)
                            (list
                             name
                             network-dependent
                             (lint-checker-description-data->lint-checker-description-set-id
                              conn descriptions-by-locale))))
                         inferior-lint-checkers-data))))
                (lint-warnings-data
                 (par-map&
                  (match-lambda
                    ((checker-name _ network-dependent?)
                     (and (and (not network-dependent?)
                               ;; Running the derivation linter is
                               ;; currently infeasible
                               (not (eq? checker-name 'derivation)))
                          (with-resource-from-pool inf-and-store-pool res
                            (match res
                              ((inferior . inferior-store)
                               (inferior-lint-warnings inferior
                                                       inferior-store
                                                       checker-name)))))))
                  inferior-lint-checkers-data)))

        (let ((package-ids (fibers-force package-ids-promise)))
          (with-resource-from-pool postgresql-connection-pool conn
            (insert-guix-revision-lint-checkers conn
                                                guix-revision-id
                                                lint-checker-ids)

            (let ((lint-warning-ids
                   (insert-lint-warnings
                    conn
                    package-ids
                    lint-checker-ids
                    lint-warnings-data)))
              (chunk-for-each!
               (lambda (lint-warning-ids-chunk)
                 (insert-guix-revision-lint-warnings conn
                                                     guix-revision-id
                                                     lint-warning-ids-chunk))
               5000
               lint-warning-ids)))))))

  (define (extract-and-store-package-derivations)
    (define packages-count
      (with-resource-from-pool inf-and-store-pool res
        (match res
          ((inferior . inferior-store)
           (ensure-gds-inferior-packages-defined! inferior)

           (inferior-eval '(vector-length gds-inferior-packages) inferior)))))

    (define chunk-size 5000)

    (define (process-system-and-target system target)
      (let loop ((wal-bytes
                  (catch #t
                    (lambda ()
                      (stat:size (stat "/var/guix/db/db.sqlite-wal")))
                    (lambda _ 0))))
        (when (> wal-bytes (* 512 (expt 2 20)))
          (simple-format #t "debug: guix-daemon WAL is large (~A), waiting\n"
                         wal-bytes)

          (sleep 30)
          (loop (catch #t
                  (lambda ()
                    (stat:size (stat "/var/guix/db/db.sqlite-wal")))
                  (lambda _ 0))))))

    (define (process-system-and-target system target)
      (with-time-logging
          (simple-format #f "processing derivations for ~A" (cons system target))
        (let ((derivations-vector (make-vector packages-count)))
          (with-time-logging
              (simple-format #f "getting derivations for ~A" (cons system target))
            (let loop ((start-index 0))
              (let* ((count
                      (if (>= (+ start-index chunk-size) packages-count)
                          (- packages-count start-index)
                          chunk-size))
                     (chunk
                      (with-resource-from-pool inf-and-store-pool res
                        (match res
                          ((inferior . inferior-store)
                           (ensure-gds-inferior-packages-defined! inferior)

                           (inferior-package-derivations
                            inferior-store
                            inferior
                            system
                            target
                            start-index
                            count))))))
                (vector-copy! derivations-vector
                              start-index
                              chunk)
                (unless (>= (+ start-index chunk-size) packages-count)
                  (loop (+ start-index chunk-size))))))

          (let* ((derivation-ids
                  (with-time-logging
                      (simple-format #f "derivation-file-names->derivation-ids (~A ~A)"
                                     system target)
                    (derivation-file-names->derivation-ids/fiberized
                     derivations-vector))))

            (let* ((package-ids (fibers-force package-ids-promise))
                   (package-derivation-ids
                    (with-resource-from-pool postgresql-connection-pool conn
                      (with-time-logging
                          (simple-format #f "insert-package-derivations (~A ~A)"
                                         system target)
                        (insert-package-derivations conn
                                                    system
                                                    (or target "")
                                                    package-ids
                                                    derivation-ids)))))
              (chunk-for-each!
               (lambda (package-derivation-ids-chunk)
                 (with-resource-from-pool postgresql-connection-pool conn
                   (insert-guix-revision-package-derivations
                    conn
                    guix-revision-id
                    package-derivation-ids-chunk)))
               2000
               package-derivation-ids)))))

      (with-resource-from-pool postgresql-connection-pool conn
        (with-time-logging
            (simple-format
             #f "insert-guix-revision-package-derivation-distribution-counts (~A ~A)"
             system target)
          (insert-guix-revision-package-derivation-distribution-counts
           conn
           guix-revision-id
           system
           (or target "")))))

    (let ((process-system-and-target/fiberized
           (fiberize process-system-and-target
                     #:parallelism parallelism)))
      (par-map&
       (match-lambda
         ((system . target)
          (retry-on-missing-store-item
           (lambda ()
             (process-system-and-target/fiberized system target)))))
       (with-resource-from-pool inf-and-store-pool res
         (match res
           ((inferior . inferior-store)
            (inferior-fetch-system-target-pairs inferior)))))))

  (define (extract-and-store-system-tests)
    (if skip-system-tests?
        (begin
          (simple-format #t "debug: skipping system tests\n")
          '())
        (let ((data-with-derivation-file-names
               (with-resource-from-pool inf-and-store-pool res
                 (match res
                   ((inferior . inferior-store)
                    (with-time-logging "getting inferior system tests"
                      (all-inferior-system-tests
                       inferior
                       inferior-store
                       guix-source
                       commit)))))))
          (when data-with-derivation-file-names
            (let ((data-with-derivation-ids
                   (map (match-lambda
                          ((name description derivation-file-names-by-system location-data)
                           (list name
                                 description
                                 (let ((systems
                                        (map car derivation-file-names-by-system))
                                       (derivation-ids
                                        (derivation-file-names->derivation-ids/fiberized
                                         (list->vector
                                          (map cdr derivation-file-names-by-system)))))
                                   (map cons systems derivation-ids))
                                 location-data)))
                        data-with-derivation-file-names)))
              (with-resource-from-pool postgresql-connection-pool conn
                (insert-system-tests-for-guix-revision
                 conn
                 guix-revision-id
                 data-with-derivation-ids)))))))

  (with-time-logging
      (simple-format #f "extract-information-from: ~A\n" store-path)
    (parallel-via-fibers
     (fibers-force package-ids-promise)
     (extract-and-store-package-derivations)
     (retry-on-missing-store-item extract-and-store-system-tests)
     (extract-and-store-lint-checkers-and-warnings)))

  #t)

(prevent-inlining-for-tests extract-information-from)

(define (load-channel-instances utility-thread-channel
                                git-repository-id commit
                                channel-derivations-by-system)
  ;; Load the channel instances in a different transaction, so that this can
  ;; commit prior to the outer transaction
  (with-postgresql-connection
   "load-new-guix-revision insert channel instances"
   (lambda (channel-instances-conn)
     (with-postgresql-transaction
      channel-instances-conn
      (lambda (channel-instances-conn)

        (with-time-logging
            "acquiring advisory transaction lock: load-new-guix-revision-inserts"
          ;; Wait until this is the only transaction inserting data, to avoid
          ;; any concurrency issues
          (obtain-advisory-transaction-lock channel-instances-conn
                                            'load-new-guix-revision-inserts))

        (let* ((existing-guix-revision-id
                (git-repository-id-and-commit->revision-id channel-instances-conn
                                                           git-repository-id
                                                           commit))
               (guix-revision-id
                (or existing-guix-revision-id
                    (insert-guix-revision channel-instances-conn
                                          git-repository-id commit)))
               (postgresql-connection-pool
                (make-resource-pool
                 (const channel-instances-conn)
                 1
                 #:min-size 0)))

          (unless existing-guix-revision-id
            (let* ((derivations-by-system
                    (filter-map
                     (match-lambda
                       ((system . derivations)
                        (and=>
                         (assoc-ref derivations
                                    'manifest-entry-item)
                         (lambda (drv)
                           (cons system drv)))))
                     channel-derivations-by-system))
                   (derivation-ids
                    (derivation-file-names->derivation-ids
                     postgresql-connection-pool
                     utility-thread-channel
                     (list->vector (map cdr derivations-by-system)))))

              (insert-channel-instances channel-instances-conn
                                        guix-revision-id
                                        (map cons
                                             (map car derivations-by-system)
                                             (vector->list derivation-ids))))
            (simple-format
             (current-error-port)
             "guix-data-service: saved the channel instance derivations to the database\n"))

          guix-revision-id))))))

(prevent-inlining-for-tests load-channel-instances)

(define* (load-new-guix-revision conn git-repository-id commit
                                 #:key skip-system-tests? parallelism
                                 extra-inferior-environment-variables)
  (define utility-thread-channel
    (make-worker-thread-channel
     (const '())
     #:parallelism parallelism))

  (%worker-thread-default-timeout #f)

  (let* ((git-repository-fields
          (select-git-repository conn git-repository-id))
         (git-repository-url
          (second git-repository-fields))
         (fetch-with-authentication?
          (fourth git-repository-fields))
         (channel-for-commit
          (channel (name 'guix)
                   (url git-repository-url)
                   (commit commit)))
         (guix-source
          channel-derivations-by-system
          (channel->source-and-derivations-by-system
           conn
           channel-for-commit
           fetch-with-authentication?
           #:parallelism parallelism))
         (guix-revision-id
          (load-channel-instances utility-thread-channel
                                  git-repository-id commit
                                  channel-derivations-by-system)))
    (let ((store-item
           guix-derivation
           (channel-derivations-by-system->guix-store-item
            channel-derivations-by-system)))
      (if store-item
          (and
           (extract-information-from conn
                                     guix-revision-id
                                     commit guix-source store-item
                                     guix-derivation
                                     utility-thread-channel
                                     #:skip-system-tests?
                                     skip-system-tests?
                                     #:extra-inferior-environment-variables
                                     extra-inferior-environment-variables
                                     #:parallelism parallelism)

           (if (defined? 'channel-news-for-commit
                 (resolve-module '(guix channels)))
               (with-time-logging "inserting channel news entries"
                 (insert-channel-news-entries-for-guix-revision
                  conn
                  guix-revision-id
                  (channel-news-for-commit channel-for-commit commit)))
               (begin
                 (simple-format
                  #t "debug: importing channel news not supported\n")
                 #t))

           (update-package-derivations-table conn
                                             git-repository-id
                                             guix-revision-id
                                             commit)
           (with-time-logging "updating builds.derivation_output_details_set_id"
             (update-builds-derivation-output-details-set-id
              conn
              (string->number guix-revision-id))))
          (begin
            (simple-format #t "Failed to generate store item for ~A\n"
                           commit)
            #f)))))

(define (enqueue-load-new-guix-revision-job conn git-repository-id commit source)
  (define query
    "
INSERT INTO load_new_guix_revision_jobs (git_repository_id, commit, source)
VALUES ($1, $2, $3)
ON CONFLICT DO NOTHING
RETURNING id;")

  (match (exec-query conn
                     query
                     (list (number->string git-repository-id)
                           commit
                           source))
    ((result)
     result)
    (() #f)))

(define (select-load-new-guix-revision-job-metrics conn)
  (define query
    "
SELECT COALESCE(git_repositories.label, git_repositories.url) AS repository_label,
       CASE WHEN succeeded_at IS NOT NULL
            THEN 'succeeded'
            WHEN (
                   SELECT COUNT(*)
                   FROM load_new_guix_revision_job_events
                   WHERE job_id = load_new_guix_revision_jobs.id
                     AND event = 'retry'
                 ) >= (
                   SELECT COUNT(*)
                   FROM load_new_guix_revision_job_events
                   WHERE job_id = load_new_guix_revision_jobs.id
                     AND event = 'failure'
                 )
            THEN 'queued'
            ELSE 'failed'
       END AS state,
       COUNT(*)
FROM load_new_guix_revision_jobs
INNER JOIN git_repositories
  ON load_new_guix_revision_jobs.git_repository_id =
      git_repositories.id
GROUP BY 1, 2")

  (map (match-lambda
         ((label state count)
          (list label
                state
                (string->number count))))
       (exec-query conn query)))

(define (select-job-for-commit conn commit)
  (let ((result
         (exec-query
          conn
          "
SELECT id,
       commit,
       source,
       git_repository_id,
       CASE WHEN succeeded_at IS NOT NULL
            THEN 'succeeded'
            WHEN (
                   SELECT COUNT(*)
                   FROM load_new_guix_revision_job_events
                   WHERE job_id = load_new_guix_revision_jobs.id
                     AND event = 'retry'
                 ) >= (
                   SELECT COUNT(*)
                   FROM load_new_guix_revision_job_events
                   WHERE job_id = load_new_guix_revision_jobs.id
                     AND event = 'failure'
                 )
            THEN 'queued'
            ELSE 'failed'
       END AS state
FROM load_new_guix_revision_jobs WHERE commit = $1"
          (list commit))))
    (match result
      (() #f)
      (((id commit source git_repository_id state))
       `((id                . ,(string->number id))
         (commit            . ,commit)
         (source            . ,source)
         (git_repository_id . ,(string->number git_repository_id))
         (state             . ,state))))))

(define* (select-recent-job-events conn
                                   #:key (limit 8))
  (define query
    (string-append
     "
SELECT
  load_new_guix_revision_jobs.id,
  load_new_guix_revision_jobs.commit,
  load_new_guix_revision_jobs.source,
  load_new_guix_revision_jobs.git_repository_id,
  load_new_guix_revision_job_events.event,
  load_new_guix_revision_job_events.occurred_at
FROM load_new_guix_revision_jobs
INNER JOIN load_new_guix_revision_job_events
  ON load_new_guix_revision_job_events.job_id = load_new_guix_revision_jobs.id
ORDER BY load_new_guix_revision_job_events.occurred_at DESC
LIMIT " (number->string limit)))

  (exec-query conn query))

(define (select-jobs-and-events conn before-id limit)
  (define query
    (string-append
     "
SELECT
  load_new_guix_revision_jobs.id,
  load_new_guix_revision_jobs.commit,
  load_new_guix_revision_jobs.source,
  load_new_guix_revision_jobs.git_repository_id,
  load_new_guix_revision_jobs.created_at,
  load_new_guix_revision_jobs.succeeded_at,
  (
    SELECT JSON_AGG(
      json_build_object('event', event, 'occurred_at', occurred_at) ORDER BY occurred_at ASC
    )
    FROM load_new_guix_revision_job_events
    WHERE job_id = load_new_guix_revision_jobs.id
  ),
  EXISTS (
    SELECT 1 FROM load_new_guix_revision_job_logs WHERE job_id = load_new_guix_revision_jobs.id
  ) AS log_exists
FROM load_new_guix_revision_jobs
"
     (if before-id
         (string-append
          "WHERE load_new_guix_revision_jobs.id < "
          (number->string before-id))
         "")
     "
ORDER BY load_new_guix_revision_jobs.id DESC
"
     (if limit
         (string-append
          "LIMIT " (number->string limit))
         "")))

  (map
   (match-lambda
     ((id commit source git-repository-id created-at succeeded-at
          events-json log-exists?)
      (list id commit source git-repository-id created-at succeeded-at
            (if (or (eq? #f events-json)
                    (string-null? events-json))
                #()
                (json-string->scm events-json))
            (string=? log-exists? "t"))))
   (exec-query conn query)))

(define (select-unprocessed-jobs-and-events conn)
  (define query
    "
SELECT
  load_new_guix_revision_jobs.id,
  load_new_guix_revision_jobs.commit,
  load_new_guix_revision_jobs.source,
  load_new_guix_revision_jobs.git_repository_id,
  load_new_guix_revision_jobs.created_at,
  (
    SELECT JSON_AGG(
      json_build_object('event', event, 'occurred_at', occurred_at) ORDER BY occurred_at ASC
    )
    FROM load_new_guix_revision_job_events
    WHERE job_id = load_new_guix_revision_jobs.id
  ),
  EXISTS (
    SELECT 1 FROM load_new_guix_revision_job_logs WHERE job_id = load_new_guix_revision_jobs.id
  ) AS log_exists,
  commit IN (
    SELECT commit FROM (
      SELECT DISTINCT ON (name)
        name, git_commits.commit
      FROM git_branches
      INNER JOIN git_commits
        ON git_commits.git_branch_id = git_branches.id
      WHERE
        git_branches.git_repository_id = load_new_guix_revision_jobs.git_repository_id
      ORDER BY name, datetime DESC
    ) branches_and_latest_commits
  ) AS latest_branch_commit
FROM load_new_guix_revision_jobs
INNER JOIN git_repositories
  ON load_new_guix_revision_jobs.git_repository_id =
     git_repositories.id
WHERE
  succeeded_at IS NULL AND
  (
    SELECT COUNT(*)
    FROM load_new_guix_revision_job_events
    WHERE job_id = load_new_guix_revision_jobs.id AND event = 'retry'
  ) >= (
    SELECT COUNT(*)
    FROM load_new_guix_revision_job_events
    WHERE job_id = load_new_guix_revision_jobs.id AND event = 'failure'
  )
ORDER BY latest_branch_commit DESC,
         git_repositories.job_priority DESC,
         id DESC")

  (map
   (match-lambda
     ((id commit source git-repository-id created-at
          events-json log-exists? latest-branch-commit)
      (list id commit source git-repository-id created-at
            (if (or (eq? #f events-json)
                    (string-null? events-json))
                #()
                (json-string->scm events-json))
            (string=? log-exists? "t")
            (string=? latest-branch-commit "t"))))
   (exec-query conn query)))

(define (select-jobs-and-events-for-commit conn commit)
  (define query
    "
SELECT
  load_new_guix_revision_jobs.id,
  load_new_guix_revision_jobs.source,
  load_new_guix_revision_jobs.git_repository_id,
  load_new_guix_revision_jobs.created_at,
  load_new_guix_revision_jobs.succeeded_at,
  (
    SELECT JSON_AGG(
      json_build_object('event', event, 'occurred_at', occurred_at) ORDER BY occurred_at ASC
    )
    FROM load_new_guix_revision_job_events
    WHERE job_id = load_new_guix_revision_jobs.id
  ),
  EXISTS (
    SELECT 1 FROM load_new_guix_revision_job_logs WHERE job_id = load_new_guix_revision_jobs.id
  ) AS log_exists
FROM load_new_guix_revision_jobs
WHERE commit = $1
ORDER BY load_new_guix_revision_jobs.id DESC")

  (map
   (match-lambda
     ((id source git-repository-id created-at succeeded-at
          events-json log-exists?)
      (list id commit source git-repository-id created-at succeeded-at
            (if (or (eq? #f events-json)
                    (string-null? events-json))
                #()
                (json-string->scm events-json))
            (string=? log-exists? "t"))))
   (exec-query conn query (list commit))))

(define (guix-revision-loaded-successfully? conn commit)
  (define query
    "
SELECT EXISTS(
  SELECT 1
  FROM load_new_guix_revision_jobs
  INNER JOIN load_new_guix_revision_job_events
    ON job_id = load_new_guix_revision_jobs.id
  WHERE commit = $1
    AND event = 'success'
)")

  (let ((result (caar
                 (exec-query conn query (list commit)))))
    (string=? result "t")))


(define (most-recent-n-load-new-guix-revision-jobs conn n)
  (let ((result
         (exec-query
          conn
          "
SELECT id, commit, source, git_repository_id
FROM load_new_guix_revision_jobs
ORDER BY id ASC
LIMIT $1"
          (list (number->string n)))))
    result))

(define (select-job-for-update conn id)
  (exec-query
   conn
   "
SELECT id, commit, source, git_repository_id
FROM load_new_guix_revision_jobs
WHERE id = $1
  AND succeeded_at IS NULL
FOR NO KEY UPDATE SKIP LOCKED"
   (list id)))

(define (record-job-event conn job-id event)
  (exec-query
   conn
   (string-append
    "
INSERT INTO load_new_guix_revision_job_events (job_id, event)
VALUES ($1, $2)")
   (list job-id event)))

(define (record-job-succeeded conn id)
  (exec-query
   conn
   (string-append
    "
UPDATE load_new_guix_revision_jobs
SET succeeded_at = clock_timestamp()
WHERE id = $1 ")
   (list id)))

(define (fetch-unlocked-jobs conn)
  (define query "
SELECT
  load_new_guix_revision_jobs.id,
  commit IN (
    SELECT commit FROM (
      SELECT DISTINCT ON (name)
        name, git_commits.commit
      FROM git_branches
      INNER JOIN git_commits
        ON git_commits.git_branch_id = git_branches.id
      WHERE
        git_branches.git_repository_id = load_new_guix_revision_jobs.git_repository_id
      ORDER BY name, datetime DESC
    ) branches_and_latest_commits
  ) AS latest_branch_commit
FROM load_new_guix_revision_jobs
INNER JOIN git_repositories
  ON load_new_guix_revision_jobs.git_repository_id =
     git_repositories.id
WHERE
  succeeded_at IS NULL AND
  (
    SELECT COUNT(*)
    FROM load_new_guix_revision_job_events
    WHERE job_id = load_new_guix_revision_jobs.id AND event = 'retry'
  ) >= (
    SELECT COUNT(*)
    FROM load_new_guix_revision_job_events
    WHERE job_id = load_new_guix_revision_jobs.id AND event = 'failure'
  )
ORDER BY latest_branch_commit DESC,
         git_repositories.job_priority DESC,
         load_new_guix_revision_jobs.id DESC
FOR NO KEY UPDATE OF load_new_guix_revision_jobs
SKIP LOCKED")

  (map
   (match-lambda
     ((id priority)
      (list id
            (string=? priority "t"))))
   (exec-query conn query)))

(define (open-store-connection)
  (let ((store (open-connection #:non-blocking? #t
                                #:built-in-builders '("download"))))
    (set-build-options store #:fallback? #t)

    store))

(prevent-inlining-for-tests open-store-connection)

(define* (with-store-connection proc)
  (let ((store (open-store-connection)))
    (define (thunk)
      (parameterize ((current-store-protocol-version
                      (store-connection-version store)))
        (call-with-values (lambda () (proc store))
          (lambda results
            (close-connection store)
            (apply values results)))))

    (with-exception-handler (lambda (exception)
                              (close-connection store)
                              (raise-exception exception))
      thunk)))


(prevent-inlining-for-tests with-store-connection)

(define* (process-load-new-guix-revision-job id #:key skip-system-tests?
                                             extra-inferior-environment-variables
                                             parallelism)
  (with-postgresql-connection
   (simple-format #f "load-new-guix-revision ~A" id)
   (lambda (conn)
     ;; Fix the hash encoding of derivation_output_details. This'll only run
     ;; once on any given database, but is kept here just to make sure any
     ;; instances have the data updated.
     (fix-derivation-output-details-hash-encoding conn)

     (exec-query conn "BEGIN")

     (match (select-job-for-update conn id)
       (((id commit source git-repository-id))

        ;; With a separate connection, outside of the transaction so the event
        ;; gets persisted regardless.
        (with-postgresql-connection
         (simple-format #f "load-new-guix-revision ~A start-event" id)
         (lambda (start-event-conn)
           (record-job-event start-event-conn id "start")))

        (simple-format #t "Processing job ~A (commit: ~A, source: ~A)\n\n"
                       id commit source)

        (if (eq?
             (with-time-logging (string-append "processing revision " commit)
               (with-exception-handler
                   (const #f)
                 (lambda ()
                   (with-throw-handler #t
                     (lambda ()
                       (load-new-guix-revision
                        conn
                        git-repository-id
                        commit
                        #:skip-system-tests? #t
                        #:extra-inferior-environment-variables
                        extra-inferior-environment-variables
                        #:parallelism parallelism))
                     (lambda (key . args)
                       (simple-format (current-error-port)
                                      "error: load-new-guix-revision: ~A ~A\n"
                                      key args)
                       (backtrace))))
                 #:unwind? #t))
             #t)
            (begin
              (record-job-succeeded conn id)
              (record-job-event conn id "success")
              (exec-query conn "COMMIT")

              (with-time-logging
                  "vacuuming package derivations by guix revision range table"
                (vacuum-package-derivations-table conn))

              (with-time-logging
                  "vacuum-derivation-inputs-table"
                (vacuum-derivation-inputs-table conn))

              (match (exec-query
                      conn
                      "SELECT reltuples::bigint FROM pg_class WHERE relname = 'derivation_inputs'")
                (((rows))
                 ;; Don't attempt counting distinct values if there are too
                 ;; many rows, as that is far to slow and could use up all the
                 ;; disk space.
                 (when (< (string->number rows)
                          1000000000)
                   (with-time-logging
                       "update-derivation-inputs-statistics"
                     (update-derivation-inputs-statistics conn)))))

              (with-time-logging
                  "vacuum-derivation-outputs-table"
                (vacuum-derivation-outputs-table conn))

              (with-time-logging
                  "update-derivation-outputs-statistics"
                (update-derivation-outputs-statistics conn))

              #t)
            (begin
              (exec-query conn "ROLLBACK")
              (record-job-event conn id "failure")

              #f)))
       (()
        (exec-query conn "ROLLBACK")
        (simple-format #t "job ~A not found to be processed\n"
                       id))))))