-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Expand file tree
/
Copy pathContext.cpp
More file actions
7773 lines (6440 loc) · 297 KB
/
Context.cpp
File metadata and controls
7773 lines (6440 loc) · 297 KB
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
#include <atomic>
#include <map>
#include <set>
#include <optional>
#include <memory>
#include <Poco/UUID.h>
#include <Poco/Util/AbstractConfiguration.h>
#include <Poco/Util/Application.h>
#include <Common/ZooKeeper/ZooKeeperCommon.h>
#include <Common/quoteString.h>
#include <Common/setThreadName.h>
#include <Common/ISlotControl.h>
#include <Common/Scheduler/IResourceManager.h>
#include <Common/AsyncLoader.h>
#include <Common/PoolId.h>
#include <Common/SensitiveDataMasker.h>
#include <Common/Macros.h>
#include <Common/EventNotifier.h>
#include <Common/getNumberOfCPUCoresToUse.h>
#include <Common/Stopwatch.h>
#include <Common/formatReadable.h>
#include <Common/Throttler.h>
#include <Common/thread_local_rng.h>
#include <Common/FieldVisitorToString.h>
#include <Common/FieldVisitorHash.h>
#include <Common/SipHash.h>
#include <Common/getMultipleKeysFromConfig.h>
#include <Common/callOnce.h>
#include <Common/SharedLockGuard.h>
#include <Common/PageCache.h>
#include <Common/NamedCollections/NamedCollectionsFactory.h>
#include <Common/isLocalAddress.h>
#include <Common/ConcurrencyControl.h>
#include <Common/SystemAllocatedMemoryHolder.h>
#include <Coordination/KeeperDispatcher.h>
#include <Core/BackgroundSchedulePool.h>
#include <Core/Settings.h>
#include <Formats/FormatFactory.h>
#include <Databases/DatabaseReplicatedSettings.h>
#include <Databases/IDatabase.h>
#include <Interpreters/Context_fwd.h>
#include <Server/ServerType.h>
#include <Storages/MarkCache.h>
#include <Common/JemallocCacheArena.h>
#include <Storages/MergeTree/MergeList.h>
#include <Storages/MergeTree/MovesList.h>
#include <Storages/MergeTree/ReplicatedFetchList.h>
#include <Storages/MergeTree/MergeTreeData.h>
#include <Storages/MergeTree/MergeTreeSettings.h>
#include <Storages/MergeTree/PrimaryIndexCache.h>
#include <Storages/MergeTree/TextIndexCache.h>
#include <Storages/ObjectStorage/DataLakes/Iceberg/IcebergMetadataFilesCache.h>
#include <Processors/Formats/Impl/ParquetMetadataCache.h>
#include <Storages/StreamingStorageRegistry.h>
#include <Storages/MergeTree/VectorSimilarityIndexCache.h>
#include <Storages/Distributed/DistributedSettings.h>
#include <Storages/CompressionCodecSelector.h>
#include <IO/AsynchronousReader.h>
#include <IO/S3Settings.h>
#include <Disks/DiskObjectStorage/ObjectStorages/AzureBlobStorage/AzureBlobStorageCommon.h>
#include <Disks/DiskLocal.h>
#include <Disks/DiskObjectStorage/ObjectStorages/IObjectStorage.h>
#include <Disks/SingleDiskVolume.h>
#include <Disks/StoragePolicy.h>
#include <Disks/IO/IOUringReader.h>
#include <Disks/IO/getIOUringReader.h>
#include <TableFunctions/TableFunctionFactory.h>
#include <Interpreters/ActionLocksManager.h>
#include <Interpreters/ExternalLoaderXMLConfigRepository.h>
#include <Interpreters/TemporaryDataOnDisk.h>
#include <Interpreters/Cache/FileCacheFactory.h>
#include <Interpreters/Cache/FileCache.h>
#include <Interpreters/Cache/QueryConditionCache.h>
#include <Interpreters/Cache/QueryResultCache.h>
#include <Interpreters/Cache/ReverseLookupCache.h>
#include <Interpreters/ContextTimeSeriesTagsCollector.h>
#include <Interpreters/SessionTracker.h>
#include <Interpreters/WasmModuleManager.h>
#include <Core/ServerSettings.h>
#include <Interpreters/PreparedSets.h>
#include <Core/SettingsQuirks.h>
#include <Access/AccessControl.h>
#include <Access/ContextAccess.h>
#include <Access/EnabledRolesInfo.h>
#include <Access/EnabledRowPolicies.h>
#include <Access/QuotaUsage.h>
#include <Access/User.h>
#include <Access/Role.h>
#include <Access/SettingsProfile.h>
#include <Access/SettingsProfilesInfo.h>
#include <Access/SettingsConstraintsAndProfileIDs.h>
#include <Access/ExternalAuthenticators.h>
#include <Access/GSSAcceptor.h>
#include <Backups/BackupsWorker.h>
#include <Dictionaries/Embedded/GeoDictionariesLoader.h>
#include <Interpreters/EmbeddedDictionaries.h>
#include <Interpreters/ExternalDictionariesLoader.h>
#include <Functions/UserDefined/ExternalUserDefinedExecutableFunctionsLoader.h>
#include <Functions/UserDefined/IUserDefinedSQLObjectsStorage.h>
#include <Functions/UserDefined/createUserDefinedSQLObjectsStorage.h>
#include <Functions/UserDefined/UserDefinedSQLFunctionFactory.h>
#include <Interpreters/ProcessList.h>
#include <Interpreters/InterserverCredentials.h>
#include <Interpreters/Cluster.h>
#include <Interpreters/InterserverIOHandler.h>
#include <Interpreters/Context.h>
#include <Interpreters/DDLWorker.h>
#include <Interpreters/DDLTask.h>
#include <Interpreters/Session.h>
#include <Interpreters/TraceCollector.h>
#include <IO/AsyncReadCounters.h>
#include <IO/UncompressedCache.h>
#include <IO/MMappedFileCache.h>
#include <IO/WriteSettings.h>
#include <Parsers/ASTCreateQuery.h>
#include <Parsers/ASTAsterisk.h>
#include <Parsers/ASTIdentifier.h>
#include <Common/Scheduler/createResourceManager.h>
#include <Common/Scheduler/Workload/createWorkloadEntityStorage.h>
#include <Common/StackTrace.h>
#include <Common/Config/ConfigHelper.h>
#include <Common/Config/ConfigProcessor.h>
#include <Common/Config/ConfigReloader.h>
#include <Common/Config/AbstractConfigurationComparison.h>
#include <Common/ZooKeeper/ZooKeeper.h>
#include <Common/logger_useful.h>
#include <Common/RemoteHostFilter.h>
#include <Common/HTTPHeaderFilter.h>
#include <Parsers/parseIdentifierOrStringLiteral.h>
#include <Interpreters/StorageID.h>
#include <Interpreters/SystemLog.h>
#include <Interpreters/InterpreterSelectQueryAnalyzer.h>
#include <Interpreters/AsynchronousInsertQueue.h>
#include <Interpreters/DatabaseCatalog.h>
#include <Interpreters/JIT/CompiledExpressionCache.h>
#include <Storages/MergeTree/BackgroundJobsAssignee.h>
#include <Storages/MergeTree/MergeTreeDataPartUUID.h>
#include <Storages/MaterializedView/RefreshSet.h>
#include <Interpreters/SynonymsExtensions.h>
#include <Interpreters/Lemmatizers.h>
#include <Interpreters/ClusterDiscovery.h>
#include <Interpreters/TransactionLog.h>
#include <Interpreters/ZooKeeperConnectionLog.h>
#include <Interpreters/AggregatedZooKeeperLog.h>
#include <filesystem>
#include <Storages/StorageView.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/FunctionParameterValuesVisitor.h>
#include <Parsers/ASTSelectWithUnionQuery.h>
#include <Interpreters/InterpreterSelectWithUnionQuery.h>
#include <base/defines.h>
#include <Processors/QueryPlan/Optimizations/RuntimeDataflowStatistics.h>
namespace fs = std::filesystem;
namespace ProfileEvents
{
extern const Event ContextLock;
extern const Event ContextLockWaitMicroseconds;
extern const Event LocalReadThrottlerBytes;
extern const Event LocalReadThrottlerSleepMicroseconds;
extern const Event LocalWriteThrottlerBytes;
extern const Event LocalWriteThrottlerSleepMicroseconds;
extern const Event RemoteReadThrottlerBytes;
extern const Event RemoteReadThrottlerSleepMicroseconds;
extern const Event RemoteWriteThrottlerBytes;
extern const Event RemoteWriteThrottlerSleepMicroseconds;
extern const Event BackupThrottlerBytes;
extern const Event BackupThrottlerSleepMicroseconds;
extern const Event MergesThrottlerBytes;
extern const Event MergesThrottlerSleepMicroseconds;
extern const Event MutationsThrottlerBytes;
extern const Event MutationsThrottlerSleepMicroseconds;
extern const Event QueryLocalReadThrottlerBytes;
extern const Event QueryLocalReadThrottlerSleepMicroseconds;
extern const Event QueryLocalWriteThrottlerBytes;
extern const Event QueryLocalWriteThrottlerSleepMicroseconds;
extern const Event QueryRemoteReadThrottlerBytes;
extern const Event QueryRemoteReadThrottlerSleepMicroseconds;
extern const Event QueryRemoteWriteThrottlerBytes;
extern const Event QueryRemoteWriteThrottlerSleepMicroseconds;
extern const Event QueryBackupThrottlerBytes;
extern const Event QueryBackupThrottlerSleepMicroseconds;
extern const Event MergeMutateBackgroundExecutorTaskExecuteStepMicroseconds;
extern const Event MergeMutateBackgroundExecutorTaskCancelMicroseconds;
extern const Event MergeMutateBackgroundExecutorTaskResetMicroseconds;
extern const Event MergeMutateBackgroundExecutorWaitMicroseconds;
extern const Event MoveBackgroundExecutorTaskExecuteStepMicroseconds;
extern const Event MoveBackgroundExecutorTaskCancelMicroseconds;
extern const Event MoveBackgroundExecutorTaskResetMicroseconds;
extern const Event MoveBackgroundExecutorWaitMicroseconds;
extern const Event FetchBackgroundExecutorTaskExecuteStepMicroseconds;
extern const Event FetchBackgroundExecutorTaskCancelMicroseconds;
extern const Event FetchBackgroundExecutorTaskResetMicroseconds;
extern const Event FetchBackgroundExecutorWaitMicroseconds;
extern const Event CommonBackgroundExecutorTaskExecuteStepMicroseconds;
extern const Event CommonBackgroundExecutorTaskCancelMicroseconds;
extern const Event CommonBackgroundExecutorTaskResetMicroseconds;
extern const Event CommonBackgroundExecutorWaitMicroseconds;
}
namespace CurrentMetrics
{
extern const Metric ContextLockWait;
extern const Metric BackgroundMovePoolTask;
extern const Metric BackgroundMovePoolSize;
extern const Metric BackgroundSchedulePoolTask;
extern const Metric BackgroundSchedulePoolSize;
extern const Metric BackgroundBufferFlushSchedulePoolTask;
extern const Metric BackgroundBufferFlushSchedulePoolSize;
extern const Metric BackgroundDistributedSchedulePoolTask;
extern const Metric BackgroundDistributedSchedulePoolSize;
extern const Metric BackgroundMessageBrokerSchedulePoolTask;
extern const Metric BackgroundMessageBrokerSchedulePoolSize;
extern const Metric BackgroundMergesAndMutationsPoolTask;
extern const Metric BackgroundMergesAndMutationsPoolSize;
extern const Metric BackgroundFetchesPoolTask;
extern const Metric BackgroundFetchesPoolSize;
extern const Metric BackgroundCommonPoolTask;
extern const Metric BackgroundCommonPoolSize;
extern const Metric IcebergSchedulePoolTask;
extern const Metric IcebergSchedulePoolSize;
extern const Metric MarksLoaderThreads;
extern const Metric MarksLoaderThreadsActive;
extern const Metric MarksLoaderThreadsScheduled;
extern const Metric IOPrefetchThreads;
extern const Metric IOPrefetchThreadsActive;
extern const Metric IOPrefetchThreadsScheduled;
extern const Metric IOWriterThreads;
extern const Metric IOWriterThreadsActive;
extern const Metric TablesLoaderBackgroundThreads;
extern const Metric TablesLoaderBackgroundThreadsActive;
extern const Metric TablesLoaderBackgroundThreadsScheduled;
extern const Metric TablesLoaderForegroundThreads;
extern const Metric TablesLoaderForegroundThreadsActive;
extern const Metric TablesLoaderForegroundThreadsScheduled;
extern const Metric IOWriterThreadsScheduled;
extern const Metric BuildVectorSimilarityIndexThreads;
extern const Metric BuildVectorSimilarityIndexThreadsActive;
extern const Metric BuildVectorSimilarityIndexThreadsScheduled;
extern const Metric AttachedTable;
extern const Metric AttachedView;
extern const Metric AttachedDictionary;
extern const Metric AttachedDatabase;
extern const Metric PartsActive;
extern const Metric NamedCollection;
extern const Metric IcebergCatalogThreads;
extern const Metric IcebergCatalogThreadsActive;
extern const Metric IcebergCatalogThreadsScheduled;
extern const Metric IndexMarkCacheBytes;
extern const Metric IndexMarkCacheFiles;
extern const Metric MarkCacheBytes;
extern const Metric MarkCacheFiles;
extern const Metric UncompressedCacheBytes;
extern const Metric UncompressedCacheCells;
extern const Metric IndexUncompressedCacheBytes;
extern const Metric IndexUncompressedCacheCells;
extern const Metric ZooKeeperSessionExpired;
extern const Metric ZooKeeperConnectionLossStartedTimestampSeconds;
}
namespace DB
{
namespace Setting
{
extern const SettingsUInt64 allow_experimental_parallel_reading_from_replicas;
extern const SettingsFloat ast_fuzzer_runs;
extern const SettingsUInt64 automatic_parallel_replicas_mode;
extern const SettingsMilliseconds async_insert_poll_timeout_ms;
extern const SettingsBool azure_allow_parallel_part_upload;
extern const SettingsString cluster_for_parallel_replicas;
extern const SettingsBool enable_filesystem_cache;
extern const SettingsBool enable_filesystem_cache_log;
extern const SettingsBool enable_filesystem_cache_on_write_operations;
extern const SettingsBool enable_filesystem_read_prefetches_log;
extern const SettingsBool enable_blob_storage_log;
extern const SettingsUInt64 filesystem_cache_max_download_size;
extern const SettingsUInt64 filesystem_cache_reserve_space_wait_lock_timeout_milliseconds;
extern const SettingsUInt64 filesystem_cache_segments_batch_size;
extern const SettingsBool filesystem_cache_allow_background_download;
extern const SettingsBool filesystem_cache_enable_background_download_for_metadata_files_in_packed_storage;
extern const SettingsBool filesystem_cache_enable_background_download_during_fetch;
extern const SettingsBool filesystem_cache_prefer_bigger_buffer_size;
extern const SettingsBool http_make_head_request;
extern const SettingsUInt64 http_max_fields;
extern const SettingsUInt64 http_max_field_name_size;
extern const SettingsUInt64 http_max_field_value_size;
extern const SettingsUInt64 http_max_tries;
extern const SettingsUInt64 http_max_uri_size;
extern const SettingsSeconds http_receive_timeout;
extern const SettingsUInt64 http_retry_initial_backoff_ms;
extern const SettingsUInt64 http_retry_max_backoff_ms;
extern const SettingsSeconds http_send_timeout;
extern const SettingsBool http_skip_not_found_url_for_globs;
extern const SettingsUInt64 hsts_max_age;
extern const SettingsString local_filesystem_read_method;
extern const SettingsBool local_filesystem_read_prefetch;
extern const SettingsUInt64 max_backup_bandwidth;
extern const SettingsUInt64 max_local_read_bandwidth;
extern const SettingsUInt64 max_local_write_bandwidth;
extern const SettingsNonZeroUInt64 max_parallel_replicas;
extern const SettingsNonZeroUInt64 max_read_buffer_size;
extern const SettingsUInt64 max_read_buffer_size_local_fs;
extern const SettingsUInt64 max_read_buffer_size_remote_fs;
extern const SettingsUInt64 max_remote_read_network_bandwidth;
extern const SettingsUInt64 max_remote_write_network_bandwidth;
extern const SettingsUInt64 min_bytes_to_use_direct_io;
extern const SettingsUInt64 min_bytes_to_use_mmap_io;
extern const SettingsBool page_cache_inject_eviction;
extern const SettingsParallelReplicasMode parallel_replicas_mode;
extern const SettingsString parallel_replicas_custom_key;
extern const SettingsUInt64 prefetch_buffer_size;
extern const SettingsBool read_from_filesystem_cache_if_exists_otherwise_bypass_cache;
extern const SettingsBool read_from_page_cache_if_exists_otherwise_bypass_cache;
extern const SettingsUInt64 page_cache_block_size;
extern const SettingsUInt64 page_cache_lookahead_blocks;
extern const SettingsInt64 read_priority;
extern const SettingsString remote_filesystem_read_method;
extern const SettingsBool remote_filesystem_read_prefetch;
extern const SettingsUInt64 remote_fs_read_max_backoff_ms;
extern const SettingsUInt64 remote_fs_read_backoff_max_tries;
extern const SettingsUInt64 remote_read_min_bytes_for_seek;
extern const SettingsBool throw_on_error_from_cache_on_write_operations;
extern const SettingsBool filesystem_cache_skip_download_if_exceeds_per_query_cache_write_limit;
extern const SettingsBool s3_allow_parallel_part_upload;
extern const SettingsBool use_page_cache_for_disks_without_file_cache;
extern const SettingsBool use_page_cache_for_local_disks;
extern const SettingsBool use_page_cache_for_object_storage;
extern const SettingsBool use_page_cache_with_distributed_cache;
extern const SettingsUInt64 use_structure_from_insertion_table_in_table_functions;
extern const SettingsString workload;
extern const SettingsString compatibility;
extern const SettingsBool allow_experimental_analyzer;
extern const SettingsBool parallel_replicas_only_with_analyzer;
extern const SettingsBool enable_hdfs_pread;
extern const SettingsUInt64 max_reverse_dictionary_lookup_cache_size_bytes;
}
namespace MergeTreeSetting
{
extern const MergeTreeSettingsString merge_workload;
extern const MergeTreeSettingsString mutation_workload;
}
namespace ServerSetting
{
extern const ServerSettingsUInt64 background_buffer_flush_schedule_pool_size;
extern const ServerSettingsUInt64 background_common_pool_size;
extern const ServerSettingsUInt64 background_distributed_schedule_pool_size;
extern const ServerSettingsUInt64 background_fetches_pool_size;
extern const ServerSettingsFloat background_merges_mutations_concurrency_ratio;
extern const ServerSettingsString background_merges_mutations_scheduling_policy;
extern const ServerSettingsUInt64 background_message_broker_schedule_pool_size;
extern const ServerSettingsUInt64 iceberg_background_schedule_pool_size;
extern const ServerSettingsUInt64 background_move_pool_size;
extern const ServerSettingsUInt64 background_pool_size;
extern const ServerSettingsUInt64 background_schedule_pool_size;
extern const ServerSettingsFloat background_schedule_pool_max_parallel_tasks_per_type_ratio;
extern const ServerSettingsBool disable_insertion_and_mutation;
extern const ServerSettingsBool display_secrets_in_show_and_select;
extern const ServerSettingsUInt64 max_backup_bandwidth_for_server;
extern const ServerSettingsUInt64 max_build_vector_similarity_index_thread_pool_size;
extern const ServerSettingsUInt64 max_local_read_bandwidth_for_server;
extern const ServerSettingsUInt64 max_local_write_bandwidth_for_server;
extern const ServerSettingsUInt64 max_merges_bandwidth_for_server;
extern const ServerSettingsUInt64 max_mutations_bandwidth_for_server;
extern const ServerSettingsUInt64 max_remote_read_network_bandwidth_for_server;
extern const ServerSettingsUInt64 max_remote_write_network_bandwidth_for_server;
extern const ServerSettingsUInt64 max_replicated_fetches_network_bandwidth_for_server;
extern const ServerSettingsUInt64 max_replicated_sends_network_bandwidth_for_server;
extern const ServerSettingsBool s3queue_disable_streaming;
extern const ServerSettingsUInt64 tables_loader_background_pool_size;
extern const ServerSettingsUInt64 tables_loader_foreground_pool_size;
extern const ServerSettingsNonZeroUInt64 prefetch_threadpool_pool_size;
extern const ServerSettingsUInt64 prefetch_threadpool_queue_size;
extern const ServerSettingsUInt64 load_marks_threadpool_pool_size;
extern const ServerSettingsUInt64 load_marks_threadpool_queue_size;
extern const ServerSettingsNonZeroUInt64 threadpool_writer_pool_size;
extern const ServerSettingsUInt64 threadpool_writer_queue_size;
extern const ServerSettingsUInt64 iceberg_catalog_threadpool_pool_size;
extern const ServerSettingsUInt64 iceberg_catalog_threadpool_queue_size;
extern const ServerSettingsBool dictionaries_lazy_load;
extern const ServerSettingsInt32 os_threads_nice_value_zookeeper_client_send_receive;
extern const ServerSettingsBool enforce_keeper_component_tracking;
extern const ServerSettingsUInt64 max_table_num_to_throw;
extern const ServerSettingsUInt64 max_view_num_to_throw;
extern const ServerSettingsUInt64 max_dictionary_num_to_throw;
extern const ServerSettingsUInt64 max_database_num_to_throw;
extern const ServerSettingsUInt64 max_named_collection_num_to_throw;
extern const ServerSettingsBool allow_experimental_webassembly_udf;
extern const ServerSettingsString webassembly_udf_engine;
}
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
extern const int UNKNOWN_DATABASE;
extern const int UNKNOWN_TABLE;
extern const int TABLE_ALREADY_EXISTS;
extern const int THERE_IS_NO_SESSION;
extern const int THERE_IS_NO_QUERY;
extern const int NO_ELEMENTS_IN_CONFIG;
extern const int TABLE_SIZE_EXCEEDS_MAX_DROP_SIZE_LIMIT;
extern const int LOGICAL_ERROR;
extern const int INVALID_SETTING_VALUE;
extern const int NOT_IMPLEMENTED;
extern const int UNKNOWN_FUNCTION;
extern const int SUPPORT_IS_DISABLED;
extern const int ILLEGAL_COLUMN;
extern const int NUMBER_OF_COLUMNS_DOESNT_MATCH;
extern const int CLUSTER_DOESNT_EXIST;
extern const int SET_NON_GRANTED_ROLE;
extern const int UNKNOWN_DISK;
extern const int UNKNOWN_READ_METHOD;
}
#define SHUTDOWN(log, desc, ptr, method) do \
{ \
if (ptr) \
{ \
LOG_DEBUG(log, "Shutting down " desc); \
(ptr)->method; \
} \
} while (false) \
/** Set of known objects (environment), that could be used in query.
* Shared (global) part. Order of members (especially, order of destruction) is very important.
*/
struct ContextSharedPart : boost::noncopyable
{
LoggerPtr log = getLogger("Context");
/// For access of most of shared objects.
mutable ContextSharedMutex mutex;
/// Separate mutex for access of dictionaries. Separate mutex to avoid locks when server doing request to itself.
mutable std::mutex embedded_dictionaries_mutex;
mutable std::mutex external_dictionaries_mutex;
mutable std::mutex external_user_defined_executable_functions_mutex;
/// Separate mutex for storage policies. During server startup we may
/// initialize some important storages (system logs with MergeTree engine)
/// under context lock.
mutable std::mutex storage_policies_mutex;
/// Separate mutex for re-initialization of zookeeper session. This operation could take a long time and must not interfere with another operations.
mutable std::mutex zookeeper_mutex;
mutable zkutil::ZooKeeperPtr zookeeper TSA_GUARDED_BY(zookeeper_mutex); /// Client for ZooKeeper.
ConfigurationPtr zookeeper_config TSA_GUARDED_BY(zookeeper_mutex); /// Stores zookeeper configs
ConfigurationPtr sensitive_data_masker_config;
mutable std::mutex auxiliary_zookeepers_mutex;
mutable std::map<String, zkutil::ZooKeeperPtr> auxiliary_zookeepers TSA_GUARDED_BY(auxiliary_zookeepers_mutex); /// Map for auxiliary ZooKeeper clients.
ConfigurationPtr auxiliary_zookeepers_config TSA_GUARDED_BY(auxiliary_zookeepers_mutex); /// Stores auxiliary zookeepers configs
/// No lock required for interserver_io_host, interserver_io_port, interserver_scheme modified only during initialization
String interserver_io_host; /// The host name by which this server is available for other servers.
UInt16 interserver_io_port = 0; /// and port.
String interserver_scheme; /// http or https
MultiVersion<InterserverCredentials> interserver_io_credentials;
String path TSA_GUARDED_BY(mutex); /// Path to the data directory, with a slash at the end.
String flags_path TSA_GUARDED_BY(mutex); /// Path to the directory with some control flags for server maintenance.
String user_files_path TSA_GUARDED_BY(mutex); /// Path to the directory with user provided files, usable by 'file' table function.
String dictionaries_lib_path TSA_GUARDED_BY(mutex); /// Path to the directory with user provided binaries and libraries for external dictionaries.
String user_scripts_path TSA_GUARDED_BY(mutex); /// Path to the directory with user provided scripts.
String filesystem_caches_path TSA_GUARDED_BY(mutex); /// Path to the directory with filesystem caches.
String filesystem_cache_user TSA_GUARDED_BY(mutex);
ConfigurationPtr config TSA_GUARDED_BY(mutex); /// Global configuration settings.
String tmp_path TSA_GUARDED_BY(mutex); /// Path to the temporary files that occur when processing the request.
/// The default disk storing metadata files for databases: database metadata files and table metadata files.
/// For DBs which have `disk` setting in the create query, the table metadata files of these DBs are stored on that disk.
/// However, the DB metadata files are still stored on this `default_db_disk`. So the instance can load its DBs during starting up.
std::shared_ptr<IDisk> default_db_disk TSA_GUARDED_BY(mutex);
/// All temporary files that occur when processing the requests accounted here.
/// Child scopes for more fine-grained accounting are created per user/query/etc.
/// Initialized once during server startup.
TemporaryDataOnDiskScopePtr root_temp_data_on_disk TSA_GUARDED_BY(mutex);
/// TODO: remove, use only root_temp_data_on_disk
VolumePtr temporary_volume_legacy;
mutable OnceFlag async_loader_initialized;
mutable std::unique_ptr<AsyncLoader> async_loader; /// Thread pool for asynchronous initialization of arbitrary DAG of `LoadJob`s (used for tables loading)
mutable std::unique_ptr<EmbeddedDictionaries> embedded_dictionaries TSA_GUARDED_BY(embedded_dictionaries_mutex); /// Metrica's dictionaries. Have lazy initialization.
mutable std::unique_ptr<ExternalDictionariesLoader> external_dictionaries_loader TSA_GUARDED_BY(external_dictionaries_mutex);
ExternalLoaderXMLConfigRepository * external_dictionaries_config_repository TSA_GUARDED_BY(external_dictionaries_mutex) = nullptr;
scope_guard dictionaries_xmls TSA_GUARDED_BY(external_dictionaries_mutex);
mutable std::unique_ptr<ExternalUserDefinedExecutableFunctionsLoader> external_user_defined_executable_functions_loader TSA_GUARDED_BY(external_user_defined_executable_functions_mutex);
ExternalLoaderXMLConfigRepository * user_defined_executable_functions_config_repository TSA_GUARDED_BY(external_user_defined_executable_functions_mutex) = nullptr;
scope_guard user_defined_executable_functions_xmls TSA_GUARDED_BY(external_user_defined_executable_functions_mutex);
mutable OnceFlag user_defined_sql_objects_storage_initialized;
mutable std::unique_ptr<IUserDefinedSQLObjectsStorage> user_defined_sql_objects_storage;
mutable OnceFlag workload_entity_storage_initialized;
mutable std::unique_ptr<IWorkloadEntityStorage> workload_entity_storage;
mutable std::unique_ptr<WasmModuleManager> wasm_module_manager;
#if USE_NLP
mutable OnceFlag synonyms_extensions_initialized;
mutable std::optional<SynonymsExtensions> synonyms_extensions;
mutable OnceFlag lemmatizers_initialized;
mutable std::optional<Lemmatizers> lemmatizers;
#endif
mutable OnceFlag backups_worker_initialized;
std::optional<BackupsWorker> backups_worker;
/// No lock required for default_profile_name, system_profile_name, buffer_profile_name modified only during initialization
String default_profile_name; /// Default profile name used for default values.
String system_profile_name; /// Profile used by system processes
String background_profile_name; /// Profile used by background operations
String buffer_profile_name; /// Profile used by Buffer engine for flushing to the underlying
String merge_workload TSA_GUARDED_BY(mutex); /// Workload setting value that is used by all merges
String mutation_workload TSA_GUARDED_BY(mutex); /// Workload setting value that is used by all mutations
String license_file TSA_GUARDED_BY(mutex); /// BYOC license text
bool throw_on_unknown_workload TSA_GUARDED_BY(mutex) = false;
bool cpu_slot_preemption TSA_GUARDED_BY(mutex) = false;
UInt64 cpu_slot_quantum_ns TSA_GUARDED_BY(mutex) = 10'000'000;
UInt64 cpu_slot_preemption_timeout_ms TSA_GUARDED_BY(mutex) = 1000;
UInt64 concurrent_threads_soft_limit_num TSA_GUARDED_BY(mutex) = 0;
UInt64 concurrent_threads_soft_limit_ratio_to_cores TSA_GUARDED_BY(mutex) = 0;
String concurrent_threads_scheduler TSA_GUARDED_BY(mutex);
std::unique_ptr<AccessControl> access_control TSA_GUARDED_BY(mutex);
mutable OnceFlag resource_manager_initialized;
mutable ResourceManagerPtr resource_manager;
mutable UncompressedCachePtr uncompressed_cache TSA_GUARDED_BY(mutex); /// The cache of decompressed blocks.
mutable MarkCachePtr mark_cache TSA_GUARDED_BY(mutex); /// Cache of marks in compressed files.
mutable PrimaryIndexCachePtr primary_index_cache TSA_GUARDED_BY(mutex);
mutable SystemAllocatedMemoryHolderPtr untracked_memory_holder TSA_GUARDED_BY(mutex);
mutable OnceFlag load_marks_threadpool_initialized;
mutable std::unique_ptr<ThreadPool> load_marks_threadpool; /// Threadpool for loading marks cache.
mutable OnceFlag prefetch_threadpool_initialized;
mutable std::unique_ptr<ThreadPool> prefetch_threadpool; /// Threadpool for loading marks cache.
mutable std::unique_ptr<ThreadPool> iceberg_catalog_threadpool;
mutable OnceFlag iceberg_catalog_threadpool_initialized;
mutable OnceFlag build_vector_similarity_index_threadpool_initialized;
mutable std::unique_ptr<ThreadPool> build_vector_similarity_index_threadpool; /// Threadpool for vector-similarity index creation.
mutable UncompressedCachePtr index_uncompressed_cache TSA_GUARDED_BY(mutex); /// The cache of decompressed blocks for MergeTree indices.
mutable VectorSimilarityIndexCachePtr vector_similarity_index_cache TSA_GUARDED_BY(mutex); /// Cache of deserialized secondary index granules.
mutable TextIndexTokensCachePtr text_index_tokens_cache TSA_GUARDED_BY(mutex); /// Cache of deserialized text index tokens.
mutable TextIndexHeaderCachePtr text_index_header_cache TSA_GUARDED_BY(mutex); /// Cache of deserialized text index headers.
mutable TextIndexPostingsCachePtr text_index_postings_cache TSA_GUARDED_BY(mutex); /// Cache of deserialized text index posting lists.
mutable QueryConditionCachePtr query_condition_cache TSA_GUARDED_BY(mutex); /// Cache of matching marks for predicates
mutable QueryResultCachePtr query_result_cache TSA_GUARDED_BY(mutex); /// Cache of query results.
mutable MarkCachePtr index_mark_cache TSA_GUARDED_BY(mutex); /// Cache of marks in compressed files of MergeTree indices.
mutable MMappedFileCachePtr mmap_cache TSA_GUARDED_BY(mutex); /// Cache of mmapped files to avoid frequent open/map/unmap/close and to reuse from several threads.
#if USE_AVRO
mutable IcebergMetadataFilesCachePtr iceberg_metadata_files_cache TSA_GUARDED_BY(mutex); /// Cache of deserialized iceberg metadata files.
#endif
#if USE_PARQUET
mutable ParquetMetadataCachePtr parquet_metadata_cache TSA_GUARDED_BY(mutex); /// Cache of deserialized parquet metadata files.
#endif
AsynchronousMetrics * asynchronous_metrics TSA_GUARDED_BY(mutex) = nullptr; /// Points to asynchronous metrics
mutable PageCachePtr page_cache TSA_GUARDED_BY(mutex); /// Userspace page cache.
ProcessList process_list; /// Executing queries at the moment.
SessionTracker session_tracker;
GlobalOvercommitTracker global_overcommit_tracker;
MergeList merge_list; /// The list of executable merge (for (Replicated)?MergeTree)
MovesList moves_list; /// The list of executing moves (for (Replicated)?MergeTree)
ReplicatedFetchList replicated_fetch_list;
RefreshSet refresh_set; /// The list of active refreshes (for MaterializedView)
ConfigurationPtr users_config TSA_GUARDED_BY(mutex); /// Config with the users, profiles and quotas sections.
InterserverIOHandler interserver_io_handler; /// Handler for interserver communication.
OnceFlag buffer_flush_schedule_pool_initialized;
mutable BackgroundSchedulePoolPtr buffer_flush_schedule_pool; /// A thread pool that can do background flush for Buffer tables.
OnceFlag schedule_pool_initialized;
mutable BackgroundSchedulePoolPtr schedule_pool; /// A thread pool that can run different jobs in background (used in replicated tables)
OnceFlag distributed_schedule_pool_initialized;
mutable BackgroundSchedulePoolPtr distributed_schedule_pool; /// A thread pool that can run different jobs in background (used for distributed sends)
OnceFlag message_broker_schedule_pool_initialized;
mutable BackgroundSchedulePoolPtr message_broker_schedule_pool; /// A thread pool that can run different jobs in background (used for message brokers, like RabbitMQ and Kafka)
OnceFlag iceberg_schedule_pool_initialized;
mutable BackgroundSchedulePoolPtr iceberg_schedule_pool; /// A thread pool that runs background metadata refresh for all active Iceberg tables
mutable OnceFlag readers_initialized;
mutable std::unique_ptr<IAsynchronousReader> asynchronous_remote_fs_reader;
mutable std::unique_ptr<IAsynchronousReader> asynchronous_local_fs_reader;
mutable std::unique_ptr<IAsynchronousReader> synchronous_local_fs_reader;
mutable OnceFlag threadpool_writer_initialized;
mutable std::unique_ptr<ThreadPool> threadpool_writer;
#if USE_LIBURING
mutable OnceFlag io_uring_reader_initialized;
mutable std::unique_ptr<IOUringReader> io_uring_reader;
#endif
mutable ThrottlerPtr replicated_fetches_throttler; /// A server-wide throttler for replicated fetches
mutable ThrottlerPtr replicated_sends_throttler; /// A server-wide throttler for replicated sends
mutable ThrottlerPtr remote_read_throttler; /// A server-wide throttler for remote IO reads
mutable ThrottlerPtr remote_write_throttler; /// A server-wide throttler for remote IO writes
mutable ThrottlerPtr local_read_throttler; /// A server-wide throttler for local IO reads
mutable ThrottlerPtr local_write_throttler; /// A server-wide throttler for local IO writes
mutable ThrottlerPtr backups_server_throttler; /// A server-wide throttler for BACKUPs
mutable ThrottlerPtr mutations_throttler; /// A server-wide throttler for mutations
mutable ThrottlerPtr merges_throttler; /// A server-wide throttler for merges
mutable ThrottlerPtr distributed_cache_read_throttler; /// A server-wide throttler for distributed cache read
mutable ThrottlerPtr distributed_cache_write_throttler; /// A server-wide throttler for distributed cache write
MultiVersion<Macros> macros; /// Substitutions extracted from config.
std::unique_ptr<DDLWorker> ddl_worker TSA_GUARDED_BY(mutex); /// Process ddl commands from zk.
LoadTaskPtr ddl_worker_startup_task; /// To postpone `ddl_worker->startup()` after all tables startup
/// Rules for selecting the compression settings, depending on the size of the part.
mutable std::unique_ptr<CompressionCodecSelector> compression_codec_selector TSA_GUARDED_BY(mutex);
/// Storage disk chooser for MergeTree engines
mutable std::shared_ptr<const DiskSelector> merge_tree_disk_selector TSA_GUARDED_BY(storage_policies_mutex);
/// Storage policy chooser for MergeTree engines
mutable std::shared_ptr<const StoragePolicySelector> merge_tree_storage_policy_selector TSA_GUARDED_BY(storage_policies_mutex);
ServerSettings server_settings;
std::optional<MergeTreeSettings> merge_tree_settings TSA_GUARDED_BY(mutex); /// Settings of MergeTree* engines.
std::optional<MergeTreeSettings> replicated_merge_tree_settings TSA_GUARDED_BY(mutex); /// Settings of ReplicatedMergeTree* engines.
std::optional<DatabaseReplicatedSettings> database_replicated_settings TSA_GUARDED_BY(mutex); /// Settings of DatabaseReplicated engine.
std::optional<DistributedSettings> distributed_settings TSA_GUARDED_BY(mutex);
std::atomic_size_t max_table_size_to_drop = 50000000000lu; /// Protects MergeTree tables from accidental DROP (50GB by default)
std::atomic_size_t max_partition_size_to_drop = 50000000000lu; /// Protects MergeTree partitions from accidental DROP (50GB by default)
/// No lock required for format_schema_path modified only during initialization
std::atomic_size_t max_database_num_to_warn = 1000lu;
std::atomic_size_t max_named_collection_num_to_warn = 1000lu;
std::atomic_size_t max_table_num_to_warn = 5000lu;
std::atomic_size_t max_view_num_to_warn = 10000lu;
std::atomic_size_t max_dictionary_num_to_warn = 1000lu;
std::atomic_size_t max_part_num_to_warn = 100000lu;
// these variables are used in inserting warning message into system.warning table based on asynchronous metrics
size_t max_pending_mutations_to_warn = 500lu;
size_t max_pending_mutations_execution_time_to_warn = 86400lu;
/// Only for system.server_settings, actually value stored in reloader itself
std::atomic_size_t config_reload_interval_ms = ConfigReloader::DEFAULT_RELOAD_INTERVAL.count();
double min_os_cpu_wait_time_ratio_to_drop_connection = 15.0;
double max_os_cpu_wait_time_ratio_to_drop_connection = 30.0;
String format_schema_path; /// Path to a directory that contains schema files used by input formats.
String google_protos_path; /// Path to a directory that contains the proto files for the well-known Protobuf types.
mutable OnceFlag action_locks_manager_initialized;
ActionLocksManagerPtr action_locks_manager; /// Set of storages' action lockers
OnceFlag system_logs_initialized;
std::unique_ptr<SystemLogs> system_logs TSA_GUARDED_BY(mutex); /// Used to log queries and operations on parts
mutable std::mutex dashboard_mutex;
std::optional<Context::Dashboards> dashboards;
mutable SharedMutex users_to_ignore_early_memory_limit_check_mutex;
std::string users_to_ignore_early_memory_limit_check_source TSA_GUARDED_BY(users_to_ignore_early_memory_limit_check_mutex);
std::shared_ptr<std::unordered_set<std::string>> users_to_ignore_early_memory_limit_check TSA_GUARDED_BY(users_to_ignore_early_memory_limit_check_mutex);
std::optional<S3SettingsByEndpoint> storage_s3_settings TSA_GUARDED_BY(mutex); /// Settings of S3 storage
std::optional<AzureSettingsByEndpoint> storage_azure_settings TSA_GUARDED_BY(mutex); /// Settings of AzureBlobStorage
std::unordered_map<Context::WarningType, PreformattedMessage> warnings TSA_GUARDED_BY(mutex); /// Store warning messages about server.
/// Background executors for *MergeTree tables
/// Has background executors for MergeTree tables been initialized?
mutable ContextSharedMutex background_executors_mutex;
bool are_background_executors_initialized TSA_GUARDED_BY(background_executors_mutex) = false;
MergeMutateBackgroundExecutorPtr merge_mutate_executor TSA_GUARDED_BY(background_executors_mutex);
OrdinaryBackgroundExecutorPtr moves_executor TSA_GUARDED_BY(background_executors_mutex);
OrdinaryBackgroundExecutorPtr fetch_executor TSA_GUARDED_BY(background_executors_mutex);
OrdinaryBackgroundExecutorPtr common_executor TSA_GUARDED_BY(background_executors_mutex);
RemoteHostFilter remote_host_filter; /// Allowed URL from config.xml
HTTPHeaderFilter http_header_filter; /// Forbidden HTTP headers from config.xml
/// No lock required for trace_collector modified only during initialization
std::optional<TraceCollector> trace_collector; /// Thread collecting traces from threads executing queries
/// Clusters for distributed tables
/// Initialized on demand (on distributed storages initialization) since Settings should be initialized
mutable std::mutex clusters_mutex; /// Guards clusters, clusters_config and cluster_discovery
std::shared_ptr<Clusters> clusters TSA_GUARDED_BY(clusters_mutex);
ConfigurationPtr clusters_config TSA_GUARDED_BY(clusters_mutex); /// Stores updated configs
std::unique_ptr<ClusterDiscovery> cluster_discovery TSA_GUARDED_BY(clusters_mutex);
size_t clusters_version TSA_GUARDED_BY(clusters_mutex) = 0;
/// No lock required for async_insert_queue modified only during initialization
std::shared_ptr<AsynchronousInsertQueue> async_insert_queue;
std::map<String, UInt16> server_ports;
std::atomic<bool> shutdown_called = false;
Stopwatch uptime_watch TSA_GUARDED_BY(mutex);
/// No lock required for application_type modified only during initialization
Context::ApplicationType application_type = Context::ApplicationType::SERVER;
/// No lock required for config_reload_callback, start_servers_callback, stop_servers_callback modified only during initialization
Context::ConfigReloadCallback config_reload_callback;
Context::StartStopServersCallback start_servers_callback;
Context::StartStopServersCallback stop_servers_callback;
bool is_server_completely_started TSA_GUARDED_BY(mutex) = false;
#if USE_NURAFT
mutable std::shared_ptr<KeeperDispatcher> keeper_dispatcher;
#endif
ContextSharedPart()
: access_control(std::make_unique<AccessControl>()), global_overcommit_tracker(&process_list), macros(std::make_unique<Macros>())
{
/// TODO: make it singleton (?)
static std::atomic<size_t> num_calls{0};
if (++num_calls > 1)
{
std::cerr << "Attempting to create multiple ContextShared instances. Stack trace:\n" << StackTrace().toString();
std::cerr.flush();
std::terminate();
}
}
~ContextSharedPart()
{
/// Shutdown must be called first to stop all background tasks (like loadOutdatedDataParts)
/// that may be using the thread pool readers. Otherwise there is a data race between
/// background tasks calling getThreadPoolReader() and the destructor resetting the readers.
/// See https://github.com/ClickHouse/ClickHouse/issues/62143
try
{
shutdown();
}
catch (...)
{
tryLogCurrentException(__PRETTY_FUNCTION__);
}
#if USE_NURAFT
if (keeper_dispatcher)
{
try
{
keeper_dispatcher->shutdown();
}
catch (...)
{
tryLogCurrentException(__PRETTY_FUNCTION__);
}
}
#endif
/// Wait for thread pool for background reads and writes,
/// since it may use per-user MemoryTracker which will be destroyed here.
if (asynchronous_remote_fs_reader)
{
try
{
LOG_DEBUG(log, "Destructing remote fs threadpool reader");
asynchronous_remote_fs_reader->wait();
asynchronous_remote_fs_reader.reset();
}
catch (...)
{
tryLogCurrentException(__PRETTY_FUNCTION__);
}
}
if (asynchronous_local_fs_reader)
{
try
{
LOG_DEBUG(log, "Destructing local fs threadpool reader");
asynchronous_local_fs_reader->wait();
asynchronous_local_fs_reader.reset();
}
catch (...)
{
tryLogCurrentException(__PRETTY_FUNCTION__);
}
}
if (synchronous_local_fs_reader)
{
try
{
LOG_DEBUG(log, "Destructing local fs threadpool reader");
synchronous_local_fs_reader->wait();
synchronous_local_fs_reader.reset();
}
catch (...)
{
tryLogCurrentException(__PRETTY_FUNCTION__);
}
}
if (threadpool_writer)
{
try
{
LOG_DEBUG(log, "Destructing threadpool writer");
threadpool_writer->wait();
threadpool_writer.reset();
}
catch (...)
{
tryLogCurrentException(__PRETTY_FUNCTION__);
}
}
if (load_marks_threadpool)
{
try
{
LOG_DEBUG(log, "Destructing marks loader");
load_marks_threadpool->wait();
load_marks_threadpool.reset();
}
catch (...)
{
tryLogCurrentException(__PRETTY_FUNCTION__);
}
}
if (prefetch_threadpool)
{
try
{
LOG_DEBUG(log, "Destructing prefetch threadpool");
prefetch_threadpool->wait();
prefetch_threadpool.reset();
}
catch (...)
{
tryLogCurrentException(__PRETTY_FUNCTION__);
}
}
}
void setConfig(const ConfigurationPtr & config_value)
{
if (!config_value)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Set nullptr config is invalid");
std::lock_guard lock(mutex);
config = config_value;
access_control->setExternalAuthenticatorsConfig(*config_value);
}
const Poco::Util::AbstractConfiguration & getConfigRefWithLock(const std::lock_guard<ContextSharedMutex> &) const TSA_REQUIRES(this->mutex)
{
return config ? *config : Poco::Util::Application::instance().config();
}
const Poco::Util::AbstractConfiguration & getConfigRef() const
{
SharedLockGuard lock(mutex);
return config ? *config : Poco::Util::Application::instance().config();
}
/** Perform a complex job of destroying objects in advance.
*/
void shutdown() TSA_NO_THREAD_SAFETY_ANALYSIS
{
bool is_shutdown_called = shutdown_called.exchange(true);
if (is_shutdown_called)
return;
/// Need to flush the async insert queue before shutting down the database catalog
std::shared_ptr<AsynchronousInsertQueue> delete_async_insert_queue;
{
std::lock_guard lock(mutex);
delete_async_insert_queue = std::move(async_insert_queue);
}
if (delete_async_insert_queue)
delete_async_insert_queue->flushAndShutdown();
/// Stop periodic reloading of the configuration files.
/// This must be done first because otherwise the reloading may pass a changed config
/// to some destroyed parts of ContextSharedPart.
/// We need to make sure no dictionary queries are concurrent with the further shutdown logic
/// because it sets various fields to null and those fields are unconditionally dereferenced by queries.
/// So we need to:
/// (1) Disable future dictionary queries.
/// (2) Make sure all the currently running dictionary queries are killed.
/// (3) Join the dictionary queries' threads.
SHUTDOWN(log, "dictionaries loader", external_dictionaries_loader, enablePeriodicUpdates(false));
process_list.killAllQueries();
SHUTDOWN(log, "dictionaries loader threads", external_dictionaries_loader, joinLoadingThreads());
SHUTDOWN(log, "UDFs loader", external_user_defined_executable_functions_loader, enablePeriodicUpdates(false));
SHUTDOWN(log, "another UDFs storage", user_defined_sql_objects_storage, stopWatching());
LOG_TRACE(log, "Shutting down named sessions");
Session::shutdownNamedSessions();
/// Stop watching DDL queue in ZooKeeper and wait until currently executed tasks finish.
/// This must be done before closing ZooKeeper connection (because DDLWorker can call Context::getZooKeeper() and resurrect it),
/// and before shutting down BackupsWorker (because DDLWorker can start an internal backup or restore).
SHUTDOWN(log, "ddl worker", ddl_worker, shutdown());
/// Waiting for current backups/restores to be finished. This must be done before shutting down DatabaseCatalog.
SHUTDOWN(log, "backups worker", backups_worker, shutdown());
LOG_TRACE(log, "Shutting down object storage queue streaming");
StreamingStorageRegistry::instance().shutdown();
/// Stop all MergeTree background executors before shutting down databases.
/// This ensures no background tasks (merges, mutations, moves, part cleanup)
/// are running when storage objects are shut down or destroyed.
/// Without this, a background task could be accessing a storage's data_parts_indexes
/// while DatabaseCatalog::shutdown is destroying that storage, causing a SIGBUS.
/// See https://github.com/ClickHouse/ClickHouse/issues/85433
SHUTDOWN(log, "merges executor", merge_mutate_executor, wait());
SHUTDOWN(log, "fetches executor", fetch_executor, wait());
SHUTDOWN(log, "moves executor", moves_executor, wait());
SHUTDOWN(log, "common executor", common_executor, wait());
LOG_TRACE(log, "Shutting down database catalog");
DatabaseCatalog::shutdown([this]()
{
SHUTDOWN(log, "system logs", TSA_SUPPRESS_WARNING_FOR_READ(system_logs), flushAndShutdown());
});
NamedCollectionFactory::instance().shutdown();
delete_async_insert_queue.reset();
TransactionLog::shutdownIfAny();
// Workload entity storage must be destructed when no queries or merges are running because PipelineExecutor may access it.
SHUTDOWN(log, "workload entity storage", workload_entity_storage, stopWatching());
std::unique_ptr<SystemLogs> delete_system_logs;
std::unique_ptr<EmbeddedDictionaries> delete_embedded_dictionaries;
std::unique_ptr<ExternalDictionariesLoader> delete_external_dictionaries_loader;
std::unique_ptr<ExternalUserDefinedExecutableFunctionsLoader> delete_external_user_defined_executable_functions_loader;
std::unique_ptr<IUserDefinedSQLObjectsStorage> delete_user_defined_sql_objects_storage;
std::unique_ptr<IWorkloadEntityStorage> delete_workload_entity_storage;
std::unique_ptr<DDLWorker> delete_ddl_worker;
BackgroundSchedulePoolPtr delete_buffer_flush_schedule_pool;
BackgroundSchedulePoolPtr delete_schedule_pool;
BackgroundSchedulePoolPtr delete_distributed_schedule_pool;
BackgroundSchedulePoolPtr delete_message_broker_schedule_pool;
BackgroundSchedulePoolPtr delete_iceberg_schedule_pool;
std::unique_ptr<AccessControl> delete_access_control;
scope_guard delete_dictionaries_xmls;
scope_guard delete_user_defined_executable_functions_xmls;
/// Background operations in cache use background schedule pool.
/// Deactivate them before destructing it.
LOG_TRACE(log, "Shutting down caches");
for (const auto & cache_data : FileCacheFactory::instance().getUniqueInstances())
cache_data->cache->deactivateBackgroundOperations();
FileCacheFactory::instance().clear();
{
std::lock_guard lock(clusters_mutex);
if (cluster_discovery)
{
LOG_TRACE(log, "Shutting down ClusterDiscovery");
/// Reset cluster_discovery if any.
/// Some classes (such as ZooKeeper, ReplicatedAccessStorage) will finalize the keeper session while deconstructing,
/// which will trigger the callback and make ClusterDiscovery reconnect to keeper again (unnecessary).
cluster_discovery.reset();
}
}
{
// Disk selector might not be initialized if there was some error during
// its initialization. Don't try to initialize it again on shutdown.
if (merge_tree_disk_selector)
{
for (const auto & [disk_name, disk] : merge_tree_disk_selector->getDisksMap())
{
LOG_INFO(log, "Shutdown disk {}", disk_name);
disk->shutdown();
}
}
/// Special volumes might also use disks that require shutdown.
if (temporary_volume_legacy)
{
auto & disks = temporary_volume_legacy->getDisks();
for (auto & disk : disks)
disk->shutdown();
}
}
LOG_TRACE(log, "Shutting down AccessControl");