Every stage between the socket and the HDF5 write was bounded by a message count and never by bytes: ZeroMQ receive watermark 100, cbor_fifo 200, outside_fifo 1000, repub_fifo 200 - up to some 1500 images in flight per writer process, each a whole compressed image, so 6-12 GB for a JUNGFRAU 4M and 30-60 GB for a 9M, and the writer's memory was a quantity nobody had chosen. ReceiveRingBuffer is a fixed ring of bytes owned by the ImagePuller base class, so the ZeroMQ and TCP pullers share it. A message is received straight into it (TCP reads the payload into the ring; ZeroMQ copies the message in) and travels as a shared_ptr region in ImagePullerOutput; the deserialised image points into that region, republish sends it as is, and the bytes come back when the last holder lets go. Space is reclaimed in arrival order: a region released out of turn - republish finishes with messages in its own order - is marked and waits for the ones in front of it. Because of that, what republish may hold is capped at a quarter of the ring, and an image over the cap is not republished, exactly as one that finds the republish queue full; otherwise a stalled republish consumer would pin the ring and slow the writer to its rate. A message larger than the whole ring (a START or END of a very long run) goes on the heap, so it cannot wedge the receiver. A full ring stalls the receive thread, and what that does depends on the stream. On TCP the pusher waits behind the writer's BUSY heartbeats and gives up only after a minute in which no image at all was written. On ZeroMQ the pusher's send blocks for one second, then drops the image and sends without waiting for the rest of the run - so on ZeroMQ a ring that is smaller than the writer's stalls costs images, where the old 1500-message queues absorbed them. That is the trade: the budget is now a number, and it has to be chosen. jfjoch_writer sets it with -m/--rcv_ring_mb, default 2048 MB per writer process; the ZeroMQ receive watermark default goes from 100 to 10 messages, since it now only counts what sits beyond the ring. The ring's peak use is in the end-of-run log line, and docs/JFJOCH_WRITER.md has a Memory section with the sizing rule. The puller's own default is no ring at all, which keeps every other user as it was: the broker's DECTRIS stream2 puller (JFJochServices, JFJochReceiverService) stays zero-copy with the ZeroMQ message as the buffer, and the broker's direct HDF5 writer (HDF5FilePusher -> FileWriter) never goes through a puller. The puller threads now also catch std::exception, as the writer does since the previous commit: deserialising the END message of a million-image run allocates a vector per per-image array, and a bad_alloc leaving the thread ended the process. Tests: ReceiveRingBufferTest (reuse, wrap-around, full ring, out-of-order release, heap fallback, zero size, stop, a region outliving its ring); ZMQImageCommTest_SmallRingBuffer (64 one-megabyte images through a three-image ring, nothing lost, peak within the bound); one of the two writers in TCPImageCommTest_2Writers_WithAck runs on a three-image ring. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
63 lines
2.0 KiB
C++
63 lines
2.0 KiB
C++
// SPDX-FileCopyrightText: 2025 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#pragma once
|
|
|
|
#include <thread>
|
|
#include <atomic>
|
|
#include <mutex>
|
|
#include <memory>
|
|
#include "ImagePuller.h"
|
|
#include "../common/Logger.h"
|
|
#include "../common/ThreadSafeFIFO.h"
|
|
|
|
class TCPImagePuller : public ImagePuller {
|
|
int fd = -1;
|
|
std::mutex fd_mutex;
|
|
|
|
std::string addr;
|
|
std::string host;
|
|
uint16_t port = 0;
|
|
std::optional<int32_t> receive_buffer_size;
|
|
std::atomic<bool> disconnect{false};
|
|
|
|
ThreadSafeFIFO<ImagePullerOutput> cbor_fifo{200};
|
|
ThreadSafeFIFO<ImagePullerOutput> repub_fifo{200};
|
|
|
|
std::unique_ptr<ZMQSocket> repub_socket;
|
|
|
|
std::thread receiver_thread;
|
|
std::thread cbor_thread;
|
|
std::thread repub_thread;
|
|
std::thread heartbeat_thread;
|
|
|
|
// Serializes all writer->pusher sends (ACKs, keepalive pongs, busy heartbeats)
|
|
// so that concurrently-sent frames cannot interleave on the socket.
|
|
std::mutex send_mutex;
|
|
|
|
Logger logger{"TCPImagePuller"};
|
|
|
|
static constexpr uint32_t default_repub_watermark = 220;
|
|
static constexpr auto RepubTimeout = std::chrono::milliseconds(100);
|
|
static constexpr auto HeartbeatInterval = std::chrono::milliseconds(250);
|
|
|
|
bool ReadExact(void *buf, size_t size);
|
|
bool SendAll(const void *buf, size_t len);
|
|
bool EnsureConnected();
|
|
void CloseSocket();
|
|
void ReceiverThread();
|
|
void CBORThread();
|
|
void RepubThread();
|
|
void HeartbeatThread();
|
|
public:
|
|
explicit TCPImagePuller(const std::string &tcp_addr,
|
|
std::optional<int32_t> rcv_buffer_size = {},
|
|
const std::string &repub_address = "",
|
|
const std::optional<int32_t> &repub_watermark = {},
|
|
const std::optional<size_t> &ring_size = {});
|
|
|
|
~TCPImagePuller() override;
|
|
bool SupportsAck() const override { return true; }
|
|
bool SendAck(const PullerAckMessage &ack) override;
|
|
void Disconnect() override;
|
|
}; |