forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDorisFE.java
More file actions
executable file
·696 lines (619 loc) · 30.2 KB
/
Copy pathDorisFE.java
File metadata and controls
executable file
·696 lines (619 loc) · 30.2 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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.
package org.apache.doris;
import org.apache.doris.catalog.Env;
import org.apache.doris.cluster.ClusterGuardFactory;
import org.apache.doris.common.CommandLineOptions;
import org.apache.doris.common.Config;
import org.apache.doris.common.FeConstants;
import org.apache.doris.common.FeMetaVersion;
import org.apache.doris.common.LdapConfig;
import org.apache.doris.common.Log4jConfig;
import org.apache.doris.common.LogUtils;
import org.apache.doris.common.ThreadPoolManager;
import org.apache.doris.common.Version;
import org.apache.doris.common.lock.DeadlockMonitor;
import org.apache.doris.common.util.JdkUtils;
import org.apache.doris.common.util.NetUtils;
import org.apache.doris.common.util.Util;
import org.apache.doris.datasource.FileCacheAdmissionManager;
import org.apache.doris.httpv2.HttpServer;
import org.apache.doris.journal.bdbje.BDBDebugger;
import org.apache.doris.journal.bdbje.BDBTool;
import org.apache.doris.journal.bdbje.BDBToolOptions;
import org.apache.doris.persist.meta.MetaReader;
import org.apache.doris.qe.Coordinator;
import org.apache.doris.qe.QeProcessorImpl;
import org.apache.doris.qe.QeService;
import org.apache.doris.qe.SimpleScheduler;
import org.apache.doris.service.ExecuteEnv;
import org.apache.doris.service.FrontendOptions;
import org.apache.doris.tls.server.FeServerStarterFactory;
import org.apache.doris.tls.server.ServerStarter;
import org.apache.doris.tls.server.TlsProtocolSet;
import com.google.common.base.Charsets;
import com.google.common.base.Strings;
import io.netty.util.internal.logging.InternalLoggerFactory;
import io.netty.util.internal.logging.Log4JLoggerFactory;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.management.ManagementFactory;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.nio.file.StandardOpenOption;
import java.time.LocalDate;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
public class DorisFE {
private static final Logger LOG = LogManager.getLogger(DorisFE.class);
static {
InternalLoggerFactory.setDefaultFactory(Log4JLoggerFactory.INSTANCE);
}
public static final String DORIS_HOME_DIR = System.getenv("DORIS_HOME");
public static final String PID_DIR = System.getenv("PID_DIR");
private static String LOCK_FILE_PATH;
private static final String LOCK_FILE_NAME = "process.lock";
private static FileChannel processLockFileChannel;
private static FileLock processFileLock;
// set to true when all servers are ready.
private static final AtomicBoolean serverReady = new AtomicBoolean(false);
// HTTP server instance, used for graceful shutdown
private static HttpServer httpServer;
private static ServerStarter thriftServerStarter;
private static QeService qeService;
public static void main(String[] args) {
// Every doris version should have a final meta version, it should not change
// between small releases. Add a check here to avoid mistake.
if (Version.DORIS_FE_META_VERSION > 0
&& FeMetaVersion.VERSION_CURRENT != Version.DORIS_FE_META_VERSION) {
System.err.println("This release's fe meta version should be "
+ Version.DORIS_FE_META_VERSION
+ " but it is " + FeMetaVersion.VERSION_CURRENT
+ ". It should not change, or FE could not rollback in this version");
return;
}
StartupOptions options = new StartupOptions();
options.enableHttpServer = true;
options.enableQeService = true;
start(DORIS_HOME_DIR, PID_DIR, args, options);
}
private static void startMonitor() {
if (Config.enable_deadlock_detection) {
DeadlockMonitor deadlockMonitor = new DeadlockMonitor();
deadlockMonitor.startMonitoring(Config.deadlock_detection_interval_minute, TimeUnit.MINUTES);
}
}
// entrance for doris frontend
public static void start(String dorisHomeDir, String pidDir, String[] args, StartupOptions options) {
if (Strings.isNullOrEmpty(dorisHomeDir)) {
System.err.println("env DORIS_HOME is not set.");
return;
}
if (Strings.isNullOrEmpty(pidDir)) {
System.err.println("env PID_DIR is not set.");
return;
}
CommandLineOptions cmdLineOpts = parseArgs(args);
try {
// init config
Config config = new Config();
config.init(dorisHomeDir + "/conf/fe.conf");
// Must init custom config after init config, separately.
// Because the path of custom config file is defined in fe.conf
config.initCustom(Config.custom_config_dir + "/fe_custom.conf");
LdapConfig ldapConfig = new LdapConfig();
if (new File(dorisHomeDir + "/conf/ldap.conf").exists()) {
ldapConfig.init(dorisHomeDir + "/conf/ldap.conf");
}
overwriteConfigs();
// check it after Config is initialized, otherwise the config 'check_java_version' won't work.
if (!JdkUtils.checkJavaVersion()) {
throw new IllegalArgumentException("Java version doesn't match");
}
// Set foreground flag after Config.init() but before Log4jConfig class loading,
// so that Log4jConfig's static block can read the correct config values (e.g. log_rollover_strategy).
if (System.getenv("DORIS_LOG_TO_STDERR") != null) {
Log4jConfig.foreground = true;
}
Log4jConfig.initLogging(dorisHomeDir + "/conf/");
// Add shutdown hook for graceful exit
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
LOG.info("Received shutdown signal, starting graceful shutdown...");
serverReady.set(false);
gracefulShutdown();
if (qeService != null) {
qeService.stop();
}
if (thriftServerStarter != null) {
try {
thriftServerStarter.stop();
} catch (Exception e) {
LOG.warn("stop FE thrift starter failed", e);
}
}
// Shutdown HTTP server after main process graceful shutdown is complete
if (httpServer != null) {
httpServer.shutdown();
}
LogManager.shutdown();
}));
// set dns cache ttl
java.security.Security.setProperty("networkaddress.cache.ttl", "60");
// pid file
if (!cmdLineOpts.isVersion() && !createAndLockPidFile(pidDir + "/fe.pid")) {
throw new IOException("pid file is already locked.");
}
// check command line options
checkCommandLineOptions(cmdLineOpts);
try {
tryLockProcess();
} catch (Exception e) {
LOG.error("start doris failed.", e);
System.exit(-1);
}
fuzzyConfigs();
initClusterGuard(dorisHomeDir);
LOG.info("Doris FE starting...");
FrontendOptions.init();
// check all port
checkAllPorts();
if (Config.enable_bdbje_debug_mode) {
// Start in BDB Debug mode
BDBDebugger.get().startDebugMode(Config.meta_dir + "/bdb");
return;
}
// To resolve: "SdkClientException: Multiple HTTP implementations were found on the classpath"
// Currently, there are 2 implements of HTTP client: ApacheHttpClient and UrlConnectionHttpClient
// The UrlConnectionHttpClient is introduced by #16602, and it causes the exception.
// So we set the default HTTP client to UrlConnectionHttpClient.
// TODO: remove this after we remove ApacheHttpClient
System.setProperty("software.amazon.awssdk.http.service.impl",
"software.amazon.awssdk.http.urlconnection.UrlConnectionSdkHttpService");
if (cmdLineOpts.getClusterSnapshotPath() != null) {
String clusterSnapshotPath = cmdLineOpts.getClusterSnapshotPath();
if (!clusterSnapshotPath.startsWith("/")) {
// relative path
clusterSnapshotPath = dorisHomeDir + "/" + clusterSnapshotPath;
}
Env.getCurrentEnv().setClusterSnapshotFile(clusterSnapshotPath);
}
// init catalog and wait it be ready
Env.getCurrentEnv().initialize(args);
Env.getCurrentEnv().waitForReady();
if (Config.enable_file_cache_admission_control) {
FileCacheAdmissionManager.getInstance().loadOnStartup();
}
// init and start:
// 1. HttpServer for HTTP Server
// 2. FE thrift server starter
// 3. QeService for MySQL Server
thriftServerStarter = FeServerStarterFactory.createThriftServerStarter(Config.rpc_port);
thriftServerStarter.start();
if (options.enableHttpServer) {
httpServer = new HttpServer();
httpServer.setPort(Config.http_port);
httpServer.setHttpsPort(Config.https_port);
httpServer.setMaxHttpPostSize(Config.jetty_server_max_http_post_size);
httpServer.setAcceptors(Config.jetty_server_acceptors);
httpServer.setSelectors(Config.jetty_server_selectors);
httpServer.setWorkers(Config.jetty_server_workers);
httpServer.setKeyStorePath(Config.key_store_path);
httpServer.setKeyStorePassword(Config.key_store_password);
httpServer.setKeyStoreType(Config.key_store_type);
httpServer.setKeyStoreAlias(Config.key_store_alias);
httpServer.setEnableHttps(Config.enable_https
&& !(Config.enable_tls && TlsProtocolSet.isProtocolIncluded(TlsProtocolSet.Protocol.HTTP)));
httpServer.setMaxThreads(Config.jetty_threadPool_maxThreads);
httpServer.setMinThreads(Config.jetty_threadPool_minThreads);
httpServer.setMaxHttpHeaderSize(Config.jetty_server_max_http_header_size);
httpServer.start();
Env.getCurrentEnv().setHttpReady(true);
}
SimpleScheduler.init();
if (options.enableQeService) {
qeService = new QeService(Config.query_port, Config.arrow_flight_sql_port,
ExecuteEnv.getInstance().getScheduler());
qeService.start();
}
ThreadPoolManager.registerAllThreadPoolMetric();
startMonitor();
serverReady.set(true);
// JVM will exit when shutdown hook is completed
while (true) {
Thread.sleep(2000);
}
} catch (Throwable e) {
// Some exception may throw before LOG is inited.
// So need to print to stdout
e.printStackTrace();
LOG.error("", e);
// to avoid nonDaemon Thread block main Thread, we need to force exit
System.exit(-1);
}
}
private static void checkAllPorts() throws IOException {
if (Config.enable_check_compatibility_mode) {
// The compatibility mode does not need to listen ports.
return;
}
if (!NetUtils.isPortAvailable(FrontendOptions.getLocalHostAddress(), Config.edit_log_port,
"Edit log port", NetUtils.EDIT_LOG_PORT_SUGGESTION)) {
throw new IOException("port " + Config.edit_log_port + " already in use");
}
if (!NetUtils.isPortAvailable(FrontendOptions.getLocalHostAddress(), Config.http_port,
"Http port", NetUtils.HTTP_PORT_SUGGESTION)) {
throw new IOException("port " + Config.http_port + " already in use");
}
if (Config.enable_https && !NetUtils.isPortAvailable(FrontendOptions.getLocalHostAddress(),
Config.https_port, "Https port", NetUtils.HTTPS_PORT_SUGGESTION)) {
throw new IOException("port " + Config.https_port + " already in use");
}
if (!NetUtils.isPortAvailable(FrontendOptions.getLocalHostAddress(), Config.query_port,
"Query port", NetUtils.QUERY_PORT_SUGGESTION)) {
throw new IOException("port " + Config.query_port + " already in use");
}
if (!NetUtils.isPortAvailable(FrontendOptions.getLocalHostAddress(), Config.rpc_port,
"Rpc port", NetUtils.RPC_PORT_SUGGESTION)) {
throw new IOException("port " + Config.rpc_port + " already in use");
}
if (Config.arrow_flight_sql_port != -1
&& !NetUtils.isPortAvailable(FrontendOptions.getLocalHostAddress(), Config.arrow_flight_sql_port,
"Arrow Flight SQL port", NetUtils.ARROW_FLIGHT_SQL_SUGGESTION)) {
throw new IOException("port " + Config.arrow_flight_sql_port + " already in use");
}
}
/*
* -v --version
* Print the version of Doris Frontend
* -h --helper
* Specify the helper node when joining a bdb je replication group
* -i --image
* Check if the specified image is valid
* -b --bdb
* Run bdbje debug tools
*
* -l --listdb
* List all database names in bdbje
* -d --db
* Specify a database in bdbje
*
* -s --stat
* Print statistic of a database, including count, first key, last key
* -f --from
* Specify the start scan key
* -t --to
* Specify the end scan key
* -m --metaversion
* Specify the meta version to decode log value
*
*/
private static CommandLineOptions parseArgs(String[] args) {
CommandLineParser commandLineParser = new DefaultParser();
Options options = new Options();
options.addOption("v", "version", false, "Print the version of Doris Frontend");
options.addOption("h", "helper", true, "Specify the helper node when joining a bdb je replication group");
options.addOption("i", "image", true, "Check if the specified image is valid");
options.addOption("b", "bdb", false, "Run bdbje debug tools");
options.addOption("l", "listdb", false, "List databases in bdbje");
options.addOption("d", "db", true, "Specify a database in bdbje");
options.addOption("s", "stat", false, "Print statistic of a database, including count, first key, last key");
options.addOption("f", "from", true, "Specify the start scan key");
options.addOption("t", "to", true, "Specify the end scan key");
options.addOption("m", "metaversion", true, "Specify the meta version to decode log value");
options.addOption("r", FeConstants.METADATA_FAILURE_RECOVERY_KEY, false,
"Check if the specified metadata recover is valid");
options.addOption(Option.builder().longOpt(FeConstants.RECOVERY_JOURNAL_ID_KEY).hasArg()
.desc("Specify the recovery truncate journal id, and journals greater than this id will be removed")
.build());
options.addOption("c", "cluster_snapshot", true, "Specify the cluster snapshot json file");
options.addOption(Option.builder().longOpt(FeConstants.DROP_BACKENDS_KEY)
.desc("When this FE becomes MASTER, drop all backends from cluster metadata (destructive)").build());
CommandLine cmd = null;
try {
cmd = commandLineParser.parse(options, args);
} catch (final ParseException e) {
LOG.warn("", e);
System.err.println("Failed to parse command line. exit now");
System.exit(-1);
}
// version
if (cmd.hasOption('v') || cmd.hasOption("version")) {
return new CommandLineOptions(true, "", null, "");
}
// helper
if (cmd.hasOption('h') || cmd.hasOption("helper")) {
String helperNode = cmd.getOptionValue("helper");
if (Strings.isNullOrEmpty(helperNode)) {
System.err.println("Missing helper node");
System.exit(-1);
}
return new CommandLineOptions(false, helperNode, null, "");
}
// image
if (cmd.hasOption('i') || cmd.hasOption("image")) {
// get image path
String imagePath = cmd.getOptionValue("image");
if (Strings.isNullOrEmpty(imagePath)) {
System.err.println("imagePath is not set");
System.exit(-1);
}
return new CommandLineOptions(false, "", null, imagePath);
}
if (cmd.hasOption('r') || cmd.hasOption(FeConstants.METADATA_FAILURE_RECOVERY_KEY)) {
System.setProperty(FeConstants.METADATA_FAILURE_RECOVERY_KEY, "true");
}
if (cmd.hasOption(FeConstants.RECOVERY_JOURNAL_ID_KEY)) {
String recoveryJournalId = cmd.getOptionValue(FeConstants.RECOVERY_JOURNAL_ID_KEY);
if (Strings.isNullOrEmpty(recoveryJournalId)) {
System.err.println("recovery_journal_id is missing");
System.exit(-1);
}
System.setProperty(FeConstants.RECOVERY_JOURNAL_ID_KEY, recoveryJournalId.trim());
}
if (cmd.hasOption(FeConstants.DROP_BACKENDS_KEY)) {
System.setProperty(FeConstants.DROP_BACKENDS_KEY, "true");
}
if (cmd.hasOption('b') || cmd.hasOption("bdb")) {
if (cmd.hasOption('l') || cmd.hasOption("listdb")) {
// list bdb je databases
BDBToolOptions bdbOpts = new BDBToolOptions(true, "", false, "", "", 0);
return new CommandLineOptions(false, "", bdbOpts, "");
}
if (cmd.hasOption('d') || cmd.hasOption("db")) {
// specify a database
String dbName = cmd.getOptionValue("db");
if (Strings.isNullOrEmpty(dbName)) {
System.err.println("BDBJE database name is missing");
System.exit(-1);
}
if (cmd.hasOption('s') || cmd.hasOption("stat")) {
BDBToolOptions bdbOpts = new BDBToolOptions(false, dbName, true, "", "", 0);
return new CommandLineOptions(false, "", bdbOpts, "");
}
String fromKey = "";
String endKey = "";
int metaVersion = 0;
if (cmd.hasOption('f') || cmd.hasOption("from")) {
fromKey = cmd.getOptionValue("from");
if (Strings.isNullOrEmpty(fromKey)) {
System.err.println("from key is missing");
System.exit(-1);
}
}
if (cmd.hasOption('t') || cmd.hasOption("to")) {
endKey = cmd.getOptionValue("to");
if (Strings.isNullOrEmpty(endKey)) {
System.err.println("end key is missing");
System.exit(-1);
}
}
if (cmd.hasOption('m') || cmd.hasOption("metaversion")) {
try {
metaVersion = Integer.parseInt(cmd.getOptionValue("metaversion"));
} catch (NumberFormatException e) {
System.err.println("Invalid meta version format");
System.exit(-1);
}
}
BDBToolOptions bdbOpts = new BDBToolOptions(false, dbName, false, fromKey, endKey, metaVersion);
return new CommandLineOptions(false, "", bdbOpts, "");
} else {
System.err.println("Invalid options when running bdb je tools");
System.exit(-1);
}
}
// cluster snapshot
if (cmd.hasOption('c') || cmd.hasOption("cluster_snapshot")) {
String clusterSnapshotFile = cmd.getOptionValue("cluster_snapshot");
if (Strings.isNullOrEmpty(clusterSnapshotFile)) {
System.err.println("Missing cluster_snapshot file");
System.exit(-1);
}
return new CommandLineOptions(false, null, null, "", clusterSnapshotFile.trim());
}
// helper node is null, means no helper node is specified
return new CommandLineOptions(false, null, null, "");
}
private static void printVersion() {
LogUtils.stdout("Build version: " + Version.DORIS_BUILD_VERSION);
LogUtils.stdout("Build time: " + Version.DORIS_BUILD_TIME);
LogUtils.stdout("Build info: " + Version.DORIS_BUILD_INFO);
LogUtils.stdout("Build hash: " + Version.DORIS_BUILD_HASH);
LogUtils.stdout("Java compile version: " + Version.DORIS_JAVA_COMPILE_VERSION);
LOG.info("Build version: {}", Version.DORIS_BUILD_VERSION);
LOG.info("Build time: {}", Version.DORIS_BUILD_TIME);
LOG.info("Build info: {}", Version.DORIS_BUILD_INFO);
LOG.info("Build hash: {}", Version.DORIS_BUILD_HASH);
LOG.info("Java compile version: {}", Version.DORIS_JAVA_COMPILE_VERSION);
if (!Version.DORIS_FEATURE_LIST.isEmpty()) {
LogUtils.stdout("Features: " + Version.DORIS_FEATURE_LIST);
LOG.info("Features: {}", Version.DORIS_FEATURE_LIST);
}
if (Config.isCloudMode()) {
LogUtils.stdout("Run FE in the cloud mode, cloud_unique_id: " + Config.cloud_unique_id
+ ", meta_service_endpoint: " + Config.meta_service_endpoint);
} else {
LogUtils.stdout("Run FE in the local mode");
}
}
private static void checkCommandLineOptions(CommandLineOptions cmdLineOpts) {
if (cmdLineOpts.isVersion()) {
printVersion();
System.exit(0);
} else if (cmdLineOpts.runBdbTools()) {
BDBTool bdbTool = new BDBTool(Env.getCurrentEnv().getBdbDir(), cmdLineOpts.getBdbToolOpts());
if (bdbTool.run()) {
System.exit(0);
} else {
System.exit(-1);
}
} else if (cmdLineOpts.runImageTool()) {
File imageFile = new File(cmdLineOpts.getImagePath());
if (!imageFile.exists()) {
LogUtils.stderr("image does not exist: " + imageFile.getAbsolutePath()
+ " . Please put an absolute path instead");
System.exit(-1);
} else {
LogUtils.stdout("Start to load image: ");
try {
MetaReader.read(imageFile, Env.getCurrentEnv());
LogUtils.stdout("Load image success. Image file " + cmdLineOpts.getImagePath() + " is valid");
} catch (Exception e) {
LogUtils.stderr("Load image failed. Image file " + cmdLineOpts.getImagePath() + " is invalid");
LOG.warn("", e);
} finally {
System.exit(0);
}
}
}
printVersion();
// go on
}
private static boolean createAndLockPidFile(String pidFilePath) throws IOException {
File pid = new File(pidFilePath);
try (RandomAccessFile file = new RandomAccessFile(pid, "rws")) {
FileLock lock = file.getChannel().tryLock();
if (lock == null) {
return false;
}
pid.deleteOnExit();
String name = ManagementFactory.getRuntimeMXBean().getName();
file.setLength(0);
file.write(name.split("@")[0].getBytes(Charsets.UTF_8));
return true;
} catch (OverlappingFileLockException e) {
return false;
} catch (IOException e) {
throw e;
}
}
/**
* When user starts multiple FE processes at the same time and uses one metadata directory,
* the metadata directory will be damaged. Therefore, we will bind the process by creating a file lock to ensure
* that only one FE process can occupy a metadata directory, and other processes will fail to start.
*/
private static void tryLockProcess() {
try {
LOCK_FILE_PATH = Config.meta_dir + "/" + LOCK_FILE_NAME;
processLockFileChannel = FileChannel.open(new File(LOCK_FILE_PATH).toPath(), StandardOpenOption.WRITE,
StandardOpenOption.CREATE);
processFileLock = processLockFileChannel.tryLock();
if (processFileLock != null) {
// we need bind the lock file with the process
Runtime.getRuntime().addShutdownHook(new Thread(DorisFE::releaseFileLockAndCloseFileChannel));
return;
}
releaseFileLockAndCloseFileChannel();
} catch (IOException e) {
releaseFileLockAndCloseFileChannel();
throw new RuntimeException("Try to lock process failed", e);
}
throw new RuntimeException("FE process has been started, please do not start multiple FE processes at the "
+ "same time");
}
private static void releaseFileLockAndCloseFileChannel() {
if (processFileLock != null && processFileLock.isValid()) {
try {
processFileLock.release();
} catch (IOException ioException) {
LOG.warn("release process lock file failed", ioException);
}
}
if (processLockFileChannel != null && processLockFileChannel.isOpen()) {
try {
processLockFileChannel.close();
} catch (IOException ignored) {
LOG.warn("release process lock file failed", ignored);
}
}
}
private static void initClusterGuard(String dorisHomeDir) throws Exception {
ClusterGuardFactory.getGuard().onStartup(dorisHomeDir);
LOG.info("Cluster guard initialized successfully.");
}
public static void overwriteConfigs() {
if (Config.isCloudMode() && Config.enable_feature_binlog) {
Config.enable_feature_binlog = false;
LOG.warn("Force set enable_feature_binlog=false because it is not supported in the cloud mode yet");
}
}
private static void fuzzyConfigs() {
if (!Config.use_fuzzy_conf) {
return;
}
// Keep global fuzzy knobs that are not session-based.
if (Config.fuzzy_test_type.equalsIgnoreCase("daily")
|| Config.fuzzy_test_type.equalsIgnoreCase("rqg")) {
Config.random_add_order_by_keys_for_mow = (LocalDate.now().getDayOfMonth() % 2 == 0);
LOG.info("fuzzy set random_add_order_by_keys_for_mow={}", Config.random_add_order_by_keys_for_mow);
}
Config.enable_txn_log_outside_lock = new Random().nextBoolean();
LOG.info("fuzzy set enable_txn_log_outside_lock={}", Config.enable_txn_log_outside_lock);
Config.enable_batch_editlog = new Random().nextBoolean();
LOG.info("fuzzy set enable_batch_editlog={}", Config.enable_batch_editlog);
setFuzzyForCatalog();
}
private static void setFuzzyForCatalog() {
if (!Config.fuzzy_test_type.equals("external")) {
return;
}
Config.max_hive_partition_cache_num = Util.getRandomLong(0, 10, 10000, 100000);
Config.max_hive_partition_table_cache_num = Util.getRandomLong(0, 10, 1000, 10000);
Config.external_cache_expire_time_seconds_after_access = Util.getRandomLong(0, 1, 10, 86400);
Config.external_cache_refresh_time_minutes = Util.getRandomLong(1, 10);
Config.max_external_cache_loader_thread_pool_size = Util.getRandomInt(1, 10, 64);
Config.max_external_file_cache_num = Util.getRandomInt(0, 10, 10000);
Config.max_external_schema_cache_num = Util.getRandomInt(0, 1, 10, 10000);
Config.max_external_table_cache_num = Util.getRandomInt(0, 1, 10, 10000);
Config.max_external_table_row_count_cache_num = Util.getRandomInt(0, 1, 10, 100000);
Config.max_external_table_split_file_meta_cache_num = Util.getRandomInt(0, 1, 10, 100000);
}
public static class StartupOptions {
public boolean enableHttpServer = true;
public boolean enableQeService = true;
}
public static boolean isServerReady() {
return serverReady.get();
}
private static void gracefulShutdown() {
// wait for all queries to finish
try {
long now = System.currentTimeMillis();
List<Coordinator> allCoordinators = QeProcessorImpl.INSTANCE.getAllCoordinators();
while (!allCoordinators.isEmpty() && System.currentTimeMillis() - now < 300 * 1000L) {
Thread.sleep(1000);
allCoordinators = QeProcessorImpl.INSTANCE.getAllCoordinators();
LOG.info("waiting {} queries to finish before shutdown", allCoordinators.size());
}
} catch (Throwable t) {
LOG.error("", t);
}
LOG.info("graceful shutdown finished");
}
}