-
Notifications
You must be signed in to change notification settings - Fork 411
/
Copy pathServer.cpp
1508 lines (1351 loc) · 61.1 KB
/
Server.cpp
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
// Copyright 2023 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <AggregateFunctions/registerAggregateFunctions.h>
#include <Common/CPUAffinityManager.h>
#include <Common/Config/ConfigReloader.h>
#include <Common/CurrentMetrics.h>
#include <Common/DynamicThreadPool.h>
#include <Common/Exception.h>
#include <Common/FailPoint.h>
#include <Common/RedactHelpers.h>
#include <Common/StringUtils/StringUtils.h>
#include <Common/ThreadManager.h>
#include <Common/TiFlashBuildInfo.h>
#include <Common/TiFlashException.h>
#include <Common/TiFlashMetrics.h>
#include <Common/UniThreadPool.h>
#include <Common/assert_cast.h>
#include <Common/config.h>
#include <Common/escapeForFileName.h>
#include <Common/formatReadable.h>
#include <Common/getFQDNOrHostName.h>
#include <Common/getMultipleKeysFromConfig.h>
#include <Common/getNumberOfCPUCores.h>
#include <Common/grpcpp.h>
#include <Common/setThreadName.h>
#include <Core/TiFlashDisaggregatedMode.h>
#include <Flash/DiagnosticsService.h>
#include <Flash/FlashService.h>
#include <Flash/Mpp/GRPCCompletionQueuePool.h>
#include <Flash/Pipeline/Schedule/TaskScheduler.h>
#include <Flash/ResourceControl/LocalAdmissionController.h>
#include <Functions/registerFunctions.h>
#include <IO/BaseFile/RateLimiter.h>
#include <IO/Encryption/DataKeyManager.h>
#include <IO/Encryption/KeyspacesKeyManager.h>
#include <IO/Encryption/MockKeyManager.h>
#include <IO/FileProvider/FileProvider.h>
#include <IO/HTTPCommon.h>
#include <IO/IOThreadPools.h>
#include <IO/ReadHelpers.h>
#include <IO/UseSSL.h>
#include <Interpreters/AsynchronousMetrics.h>
#include <Interpreters/Context.h>
#include <Interpreters/ProcessList.h>
#include <Interpreters/SharedContexts/Disagg.h>
#include <Interpreters/loadMetadata.h>
#include <Poco/DirectoryIterator.h>
#include <Poco/StringTokenizer.h>
#include <Poco/Timestamp.h>
#include <Poco/Util/HelpFormatter.h>
#include <Poco/Util/LayeredConfiguration.h>
#include <Server/BgStorageInit.h>
#include <Server/Bootstrap.h>
#include <Server/CertificateReloader.h>
#include <Server/MetricsPrometheus.h>
#include <Server/RaftConfigParser.h>
#include <Server/Server.h>
#include <Server/ServerInfo.h>
#include <Server/Setup.h>
#include <Server/StatusFile.h>
#include <Server/StorageConfigParser.h>
#include <Server/TCPServersHolder.h>
#include <Server/UserConfigParser.h>
#include <Storages/DeltaMerge/ColumnFile/ColumnFileSchema.h>
#include <Storages/DeltaMerge/ReadThread/DMFileReaderPool.h>
#include <Storages/DeltaMerge/ReadThread/SegmentReadTaskScheduler.h>
#include <Storages/DeltaMerge/ReadThread/SegmentReader.h>
#include <Storages/DeltaMerge/ScanContext.h>
#include <Storages/FormatVersion.h>
#include <Storages/IManageableStorage.h>
#include <Storages/KVStore/FFI/FileEncryption.h>
#include <Storages/KVStore/FFI/ProxyFFI.h>
#include <Storages/KVStore/KVStore.h>
#include <Storages/KVStore/TMTContext.h>
#include <Storages/KVStore/TiKVHelpers/PDTiKVClient.h>
#include <Storages/Page/V3/Universal/UniversalPageStorage.h>
#include <Storages/PathCapacityMetrics.h>
#include <Storages/S3/FileCache.h>
#include <Storages/S3/S3Common.h>
#include <Storages/System/attachSystemTables.h>
#include <Storages/registerStorages.h>
#include <TableFunctions/registerTableFunctions.h>
#include <TiDB/Schema/SchemaSyncer.h>
#include <TiDB/Schema/TiDBSchemaManager.h>
#include <WindowFunctions/registerWindowFunctions.h>
#include <boost_wrapper/string_split.h>
#include <common/ErrorHandlers.h>
#include <common/config_common.h>
#include <common/logger_useful.h>
#include <sys/resource.h>
#include <boost/algorithm/string/classification.hpp>
#include <ext/scope_guard.h>
#include <magic_enum.hpp>
#include <memory>
#include <thread>
#ifdef FIU_ENABLE
#include <fiu.h>
#endif
namespace CurrentMetrics
{
extern const Metric LogicalCPUCores;
extern const Metric MemoryCapacity;
} // namespace CurrentMetrics
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 INVALID_CONFIG_PARAMETER;
} // namespace ErrorCodes
namespace Debug
{
extern void setServiceAddr(const std::string & addr);
}
static std::string getCanonicalPath(std::string path)
{
Poco::trimInPlace(path);
if (path.empty())
throw Exception("path configuration parameter is empty");
if (path.back() != '/')
path += '/';
return path;
}
void Server::uninitialize()
{
logger().information("shutting down");
BaseDaemon::uninitialize();
}
void Server::initialize(Poco::Util::Application & self)
{
BaseDaemon::initialize(self);
logger().information("starting up");
}
void Server::defineOptions(Poco::Util::OptionSet & options)
{
options.addOption(
Poco::Util::Option("help", "h", "show help and exit").required(false).repeatable(false).binding("help"));
BaseDaemon::defineOptions(options);
}
int Server::run()
{
if (config().hasOption("help"))
{
Poco::Util::HelpFormatter help_formatter(Server::options());
auto header_str = fmt::format(
"{} server [OPTION] [-- [POSITIONAL_ARGS]...]\n"
"POSITIONAL_ARGS can be used to rewrite config properties, for example, --http_port=8010",
commandName());
help_formatter.setHeader(header_str);
help_formatter.format(std::cout);
return 0;
}
return BaseDaemon::run();
}
std::string Server::getDefaultCorePath() const
{
return getCanonicalPath(config().getString("path")) + "cores";
}
struct TiFlashProxyConfig
{
std::vector<const char *> args;
std::unordered_map<std::string, std::string> val_map;
bool is_proxy_runnable = false;
// TiFlash Proxy will set the default value of "flash.proxy.addr", so we don't need to set here.
void addExtraArgs(const std::string & k, const std::string & v)
{
std::string key = "--" + k;
val_map[key] = v;
auto iter = val_map.find(key);
args.push_back(iter->first.data());
args.push_back(iter->second.data());
}
// Try to parse start args from `config`.
// Return true if proxy need to be started, and `val_map` will be filled with the
// proxy start params.
// Return false if proxy is not need.
bool tryParseFromConfig(
const Poco::Util::LayeredConfiguration & config,
const DisaggregatedMode disaggregated_mode,
const bool use_autoscaler,
const LoggerPtr & log)
{
// tiflash_compute doesn't need proxy.
if (disaggregated_mode == DisaggregatedMode::Compute && use_autoscaler)
{
LOG_INFO(log, "TiFlash Proxy will not start because AutoScale Disaggregated Compute Mode is specified.");
return false;
}
Poco::Util::AbstractConfiguration::Keys keys;
config.keys("flash.proxy", keys);
if (!config.has("raft.pd_addr"))
{
LOG_WARNING(log, "TiFlash Proxy will not start because `raft.pd_addr` is not configured.");
if (!keys.empty())
LOG_WARNING(log, "`flash.proxy.*` is ignored because TiFlash Proxy will not start.");
return false;
}
{
// config items start from `flash.proxy.`
std::unordered_map<std::string, std::string> args_map;
for (const auto & key : keys)
args_map[key] = config.getString("flash.proxy." + key);
args_map["pd-endpoints"] = config.getString("raft.pd_addr");
args_map["engine-version"] = TiFlashBuildInfo::getReleaseVersion();
args_map["engine-git-hash"] = TiFlashBuildInfo::getGitHash();
if (!args_map.contains("engine-addr"))
args_map["engine-addr"] = config.getString("flash.service_addr", "0.0.0.0:3930");
else
args_map["advertise-engine-addr"] = args_map["engine-addr"];
args_map["engine-label"] = getProxyLabelByDisaggregatedMode(disaggregated_mode);
// For tiflash write node, it should report a extra label with "key" == "engine-role-label"
if (disaggregated_mode == DisaggregatedMode::Storage)
args_map["engine-role-label"] = DISAGGREGATED_MODE_WRITE_ENGINE_ROLE;
for (auto && [k, v] : args_map)
val_map.emplace("--" + k, std::move(v));
}
return true;
}
TiFlashProxyConfig(
Poco::Util::LayeredConfiguration & config,
const DisaggregatedMode disaggregated_mode,
const bool use_autoscaler,
const StorageFormatVersion & format_version,
const Settings & settings,
const LoggerPtr & log)
{
is_proxy_runnable = tryParseFromConfig(config, disaggregated_mode, use_autoscaler, log);
args.push_back("TiFlash Proxy");
for (const auto & v : val_map)
{
args.push_back(v.first.data());
args.push_back(v.second.data());
}
// Enable unips according to `format_version`
if (format_version.page == PageFormat::V4)
{
LOG_INFO(log, "Using UniPS for proxy");
addExtraArgs("unips-enabled", "1");
}
// Set the proxy's memory by size or ratio
std::visit(
[&](auto && arg) {
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, UInt64>)
{
if (arg != 0)
{
LOG_INFO(log, "Limit proxy's memory, size={}", arg);
addExtraArgs("memory-limit-size", std::to_string(arg));
}
}
else if constexpr (std::is_same_v<T, double>)
{
if (arg > 0 && arg <= 1.0)
{
LOG_INFO(log, "Limit proxy's memory, ratio={}", arg);
addExtraArgs("memory-limit-ratio", std::to_string(arg));
}
}
},
settings.max_memory_usage_for_all_queries.get());
}
};
pingcap::ClusterConfig getClusterConfig(
TiFlashSecurityConfigPtr security_config,
const int api_version,
const LoggerPtr & log)
{
pingcap::ClusterConfig config;
config.tiflash_engine_key = "engine";
config.tiflash_engine_value = DEF_PROXY_LABEL;
auto [ca_path, cert_path, key_path] = security_config->getPaths();
config.ca_path = ca_path;
config.cert_path = cert_path;
config.key_path = key_path;
switch (api_version)
{
case 1:
config.api_version = kvrpcpb::APIVersion::V1;
break;
case 2:
config.api_version = kvrpcpb::APIVersion::V2;
break;
default:
throw Exception(ErrorCodes::INVALID_CONFIG_PARAMETER, "Invalid api version {}", api_version);
}
LOG_INFO(
log,
"update cluster config, ca_path: {}, cert_path: {}, key_path: {}, api_version: {}",
ca_path,
cert_path,
key_path,
fmt::underlying(config.api_version));
return config;
}
LoggerPtr grpc_log;
void printGRPCLog(gpr_log_func_args * args)
{
String log_msg = fmt::format("{}, line number: {}, log msg : {}", args->file, args->line, args->message);
if (args->severity == GPR_LOG_SEVERITY_DEBUG)
{
LOG_DEBUG(grpc_log, log_msg);
}
else if (args->severity == GPR_LOG_SEVERITY_INFO)
{
LOG_INFO(grpc_log, log_msg);
}
else if (args->severity == GPR_LOG_SEVERITY_ERROR)
{
LOG_ERROR(grpc_log, log_msg);
}
}
extern "C" {
void run_raftstore_proxy_ffi(int argc, const char * const * argv, const EngineStoreServerHelper *);
}
struct RaftStoreProxyRunner : boost::noncopyable
{
struct RunRaftStoreProxyParms
{
const EngineStoreServerHelper * helper;
const TiFlashProxyConfig & conf;
/// set big enough stack size to avoid runtime error like stack-overflow.
size_t stack_size = 1024 * 1024 * 20;
};
RaftStoreProxyRunner(RunRaftStoreProxyParms && parms_, const LoggerPtr & log_)
: parms(std::move(parms_))
, log(log_)
{}
void join() const
{
if (!parms.conf.is_proxy_runnable)
return;
pthread_join(thread, nullptr);
}
void run()
{
if (!parms.conf.is_proxy_runnable)
return;
pthread_attr_t attribute;
pthread_attr_init(&attribute);
pthread_attr_setstacksize(&attribute, parms.stack_size);
LOG_INFO(log, "Start raft store proxy. Args: {}", parms.conf.args);
pthread_create(&thread, &attribute, runRaftStoreProxyFFI, &parms);
pthread_attr_destroy(&attribute);
}
private:
static void * runRaftStoreProxyFFI(void * pv)
{
setThreadName("RaftStoreProxy");
const auto & parms = *static_cast<const RunRaftStoreProxyParms *>(pv);
run_raftstore_proxy_ffi(static_cast<int>(parms.conf.args.size()), parms.conf.args.data(), parms.helper);
return nullptr;
}
RunRaftStoreProxyParms parms;
pthread_t thread{};
const LoggerPtr & log;
};
// By default init global thread pool by hardware_concurrency
// Later we will adjust it by `adjustThreadPoolSize`
void initThreadPool(DisaggregatedMode disaggregated_mode)
{
size_t default_num_threads = std::max(4UL, 2 * std::thread::hardware_concurrency());
// Note: Global Thread Pool must be larger than sub thread pools.
GlobalThreadPool::initialize(
/*max_threads*/ default_num_threads * 20,
/*max_free_threads*/ default_num_threads,
/*queue_size*/ default_num_threads * 8);
if (disaggregated_mode == DisaggregatedMode::Compute)
{
BuildReadTaskForWNPool::initialize(
/*max_threads*/ default_num_threads,
/*max_free_threads*/ default_num_threads / 2,
/*queue_size*/ default_num_threads * 2);
BuildReadTaskForWNTablePool::initialize(
/*max_threads*/ default_num_threads,
/*max_free_threads*/ default_num_threads / 2,
/*queue_size*/ default_num_threads * 2);
BuildReadTaskPool::initialize(
/*max_threads*/ default_num_threads,
/*max_free_threads*/ default_num_threads / 2,
/*queue_size*/ default_num_threads * 2);
RNWritePageCachePool::initialize(
/*max_threads*/ default_num_threads,
/*max_free_threads*/ default_num_threads / 2,
/*queue_size*/ default_num_threads * 2);
}
if (disaggregated_mode == DisaggregatedMode::Compute || disaggregated_mode == DisaggregatedMode::Storage)
{
DataStoreS3Pool::initialize(
/*max_threads*/ default_num_threads,
/*max_free_threads*/ default_num_threads / 2,
/*queue_size*/ default_num_threads * 2);
S3FileCachePool::initialize(
/*max_threads*/ default_num_threads,
/*max_free_threads*/ default_num_threads / 2,
/*queue_size*/ default_num_threads * 2);
}
if (disaggregated_mode == DisaggregatedMode::Storage)
{
WNEstablishDisaggTaskPool::initialize(
/*max_threads*/ default_num_threads,
/*max_free_threads*/ default_num_threads / 2,
/*queue_size*/ default_num_threads * 2);
}
}
void adjustThreadPoolSize(const Settings & settings, size_t logical_cores)
{
// TODO: make BackgroundPool/BlockableBackgroundPool/DynamicThreadPool spawned from `GlobalThreadPool`
size_t max_io_thread_count = std::ceil(settings.io_thread_count_scale * logical_cores);
// Note: Global Thread Pool must be larger than sub thread pools.
GlobalThreadPool::instance().setMaxThreads(max_io_thread_count * 200);
GlobalThreadPool::instance().setMaxFreeThreads(max_io_thread_count);
GlobalThreadPool::instance().setQueueSize(max_io_thread_count * 400);
if (BuildReadTaskForWNPool::instance)
{
BuildReadTaskForWNPool::instance->setMaxThreads(max_io_thread_count);
BuildReadTaskForWNPool::instance->setMaxFreeThreads(max_io_thread_count / 2);
BuildReadTaskForWNPool::instance->setQueueSize(max_io_thread_count * 2);
}
if (BuildReadTaskForWNTablePool::instance)
{
BuildReadTaskForWNTablePool::instance->setMaxThreads(max_io_thread_count);
BuildReadTaskForWNTablePool::instance->setMaxFreeThreads(max_io_thread_count / 2);
BuildReadTaskForWNTablePool::instance->setQueueSize(max_io_thread_count * 2);
}
if (BuildReadTaskPool::instance)
{
BuildReadTaskPool::instance->setMaxThreads(max_io_thread_count);
BuildReadTaskPool::instance->setMaxFreeThreads(max_io_thread_count / 2);
BuildReadTaskPool::instance->setQueueSize(max_io_thread_count * 2);
}
if (DataStoreS3Pool::instance)
{
DataStoreS3Pool::instance->setMaxThreads(max_io_thread_count);
DataStoreS3Pool::instance->setMaxFreeThreads(max_io_thread_count / 2);
DataStoreS3Pool::instance->setQueueSize(max_io_thread_count * 2);
}
if (S3FileCachePool::instance)
{
S3FileCachePool::instance->setMaxThreads(max_io_thread_count);
S3FileCachePool::instance->setMaxFreeThreads(max_io_thread_count / 2);
S3FileCachePool::instance->setQueueSize(max_io_thread_count * 2);
}
if (RNWritePageCachePool::instance)
{
RNWritePageCachePool::instance->setMaxThreads(max_io_thread_count);
RNWritePageCachePool::instance->setMaxFreeThreads(max_io_thread_count / 2);
RNWritePageCachePool::instance->setQueueSize(max_io_thread_count * 2);
}
size_t max_cpu_thread_count = std::ceil(settings.cpu_thread_count_scale * logical_cores);
if (WNEstablishDisaggTaskPool::instance)
{
// Tasks of EstablishDisaggTask is computation-intensive.
WNEstablishDisaggTaskPool::instance->setMaxThreads(max_cpu_thread_count);
WNEstablishDisaggTaskPool::instance->setMaxFreeThreads(max_cpu_thread_count / 2);
WNEstablishDisaggTaskPool::instance->setQueueSize(max_cpu_thread_count * 2);
}
}
void syncSchemaWithTiDB(
const TiFlashStorageConfig & storage_config,
BgStorageInitHolder & bg_init_stores,
const std::unique_ptr<Context> & global_context,
const LoggerPtr & log)
{
/// Then, sync schemas with TiDB, and initialize schema sync service.
/// If in API V2 mode, each keyspace's schema is fetch lazily.
if (storage_config.api_version == 1)
{
Stopwatch watch;
while (watch.elapsedSeconds() < global_context->getSettingsRef().ddl_restart_wait_seconds) // retry for 3 mins
{
try
{
global_context->getTMTContext().getSchemaSyncerManager()->syncSchemas(*global_context, NullspaceID);
break;
}
catch (Poco::Exception & e)
{
const int wait_seconds = 3;
LOG_ERROR(
log,
"Bootstrap failed because sync schema error: {}\nWe will sleep for {}"
" seconds and try again.",
e.displayText(),
wait_seconds);
::sleep(wait_seconds);
}
}
LOG_DEBUG(log, "Sync schemas done.");
}
// Init the DeltaMergeStore instances if data exist.
// Make the disk usage correct and prepare for serving
// queries.
bg_init_stores
.start(*global_context, log, storage_config.lazily_init_store, storage_config.s3_config.isS3Enabled());
// init schema sync service with tidb
global_context->initializeSchemaSyncService();
}
int Server::main(const std::vector<std::string> & /*args*/)
{
setThreadName("TiFlashMain");
UseSSL ssl_holder;
const auto log = Logger::get();
#ifdef FIU_ENABLE
fiu_init(0); // init failpoint
FailPointHelper::initRandomFailPoints(config(), log);
#endif
// Setup the config for jemalloc or mimalloc when enabled
setupAllocator(log);
// Setup the SIMD flags
setupSIMD(log);
registerFunctions();
registerAggregateFunctions();
registerWindowFunctions();
registerTableFunctions();
registerStorages();
const auto disaggregated_mode = getDisaggregatedMode(config());
const auto use_autoscaler = useAutoScaler(config());
// Later we may create thread pool from GlobalThreadPool
// init it before other components
initThreadPool(disaggregated_mode);
TiFlashErrorRegistry::instance(); // This invocation is for initializing
DM::ScanContext::initCurrentInstanceId(config(), log);
// Some Storage's config is necessary for Proxy
TiFlashStorageConfig storage_config;
// Deprecated settings.
// `global_capacity_quota` will be ignored if `storage_config.main_capacity_quota` is not empty.
// "0" by default, means no quota, the actual disk capacity is used.
size_t global_capacity_quota = 0;
std::tie(global_capacity_quota, storage_config) = TiFlashStorageConfig::parseSettings(config(), log);
if (!storage_config.s3_config.bucket.empty())
{
storage_config.s3_config.enable(/*check_requirements*/ true, log);
}
else if (disaggregated_mode == DisaggregatedMode::Compute && use_autoscaler)
{
// compute node with auto scaler, the requirements will be initted later.
storage_config.s3_config.enable(/*check_requirements*/ false, log);
}
if (storage_config.format_version != 0)
{
if (storage_config.s3_config.isS3Enabled() && !isStorageFormatForDisagg(storage_config.format_version))
{
auto message = fmt::format(
"'storage.format_version' must be set to {} when S3 is enabled!",
getStorageFormatsForDisagg());
LOG_ERROR(log, message);
throw Exception(ErrorCodes::INVALID_CONFIG_PARAMETER, message);
}
setStorageFormat(storage_config.format_version);
LOG_INFO(log, "Using format_version={} (explicit storage format detected).", STORAGE_FORMAT_CURRENT.identifier);
}
else
{
if (storage_config.s3_config.isS3Enabled())
{
// If the user does not explicitly set format_version in the config file but
// enables S3, then we set up a proper format version to support S3.
setStorageFormat(DEFAULT_STORAGE_FORMAT_FOR_DISAGG.identifier);
LOG_INFO(log, "Using format_version={} (infer by S3 is enabled).", STORAGE_FORMAT_CURRENT.identifier);
}
else
{
// Use the default settings
LOG_INFO(log, "Using format_version={} (default settings).", STORAGE_FORMAT_CURRENT.identifier);
}
}
// sanitize check for disagg mode
if (storage_config.s3_config.isS3Enabled())
{
if (disaggregated_mode == DisaggregatedMode::None)
{
const String message = "'flash.disaggregated_mode' must be set when S3 is enabled!";
LOG_ERROR(log, message);
throw Exception(ErrorCodes::INVALID_CONFIG_PARAMETER, message);
}
}
// Set whether to use safe point v2.
PDClientHelper::enable_safepoint_v2 = config().getBool("enable_safe_point_v2", false);
/** Context contains all that query execution is dependent:
* settings, available functions, data types, aggregate functions, databases...
*/
global_context = Context::createGlobal();
/// Initialize users config reloader.
auto users_config_reloader = UserConfig::parseSettings(config(), config_path, global_context, log);
/// Load global settings from default_profile and system_profile.
/// It internally depends on UserConfig::parseSettings.
// TODO: Parse the settings from config file at the program beginning
global_context->setDefaultProfiles();
LOG_INFO(
log,
"Loaded global settings from default_profile and system_profile, changed configs: {{{}}}",
global_context->getSettingsRef().toString());
Settings & settings = global_context->getSettingsRef();
// Init Proxy's config
TiFlashProxyConfig proxy_conf( //
config(),
disaggregated_mode,
use_autoscaler,
STORAGE_FORMAT_CURRENT,
settings,
log);
EngineStoreServerWrap tiflash_instance_wrap{};
auto helper = GetEngineStoreServerHelper(&tiflash_instance_wrap);
#ifdef USE_JEMALLOC
LOG_INFO(log, "Using Jemalloc for TiFlash");
#else
LOG_INFO(log, "Not using Jemalloc for TiFlash");
#endif
RaftStoreProxyRunner proxy_runner(RaftStoreProxyRunner::RunRaftStoreProxyParms{&helper, proxy_conf}, log);
if (proxy_conf.is_proxy_runnable)
{
proxy_runner.run();
LOG_INFO(log, "wait for tiflash proxy initializing");
while (!tiflash_instance_wrap.proxy_helper)
std::this_thread::sleep_for(std::chrono::milliseconds(200));
LOG_INFO(log, "tiflash proxy is initialized");
}
else
{
LOG_WARNING(log, "Skipped initialize TiFlash Proxy");
}
SCOPE_EXIT({
if (!proxy_conf.is_proxy_runnable)
return;
LOG_INFO(log, "Let tiflash proxy shutdown");
tiflash_instance_wrap.status = EngineStoreServerStatus::Terminated;
tiflash_instance_wrap.tmt = nullptr;
LOG_INFO(log, "Wait for tiflash proxy thread to join");
proxy_runner.join();
LOG_INFO(log, "tiflash proxy thread is joined");
});
/// get CPU/memory/disk info of this server
diagnosticspb::ServerInfoRequest request;
diagnosticspb::ServerInfoResponse response;
request.set_tp(static_cast<diagnosticspb::ServerInfoType>(1));
std::string req = request.SerializeAsString();
ffi_get_server_info_from_proxy(reinterpret_cast<intptr_t>(&helper), strIntoView(&req), &response);
server_info.parseSysInfo(response);
setNumberOfLogicalCPUCores(server_info.cpu_info.logical_cores);
computeAndSetNumberOfPhysicalCPUCores(server_info.cpu_info.logical_cores, server_info.cpu_info.physical_cores);
LOG_INFO(log, "ServerInfo: {}", server_info.debugString());
grpc_log = Logger::get("grpc");
gpr_set_log_verbosity(GPR_LOG_SEVERITY_DEBUG);
gpr_set_log_function(&printGRPCLog);
SCOPE_EXIT({
if (!proxy_conf.is_proxy_runnable)
return;
LOG_INFO(log, "Unlink tiflash_instance_wrap.tmt");
// Reset the `tiflash_instance_wrap.tmt` before `global_context` get released, or it will be a dangling pointer
tiflash_instance_wrap.tmt = nullptr;
});
global_context->setApplicationType(Context::ApplicationType::SERVER);
global_context->getSharedContextDisagg()->disaggregated_mode = disaggregated_mode;
global_context->getSharedContextDisagg()->use_autoscaler = use_autoscaler;
// Must init this before KVStore.
global_context->initializeJointThreadInfoJeallocMap();
/// Init File Provider
if (proxy_conf.is_proxy_runnable)
{
const bool enable_encryption = tiflash_instance_wrap.proxy_helper->checkEncryptionEnabled();
if (enable_encryption && storage_config.s3_config.isS3Enabled())
{
LOG_INFO(log, "encryption can be enabled, method is Aes256Ctr");
// The UniversalPageStorage has not been init yet, the UniversalPageStoragePtr in KeyspacesKeyManager is nullptr.
KeyManagerPtr key_manager
= std::make_shared<KeyspacesKeyManager<TiFlashRaftProxyHelper>>(tiflash_instance_wrap.proxy_helper);
global_context->initializeFileProvider(key_manager, true);
}
else if (enable_encryption)
{
const auto method = tiflash_instance_wrap.proxy_helper->getEncryptionMethod();
LOG_INFO(log, "encryption is enabled, method is {}", magic_enum::enum_name(method));
KeyManagerPtr key_manager = std::make_shared<DataKeyManager>(&tiflash_instance_wrap);
global_context->initializeFileProvider(key_manager, method != EncryptionMethod::Plaintext);
}
else
{
LOG_INFO(log, "encryption is disabled");
KeyManagerPtr key_manager = std::make_shared<DataKeyManager>(&tiflash_instance_wrap);
global_context->initializeFileProvider(key_manager, false);
}
}
else
{
KeyManagerPtr key_manager = std::make_shared<MockKeyManager>(false);
global_context->initializeFileProvider(key_manager, false);
}
/// ===== Paths related configuration initialized start ===== ///
/// Note that theses global variables should be initialized by the following order:
// 1. capacity
// 2. path pool
// 3. TMTContext
LOG_INFO(
log,
"disaggregated_mode={} use_autoscaler={} enable_s3={}",
magic_enum::enum_name(global_context->getSharedContextDisagg()->disaggregated_mode),
global_context->getSharedContextDisagg()->use_autoscaler,
storage_config.s3_config.isS3Enabled());
if (storage_config.s3_config.isS3Enabled())
S3::ClientFactory::instance().init(storage_config.s3_config);
global_context->getSharedContextDisagg()->initRemoteDataStore(
global_context->getFileProvider(),
storage_config.s3_config.isS3Enabled());
const auto is_compute_mode = global_context->getSharedContextDisagg()->isDisaggregatedComputeMode();
const auto [remote_cache_paths, remote_cache_capacity_quota]
= storage_config.remote_cache_config.getCacheDirInfos(is_compute_mode);
global_context->initializePathCapacityMetric( //
global_capacity_quota, //
storage_config.main_data_paths,
storage_config.main_capacity_quota, //
storage_config.latest_data_paths,
storage_config.latest_capacity_quota,
remote_cache_paths,
remote_cache_capacity_quota);
TiFlashRaftConfig raft_config = TiFlashRaftConfig::parseSettings(config(), log);
global_context->setPathPool( //
storage_config.main_data_paths, //
storage_config.latest_data_paths, //
storage_config.kvstore_data_path, //
global_context->getPathCapacity(),
global_context->getFileProvider());
if (const auto & config = storage_config.remote_cache_config; config.isCacheEnabled() && is_compute_mode)
{
config.initCacheDir();
FileCache::initialize(global_context->getPathCapacity(), config);
}
/// Determining PageStorage run mode based on current files on disk and storage config.
/// Do it as early as possible after loading storage config.
global_context->initializePageStorageMode(global_context->getPathPool(), STORAGE_FORMAT_CURRENT.page);
// Use "system" as the default_database for all TCP connections, which is always exist in TiFlash.
const std::string default_database = "system";
Strings all_normal_path = storage_config.getAllNormalPaths();
const std::string path = all_normal_path[0];
global_context->setPath(path);
/// ===== Paths related configuration initialized end ===== ///
global_context->setSecurityConfig(config(), log);
Redact::setRedactLog(global_context->getSecurityConfig()->redactInfoLog());
// Create directories for 'path' and for default database, if not exist.
for (const String & candidate_path : all_normal_path)
{
Poco::File(candidate_path + "data/" + default_database).createDirectories();
}
Poco::File(path + "metadata/" + default_database).createDirectories();
StatusFile status{path + "status"};
SCOPE_EXIT({
/** Explicitly destroy Context. It is more convenient than in destructor of Server, because logger is still available.
* At this moment, no one could own shared part of Context.
*/
global_context.reset();
LOG_DEBUG(log, "Destroyed global context.");
});
/// Try to increase limit on number of open files.
{
rlimit rlim{};
if (getrlimit(RLIMIT_NOFILE, &rlim))
throw Poco::Exception("Cannot getrlimit");
if (rlim.rlim_cur == rlim.rlim_max)
{
LOG_DEBUG(log, "rlimit on number of file descriptors is {}", rlim.rlim_cur);
}
else
{
rlim_t old = rlim.rlim_cur;
rlim.rlim_cur = config().getUInt("max_open_files", rlim.rlim_max);
int rc = setrlimit(RLIMIT_NOFILE, &rlim);
if (rc != 0)
LOG_WARNING(
log,
"Cannot set max number of file descriptors to {}"
". Try to specify max_open_files according to your system limits. error: {}",
rlim.rlim_cur,
strerror(errno));
else
LOG_DEBUG(log, "Set max number of file descriptors to {} (was {}).", rlim.rlim_cur, old);
}
}
static ServerErrorHandler error_handler;
Poco::ErrorHandler::set(&error_handler);
/// Initialize DateLUT early, to not interfere with running time of first query.
LOG_DEBUG(log, "Initializing DateLUT.");
DateLUT::instance();
LOG_TRACE(log, "Initialized DateLUT with time zone `{}`.", DateLUT::instance().getTimeZone());
/// Directory with temporary data for processing of heavy queries.
{
std::string tmp_path = config().getString("tmp_path", path + "tmp/");
global_context->setTemporaryPath(tmp_path);
Poco::File(tmp_path).createDirectories();
/// Clearing old temporary files.
Poco::DirectoryIterator dir_end;
for (Poco::DirectoryIterator it(tmp_path); it != dir_end; ++it)
{
if (it->isFile() && startsWith(it.name(), "tmp"))
{
LOG_DEBUG(log, "Removing old temporary file {}", it->path());
global_context->getFileProvider()->deleteRegularFile(it->path(), EncryptionPath(it->path(), ""));
}
}
}
/** Directory with 'flags': files indicating temporary settings for the server set by system administrator.
* Flags may be cleared automatically after being applied by the server.
* Examples: do repair of local data; clone all replicated tables from replica.
*/
{
Poco::File(path + "flags/").createDirectories();
global_context->setFlagsPath(path + "flags/");
}
/// Init TiFlash metrics.
global_context->initializeTiFlashMetrics();
///
/// The config value in global settings can only be used from here because we just loaded it from config file.
///
/// Initialize the background & blockable background thread pool.
LOG_INFO(log, "Background & Blockable Background pool size: {}", settings.background_pool_size);
auto & bg_pool = global_context->initializeBackgroundPool(settings.background_pool_size);
auto & blockable_bg_pool = global_context->initializeBlockableBackgroundPool(settings.background_pool_size);
// adjust the thread pool size according to settings and logical cores num
adjustThreadPoolSize(settings, server_info.cpu_info.logical_cores);
initStorageMemoryTracker(
settings.max_memory_usage_for_all_queries.getActualBytes(server_info.memory_info.capacity),
settings.bytes_that_rss_larger_than_limit);
if (global_context->getSharedContextDisagg()->isDisaggregatedComputeMode())
{
// No need to have local index scheduler.
}
else if (global_context->getSharedContextDisagg()->isDisaggregatedStorageMode())
{
// There is no compute task in write node.
// Set the pool size to 80% of logical cores and 60% of memory
// to take full advantage of the resources and avoid blocking other tasks like writes and compactions.
global_context->initializeGlobalLocalIndexerScheduler(
std::max(1, server_info.cpu_info.logical_cores * 8 / 10), // at least 1 thread
std::max(256 * 1024 * 1024ULL, server_info.memory_info.capacity * 6 / 10)); // at least 256MB
}
else
{
// There could be compute tasks, reserve more memory for computes.
global_context->initializeGlobalLocalIndexerScheduler(
std::max(1, server_info.cpu_info.logical_cores * 4 / 10), // at least 1 thread
std::max(256 * 1024 * 1024ULL, server_info.memory_info.capacity * 4 / 10)); // at least 256MB
}
/// PageStorage run mode has been determined above
global_context->initializeGlobalPageIdAllocator();
if (!global_context->getSharedContextDisagg()->isDisaggregatedComputeMode())
{
global_context->initializeGlobalStoragePoolIfNeed(global_context->getPathPool());
LOG_INFO(
log,
"Global PageStorage run mode is {}",
magic_enum::enum_name(global_context->getPageStorageRunMode()));
}
/// Try to restore the StoreIdent from UniPS. There are many services that require
/// `store_id` to generate the path to RemoteStore under disagg mode.
std::optional<raft_serverpb::StoreIdent> store_ident;
// Only when this node is disagg compute node and autoscaler is enabled, we don't need the WriteNodePageStorage instance
// Disagg compute node without autoscaler still need this instance for proxy's data
if (!(global_context->getSharedContextDisagg()->isDisaggregatedComputeMode()
&& global_context->getSharedContextDisagg()->use_autoscaler))
{
global_context->initializeWriteNodePageStorageIfNeed(global_context->getPathPool());
if (auto wn_ps = global_context->tryGetWriteNodePageStorage(); wn_ps != nullptr)
{
if (tiflash_instance_wrap.proxy_helper->checkEncryptionEnabled() && storage_config.s3_config.isS3Enabled())
{
global_context->getFileProvider()->setPageStoragePtrForKeyManager(wn_ps);
}
store_ident = tryGetStoreIdent(wn_ps);
if (!store_ident)
{
LOG_INFO(log, "StoreIdent not exist, new tiflash node");
}
else
{
LOG_INFO(log, "StoreIdent restored, {{{}}}", store_ident->ShortDebugString());
}
}
}
if (global_context->getSharedContextDisagg()->isDisaggregatedStorageMode())
{
global_context->getSharedContextDisagg()->initWriteNodeSnapManager();