-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.cpp
More file actions
250 lines (208 loc) · 8.4 KB
/
Copy pathserver.cpp
File metadata and controls
250 lines (208 loc) · 8.4 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
// Mini2 gRPC Server Main
// Phase 1: Ping, Query (local), Forward
#include <iostream>
#include <memory>
#include <chrono>
#include <cstddef>
#include <filesystem>
#include <stdexcept>
#include <string>
#include <yaml-cpp/yaml.h>
#include <grpcpp/server.h>
#include <grpcpp/server_builder.h>
#include <grpcpp/security/server_credentials.h>
#include "InsertRouteConfig.h"
#include "Mini2ServiceImpl.h"
using grpc::Server;
using grpc::ServerBuilder;
namespace fs = std::filesystem;
namespace {
constexpr int kMaxGrpcMessageBytes = 64 * 1024 * 1024;
fs::path ResolveConfigPath(const std::string& node_id) {
const fs::path relative = fs::path("config") / ("node_" + node_id + ".yaml");
if (fs::exists(relative)) {
return relative;
}
const fs::path source_relative = fs::path(MINI2_SOURCE_DIR) / relative;
if (fs::exists(source_relative)) {
return source_relative;
}
return relative;
}
fs::path ResolveDatasetPath(const YAML::Node& config, const fs::path& config_path) {
const fs::path configured = config["dataset_path"].as<std::string>();
if (configured.is_absolute()) {
return configured;
}
const fs::path from_cwd = fs::current_path() / configured;
if (fs::exists(from_cwd)) {
return from_cwd;
}
const fs::path from_source_dir = fs::path(MINI2_SOURCE_DIR) / configured;
if (fs::exists(from_source_dir)) {
return from_source_dir;
}
return config_path.parent_path() / configured;
}
std::string ResolveOptionalPath(
const YAML::Node& config,
const char* key,
const fs::path& config_path) {
if (!config[key]) {
return "";
}
const fs::path configured = config[key].as<std::string>();
if (configured.is_absolute()) {
return configured.string();
}
const fs::path from_cwd = fs::current_path() / configured;
if (fs::exists(from_cwd)) {
return from_cwd.string();
}
const fs::path from_source_dir = fs::path(MINI2_SOURCE_DIR) / configured;
if (fs::exists(from_source_dir)) {
return from_source_dir.string();
}
return (config_path.parent_path() / configured).string();
}
DatasetLoadMode ParseDatasetLoadMode(const YAML::Node& config) {
const std::string configured =
config["dataset_mode"] ? config["dataset_mode"].as<std::string>() : "both";
if (configured == "aos" || configured == "AOS") {
return DatasetLoadMode::AOS;
}
if (configured == "soa" || configured == "SOA") {
return DatasetLoadMode::SOA;
}
if (configured == "both" || configured == "Both" || configured == "BOTH") {
return DatasetLoadMode::Both;
}
throw std::runtime_error(
"Invalid dataset_mode '" + configured + "'. Expected aos, soa, or both.");
}
QueueMode ParseQueueMode(const YAML::Node& config) {
const std::string configured =
config["queue_mode"] ? config["queue_mode"].as<std::string>() : "fifo";
if (configured == "fifo" || configured == "FIFO") {
return QueueMode::FIFO;
}
if (configured == "priority" || configured == "Priority" ||
configured == "PRIORITY") {
return QueueMode::Priority;
}
throw std::runtime_error(
"Invalid queue_mode '" + configured + "'. Expected fifo or priority.");
}
} // namespace
// ===== Server Startup =====
void RunServer(const std::string& node_id)
{
const auto server_start_begin = std::chrono::steady_clock::now();
const fs::path config_path = ResolveConfigPath(node_id);
try
{
YAML::Node config = YAML::LoadFile(config_path.string());
std::string host = config["host"].as<std::string>();
uint16_t port = config["port"].as<uint16_t>();
bool coordinator_only = config["coordinator_only"] && config["coordinator_only"].as<bool>();
// Cache policy is configured per node in YAML instead of hardcoding node A in C++.
bool enable_cache = config["enable_cache"] && config["enable_cache"].as<bool>();
std::string cache_policy =
config["cache_policy"] ? config["cache_policy"].as<std::string>() : "lru";
std::size_t cache_max_entries =
config["cache_max_entries"] ? config["cache_max_entries"].as<std::size_t>() : 32;
QueueMode queue_mode = ParseQueueMode(config);
std::string server_address = host + ":" + std::to_string(port);
Mini2ServiceImpl service(
node_id,
port,
enable_cache,
cache_policy,
cache_max_entries,
queue_mode);
const InsertRouteConfig insert_route_config =
LoadInsertRouteConfig(config, config_path);
if (insert_route_config.loaded()) {
service.SetInsertRoutes(
insert_route_config.routes,
insert_route_config.default_node_id);
std::cout << "Loaded insert routes from "
<< insert_route_config.path.string() << std::endl;
}
if (config["peers"]) {
std::vector<PeerInfo> peers;
for (auto const& peer_node : config["peers"]) {
PeerInfo p;
p.id = peer_node["id"].as<std::string>();
p.address = peer_node["host"].as<std::string>() + ":" +
std::to_string(peer_node["port"].as<int>());
peers.push_back(p);
}
service.SetPeers(peers);
}
// Initialize dataset
if (!coordinator_only) {
const fs::path dataset_path = ResolveDatasetPath(config, config_path);
const std::string agency_dict_path =
ResolveOptionalPath(config, "agency_dict_path", config_path);
const std::string problem_dict_path =
ResolveOptionalPath(config, "problem_dict_path", config_path);
const std::string borough_dict_path =
ResolveOptionalPath(config, "borough_dict_path", config_path);
const std::string status_dict_path =
ResolveOptionalPath(config, "status_dict_path", config_path);
const DatasetLoadMode dataset_load_mode = ParseDatasetLoadMode(config);
if (!service.Initialize(
dataset_path.string(),
dataset_load_mode,
agency_dict_path,
problem_dict_path,
borough_dict_path,
status_dict_path)) {
std::cerr << "Failed to initialize dataset at node: " << node_id << std::endl;
return;
}
} else {
std::cout << "Node " << node_id << " is coordinator-only, skipping dataset initialization." << std::endl;
}
// Build and start server
ServerBuilder builder;
builder.SetMaxReceiveMessageSize(kMaxGrpcMessageBytes);
builder.SetMaxSendMessageSize(kMaxGrpcMessageBytes);
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
builder.RegisterService(&service);
std::unique_ptr<Server> server(builder.BuildAndStart());
if (!server) {
std::cerr << "Node " << node_id
<< " failed to start gRPC server on "
<< server_address << std::endl;
return;
}
const double server_ready_ms = std::chrono::duration<double, std::milli>(
std::chrono::steady_clock::now() - server_start_begin).count();
std::cout << "\n============================================" << std::endl;
std::cout << "Mini2 gRPC Server started" << std::endl;
std::cout << "[" << node_id << "] Server listening on " << server_address << std::endl;
std::cout << "[" << node_id << "] Cold start server ready in "
<< server_ready_ms << " ms" << std::endl;
std::cout << "============================================\n" << std::endl;
server->Wait();
} catch (const std::exception& e)
{
std::cerr << "Node " << node_id << " failed to load config from "
<< config_path << ": " << e.what() << std::endl;
return;
}
}
// ===== Main Function =====
int main(int argc, char** argv) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <NodeID>" << std::endl;
return 1;
}
std::string node_id = argv[1];
std::cout << "Starting node: " << node_id << std::endl;
RunServer(node_id);
return 0;
}
// Run command: ./build/bin/server A benchmarks/workload_100k.csv