Skip to content

Commit 5bec4fe

Browse files
authored
AOD: add o2-aod-validator to find what interrupted writes leave behind (#16200)
1 parent 07e0d71 commit 5bec4fe

2 files changed

Lines changed: 193 additions & 0 deletions

File tree

‎Framework/AODMerger/CMakeLists.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ o2_add_executable(strainer
2323
COMPONENT_NAME aod
2424
SOURCES src/aodStrainer.cxx
2525
PUBLIC_LINK_LIBRARIES ROOT::Core ROOT::Net)
26+
27+
o2_add_executable(validator
28+
COMPONENT_NAME aod
29+
SOURCES src/aodValidator.cxx
30+
PUBLIC_LINK_LIBRARIES ROOT::Core ROOT::Net ROOT::Tree)
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
// Copyright 2019-2020 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 <algorithm>
13+
#include <cstring>
14+
#include <fstream>
15+
#include <memory>
16+
#include <string>
17+
#include <vector>
18+
#include <getopt.h>
19+
20+
#include "TFile.h"
21+
#include "TKey.h"
22+
#include "TTree.h"
23+
#include "TBranch.h"
24+
#include "TDirectory.h"
25+
#include <TGrid.h>
26+
27+
// Checks the structure of AO2D files, local or on the grid (alien://), for
28+
// what an interrupted write or merge leaves behind: a file which was not
29+
// closed (ROOT has to recover its keys), no DF folder, a DF folder with no
30+
// tree, a branch with fewer entries than its tree, or a basket with no size
31+
// or location. Only the metadata is read (one read per tree); --deep also
32+
// reads every basket's key to check that it belongs to its branch (one small
33+
// read per basket).
34+
namespace
35+
{
36+
int validate(const char* url, bool deep, int maxReports)
37+
{
38+
if (!std::strncmp(url, "alien:", 6) && !gGrid) {
39+
TGrid::Connect("alien:");
40+
if (!gGrid) {
41+
printf("ERROR: cannot connect to AliEn for %s\n", url);
42+
return -1;
43+
}
44+
}
45+
std::unique_ptr<TFile> file(TFile::Open(url));
46+
if (!file || file->IsZombie()) {
47+
printf("ERROR: cannot open %s\n", url);
48+
return -1;
49+
}
50+
const Long64_t size = file->GetSize();
51+
int problems = 0, dfs = 0, trees = 0, baskets = 0;
52+
auto report = [&](const char* df, const char* tree, std::string const& what) {
53+
if (problems++ < maxReports) {
54+
printf("BAD %s/%s: %s\n", df, tree, what.c_str());
55+
}
56+
};
57+
if (file->TestBit(TFile::kRecovered)) {
58+
report("", "", "the file was not closed: ROOT recovered its keys, the DF folders may be empty");
59+
}
60+
61+
for (auto* key : TRangeDynCast<TKey>(file->GetListOfKeys())) {
62+
if (!key || std::strncmp(key->GetName(), "DF_", 3) != 0) {
63+
continue;
64+
}
65+
++dfs;
66+
std::unique_ptr<TDirectory> dir((TDirectory*)key->ReadObj());
67+
if (!dir) {
68+
report(key->GetName(), "", "cannot read the folder");
69+
continue;
70+
}
71+
int treesInFolder = 0;
72+
for (auto* treeKey : TRangeDynCast<TKey>(dir->GetListOfKeys())) {
73+
if (!treeKey || std::strcmp(treeKey->GetClassName(), "TTree") != 0) {
74+
continue;
75+
}
76+
std::unique_ptr<TTree> tree((TTree*)treeKey->ReadObj());
77+
if (!tree) {
78+
report(key->GetName(), treeKey->GetName(), "cannot read the tree");
79+
continue;
80+
}
81+
++trees;
82+
++treesInFolder;
83+
TObjArray* branches = tree->GetListOfBranches();
84+
for (int i = 0; i < branches->GetEntriesFast(); ++i) {
85+
auto* br = (TBranch*)branches->UncheckedAt(i);
86+
if (br->GetEntries() != tree->GetEntries()) {
87+
report(key->GetName(), tree->GetName(), Form("branch %s has %lld entries, the tree %lld", br->GetName(), br->GetEntries(), tree->GetEntries()));
88+
}
89+
for (int b = 0; b < br->GetWriteBasket(); ++b) {
90+
++baskets;
91+
Int_t bytes = br->GetBasketBytes()[b];
92+
Long64_t seek = br->GetBasketSeek(b);
93+
if (bytes <= 0 || seek <= 0 || seek + bytes > size) {
94+
report(key->GetName(), tree->GetName(), Form("branch %s basket %d: %d bytes at %lld", br->GetName(), b, bytes, seek));
95+
continue;
96+
}
97+
if (deep) {
98+
char buffer[256];
99+
if (file->ReadBuffer(buffer, seek, std::min<Long64_t>(sizeof(buffer), size - seek))) {
100+
report(key->GetName(), tree->GetName(), Form("branch %s basket %d: cannot read its key at %lld", br->GetName(), b, seek));
101+
continue;
102+
}
103+
TKey basketKey(file.get());
104+
char* cursor = buffer;
105+
basketKey.ReadKeyBuffer(cursor);
106+
if (std::strcmp(basketKey.GetName(), br->GetName()) != 0) {
107+
report(key->GetName(), tree->GetName(), Form("branch %s basket %d at %lld is a basket of %s", br->GetName(), b, seek, basketKey.GetName()));
108+
}
109+
}
110+
}
111+
}
112+
}
113+
if (treesInFolder == 0) {
114+
report(key->GetName(), "", "the folder has no tree");
115+
}
116+
}
117+
if (dfs == 0) {
118+
report("", "", "the file has no DF folder");
119+
}
120+
printf("%s: %s, %d DFs, %d trees, %d baskets checked%s\n", url,
121+
problems ? Form("%d PROBLEMS", problems) : "OK", dfs, trees, baskets, deep ? " (with keys)" : "");
122+
return problems;
123+
}
124+
} // namespace
125+
126+
int main(int argc, char* argv[])
127+
{
128+
bool deep = false;
129+
int maxReports = 50;
130+
std::vector<std::string> files;
131+
132+
static struct option longOptions[] = {
133+
{"input", required_argument, nullptr, 'i'},
134+
{"deep", no_argument, nullptr, 'd'},
135+
{"max-reports", required_argument, nullptr, 'm'},
136+
{"help", no_argument, nullptr, 'h'},
137+
{nullptr, 0, nullptr, 0}};
138+
139+
int c;
140+
while ((c = getopt_long(argc, argv, "", longOptions, nullptr)) != -1) {
141+
if (c == 'i') {
142+
std::ifstream in(optarg);
143+
if (!in) {
144+
printf("ERROR: cannot read %s\n", optarg);
145+
return 2;
146+
}
147+
for (std::string line; std::getline(in, line);) {
148+
if (!line.empty() && line[0] != '#') {
149+
files.push_back(line);
150+
}
151+
}
152+
} else if (c == 'd') {
153+
deep = true;
154+
} else if (c == 'm') {
155+
maxReports = std::stoi(optarg);
156+
} else {
157+
printf(
158+
"AO2D validator: checks AO2D files for what an interrupted write or merge leaves behind:\n"
159+
"a file not closed, no DF folder, a DF folder with no tree, a branch with fewer entries\n"
160+
"than its tree, a basket with no size or location. Reads only metadata.\n\n"
161+
"Usage: %s [options] FILE... (local paths or alien:// names)\n"
162+
" --input LIST also check the files listed in LIST, one per line\n"
163+
" --deep also read every basket's key (one small read per basket)\n"
164+
" --max-reports N print at most N problems per file (default 50)\n\n"
165+
"Exit code: 0 all files fine, 1 a file has problems, 2 a file could not be read.\n",
166+
argv[0]);
167+
return c == 'h' ? 0 : 2;
168+
}
169+
}
170+
for (int i = optind; i < argc; ++i) {
171+
files.push_back(argv[i]);
172+
}
173+
if (files.empty()) {
174+
printf("ERROR: no input file, see --help\n");
175+
return 2;
176+
}
177+
178+
int bad = 0, unreadable = 0;
179+
for (auto const& f : files) {
180+
int problems = validate(f.c_str(), deep, maxReports);
181+
bad += problems > 0;
182+
unreadable += problems < 0;
183+
}
184+
if (files.size() > 1) {
185+
printf("%zu files: %d with problems, %d unreadable\n", files.size(), bad, unreadable);
186+
}
187+
return unreadable ? 2 : (bad ? 1 : 0);
188+
}

0 commit comments

Comments
 (0)