Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Framework/AODMerger/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ o2_add_executable(strainer
COMPONENT_NAME aod
SOURCES src/aodStrainer.cxx
PUBLIC_LINK_LIBRARIES ROOT::Core ROOT::Net)

o2_add_executable(validator
COMPONENT_NAME aod
SOURCES src/aodValidator.cxx
PUBLIC_LINK_LIBRARIES ROOT::Core ROOT::Net ROOT::Tree)
71 changes: 69 additions & 2 deletions Framework/AODMerger/src/aodMerger.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,51 @@
#include <TGrid.h>
#include <TMap.h>
#include <TLeaf.h>
#include <TError.h>

#include "aodMerger.h"
#include <cinttypes>

namespace
{
// ROOT errors (e.g. an expired XRootD read) seen while copying a tree. A fast
// copy does not fail on them: TTree::CopyEntries runs TTreeCloner with
// kNoWarnings, and a basket which could not be read is written with 0 bytes.
int rootErrors = 0;
ErrorHandlerFunc_t previousErrorHandler = nullptr;

void countingErrorHandler(int level, Bool_t abort, const char* location, const char* msg)
{
if (level >= kError) {
++rootErrors;
}
previousErrorHandler(level, abort, location, msg);
}

// True if every branch of the copied tree has all its entries and every
// written basket a location. A basket lost in a fast copy shows up here.
bool isCopyComplete(TTree* tree)
{
bool complete = true;
TObjArray* branches = tree->GetListOfBranches();
for (int i = 0; i < branches->GetEntriesFast(); ++i) {
auto* br = (TBranch*)branches->UncheckedAt(i);
if (br->GetEntries() != tree->GetEntries()) {
printf(" *** FATAL ***: Branch %s of tree %s has %lld entries, the tree %lld\n", br->GetName(), tree->GetName(), br->GetEntries(), tree->GetEntries());
complete = false;
}
for (int b = 0; b < br->GetWriteBasket(); ++b) {
if (br->GetBasketBytes()[b] <= 0 || br->GetBasketSeek(b) <= 0) {
printf(" *** FATAL ***: Basket %d of branch %s of tree %s has %d bytes at %lld\n", b, br->GetName(), tree->GetName(), br->GetBasketBytes()[b], br->GetBasketSeek(b));
complete = false;
break;
}
}
}
return complete;
}
} // namespace

// AOD merger with correct index rewriting
// No need to know the datamodel because the branch names follow a canonical standard (identified by fIndex)
int main(int argc, char* argv[])
Expand Down Expand Up @@ -103,6 +144,8 @@ int main(int argc, char* argv[])
printf(" Merging only folders with the same name.\n");
}

previousErrorHandler = SetErrorHandler(countingErrorHandler);

std::map<std::string, TTree*> trees;
std::map<std::string, uint64_t> sizeCompressed;
std::map<std::string, uint64_t> sizeUncompressed;
Expand Down Expand Up @@ -167,6 +210,12 @@ int main(int argc, char* argv[])
break;
}
}
if (inputFile->TestBit(TFile::kRecovered)) {
// Written by a job which stopped before closing it: its DF folders come back without trees.
printf("*** FATAL ***: Input file %s was not closed, ROOT had to recover its keys. Aborting merge!\n", line.Data());
exitCode = 6;
break;
}

TList* keyList = inputFile->GetListOfKeys();
keyList->Sort();
Expand Down Expand Up @@ -227,6 +276,11 @@ int main(int argc, char* argv[])
++totalMergedDFs;
auto folder = (TDirectoryFile*)inputFile->Get(dfName);
auto treeList = folder->GetListOfKeys();
if (treeList->GetEntries() == 0) {
printf(" *** FATAL ***: Folder %s of %s has no tree\n", dfName, line.Data());
exitCode = 6;
break;
}

treeList->Sort();

Expand Down Expand Up @@ -268,6 +322,7 @@ int main(int argc, char* argv[])
printf(" Processing tree %s with %lld entries with total size %lld (fast copy: %d)\n", treeName, inputTree->GetEntries(), inputTree->GetTotBytes(), fastCopy);
}

int errorsBefore = rootErrors;
bool alreadyCopied = false;
if (trees.count(treeName) == 0) {
if (mergedDFs > 1) {
Expand Down Expand Up @@ -359,7 +414,11 @@ int main(int argc, char* argv[])
for (auto& index : indexList) {
*(index.first) = 0; // Any positive number will do, in any case it will not be filled in the output. Otherwise the previous entry is used and manipulated in the following.
}
inputTree->GetEntry(i);
if (inputTree->GetEntry(i) < 0) {
printf(" *** FATAL ***: Could not read entry %d of tree %s\n", i, treeName);
exitCode = 6;
break;
}
// shift index columns by offset
for (const auto& idx : indexList) {
// if negative, the index is unassigned. In this case, the different unassigned blocks have to get unique negative IDs
Expand All @@ -385,6 +444,14 @@ int main(int argc, char* argv[])
}
}

if (rootErrors > errorsBefore) {
printf(" *** FATAL ***: %d ROOT error(s) while copying tree %s\n", rootErrors - errorsBefore, treeName);
exitCode = 6;
}
if (!isCopyComplete(outputTree)) {
exitCode = 6;
}

delete inputTree;

for (auto& buffer : indexPointers) {
Expand Down Expand Up @@ -450,7 +517,7 @@ int main(int argc, char* argv[])
outputFile->Write();
outputFile->Close();

if (totalMergedDFs == 0) {
if (totalMergedDFs == 0 && exitCode == 0) {
printf("ERROR: Did not merge a single DF. This does not seem right.\n");
exitCode = 2;
}
Expand Down
188 changes: 188 additions & 0 deletions Framework/AODMerger/src/aodValidator.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

#include <algorithm>
#include <cstring>
#include <fstream>
#include <memory>
#include <string>
#include <vector>
#include <getopt.h>

#include "TFile.h"
#include "TKey.h"
#include "TTree.h"
#include "TBranch.h"
#include "TDirectory.h"
#include <TGrid.h>

// Checks the structure of AO2D files, local or on the grid (alien://), for
// what an interrupted write or merge leaves behind: a file which was not
// closed (ROOT has to recover its keys), no DF folder, a DF folder with no
// tree, a branch with fewer entries than its tree, or a basket with no size
// or location. Only the metadata is read (one read per tree); --deep also
// reads every basket's key to check that it belongs to its branch (one small
// read per basket).
namespace
{
int validate(const char* url, bool deep, int maxReports)
{
if (!std::strncmp(url, "alien:", 6) && !gGrid) {
TGrid::Connect("alien:");
if (!gGrid) {
printf("ERROR: cannot connect to AliEn for %s\n", url);
return -1;
}
}
std::unique_ptr<TFile> file(TFile::Open(url));
if (!file || file->IsZombie()) {
printf("ERROR: cannot open %s\n", url);
return -1;
}
const Long64_t size = file->GetSize();
int problems = 0, dfs = 0, trees = 0, baskets = 0;
auto report = [&](const char* df, const char* tree, std::string const& what) {
if (problems++ < maxReports) {
printf("BAD %s/%s: %s\n", df, tree, what.c_str());
}
};
if (file->TestBit(TFile::kRecovered)) {
report("", "", "the file was not closed: ROOT recovered its keys, the DF folders may be empty");
}

for (auto* key : TRangeDynCast<TKey>(file->GetListOfKeys())) {
if (!key || std::strncmp(key->GetName(), "DF_", 3) != 0) {
continue;
}
++dfs;
std::unique_ptr<TDirectory> dir((TDirectory*)key->ReadObj());
if (!dir) {
report(key->GetName(), "", "cannot read the folder");
continue;
}
int treesInFolder = 0;
for (auto* treeKey : TRangeDynCast<TKey>(dir->GetListOfKeys())) {
if (!treeKey || std::strcmp(treeKey->GetClassName(), "TTree") != 0) {
continue;
}
std::unique_ptr<TTree> tree((TTree*)treeKey->ReadObj());
if (!tree) {
report(key->GetName(), treeKey->GetName(), "cannot read the tree");
continue;
}
++trees;
++treesInFolder;
TObjArray* branches = tree->GetListOfBranches();
for (int i = 0; i < branches->GetEntriesFast(); ++i) {
auto* br = (TBranch*)branches->UncheckedAt(i);
if (br->GetEntries() != tree->GetEntries()) {
report(key->GetName(), tree->GetName(), Form("branch %s has %lld entries, the tree %lld", br->GetName(), br->GetEntries(), tree->GetEntries()));
}
for (int b = 0; b < br->GetWriteBasket(); ++b) {
++baskets;
Int_t bytes = br->GetBasketBytes()[b];
Long64_t seek = br->GetBasketSeek(b);
if (bytes <= 0 || seek <= 0 || seek + bytes > size) {
report(key->GetName(), tree->GetName(), Form("branch %s basket %d: %d bytes at %lld", br->GetName(), b, bytes, seek));
continue;
}
if (deep) {
char buffer[256];
if (file->ReadBuffer(buffer, seek, std::min<Long64_t>(sizeof(buffer), size - seek))) {
report(key->GetName(), tree->GetName(), Form("branch %s basket %d: cannot read its key at %lld", br->GetName(), b, seek));
continue;
}
TKey basketKey(file.get());
char* cursor = buffer;
basketKey.ReadKeyBuffer(cursor);
if (std::strcmp(basketKey.GetName(), br->GetName()) != 0) {
report(key->GetName(), tree->GetName(), Form("branch %s basket %d at %lld is a basket of %s", br->GetName(), b, seek, basketKey.GetName()));
}
}
}
}
}
if (treesInFolder == 0) {
report(key->GetName(), "", "the folder has no tree");
}
}
if (dfs == 0) {
report("", "", "the file has no DF folder");
}
printf("%s: %s, %d DFs, %d trees, %d baskets checked%s\n", url,
problems ? Form("%d PROBLEMS", problems) : "OK", dfs, trees, baskets, deep ? " (with keys)" : "");
return problems;
}
} // namespace

int main(int argc, char* argv[])
{
bool deep = false;
int maxReports = 50;
std::vector<std::string> files;

static struct option longOptions[] = {
{"input", required_argument, nullptr, 'i'},
{"deep", no_argument, nullptr, 'd'},
{"max-reports", required_argument, nullptr, 'm'},
{"help", no_argument, nullptr, 'h'},
{nullptr, 0, nullptr, 0}};

int c;
while ((c = getopt_long(argc, argv, "", longOptions, nullptr)) != -1) {
if (c == 'i') {
std::ifstream in(optarg);
if (!in) {
printf("ERROR: cannot read %s\n", optarg);
return 2;
}
for (std::string line; std::getline(in, line);) {
if (!line.empty() && line[0] != '#') {
files.push_back(line);
}
}
} else if (c == 'd') {
deep = true;
} else if (c == 'm') {
maxReports = std::stoi(optarg);
} else {
printf(
"AO2D validator: checks AO2D files for what an interrupted write or merge leaves behind:\n"
"a file not closed, no DF folder, a DF folder with no tree, a branch with fewer entries\n"
"than its tree, a basket with no size or location. Reads only metadata.\n\n"
"Usage: %s [options] FILE... (local paths or alien:// names)\n"
" --input LIST also check the files listed in LIST, one per line\n"
" --deep also read every basket's key (one small read per basket)\n"
" --max-reports N print at most N problems per file (default 50)\n\n"
"Exit code: 0 all files fine, 1 a file has problems, 2 a file could not be read.\n",
argv[0]);
return c == 'h' ? 0 : 2;
}
}
for (int i = optind; i < argc; ++i) {
files.push_back(argv[i]);
}
if (files.empty()) {
printf("ERROR: no input file, see --help\n");
return 2;
}

int bad = 0, unreadable = 0;
for (auto const& f : files) {
int problems = validate(f.c_str(), deep, maxReports);
bad += problems > 0;
unreadable += problems < 0;
}
if (files.size() > 1) {
printf("%zu files: %d with problems, %d unreadable\n", files.size(), bad, unreadable);
}
return unreadable ? 2 : (bad ? 1 : 0);
}