OdbDesign
OdbDesign ODB++ Parser - C++ library, gRPC/REST server, CLI, tests
 
Loading...
Searching...
No Matches
StreamSpoolCache.cpp
Go to the documentation of this file.
1#include "StreamSpoolCache.h"
2#include <Logger.h>
3
4#include <atomic>
5#include <chrono>
6#include <system_error>
7
8#if defined(_WIN32)
9#include <process.h>
10#else
11#include <unistd.h>
12#endif
13
14namespace OdbDesignServer
15{
16 namespace Services
17 {
18 namespace
19 {
20 // 64-bit FNV-1a: collision-free-enough namespacing of spool file
21 // names (not a security boundary — the design name may contain
22 // filesystem-hostile characters).
23 std::uint64_t fnv1a64(const std::string& s)
24 {
25 std::uint64_t hash = 14695981039346656037ull;
26 for (const char c : s)
27 {
28 hash ^= static_cast<std::uint64_t>(static_cast<unsigned char>(c));
29 hash *= 1099511628211ull;
30 }
31 return hash;
32 }
33
34 // Filesystem-safe projection of the design name (kept short; the
35 // FNV suffix disambiguates).
36 std::string sanitize_name(const std::string& designName)
37 {
38 std::string out;
39 out.reserve(designName.size() < 48 ? designName.size() : 48);
40 for (const char c : designName)
41 {
42 const bool ok = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
43 (c >= '0' && c <= '9') || c == '_' || c == '-' || c == '.';
44 out.push_back(ok ? c : '_');
45 if (out.size() >= 48) break;
46 }
47 return out;
48 }
49
50 unsigned long current_pid()
51 {
52#if defined(_WIN32)
53 return static_cast<unsigned long>(_getpid());
54#else
55 return static_cast<unsigned long>(::getpid());
56#endif
57 }
58
59 void append_varint(std::string& out, std::uint64_t value)
60 {
61 while (value >= 0x80)
62 {
63 out.push_back(static_cast<char>((value & 0x7f) | 0x80));
64 value >>= 7;
65 }
66 out.push_back(static_cast<char>(value));
67 }
68
69 enum class VarintResult
70 {
71 Ok,
72 CleanEof, // no bytes consumed: legitimate end of the record stream
73 Error // truncated varint or malformed (too long) encoding
74 };
75
76 VarintResult read_varint(std::istream& in, std::uint64_t& valueOut)
77 {
78 std::uint64_t value = 0;
79 int shift = 0;
80 for (int i = 0; i < 10; ++i)
81 {
82 const int byte = in.get();
83 if (byte == std::char_traits<char>::eof())
84 {
85 return (i == 0) ? VarintResult::CleanEof : VarintResult::Error;
86 }
87 value |= static_cast<std::uint64_t>(byte & 0x7f) << shift;
88 if ((byte & 0x80) == 0)
89 {
90 valueOut = value;
91 return VarintResult::Ok;
92 }
93 shift += 7;
94 }
95 return VarintResult::Error;
96 }
97
98 // Sanity cap for one record: chunk payloads are bounded by batch
99 // sizes (single-digit MBs); refuse absurd lengths from a corrupt
100 // file instead of allocating them.
101 constexpr std::uint64_t kMaxRecordBytes = 512ull * 1024ull * 1024ull;
102
103 // Process-global fill counter: the spool path embeds pid + fillId
104 // so concurrent fills never share a file. pid uniquifies across
105 // processes; the GLOBAL counter (not a per-instance member)
106 // uniquifies same-process cache instances sharing an explicit
107 // spool directory — two instances would otherwise both start at
108 // fill 1 and truncate-open each other's path.
109 std::atomic<std::uint64_t> g_fillCounter{ 0 };
110 }
111
112 // ---- Reader ----
113
114 bool StreamSpoolCache::Reader::Next(google::protobuf::Message& record)
115 {
116 if (m_failed || !m_stream.is_open())
117 {
118 return false;
119 }
120
121 std::uint64_t length = 0;
122 const auto varint = read_varint(m_stream, length);
123 if (varint == VarintResult::CleanEof)
124 {
125 return false;
126 }
127 if (varint == VarintResult::Error || length == 0 || length > kMaxRecordBytes)
128 {
129 m_failed = true;
130 return false;
131 }
132
133 std::string bytes(static_cast<std::size_t>(length), '\0');
134 m_stream.read(bytes.data(), bytes.size());
135 if (static_cast<std::uint64_t>(m_stream.gcount()) != length)
136 {
137 m_failed = true;
138 return false;
139 }
140 if (!record.ParseFromString(bytes))
141 {
142 m_failed = true;
143 return false;
144 }
145 ++m_recordsRead;
146 return true;
147 }
148
149 // ---- Writer ----
150
152 {
153 if (m_openForWrite && !m_committed)
154 {
155 Close(/*commit=*/false);
156 }
157 }
158
160 : m_stream(std::move(other.m_stream)),
161 m_path(std::move(other.m_path)),
162 m_pCache(other.m_pCache),
163 m_designName(std::move(other.m_designName)),
164 m_generation(other.m_generation),
165 m_bytesWritten(other.m_bytesWritten),
166 m_recordsWritten(other.m_recordsWritten),
167 m_openForWrite(other.m_openForWrite),
168 m_committed(other.m_committed),
169 m_publishSucceeded(other.m_publishSucceeded)
170 {
171 other.m_pCache = nullptr;
172 other.m_openForWrite = false;
173 other.m_committed = false;
174 other.m_publishSucceeded = false;
175 }
176
178 {
179 if (this != &other)
180 {
181 if (m_openForWrite && !m_committed)
182 {
183 Close(/*commit=*/false);
184 }
185 m_stream = std::move(other.m_stream);
186 m_path = std::move(other.m_path);
187 m_pCache = other.m_pCache;
188 m_designName = std::move(other.m_designName);
189 m_generation = other.m_generation;
190 m_bytesWritten = other.m_bytesWritten;
191 m_recordsWritten = other.m_recordsWritten;
192 m_openForWrite = other.m_openForWrite;
193 m_committed = other.m_committed;
194 m_publishSucceeded = other.m_publishSucceeded;
195 other.m_pCache = nullptr;
196 other.m_openForWrite = false;
197 other.m_committed = false;
198 other.m_publishSucceeded = false;
199 }
200 return *this;
201 }
202
203 bool StreamSpoolCache::Writer::Append(const google::protobuf::Message& record)
204 {
205 if (!m_openForWrite || !m_stream.is_open())
206 {
207 return false;
208 }
209
210 std::string bytes;
211 if (!record.SerializeToString(&bytes))
212 {
213 return false;
214 }
215
216 std::string framed;
217 framed.reserve(bytes.size() + 10);
218 append_varint(framed, bytes.size());
219 framed.append(bytes);
220
221 m_stream.write(framed.data(), framed.size());
222 if (!m_stream.good())
223 {
224 return false;
225 }
226 m_bytesWritten += framed.size();
227 ++m_recordsWritten;
228 return true;
229 }
230
232 {
233 if (!m_openForWrite || m_committed)
234 {
235 return false;
236 }
237 Close(/*commit=*/true);
238 return m_publishSucceeded;
239 }
240
242 {
243 if (!m_openForWrite || m_committed)
244 {
245 return;
246 }
247 Close(/*commit=*/false);
248 }
249
250 void StreamSpoolCache::Writer::Close(bool commit)
251 {
252 m_openForWrite = false;
253 bool streamOk = true;
254 if (m_stream.is_open())
255 {
256 // ofstream buffers; an I/O error such as ENOSPC typically
257 // surfaces only at flush/close time. A failed flush means the
258 // final bytes never reached the disk — publishing the file
259 // would poison the warm path with a silently truncated spool.
260 m_stream.flush();
261 streamOk = m_stream.good();
262 m_stream.close();
263 }
264
265 if (commit)
266 {
267 m_committed = true;
268 m_publishSucceeded = streamOk && m_pCache != nullptr;
269 if (m_publishSucceeded)
270 {
271 m_publishSucceeded = m_pCache->PublishEntry(m_designName,
272 Entry{ m_path, m_generation, m_bytesWritten, m_recordsWritten, 0 });
273 }
274 else
275 {
276 std::error_code ec;
277 std::filesystem::remove(m_path, ec); // best-effort
278 logerror("StreamSpoolCache: spool flush/close failed for \"" + m_designName +
279 "\"; incomplete spool deleted, not published (next request refills cold)");
280 }
281 }
282 else
283 {
284 std::error_code ec;
285 std::filesystem::remove(m_path, ec); // best-effort temp cleanup
286 }
287 }
288
289 // ---- Cache ----
290
291 StreamSpoolCache::StreamSpoolCache(std::uint64_t maxTotalBytes)
292 {
293 namespace fs = std::filesystem;
294
295 std::error_code ec;
296 const auto base = fs::temp_directory_path(ec);
297 if (ec)
298 {
299 logerror("StreamSpoolCache: cannot resolve temp directory; spooling disabled");
300 return;
301 }
302
303 // Unique per-instance directory (pid + steady ticks);
304 // create_directory fails on collision, so retry with a fresh suffix.
305 const auto pid = current_pid();
306 for (int attempt = 0; attempt < 8; ++attempt)
307 {
308 const auto tick = static_cast<std::uint64_t>(
309 std::chrono::steady_clock::now().time_since_epoch().count()) + attempt;
310 const auto candidate = base / ("odbdesign-spool-" + std::to_string(pid) + "-" + std::to_string(tick));
311 std::error_code createEc;
312 if (fs::create_directory(candidate, createEc) && !createEc)
313 {
314 InitializeDirectory(candidate, /*ownsDirectory=*/true, maxTotalBytes);
315 return;
316 }
317 }
318 logerror("StreamSpoolCache: failed to create spool directory; spooling disabled");
319 }
320
321 StreamSpoolCache::StreamSpoolCache(const std::filesystem::path& directory, std::uint64_t maxTotalBytes)
322 {
323 InitializeDirectory(directory, /*ownsDirectory=*/false, maxTotalBytes);
324 }
325
327 {
328 if (m_ownsDirectory)
329 {
330 std::error_code ec;
331 std::filesystem::remove_all(m_directory, ec); // best-effort
332 return;
333 }
334 // Explicit (caller-owned) directory: still remove the spool files
335 // THIS instance published — the constructor's documented contract.
336 // Without it a clean shutdown leaves every entry behind, and a
337 // restarted service neither indexes nor accounts for them, so
338 // restarts accumulate multi-GB spools outside the configured cap.
339 // Unrelated files are untouched; POSIX unlink-while-open keeps
340 // any concurrent warm reader's fd valid through the removal.
341 std::lock_guard<std::mutex> lock(m_mutex);
342 for (const auto& kv : m_entriesByName)
343 {
344 std::error_code ec;
345 std::filesystem::remove(kv.second.path, ec); // best-effort
346 }
347 }
348
349 void StreamSpoolCache::InitializeDirectory(const std::filesystem::path& directory, bool ownsDirectory, std::uint64_t maxTotalBytes)
350 {
351 namespace fs = std::filesystem;
352 std::error_code ec;
353 fs::create_directories(directory, ec);
354 if (ec || !fs::is_directory(directory))
355 {
356 logerror("StreamSpoolCache: spool directory unusable (" + directory.string() + "); spooling disabled");
357 return;
358 }
359 m_directory = directory;
360 m_ownsDirectory = ownsDirectory;
361 m_maxTotalBytes = maxTotalBytes;
362 m_usable = true;
363 }
364
365 std::filesystem::path StreamSpoolCache::MakeSpoolPath(const std::string& designName, std::uint64_t generation, std::uint64_t fillId) const
366 {
367 auto name = sanitize_name(designName);
368 if (name.empty())
369 {
370 name = "design";
371 }
372 // pid uniquifies across processes sharing an explicit spool
373 // directory; fillId uniquifies concurrent fills within one
374 // process. Without the per-fill suffix two cold fills of the
375 // same (design, generation) would open one path with
376 // std::ios::trunc and interleave their length-delimited writes.
377 return m_directory / (name + "-" + std::to_string(fnv1a64(designName)) + "-" +
378 std::to_string(generation) + "-" +
379 std::to_string(current_pid()) + "-" + std::to_string(fillId) + ".spool");
380 }
381
382 bool StreamSpoolCache::TryOpenReader(const std::string& designName, std::uint64_t generation, Reader& readerOut)
383 {
384 if (!m_usable)
385 {
386 return false;
387 }
388
389 std::filesystem::path path;
390 std::uint64_t expectedRecords = 0;
391 {
392 std::lock_guard<std::mutex> lock(m_mutex);
393 auto findIt = m_entriesByName.find(designName);
394 if (findIt == m_entriesByName.end())
395 {
396 return false;
397 }
398 if (findIt->second.generation != generation)
399 {
400 // Stale spool (the design was invalidated/evicted/re-uploaded
401 // since it was written): drop it and refill cold.
402 std::error_code ec;
403 std::filesystem::remove(findIt->second.path, ec);
404 m_totalBytes -= findIt->second.bytes;
405 m_entriesByName.erase(findIt);
406 return false;
407 }
408 path = findIt->second.path;
409 expectedRecords = findIt->second.records;
410 findIt->second.lastUsed = ++m_useCounter;
411 }
412
413 std::error_code ec;
414 if (!std::filesystem::is_regular_file(path, ec))
415 {
416 // Path-guarded: a concurrent PublishEntry (a different
417 // generation, or a same-generation last-committer-wins fill)
418 // may have swapped in a replacement and unlinked the old path
419 // between the lock-guarded lookup above and this probe; only
420 // the entry still referring to the file we observed may be
421 // dropped.
422 DropStaleEntry(designName, path);
423 return false;
424 }
425
426 readerOut.m_stream.open(path, std::ios::binary);
427 if (!readerOut.m_stream.is_open())
428 {
429 return false;
430 }
431 readerOut.m_recordsRead = 0;
432 // Committed record count: a clean EOF short of it means the file
433 // was truncated AT a record boundary (or emptied) — framing alone
434 // cannot distinguish that from a legitimate end-of-spool, so the
435 // reader carries the count and Truncated() reports the mismatch.
436 readerOut.m_expectedRecords = expectedRecords;
437 readerOut.m_failed = false;
438
439 std::lock_guard<std::mutex> lock(m_mutex);
440 ++m_warmOpens;
441 return true;
442 }
443
444 bool StreamSpoolCache::BeginWrite(const std::string& designName, std::uint64_t generation, Writer& writerOut)
445 {
446 if (!m_usable)
447 {
448 return false;
449 }
450
451 // Each fill writes its OWN file (the path embeds a per-fill
452 // suffix — see MakeSpoolPath): concurrent cold fills of the same
453 // (design, generation) must never share a file, because the
454 // ios::trunc open would interleave their length-delimited writes
455 // and publish a corrupt spool. Publishing is a bookkeeping
456 // insert, not a rename: the last committer wins, and
457 // PublishEntry removes the loser's (distinct) file. A warm
458 // reader already streaming the loser's file is safe on POSIX
459 // (unlink-while-open keeps its fd valid); on Windows the remove
460 // can fail while the handle is open — best-effort, the orphan is
461 // reclaimed when an owned spool directory is torn down.
462 std::uint64_t fillId = 0;
463 {
464 // Process-global counter (not a per-instance member): two
465 // cache instances in one process sharing an explicit spool
466 // directory would otherwise both start at fill 1 and produce
467 // the same pathname, trunc-opening each other's spool. The
468 // pid component in the path already uniquifies across
469 // processes.
470 fillId = ++g_fillCounter;
471 }
472 const auto path = MakeSpoolPath(designName, generation, fillId);
473
474 writerOut.m_stream.open(path, std::ios::binary | std::ios::trunc);
475 if (!writerOut.m_stream.is_open())
476 {
477 return false;
478 }
479 writerOut.m_pCache = this;
480 writerOut.m_path = path;
481 writerOut.m_designName = designName;
482 writerOut.m_generation = generation;
483 writerOut.m_bytesWritten = 0;
484 writerOut.m_recordsWritten = 0;
485 writerOut.m_committed = false;
486 writerOut.m_openForWrite = true;
487 return true;
488 }
489
490 bool StreamSpoolCache::PublishEntry(const std::string& designName, const Entry& entry)
491 {
492 std::lock_guard<std::mutex> lock(m_mutex);
493 Entry published = entry;
494 published.lastUsed = ++m_useCounter;
495
496 // A single entry larger than the whole budget would evict every
497 // other design and still leave the cache over cap indefinitely
498 // (the just-published design is eviction-protected). Reject it
499 // instead: the completed stream simply stays unspooled and every
500 // request for this design refills cold — raise spool_max_bytes
501 // if that design should be warm-servable.
502 if (m_maxTotalBytes != 0 && published.bytes > m_maxTotalBytes)
503 {
504 std::error_code ec;
505 std::filesystem::remove(published.path, ec); // best-effort
506 logwarn("StreamSpoolCache: spool for \"" + designName + "\" (" +
507 std::to_string(published.bytes) + " bytes) exceeds spool_max_bytes (" +
508 std::to_string(m_maxTotalBytes) + "); not published (design serves cold)");
509 return false;
510 }
511
512 auto findIt = m_entriesByName.find(designName);
513 if (findIt != m_entriesByName.end())
514 {
515 std::error_code ec;
516 std::filesystem::remove(findIt->second.path, ec); // best-effort
517 m_totalBytes -= findIt->second.bytes;
518 findIt->second = published;
519 }
520 else
521 {
522 m_entriesByName.emplace(designName, published);
523 }
524 m_totalBytes += published.bytes;
525
526 EvictOverBudgetLocked(designName);
527 return true;
528 }
529
530 void StreamSpoolCache::Drop(const std::string& designName)
531 {
532 std::lock_guard<std::mutex> lock(m_mutex);
533 auto findIt = m_entriesByName.find(designName);
534 if (findIt == m_entriesByName.end())
535 {
536 return;
537 }
538 std::error_code ec;
539 std::filesystem::remove(findIt->second.path, ec); // best-effort
540 m_totalBytes -= findIt->second.bytes;
541 m_entriesByName.erase(findIt);
542 }
543
544 void StreamSpoolCache::DropStaleEntry(const std::string& designName, const std::filesystem::path& expectedPath)
545 {
546 std::lock_guard<std::mutex> lock(m_mutex);
547 auto findIt = m_entriesByName.find(designName);
548 if (findIt == m_entriesByName.end() || findIt->second.path != expectedPath)
549 {
550 return; // replaced or removed since it was observed - leave the new entry alone
551 }
552 std::error_code ec;
553 std::filesystem::remove(findIt->second.path, ec); // best-effort
554 m_totalBytes -= findIt->second.bytes;
555 m_entriesByName.erase(findIt);
556 }
557
558 void StreamSpoolCache::EvictOverBudgetLocked(const std::string& protectedName)
559 {
560 if (m_maxTotalBytes == 0)
561 {
562 return;
563 }
564 while (m_totalBytes > m_maxTotalBytes)
565 {
566 // Least-recently-used victim, never the just-published design.
567 auto victimIt = m_entriesByName.end();
568 for (auto it = m_entriesByName.begin(); it != m_entriesByName.end(); ++it)
569 {
570 if (it->first == protectedName) continue;
571 if (victimIt == m_entriesByName.end() || it->second.lastUsed < victimIt->second.lastUsed)
572 {
573 victimIt = it;
574 }
575 }
576 if (victimIt == m_entriesByName.end())
577 {
578 break; // only the protected entry remains; tolerate over-budget
579 }
580 std::error_code ec;
581 std::filesystem::remove(victimIt->second.path, ec); // best-effort
582 m_totalBytes -= victimIt->second.bytes;
583 m_entriesByName.erase(victimIt);
584 }
585 }
586
587 std::uint64_t StreamSpoolCache::WarmOpens() const
588 {
589 std::lock_guard<std::mutex> lock(m_mutex);
590 return m_warmOpens;
591 }
592
594 {
595 std::lock_guard<std::mutex> lock(m_mutex);
596 return m_totalBytes;
597 }
598
600 {
601 std::lock_guard<std::mutex> lock(m_mutex);
602 return m_entriesByName.size();
603 }
604 }
605}
#define logerror(s)
Definition Logger.h:17
#define logwarn(s)
Definition Logger.h:20
bool Next(google::protobuf::Message &record)
bool Append(const google::protobuf::Message &record)
bool BeginWrite(const std::string &designName, std::uint64_t generation, Writer &writerOut)
void DropStaleEntry(const std::string &designName, const std::filesystem::path &expectedPath)
bool TryOpenReader(const std::string &designName, std::uint64_t generation, Reader &readerOut)
StreamSpoolCache(std::uint64_t maxTotalBytes=kDefaultMaxTotalBytes)
void Drop(const std::string &designName)