Files
Jungfraujoch/writer/FileWriter.h
T
leonarski_fandClaude Opus 5.5 509a54ef92 Broker: tell a reader which data files of the current collection are closed
So that rugnux can process a collection while it is written, the broker reports each data file once
a writer has closed it and renamed it into place:

- Writer: FileWriter takes a callback, called for every closed data file. StreamWriter uses it on TCP
  to send an acknowledgement with ack_for = FILE_CLOSED (new TCPFrameType 10): file number and the
  images the file holds. TCP protocol version 5. Files closed at END are reported before the END
  acknowledgement.
- LiveCollection (image_pusher): the start message the writer of the master file received, the
  closed files, the end. Fed by the TCP pusher (FILE_CLOSED acknowledgements; end once every writer
  acknowledged END) and by the in-process HDF5 pusher (the FileWriter callback). The ZeroMQ pusher
  has no back channel and offers none.
- Two routes, protected by the dataset's bearer tokens like the other dataset routes:
  - GET /live/start.cbor: that start message;
  - GET /live/events: server-sent events (collection, file, end, superseded) with the history first
    and a keepalive every 10 s. One follower at a time; a second gets 409.
  Documented in jfjoch_api.yaml; they add no schema, so the generated C++ model is unchanged.
- The HTTP server's thread pool is set explicitly (16, up to 64): an event stream holds a thread for
  the whole collection.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SVmAWnzCmRKAXVUCdc4iNi
2026-10-08 15:52:21 +02:00

63 lines
3.2 KiB
C++

// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include <numeric>
#include "HDF5DataFile.h"
#include "../common/JFJochMessages.h"
#include "../common/ZMQWrappers.h"
#include "HDF5NXmx.h"
#include <functional>
#include <unordered_set>
class FileWriter {
FileWriterFormat format = FileWriterFormat::NXmxLegacy;
StartMessage start_message;
std::unique_ptr<NXmx> master_file;
std::vector<std::unique_ptr<HDF5DataFile> > files;
std::vector<HDF5DataFileStatistics> stats;
std::unique_ptr<ZMQSocket> finalized_file_socket;
std::function<void(const HDF5DataFileStatistics &)> file_closed_callback;
std::unordered_set<uint64_t> closed_files;
constexpr static uint64_t close_file_lag_images = 1000;
constexpr static uint64_t default_images_per_file = 1000;
void CreateHDF5MasterFile(const StartMessage& msg);
static void CheckOutputFilesAvailable(const StartMessage &msg, FileWriterFormat format);
void AddStats(const std::optional<HDF5DataFileStatistics>& s);
void CloseFile(uint64_t file_number);
void CloseOldFiles(uint64_t current_image_number);
public:
// check_overwrite_at_start: fail the constructor if an output file already
// exists and overwrite is off. Only safe for transports with a back-channel
// (direct HDF5 pusher, TCP) that can report the failure to the broker before
// acquisition arms. The ZeroMQ pusher is fire-and-forget with no back-channel,
// so it must pass false: the writer then falls back to writing .tmp files and
// failing at the final rename (see docs/JFJOCH_WRITER.md).
// trusted_path: the caller supplied the output prefix itself (offline rugnux / the viewer's
// processing), so the multi-user CheckPath guard - which forbids absolute paths and '..' traversal
// for remotely-supplied prefixes - is skipped, allowing an absolute output path. The broker/writer
// never set it, so their behaviour is unchanged.
explicit FileWriter(const StartMessage &request, bool check_overwrite_at_start = true,
bool trusted_path = false);
// Everything the constructor does before it opens anything: the path guard, creating the
// output directory and checking that no output file is in the way. Sent to the writer as a
// PREFLIGHT frame before the detector is armed, so a run that cannot be written is refused
// while refusing it is still free. Shares its code with the constructor, so a pre-flight that
// passes cannot be contradicted by the start that follows it.
static void Preflight(const StartMessage &request, bool trusted_path = false);
void Write(const DataMessage& msg);
void WriteHDF5(const DataMessage& msg);
void WriteHDF5(const CompressedImage& msg);
void WriteHDF5(const EndMessage& msg);
std::vector<HDF5DataFileStatistics> Finalize();
void SetupFinalizedFileSocket(const std::string &addr);
// Called for every data file once it is closed and renamed into place.
void FileClosedCallback(std::function<void(const HDF5DataFileStatistics &)> callback);
std::optional<std::string> GetZMQAddr();
};