Project
Loading...
Searching...
No Matches
XRootDByteSource.cxx
Go to the documentation of this file.
1// Copyright 2019-2026 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#include "XRootDByteSource.h"
13#include "AODNative/Errors.h"
14#include <XrdCl/XrdClFile.hh>
15#include <XrdCl/XrdClFileSystem.hh>
16#include <XrdCl/XrdClURL.hh>
17#include <XrdCl/XrdClXRootDResponses.hh>
18#include <condition_variable>
19#include <mutex>
20#include <sstream>
21#include <vector>
22
23namespace o2::aod::native
24{
25
26namespace
27{
28// XrdCl's defaults for kXR_readv, used when the server does not tell its own
29// limits: chunks of at most 2097136 bytes, at most 1024 of them per request.
30// Larger ranges are read with plain reads, in pieces of at most kMaxPlainRead:
31// small enough that one request completes well within the request timeout on
32// a slow link, as ROOT's basket-sized reads do.
33constexpr size_t kDefaultVectorChunk = 2097136;
34constexpr size_t kDefaultVectorChunks = 1024;
35constexpr size_t kMaxPlainRead = 8 * 1024 * 1024;
36
39class Completion
40{
41 public:
42 explicit Completion(size_t pending) : mPending(pending) {}
43
44 void done(std::string error)
45 {
46 std::lock_guard lock{mMutex};
47 if (!error.empty() && mError.empty()) {
48 mError = std::move(error);
49 }
50 if (--mPending == 0) {
51 mDone.notify_all();
52 }
53 }
54
57 std::string wait()
58 {
59 std::unique_lock lock{mMutex};
60 mDone.wait(lock, [this] { return mPending == 0; });
61 return mError;
62 }
63
64 private:
65 std::mutex mMutex;
66 std::condition_variable mDone;
67 size_t mPending;
68 std::string mError;
69};
70
72class Reply : public XrdCl::ResponseHandler
73{
74 public:
75 Reply(Completion& completion, size_t expected) : mCompletion(completion), mExpected(expected) {}
76
77 void HandleResponse(XrdCl::XRootDStatus* status, XrdCl::AnyObject* response) override
78 {
79 std::string error;
80 if (!status->IsOK()) {
81 error = status->ToString();
82 } else if (auto got = received(response); got != mExpected) {
83 error = "short read: " + std::to_string(got) + " of " + std::to_string(mExpected) + " bytes";
84 }
85 delete status;
86 delete response;
87 mCompletion.done(std::move(error));
88 delete this;
89 }
90
91 private:
92 static size_t received(XrdCl::AnyObject* response)
93 {
94 if (!response) {
95 return 0;
96 }
97 XrdCl::ChunkInfo* chunk = nullptr;
98 response->Get(chunk);
99 if (chunk) {
100 return chunk->length;
101 }
102 XrdCl::VectorReadInfo* vector = nullptr;
103 response->Get(vector);
104 return vector ? vector->GetSize() : 0;
105 }
106
107 Completion& mCompletion;
108 size_t mExpected;
109};
110
112struct Request {
113 XrdCl::ChunkList chunks;
114 size_t bytes = 0;
115};
116
120void queryVectorLimits(XrdCl::File& file, size_t& chunkMax, size_t& chunksMax)
121{
122 std::string server;
123 if (!file.GetProperty("DataServer", server) || server.empty()) {
124 return;
125 }
126 XrdCl::FileSystem fs{XrdCl::URL{server}};
127 XrdCl::Buffer arg;
128 arg.FromString("readv_ior_max readv_iov_max");
129 XrdCl::Buffer* response = nullptr;
130 auto status = fs.Query(XrdCl::QueryCode::Config, arg, response);
131 if (!status.IsOK() || !response) {
132 delete response;
133 return;
134 }
135 std::istringstream lines{response->ToString()};
136 delete response;
137 std::string ior, iov;
138 if (!std::getline(lines, ior) || !std::getline(lines, iov)) {
139 return;
140 }
141 // An unknown setting is echoed back by name instead of a number.
142 try {
143 if (auto v = std::stoull(ior); v > 0) {
144 chunkMax = std::min<size_t>(v, kDefaultVectorChunk);
145 }
146 if (auto v = std::stoull(iov); v > 0) {
147 chunksMax = std::min<size_t>(v, kDefaultVectorChunks);
148 }
149 } catch (std::exception const&) {
150 }
151}
152} // namespace
153
155 : mName(url.substr(0, url.find('?'))),
156 mFile(std::make_unique<XrdCl::File>()),
157 mVectorChunkMax(kDefaultVectorChunk),
158 mVectorChunksMax(kDefaultVectorChunks),
159 mInFlightMax(options.remoteInFlightBytes)
160{
161 auto status = mFile->Open(url, XrdCl::OpenFlags::Read);
162 if (!status.IsOK()) {
163 throw IoError(mName, status.ToString());
164 }
165 XrdCl::StatInfo* info = nullptr;
166 status = mFile->Stat(false, info);
167 if (!status.IsOK() || !info) {
168 delete info;
169 throw IoError(mName, "stat failed: " + status.ToString());
170 }
171 mSize = info->GetSize();
172 delete info;
173 queryVectorLimits(*mFile, mVectorChunkMax, mVectorChunksMax);
174}
175
177{
178 if (mFile->IsOpen()) {
179 [[maybe_unused]] auto status = mFile->Close();
180 }
181}
182
183void XRootDByteSource::checkRange(uint64_t off, size_t len) const
184{
185 if (off > mSize || len > mSize - off) {
186 throw CorruptData(mName, off, "read of " + std::to_string(len) + " bytes past the end of a " + std::to_string(mSize) + " byte file");
187 }
188}
189
191{
192 ReadRequest request{off, len, dst};
193 readvInto({&request, 1});
194}
195
196void XRootDByteSource::readvInto(std::span<ReadRequest> requests)
197{
198 // Ranges larger than a vector chunk are read with plain reads of at most
199 // kMaxPlainRead; the others are gathered into vectored reads within the
200 // server's limits. No request carries more than the in-flight limit.
201 size_t window = mInFlightMax > 0 ? mInFlightMax : SIZE_MAX;
202 std::vector<Request> out;
203 Request vector;
204 size_t total = 0;
205 auto flushVector = [&] {
206 if (!vector.chunks.empty()) {
207 out.push_back(std::move(vector));
208 vector = Request{};
209 }
210 };
211 for (auto const& r : requests) {
212 checkRange(r.offset, r.length);
213 total += r.length;
214 if (r.length == 0) {
215 continue;
216 }
217 if (r.length > mVectorChunkMax) {
218 for (size_t done = 0; done < r.length; done += kMaxPlainRead) {
219 auto n = std::min(kMaxPlainRead, r.length - done);
220 Request plain;
221 plain.chunks.emplace_back(r.offset + done, (uint32_t)n, (uint8_t*)r.dst + done);
222 plain.bytes = n;
223 out.push_back(std::move(plain));
224 }
225 continue;
226 }
227 if (vector.chunks.size() == mVectorChunksMax || (!vector.chunks.empty() && vector.bytes + r.length > window)) {
228 flushVector();
229 }
230 vector.chunks.emplace_back(r.offset, (uint32_t)r.length, r.dst);
231 vector.bytes += r.length;
232 }
233 flushVector();
234
235 // Send the requests in rounds of at most `window` bytes, waiting for each
236 // round: on a slow link every request then gets its share of the bandwidth,
237 // instead of all of them competing until some expire.
238 size_t calls = 0;
239 for (size_t first = 0; first < out.size();) {
240 size_t last = first, bytes = 0;
241 while (last < out.size() && (last == first || bytes + out[last].bytes <= window)) {
242 bytes += out[last++].bytes;
243 }
244 Completion completion{last - first};
245 for (size_t i = first; i < last; ++i) {
246 auto& request = out[i];
247 auto* reply = new Reply(completion, request.bytes);
248 XrdCl::XRootDStatus status;
249 if (request.chunks.size() == 1) {
250 auto& c = request.chunks.front();
251 status = mFile->Read(c.offset, c.length, c.buffer, reply);
252 } else {
253 status = mFile->VectorRead(request.chunks, nullptr, reply);
254 }
255 if (!status.IsOK()) {
256 delete reply;
257 completion.done(status.ToString());
258 }
259 }
260 auto error = completion.wait();
261 if (!error.empty()) {
262 throw IoError(mName, error);
263 }
264 calls += last - first;
265 first = last;
266 }
267 mReadCalls.fetch_add(calls, std::memory_order_relaxed);
268 mBytesRead.fetch_add(total, std::memory_order_relaxed);
269}
270
271} // namespace o2::aod::native
std::string url
int32_t i
bool done
configStandalone Light OpenGL display display_light Apply smoothing to points Enable Z buffer Highlight links Highlight seeds Highlight sector tracks Highlight final tracks Show individual Show related Highlight grid Show only individual collision Color clusters belonging to track objects Set line Enable drawing TPC data Enable drawing TOF data Enable drawing magnetic field Set field line step count Invert colors configStandalone Heavy OpenGL display display_heavy Show tracks with ITS contribution Show tracks with TOF contribution Enable propagation of loopers Show clusters at nominal x of pad row for early transformed data Split CE tracks when they cross the central Mark clusters Mark adjacent clusters Hide rejected tracks Apply filter on tracks to be displayed Separate increase X h configStandalone Camera window renderer OpenGL display display_camera Orient the camera such that the y axis is always upwards Full Screen MultiSample Anti Downsampling Anti Enable Vertical Sync Target frame or if the backend cannot limit to the refresh Use OpenGL indirect draws to reduce number of draw calls Display h configStandalone Vulkan display display_vulkan Have uniform buffers in host accessible device memory h configStandalone Internal display display_internal Number of updates the Qt GUI may send ahead of the last one the display has before it waits for the display Opacity of the console background Number of console lines the history is shortened to this when it reaches twice the size Time in seconds an info message fades out after setInfoTime h OpenGL display display Font size Do not use Freetype for font Renderer for event Frontend to use for event ROOT macros used as track filter Console script file
Timeframe settings Average distance in cm of events merged into Randomize distance around average distance of merged events Simulate correct bunch interactions instead of placing only the average number of events A value[n] sets TFlen for[n] collisions in Number of bunches per trainsort Number of bunch trains Instantaneous interaction rate Do not simulate border Total number of collisions to be placed in the interior of all time Do not select random but walk over array of events in stride steps h Event generator settings h configStandalone Use CPU for GPU Force usage of the specified GPU device no CPU fallback Continue processing after an error First event to Number of events to Number of iterations to Number of iterations to Number of initial iterations excluded from const char Directory with events to Events directory is and not inside events Show standalone event Run event generator maximum time bin of continuous for triggered for automatic continuous for automatic continuous triggered uint64_t
char error[CURL_ERROR_SIZE]
size_t received
uint32_t c
Definition RawData.h:2
XrdCl::ChunkList chunks
size_t bytes
std::string url() const override
Without the query, which may hold an access token.
void readvInto(std::span< ReadRequest > requests) override
XRootDByteSource(std::string url, Options const &options)
void readInto(uint64_t off, size_t len, void *dst) override
Throws IoError, or CorruptData when the range is past the end of the file.
GLdouble n
Definition glcorearb.h:1982
const GLdouble * v
Definition glcorearb.h:832
GLint first
Definition glcorearb.h:399
GLenum GLenum dst
Definition glcorearb.h:1767
GLenum GLenum GLsizei len
Definition glcorearb.h:4232
std::vector< T, fair::mq::pmr::polymorphic_allocator< T > > vector
std::string to_string(gsl::span< T, Size > span)
Definition common.h:52
Failure of the underlying transport.
Definition Errors.h:69
std::map< std::string, ID > expected