summaryrefslogtreecommitdiff
path: root/zuul/scheduler.py
blob: 9548cb6d20d806bac7cb04689f64503b60587e32 (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
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
# Copyright 2012-2015 Hewlett-Packard Development Company, L.P.
# Copyright 2013 OpenStack Foundation
# Copyright 2013 Antoine "hashar" Musso
# Copyright 2013 Wikimedia Foundation Inc.
# Copyright 2021-2022 Acme Gating, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import itertools
import logging
import socket
import sys
import threading
import time
import traceback
import uuid
from contextlib import suppress
from zuul.vendor.contextlib import nullcontext
from collections import defaultdict, OrderedDict

from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.interval import IntervalTrigger
from kazoo.exceptions import NotEmptyError

from zuul import configloader, exceptions
from zuul.lib import commandsocket
from zuul.lib.ansible import AnsibleManager
from zuul.lib.config import get_default
from zuul.lib.keystorage import KeyStorage
from zuul.lib.logutil import get_annotated_logger
from zuul.lib.monitoring import MonitoringServer
from zuul.lib.queue import NamedQueue
from zuul.lib.times import Times
from zuul.lib.statsd import get_statsd, normalize_statsd_name
from zuul.lib import tracing
import zuul.lib.queue
import zuul.lib.repl
from zuul import nodepool
from zuul.executor.client import ExecutorClient
from zuul.merger.client import MergeClient
from zuul.model import (
    Abide,
    Build,
    BuildCompletedEvent,
    BuildPausedEvent,
    BuildStartedEvent,
    BuildStatusEvent,
    Change,
    ChangeManagementEvent,
    PipelinePostConfigEvent,
    PipelineSemaphoreReleaseEvent,
    DequeueEvent,
    EnqueueEvent,
    FilesChangesCompletedEvent,
    HoldRequest,
    Job,
    MergeCompletedEvent,
    NodesProvisionedEvent,
    PromoteEvent,
    ReconfigureEvent,
    TenantReconfigureEvent,
    UnparsedAbideConfig,
    SupercedeEvent,
    SystemAttributes,
    STATE_FAILED,
)
from zuul.version import get_version_string
from zuul.zk import ZooKeeperClient
from zuul.zk.blob_store import BlobStore
from zuul.zk.cleanup import (
    SemaphoreCleanupLock,
    BuildRequestCleanupLock,
    ConnectionCleanupLock,
    GeneralCleanupLock,
    MergeRequestCleanupLock,
    NodeRequestCleanupLock,
)
from zuul.zk.components import (
    BaseComponent, COMPONENT_REGISTRY, SchedulerComponent
)
from zuul.zk.config_cache import SystemConfigCache, UnparsedConfigCache
from zuul.zk.event_queues import (
    EventWatcher,
    TenantManagementEventQueue,
    TenantTriggerEventQueue,
    PipelineManagementEventQueue,
    PipelineResultEventQueue,
    PipelineTriggerEventQueue,
    TENANT_ROOT,
)
from zuul.zk.exceptions import LockException
from zuul.zk.layout import LayoutState, LayoutStateStore
from zuul.zk.locks import (
    locked,
    tenant_read_lock,
    tenant_write_lock,
    pipeline_lock,
    management_queue_lock,
    trigger_queue_lock,
)
from zuul.zk.system import ZuulSystem
from zuul.zk.zkobject import ZKContext
from zuul.zk.election import SessionAwareElection

RECONFIG_LOCK_ID = "RECONFIG"


class FullReconfigureCommand(commandsocket.Command):
    name = 'full-reconfigure'
    help = 'Perform a reconfiguration of all tenants'


class SmartReconfigureCommand(commandsocket.Command):
    name = 'smart-reconfigure'
    help = 'Perform a reconfiguration of updated tenants'


class TenantArgument(commandsocket.Argument):
    name = 'tenant'
    help = 'The name of the tenant'


class TenantReconfigureCommand(commandsocket.Command):
    name = 'tenant-reconfigure'
    help = 'Perform a reconfiguration of a specific tenant'
    args = [TenantArgument]


COMMANDS = [
    FullReconfigureCommand,
    SmartReconfigureCommand,
    TenantReconfigureCommand,
    commandsocket.StopCommand,
    commandsocket.ReplCommand,
    commandsocket.NoReplCommand,
]


class SchedulerStatsElection(SessionAwareElection):
    """Election for emitting scheduler stats."""

    def __init__(self, client):
        self.election_root = "/zuul/scheduler/stats-election"
        super().__init__(client.client, self.election_root)


class Scheduler(threading.Thread):
    """The engine of Zuul.

    The Scheduler is responsible for receiving events and dispatching
    them to appropriate components (including pipeline managers,
    mergers and executors).

    It runs a single threaded main loop which processes events
    received one at a time and takes action as appropriate.  Other
    parts of Zuul may run in their own thread, but synchronization is
    performed within the scheduler to reduce or eliminate the need for
    locking in most circumstances.

    The main daemon will have one instance of the Scheduler class
    running which will persist for the life of the process.  The
    Scheduler instance is supplied to other Zuul components so that
    they can submit events or otherwise communicate with other
    components.

    """

    log = logging.getLogger("zuul.Scheduler")
    _stats_interval = 30
    _semaphore_cleanup_interval = IntervalTrigger(minutes=60, jitter=60)
    _general_cleanup_interval = IntervalTrigger(minutes=60, jitter=60)
    _build_request_cleanup_interval = IntervalTrigger(seconds=60, jitter=5)
    _merge_request_cleanup_interval = IntervalTrigger(seconds=60, jitter=5)
    _connection_cleanup_interval = IntervalTrigger(minutes=5, jitter=10)
    _merger_client_class = MergeClient
    _executor_client_class = ExecutorClient

    def __init__(self, config, connections, app, wait_for_init,
                 testonly=False):
        threading.Thread.__init__(self)
        self.daemon = True
        self.wait_for_init = wait_for_init
        self.hostname = socket.getfqdn()
        self.tracing = tracing.Tracing(config)
        self.primed_event = threading.Event()
        # Wake up the main run loop
        self.wake_event = threading.Event()
        # Wake up the update loop
        self.layout_update_event = threading.Event()
        # Only used by tests in order to quiesce the layout update loop
        self.layout_update_lock = threading.Lock()
        # Don't change the abide without holding this lock
        self.layout_lock = threading.Lock()
        # Only used by tests in order to quiesce the main run loop
        self.run_handler_lock = threading.Lock()
        self.command_map = {
            FullReconfigureCommand.name: self.fullReconfigureCommandHandler,
            SmartReconfigureCommand.name: self.smartReconfigureCommandHandler,
            TenantReconfigureCommand.name:
                self.tenantReconfigureCommandHandler,
            commandsocket.StopCommand.name: self.stop,
            commandsocket.ReplCommand.name: self.startRepl,
            commandsocket.NoReplCommand.name: self.stopRepl,
        }
        self._stopped = False

        self._zuul_app = app
        self.connections = connections
        self.sql = self.connections.getSqlReporter(None)
        self.statsd = get_statsd(config)
        if self.statsd:
            self.statsd_timer = self.statsd.timer
        else:
            self.statsd_timer = nullcontext
        self.times = Times(self.sql, self.statsd)
        self.repl = None
        self.stats_thread = threading.Thread(target=self.runStatsElection)
        self.stats_thread.daemon = True
        self.stop_event = threading.Event()
        self.apsched = BackgroundScheduler()
        # TODO(jeblair): fix this
        # Despite triggers being part of the pipeline, there is one trigger set
        # per scheduler. The pipeline handles the trigger filters but since
        # the events are handled by the scheduler itself it needs to handle
        # the loading of the triggers.
        # self.triggers['connection_name'] = triggerObject
        self.triggers = dict()
        self.config = config

        self.zk_client = ZooKeeperClient.fromConfig(self.config)
        self.zk_client.connect()
        self.system = ZuulSystem(self.zk_client)

        self.zuul_version = get_version_string()

        self.component_info = SchedulerComponent(
            self.zk_client, self.hostname, version=self.zuul_version)
        self.component_info.register()
        self.component_registry = COMPONENT_REGISTRY.create(self.zk_client)
        self.system_config_cache = SystemConfigCache(self.zk_client,
                                                     self.wake_event.set)
        self.unparsed_config_cache = UnparsedConfigCache(self.zk_client)

        self.monitoring_server = MonitoringServer(self.config, 'scheduler',
                                                  self.component_info)
        self.monitoring_server.start()

        # TODO (swestphahl): Remove after we've refactored reconfigurations
        # to be performed on the tenant level.
        self.reconfigure_event_queue = NamedQueue("ReconfigureEventQueue")
        self.event_watcher = EventWatcher(
            self.zk_client, self.wake_event.set
        )
        self.management_events = TenantManagementEventQueue.createRegistry(
            self.zk_client)
        self.pipeline_management_events = (
            PipelineManagementEventQueue.createRegistry(
                self.zk_client
            )
        )
        self.trigger_events = TenantTriggerEventQueue.createRegistry(
            self.zk_client, self.connections
        )
        self.pipeline_trigger_events = (
            PipelineTriggerEventQueue.createRegistry(
                self.zk_client, self.connections
            )
        )
        self.pipeline_result_events = PipelineResultEventQueue.createRegistry(
            self.zk_client
        )

        self.general_cleanup_lock = GeneralCleanupLock(self.zk_client)
        self.semaphore_cleanup_lock = SemaphoreCleanupLock(self.zk_client)
        self.build_request_cleanup_lock = BuildRequestCleanupLock(
            self.zk_client)
        self.merge_request_cleanup_lock = MergeRequestCleanupLock(
            self.zk_client)
        self.connection_cleanup_lock = ConnectionCleanupLock(self.zk_client)
        self.node_request_cleanup_lock = NodeRequestCleanupLock(self.zk_client)

        self.abide = Abide()
        self.unparsed_abide = UnparsedAbideConfig()
        self.tenant_layout_state = LayoutStateStore(
            self.zk_client,
            self.layout_update_event.set)
        self.local_layout_state = {}

        command_socket = get_default(
            self.config, 'scheduler', 'command_socket',
            '/var/lib/zuul/scheduler.socket')
        self.command_socket = commandsocket.CommandSocket(command_socket)

        self.last_reconfigured = None

        self.globals = SystemAttributes.fromConfig(self.config)
        self.ansible_manager = AnsibleManager(
            default_version=self.globals.default_ansible_version)

        self.stats_election = SchedulerStatsElection(self.zk_client)

        if not testonly:
            self.executor = self._executor_client_class(self.config, self)
            self.merger = self._merger_client_class(self.config, self)
            self.nodepool = nodepool.Nodepool(
                self.zk_client, self.system.system_id, self.statsd,
                scheduler=True)

    def start(self):
        super(Scheduler, self).start()
        self.keystore = KeyStorage(
            self.zk_client,
            password=self._get_key_store_password())

        self._command_running = True
        self.log.debug("Starting command processor")
        self.command_socket.start()
        self.command_thread = threading.Thread(target=self.runCommand,
                                               name='command')
        self.command_thread.daemon = True
        self.command_thread.start()

        self.stats_thread.start()
        self.apsched.start()
        self.times.start()
        # Start a thread to perform initial cleanup, then schedule
        # later cleanup tasks.
        self.start_cleanup_thread = threading.Thread(
            target=self.startCleanup, name='cleanup start')
        self.start_cleanup_thread.daemon = True
        self.start_cleanup_thread.start()
        self.component_info.state = self.component_info.INITIALIZING

        # Start a thread to perform background tenant layout updates
        self.layout_update_thread = threading.Thread(
            target=self.runTenantLayoutUpdates, name='layout updates')
        self.layout_update_thread.daemon = True
        self.layout_update_thread.start()

    def stop(self):
        self.log.debug("Stopping scheduler")
        self._stopped = True
        self.wake_event.set()
        self.start_cleanup_thread.join()
        self.log.debug("Stopping apscheduler")
        self.apsched.shutdown()
        self.log.debug("Waiting for main thread")
        self.join()
        self.component_info.state = self.component_info.STOPPED
        # Don't set the stop event until after the main thread is
        # joined because doing so will terminate the ZKContext.
        self.stop_event.set()
        self.times.stop()
        self.log.debug("Stopping nodepool")
        self.nodepool.stop()
        self.log.debug("Stopping connections")
        # Connections may be waiting on the primed event
        self.primed_event.set()
        self.stopConnections()
        self.log.debug("Stopping stats thread")
        self.stats_election.cancel()
        self.stats_thread.join()
        self.log.debug("Waiting for layout update thread")
        self.layout_update_event.set()
        self.layout_update_thread.join()
        self.stopRepl()
        self._command_running = False
        self.log.debug("Stopping command socket")
        self.command_socket.stop()
        # If we stop from the command socket we cannot join the command thread.
        if threading.current_thread() is not self.command_thread:
            self.command_thread.join()
        self.log.debug("Stopping timedb thread")
        self.times.join()
        self.log.debug("Stopping monitoring server")
        self.monitoring_server.stop()
        self.monitoring_server.join()
        self.log.debug("Disconnecting from ZooKeeper")
        self.zk_client.disconnect()
        self.log.debug("Stopping tracing")
        self.tracing.stop()

    def runCommand(self):
        while self._command_running:
            try:
                command, args = self.command_socket.get()
                if command != '_stop':
                    self.command_map[command](*args)
            except Exception:
                self.log.exception("Exception while processing command")

    def stopConnections(self):
        self.connections.stop()

    def runStatsElection(self):
        while not self._stopped:
            try:
                self.log.debug("Running stats election")
                self.stats_election.run(self.runStats)
            except Exception:
                self.log.exception("Exception running election stats:")
                time.sleep(1)

    def runStats(self):
        self.log.debug("Won stats election")
        while not self.stop_event.wait(self._stats_interval):
            if not self.stats_election.is_still_valid():
                self.log.debug("Stats election no longer valid")
                return
            try:
                self._runStats()
            except Exception:
                self.log.exception("Error in periodic stats:")

    def _runStats(self):
        if not self.statsd:
            return

        executor_stats_default = {
            "online": 0,
            "accepting": 0,
            "queued": 0,
            "running": 0
        }
        # Calculate the executor and merger stats
        executors_online = 0
        executors_accepting = 0
        executors_unzoned_online = 0
        executors_unzoned_accepting = 0
        # zone -> accepting|online
        zoned_executor_stats = {}
        zoned_executor_stats.setdefault(None, executor_stats_default.copy())
        mergers_online = 0

        for executor_component in self.component_registry.all("executor"):
            if executor_component.allow_unzoned:
                if executor_component.state == BaseComponent.RUNNING:
                    executors_unzoned_online += 1
                if executor_component.accepting_work:
                    executors_unzoned_accepting += 1
            if executor_component.zone:
                zone_stats = zoned_executor_stats.setdefault(
                    executor_component.zone,
                    executor_stats_default.copy())
                if executor_component.state == BaseComponent.RUNNING:
                    zone_stats["online"] += 1
                if executor_component.accepting_work:
                    zone_stats["accepting"] += 1
            # An executor with merger capabilities does also count as merger
            if executor_component.process_merge_jobs:
                mergers_online += 1

            # TODO(corvus): Remove for 5.0:
            executors_online += 1
            if executor_component.accepting_work:
                executors_accepting += 1

        # Get all builds so we can filter by state and zone
        for build_request in self.executor.executor_api.inState():
            zone_stats = zoned_executor_stats.setdefault(
                build_request.zone,
                executor_stats_default.copy())
            if build_request.state == build_request.REQUESTED:
                zone_stats['queued'] += 1
            if build_request.state in (
                    build_request.RUNNING, build_request.PAUSED):
                zone_stats['running'] += 1

        # Publish the executor stats
        self.statsd.gauge('zuul.executors.unzoned.accepting',
                          executors_unzoned_accepting)
        self.statsd.gauge('zuul.executors.unzoned.online',
                          executors_unzoned_online)
        self.statsd.gauge('zuul.executors.unzoned.jobs_running',
                          zoned_executor_stats[None]['running'])
        self.statsd.gauge('zuul.executors.unzoned.jobs_queued',
                          zoned_executor_stats[None]['queued'])
        # TODO(corvus): Remove for 5.0:
        self.statsd.gauge('zuul.executors.jobs_running',
                          zoned_executor_stats[None]['running'])
        self.statsd.gauge('zuul.executors.jobs_queued',
                          zoned_executor_stats[None]['queued'])

        for zone, stats in zoned_executor_stats.items():
            if zone is None:
                continue
            self.statsd.gauge(
                f'zuul.executors.zone.{normalize_statsd_name(zone)}.accepting',
                stats["accepting"],
            )
            self.statsd.gauge(
                f'zuul.executors.zone.{normalize_statsd_name(zone)}.online',
                stats["online"],
            )
            self.statsd.gauge(
                'zuul.executors.zone.'
                f'{normalize_statsd_name(zone)}.jobs_running',
                stats['running'])
            self.statsd.gauge(
                'zuul.executors.zone.'
                f'{normalize_statsd_name(zone)}.jobs_queued',
                stats['queued'])

        # TODO(corvus): Remove for 5.0:
        self.statsd.gauge('zuul.executors.accepting', executors_accepting)
        self.statsd.gauge('zuul.executors.online', executors_online)

        # Publish the merger stats
        for merger_component in self.component_registry.all("merger"):
            if merger_component.state == BaseComponent.RUNNING:
                mergers_online += 1

        merge_queue = 0
        merge_running = 0
        for merge_request in self.merger.merger_api.inState():
            if merge_request.state == merge_request.REQUESTED:
                merge_queue += 1
            if merge_request.state == merge_request.RUNNING:
                merge_running += 1

        self.statsd.gauge('zuul.mergers.online', mergers_online)
        self.statsd.gauge('zuul.mergers.jobs_running', merge_running)
        self.statsd.gauge('zuul.mergers.jobs_queued', merge_queue)
        self.statsd.gauge('zuul.scheduler.eventqueues.management',
                          self.reconfigure_event_queue.qsize())
        queue_base = 'zuul.scheduler.eventqueues.connection'
        conn_base = 'zuul.connection'
        for connection in self.connections.connections.values():
            queue = connection.getEventQueue()
            if queue is not None:
                self.statsd.gauge(f'{queue_base}.{connection.connection_name}',
                                  len(queue))
            if hasattr(connection, 'estimateCacheDataSize'):
                compressed_size, uncompressed_size =\
                    connection.estimateCacheDataSize()
                self.statsd.gauge(f'{conn_base}.{connection.connection_name}.'
                                  'cache.data_size_compressed',
                                  compressed_size)
                self.statsd.gauge(f'{conn_base}.{connection.connection_name}.'
                                  'cache.data_size_uncompressed',
                                  uncompressed_size)

        for tenant in self.abide.tenants.values():
            self.statsd.gauge(f"zuul.tenant.{tenant.name}.management_events",
                              len(self.management_events[tenant.name]))
            self.statsd.gauge(f"zuul.tenant.{tenant.name}.trigger_events",
                              len(self.trigger_events[tenant.name]))
            trigger_event_queues = self.pipeline_trigger_events[tenant.name]
            result_event_queues = self.pipeline_result_events[tenant.name]
            management_event_queues = (
                self.pipeline_management_events[tenant.name]
            )
            for pipeline in tenant.layout.pipelines.values():
                base = f"zuul.tenant.{tenant.name}.pipeline.{pipeline.name}"
                self.statsd.gauge(f"{base}.trigger_events",
                                  len(trigger_event_queues[pipeline.name]))
                self.statsd.gauge(f"{base}.result_events",
                                  len(result_event_queues[pipeline.name]))
                self.statsd.gauge(f"{base}.management_events",
                                  len(management_event_queues[pipeline.name]))
                compressed_size, uncompressed_size =\
                    pipeline.state.estimateDataSize()
                self.statsd.gauge(f'{base}.data_size_compressed',
                                  compressed_size)
                self.statsd.gauge(f'{base}.data_size_uncompressed',
                                  uncompressed_size)

        self.nodepool.emitStatsTotals(self.abide)

    def startCleanup(self):
        # Run the first cleanup immediately after the first
        # reconfiguration.
        while not self.stop_event.wait(0):
            if self._stopped:
                return
            if not self.last_reconfigured:
                time.sleep(0.1)
                continue

            try:
                self._runSemaphoreCleanup()
            except Exception:
                self.log.exception("Error in semaphore cleanup:")
            if self._stopped:
                return
            try:
                self._runBuildRequestCleanup()
            except Exception:
                self.log.exception("Error in build request cleanup:")
            if self._stopped:
                return
            try:
                self._runNodeRequestCleanup()
            except Exception:
                self.log.exception("Error in node request cleanup:")
            if self._stopped:
                return

            self.apsched.add_job(self._runSemaphoreCleanup,
                                 trigger=self._semaphore_cleanup_interval)
            self.apsched.add_job(self._runBuildRequestCleanup,
                                 trigger=self._build_request_cleanup_interval)
            self.apsched.add_job(self._runMergeRequestCleanup,
                                 trigger=self._merge_request_cleanup_interval)
            self.apsched.add_job(self._runConnectionCleanup,
                                 trigger=self._connection_cleanup_interval)
            self.apsched.add_job(self._runGeneralCleanup,
                                 trigger=self._general_cleanup_interval)
            return

    def _runSemaphoreCleanup(self):
        # Get the layout lock to make sure the abide doesn't change
        # under us.
        with self.layout_lock:
            if self.semaphore_cleanup_lock.acquire(blocking=False):
                try:
                    self.log.debug("Starting semaphore cleanup")
                    for tenant in self.abide.tenants.values():
                        try:
                            tenant.semaphore_handler.cleanupLeaks()
                        except Exception:
                            self.log.exception("Error in semaphore cleanup:")
                finally:
                    self.semaphore_cleanup_lock.release()

    def _runNodeRequestCleanup(self):
        # Get the layout lock to make sure the abide doesn't change
        # under us.
        with self.layout_lock:
            if self.node_request_cleanup_lock.acquire(blocking=False):
                try:
                    self.log.debug("Starting node request cleanup")
                    try:
                        self._cleanupNodeRequests()
                    except Exception:
                        self.log.exception("Error in node request cleanup:")
                finally:
                    self.node_request_cleanup_lock.release()

    def _cleanupNodeRequests(self):
        # Get all the requests in ZK that belong to us
        zk_requests = set()
        for req_id in self.nodepool.zk_nodepool.getNodeRequests():
            req = self.nodepool.zk_nodepool.getNodeRequest(req_id, cached=True)
            if req.requestor == self.system.system_id:
                zk_requests.add(req_id)
        # Get all the current node requests in the queues
        outstanding_requests = set()
        for tenant in self.abide.tenants.values():
            for pipeline in tenant.layout.pipelines.values():
                with pipeline_lock(
                    self.zk_client, tenant.name, pipeline.name,
                ) as lock:
                    ctx = self.createZKContext(lock, self.log)
                    with pipeline.manager.currentContext(ctx):
                        pipeline.change_list.refresh(ctx)
                        pipeline.state.refresh(ctx)
                        # In case we're in the middle of a reconfig,
                        # include the old queue items.
                        for item in pipeline.getAllItems(include_old=True):
                            nrs = item.current_build_set.node_requests
                            for req_id in (nrs.values()):
                                outstanding_requests.add(req_id)
        leaked_requests = zk_requests - outstanding_requests
        for req_id in leaked_requests:
            try:
                self.log.warning("Deleting leaked node request: %s", req_id)
                self.nodepool.zk_nodepool.deleteNodeRequest(req_id)
            except Exception:
                self.log.exception("Error deleting leaked node request: %s",
                                   req_id)

    def _runGeneralCleanup(self):
        self.log.debug("Starting general cleanup")
        if self.general_cleanup_lock.acquire(blocking=False):
            try:
                self._runConfigCacheCleanup()
                self._runExecutorApiCleanup()
                self._runMergerApiCleanup()
                self._runLayoutDataCleanup()
                self._runBlobStoreCleanup()
                self.maintainConnectionCache()
            except Exception:
                self.log.exception("Error in general cleanup:")
            finally:
                self.general_cleanup_lock.release()
        # This has its own locking
        self._runNodeRequestCleanup()
        self.log.debug("Finished general cleanup")

    def _runConfigCacheCleanup(self):
        with self.layout_lock:
            try:
                self.log.debug("Starting config cache cleanup")
                cached_projects = set(
                    self.unparsed_config_cache.listCachedProjects())
                active_projects = set(
                    self.abide.unparsed_project_branch_cache.keys())
                unused_projects = cached_projects - active_projects
                for project_cname in unused_projects:
                    self.unparsed_config_cache.clearCache(project_cname)
                self.log.debug("Finished config cache cleanup")
            except Exception:
                self.log.exception("Error in config cache cleanup:")

    def _runExecutorApiCleanup(self):
        try:
            self.executor.executor_api.cleanup()
        except Exception:
            self.log.exception("Error in executor API cleanup:")

    def _runMergerApiCleanup(self):
        try:
            self.merger.merger_api.cleanup()
        except Exception:
            self.log.exception("Error in merger API cleanup:")

    def _runLayoutDataCleanup(self):
        try:
            self.tenant_layout_state.cleanup()
        except Exception:
            self.log.exception("Error in layout data cleanup:")

    def _runBlobStoreCleanup(self):
        self.log.debug("Starting blob store cleanup")
        try:
            live_blobs = set()
            with self.layout_lock:
                # get the start ltime so that we can filter out any
                # blobs used since this point
                start_ltime = self.zk_client.getCurrentLtime()
                # lock and refresh the pipeline
                for tenant in self.abide.tenants.values():
                    for pipeline in tenant.layout.pipelines.values():
                        with pipeline_lock(
                                self.zk_client, tenant.name, pipeline.name,
                        ) as lock:
                            ctx = self.createZKContext(lock, self.log)
                            pipeline.state.refresh(ctx)
                            # add any blobstore references
                            for item in pipeline.getAllItems(include_old=True):
                                live_blobs.update(item.getBlobKeys())
            ctx = self.createZKContext(None, self.log)
            blobstore = BlobStore(ctx)
            # get the set of blob keys unused since the start time
            # (ie, we have already filtered any newly added keys)
            unused_blobs = blobstore.getKeysLastUsedBefore(start_ltime)
            # remove the current refences
            unused_blobs -= live_blobs
            # delete what's left
            for key in unused_blobs:
                self.log.debug("Deleting unused blob: %s", key)
                blobstore.delete(key, start_ltime)
            self.log.debug("Finished blob store cleanup")
        except Exception:
            self.log.exception("Error in blob store cleanup:")

    def _runBuildRequestCleanup(self):
        # If someone else is running the cleanup, skip it.
        if self.build_request_cleanup_lock.acquire(blocking=False):
            try:
                self.log.debug("Starting build request cleanup")
                self.executor.cleanupLostBuildRequests()
            finally:
                self.log.debug("Finished build request cleanup")
                self.build_request_cleanup_lock.release()

    def _runMergeRequestCleanup(self):
        # If someone else is running the cleanup, skip it.
        if self.merge_request_cleanup_lock.acquire(blocking=False):
            try:
                self.log.debug("Starting merge request cleanup")
                self.merger.cleanupLostMergeRequests()
            finally:
                self.log.debug("Finished merge request cleanup")
                self.merge_request_cleanup_lock.release()

    def _runConnectionCleanup(self):
        if self.connection_cleanup_lock.acquire(blocking=False):
            try:
                for connection in self.connections.connections.values():
                    self.log.debug("Cleaning up connection cache for: %s",
                                   connection)
                    connection.cleanupCache()
            finally:
                self.connection_cleanup_lock.release()

    def addTriggerEvent(self, driver_name, event):
        event.arrived_at_scheduler_timestamp = time.time()
        for tenant in self.abide.tenants.values():
            trusted, project = tenant.getProject(event.canonical_project_name)

            if project is None:
                continue
            self.trigger_events[tenant.name].put(driver_name, event)

    def addChangeManagementEvent(self, event):
        tenant_name = event.tenant_name
        pipeline_name = event.pipeline_name

        tenant = self.abide.tenants.get(tenant_name)
        if tenant is None:
            raise ValueError(f'Unknown tenant {event.tenant_name}')
        pipeline = tenant.layout.pipelines.get(pipeline_name)
        if pipeline is None:
            raise ValueError(f'Unknown pipeline {event.pipeline_name}')

        self.pipeline_management_events[tenant_name][pipeline_name].put(
            event, needs_result=False
        )

    def _reportBuildStats(self, build):
        # Note, as soon as the result is set, other threads may act
        # upon this, even though the event hasn't been fully
        # processed. This could result in race conditions when e.g. skipping
        # child jobs via zuul_return. Therefore we must delay setting the
        # result to the main event loop.
        try:
            if self.statsd and build.pipeline:
                tenant = build.pipeline.tenant
                jobname = build.job.name.replace('.', '_').replace('/', '_')
                hostname = (build.build_set.item.change.project.
                            canonical_hostname.replace('.', '_'))
                projectname = (build.build_set.item.change.project.name.
                               replace('.', '_').replace('/', '_'))
                branchname = (getattr(build.build_set.item.change,
                                      'branch', '').
                              replace('.', '_').replace('/', '_'))
                basekey = 'zuul.tenant.%s' % tenant.name
                pipekey = '%s.pipeline.%s' % (basekey, build.pipeline.name)
                # zuul.tenant.<tenant>.pipeline.<pipeline>.all_jobs
                key = '%s.all_jobs' % pipekey
                self.statsd.incr(key)
                jobkey = '%s.project.%s.%s.%s.job.%s' % (
                    pipekey, hostname, projectname, branchname, jobname)
                # zuul.tenant.<tenant>.pipeline.<pipeline>.project.
                #   <host>.<project>.<branch>.job.<job>.<result>
                key = '%s.%s' % (
                    jobkey,
                    'RETRY' if build.result is None else build.result
                )
                results_with_runtime = ['SUCCESS', 'FAILURE',
                                        'POST_FAILURE', 'TIMED_OUT']
                if build.result in results_with_runtime and build.start_time:
                    dt = (build.end_time - build.start_time) * 1000
                    self.statsd.timing(key, dt)
                self.statsd.incr(key)
                # zuul.tenant.<tenant>.pipeline.<pipeline>.project.
                #  <host>.<project>.<branch>.job.<job>.wait_time
                if build.start_time:
                    key = '%s.wait_time' % jobkey
                    dt = (build.start_time - build.execute_time) * 1000
                    self.statsd.timing(key, dt)
        except Exception:
            self.log.exception("Exception reporting runtime stats")

    def reconfigureTenant(self, tenant, project, trigger_event):
        if trigger_event:
            trigger_event_ltime = trigger_event.zuul_event_ltime
        else:
            trigger_event_ltime = None
        self.log.debug("Submitting tenant reconfiguration event for "
                       "%s due to event %s in project %s, ltime %s",
                       tenant.name, trigger_event, project,
                       trigger_event_ltime)
        branch = trigger_event and trigger_event.branch
        event = TenantReconfigureEvent(
            tenant.name, project.canonical_name, branch,
        )
        if trigger_event:
            event.branch_cache_ltimes[trigger_event.connection_name] = (
                trigger_event.branch_cache_ltime)
            event.trigger_event_ltime = trigger_event_ltime
        self.management_events[tenant.name].put(event, needs_result=False)

    def fullReconfigureCommandHandler(self):
        self._zuul_app.fullReconfigure()

    def smartReconfigureCommandHandler(self):
        self._zuul_app.smartReconfigure()

    def tenantReconfigureCommandHandler(self, tenant_name):
        self._zuul_app.tenantReconfigure([tenant_name])

    def startRepl(self):
        if self.repl:
            return
        self.repl = zuul.lib.repl.REPLServer(self)
        self.repl.start()

    def stopRepl(self):
        if not self.repl:
            return
        self.repl.stop()
        self.repl = None

    def _reportInitialStats(self, tenant):
        if not self.statsd:
            return
        try:
            for pipeline in tenant.layout.pipelines.values():
                # stats.gauges.zuul.tenant.<tenant>.pipeline.
                #    <pipeline>.current_changes
                key = 'zuul.tenant.%s.pipeline.%s' % (
                    tenant.name, pipeline.name)
                self.statsd.gauge(key + '.current_changes', 0)
        except Exception:
            self.log.exception("Exception reporting initial "
                               "pipeline stats:")

    def prime(self, config):
        self.log.info("Priming scheduler config")
        start = time.monotonic()

        if self.system_config_cache.is_valid:
            self.log.info("Using system config from Zookeeper")
            self.updateSystemConfig()
        else:
            self.log.info("Creating initial system config")
            # This verifies the voluptuous schema
            self.primeSystemConfig()

        loader = configloader.ConfigLoader(
            self.connections, self.zk_client, self.globals, self.statsd, self,
            self.merger, self.keystore)
        new_tenants = (set(self.unparsed_abide.tenants)
                       - self.abide.tenants.keys())

        with self.layout_lock:
            for tenant_name in new_tenants:
                stats_key = f'zuul.tenant.{tenant_name}'
                layout_state = self.tenant_layout_state.get(tenant_name)
                # In case we don't have a cached layout state we need to
                # acquire the write lock since we load a new tenant.
                if layout_state is None:
                    # There is no need to use the reconfig lock ID here as
                    # we are starting from an empty layout state and there
                    # should be no concurrent read locks.
                    lock_ctx = tenant_write_lock(self.zk_client, tenant_name)
                    timer_ctx = self.statsd_timer(
                        f'{stats_key}.reconfiguration_time')
                else:
                    lock_ctx = tenant_read_lock(self.zk_client, tenant_name)
                    timer_ctx = nullcontext()

                with lock_ctx as tlock, timer_ctx:
                    # Refresh the layout state now that we are holding the lock
                    # and we can be sure it won't be changed concurrently.
                    layout_state = self.tenant_layout_state.get(tenant_name)
                    layout_uuid = layout_state and layout_state.uuid

                    if layout_state:
                        # Get the ltimes from the previous reconfiguration.
                        min_ltimes = self.tenant_layout_state.getMinLtimes(
                            layout_state)
                        branch_cache_min_ltimes = (
                            layout_state.branch_cache_min_ltimes)
                    else:
                        # Consider all caches valid if we don't have a
                        # layout state.
                        min_ltimes = defaultdict(
                            lambda: defaultdict(lambda: -1))
                        branch_cache_min_ltimes = defaultdict(lambda: -1)

                    tenant = loader.loadTenant(
                        self.abide, tenant_name, self.ansible_manager,
                        self.unparsed_abide, min_ltimes=min_ltimes,
                        layout_uuid=layout_uuid,
                        branch_cache_min_ltimes=branch_cache_min_ltimes)

                    if layout_state is None:
                        # Reconfigure only tenants w/o an existing layout state
                        ctx = self.createZKContext(tlock, self.log)
                        self._reconfigureTenant(ctx, min_ltimes, -1, tenant)
                        self._reportInitialStats(tenant)
                    else:
                        self.local_layout_state[tenant_name] = layout_state
                    self.connections.reconfigureDrivers(tenant)

        # TODO(corvus): Consider removing this implicit reconfigure
        # event with v5.  Currently the expectation is that if you
        # stop a scheduler, change the tenant config, and start it,
        # the new tenant config should take effect.  If we change that
        # expectation with multiple schedulers, we can remove this.
        event = ReconfigureEvent(smart=True)
        event.zuul_event_ltime = self.zk_client.getCurrentLtime()
        self._doReconfigureEvent(event)

        # TODO(corvus): This isn't quite accurate; we don't really
        # know when the last reconfiguration took place.  But we
        # need to set some value here in order for the cleanup
        # start thread to know that it can proceed.  We should
        # store the last reconfiguration times in ZK and use them
        # here.
        self.last_reconfigured = int(time.time())

        duration = round(time.monotonic() - start, 3)
        self.log.info("Config priming complete (duration: %s seconds)",
                      duration)
        self.primed_event.set()
        self.wake_event.set()
        self.component_info.state = self.component_info.RUNNING

    def reconfigure(self, config, smart=False, tenants=None):
        self.log.debug("Submitting reconfiguration event")

        event = ReconfigureEvent(smart=smart, tenants=tenants)
        event.zuul_event_ltime = self.zk_client.getCurrentLtime()
        event.ack_ref = threading.Event()
        self.reconfigure_event_queue.put(event)
        self.wake_event.set()

        self.log.debug("Waiting for reconfiguration")
        event.ack_ref.wait()
        self.log.debug("Reconfiguration complete")
        self.last_reconfigured = int(time.time())
        # TODOv3(jeblair): reconfigure time should be per-tenant

    def autohold(self, tenant_name, project_name, job_name, ref_filter,
                 reason, count, node_hold_expiration):
        key = (tenant_name, project_name, job_name, ref_filter)
        self.log.debug("Autohold requested for %s", key)

        request = HoldRequest()
        request.tenant = tenant_name
        request.project = project_name
        request.job = job_name
        request.ref_filter = ref_filter
        request.reason = reason
        request.max_count = count

        # Set node_hold_expiration to default if no value is supplied
        if node_hold_expiration is None:
            node_hold_expiration = self.globals.default_hold_expiration

        # Reset node_hold_expiration to max if it exceeds the max
        elif self.globals.max_hold_expiration and (
                node_hold_expiration == 0 or
                node_hold_expiration > self.globals.max_hold_expiration):
            node_hold_expiration = self.globals.max_hold_expiration

        request.node_expiration = node_hold_expiration

        # No need to lock it since we are creating a new one.
        self.nodepool.zk_nodepool.storeHoldRequest(request)

    def autohold_list(self):
        '''
        Return current hold requests as a list of dicts.
        '''
        data = []
        for request_id in self.nodepool.zk_nodepool.getHoldRequests():
            request = self.nodepool.zk_nodepool.getHoldRequest(request_id)
            if not request:
                continue
            data.append(request.toDict())
        return data

    def autohold_info(self, hold_request_id):
        '''
        Get autohold request details.

        :param str hold_request_id: The unique ID of the request to delete.
        '''
        try:
            hold_request = self.nodepool.zk_nodepool.getHoldRequest(
                hold_request_id)
        except Exception:
            self.log.exception(
                "Error retrieving autohold ID %s:", hold_request_id)
            return {}

        if hold_request is None:
            return {}
        return hold_request.toDict()

    def autohold_delete(self, hold_request_id):
        '''
        Delete an autohold request.

        :param str hold_request_id: The unique ID of the request to delete.
        '''
        hold_request = None
        try:
            hold_request = self.nodepool.zk_nodepool.getHoldRequest(
                hold_request_id)
        except Exception:
            self.log.exception(
                "Error retrieving autohold ID %s:", hold_request_id)

        if not hold_request:
            self.log.info("Ignored request to remove invalid autohold ID %s",
                          hold_request_id)
            return

        self.log.debug("Removing autohold %s", hold_request)
        try:
            self.nodepool.zk_nodepool.deleteHoldRequest(hold_request)
        except Exception:
            self.log.exception(
                "Error removing autohold request %s:", hold_request)

    def promote(self, tenant_name, pipeline_name, change_ids):
        event = PromoteEvent(tenant_name, pipeline_name, change_ids)
        result = self.management_events[tenant_name].put(event)
        self.log.debug("Waiting for promotion")
        result.wait()
        self.log.debug("Promotion complete")

    def dequeue(self, tenant_name, pipeline_name, project_name, change, ref):
        # We need to do some pre-processing here to get the correct
        # form of the project hostname and name based on un-known
        # inputs.
        tenant = self.abide.tenants.get(tenant_name)
        if tenant is None:
            raise ValueError(f'Unknown tenant {tenant_name}')
        (trusted, project) = tenant.getProject(project_name)
        if project is None:
            raise ValueError(f'Unknown project {project_name}')

        event = DequeueEvent(tenant_name, pipeline_name,
                             project.canonical_hostname, project.name,
                             change, ref)
        result = self.management_events[tenant_name].put(event)
        self.log.debug("Waiting for dequeue")
        result.wait()
        self.log.debug("Dequeue complete")

    def enqueue(self, tenant_name, pipeline_name, project_name,
                change, ref, oldrev, newrev):
        # We need to do some pre-processing here to get the correct
        # form of the project hostname and name based on un-known
        # inputs.
        tenant = self.abide.tenants.get(tenant_name)
        if tenant is None:
            raise ValueError(f'Unknown tenant {tenant_name}')
        (trusted, project) = tenant.getProject(project_name)
        if project is None:
            raise ValueError(f'Unknown project {project_name}')

        event = EnqueueEvent(tenant_name, pipeline_name,
                             project.canonical_hostname, project.name,
                             change, ref, oldrev, newrev)
        result = self.management_events[tenant_name].put(event)
        self.log.debug("Waiting for enqueue")
        result.wait()
        self.log.debug("Enqueue complete")

    def _get_key_store_password(self):
        try:
            return self.config["keystore"]["password"]
        except KeyError:
            raise RuntimeError("No key store password configured!")

    def runTenantLayoutUpdates(self):
        log = logging.getLogger("zuul.Scheduler.LayoutUpdate")
        # Only run this after config priming is complete
        self.primed_event.wait()
        while not self._stopped:
            self.layout_update_event.wait()
            self.layout_update_event.clear()
            if self._stopped:
                break
            with self.layout_update_lock:
                # We need to handle new and deleted tenants, so we need to
                # process all tenants currently known and the new ones.
                tenant_names = set(self.abide.tenants)
                tenant_names.update(self.unparsed_abide.tenants.keys())

                for tenant_name in tenant_names:
                    if self._stopped:
                        break
                    try:
                        if (self.unparsed_abide.ltime
                                < self.system_config_cache.ltime):
                            self.updateSystemConfig()

                        with tenant_read_lock(self.zk_client, tenant_name,
                                              blocking=False):
                            remote_state = self.tenant_layout_state.get(
                                tenant_name)
                            local_state = self.local_layout_state.get(
                                tenant_name)

                            if not local_state and not remote_state:
                                # The tenant may still be in the
                                # process of initial configuration
                                self.layout_update_event.set()
                                continue

                            if (local_state is None or
                                    remote_state is None or
                                    remote_state > local_state):
                                log.debug(
                                    "Local layout of tenant %s not up to date",
                                    tenant_name)
                                self.updateTenantLayout(log, tenant_name)
                                # Wake up the main thread to process any
                                # events for this tenant.
                                self.wake_event.set()
                    except LockException:
                        log.debug(
                            "Skipping layout update of locked tenant %s",
                            tenant_name)
                        self.layout_update_event.set()
                    except Exception:
                        log.exception("Error updating layout of tenant %s",
                                      tenant_name)
                        self.layout_update_event.set()
                # In case something is locked, don't busy-loop.
                time.sleep(0.1)

    def updateTenantLayout(self, log, tenant_name):
        log.debug("Updating layout of tenant %s", tenant_name)
        loader = configloader.ConfigLoader(
            self.connections, self.zk_client, self.globals, self.statsd, self,
            self.merger, self.keystore)
        # Since we are using the ZK 'locked' context manager with a threading
        # lock, we need to pass -1 as the timeout value here as the default
        # value for ZK locks is None.
        with locked(self.layout_lock, blocking=False, timeout=-1):
            start = time.monotonic()
            log.debug("Updating local layout of tenant %s ", tenant_name)
            layout_state = self.tenant_layout_state.get(tenant_name)
            layout_uuid = layout_state and layout_state.uuid
            if layout_state:
                branch_cache_min_ltimes = layout_state.branch_cache_min_ltimes
            else:
                # We don't need the cache ltimes as the tenant was deleted
                branch_cache_min_ltimes = None

            # Get the ltimes from the previous reconfiguration.
            if layout_state:
                min_ltimes = self.tenant_layout_state.getMinLtimes(
                    layout_state)
            else:
                min_ltimes = None

            loader.loadTPCs(self.abide, self.unparsed_abide,
                            [tenant_name])
            tenant = loader.loadTenant(
                self.abide, tenant_name, self.ansible_manager,
                self.unparsed_abide, min_ltimes=min_ltimes,
                layout_uuid=layout_uuid,
                branch_cache_min_ltimes=branch_cache_min_ltimes)
            if tenant is not None:
                self.local_layout_state[tenant_name] = layout_state
                self.connections.reconfigureDrivers(tenant)
            else:
                with suppress(KeyError):
                    del self.local_layout_state[tenant_name]

        duration = round(time.monotonic() - start, 3)
        self.log.info("Local layout update complete for %s (duration: %s "
                      "seconds)", tenant_name, duration)

    def _checkTenantSourceConf(self, config):
        tenant_config = None
        script = False
        if self.config.has_option(
            'scheduler', 'tenant_config'):
            tenant_config = self.config.get(
                'scheduler', 'tenant_config')
        if self.config.has_option(
            'scheduler', 'tenant_config_script'):
            if tenant_config:
                raise Exception(
                    "tenant_config and tenant_config_script options "
                    "are exclusive.")
            tenant_config = self.config.get(
                'scheduler', 'tenant_config_script')
            script = True
        if not tenant_config:
            raise Exception(
                "tenant_config or tenant_config_script option "
                "is missing from the configuration.")
        return tenant_config, script

    def validateTenants(self, config, tenants_to_validate):
        self.config = config
        with self.layout_lock:
            self.log.info("Config validation beginning")
            start = time.monotonic()

            loader = configloader.ConfigLoader(
                self.connections, self.zk_client, self.globals, self.statsd,
                self, self.merger, self.keystore)
            tenant_config, script = self._checkTenantSourceConf(self.config)
            unparsed_abide = loader.readConfig(
                tenant_config,
                from_script=script,
                tenants_to_validate=tenants_to_validate)
            available_tenants = list(unparsed_abide.tenants)
            tenants_to_load = tenants_to_validate or available_tenants

            # Use a temporary config cache for the validation
            validate_root = f"/zuul/validate/{uuid.uuid4().hex}"
            self.unparsed_config_cache = UnparsedConfigCache(self.zk_client,
                                                             validate_root)

            try:
                abide = Abide()
                loader.loadAdminRules(abide, unparsed_abide)
                loader.loadSemaphores(abide, unparsed_abide)
                loader.loadTPCs(abide, unparsed_abide)
                for tenant_name in tenants_to_load:
                    loader.loadTenant(abide, tenant_name, self.ansible_manager,
                                      unparsed_abide, min_ltimes=None,
                                      ignore_cat_exception=False)
            finally:
                self.zk_client.client.delete(validate_root, recursive=True)

            loading_errors = []
            for tenant in abide.tenants.values():
                for error in tenant.layout.loading_errors:
                    loading_errors.append(repr(error))
            if loading_errors:
                summary = '\n\n\n'.join(loading_errors)
                raise configloader.ConfigurationSyntaxError(
                    f"Configuration errors: {summary}")

        duration = round(time.monotonic() - start, 3)
        self.log.info("Config validation complete (duration: %s seconds)",
                      duration)

    def _doReconfigureEvent(self, event):
        # This is called in the scheduler loop after another thread submits
        # a request
        reconfigured_tenants = []
        with self.layout_lock:
            self.log.info("Reconfiguration beginning (smart=%s, tenants=%s)",
                          event.smart, event.tenants)
            start = time.monotonic()

            # Update runtime related system attributes from config
            self.config = self._zuul_app.config
            self.globals = SystemAttributes.fromConfig(self.config)
            self.ansible_manager = AnsibleManager(
                default_version=self.globals.default_ansible_version)

            loader = configloader.ConfigLoader(
                self.connections, self.zk_client, self.globals, self.statsd,
                self, self.merger, self.keystore)
            tenant_config, script = self._checkTenantSourceConf(self.config)
            old_unparsed_abide = self.unparsed_abide
            self.unparsed_abide = loader.readConfig(
                tenant_config, from_script=script)
            # Cache system config in Zookeeper
            self.system_config_cache.set(self.unparsed_abide, self.globals)

            # We need to handle new and deleted tenants, so we need to process
            # all tenants currently known and the new ones.
            tenant_names = {t for t in self.abide.tenants}
            tenant_names.update(self.unparsed_abide.tenants.keys())

            # Remove TPCs of deleted tenants
            deleted_tenants = tenant_names.difference(
                self.unparsed_abide.tenants.keys())
            for tenant_name in deleted_tenants:
                self.abide.clearTPCs(tenant_name)

            loader.loadAdminRules(self.abide, self.unparsed_abide)
            loader.loadSemaphores(self.abide, self.unparsed_abide)
            loader.loadTPCs(self.abide, self.unparsed_abide)

            if event.smart:
                # Consider caches always valid
                min_ltimes = defaultdict(
                    lambda: defaultdict(lambda: -1))
                # Consider all project branch caches valid.
                branch_cache_min_ltimes = defaultdict(lambda: -1)
            else:
                # Consider caches valid if the cache ltime >= event ltime
                min_ltimes = defaultdict(
                    lambda: defaultdict(lambda: event.zuul_event_ltime))
                # Invalidate the branch cache
                for connection in self.connections.connections.values():
                    if hasattr(connection, 'clearBranchCache'):
                        if event.tenants:
                            # Only clear the projects used by this
                            # tenant (zuul-web won't be able to load
                            # any tenants that we don't immediately
                            # reconfigure after clearing)
                            for tenant_name in event.tenants:
                                projects = [
                                    tpc.project.name for tpc in
                                    itertools.chain(
                                        self.abide.getConfigTPCs(tenant_name),
                                        self.abide.getUntrustedTPCs(
                                            tenant_name))
                                ]
                                connection.clearBranchCache(projects)
                        else:
                            # Clear all projects since we're reloading
                            # all tenants.
                            connection.clearBranchCache()
                ltime = self.zk_client.getCurrentLtime()
                # Consider the branch cache valid only after we
                # cleared it
                branch_cache_min_ltimes = defaultdict(lambda: ltime)

            for tenant_name in tenant_names:
                if event.smart:
                    old_tenant = old_unparsed_abide.tenants.get(tenant_name)
                    new_tenant = self.unparsed_abide.tenants.get(tenant_name)
                    if old_tenant == new_tenant:
                        continue
                if event.tenants and tenant_name not in event.tenants:
                    continue

                old_tenant = self.abide.tenants.get(tenant_name)

                stats_key = f'zuul.tenant.{tenant_name}'
                with tenant_write_lock(
                        self.zk_client, tenant_name,
                        identifier=RECONFIG_LOCK_ID) as lock,\
                        self.statsd_timer(f'{stats_key}.reconfiguration_time'):
                    tenant = loader.loadTenant(
                        self.abide, tenant_name, self.ansible_manager,
                        self.unparsed_abide, min_ltimes=min_ltimes,
                        branch_cache_min_ltimes=branch_cache_min_ltimes)
                    reconfigured_tenants.append(tenant_name)
                    ctx = self.createZKContext(lock, self.log)
                    if tenant is not None:
                        self._reconfigureTenant(ctx, min_ltimes,
                                                -1,
                                                tenant, old_tenant)
                    else:
                        self._reconfigureDeleteTenant(ctx, old_tenant)
                        with suppress(KeyError):
                            del self.tenant_layout_state[tenant_name]
                        with suppress(KeyError):
                            del self.local_layout_state[tenant_name]

        duration = round(time.monotonic() - start, 3)
        self.log.info("Reconfiguration complete (smart: %s, tenants: %s, "
                      "duration: %s seconds)", event.smart, event.tenants,
                      duration)
        if event.smart:
            self.log.info("Reconfigured tenants: %s", reconfigured_tenants)

    def _doTenantReconfigureEvent(self, event):
        # This is called in the scheduler loop after another thread submits
        # a request
        if self.unparsed_abide.ltime < self.system_config_cache.ltime:
            self.updateSystemConfig()

        with self.layout_lock:
            self.log.info("Tenant reconfiguration beginning for %s due to "
                          "projects %s",
                          event.tenant_name, event.project_branches)
            start = time.monotonic()
            # Consider all caches valid (min. ltime -1) except for the
            # changed project-branches.
            min_ltimes = defaultdict(lambda: defaultdict(lambda: -1))
            for project_name, branch_name in event.project_branches:
                if branch_name is None:
                    min_ltimes[project_name] = defaultdict(
                        lambda: event.zuul_event_ltime)
                else:
                    min_ltimes[project_name][
                        branch_name
                    ] = event.zuul_event_ltime

            # Consider all branch caches valid except for the ones where
            # the events provides a minimum ltime.
            branch_cache_min_ltimes = defaultdict(lambda: -1)
            for connection_name, ltime in event.branch_cache_ltimes.items():
                branch_cache_min_ltimes[connection_name] = ltime

            loader = configloader.ConfigLoader(
                self.connections, self.zk_client, self.globals, self.statsd,
                self, self.merger, self.keystore)
            old_tenant = self.abide.tenants.get(event.tenant_name)
            loader.loadTPCs(self.abide, self.unparsed_abide,
                            [event.tenant_name])

            stats_key = f'zuul.tenant.{event.tenant_name}'
            with tenant_write_lock(
                    self.zk_client, event.tenant_name,
                    identifier=RECONFIG_LOCK_ID) as lock,\
                    self.statsd_timer(f'{stats_key}.reconfiguration_time'):
                loader.loadTenant(
                    self.abide, event.tenant_name, self.ansible_manager,
                    self.unparsed_abide, min_ltimes=min_ltimes,
                    branch_cache_min_ltimes=branch_cache_min_ltimes)
                tenant = self.abide.tenants[event.tenant_name]
                ctx = self.createZKContext(lock, self.log)
                self._reconfigureTenant(ctx, min_ltimes,
                                        event.trigger_event_ltime,
                                        tenant, old_tenant)
        duration = round(time.monotonic() - start, 3)
        self.log.info("Tenant reconfiguration complete for %s (duration: %s "
                      "seconds)", event.tenant_name, duration)

    def _reenqueueGetProject(self, tenant, item):
        project = item.change.project
        # Attempt to get the same project as the one passed in.  If
        # the project is now found on a different connection or if it
        # is no longer available (due to a connection being removed),
        # return None.
        (trusted, new_project) = tenant.getProject(project.canonical_name)
        if new_project:
            if project.connection_name != new_project.connection_name:
                return None
            return new_project

        if item.live:
            return None

        # If this is a non-live item we may be looking at a
        # "foreign" project, ie, one which is not defined in the
        # config but is constructed ad-hoc to satisfy a
        # cross-repo-dependency.  Find the corresponding live item
        # and use its source.
        child = item
        while child and not child.live:
            # This assumes that the queue does not branch behind this
            # item, which is currently true for non-live items; if
            # that changes, this traversal will need to be more
            # complex.
            if child.items_behind:
                child = child.items_behind[0]
            else:
                child = None
        if child is item:
            return None
        if child and child.live:
            (child_trusted, child_project) = tenant.getProject(
                child.change.project.canonical_name)
            if child_project:
                source = child_project.source
                new_project = source.getProject(project.name)
                return new_project

        return None

    def _reenqueuePipeline(self, tenant, new_pipeline, context):
        self.log.debug("Re-enqueueing changes for pipeline %s",
                       new_pipeline.name)
        # TODO(jeblair): This supports an undocument and
        # unanticipated hack to create a static window.  If we
        # really want to support this, maybe we should have a
        # 'static' type?  But it may be in use in the wild, so we
        # should allow this at least until there's an upgrade
        # path.
        if (new_pipeline.window and
            new_pipeline.window_increase_type == 'exponential' and
            new_pipeline.window_decrease_type == 'exponential' and
            new_pipeline.window_increase_factor == 1 and
            new_pipeline.window_decrease_factor == 1):
            static_window = True
        else:
            static_window = False

        items_to_remove = []
        builds_to_cancel = []
        requests_to_cancel = []
        for shared_queue in list(new_pipeline.state.old_queues):
            last_head = None
            for item in shared_queue.queue:
                # If the old item ahead made it in, re-enqueue
                # this one behind it.
                new_project = self._reenqueueGetProject(
                    tenant, item)
                if item.item_ahead in items_to_remove:
                    old_item_ahead = None
                    item_ahead_valid = False
                else:
                    old_item_ahead = item.item_ahead
                    item_ahead_valid = True
                with item.activeContext(context):
                    item.item_ahead = None
                    item.items_behind = []
                    reenqueued = False
                    if new_project:
                        item.change.project = new_project
                        item.pipeline = None
                        item.queue = None
                        if not old_item_ahead or not last_head:
                            last_head = item
                        try:
                            reenqueued = new_pipeline.manager.reEnqueueItem(
                                item, last_head, old_item_ahead,
                                item_ahead_valid=item_ahead_valid)
                        except Exception:
                            self.log.exception(
                                "Exception while re-enqueing item %s",
                                item)
                if reenqueued:
                    for build in item.current_build_set.getBuilds():
                        new_job = item.getJob(build.job.name)
                        if not new_job:
                            item.removeBuild(build)
                            builds_to_cancel.append(build)
                    for request_job, request in \
                        item.current_build_set.node_requests.items():
                        new_job = item.getJob(request_job)
                        if not new_job:
                            requests_to_cancel.append(
                                (item.current_build_set, request))
                else:
                    items_to_remove.append(item)

            # Attempt to keep window sizes from shrinking where possible
            project, branch = shared_queue.project_branches[0]
            new_queue = new_pipeline.getQueue(project, branch)
            if new_queue and shared_queue.window and (not static_window):
                new_queue.updateAttributes(
                    context, window=max(shared_queue.window,
                                        new_queue.window_floor))
            new_pipeline.state.removeOldQueue(context, shared_queue)
        for item in items_to_remove:
            self.log.info(
                "Removing item %s during reconfiguration" % (item,))
            for build in item.current_build_set.getBuilds():
                builds_to_cancel.append(build)
            for request_job, request in \
                item.current_build_set.node_requests.items():
                requests_to_cancel.append(
                    (
                        item.current_build_set,
                        request,
                        item.getJob(request_job),
                    )
                )
            try:
                self.sql.reportBuildsetEnd(
                    item.current_build_set, 'dequeue',
                    final=False, result='DEQUEUED')
            except Exception:
                self.log.exception(
                    "Error reporting buildset completion to DB:")

        for build in builds_to_cancel:
            self.log.info(
                "Canceling build %s during reconfiguration" % (build,))
            self.cancelJob(build.build_set, build.job, build=build)
        for build_set, request, request_job in requests_to_cancel:
            self.log.info(
                "Canceling node request %s during reconfiguration",
                request)
            self.cancelJob(build_set, request_job)

    def _reconfigureTenant(self, context, min_ltimes,
                           last_reconfigure_event_ltime, tenant,
                           old_tenant=None):
        # This is called from _doReconfigureEvent while holding the
        # layout lock
        if old_tenant:
            for name, old_pipeline in old_tenant.layout.pipelines.items():
                new_pipeline = tenant.layout.pipelines.get(name)
                if not new_pipeline:
                    with old_pipeline.manager.currentContext(context):
                        self._reconfigureDeletePipeline(old_pipeline)

        self.connections.reconfigureDrivers(tenant)

        # TODOv3(jeblair): remove postconfig calls?
        for pipeline in tenant.layout.pipelines.values():
            for trigger in pipeline.triggers:
                trigger.postConfig(pipeline)
            for reporter in pipeline.actions:
                reporter.postConfig()

        # Assemble a new list of min. ltimes of the project branch caches.
        branch_cache_min_ltimes = {
            s.connection.connection_name:
                s.getProjectBranchCacheLtime()
            for s in self.connections.getSources()
        }

        # Make sure last_reconfigure_event_ltime never goes backward
        old_layout_state = self.tenant_layout_state.get(tenant.name)
        if old_layout_state:
            if (old_layout_state.last_reconfigure_event_ltime >
                last_reconfigure_event_ltime):
                self.log.debug("Setting layout state last reconfigure ltime "
                               "to previous ltime %s which is newer than %s",
                               old_layout_state.last_reconfigure_event_ltime,
                               last_reconfigure_event_ltime)
                last_reconfigure_event_ltime =\
                    old_layout_state.last_reconfigure_event_ltime
        if last_reconfigure_event_ltime < 0:
            last_reconfigure_event_ltime = self.zk_client.getCurrentLtime()
            self.log.debug("Setting layout state last reconfigure ltime "
                           "to current ltime %s", last_reconfigure_event_ltime)
        else:
            self.log.debug("Setting layout state last reconfigure ltime "
                           "to %s", last_reconfigure_event_ltime)
        layout_state = LayoutState(
            tenant_name=tenant.name,
            hostname=self.hostname,
            last_reconfigured=int(time.time()),
            last_reconfigure_event_ltime=last_reconfigure_event_ltime,
            uuid=tenant.layout.uuid,
            branch_cache_min_ltimes=branch_cache_min_ltimes,
        )
        # Save the min_ltimes which are sharded before we atomically
        # update the layout state.
        self.tenant_layout_state.setMinLtimes(layout_state, min_ltimes)
        # We need to update the local layout state before the remote state,
        # to avoid race conditions in the layout changed callback.
        self.local_layout_state[tenant.name] = layout_state
        self.tenant_layout_state[tenant.name] = layout_state

    def _reconfigureDeleteTenant(self, context, tenant):
        # Called when a tenant is deleted during reconfiguration
        self.log.info("Removing tenant %s during reconfiguration" %
                      (tenant,))
        for pipeline in tenant.layout.pipelines.values():
            with pipeline.manager.currentContext(context):
                self._reconfigureDeletePipeline(pipeline)

        # Delete the tenant root path for this tenant in ZooKeeper to remove
        # all tenant specific event queues
        try:
            self.zk_client.client.delete(f"{TENANT_ROOT}/{tenant.name}",
                                         recursive=True)
        except NotEmptyError:
            # In case a build result has been submitted during the
            # reconfiguration, this cleanup will fail. We handle this in a
            # periodic cleanup job.
            pass

    def _reconfigureDeletePipeline(self, pipeline):
        self.log.info("Removing pipeline %s during reconfiguration" %
                      (pipeline,))
        for shared_queue in pipeline.queues:
            builds_to_cancel = []
            requests_to_cancel = []
            for item in shared_queue.queue:
                with item.activeContext(pipeline.manager.current_context):
                    item.item_ahead = None
                    item.items_behind = []
                self.log.info(
                    "Removing item %s during reconfiguration" % (item,))
                for build in item.current_build_set.getBuilds():
                    builds_to_cancel.append(build)
                for request_job, request in \
                    item.current_build_set.node_requests.items():
                    requests_to_cancel.append(
                        (
                            item.current_build_set,
                            request,
                            item.getJob(request_job),
                        )
                    )
                try:
                    self.sql.reportBuildsetEnd(
                        item.current_build_set, 'dequeue',
                        final=False, result='DEQUEUED')
                except Exception:
                    self.log.exception(
                        "Error reporting buildset completion to DB:")

            for build in builds_to_cancel:
                self.log.info(
                    "Canceling build %s during reconfiguration" % (build,))
                self.cancelJob(build.build_set, build.job,
                               build=build, force=True)
            for build_set, request, request_job in requests_to_cancel:
                self.log.info(
                    "Canceling node request %s during reconfiguration",
                    request)
                self.cancelJob(build_set, request_job, force=True)
            shared_queue.delete(pipeline.manager.current_context)

    def _doPromoteEvent(self, event):
        tenant = self.abide.tenants.get(event.tenant_name)
        pipeline = tenant.layout.pipelines[event.pipeline_name]
        change_ids = [c.split(',') for c in event.change_ids]
        items_to_enqueue = []
        change_queue = None

        # A list of (queue, items); the queue should be promoted
        # within the pipeline, and the items should be promoted within
        # the queue.
        promote_operations = OrderedDict()
        for number, patchset in change_ids:
            found = False
            for shared_queue in pipeline.queues:
                for item in shared_queue.queue:
                    if not item.live:
                        continue
                    if (item.change.number == number and
                        item.change.patchset == patchset):
                        promote_operations.setdefault(
                            shared_queue, []).append(item)
                        found = True
                        break
                if found:
                    break
            if not found:
                raise Exception("Unable to find %s,%s" % (number, patchset))

        # Reverse the promote operations so that we promote the first
        # change queue last (which will put it at the top).
        for change_queue, items_to_enqueue in\
            reversed(promote_operations.items()):
            # We can leave changes in the pipeline as long as the head
            # remains identical; as soon as we make a change, we have
            # to re-enqueue everything behind it in order to ensure
            # dependencies are correct.
            head_same = True
            for item in change_queue.queue[:]:
                if item.live and item == items_to_enqueue[0] and head_same:
                    # We can skip this one and leave it in place
                    items_to_enqueue.pop(0)
                    continue
                elif not item.live:
                    # Ignore this; if the actual item behind it is
                    # dequeued, it will get cleaned up eventually.
                    continue
                if item not in items_to_enqueue:
                    items_to_enqueue.append(item)
                head_same = False
                pipeline.manager.cancelJobs(item)
                pipeline.manager.dequeueItem(item)

            for item in items_to_enqueue:
                pipeline.manager.addChange(
                    item.change, item.event,
                    enqueue_time=item.enqueue_time,
                    quiet=True,
                    ignore_requirements=True)
            # Regardless, move this shared change queue to the head.
            pipeline.promoteQueue(change_queue)

    def _doDequeueEvent(self, event):
        tenant = self.abide.tenants.get(event.tenant_name)
        if tenant is None:
            raise ValueError('Unknown tenant %s' % event.tenant_name)
        pipeline = tenant.layout.pipelines.get(event.pipeline_name)
        if pipeline is None:
            raise ValueError('Unknown pipeline %s' % event.pipeline_name)
        canonical_name = event.project_hostname + '/' + event.project_name
        (trusted, project) = tenant.getProject(canonical_name)
        if project is None:
            raise ValueError('Unknown project %s' % event.project_name)
        change_key = project.source.getChangeKey(event)
        change = project.source.getChange(change_key, event=event)
        if change.project.name != project.name:
            if event.change:
                item = 'Change %s' % event.change
            else:
                item = 'Ref %s' % event.ref
            raise Exception('%s does not belong to project "%s"'
                            % (item, project.name))
        for shared_queue in pipeline.queues:
            for item in shared_queue.queue:
                if item.change.project != change.project:
                    continue
                if (isinstance(item.change, Change) and
                        item.change.number == change.number and
                        item.change.patchset == change.patchset) or\
                   (item.change.ref == change.ref):
                    pipeline.manager.removeItem(item)
                    return
        raise Exception("Unable to find shared change queue for %s:%s" %
                        (event.project_name,
                         event.change or event.ref))

    def _doEnqueueEvent(self, event):
        tenant = self.abide.tenants.get(event.tenant_name)
        if tenant is None:
            raise ValueError(f'Unknown tenant {event.tenant_name}')
        pipeline = tenant.layout.pipelines.get(event.pipeline_name)
        if pipeline is None:
            raise ValueError(f'Unknown pipeline {event.pipeline_name}')
        canonical_name = event.project_hostname + '/' + event.project_name
        (trusted, project) = tenant.getProject(canonical_name)
        if project is None:
            raise ValueError(f'Unknown project {event.project_name}')
        try:
            change_key = project.source.getChangeKey(event)
            change = project.source.getChange(change_key,
                                              event=event, refresh=True)
        except Exception as exc:
            raise ValueError('Unknown change') from exc

        if change.project.name != project.name:
            raise Exception(
                f'Change {change} does not belong to project "{project.name}"')
        self.log.debug("Event %s for change %s was directly assigned "
                       "to pipeline %s", event, change, self)
        pipeline.manager.addChange(change, event, ignore_requirements=True)

    def _doSupercedeEvent(self, event):
        tenant = self.abide.tenants[event.tenant_name]
        pipeline = tenant.layout.pipelines[event.pipeline_name]
        canonical_name = f"{event.project_hostname}/{event.project_name}"
        trusted, project = tenant.getProject(canonical_name)
        if project is None:
            return
        change_key = project.source.getChangeKey(event)
        change = project.source.getChange(change_key, event=event)
        for shared_queue in pipeline.queues:
            for item in shared_queue.queue:
                if item.change.project != change.project:
                    continue
                if not item.live:
                    continue
                if ((isinstance(item.change, Change)
                     and item.change.number == change.number
                     and item.change.patchset == change.patchset
                    ) or (item.change.ref == change.ref)):
                    self.log.info("Item %s is superceded, dequeuing", item)
                    pipeline.manager.removeItem(item)
                    return

    def _areAllBuildsComplete(self):
        self.log.debug("Checking if all builds are complete")
        waiting = False
        for tenant in self.abide.tenants.values():
            for pipeline in tenant.layout.pipelines.values():
                for item in pipeline.getAllItems():
                    for build in item.current_build_set.getBuilds():
                        if build.result is None:
                            self.log.debug("%s waiting on %s" %
                                           (pipeline.manager, build))
                            waiting = True
        if not waiting:
            self.log.debug("All builds are complete")
            return True
        return False

    def run(self):
        if self.statsd:
            self.log.debug("Statsd enabled")
        else:
            self.log.debug("Statsd not configured")
        if self.wait_for_init:
            self.log.debug("Waiting for tenant initialization")
            self.primed_event.wait()
        while True:
            self.log.debug("Run handler sleeping")
            self.wake_event.wait()
            self.wake_event.clear()
            if self._stopped:
                self.log.debug("Run handler stopping")
                return
            self.log.debug("Run handler awake")
            self.run_handler_lock.acquire()
            try:
                if not self._stopped:
                    self.process_reconfigure_queue()

                if self.unparsed_abide.ltime < self.system_config_cache.ltime:
                    self.updateSystemConfig()

                for tenant_name in self.unparsed_abide.tenants:
                    if self._stopped:
                        break

                    tenant = self.abide.tenants.get(tenant_name)
                    if not tenant:
                        continue

                    # This will also forward events for the pipelines
                    # (e.g. enqueue or dequeue events) to the matching
                    # pipeline event queues that are processed afterwards.
                    self.process_tenant_management_queue(tenant)

                    if self._stopped:
                        break

                    try:
                        with tenant_read_lock(
                            self.zk_client, tenant_name, blocking=False
                        ) as tlock:
                            remote_state = self.tenant_layout_state.get(
                                tenant_name)
                            if remote_state is None:
                                # The tenant may still be in the
                                # process of initial configuration
                                self.wake_event.set()
                                continue
                            local_state = self.local_layout_state.get(
                                tenant_name)
                            if (local_state is None or
                                remote_state > local_state):
                                self.log.debug(
                                    "Local layout of tenant %s not up to date",
                                    tenant.name)
                                self.layout_update_event.set()
                                continue

                            # Get tenant again, as it might have been updated
                            # by a tenant reconfig or layout change.
                            tenant = self.abide.tenants[tenant_name]

                            if not self._stopped:
                                # This will forward trigger events to pipeline
                                # event queues that are processed below.
                                self.process_tenant_trigger_queue(tenant)

                            self.process_pipelines(tenant, tlock)
                    except LockException:
                        self.log.debug("Skipping locked tenant %s",
                                       tenant.name)
                        remote_state = self.tenant_layout_state.get(
                            tenant_name)
                        local_state = self.local_layout_state.get(
                            tenant_name)
                        if (remote_state is None or
                            local_state is None or
                            remote_state > local_state):
                            # Let's keep looping until we've updated to the
                            # latest tenant layout.
                            self.wake_event.set()
                    except Exception:
                        self.log.exception("Exception processing tenant %s:",
                                           tenant_name)
                        # There may still be more events to process
                        self.wake_event.set()
            except Exception:
                self.log.exception("Exception in run handler:")
                # There may still be more events to process
                self.wake_event.set()
            finally:
                self.run_handler_lock.release()

    def primeSystemConfig(self):
        with self.layout_lock:
            loader = configloader.ConfigLoader(
                self.connections, self.zk_client, self.globals, self.statsd,
                self, self.merger, self.keystore)
            tenant_config, script = self._checkTenantSourceConf(self.config)
            self.unparsed_abide = loader.readConfig(
                tenant_config, from_script=script)
            self.system_config_cache.set(self.unparsed_abide, self.globals)

            loader.loadAdminRules(self.abide, self.unparsed_abide)
            loader.loadSemaphores(self.abide, self.unparsed_abide)
            loader.loadTPCs(self.abide, self.unparsed_abide)

    def updateSystemConfig(self):
        with self.layout_lock:
            self.unparsed_abide, self.globals = self.system_config_cache.get()
            self.ansible_manager = AnsibleManager(
                default_version=self.globals.default_ansible_version)
            loader = configloader.ConfigLoader(
                self.connections, self.zk_client, self.globals, self.statsd,
                self, self.merger, self.keystore)

            tenant_names = set(self.abide.tenants)
            deleted_tenants = tenant_names.difference(
                self.unparsed_abide.tenants.keys())

            # Remove TPCs of deleted tenants
            for tenant_name in deleted_tenants:
                self.abide.clearTPCs(tenant_name)

            loader.loadAdminRules(self.abide, self.unparsed_abide)
            loader.loadSemaphores(self.abide, self.unparsed_abide)
            loader.loadTPCs(self.abide, self.unparsed_abide)

    def process_pipelines(self, tenant, tenant_lock):
        for pipeline in tenant.layout.pipelines.values():
            if self._stopped:
                return
            if RECONFIG_LOCK_ID in tenant_lock.contenders():
                self.log.debug("Stopping tenant %s pipeline processing due to "
                               "pending reconfig", tenant.name)
                self.wake_event.set()
                return
            stats_key = f'zuul.tenant.{tenant.name}.pipeline.{pipeline.name}'
            try:
                with pipeline_lock(
                    self.zk_client, tenant.name, pipeline.name, blocking=False
                ) as lock:
                    ctx = self.createZKContext(lock, self.log)
                    with pipeline.manager.currentContext(ctx):
                        with self.statsd_timer(f'{stats_key}.handling'):
                            refreshed = self._process_pipeline(
                                tenant, pipeline)
                    # Update pipeline summary for zuul-web
                    if refreshed:
                        pipeline.summary.update(ctx, self.globals)
                        if self.statsd:
                            self._contextStats(ctx, stats_key)
            except LockException:
                self.log.debug("Skipping locked pipeline %s in tenant %s",
                               pipeline.name, tenant.name)
            except Exception:
                self.log.exception(
                    "Exception processing pipeline %s in tenant %s",
                    pipeline.name, tenant.name)

    def _contextStats(self, ctx, stats_key):
        self.statsd.timing(f'{stats_key}.read_time',
                           ctx.cumulative_read_time * 1000)
        self.statsd.timing(f'{stats_key}.write_time',
                           ctx.cumulative_write_time * 1000)
        self.statsd.gauge(f'{stats_key}.read_objects',
                          ctx.cumulative_read_objects)
        self.statsd.gauge(f'{stats_key}.write_objects',
                          ctx.cumulative_write_objects)
        self.statsd.gauge(f'{stats_key}.read_znodes',
                          ctx.cumulative_read_znodes)
        self.statsd.gauge(f'{stats_key}.write_znodes',
                          ctx.cumulative_write_znodes)
        self.statsd.gauge(f'{stats_key}.read_bytes',
                          ctx.cumulative_read_bytes)
        self.statsd.gauge(f'{stats_key}.write_bytes',
                          ctx.cumulative_write_bytes)

    def _process_pipeline(self, tenant, pipeline):
        # Return whether or not we refreshed the pipeline.

        # We only need to process the pipeline if there are
        # outstanding events.
        if not any((
            self.pipeline_trigger_events[
                tenant.name][pipeline.name].hasEvents(),
            self.pipeline_result_events[
                tenant.name][pipeline.name].hasEvents(),
            self.pipeline_management_events[
                tenant.name][pipeline.name].hasEvents(),
            pipeline.state.isDirty(self.zk_client.client),
        )):
            self.log.debug("No events to process for pipeline %s in tenant %s",
                           pipeline.name, tenant.name)
            return False

        stats_key = f'zuul.tenant.{tenant.name}.pipeline.{pipeline.name}'
        ctx = pipeline.manager.current_context
        with self.statsd_timer(f'{stats_key}.refresh'):
            pipeline.change_list.refresh(ctx)
            pipeline.summary.refresh(ctx)
            pipeline.state.refresh(ctx)

        pipeline.state.setDirty(self.zk_client.client)
        if pipeline.state.old_queues:
            self._reenqueuePipeline(tenant, pipeline, ctx)
        pipeline.state.cleanup(ctx)

        with self.statsd_timer(f'{stats_key}.event_process'):
            self.process_pipeline_management_queue(tenant, pipeline)
            # Give result events priority -- they let us stop builds,
            # whereas trigger events cause us to execute builds.
            self.process_pipeline_result_queue(tenant, pipeline)
            self.process_pipeline_trigger_queue(tenant, pipeline)
        try:
            with self.statsd_timer(f'{stats_key}.process'):
                while not self._stopped and pipeline.manager.processQueue():
                    pass
        except Exception:
            self.log.exception("Exception in pipeline processing:")
            pipeline.state.updateAttributes(
                ctx, state=pipeline.STATE_ERROR)
            # Continue processing other pipelines+tenants
        else:
            pipeline.state.updateAttributes(
                ctx, state=pipeline.STATE_NORMAL)
            pipeline.state.clearDirty(self.zk_client.client)
        return True

    def _gatherConnectionCacheKeys(self):
        relevant = set()
        with self.layout_lock:
            ctx = self.createZKContext(None, self.log)
            for tenant in self.abide.tenants.values():
                for pipeline in tenant.layout.pipelines.values():
                    self.log.debug("Gather relevant cache items for: %s %s",
                                   tenant.name, pipeline.name)
                    pipeline.change_list.refresh(ctx)
                    change_keys = pipeline.change_list.getChangeKeys()
                    relevant_changes = pipeline.manager.resolveChangeKeys(
                        change_keys)
                    for change in relevant_changes:
                        relevant.add(change.cache_stat.key)
        return relevant

    def maintainConnectionCache(self):
        self.log.debug("Starting connection cache maintenance")
        relevant = self._gatherConnectionCacheKeys()

        # We'll only remove changes older than `max_age` from the cache, as
        # it may take a while for an event that was processed by a connection
        # (which updated/populated the cache) to end up in a pipeline.
        for connection in self.connections.connections.values():
            connection.maintainCache(relevant, max_age=7200)  # 2h
            self.log.debug("Finished connection cache maintenance for: %s",
                           connection)
        self.log.debug("Finished connection cache maintenance")

    def process_tenant_trigger_queue(self, tenant):
        try:
            with trigger_queue_lock(
                self.zk_client, tenant.name, blocking=False
            ):
                # Update the pipeline changes
                ctx = self.createZKContext(None, self.log)
                for pipeline in tenant.layout.pipelines.values():
                    try:
                        pipeline.change_list.refresh(ctx)
                    except Exception:
                        self.log.exception(
                            "Unable to refresh pipeline change list for %s",
                            pipeline.name)

                # Get the ltime of the last reconfiguration event
                self.trigger_events[tenant.name].refreshMetadata()
                for event in self.trigger_events[tenant.name]:
                    log = get_annotated_logger(self.log, event.zuul_event_id)
                    log.debug("Forwarding trigger event %s", event)
                    try:
                        self._forward_trigger_event(event, tenant)
                    except Exception:
                        log.exception("Unable to forward event %s "
                                      "to tenant %s", event, tenant.name)
                    finally:
                        self.trigger_events[tenant.name].ack(event)
                self.trigger_events[tenant.name].cleanup()
        except LockException:
            self.log.debug("Skipping locked trigger event queue in tenant %s",
                           tenant.name)

    def _forward_trigger_event(self, event, tenant):
        log = get_annotated_logger(self.log, event.zuul_event_id)
        trusted, project = tenant.getProject(event.canonical_project_name)

        if project is None:
            return

        try:
            change_key = project.source.getChangeKey(event)
            change = project.source.getChange(change_key, event=event)
        except exceptions.ChangeNotFound as e:
            log.debug("Unable to get change %s from source %s",
                      e.change, project.source)
            return

        reconfigure_tenant = False
        if (event.branch_updated and
            hasattr(change, 'files') and
            change.updatesConfig(tenant)):
            reconfigure_tenant = True
            if change.files is None:
                log.debug("Reconfiguring tenant after branch updated "
                          "without file list, assuming config update")
        elif (event.branch_deleted and
              self.abide.hasUnparsedBranchCache(project.canonical_name,
                                                event.branch)):
            reconfigure_tenant = True

        # The branch_created attribute is also true when a tag is
        # created. Since we load config only from branches only trigger
        # a tenant reconfiguration if the branch is set as well.
        if event.branch_created and event.branch:
            reconfigure_tenant = True
        # If the driver knows the branch but we don't have a config, we
        # also need to reconfigure. This happens if a GitHub branch
        # was just configured as protected without a push in between.
        elif (event.branch in project.source.getProjectBranches(
                project, tenant, min_ltime=event.branch_cache_ltime)
              and not self.abide.hasUnparsedBranchCache(
                project.canonical_name, event.branch)):
            reconfigure_tenant = True

        # If the branch is unprotected and unprotected branches
        # are excluded from the tenant for that project skip reconfig.
        if (reconfigure_tenant and not
            event.branch_protected and
            tenant.getExcludeUnprotectedBranches(project)):

            reconfigure_tenant = False

        # If all config classes are excluded for this project we don't need
        # to trigger a reconfiguration.
        tpc = tenant.project_configs.get(project.canonical_name)
        if tpc and not tpc.load_classes:
            reconfigure_tenant = False

        # If we are listing included branches and this branch
        # is not included, skip reconfig.
        if (reconfigure_tenant and
            not tpc.includesBranch(event.branch)):
            reconfigure_tenant = False

        # But if the event is that branch protection status has
        # changed, do reconfigure.
        if (event.isBranchProtectionChanged()):
            reconfigure_tenant = True

        if reconfigure_tenant:
            # The change that just landed updates the config
            # or a branch was just created or deleted.  Clear
            # out cached data for this project and perform a
            # reconfiguration.
            self.reconfigureTenant(tenant, change.project, event)
            # This will become the new required minimum event ltime
            # for every trigger event processed after the
            # reconfiguration, so make sure we update it after having
            # submitted the reconfiguration event.
            self.trigger_events[tenant.name].last_reconfigure_event_ltime =\
                event.zuul_event_ltime

        event.min_reconfigure_ltime = self.trigger_events[
            tenant.name].last_reconfigure_event_ltime
        for pipeline in tenant.layout.pipelines.values():
            if (
                pipeline.manager.eventMatches(event, change)
                or pipeline.manager.isAnyVersionOfChangeInPipeline(change)
            ):
                self.pipeline_trigger_events[tenant.name][
                    pipeline.name
                ].put(event.driver_name, event)

    def process_pipeline_trigger_queue(self, tenant, pipeline):
        for event in self.pipeline_trigger_events[tenant.name][pipeline.name]:
            if self._stopped:
                return
            log = get_annotated_logger(self.log, event.zuul_event_id)
            if not isinstance(event, SupercedeEvent):
                local_state = self.local_layout_state[tenant.name]
                last_ltime = local_state.last_reconfigure_event_ltime
                # The event tells us the ltime of the most recent
                # reconfiguration event up to that point.  If our local
                # layout state wasn't generated by an event after that
                # time, then we are too out of date to process this event.
                # Abort now and wait for an update.
                if (event.min_reconfigure_ltime > -1 and
                    event.min_reconfigure_ltime > last_ltime):
                    log.debug("Trigger event minimum reconfigure ltime of %s "
                              "newer than current reconfigure ltime of %s, "
                              "aborting early",
                              event.min_reconfigure_ltime, last_ltime)
                    return
            log.debug("Processing trigger event %s", event)
            try:
                if isinstance(event, SupercedeEvent):
                    self._doSupercedeEvent(event)
                else:
                    self._process_trigger_event(tenant, pipeline, event)
            finally:
                self.pipeline_trigger_events[tenant.name][
                    pipeline.name
                ].ack(event)
        self.pipeline_trigger_events[tenant.name][pipeline.name].cleanup()

    def _process_trigger_event(self, tenant, pipeline, event):
        log = get_annotated_logger(
            self.log, event.zuul_event_id
        )
        trusted, project = tenant.getProject(event.canonical_project_name)
        if project is None:
            return
        try:
            change_key = project.source.getChangeKey(event)
            change = project.source.getChange(change_key, event=event)
        except exceptions.ChangeNotFound as e:
            log.debug("Unable to get change %s from source %s",
                      e.change, project.source)
            return

        if event.isPatchsetCreated():
            pipeline.manager.removeOldVersionsOfChange(change, event)
        elif event.isChangeAbandoned():
            pipeline.manager.removeAbandonedChange(change, event)

        # Let the pipeline update any dependencies that may need
        # refreshing if this change has updated.
        if event.isPatchsetCreated() or event.isMessageChanged():
            pipeline.manager.refreshDeps(change, event)

        if pipeline.manager.eventMatches(event, change):
            pipeline.manager.addChange(change, event)

    def process_tenant_management_queue(self, tenant):
        try:
            with management_queue_lock(
                self.zk_client, tenant.name, blocking=False
            ):
                self._process_tenant_management_queue(tenant)
        except LockException:
            self.log.debug("Skipping locked management event queue"
                           " in tenant %s", tenant.name)

    def _process_tenant_management_queue(self, tenant):
        for event in self.management_events[tenant.name]:
            event_forwarded = False
            try:
                if isinstance(event, TenantReconfigureEvent):
                    self._doTenantReconfigureEvent(event)
                elif isinstance(event, (PromoteEvent, ChangeManagementEvent)):
                    event_forwarded = self._forward_management_event(event)
                else:
                    self.log.error("Unable to handle event %s", event)
            finally:
                if event_forwarded:
                    self.management_events[tenant.name].ackWithoutResult(
                        event)
                else:
                    self.management_events[tenant.name].ack(event)
        self.management_events[tenant.name].cleanup()

    def _forward_management_event(self, event):
        event_forwarded = False
        try:
            tenant = self.abide.tenants.get(event.tenant_name)
            if tenant is None:
                raise ValueError(f'Unknown tenant {event.tenant_name}')
            pipeline = tenant.layout.pipelines.get(event.pipeline_name)
            if pipeline is None:
                raise ValueError(f'Unknown pipeline {event.pipeline_name}')
            self.pipeline_management_events[tenant.name][
                pipeline.name
            ].put(event)
            event_forwarded = True
        except Exception:
            event.exception(
                "".join(
                    traceback.format_exception(*sys.exc_info())
                )
            )
        return event_forwarded

    def process_reconfigure_queue(self):
        while not self.reconfigure_event_queue.empty() and not self._stopped:
            self.log.debug("Fetching reconfiguration event")
            event = self.reconfigure_event_queue.get()
            try:
                if isinstance(event, ReconfigureEvent):
                    self._doReconfigureEvent(event)
                else:
                    self.log.error("Unable to handle event %s", event)
            finally:
                if event.ack_ref:
                    event.ack_ref.set()
                self.reconfigure_event_queue.task_done()

    def process_pipeline_management_queue(self, tenant, pipeline):
        for event in self.pipeline_management_events[tenant.name][
            pipeline.name
        ]:
            if self._stopped:
                return
            log = get_annotated_logger(self.log, event.zuul_event_id)
            log.debug("Processing management event %s", event)
            try:
                self._process_management_event(event)
            finally:
                self.pipeline_management_events[tenant.name][
                    pipeline.name
                ].ack(event)
        self.pipeline_management_events[tenant.name][pipeline.name].cleanup()

    def _process_management_event(self, event):
        try:
            if isinstance(event, PromoteEvent):
                self._doPromoteEvent(event)
            elif isinstance(event, DequeueEvent):
                self._doDequeueEvent(event)
            elif isinstance(event, EnqueueEvent):
                self._doEnqueueEvent(event)
            elif isinstance(event, PipelinePostConfigEvent):
                # We don't need to do anything; the event just
                # triggers a pipeline run.
                pass
            elif isinstance(event, PipelineSemaphoreReleaseEvent):
                # Same as above.
                pass
            else:
                self.log.error("Unable to handle event %s" % event)
        except Exception:
            self.log.exception("Exception in management event:")
            event.exception(
                "".join(traceback.format_exception(*sys.exc_info()))
            )

    def process_pipeline_result_queue(self, tenant, pipeline):
        for event in self.pipeline_result_events[tenant.name][pipeline.name]:
            if self._stopped:
                return

            log = get_annotated_logger(
                self.log,
                event=getattr(event, "zuul_event_id", None),
                build=getattr(event, "build_uuid", None),
            )
            log.debug("Processing result event %s", event)
            try:
                self._process_result_event(event, pipeline)
            finally:
                self.pipeline_result_events[tenant.name][
                    pipeline.name
                ].ack(event)
        self.pipeline_result_events[tenant.name][pipeline.name].cleanup()

    def _process_result_event(self, event, pipeline):
        if isinstance(event, BuildStartedEvent):
            self._doBuildStartedEvent(event, pipeline)
        elif isinstance(event, BuildStatusEvent):
            self._doBuildStatusEvent(event, pipeline)
        elif isinstance(event, BuildPausedEvent):
            self._doBuildPausedEvent(event, pipeline)
        elif isinstance(event, BuildCompletedEvent):
            self._doBuildCompletedEvent(event, pipeline)
        elif isinstance(event, MergeCompletedEvent):
            self._doMergeCompletedEvent(event, pipeline)
        elif isinstance(event, FilesChangesCompletedEvent):
            self._doFilesChangesCompletedEvent(event, pipeline)
        elif isinstance(event, NodesProvisionedEvent):
            self._doNodesProvisionedEvent(event, pipeline)
        else:
            self.log.error("Unable to handle event %s", event)

    def _getBuildSetFromPipeline(self, event, pipeline):
        log = get_annotated_logger(
            self.log,
            event=getattr(event, "zuul_event_id", None),
            build=getattr(event, "build_uuid", None),
        )
        if not pipeline:
            log.warning(
                "Build set %s is not associated with a pipeline",
                event.build_set_uuid,
            )
            return

        for item in pipeline.getAllItems():
            # If the provided buildset UUID doesn't match any current one,
            # we assume that it's not current anymore.
            if item.current_build_set.uuid == event.build_set_uuid:
                return item.current_build_set

        log.warning("Build set %s is not current", event.build_set_uuid)

    def _getBuildFromPipeline(self, event, pipeline):
        log = get_annotated_logger(
            self.log, event.zuul_event_id, build=event.build_uuid)
        build_set = self._getBuildSetFromPipeline(event, pipeline)
        if not build_set:
            return

        build = build_set.getBuild(event.job_name)
        # Verify that the build uuid matches the one of the result
        if not build:
            log.debug(
                "Build %s could not be found in the current buildset",
                event.build_uuid)
            return

        # Verify that the build UUIDs match since we looked up the build by
        # its job name. In case of a retried build, we might already have a new
        # build in the buildset.
        # TODO (felix): Not sure if that reasoning is correct, but I think it
        # shouldn't harm to have such an additional safeguard.
        if not build.uuid == event.build_uuid:
            log.debug(
                "Build UUID %s doesn't match the current build's UUID %s",
                event.build_uuid, build.uuid)
            return

        return build

    def _doBuildStartedEvent(self, event, pipeline):
        build = self._getBuildFromPipeline(event, pipeline)
        if not build:
            return

        with build.activeContext(pipeline.manager.current_context):
            build.start_time = event.data["start_time"]

            log = get_annotated_logger(
                self.log, build.zuul_event_id, build=build.uuid)
            try:
                change = build.build_set.item.change
                estimate = self.times.getEstimatedTime(
                    pipeline.tenant.name,
                    change.project.name,
                    getattr(change, 'branch', None),
                    build.job.name)
                if not estimate:
                    estimate = 0.0
                build.estimated_time = estimate
            except Exception:
                log.exception("Exception estimating build time:")
        pipeline.manager.onBuildStarted(build)

    def _doBuildStatusEvent(self, event, pipeline):
        build = self._getBuildFromPipeline(event, pipeline)
        if not build:
            return

        # Allow URL to be updated
        build.updateAttributes(pipeline.manager.current_context,
                               url=event.data.get('url', build.url))

    def _doBuildPausedEvent(self, event, pipeline):
        build = self._getBuildFromPipeline(event, pipeline)
        if not build:
            return

        # Setting paused is deferred to event processing stage to avoid a race
        # with child job skipping.
        with build.activeContext(pipeline.manager.current_context):
            build.paused = True
            build.setResultData(
                event.data.get("data", {}),
                event.data.get("secret_data", {}))

        pipeline.manager.onBuildPaused(build)

    def _doBuildCompletedEvent(self, event, pipeline):
        log = get_annotated_logger(
            self.log, event.zuul_event_id, build=event.build_uuid)
        build = self._getBuildFromPipeline(event, pipeline)
        if not build:
            self.log.error(
                "Unable to find build %s. Creating a fake build to clean up "
                "build resources.",
                event.build_uuid)
            # Create a fake build with a minimal set of attributes that
            # allows reporting the build via SQL and cleaning up build
            # resources.
            build = Build()
            build._set(
                job=Job(event.job_name),
                uuid=event.build_uuid,
                zuul_event_id=event.zuul_event_id,

                # Set the build_request_ref on the fake build to make the
                # cleanup work.
                build_request_ref=event.build_request_ref,

                # TODO (felix): Do we have to fully evaluate the build
                # result (see the if/else block with different results
                # further down) or is it sufficient to just use the result
                # as-is from the executor since the build is anyways
                # outdated. In case the build result is None it won't be
                # changed to RETRY.
                result=event.data.get("result"),
            )

            self._cleanupCompletedBuild(build)
            try:
                self.sql.reportBuildEnd(
                    build, tenant=pipeline.tenant.name,
                    final=True)
            except Exception:
                log.exception("Error reporting build completion to DB:")

            # Make sure we don't forward this outdated build result with
            # an incomplete (fake) build object to the pipeline manager.
            return

        event_result = event.data

        result = event_result.get("result")
        result_data = event_result.get("data", {})
        secret_result_data = event_result.get("secret_data", {})
        warnings = event_result.get("warnings", [])

        log.info("Build complete, result %s, warnings %s", result, warnings)

        with build.activeContext(pipeline.manager.current_context):
            build.error_detail = event_result.get("error_detail")

            if result is None:
                build.retry = True
            if result == "ABORTED":
                # Always retry if the executor just went away
                build.retry = True
            # TODO: Remove merger_failure in v6.0
            if result in ["MERGE_CONFLICT", "MERGER_FAILURE"]:
                # The build result MERGE_CONFLICT is a bit misleading here
                # because when we got here we know that there are no merge
                # conflicts. Instead this is most likely caused by some
                # infrastructure failure. This can be anything like connection
                # issue, drive corruption, full disk, corrupted git cache, etc.
                # This may or may not be a recoverable failure so we should
                # retry here respecting the max retries. But to be able to
                # distinguish from RETRY_LIMIT which normally indicates pre
                # playbook failures we keep the build result after the max
                # attempts.
                if build.build_set.getTries(
                        build.job.name) < build.job.attempts:
                    build.retry = True

            if build.retry:
                result = "RETRY"

            # If the build was canceled, we did actively cancel the job so
            # don't overwrite the result and don't retry.
            if build.canceled:
                result = build.result or "CANCELED"
                build.retry = False

            build.end_time = event_result["end_time"]
            build.setResultData(result_data, secret_result_data)
            build.build_set.updateAttributes(
                pipeline.manager.current_context,
                warning_messages=build.build_set.warning_messages + warnings)
            build.held = event_result.get("held")

            build.result = result

            attributes = {
                "uuid": build.uuid,
                "job": build.job.name,
                "buildset_uuid": build.build_set.item.current_build_set.uuid,
                "zuul_event_id": build.build_set.item.event.zuul_event_id,
            }
            tracing.endSavedSpan(build.span_info, attributes=attributes)

        self._reportBuildStats(build)

        self._cleanupCompletedBuild(build)
        try:
            self.sql.reportBuildEnd(
                build, tenant=pipeline.tenant.name, final=(not build.retry))
        except Exception:
            log.exception("Error reporting build completion to DB:")

        pipeline.manager.onBuildCompleted(build)

    def _cleanupCompletedBuild(self, build):
        # TODO (felix): Returning the nodes doesn't work in case the buildset
        # is not current anymore. Does it harm to not do anything in here in
        # this case?
        if build.build_set:
            # In case the build didn't show up on any executor, the node
            # request does still exist, so we have to make sure it is
            # removed from ZK.
            request_id = build.build_set.getJobNodeRequestID(build.job.name)
            if request_id:
                self.nodepool.deleteNodeRequest(
                    request_id, event_id=build.zuul_event_id)

            # The build is completed and the nodes were already returned
            # by the executor. For consistency, also remove the node
            # request from the build set.
            build.build_set.removeJobNodeRequestID(build.job.name)

        # The test suite expects the build to be removed from the
        # internal dict after it's added to the report queue.
        self.executor.removeBuild(build)

    def _doMergeCompletedEvent(self, event, pipeline):
        build_set = self._getBuildSetFromPipeline(event, pipeline)
        if not build_set:
            return

        tracing.endSavedSpan(
            event.span_info,
            attributes={
                "uuid": event.request_uuid,
                "buildset_uuid": build_set.uuid,
                "zuul_event_id": build_set.item.event.zuul_event_id,
            }
        )
        pipeline.manager.onMergeCompleted(event, build_set)

    def _doFilesChangesCompletedEvent(self, event, pipeline):
        build_set = self._getBuildSetFromPipeline(event, pipeline)
        if not build_set:
            return

        tracing.endSavedSpan(
            event.span_info,
            attributes={
                "uuid": event.request_uuid,
                "buildset_uuid": build_set.uuid,
                "zuul_event_id": build_set.item.event.zuul_event_id,
            }
        )
        pipeline.manager.onFilesChangesCompleted(event, build_set)

    def _doNodesProvisionedEvent(self, event, pipeline):
        request = self.nodepool.zk_nodepool.getNodeRequest(event.request_id)

        if not request:
            self.log.warning("Unable to find request %s while processing"
                             "nodes provisioned event %s", request, event)
            return

        # Look up the buildset to access the local node request object
        build_set = self._getBuildSetFromPipeline(event, pipeline)
        if not build_set:
            self.log.warning("Build set not found while processing"
                             "nodes provisioned event %s", event)
            return

        log = get_annotated_logger(self.log, request.event_id)
        job = build_set.item.getJob(request.job_name)
        if job is None:
            log.warning("Item %s does not contain job %s "
                        "for node request %s",
                        build_set.item, request.job_name, request)
            build_set.removeJobNodeRequestID(request.job_name)
            return

        # If the request failed, we must directly delete it as the nodes will
        # never be accepted.
        if request.state == STATE_FAILED:
            self.nodepool.deleteNodeRequest(request.id,
                                            event_id=event.request_id)

        nodeset = self.nodepool.getNodeSet(request, job.nodeset)

        if build_set.getJobNodeSetInfo(request.job_name) is None:
            pipeline.manager.onNodesProvisioned(request, nodeset, build_set)
        else:
            self.log.warning("Duplicate nodes provisioned event: %s",
                             event)

    def cancelJob(self, buildset, job, build=None, final=False,
                  force=False):
        """Cancel a running build

        Set final to true to create a fake build result even if the
        job has not run.  TODO: explain this better.

        Set force to true to forcibly release build resources without
        waiting for a result from the executor.  Use this when
        removing pipelines.

        """
        item = buildset.item
        log = get_annotated_logger(self.log, item.event)
        job_name = job.name
        try:
            # Cancel node request if needed
            req_id = buildset.getJobNodeRequestID(job_name)
            if req_id:
                req = self.nodepool.zk_nodepool.getNodeRequest(req_id)
                if req:
                    self.nodepool.cancelRequest(req)
                buildset.removeJobNodeRequestID(job_name)

            # Cancel build if needed
            build = build or buildset.getBuild(job_name)
            if build:
                try:
                    self.executor.cancel(build)
                except Exception:
                    log.exception(
                        "Exception while canceling build %s for change %s",
                        build, item.change)

                # In the unlikely case that a build is removed and
                # later added back, make sure we clear out the nodeset
                # so it gets requested again.
                req_info = buildset.getJobNodeSetInfo(job_name)
                if req_info:
                    buildset.removeJobNodeSetInfo(job_name)

                if build.result is None:
                    build.updateAttributes(
                        buildset.item.pipeline.manager.current_context,
                        result='CANCELED')

                if force:
                    # Directly delete the build rather than putting a
                    # CANCELED event in the result event queue since
                    # the result event queue won't be processed
                    # anymore once the pipeline is removed.
                    self.executor.removeBuild(build)
                    try:
                        self.sql.reportBuildEnd(
                            build, build.build_set.item.pipeline.tenant.name,
                            final=False)
                    except Exception:
                        self.log.exception(
                            "Error reporting build completion to DB:")

            else:
                if final:
                    # If final is set make sure that the job is not resurrected
                    # later by re-requesting nodes.
                    fakebuild = Build.new(
                        buildset.item.pipeline.manager.current_context,
                        job=job, build_set=item.current_build_set,
                        result='CANCELED')
                    buildset.addBuild(fakebuild)
        finally:
            # Release the semaphore in any case
            tenant = buildset.item.pipeline.tenant
            tenant.semaphore_handler.release(self, item, job)

    def createZKContext(self, lock, log):
        return ZKContext(self.zk_client, lock, self.stop_event, log)