ps/ps_server.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <csignal> | ||
| 2 | #include <iostream> | ||
| 3 | #include <fstream> | ||
| 4 | #include <string> | ||
| 5 | #include <algorithm> | ||
| 6 | #include <cstdlib> | ||
| 7 | |||
| 8 | #include <gflags/gflags.h> | ||
| 9 | |||
| 10 | #include "base/base.h" | ||
| 11 | #include "base/factory.h" | ||
| 12 | #include "base/init.h" | ||
| 13 | #include "base/json.h" | ||
| 14 | #include "ps/base/base_ps_server.h" | ||
| 15 | #include "recstore_config.h" | ||
| 16 | #include "src/base/config.h" | ||
| 17 | |||
| 18 | #ifdef ENABLE_PERF_REPORT | ||
| 19 | # include <chrono> | ||
| 20 | # include "base/report/report_client.h" | ||
| 21 | #endif | ||
| 22 | |||
| 23 | #ifdef ENABLE_GPERF_PROFILING | ||
| 24 | # include <gperftools/profiler.h> | ||
| 25 | #endif | ||
| 26 | |||
| 27 | DECLARE_string(config_path); | ||
| 28 | DECLARE_string(brpc_config_path); | ||
| 29 | |||
| 30 | using recstore::BaseParameterServer; | ||
| 31 | |||
| 32 | #ifdef ENABLE_GPERF_PROFILING | ||
| 33 | void StopProfilerAndExit(int signum) { | ||
| 34 | LOG(INFO) << "Caught signal " << signum << ", stopping gperf profiler."; | ||
| 35 | ProfilerStop(); | ||
| 36 | std::exit(signum); | ||
| 37 | } | ||
| 38 | #endif | ||
| 39 | |||
| 40 | ✗ | static inline std::string ToUpper(std::string s) { | |
| 41 | ✗ | std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { | |
| 42 | ✗ | return std::toupper(c); | |
| 43 | }); | ||
| 44 | ✗ | return s; | |
| 45 | } | ||
| 46 | |||
| 47 | ✗ | int main(int argc, char** argv) { | |
| 48 | ✗ | base::Init(&argc, &argv); | |
| 49 | |||
| 50 | #ifdef ENABLE_GPERF_PROFILING | ||
| 51 | std::signal(SIGINT, StopProfilerAndExit); | ||
| 52 | std::signal(SIGTERM, StopProfilerAndExit); | ||
| 53 | |||
| 54 | struct ProfilerGuard { | ||
| 55 | const char* profile; | ||
| 56 | ProfilerGuard() : profile(std::getenv("CPUPROFILE")) { | ||
| 57 | if (profile) { | ||
| 58 | ProfilerStart(profile); | ||
| 59 | LOG(INFO) << "gperftools CPU profiling started, outputting to " | ||
| 60 | << profile; | ||
| 61 | } | ||
| 62 | } | ||
| 63 | ~ProfilerGuard() { | ||
| 64 | if (profile) { | ||
| 65 | ProfilerStop(); | ||
| 66 | LOG(INFO) << "gperftools CPU profiling stopped."; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | } profiler_guard; | ||
| 70 | #endif | ||
| 71 | |||
| 72 | ✗ | std::string cfg_path = FLAGS_config_path; | |
| 73 | { | ||
| 74 | ✗ | if (!cfg_path.empty()) { | |
| 75 | ✗ | std::ifstream test(cfg_path); | |
| 76 | ✗ | if (!test.good()) { | |
| 77 | ✗ | cfg_path.clear(); | |
| 78 | } | ||
| 79 | ✗ | } | |
| 80 | ✗ | if (cfg_path.empty() && !FLAGS_brpc_config_path.empty()) { | |
| 81 | ✗ | std::ifstream test_b(FLAGS_brpc_config_path); | |
| 82 | ✗ | if (test_b.good()) { | |
| 83 | ✗ | cfg_path = FLAGS_brpc_config_path; | |
| 84 | } | ||
| 85 | ✗ | } | |
| 86 | ✗ | if (cfg_path.empty()) { | |
| 87 | try { | ||
| 88 | ✗ | cfg_path = base::ResolveRecStoreConfigPath().string(); | |
| 89 | ✗ | } catch (const std::exception& e) { | |
| 90 | ✗ | LOG(ERROR) << e.what(); | |
| 91 | ✗ | return 1; | |
| 92 | ✗ | } | |
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | ✗ | std::ifstream config_file(cfg_path); | |
| 97 | ✗ | if (!config_file.good()) { | |
| 98 | ✗ | LOG(ERROR) << "Failed to open config file: " << cfg_path; | |
| 99 | ✗ | return 1; | |
| 100 | } | ||
| 101 | |||
| 102 | ✗ | json config; | |
| 103 | ✗ | config_file >> config; | |
| 104 | |||
| 105 | ✗ | std::string ps_type = "GRPC"; | |
| 106 | try { | ||
| 107 | ✗ | if (config.contains("cache_ps") && config["cache_ps"].contains("ps_type")) { | |
| 108 | ✗ | ps_type = config["cache_ps"]["ps_type"].get<std::string>(); | |
| 109 | } | ||
| 110 | ✗ | } catch (...) { | |
| 111 | ✗ | } | |
| 112 | |||
| 113 | ✗ | std::string key; | |
| 114 | ✗ | std::string type_upper = ToUpper(ps_type); | |
| 115 | ✗ | if (type_upper == "GRPC") { | |
| 116 | ✗ | key = "GRPCParameterServer"; | |
| 117 | ✗ | } else if (type_upper == "BRPC") { | |
| 118 | ✗ | key = "BRPCParameterServer"; | |
| 119 | } else { | ||
| 120 | ✗ | LOG(ERROR) << "Unknown ps_type: " << ps_type << ", expected GRPC or BRPC"; | |
| 121 | ✗ | return 2; | |
| 122 | } | ||
| 123 | |||
| 124 | ✗ | LOG(INFO) << "Using ps_type: " << type_upper << " (key=" << key << ")"; | |
| 125 | ✗ | LOG(INFO) << "Parameter server config: " << config.dump(2); | |
| 126 | |||
| 127 | std::unique_ptr<BaseParameterServer> server( | ||
| 128 | ✗ | base::Factory<BaseParameterServer>::NewInstance(key)); | |
| 129 | try { | ||
| 130 | #ifdef ENABLE_PERF_REPORT | ||
| 131 | auto start_time = std::chrono::high_resolution_clock::now(); | ||
| 132 | #endif | ||
| 133 | ✗ | server->Init(config); | |
| 134 | #ifdef ENABLE_PERF_REPORT | ||
| 135 | auto end_time = std::chrono::high_resolution_clock::now(); | ||
| 136 | auto duration = | ||
| 137 | std::chrono::duration_cast<std::chrono::microseconds>( | ||
| 138 | end_time - start_time) | ||
| 139 | .count(); | ||
| 140 | report( | ||
| 141 | "server_latency", "Init", "latency_us", static_cast<double>(duration)); | ||
| 142 | #endif | ||
| 143 | ✗ | server->Run(); | |
| 144 | ✗ | } catch (const std::exception& e) { | |
| 145 | ✗ | std::cerr << "FATAL: Uncaught exception in ps_server: " << e.what() | |
| 146 | ✗ | << std::endl; | |
| 147 | ✗ | LOG(FATAL) << "Uncaught exception in ps_server: " << e.what(); | |
| 148 | return 1; | ||
| 149 | ✗ | } catch (...) { | |
| 150 | ✗ | std::cerr << "FATAL: Unknown exception in ps_server" << std::endl; | |
| 151 | ✗ | LOG(FATAL) << "Unknown exception in ps_server"; | |
| 152 | return 1; | ||
| 153 | ✗ | } | |
| 154 | |||
| 155 | ✗ | return 0; | |
| 156 | ✗ | } | |
| 157 |