-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
109 lines (95 loc) · 2.97 KB
/
Copy pathmain.cpp
File metadata and controls
109 lines (95 loc) · 2.97 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
// main.cpp
// ./imdragonfly
// valgrind ./imdragonfly , 与mimalloc, glog冲突
#include <glog/logging.h>
// cd programs/ImDragonfly
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <memory>
#include <new>
#include <string>
#include <thread>
#include "io/fd_wrapper.hpp"
#include "src/server/redis_server.hpp"
#include "src/util/json_config.hpp"
#include "src/util/startup_log.hpp"
using namespace dfly;
// ASAN对协程有误报,注意一下
int main(int argc, char* argv[]) {
std::set_new_handler([]() noexcept {
std::fputs("out of memory: operator new failed\n", stderr);
std::fflush(stderr);
std::abort();
});
// google::ParseCommandLineFlags(&argc, &argv, true); 没有引入#include
// <gflags/gflags.h>,所以不可用
FLAGS_logtostderr = false; // 常规日志只写日志文件
FLAGS_alsologtostderr = false; // 终端仅打印启动信息(见 util::StartupLog)
FLAGS_minloglevel = 0;
#ifndef NDEBUG
FLAGS_logbufsecs = 0;
#endif
google::InitGoogleLogging(argv[0]);
int ret = mkdir("./logs", 0755);
if (ret != 0 && errno != EEXIST) {
LOG(ERROR) << "Failed to create logs directory: " << strerror(errno);
google::ShutdownGoogleLogging();
return 1;
}
FLAGS_log_dir = "./logs";
FLAGS_logtostderr = false;
LOG(INFO) << "ImDragonfly server starting...";
int num = 4;
uint16_t port = 6379;
std::string config_path;
util::JsonConfig config;
// 命令行参数:./imdragonfly [shards] [port] [--config <path>]
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "--config") {
if (i + 1 < argc) {
config_path = argv[++i];
} else {
LOG(ERROR) << "--config 需要一个文件路径参数";
google::ShutdownGoogleLogging();
return 1;
}
} else if (i == 1) {
num = std::atoi(argv[1]);
} else if (i == 2) {
port = static_cast<uint16_t>(std::atoi(argv[2]));
}
}
// 指定了配置文件则加载,并让配置覆盖命令行参数
const util::JsonConfig* cfg = nullptr;
if (!config_path.empty()) {
std::string err;
if (!config.LoadFromFile(config_path, &err)) {
LOG(ERROR) << "加载配置文件失败: " << err;
google::ShutdownGoogleLogging();
return 1;
}
num = static_cast<int>(config.GetInt("shards", num));
port = static_cast<uint16_t>(config.GetInt("port", port));
cfg = &config;
LOG(INFO) << "已加载配置文件: " << config_path;
}
int listenFd = base::ListenFd(port);
if (listenFd < 0) {
LOG(ERROR) << "Failed to create listen socket";
google::ShutdownGoogleLogging();
return 1;
}
RedisServer::Init(listenFd, num, cfg);
util::StartupLog("RedisServer initialized with " + std::to_string(num) +
" shards");
RedisServer::Instance().Start();
RedisServer::Destroy();
LOG(INFO) << "ImDragonfly server shutting down...";
google::ShutdownGoogleLogging();
return 0;
}