Line data Source code
1 : #include "GrpcServiceConfig.h"
2 : #include <algorithm>
3 : #include <cstdint>
4 : #include <fstream>
5 : #include <iostream>
6 : #include <limits>
7 : #include <sstream>
8 : #include <crow/json.h>
9 : #include <filesystem>
10 :
11 : namespace OdbDesignServer
12 : {
13 : namespace Config
14 : {
15 : namespace
16 : {
17 : // Map a config string to a gRPC compression level;
18 : // unknown values fall back to high (matches previous gzip behavior)
19 7 : grpc_compression_level ParseCompressionLevel(const std::string& value)
20 : {
21 7 : if (value == "none") return GRPC_COMPRESS_LEVEL_NONE;
22 6 : if (value == "low") return GRPC_COMPRESS_LEVEL_LOW;
23 4 : if (value == "medium") return GRPC_COMPRESS_LEVEL_MED;
24 2 : if (value == "high") return GRPC_COMPRESS_LEVEL_HIGH;
25 : std::cerr << "WARNING: unknown grpc compression level \"" << value
26 1 : << "\", defaulting to \"high\"" << std::endl;
27 1 : return GRPC_COMPRESS_LEVEL_HIGH;
28 : }
29 : }
30 :
31 34 : GrpcServiceConfig::LoadResult GrpcServiceConfig::LoadFromFile(const std::string& configPath)
32 : {
33 34 : LoadResult result;
34 34 : result.config = std::make_shared<GrpcServiceConfig>();
35 :
36 : // Check if file exists
37 34 : if (!std::filesystem::exists(configPath))
38 : {
39 2 : result.loadedFromFile = false;
40 2 : result.message = "Using default gRPC config (" + configPath + " not found)";
41 2 : return result;
42 : }
43 :
44 : try
45 : {
46 : // Read file contents
47 32 : std::ifstream file(configPath);
48 32 : if (!file.is_open())
49 : {
50 0 : result.loadedFromFile = false;
51 0 : result.message = "Using default gRPC config (could not open " + configPath + ")";
52 0 : return result;
53 : }
54 :
55 32 : std::stringstream buffer;
56 32 : buffer << file.rdbuf();
57 32 : file.close();
58 :
59 32 : std::string jsonContent = buffer.str();
60 32 : if (jsonContent.empty())
61 : {
62 1 : result.loadedFromFile = false;
63 1 : result.message = "Using default gRPC config (" + configPath + " is empty)";
64 1 : return result;
65 : }
66 :
67 : // Parse JSON using Crow
68 31 : auto json = crow::json::load(jsonContent);
69 31 : if (!json)
70 : {
71 1 : result.loadedFromFile = false;
72 1 : result.message = "Using default gRPC config (failed to parse JSON in " + configPath + ")";
73 1 : return result;
74 : }
75 :
76 : // Extract grpc configuration
77 30 : if (json.has("grpc"))
78 : {
79 30 : auto grpcSection = json["grpc"];
80 :
81 : // Extract message size limits
82 30 : if (grpcSection.has("max_receive_message_size_mb"))
83 : {
84 1 : int value = static_cast<int>(grpcSection["max_receive_message_size_mb"].i());
85 : // Clamp to reasonable range: 1MB minimum, 1000MB maximum
86 1 : result.config->max_receive_message_size_mb = std::max(1, std::min(1000, value));
87 : }
88 :
89 30 : if (grpcSection.has("max_send_message_size_mb"))
90 : {
91 1 : int value = static_cast<int>(grpcSection["max_send_message_size_mb"].i());
92 : // Clamp to reasonable range: 1MB minimum, 1000MB maximum
93 1 : result.config->max_send_message_size_mb = std::max(1, std::min(1000, value));
94 : }
95 :
96 : // Extract compression configuration
97 30 : if (grpcSection.has("compression"))
98 : {
99 10 : auto compressionSection = grpcSection["compression"];
100 :
101 : // Legacy boolean/algorithm keys. "enabled": false is honored as a
102 : // one-time migration (-> level "none") so an explicit opt-out never
103 : // silently re-enables compression after upgrade; "enabled": true and
104 : // "algorithm" are superseded by level-based negotiation.
105 10 : const bool hasLevel = compressionSection.has("level");
106 10 : if (compressionSection.has("enabled"))
107 : {
108 3 : const bool enabled = compressionSection["enabled"].b();
109 3 : if (!enabled && !hasLevel)
110 : {
111 1 : result.config->compression_level = GRPC_COMPRESS_LEVEL_NONE;
112 : }
113 : // The disposition must match the gating above: nothing is
114 : // migrated when an explicit "level" is present.
115 : const char* disposition;
116 3 : if (hasLevel)
117 : {
118 1 : disposition = "ignored; the explicit \"level\" applies";
119 : }
120 2 : else if (enabled)
121 : {
122 1 : disposition = "ignored (the \"level\" default applies)";
123 : }
124 : else
125 : {
126 1 : disposition = "migrated to \"level\": \"none\"";
127 : }
128 : std::cerr << "WARNING: deprecated grpc.compression \"enabled\" key "
129 : << disposition
130 3 : << "; use \"level\": \"none|low|medium|high\" instead" << std::endl;
131 : }
132 10 : if (compressionSection.has("algorithm"))
133 : {
134 : std::cerr << "WARNING: deprecated grpc.compression \"algorithm\" key ignored; "
135 3 : << "algorithm selection now follows \"level\"" << std::endl;
136 : }
137 :
138 10 : if (compressionSection.has("level"))
139 : {
140 7 : result.config->compression_level = ParseCompressionLevel(compressionSection["level"].s());
141 : }
142 10 : }
143 :
144 : // Extract batch streaming configuration
145 30 : if (grpcSection.has("batch_streaming"))
146 : {
147 1 : auto batchSection = grpcSection["batch_streaming"];
148 :
149 1 : if (batchSection.has("enabled"))
150 : {
151 1 : result.config->enable_batch_streaming = batchSection["enabled"].b();
152 : }
153 :
154 1 : if (batchSection.has("batch_size"))
155 : {
156 1 : result.config->batch_size = static_cast<int>(batchSection["batch_size"].i());
157 1 : result.config->ClampBatchSize(); // Ensure valid range
158 : }
159 1 : }
160 :
161 : // Extract design streaming configuration (M6)
162 30 : if (grpcSection.has("design_streaming"))
163 : {
164 4 : auto streamSection = grpcSection["design_streaming"];
165 :
166 4 : if (streamSection.has("enabled"))
167 : {
168 1 : result.config->enable_design_streaming = streamSection["enabled"].b();
169 : }
170 :
171 4 : if (streamSection.has("feature_batch_size"))
172 : {
173 3 : int value = static_cast<int>(streamSection["feature_batch_size"].i());
174 3 : result.config->design_stream_feature_batch_size = std::max(100, std::min(10000, value));
175 : }
176 :
177 4 : if (streamSection.has("spool_dir"))
178 : {
179 1 : result.config->stream_spool_dir = streamSection["spool_dir"].s();
180 : }
181 :
182 4 : if (streamSection.has("spool_max_bytes"))
183 : {
184 : // Clamp negatives to 0 first: Crow's .i() is signed,
185 : // and a raw cast would wrap a negative JSON value to
186 : // a huge uint64_t, silently disabling the cap.
187 2 : const auto value = static_cast<std::uint64_t>(std::max<std::int64_t>(0, streamSection["spool_max_bytes"].i()));
188 2 : result.config->stream_spool_max_bytes = (value == 0)
189 2 : ? std::numeric_limits<std::uint64_t>::max()
190 : : value;
191 : }
192 4 : }
193 :
194 : // Extract thread pool configuration
195 30 : if (grpcSection.has("thread_pool"))
196 : {
197 9 : auto threadSection = grpcSection["thread_pool"];
198 :
199 9 : if (threadSection.has("max_threads"))
200 : {
201 8 : int value = static_cast<int>(threadSection["max_threads"].i());
202 8 : result.config->max_threads = std::max(1, std::min(64, value));
203 : }
204 :
205 9 : if (threadSection.has("min_pollers"))
206 : {
207 6 : int value = static_cast<int>(threadSection["min_pollers"].i());
208 6 : result.config->min_pollers = std::max(1, std::min(16, value));
209 : }
210 9 : }
211 :
212 : // Extract keepalive configuration
213 30 : if (grpcSection.has("keepalive"))
214 : {
215 10 : auto keepaliveSection = grpcSection["keepalive"];
216 :
217 10 : if (keepaliveSection.has("time_s"))
218 : {
219 8 : int value = static_cast<int>(keepaliveSection["time_s"].i());
220 8 : result.config->keepalive_time_s = std::max(1, std::min(3600, value));
221 : }
222 :
223 10 : if (keepaliveSection.has("timeout_s"))
224 : {
225 7 : int value = static_cast<int>(keepaliveSection["timeout_s"].i());
226 7 : result.config->keepalive_timeout_s = std::max(1, std::min(300, value));
227 : }
228 :
229 10 : if (keepaliveSection.has("permit_without_calls"))
230 : {
231 2 : result.config->keepalive_permit_without_calls = keepaliveSection["permit_without_calls"].b();
232 : }
233 10 : }
234 30 : }
235 :
236 30 : result.loadedFromFile = true;
237 30 : result.message = "Loaded gRPC config from: " + configPath;
238 37 : }
239 0 : catch (const std::exception& e)
240 : {
241 : // TODO: Refactor config error handling - preserve partially loaded config instead of silently falling back to defaults
242 : // Issue: When JSON parsing fails, any successfully parsed values are lost
243 : // Proposed: Store config values as parsed, only reset fields that failed, or fail explicitly
244 : // Log exception and return default config
245 0 : result.config = std::make_shared<GrpcServiceConfig>();
246 0 : result.loadedFromFile = false;
247 0 : result.message = "Using default gRPC config (exception loading " + configPath + ": " + e.what() + ")";
248 0 : }
249 0 : catch (...)
250 : {
251 : // TODO: Refactor config error handling - preserve partially loaded config instead of silently falling back to defaults
252 : // Issue: When JSON parsing fails, any successfully parsed values are lost
253 : // Proposed: Store config values as parsed, only reset fields that failed, or fail explicitly
254 : // Log unknown exception and return default config
255 0 : result.config = std::make_shared<GrpcServiceConfig>();
256 0 : result.loadedFromFile = false;
257 0 : result.message = "Using default gRPC config (unknown exception loading " + configPath + ")";
258 0 : }
259 :
260 30 : return result;
261 0 : }
262 : }
263 : }
|