Skip to content

Commit ef8856a

Browse files
authored
Detect failed reads when a tree is missing from a DF (#15978)
1 parent ec214c8 commit ef8856a

1 file changed

Lines changed: 51 additions & 1 deletion

File tree

‎Framework/AnalysisSupport/src/DataInputDirector.cxx‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@
3131

3232
#include "TGrid.h"
3333
#include "TFile.h"
34+
#include "TError.h"
3435

3536
#include <arrow/dataset/file_base.h>
3637
#include <arrow/dataset/dataset.h>
3738
#include <uv.h>
3839
#include <exception>
3940
#include <memory>
41+
#include <string_view>
4042
#include <utility>
4143

4244
#include <dlfcn.h>
@@ -54,6 +56,42 @@ FileNameHolder makeFileNameHolder(std::string fileName)
5456
return holder;
5557
}
5658

59+
namespace
60+
{
61+
// ROOT reports a failed key read (a folder's key list or the object itself) and carries on,
62+
// so the lookup returns nullptr, or an object built from whatever was in the buffer, and a
63+
// failed read looks like a missing object. Count the reports while the watch is alive.
64+
struct RootReadErrorWatch {
65+
RootReadErrorWatch()
66+
: mPrevious(SetErrorHandler(handler))
67+
{
68+
sPrevious = mPrevious;
69+
sErrors = 0;
70+
}
71+
~RootReadErrorWatch()
72+
{
73+
SetErrorHandler(mPrevious);
74+
}
75+
RootReadErrorWatch(RootReadErrorWatch const&) = delete;
76+
RootReadErrorWatch& operator=(RootReadErrorWatch const&) = delete;
77+
78+
[[nodiscard]] bool failed() const { return sErrors > 0; }
79+
80+
static void handler(int level, Bool_t abort, char const* location, char const* msg)
81+
{
82+
if (level >= kError && location && std::string_view{location} == "TKey::ReadFile") {
83+
++sErrors;
84+
}
85+
(sPrevious ? sPrevious : DefaultErrorHandler)(level, abort, location, msg);
86+
}
87+
88+
ErrorHandlerFunc_t mPrevious;
89+
// Errors from other threads are passed on but not counted.
90+
static inline thread_local int sErrors = 0;
91+
static inline ErrorHandlerFunc_t sPrevious = nullptr;
92+
};
93+
} // namespace
94+
5795
DataInputDescriptor::DataInputDescriptor(bool alienSupport, int level, DataInputDirectorContext& context)
5896
: mAlienSupport(alienSupport),
5997
mContext(context),
@@ -603,7 +641,19 @@ try {
603641

604642
for (auto& capability : mFactory.capabilities) {
605643
auto objectPath = capability.lfn2objectPath(fullpath.path());
606-
void* handle = capability.getHandle(rootFS, objectPath);
644+
void* handle = nullptr;
645+
bool readFailed = false;
646+
try {
647+
RootReadErrorWatch watch;
648+
handle = capability.getHandle(rootFS, objectPath);
649+
readFailed = watch.failed();
650+
} catch (...) {
651+
// Sizes from a partial read can be garbage, e.g. giving std::bad_alloc.
652+
std::throw_with_nested(AODReadError(AODReadError::Kind::InvalidTimeframe, fmt::format(R"(Unable to look up "{}" in "{}")", objectPath, mCurrentFile->name())));
653+
}
654+
if (readFailed) {
655+
throw AODReadError(AODReadError::Kind::InvalidTimeframe, fmt::format(R"(Unable to read "{}" from "{}")", objectPath, mCurrentFile->name()));
656+
}
607657
if (handle) {
608658
format = capability.factory().format();
609659
creator = capability.factory().deferredOutputStreamer;

0 commit comments

Comments
 (0)