forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.cpp
More file actions
4263 lines (3753 loc) · 209 KB
/
Copy pathServer.cpp
File metadata and controls
4263 lines (3753 loc) · 209 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 <Server.h>
#include <Common/CurrentThread.h>
#include <Common/QueryScope.h>
#include <memory>
#include <Interpreters/ClientInfo.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <pwd.h>
#include <unistd.h>
#include <Poco/Net/HTTPServer.h>
#include <Poco/Net/NetException.h>
#include <Poco/Util/HelpFormatter.h>
#include <Poco/Environment.h>
#include <Poco/Config.h>
#include <Common/ErrorCodes.h>
#include <Common/scope_guard_safe.h>
#include <Common/logger_useful.h>
#include <base/phdr_cache.h>
#include <Common/ErrorHandlers.h>
#include <Processors/QueryPlan/QueryPlanStepRegistry.h>
#include <base/getMemoryAmount.h>
#include <base/getAvailableMemoryAmount.h>
#include <base/errnoToString.h>
#include <base/coverage.h>
#include <base/getFQDNOrHostName.h>
#include <base/safeExit.h>
#include <base/Numa.h>
#include <Common/PoolId.h>
#include <Common/CurrentMemoryTracker.h>
#include <Common/MemoryTracker.h>
#include <Common/PerCPUMemory.h>
#include <Common/MemoryWorker.h>
#include <Common/OOMCanary/OOMCanary.h>
#include <Common/ClickHouseRevision.h>
#include <Common/DNSResolver.h>
#include <Common/CgroupsMemoryUsageObserver.h>
#include <Common/CurrentMetrics.h>
#include <Common/DimensionalMetrics.h>
#include <Common/ISlotControl.h>
#include <Common/Macros.h>
#include <Common/ShellCommand.h>
#include <Common/ZooKeeper/ZooKeeper.h>
#include <Common/ZooKeeper/ZooKeeperNodeCache.h>
#include <Common/formatReadable.h>
#include <Common/getMultipleKeysFromConfig.h>
#include <Common/getNumberOfCPUCoresToUse.h>
#include <Common/getExecutablePath.h>
#include <Common/ProfileEvents.h>
#include <Common/Scheduler/IResourceManager.h>
#include <Common/ThreadProfileEvents.h>
#include <Common/ThreadStatus.h>
#include <Common/getMappedArea.h>
#include <Common/remapExecutable.h>
#include <Common/TLDListsHolder.h>
#include <Common/Config/AbstractConfigurationComparison.h>
#include <Common/Config/ConfigHelper.h>
#include <Common/assertProcessUserMatchesDataOwner.h>
#include <Common/makeSocketAddress.h>
#include <Common/FailPoint.h>
#include <Common/CPUID.h>
#include <Common/HTTPConnectionPool.h>
#include <Common/NamedCollections/NamedCollectionsFactory.h>
#include <Server/createServer.h>
#include <Server/socketBindListen.h>
#include <Server/stopServers.h>
#include <Server/waitServersToFinish.h>
#include <Interpreters/FileCache/FileCacheFactory.h>
#include <Core/BackgroundSchedulePool.h>
#include <Core/ServerSettings.h>
#include <Core/ServerUUID.h>
#include <Core/Settings.h>
#include <IO/ReadHelpers.h>
#include <IO/ReadBufferFromFile.h>
#include <IO/SharedThreadPools.h>
#include <IO/S3/Credentials.h>
#include <Interpreters/CancellationChecker.h>
#include <Interpreters/ServerAsynchronousMetrics.h>
#include <Interpreters/DDLWorker.h>
#include <Interpreters/DNSCacheUpdater.h>
#include <Interpreters/DatabaseCatalog.h>
#include <Interpreters/ExternalDictionariesLoader.h>
#include <Interpreters/ProcessList.h>
#include <Interpreters/executeQuery.h>
#include <Interpreters/loadMetadata.h>
#include <Interpreters/registerInterpreters.h>
#include <Interpreters/JIT/CompiledExpressionCache.h>
#include <Access/AccessControl.h>
#include <Access/ContextAccess.h>
#include <Access/User.h>
#include <Storages/MaterializedView/RefreshSet.h>
#include <Storages/MergeTree/MergeTreeBackgroundExecutor.h>
#include <Storages/MergeTree/MergeTreeSettings.h>
#include <Storages/System/attachSystemTables.h>
#include <Storages/System/attachInformationSchemaTables.h>
#include <Storages/Cache/registerRemoteFileMetadatas.h>
#include <AggregateFunctions/registerAggregateFunctions.h>
#include <Functions/UserDefined/IUserDefinedSQLObjectsStorage.h>
#include <Functions/UserDefined/UserDefinedSQLFunctionFactory.h>
#include <Functions/pointInPolygon.h>
#include <Functions/registerFunctions.h>
#include <TableFunctions/registerTableFunctions.h>
#include <Formats/registerFormats.h>
#include <Storages/registerStorages.h>
#include <Databases/registerDatabases.h>
#include <Dictionaries/registerDictionaries.h>
#include <Disks/registerDisks.h>
#include <Common/Scheduler/Nodes/registerSchedulerNodes.h>
#include <Common/Scheduler/Workload/IWorkloadEntityStorage.h>
#include <Coordination/KeeperContext.h>
#include <Common/Config/ConfigReloader.h>
#include <Server/HTTPHandlerFactory.h>
#include <Common/ReplicasReconnector.h>
#include <MetricsTransmitter.h>
#include <Common/StatusFile.h>
#include <Server/TCPHandlerFactory.h>
#include <Server/TCPServer.h>
#include <Common/SensitiveDataMasker.h>
#include <Common/ThreadFuzzer.h>
#include <Common/ThreadStackSize.h>
#include <Common/getHashOfLoadedBinary.h>
#include <Common/filesystemHelpers.h>
#include <Compression/CompressionCodecEncrypted.h>
#include <Parsers/ASTAlterQuery.h>
#include <Server/CloudPlacementInfo.h>
#include <Server/DistributedQuery/ExchangeConnections.h>
#include <Server/DistributedQuery/ExchangeServer.h>
#include <Server/HTTP/HTTPServer.h>
#include <Server/HTTP/HTTPServerConnectionFactory.h>
#include <Server/StatelessWorker/StatelessWorkerEndpoint.h>
#include <Server/MySQLHandlerFactory.h>
#include <Server/PostgreSQLHandlerFactory.h>
#include <Server/ProtocolServerAdapter.h>
#include <Server/ProxyV1HandlerFactory.h>
#include <Server/TLSHandlerFactory.h>
#include <Server/KeeperHTTPHandlerFactory.h>
#include <Server/ArrowFlight/ArrowFlightServer.h>
#include <Interpreters/AsynchronousInsertQueue.h>
#include <filesystem>
#include <unordered_set>
#include <Common/Jemalloc.h>
#include <Common/JemallocCacheArena.h>
#include "config.h"
#include <Common/config_version.h>
#if defined(OS_LINUX)
# include <cstdlib>
# include <sys/un.h>
# include <sys/mman.h>
# include <sys/ptrace.h>
# include <Common/hasLinuxCapability.h>
#endif
#if USE_SSL
# include <Poco/Net/SecureServerSocket.h>
# include <Server/CertificateReloader.h>
# include <Server/ACME/Client.h>
#endif
#if USE_SSH && defined(OS_LINUX)
# include <Server/SSH/SSHPtyHandlerFactory.h>
# include <Common/LibSSHInitializer.h>
# include <Common/LibSSHLogger.h>
#endif
#if USE_GRPC
# include <Server/GRPCServer.h>
#endif
#if USE_NURAFT
# include <Coordination/FourLetterCommand.h>
# include <Coordination/KeeperAsynchronousMetrics.h>
# include <Server/KeeperTCPHandlerFactory.h>
#endif
#if USE_AZURE_BLOB_STORAGE
# include <azure/storage/common/internal/xml_wrapper.hpp>
# include <azure/core/diagnostics/logger.hpp>
#endif
/// A minimal file used when the server is run without installation
constexpr unsigned char resource_embedded_xml[] =
{
#embed "embedded.xml"
};
namespace DB
{
namespace Setting
{
extern const SettingsSeconds http_receive_timeout;
extern const SettingsSeconds http_send_timeout;
extern const SettingsSeconds receive_timeout;
extern const SettingsSeconds send_timeout;
}
namespace MergeTreeSetting
{
extern const MergeTreeSettingsBool allow_remote_fs_zero_copy_replication;
}
namespace ServerSetting
{
extern const ServerSettingsUInt32 allow_feature_tier;
extern const ServerSettingsUInt32 asynchronous_heavy_metrics_update_period_s;
extern const ServerSettingsUInt32 asynchronous_metrics_update_period_s;
extern const ServerSettingsBool asynchronous_metrics_enable_heavy_metrics;
extern const ServerSettingsBool asynchronous_metrics_keeper_metrics_only;
extern const ServerSettingsBool async_insert_queue_flush_on_shutdown;
extern const ServerSettingsUInt64 async_insert_threads;
extern const ServerSettingsBool async_load_databases;
extern const ServerSettingsBool async_load_system_database;
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 background_move_pool_size;
extern const ServerSettingsUInt64 background_pool_size;
extern const ServerSettingsUInt64 background_schedule_pool_size;
extern const ServerSettingsUInt64 backups_io_thread_pool_queue_size;
extern const ServerSettingsDouble cache_size_to_ram_max_ratio;
extern const ServerSettingsDouble cannot_allocate_thread_fault_injection_probability;
extern const ServerSettingsUInt64 cgroups_memory_usage_observer_wait_time;
extern const ServerSettingsUInt64 compiled_expression_cache_elements_size;
extern const ServerSettingsUInt64 compiled_expression_cache_size;
extern const ServerSettingsUInt64 concurrent_threads_soft_limit_num;
extern const ServerSettingsUInt64 concurrent_threads_soft_limit_ratio_to_cores;
extern const ServerSettingsString concurrent_threads_scheduler;
extern const ServerSettingsBool concurrent_threads_lazy_allocation;
extern const ServerSettingsUInt64 config_reload_interval_ms;
extern const ServerSettingsUInt64 database_catalog_drop_table_concurrency;
extern const ServerSettingsString default_database;
extern const ServerSettingsString insert_deduplication_version;
extern const ServerSettingsBool disable_internal_dns_cache;
extern const ServerSettingsBool s3queue_disable_streaming;
extern const ServerSettingsBool message_queue_disable_insertion;
extern const ServerSettingsUInt64 disk_connections_soft_limit;
extern const ServerSettingsUInt64 disk_connections_store_limit;
extern const ServerSettingsUInt64 disk_connections_hard_limit;
extern const ServerSettingsUInt64 disk_connections_warn_limit;
extern const ServerSettingsUInt64 disk_connections_rcvbuf;
extern const ServerSettingsUInt64 disk_connections_sndbuf;
extern const ServerSettingsBool dns_allow_resolve_names_to_ipv4;
extern const ServerSettingsBool dns_allow_resolve_names_to_ipv6;
extern const ServerSettingsUInt64 dns_cache_max_entries;
extern const ServerSettingsInt32 dns_cache_update_period;
extern const ServerSettingsUInt32 dns_max_consecutive_failures;
extern const ServerSettingsBool enable_azure_sdk_logging;
extern const ServerSettingsUInt64 global_profiler_cpu_time_period_ns;
extern const ServerSettingsUInt64 global_profiler_real_time_period_ns;
extern const ServerSettingsUInt64 http_connections_soft_limit;
extern const ServerSettingsUInt64 http_connections_store_limit;
extern const ServerSettingsUInt64 http_connections_hard_limit;
extern const ServerSettingsUInt64 http_connections_warn_limit;
extern const ServerSettingsUInt64 http_connections_rcvbuf;
extern const ServerSettingsUInt64 http_connections_sndbuf;
extern const ServerSettingsString index_mark_cache_policy;
extern const ServerSettingsUInt64 index_mark_cache_size;
extern const ServerSettingsDouble index_mark_cache_size_ratio;
extern const ServerSettingsString vector_similarity_index_cache_policy;
extern const ServerSettingsUInt64 vector_similarity_index_cache_size;
extern const ServerSettingsUInt64 vector_similarity_index_cache_max_entries;
extern const ServerSettingsDouble vector_similarity_index_cache_size_ratio;
extern const ServerSettingsString text_index_tokens_cache_policy;
extern const ServerSettingsUInt64 text_index_tokens_cache_size;
extern const ServerSettingsUInt64 text_index_tokens_cache_max_entries;
extern const ServerSettingsDouble text_index_tokens_cache_size_ratio;
extern const ServerSettingsString text_index_header_cache_policy;
extern const ServerSettingsUInt64 text_index_header_cache_size;
extern const ServerSettingsUInt64 text_index_header_cache_max_entries;
extern const ServerSettingsDouble text_index_header_cache_size_ratio;
extern const ServerSettingsString text_index_postings_cache_policy;
extern const ServerSettingsUInt64 text_index_postings_cache_size;
extern const ServerSettingsUInt64 text_index_postings_cache_max_entries;
extern const ServerSettingsDouble text_index_postings_cache_size_ratio;
extern const ServerSettingsString index_uncompressed_cache_policy;
extern const ServerSettingsUInt64 index_uncompressed_cache_size;
extern const ServerSettingsDouble index_uncompressed_cache_size_ratio;
extern const ServerSettingsString iceberg_metadata_files_cache_policy;
extern const ServerSettingsUInt64 iceberg_metadata_files_cache_size;
extern const ServerSettingsUInt64 iceberg_metadata_files_cache_max_entries;
extern const ServerSettingsDouble iceberg_metadata_files_cache_size_ratio;
extern const ServerSettingsString parquet_metadata_cache_policy;
extern const ServerSettingsUInt64 parquet_metadata_cache_size;
extern const ServerSettingsUInt64 parquet_metadata_cache_max_entries;
extern const ServerSettingsDouble parquet_metadata_cache_size_ratio;
extern const ServerSettingsUInt64 io_thread_pool_queue_size;
extern const ServerSettingsBool jemalloc_enable_global_profiler;
extern const ServerSettingsBool jemalloc_collect_global_profile_samples_in_trace_log;
extern const ServerSettingsBool jemalloc_enable_background_threads;
extern const ServerSettingsUInt64 jemalloc_max_background_threads_num;
extern const ServerSettingsUInt64 jemalloc_profiler_sampling_rate;
extern const ServerSettingsSeconds keep_alive_timeout;
extern const ServerSettingsString mark_cache_policy;
extern const ServerSettingsUInt64 mark_cache_size;
extern const ServerSettingsDouble mark_cache_size_ratio;
extern const ServerSettingsString unique_key_index_cache_policy;
extern const ServerSettingsUInt64 unique_key_index_cache_size_bytes;
extern const ServerSettingsDouble unique_key_index_cache_size_ratio;
extern const ServerSettingsString unique_key_bitmap_cache_policy;
extern const ServerSettingsUInt64 unique_key_bitmap_cache_size_bytes;
extern const ServerSettingsDouble unique_key_bitmap_cache_size_ratio;
extern const ServerSettingsUInt64 max_fetch_partition_thread_pool_size;
extern const ServerSettingsUInt64 max_active_parts_loading_thread_pool_size;
extern const ServerSettingsUInt64 max_backups_io_thread_pool_free_size;
extern const ServerSettingsUInt64 max_backups_io_thread_pool_size;
extern const ServerSettingsUInt64 max_concurrent_insert_queries;
extern const ServerSettingsUInt64 max_concurrent_queries;
extern const ServerSettingsUInt64 max_concurrent_select_queries;
extern const ServerSettingsInt32 max_connections;
extern const ServerSettingsUInt64 max_database_num_to_warn;
extern const ServerSettingsUInt32 max_database_replicated_create_table_thread_pool_size;
extern const ServerSettingsUInt64 max_dictionary_num_to_warn;
extern const ServerSettingsUInt64 max_io_thread_pool_free_size;
extern const ServerSettingsUInt64 max_io_thread_pool_size;
extern const ServerSettingsUInt64 max_keep_alive_requests;
extern const ServerSettingsUInt64 max_outdated_parts_loading_thread_pool_size;
extern const ServerSettingsUInt64 max_per_cpu_untracked_memory;
extern const ServerSettingsUInt64 max_partition_size_to_drop;
extern const ServerSettingsUInt64 max_part_num_to_warn;
extern const ServerSettingsUInt64 max_pending_mutations_to_warn;
extern const ServerSettingsUInt64 max_pending_mutations_execution_time_to_warn;
extern const ServerSettingsUInt64 max_parts_cleaning_thread_pool_size;
extern const ServerSettingsUInt64 max_named_collection_num_to_warn;
extern const ServerSettingsUInt64 max_named_collection_num_to_throw;
extern const ServerSettingsUInt64 max_table_num_to_throw;
extern const ServerSettingsUInt64 max_replicated_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_remote_read_network_bandwidth_for_server;
extern const ServerSettingsUInt64 max_remote_write_network_bandwidth_for_server;
extern const ServerSettingsUInt64 max_remote_read_connections;
extern const ServerSettingsUInt64 max_local_read_bandwidth_for_server;
extern const ServerSettingsUInt64 max_local_write_bandwidth_for_server;
extern const ServerSettingsUInt64 max_server_memory_usage;
extern const ServerSettingsDouble max_server_memory_usage_to_ram_ratio;
extern const ServerSettingsUInt64 max_table_num_to_warn;
extern const ServerSettingsUInt64 max_table_size_to_drop;
extern const ServerSettingsUInt64 max_temporary_data_on_disk_size;
extern const ServerSettingsUInt64 max_thread_pool_free_size;
extern const ServerSettingsUInt64 max_thread_pool_size;
extern const ServerSettingsUInt64 max_unexpected_parts_loading_thread_pool_size;
extern const ServerSettingsUInt64 max_view_num_to_warn;
extern const ServerSettingsUInt64 max_waiting_queries;
extern const ServerSettingsUInt64 memory_worker_period_ms;
extern const ServerSettingsDouble memory_worker_purge_dirty_pages_threshold_ratio;
extern const ServerSettingsDouble memory_worker_purge_total_memory_threshold_ratio;
extern const ServerSettingsUInt64 memory_worker_decay_adjustment_period_ms;
extern const ServerSettingsBool memory_worker_correct_memory_tracker;
extern const ServerSettingsBool memory_worker_use_cgroup;
extern const ServerSettingsDouble memory_worker_rss_speculative_reserve_ratio;
extern const ServerSettingsBool memory_worker_dynamic_hard_limit;
extern const ServerSettingsUInt64 merges_mutations_memory_usage_soft_limit;
extern const ServerSettingsDouble merges_mutations_memory_usage_to_ram_ratio;
extern const ServerSettingsString merge_workload;
extern const ServerSettingsUInt64 min_allocation_size_to_throw_on_memory_limit;
extern const ServerSettingsUInt64 mmap_cache_size;
extern const ServerSettingsString mutation_workload;
extern const ServerSettingsString query_condition_cache_policy;
extern const ServerSettingsUInt64 query_condition_cache_size;
extern const ServerSettingsDouble query_condition_cache_size_ratio;
extern const ServerSettingsBool prepare_system_log_tables_on_startup;
extern const ServerSettingsBool user_profile_events_per_cpu;
extern const ServerSettingsBool show_addresses_in_stack_traces;
extern const ServerSettingsBool shutdown_wait_backups_and_restores;
extern const ServerSettingsUInt64 shutdown_wait_unfinished;
extern const ServerSettingsBool shutdown_wait_unfinished_queries;
extern const ServerSettingsUInt64 storage_connections_soft_limit;
extern const ServerSettingsUInt64 storage_connections_store_limit;
extern const ServerSettingsUInt64 storage_connections_hard_limit;
extern const ServerSettingsUInt64 storage_connections_warn_limit;
extern const ServerSettingsUInt64 storage_connections_rcvbuf;
extern const ServerSettingsUInt64 storage_connections_sndbuf;
extern const ServerSettingsUInt64 tables_loader_background_pool_size;
extern const ServerSettingsUInt64 tables_loader_foreground_pool_size;
extern const ServerSettingsString temporary_data_in_cache;
extern const ServerSettingsUInt64 thread_pool_queue_size;
extern const ServerSettingsString tmp_policy;
extern const ServerSettingsUInt64 total_memory_profiler_sample_max_allocation_size;
extern const ServerSettingsUInt64 total_memory_profiler_sample_min_allocation_size;
extern const ServerSettingsUInt64 total_memory_profiler_step;
extern const ServerSettingsDouble total_memory_tracker_sample_probability;
extern const ServerSettingsBool throw_on_unknown_workload;
extern const ServerSettingsBool cpu_slot_preemption;
extern const ServerSettingsUInt64 cpu_slot_quantum_ns;
extern const ServerSettingsUInt64 cpu_slot_preemption_timeout_ms;
extern const ServerSettingsString uncompressed_cache_policy;
extern const ServerSettingsUInt64 uncompressed_cache_size;
extern const ServerSettingsDouble uncompressed_cache_size_ratio;
extern const ServerSettingsUInt64 per_cpu_untracked_memory_thread_buffer;
extern const ServerSettingsBool use_separate_cache_arena;
extern const ServerSettingsString primary_index_cache_policy;
extern const ServerSettingsUInt64 primary_index_cache_size;
extern const ServerSettingsDouble primary_index_cache_size_ratio;
extern const ServerSettingsUInt64 point_in_polygon_cache_size;
extern const ServerSettingsBool dictionaries_lazy_load;
extern const ServerSettingsBool wait_dictionaries_load_at_startup;
extern const ServerSettingsUInt64 max_prefixes_deserialization_thread_pool_size;
extern const ServerSettingsUInt64 max_prefixes_deserialization_thread_pool_free_size;
extern const ServerSettingsUInt64 prefixes_deserialization_thread_pool_thread_pool_queue_size;
extern const ServerSettingsUInt64 max_format_parsing_thread_pool_size;
extern const ServerSettingsUInt64 max_format_parsing_thread_pool_free_size;
extern const ServerSettingsUInt64 format_parsing_thread_pool_queue_size;
extern const ServerSettingsUInt64 page_cache_history_window_ms;
extern const ServerSettingsString page_cache_policy;
extern const ServerSettingsDouble page_cache_size_ratio;
extern const ServerSettingsUInt64 page_cache_min_size;
extern const ServerSettingsUInt64 page_cache_max_size;
extern const ServerSettingsDouble page_cache_free_memory_ratio;
extern const ServerSettingsUInt64 page_cache_shards;
extern const ServerSettingsUInt64 os_cpu_busy_time_threshold;
extern const ServerSettingsFloat min_os_cpu_wait_time_ratio_to_drop_connection;
extern const ServerSettingsFloat max_os_cpu_wait_time_ratio_to_drop_connection;
extern const ServerSettingsBool skip_binary_checksum_checks;
extern const ServerSettingsBool abort_on_logical_error;
extern const ServerSettingsUInt64 jemalloc_flush_profile_interval_bytes;
extern const ServerSettingsBool jemalloc_flush_profile_on_memory_exceeded;
extern const ServerSettingsUInt64 jemalloc_flush_profile_on_memory_exceeded_interval;
extern const ServerSettingsString allowed_disks_for_table_engines;
extern const ServerSettingsUInt64 s3_credentials_provider_max_cache_size;
extern const ServerSettingsUInt64 max_open_files;
extern const ServerSettingsString path;
extern const ServerSettingsString user_files_path;
extern const ServerSettingsString dictionaries_lib_path;
extern const ServerSettingsString user_scripts_path;
extern const ServerSettingsString dynamic_user_defined_executable_functions_path;
extern const ServerSettingsString top_level_domains_path;
extern const ServerSettingsString interserver_http_host;
extern const ServerSettingsUInt64 interserver_http_port;
extern const ServerSettingsString interserver_https_host;
extern const ServerSettingsUInt64 interserver_https_port;
extern const ServerSettingsString include_from;
extern const ServerSettingsString tmp_path;
extern const ServerSettingsString format_schema_path;
extern const ServerSettingsString google_protos_path;
extern const ServerSettingsString filesystem_caches_path;
extern const ServerSettingsInt32 oom_score;
extern const ServerSettingsBool oom_canary_enable;
extern const ServerSettingsUInt64 oom_canary_size;
extern const ServerSettingsBool oom_canary_relaunch;
extern const ServerSettingsUInt64 oom_canary_max_rapid_relaunches;
extern const ServerSettingsUInt64 oom_canary_initial_backoff_seconds;
extern const ServerSettingsUInt64 oom_canary_max_backoff_seconds;
extern const ServerSettingsBool remap_executable;
extern const ServerSettingsBool mlock_executable;
extern const ServerSettingsUInt64 mlock_executable_min_total_memory_amount_bytes;
extern const ServerSettingsUInt32 listen_backlog;
extern const ServerSettingsBool listen_reuse_port;
extern const ServerSettingsBool listen_try;
extern const ServerSettingsBool mysql_require_secure_transport;
extern const ServerSettingsBool postgresql_require_secure_transport;
extern const ServerSettingsBool skip_check_for_incorrect_settings;
extern const ServerSettingsUInt64 query_cache_max_size_in_bytes;
extern const ServerSettingsUInt64 query_cache_max_entries;
extern const ServerSettingsUInt64 query_cache_max_entry_size_in_bytes;
extern const ServerSettingsUInt64 query_cache_max_entry_size_in_rows;
extern const ServerSettingsString logger_log;
extern const ServerSettingsString logger_level;
extern const ServerSettingsString logger_startup_level;
extern const ServerSettingsString logger_shutdown_level;
extern const ServerSettingsString openssl_server_certificate_file;
extern const ServerSettingsString openssl_server_private_key_file;
extern const ServerSettingsString distributed_ddl_path;
extern const ServerSettingsString distributed_ddl_replicas_path;
extern const ServerSettingsInt32 distributed_ddl_pool_size;
extern const ServerSettingsBool prometheus_keeper_metrics_only;
extern const ServerSettingsBool startup_scripts_throw_on_error;
extern const ServerSettingsUInt64 keeper_server_socket_receive_timeout_sec;
extern const ServerSettingsUInt64 keeper_server_socket_send_timeout_sec;
extern const ServerSettingsString hdfs_libhdfs3_conf;
extern const ServerSettingsString config_file;
extern const ServerSettingsString users_to_ignore_early_memory_limit_check;
}
namespace ErrorCodes
{
extern const int STARTUP_SCRIPTS_ERROR;
}
namespace FileCacheSetting
{
extern const FileCacheSettingsBool load_metadata_asynchronously;
}
}
namespace CurrentMetrics
{
extern const Metric Revision;
extern const Metric VersionInteger;
extern const Metric MemoryTracking;
extern const Metric MergesMutationsMemoryTracking;
extern const Metric MaxDDLEntryID;
extern const Metric MaxPushedDDLEntryID;
extern const Metric StartupScriptsExecutionState;
extern const Metric IsServerShuttingDown;
}
namespace DimensionalMetrics
{
extern MetricFamily & StartupScriptsFailureReason;
}
namespace ProfileEvents
{
extern const Event MainConfigLoads;
extern const Event ServerStartupMilliseconds;
extern const Event InterfaceNativeSendBytes;
extern const Event InterfaceNativeReceiveBytes;
extern const Event InterfaceHTTPSendBytes;
extern const Event InterfaceHTTPReceiveBytes;
extern const Event InterfacePrometheusSendBytes;
extern const Event InterfacePrometheusReceiveBytes;
extern const Event InterfaceInterserverSendBytes;
extern const Event InterfaceInterserverReceiveBytes;
extern const Event InterfaceMySQLSendBytes;
extern const Event InterfaceMySQLReceiveBytes;
extern const Event InterfacePostgreSQLSendBytes;
extern const Event InterfacePostgreSQLReceiveBytes;
}
namespace fs = std::filesystem;
int mainEntryClickHouseServer(int argc, char ** argv);
int mainEntryClickHouseServer(int argc, char ** argv)
{
DB::Server app;
/// Do not fork separate process from watchdog if we attached to terminal.
/// Otherwise it breaks gdb usage.
/// Can be overridden by environment variable (cannot use server config at this moment).
if (argc > 0)
{
const char * env_watchdog = getenv("CLICKHOUSE_WATCHDOG_ENABLE"); // NOLINT(concurrency-mt-unsafe)
if (env_watchdog)
{
if (0 == strcmp(env_watchdog, "1"))
app.shouldSetupWatchdog(argv[0]);
/// Other values disable watchdog explicitly.
}
else if (!isatty(STDIN_FILENO) && !isatty(STDOUT_FILENO) && !isatty(STDERR_FILENO))
app.shouldSetupWatchdog(argv[0]);
}
try
{
return app.run(argc, argv);
}
catch (...)
{
std::cerr << DB::getCurrentExceptionMessage(true) << "\n";
auto code = DB::getCurrentExceptionCode();
return static_cast<UInt8>(code) ? code : 1;
}
}
namespace DB
{
namespace ErrorCodes
{
extern const int NO_ELEMENTS_IN_CONFIG;
extern const int SUPPORT_IS_DISABLED;
extern const int ARGUMENT_OUT_OF_BOUND;
extern const int EXCESSIVE_ELEMENT_IN_CONFIG;
extern const int INVALID_CONFIG_PARAMETER;
extern const int INVALID_SETTING_VALUE;
extern const int NETWORK_ERROR;
extern const int CORRUPTED_DATA;
extern const int BAD_ARGUMENTS;
}
enum StartupScriptsExecutionState : CurrentMetrics::Value
{
NotFinished = 0,
Success = 1,
Failure = 2,
};
namespace
{
std::string getCanonicalPath(std::string && path, const std::string & base = {})
{
Poco::trimInPlace(path);
if (path.empty())
throw Exception(ErrorCodes::INVALID_CONFIG_PARAMETER, "path configuration parameter is empty");
if (!base.empty() && fs::path(path).is_relative())
path = fs::weakly_canonical(fs::path(base) / path);
if (path.back() != '/')
path += '/';
return std::move(path);
}
Poco::Net::TCPServerParams::Ptr makeServerParams(const ServerSettings & server_settings)
{
Poco::Net::TCPServerParams::Ptr params = new Poco::Net::TCPServerParams();
params->setMaxQueued(server_settings[ServerSetting::listen_backlog]);
return params;
}
}
Poco::Net::SocketAddress Server::socketBindListen(
const ServerSettings & server_settings,
Poco::Net::ServerSocket & socket,
const std::string & host,
UInt16 port,
[[maybe_unused]] bool secure) const
{
return DB::socketBindListen(server_settings, socket, host, port, &logger());
}
namespace
{
Strings getListenHosts(const Poco::Util::AbstractConfiguration & config)
{
auto listen_hosts = DB::getMultipleValuesFromConfig(config, "", "listen_host");
if (listen_hosts.empty())
{
listen_hosts.emplace_back("::1");
listen_hosts.emplace_back("127.0.0.1");
}
return listen_hosts;
}
Strings getInterserverListenHosts(const Poco::Util::AbstractConfiguration & config)
{
auto interserver_listen_hosts = DB::getMultipleValuesFromConfig(config, "", "interserver_listen_host");
if (!interserver_listen_hosts.empty())
return interserver_listen_hosts;
/// Use more general restriction in case of emptiness
return getListenHosts(config);
}
bool getListenTry(const Poco::Util::AbstractConfiguration & config, const ServerSettings & server_settings)
{
bool listen_try = server_settings[ServerSetting::listen_try];
if (!listen_try)
{
Poco::Util::AbstractConfiguration::Keys protocols;
config.keys("protocols", protocols);
listen_try =
DB::getMultipleValuesFromConfig(config, "", "listen_host").empty() &&
std::none_of(protocols.begin(), protocols.end(), [&](const auto & protocol)
{
return config.has("protocols." + protocol + ".host") && config.has("protocols." + protocol + ".port");
});
}
return listen_try;
}
}
void Server::createServer(
Poco::Util::AbstractConfiguration & config,
const std::string & listen_host,
const char * port_name,
bool listen_try,
bool start_server,
std::vector<ProtocolServerAdapter> & servers,
CreateServerFunc && func) const
{
if (DB::createServer(config, listen_host, port_name, listen_try, start_server, servers, std::move(func), &logger()))
{
/// Register the configured port rather than the actual bound port. `getServerPort` keeps a
/// single value per `port_name`, so with `tcp_port=0` (OS-assigned) and several `listen_host`
/// values (e.g. the default `::1` + `127.0.0.1`) each host binds a distinct ephemeral port and
/// registering the actual port would let the last host overwrite the others, leaving
/// `getServerPort` pointing at a port that is not listening on the host a client uses. When the
/// configured port is non-zero it equals the bound port anyway, so this preserves the previous
/// behavior in all cases. (`clickhouse-local` registers the actual bound port because it needs
/// the OS-assigned value, but it rejects the ambiguous `port=0` + multiple `listen_host` combo.)
global_context->registerServerPort(port_name, static_cast<UInt16>(config.getInt(port_name)));
}
}
#if defined(OS_LINUX)
namespace
{
void setOOMScore(int value, LoggerRawPtr log)
{
try
{
std::string value_string = std::to_string(value);
DB::WriteBufferFromFile buf("/proc/self/oom_score_adj");
buf.write(value_string.c_str(), value_string.size());
buf.next();
buf.close();
}
catch (const Poco::Exception & e)
{
LOG_WARNING(log, "Failed to adjust OOM score: '{}'.", e.displayText());
return;
}
LOG_INFO(log, "Set OOM score adjustment to {}", value);
}
}
#endif
void Server::uninitialize()
{
logger().information("shutting down");
BaseDaemon::uninitialize();
}
int Server::run()
{
if (config().hasOption("help"))
{
Poco::Util::HelpFormatter help_formatter(Server::options());
std::string app_name = (commandName() == "clickhouse-server") ? "clickhouse-server" : "clickhouse server";
auto header_str = fmt::format("{} [OPTION] [-- [ARG]...]\n"
"positional arguments can be used to rewrite config.xml properties, for example, --http_port=8010",
app_name);
help_formatter.setHeader(header_str);
help_formatter.format(std::cout);
return 0;
}
if (config().hasOption("version"))
{
std::cout << VERSION_NAME << " server version " << VERSION_STRING << VERSION_OFFICIAL << "." << std::endl;
return 0;
}
return Application::run(); // NOLINT
}
void Server::initialize(Poco::Util::Application & self)
{
ConfigProcessor::registerEmbeddedConfig("config.xml", std::string_view(reinterpret_cast<const char *>(resource_embedded_xml), std::size(resource_embedded_xml)));
BaseDaemon::initialize(self);
logger().information("starting up");
LOG_INFO(&logger(), "OS name: {}, version: {}, architecture: {}",
Poco::Environment::osName(),
Poco::Environment::osVersion(),
Poco::Environment::osArchitecture());
}
std::string Server::getDefaultCorePath() const
{
return getCanonicalPath(config().getString("path", DBMS_DEFAULT_PATH), original_working_directory) + "cores";
}
void Server::defineOptions(Poco::Util::OptionSet & options)
{
options.addOption(
Poco::Util::Option("help", "h", "show help and exit")
.required(false)
.repeatable(false)
.binding("help"));
options.addOption(
Poco::Util::Option("version", "V", "show version and exit")
.required(false)
.repeatable(false)
.binding("version"));
BaseDaemon::defineOptions(options);
}
[[maybe_unused]] static void checkForUsersNotInMainConfig(
const Poco::Util::AbstractConfiguration & config,
const ServerSettings & server_settings,
const std::string & config_path,
const std::string & users_config_path,
LoggerPtr log)
{
if (server_settings[ServerSetting::skip_check_for_incorrect_settings])
return;
if (config.has("users") || config.has("profiles") || config.has("quotas"))
{
/// We cannot throw exception here, because we have support for obsolete 'conf.d' directory
/// (that does not correspond to config.d or users.d) but substitute configuration to both of them.
LOG_ERROR(log, "The <users>, <profiles> and <quotas> elements should be located in users config file: {} not in main config {}."
" Also note that you should place configuration changes to the appropriate *.d directory like 'users.d'.",
users_config_path, config_path);
}
}
namespace
{
/// Unused in other builds
#if defined(OS_LINUX)
String readLine(const String & path)
{
ReadBufferFromFile in(path);
String contents;
readStringUntilNewlineInto(contents, in);
return contents;
}
int readNumber(const String & path)
{
ReadBufferFromFile in(path);
int result = {};
readText(result, in);
return result;
}
#endif
void sanityChecks(Server & server, const ServerSettings & server_settings)
{
std::string data_path = getCanonicalPath(String(server_settings[ServerSetting::path]), server.getOriginalWorkingDirectory());
std::string logs_path = server_settings[ServerSetting::logger_log];
if (server.logger().is(Poco::Message::PRIO_TEST))
server.context()->addOrUpdateWarningMessage(
Context::WarningType::SERVER_LOGGING_LEVEL_TEST,
PreformattedMessage::create(
"Server logging level is set to 'test' and performance is degraded. This cannot be used in production."));
#if defined(OS_LINUX)
try
{
const std::unordered_set<std::string> fast_clock_sources = {
// ARM clock
"arch_sys_counter",
// KVM guest clock
"kvm-clock",
// X86 clock
"tsc",
};
const char * filename = "/sys/devices/system/clocksource/clocksource0/current_clocksource";
if (!fast_clock_sources.contains(readLine(filename)))
server.context()->addOrUpdateWarningMessage(
Context::WarningType::LINUX_FAST_CLOCK_SOURCE_NOT_USED,
PreformattedMessage::create("Linux is not using a fast clock source. Performance can be degraded. Check {}", filename));
}
catch (const std::exception &) // NOLINT(bugprone-empty-catch)
{
}
try
{
const char * filename = "/proc/sys/vm/overcommit_memory";
if (readNumber(filename) == 2)
server.context()->addOrUpdateWarningMessage(
Context::WarningType::LINUX_MEMORY_OVERCOMMIT_DISABLED,
PreformattedMessage::create("Linux memory overcommit is disabled. Check {}", String(filename)));
}
catch (const std::exception &) // NOLINT(bugprone-empty-catch)
{
}
try
{
const char * filename = "/sys/kernel/mm/transparent_hugepage/enabled";
if (readLine(filename).find("[always]") != std::string::npos)
server.context()->addOrUpdateWarningMessage(
Context::WarningType::LINUX_TRANSPARENT_HUGEPAGES_SET_TO_ALWAYS,
PreformattedMessage::create("Linux transparent hugepages are set to \"always\". Check {}", String(filename)));
}
catch (const std::exception &) // NOLINT(bugprone-empty-catch)
{
}
try
{
const char * filename = "/proc/sys/kernel/pid_max";
if (readNumber(filename) < 30000)
server.context()->addOrUpdateWarningMessage(
Context::WarningType::LINUX_MAX_PID_TOO_LOW,
PreformattedMessage::create("Linux max PID is too low. Check {}", String(filename)));
}
catch (const std::exception &) // NOLINT(bugprone-empty-catch)
{
}
try
{
const char * filename = "/proc/sys/kernel/threads-max";
if (readNumber(filename) < 30000)
server.context()->addOrUpdateWarningMessage(
Context::WarningType::LINUX_MAX_THREADS_COUNT_TOO_LOW,
PreformattedMessage::create("Linux threads max count is too low. Check {}", String(filename)));
}
catch (const std::exception &) // NOLINT(bugprone-empty-catch)
{
}
try
{
const char * filename = "/proc/sys/kernel/task_delayacct";
if (readNumber(filename) == 0)
server.context()->addOrUpdateWarningMessage(
Context::WarningType::DELAY_ACCOUNTING_DISABLED,
PreformattedMessage::create(
"Delay accounting is not enabled, OSIOWaitMicroseconds will not be gathered. You can enable it "
"using `sudo sh -c 'echo 1 > {}'` or by using sysctl.",
String(filename)));
}
catch (const std::exception &) // NOLINT(bugprone-empty-catch)
{
}
std::string dev_id = getBlockDeviceId(data_path);
if (getBlockDeviceType(dev_id) == BlockDeviceType::ROT && getBlockDeviceReadAheadBytes(dev_id) == 0)
server.context()->addOrUpdateWarningMessage(
Context::WarningType::ROTATIONAL_DISK_WITH_DISABLED_READHEAD,
PreformattedMessage::create(
"Rotational disk with disabled readahead is in use. Performance can be degraded. Used for data: {}", String(data_path)));
try
{
/// Check if any mdraid arrays are currently being checked, repaired, or degraded.
/// Resynchronization can significantly degrade disk I/O performance.
/// A degraded array means one or more disks are missing or faulty.
fs::path sys_block("/sys/block");
if (fs::exists(sys_block))
{
std::optional<PreformattedMessage> resync_warning;
std::optional<PreformattedMessage> degraded_warning;
for (const auto & entry : fs::directory_iterator(sys_block))
{
const auto name = entry.path().filename().string();
if (!name.starts_with("md"))
continue;
auto sync_action_path = entry.path() / "md" / "sync_action";
if (fs::exists(sync_action_path))
{
String sync_action = readLine(sync_action_path.string());
if (sync_action != "idle")
{
resync_warning = PreformattedMessage::create(
"Linux mdraid array {} is currently performing `{}`. Disk I/O performance can be degraded. Check {}",
name, sync_action, sync_action_path.string());
}
}
auto array_state_path = entry.path() / "md" / "array_state";
if (fs::exists(array_state_path))
{
static const std::unordered_set<String> normal_states = {"active", "active-idle", "clean", "write-pending", "readonly", "read-auto"};
String array_state = readLine(array_state_path.string());
if (!normal_states.contains(array_state))
{
degraded_warning = PreformattedMessage::create(
"Linux mdraid array {} has state `{}`. Check {}",
name, array_state, array_state_path.string());
}
}
if (resync_warning && degraded_warning)
break;
}
server.context()->addOrUpdateWarningMessage(
Context::WarningType::LINUX_MDRAID_IS_BEING_RESYNCHRONIZED, resync_warning);
server.context()->addOrUpdateWarningMessage(
Context::WarningType::LINUX_MDRAID_IS_DEGRADED, degraded_warning);
}
}
catch (const std::exception &) // NOLINT(bugprone-empty-catch)
{
}
#endif
try
{
if (getAvailableMemoryAmount() < (2l << 30))
server.context()->addOrUpdateWarningMessage(
Context::WarningType::AVAILABLE_MEMORY_TOO_LOW,
PreformattedMessage::create("Available memory at server startup is too low (2GiB)."));
}
catch (const std::exception &) // NOLINT(bugprone-empty-catch)
{
}
try
{
if (!enoughSpaceInDirectory(data_path, 1ull << 30))
server.context()->addOrUpdateWarningMessage(
Context::WarningType::AVAILABLE_DISK_SPACE_TOO_LOW_FOR_DATA,
PreformattedMessage::create("Available disk space for data at server startup is too low (1GiB): {}", String(data_path)));
}
catch (const std::exception &) // NOLINT(bugprone-empty-catch)