Coverage Report

Created: 2026-06-26 03:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/service/doris_main.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include <arrow/flight/client.h>
19
#include <arrow/flight/sql/client.h>
20
#include <arrow/scalar.h>
21
#include <arrow/status.h>
22
#include <arrow/table.h>
23
#include <butil/macros.h>
24
// IWYU pragma: no_include <bthread/errno.h>
25
#include <errno.h> // IWYU pragma: keep
26
#include <fcntl.h>
27
#include <fmt/core.h>
28
#if !defined(__SANITIZE_ADDRESS__) && !defined(ADDRESS_SANITIZER) && !defined(LEAK_SANITIZER) && \
29
        !defined(THREAD_SANITIZER) && !defined(USE_JEMALLOC)
30
#include <gperftools/malloc_extension.h> // IWYU pragma: keep
31
#endif
32
#include <libgen.h>
33
#include <setjmp.h>
34
#include <signal.h>
35
#include <stdint.h>
36
#include <stdio.h>
37
#include <stdlib.h>
38
#include <unistd.h>
39
40
#include <cstring>
41
#include <functional>
42
#include <memory>
43
#include <ostream>
44
#include <string>
45
#include <string_view>
46
#include <tuple>
47
#include <vector>
48
49
#include "cloud/cloud_backend_service.h"
50
#include "cloud/config.h"
51
#include "common/phdr_cache.h"
52
#include "common/stack_trace.h"
53
#include "runtime/memory/mem_tracker_limiter.h"
54
#include "storage/tablet/tablet_schema_cache.h"
55
#include "storage/utils.h"
56
#include "util/concurrency_stats.h"
57
#include "util/jni-util.h"
58
59
#if defined(LEAK_SANITIZER)
60
#include <sanitizer/lsan_interface.h>
61
#endif
62
63
#include <curl/curl.h>
64
#include <thrift/TOutput.h>
65
66
#include "agent/heartbeat_server.h"
67
#include "common/config.h"
68
#include "common/daemon.h"
69
#include "common/logging.h"
70
#include "common/signal_handler.h"
71
#include "common/status.h"
72
#include "io/cache/block_file_cache_factory.h"
73
#include "runtime/exec_env.h"
74
#include "runtime/user_function_cache.h"
75
#include "service/arrow_flight/flight_sql_service.h"
76
#include "service/backend_options.h"
77
#include "service/backend_service.h"
78
#include "service/http_service.h"
79
#include "service/server/be_server_starter_factory.h"
80
#include "storage/options.h"
81
#include "storage/storage_engine.h"
82
#include "udf/python/python_env.h"
83
#include "util/debug_util.h"
84
#include "util/disk_info.h"
85
#include "util/mem_info.h"
86
#include "util/string_util.h"
87
#include "util/thrift_rpc_helper.h"
88
#include "util/thrift_server.h"
89
#include "util/uid_util.h"
90
91
namespace doris {} // namespace doris
92
93
static void help(const char*);
94
95
extern "C" {
96
void __lsan_do_leak_check();
97
int __llvm_profile_write_file();
98
}
99
100
namespace doris {
101
102
7
void signal_handler(int signal) {
103
7
    if (signal == SIGINT || signal == SIGTERM) {
104
7
        k_doris_exit = true;
105
7
    }
106
7
}
107
108
14
int install_signal(int signo, void (*handler)(int)) {
109
14
    struct sigaction sa;
110
14
    memset(&sa, 0, sizeof(struct sigaction));
111
14
    sa.sa_handler = handler;
112
14
    sigemptyset(&sa.sa_mask);
113
14
    auto ret = sigaction(signo, &sa, nullptr);
114
14
    if (ret != 0) {
115
0
        char buf[64];
116
0
        LOG(ERROR) << "install signal failed, signo=" << signo << ", errno=" << errno
117
0
                   << ", errmsg=" << strerror_r(errno, buf, sizeof(buf));
118
0
    }
119
14
    return ret;
120
14
}
121
122
7
void init_signals() {
123
7
    auto ret = install_signal(SIGINT, signal_handler);
124
7
    if (ret < 0) {
125
0
        exit(-1);
126
0
    }
127
7
    ret = install_signal(SIGTERM, signal_handler);
128
7
    if (ret < 0) {
129
0
        exit(-1);
130
0
    }
131
7
}
132
133
4
static void thrift_output(const char* x) {
134
4
    LOG(WARNING) << "thrift internal message: " << x;
135
4
}
136
137
} // namespace doris
138
139
// These code is referenced from clickhouse
140
// It is used to check the SIMD instructions
141
enum class InstructionFail {
142
    NONE = 0,
143
    SSE3 = 1,
144
    SSSE3 = 2,
145
    SSE4_1 = 3,
146
    SSE4_2 = 4,
147
    POPCNT = 5,
148
    AVX = 6,
149
    AVX2 = 7,
150
    AVX512 = 8,
151
    ARM_NEON = 9
152
};
153
154
0
auto instruction_fail_to_string(InstructionFail fail) {
155
0
    switch (fail) {
156
0
#define ret(x) return std::make_tuple(STDERR_FILENO, x, ARRAY_SIZE(x) - 1)
157
0
    case InstructionFail::NONE:
158
0
        ret("NONE");
159
0
    case InstructionFail::SSE3:
160
0
        ret("SSE3");
161
0
    case InstructionFail::SSSE3:
162
0
        ret("SSSE3");
163
0
    case InstructionFail::SSE4_1:
164
0
        ret("SSE4.1");
165
0
    case InstructionFail::SSE4_2:
166
0
        ret("SSE4.2");
167
0
    case InstructionFail::POPCNT:
168
0
        ret("POPCNT");
169
0
    case InstructionFail::AVX:
170
0
        ret("AVX");
171
0
    case InstructionFail::AVX2:
172
0
        ret("AVX2");
173
0
    case InstructionFail::AVX512:
174
0
        ret("AVX512");
175
0
    case InstructionFail::ARM_NEON:
176
0
        ret("ARM_NEON");
177
0
    }
178
179
0
    LOG(ERROR) << "Unrecognized instruction fail value." << std::endl;
180
0
    exit(-1);
181
0
}
182
183
sigjmp_buf jmpbuf;
184
185
0
void sig_ill_check_handler(int, siginfo_t*, void*) {
186
0
    siglongjmp(jmpbuf, 1);
187
0
}
188
189
/// Check if necessary SSE extensions are available by trying to execute some sse instructions.
190
/// If instruction is unavailable, SIGILL will be sent by kernel.
191
7
void check_required_instructions_impl(volatile InstructionFail& fail) {
192
7
#if defined(__SSE3__)
193
7
    fail = InstructionFail::SSE3;
194
7
    __asm__ volatile("addsubpd %%xmm0, %%xmm0" : : : "xmm0");
195
7
#endif
196
197
7
#if defined(__SSSE3__)
198
7
    fail = InstructionFail::SSSE3;
199
7
    __asm__ volatile("pabsw %%xmm0, %%xmm0" : : : "xmm0");
200
201
7
#endif
202
203
7
#if defined(__SSE4_1__)
204
7
    fail = InstructionFail::SSE4_1;
205
7
    __asm__ volatile("pmaxud %%xmm0, %%xmm0" : : : "xmm0");
206
7
#endif
207
208
7
#if defined(__SSE4_2__)
209
7
    fail = InstructionFail::SSE4_2;
210
7
    __asm__ volatile("pcmpgtq %%xmm0, %%xmm0" : : : "xmm0");
211
7
#endif
212
213
    /// Defined by -msse4.2
214
7
#if defined(__POPCNT__)
215
7
    fail = InstructionFail::POPCNT;
216
7
    {
217
7
        uint64_t a = 0;
218
7
        uint64_t b = 0;
219
7
        __asm__ volatile("popcnt %1, %0" : "=r"(a) : "r"(b) :);
220
7
    }
221
7
#endif
222
223
7
#if defined(__AVX__)
224
7
    fail = InstructionFail::AVX;
225
7
    __asm__ volatile("vaddpd %%ymm0, %%ymm0, %%ymm0" : : : "ymm0");
226
7
#endif
227
228
7
#if defined(__AVX2__)
229
7
    fail = InstructionFail::AVX2;
230
7
    __asm__ volatile("vpabsw %%ymm0, %%ymm0" : : : "ymm0");
231
7
#endif
232
233
#if defined(__AVX512__)
234
    fail = InstructionFail::AVX512;
235
    __asm__ volatile("vpabsw %%zmm0, %%zmm0" : : : "zmm0");
236
#endif
237
238
#if defined(__ARM_NEON__)
239
    fail = InstructionFail::ARM_NEON;
240
#ifndef __APPLE__
241
    __asm__ volatile("vadd.i32  q8, q8, q8" : : : "q8");
242
#endif
243
#endif
244
245
7
    fail = InstructionFail::NONE;
246
7
}
247
248
0
bool write_retry(int fd, const char* data, size_t size) {
249
0
    if (!size) size = strlen(data);
250
251
0
    while (size != 0) {
252
0
        ssize_t res = ::write(fd, data, size);
253
254
0
        if ((-1 == res || 0 == res) && errno != EINTR) return false;
255
256
0
        if (res > 0) {
257
0
            data += res;
258
0
            size -= res;
259
0
        }
260
0
    }
261
262
0
    return true;
263
0
}
264
265
/// Macros to avoid using strlen(), since it may fail if SSE is not supported.
266
#define WRITE_ERROR(data)                                                      \
267
0
    do {                                                                       \
268
0
        static_assert(__builtin_constant_p(data));                             \
269
0
        if (!write_retry(STDERR_FILENO, data, ARRAY_SIZE(data) - 1)) _Exit(1); \
270
0
    } while (false)
271
272
/// Check SSE and others instructions availability. Calls exit on fail.
273
/// This function must be called as early as possible, even before main, because static initializers may use unavailable instructions.
274
7
void check_required_instructions() {
275
7
    struct sigaction sa {};
276
7
    struct sigaction sa_old {};
277
7
    sa.sa_sigaction = sig_ill_check_handler;
278
7
    sa.sa_flags = SA_SIGINFO;
279
7
    auto signal = SIGILL;
280
7
    if (sigemptyset(&sa.sa_mask) != 0 || sigaddset(&sa.sa_mask, signal) != 0 ||
281
7
        sigaction(signal, &sa, &sa_old) != 0) {
282
        /// You may wonder about strlen.
283
        /// Typical implementation of strlen is using SSE4.2 or AVX2.
284
        /// But this is not the case because it's compiler builtin and is executed at compile time.
285
286
0
        WRITE_ERROR("Can not set signal handler\n");
287
0
        _Exit(1);
288
0
    }
289
290
7
    volatile InstructionFail fail = InstructionFail::NONE;
291
292
7
    if (sigsetjmp(jmpbuf, 1)) {
293
0
        WRITE_ERROR("Instruction check fail. The CPU does not support ");
294
0
        if (!std::apply(write_retry, instruction_fail_to_string(fail))) _Exit(1);
295
0
        WRITE_ERROR(" instruction set.\n");
296
0
        WRITE_ERROR(
297
0
                "For example, if your CPU does not support AVX2, you need to rebuild the Doris BE "
298
0
                "with: USE_AVX2=0 sh build.sh --be");
299
0
        _Exit(1);
300
0
    }
301
302
7
    check_required_instructions_impl(fail);
303
304
7
    if (sigaction(signal, &sa_old, nullptr)) {
305
0
        WRITE_ERROR("Can not set signal handler\n");
306
0
        _Exit(1);
307
0
    }
308
7
}
309
310
struct Checker {
311
7
    Checker() { check_required_instructions(); }
312
} checker
313
#ifndef __APPLE__
314
        __attribute__((init_priority(101))) /// Run before other static initializers.
315
#endif
316
        ;
317
318
7
int main(int argc, char** argv) {
319
7
    doris::signal::InstallFailureSignalHandler();
320
    // create StackTraceCache Instance, at the beginning, other static destructors may use.
321
7
    StackTrace::createCache();
322
    // extern doris::ErrorCode::ErrorCodeInitializer error_code_init;
323
    // Some developers will modify status.h and we use a very ticky logic to init error_states
324
    // and it maybe not inited. So add a check here.
325
7
    doris::ErrorCode::error_code_init.check_init();
326
    // check if print version or help
327
7
    if (argc > 1) {
328
0
        if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-v") == 0) {
329
0
            puts(doris::get_build_version(false).c_str());
330
0
            exit(0);
331
0
        } else if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0) {
332
0
            help(basename(argv[0]));
333
0
            exit(0);
334
0
        }
335
0
    }
336
337
7
    if (getenv("DORIS_HOME") == nullptr) {
338
0
        fprintf(stderr, "you need set DORIS_HOME environment variable.\n");
339
0
        exit(-1);
340
0
    }
341
7
    if (getenv("PID_DIR") == nullptr) {
342
0
        fprintf(stderr, "you need set PID_DIR environment variable.\n");
343
0
        exit(-1);
344
0
    }
345
346
7
    SCOPED_INIT_THREAD_CONTEXT();
347
348
7
    using doris::Status;
349
7
    using std::string;
350
351
    // open pid file, obtain file lock and save pid
352
7
    string pid_file = string(getenv("PID_DIR")) + "/be.pid";
353
7
    int fd = open(pid_file.c_str(), O_RDWR | O_CREAT,
354
7
                  S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
355
7
    if (fd < 0) {
356
0
        fprintf(stderr, "fail to create pid file.");
357
0
        exit(-1);
358
0
    }
359
360
7
    string pid = std::to_string((long)getpid());
361
7
    pid += "\n";
362
7
    size_t length = write(fd, pid.c_str(), pid.size());
363
7
    if (length != pid.size()) {
364
0
        fprintf(stderr, "fail to save pid into pid file.");
365
0
        exit(-1);
366
0
    }
367
368
    // descriptor will be leaked when failing to close fd
369
7
    if (::close(fd) < 0) {
370
0
        fprintf(stderr, "failed to close fd of pidfile.");
371
0
        exit(-1);
372
0
    }
373
374
    // init config.
375
    // the config in be_custom.conf will overwrite the config in be.conf
376
    // Must init custom config after init config, separately.
377
    // Because the path of custom config file is defined in be.conf
378
7
    string conffile = string(getenv("DORIS_HOME")) + "/conf/be.conf";
379
7
    if (!doris::config::init(conffile.c_str(), true, true, true)) {
380
0
        fprintf(stderr, "error read config file. \n");
381
0
        return -1;
382
0
    }
383
384
7
    string custom_conffile = doris::config::custom_config_dir + "/be_custom.conf";
385
7
    if (!doris::config::init(custom_conffile.c_str(), true, false, false)) {
386
0
        fprintf(stderr, "error read custom config file. \n");
387
0
        return -1;
388
0
    }
389
390
    // ATTN: Callers that want to override default gflags variables should do so before calling this method
391
7
    google::ParseCommandLineFlags(&argc, &argv, true);
392
    // ATTN: MUST init before LOG
393
7
    doris::init_glog("be");
394
395
7
    LOG(INFO) << doris::get_version_string(false);
396
397
7
    doris::init_thrift_logging();
398
399
7
    if (doris::config::enable_fuzzy_mode) {
400
7
        Status status = doris::config::set_fuzzy_configs();
401
7
        if (!status.ok()) {
402
0
            LOG(WARNING) << "Failed to initialize fuzzy config: " << status;
403
0
            exit(1);
404
0
        }
405
7
    }
406
407
#if !defined(__SANITIZE_ADDRESS__) && !defined(ADDRESS_SANITIZER) && !defined(LEAK_SANITIZER) && \
408
        !defined(THREAD_SANITIZER) && !defined(USE_JEMALLOC)
409
    // Change the total TCMalloc thread cache size if necessary.
410
    const size_t kDefaultTotalThreadCacheBytes = 1024 * 1024 * 1024;
411
    if (!MallocExtension::instance()->SetNumericProperty("tcmalloc.max_total_thread_cache_bytes",
412
                                                         kDefaultTotalThreadCacheBytes)) {
413
        fprintf(stderr, "Failed to change TCMalloc total thread cache size.\n");
414
        return -1;
415
    }
416
#endif
417
418
7
    std::vector<doris::StorePath> paths;
419
7
    auto olap_res = doris::parse_conf_store_paths(doris::config::storage_root_path, &paths);
420
7
    if (!olap_res) {
421
0
        LOG(ERROR) << "parse config storage path failed, path=" << doris::config::storage_root_path;
422
0
        exit(-1);
423
0
    }
424
425
7
    std::vector<doris::StorePath> spill_paths;
426
7
    if (doris::config::spill_storage_root_path.empty()) {
427
7
        doris::config::spill_storage_root_path = doris::config::storage_root_path;
428
7
    }
429
7
    olap_res = doris::parse_conf_store_paths(doris::config::spill_storage_root_path, &spill_paths);
430
7
    if (!olap_res) {
431
0
        LOG(ERROR) << "parse config spill storage path failed, path="
432
0
                   << doris::config::spill_storage_root_path;
433
0
        exit(-1);
434
0
    }
435
7
    std::set<std::string> broken_paths;
436
7
    doris::parse_conf_broken_store_paths(doris::config::broken_storage_path, &broken_paths);
437
438
7
    auto it = paths.begin();
439
18
    for (; it != paths.end();) {
440
11
        if (broken_paths.count(it->path) > 0) {
441
0
            if (doris::config::ignore_broken_disk) {
442
0
                LOG(WARNING) << "ignore broken disk, path = " << it->path;
443
0
                it = paths.erase(it);
444
0
            } else {
445
0
                LOG(ERROR) << "a broken disk is found " << it->path;
446
0
                exit(-1);
447
0
            }
448
11
        } else if (!doris::check_datapath_rw(it->path)) {
449
0
            if (doris::config::ignore_broken_disk) {
450
0
                LOG(WARNING) << "read write test file failed, path=" << it->path;
451
0
                it = paths.erase(it);
452
0
            } else {
453
0
                LOG(ERROR) << "read write test file failed, path=" << it->path;
454
                // if only one disk and the disk is full, also need exit because rocksdb will open failed
455
0
                exit(-1);
456
0
            }
457
11
        } else {
458
11
            ++it;
459
11
        }
460
11
    }
461
462
7
    if (paths.empty()) {
463
0
        LOG(ERROR) << "All disks are broken, exit.";
464
0
        exit(-1);
465
0
    }
466
467
7
    it = spill_paths.begin();
468
18
    for (; it != spill_paths.end();) {
469
11
        if (!doris::check_datapath_rw(it->path)) {
470
0
            if (doris::config::ignore_broken_disk) {
471
0
                LOG(WARNING) << "read write test file failed, path=" << it->path;
472
0
                it = spill_paths.erase(it);
473
0
            } else {
474
0
                LOG(ERROR) << "read write test file failed, path=" << it->path;
475
0
                exit(-1);
476
0
            }
477
11
        } else {
478
11
            ++it;
479
11
        }
480
11
    }
481
7
    if (spill_paths.empty()) {
482
0
        LOG(ERROR) << "All spill disks are broken, exit.";
483
0
        exit(-1);
484
0
    }
485
486
    // initialize libcurl here to avoid concurrent initialization
487
7
    auto curl_ret = curl_global_init(CURL_GLOBAL_ALL);
488
7
    if (curl_ret != 0) {
489
0
        LOG(ERROR) << "fail to initialize libcurl, curl_ret=" << curl_ret;
490
0
        exit(-1);
491
0
    }
492
    // add logger for thrift internal
493
7
    apache::thrift::GlobalOutput.setOutputFunction(doris::thrift_output);
494
495
7
    Status status = Status::OK();
496
7
    if (doris::config::enable_java_support) {
497
        // Init jni
498
7
        status = doris::Jni::Util::Init();
499
7
        if (!status.ok()) {
500
0
            LOG(WARNING) << "Failed to initialize JNI: " << status;
501
0
            exit(1);
502
7
        } else {
503
7
            LOG(INFO) << "Doris backend JNI is initialized.";
504
7
        }
505
7
    }
506
507
7
    if (doris::config::enable_python_udf_support) {
508
7
        if (std::string python_udf_root_path =
509
7
                    fmt::format("{}/lib/udf/python", std::getenv("DORIS_HOME"));
510
7
            !std::filesystem::exists(python_udf_root_path)) {
511
3
            std::filesystem::create_directories(python_udf_root_path);
512
3
        }
513
514
        // Normalize and trim all Python-related config parameters
515
7
        std::string python_env_mode =
516
7
                std::string(doris::trim(doris::to_lower(doris::config::python_env_mode)));
517
7
        std::string python_conda_root_path =
518
7
                std::string(doris::trim(doris::config::python_conda_root_path));
519
7
        std::string python_venv_root_path =
520
7
                std::string(doris::trim(doris::config::python_venv_root_path));
521
7
        std::string python_venv_interpreter_paths =
522
7
                std::string(doris::trim(doris::config::python_venv_interpreter_paths));
523
524
7
        if (python_env_mode == "conda") {
525
1
            if (python_conda_root_path.empty()) {
526
0
                LOG(ERROR)
527
0
                        << "Python conda root path is empty, please set `python_conda_root_path` "
528
0
                           "or set `enable_python_udf_support` to `false`";
529
0
                exit(1);
530
0
            }
531
1
            LOG(INFO) << "Doris backend python version manager is initialized. Python conda "
532
1
                         "root path: "
533
1
                      << python_conda_root_path;
534
1
            status = doris::PythonVersionManager::instance().init(doris::PythonEnvType::CONDA,
535
1
                                                                  python_conda_root_path, "");
536
6
        } else if (python_env_mode == "venv") {
537
6
            if (python_venv_root_path.empty()) {
538
0
                LOG(ERROR)
539
0
                        << "Python venv root path is empty, please set `python_venv_root_path` or "
540
0
                           "set `enable_python_udf_support` to `false`";
541
0
                exit(1);
542
0
            }
543
6
            if (python_venv_interpreter_paths.empty()) {
544
0
                LOG(ERROR)
545
0
                        << "Python interpreter paths is empty, please set "
546
0
                           "`python_venv_interpreter_paths` or set `enable_python_udf_support` to "
547
0
                           "`false`";
548
0
                exit(1);
549
0
            }
550
6
            LOG(INFO) << "Doris backend python version manager is initialized. Python venv "
551
6
                         "root path: "
552
6
                      << python_venv_root_path
553
6
                      << ", python interpreter paths: " << python_venv_interpreter_paths;
554
6
            status = doris::PythonVersionManager::instance().init(doris::PythonEnvType::VENV,
555
6
                                                                  python_venv_root_path,
556
6
                                                                  python_venv_interpreter_paths);
557
6
        } else {
558
0
            status = Status::InvalidArgument(
559
0
                    "Python env mode is invalid, should be `conda` or `venv`. If you don't want to "
560
0
                    "enable the Python UDF function, please set `enable_python_udf_support` to "
561
0
                    "`false`");
562
0
        }
563
564
7
        if (!status.ok()) {
565
0
            LOG(ERROR) << "Failed to initialize python version manager: " << status;
566
0
            exit(1);
567
0
        }
568
7
        LOG(INFO) << doris::PythonVersionManager::instance().to_string();
569
7
    }
570
571
    // Doris own signal handler must be register after jvm is init.
572
    // Or our own sig-handler for SIGINT & SIGTERM will not be chained ...
573
    // https://www.oracle.com/java/technologies/javase/signals.html
574
7
    doris::init_signals();
575
    // ATTN: MUST init before `ExecEnv`, `StorageEngine` and other daemon services
576
    //
577
    //       Daemon ───┬──► StorageEngine ──► ExecEnv ──► Disk/Mem/CpuInfo
578
    //                 │
579
    //                 │
580
    // BackendService ─┘
581
7
    doris::CpuInfo::init();
582
7
    doris::DiskInfo::init();
583
7
    doris::MemInfo::init();
584
585
7
    LOG(INFO) << doris::CpuInfo::debug_string();
586
7
    LOG(INFO) << doris::DiskInfo::debug_string();
587
7
    LOG(INFO) << doris::MemInfo::debug_string();
588
589
    // The BE stack trace signal handler uses libunwind on interrupted thread contexts. Populate an
590
    // initial PHDR snapshot before daemon threads start; normal code still uses glibc's live
591
    // dl_iterate_phdr, and only the signal handler reads this snapshot while unwinding.
592
7
    updatePHDRCache();
593
7
    LOG(INFO) << "PHDR cache enabled: " << hasPHDRCache();
594
7
    if (!doris::BackendOptions::init()) {
595
0
        exit(-1);
596
0
    }
597
598
    // init exec env
599
7
    auto* exec_env(doris::ExecEnv::GetInstance());
600
7
    status = doris::ExecEnv::init(doris::ExecEnv::GetInstance(), paths, spill_paths, broken_paths);
601
7
    if (status != Status::OK()) {
602
0
        std::cerr << "failed to init doris storage engine, res=" << status;
603
0
        return 0;
604
0
    }
605
606
    // Start concurrency stats manager
607
7
    doris::ConcurrencyStatsManager::instance().start();
608
609
    // begin to start services
610
7
    doris::ThriftRpcHelper::setup(exec_env);
611
    // 1. thrift server with be_port
612
7
    std::shared_ptr<doris::BaseBackendService> service;
613
7
    std::function<void(Status&, std::string_view)> stop_work_if_error = [&](Status& status,
614
42
                                                                            std::string_view msg) {
615
42
        if (!status.ok()) {
616
0
            std::cerr << msg << '\n';
617
0
            service->stop_works();
618
0
            exit(-1);
619
0
        }
620
42
    };
621
622
7
    if (doris::config::is_cloud_mode()) {
623
1
        service = std::make_shared<doris::CloudBackendService>(
624
1
                exec_env->storage_engine().to_cloud(), exec_env);
625
6
    } else {
626
6
        service = std::make_shared<doris::BackendService>(exec_env->storage_engine().to_local(),
627
6
                                                          exec_env);
628
6
    }
629
630
7
    std::unique_ptr<doris::server::IServerStarter> backend_thrift_starter;
631
7
    EXIT_IF_ERROR(doris::server::create_backend_thrift_starter(exec_env, doris::config::be_port,
632
7
                                                               service, &backend_thrift_starter));
633
7
    status = backend_thrift_starter->start();
634
7
    stop_work_if_error(status, "Doris BE server did not start correctly, exiting");
635
636
    // 2. brpc service
637
7
    std::unique_ptr<doris::server::IServerStarter> brpc_starter;
638
7
    EXIT_IF_ERROR(doris::server::create_brpc_starter(
639
7
            exec_env, doris::config::brpc_port, doris::config::brpc_num_threads, &brpc_starter));
640
7
    status = brpc_starter->start();
641
7
    stop_work_if_error(status, "BRPC service did not start correctly, exiting");
642
643
    // 3. http service
644
7
    std::unique_ptr<doris::server::IServerStarter> http_starter;
645
7
    EXIT_IF_ERROR(doris::server::create_http_starter(exec_env, doris::config::webserver_port,
646
7
                                                     doris::config::webserver_num_workers,
647
7
                                                     &http_starter));
648
7
    status = http_starter->start();
649
7
    stop_work_if_error(status, "Doris Be http service did not start correctly, exiting");
650
651
    // 4. heart beat server
652
7
    doris::ClusterInfo* cluster_info = exec_env->cluster_info();
653
7
    std::unique_ptr<doris::server::IServerStarter> heartbeat_thrift_starter;
654
7
    status = doris::server::create_heartbeat_thrift_starter(
655
7
            exec_env, doris::config::heartbeat_service_port,
656
7
            doris::config::heartbeat_service_thread_count, cluster_info, &heartbeat_thrift_starter);
657
7
    stop_work_if_error(status, "Heartbeat services did not start correctly, exiting");
658
659
7
    status = heartbeat_thrift_starter->start();
660
7
    stop_work_if_error(status, "Doris BE HeartBeat Service did not start correctly, exiting: " +
661
7
                                       status.to_string());
662
663
    // 5. arrow flight service
664
7
    std::unique_ptr<doris::server::IServerStarter> flight_starter;
665
7
    EXIT_IF_ERROR(doris::server::create_flight_starter(doris::config::arrow_flight_sql_port,
666
7
                                                       &flight_starter));
667
7
    status = flight_starter->start();
668
7
    stop_work_if_error(
669
7
            status, "Arrow Flight Service did not start correctly, exiting, " + status.to_string());
670
671
    // 6. start daemon thread to do clean or gc jobs
672
7
    doris::Daemon daemon;
673
7
    daemon.start();
674
675
7
    exec_env->storage_engine().notify_listeners();
676
677
7
    doris::k_is_server_ready = true;
678
679
4.52k
    while (!doris::k_doris_exit) {
680
#if defined(LEAK_SANITIZER)
681
        __lsan_do_leak_check();
682
#endif
683
4.51k
        sleep(3);
684
4.51k
    }
685
7
    doris::k_is_server_ready = false;
686
7
    LOG(INFO) << "Doris main exiting.";
687
7
#if defined(LLVM_PROFILE)
688
7
    __llvm_profile_write_file();
689
7
    LOG(INFO) << "Flush profile file.";
690
7
#endif
691
    // For graceful shutdown, need to wait for all running queries to stop
692
7
    exec_env->wait_for_all_tasks_done();
693
694
7
    if (!doris::config::enable_graceful_exit_check) {
695
        // If not in memleak check mode, no need to wait all objects de-constructed normally, just exit.
696
        // It will make sure that graceful shutdown can be done definitely.
697
0
        LOG(INFO) << "Doris main exited.";
698
0
        google::FlushLogFiles(google::GLOG_INFO);
699
0
        _exit(0); // Do not call exit(0), it will wait for all objects de-constructed normally
700
0
        return 0;
701
0
    }
702
7
    daemon.stop();
703
7
    flight_starter->stop();
704
7
    flight_starter->join();
705
7
    LOG(INFO) << "Flight server stopped.";
706
7
    heartbeat_thrift_starter->stop();
707
7
    heartbeat_thrift_starter->join();
708
7
    LOG(INFO) << "Heartbeat server stopped";
709
    // TODO(zhiqiang): http_service
710
7
    http_starter->stop();
711
7
    http_starter->join();
712
7
    LOG(INFO) << "Http service stopped";
713
7
    backend_thrift_starter->stop();
714
7
    backend_thrift_starter->join();
715
7
    LOG(INFO) << "Be server stopped";
716
7
    brpc_starter->stop();
717
7
    brpc_starter->join();
718
7
    LOG(INFO) << "Brpc service stopped";
719
7
    service.reset();
720
7
    LOG(INFO) << "Backend Service stopped";
721
7
    exec_env->destroy();
722
7
    LOG(INFO) << "All service stopped, doris main exited.";
723
7
    return 0;
724
7
}
725
726
0
static void help(const char* progname) {
727
0
    printf("%s is the Doris backend server.\n\n", progname);
728
0
    printf("Usage:\n  %s [OPTION]...\n\n", progname);
729
0
    printf("Options:\n");
730
0
    printf("  -v, --version      output version information, then exit\n");
731
0
    printf("  -?, --help         show this help, then exit\n");
732
0
}