forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TPacketizerMulti.cxx
340 lines (295 loc) · 11.8 KB
/
TPacketizerMulti.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// @(#)root/proofplayer:$Id$
// Author: G. Ganis Jan 2010
/*************************************************************************
* Copyright (C) 1995-2002, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
/** \class TPacketizerMulti
\ingroup proofkernel
This class allows to do multiple runs in the same query; each run
can be a, for example, different dataset or the same dataset with
entry list.
The multiple packetizer contains a list of packetizers which are
processed in turn.
The bit TSelector::kNewRun is set in the TSelector object when a new
packetizer is used.
*/
#include "TPacketizerMulti.h"
#include "TClass.h"
#include "TDSet.h"
#include "TError.h"
#include "TFileInfo.h"
#include "TList.h"
#include "TMap.h"
#include "TMethodCall.h"
#include "TProof.h"
#include "TProofDebug.h"
ClassImp(TPacketizerMulti);
////////////////////////////////////////////////////////////////////////////////
/// Constructor
TPacketizerMulti::TPacketizerMulti(TDSet *dset, TList *wrks,
Long64_t first, Long64_t num,
TList *input, TProofProgressStatus *st)
: TVirtualPacketizer(input, st)
{
PDB(kPacketizer,1) Info("TPacketizerMulti",
"enter (first %lld, num %lld)", first, num);
fValid = kFALSE;
fPacketizersIter = 0;
fCurrent = 0;
fAssignedPack = 0;
// Check inputs
if (!dset || !wrks || !input || !st) {
Error("TPacketizerMulti", "invalid inputs: dset:%p wrks:%p input:%p st:%p",
dset, wrks, input, st);
return;
}
// Create the list
fPacketizers = new TList;
// We do not want progress timers from the single packetizers
TNamed *progTimerFlag = new TNamed("PROOF_StartProgressTimer", "no");
input->Add(progTimerFlag);
fTotalEntries = 0;
TVirtualPacketizer *packetizer = 0;
// Simple or multi?
if (!(dset->TestBit(TDSet::kMultiDSet))) {
if ((packetizer = CreatePacketizer(dset, wrks, first, num, input, st))) {
fPacketizers->Add(packetizer);
fTotalEntries = packetizer->GetTotalEntries();
} else {
Error("TPacketizerMulti", "problems initializing packetizer for single dataset");
input->Remove(progTimerFlag);
delete progTimerFlag;
return;
}
} else {
// Iterate on the datasets
TIter nxds(dset->GetListOfElements());
TDSet *ds = 0;
while ((ds = (TDSet *)nxds())) {
if ((packetizer = CreatePacketizer(ds, wrks, first, num, input, st))) {
fPacketizers->Add(packetizer);
fTotalEntries += packetizer->GetTotalEntries();
} else {
Error("TPacketizerMulti", "problems initializing packetizer for dataset '%s'", ds->GetName());
}
}
}
// Cleanup temporary additions to the input list
input->Remove(progTimerFlag);
delete progTimerFlag;
// If no valid packetizer could be initialized we fail
if (fPacketizers->GetSize() <= 0) {
Error("TPacketizerMulti", "no valid packetizer could be initialized - aborting");
SafeDelete(fPacketizers);
return;
} else {
Info("TPacketizerMulti", "%d packetizer(s) have been successfully initialized (%lld events in total)",
fPacketizers->GetSize(), fTotalEntries);
// To avoid problems with counters we must set the total entries in each packetizer
TIter nxp(fPacketizers);
while ((packetizer = (TVirtualPacketizer *) nxp()))
packetizer->SetTotalEntries(fTotalEntries);
}
// Create the interator
fPacketizersIter = new TIter(fPacketizers);
// Set the current the first
if (!(fCurrent = (TVirtualPacketizer *) fPacketizersIter->Next())) {
// Weird
Error("TPacketizerMulti", "could not point to the first valid packetizer");
fPacketizers->SetOwner(kTRUE);
SafeDelete(fPacketizers);
SafeDelete(fPacketizersIter);
return;
}
// Create map
fAssignedPack = new TMap;
// Ok, everything went fine
fValid = kTRUE;
PDB(kPacketizer,1) Info("TPacketizerMulti", "done");
}
////////////////////////////////////////////////////////////////////////////////
/// Destructor.
TPacketizerMulti::~TPacketizerMulti()
{
if (fPacketizers) {
fPacketizers->SetOwner(kTRUE);
SafeDelete(fPacketizers);
}
SafeDelete(fPacketizers);
fCurrent = 0;
if (fAssignedPack) {
fAssignedPack->SetOwner(kFALSE);
SafeDelete(fAssignedPack);
}
SafeDelete(fPacketizersIter);
}
////////////////////////////////////////////////////////////////////////////////
/// Get next packet from the current packetizer.
/// If the current packetizer is done, move to next.
/// Retun null when all packetizers are done.
TDSetElement *TPacketizerMulti::GetNextPacket(TSlave *wrk, TMessage *r)
{
TDSetElement *elem = 0;
// Must be valid
if (!fValid) return elem;
// Point to the packetizer last used for thsi worker
TVirtualPacketizer *lastPacketizer = dynamic_cast<TVirtualPacketizer *>(fAssignedPack->GetValue(wrk));
if (lastPacketizer && lastPacketizer != fCurrent) {
PDB(kPacketizer,2)
Info("GetNextPacket", "%s: asking old packetizer %p ... ", wrk->GetOrdinal(), lastPacketizer);
if ((elem = lastPacketizer->GetNextPacket(wrk, r))) return elem;
if (fCurrent) {
// Transfer the status info
TVirtualSlaveStat *oldstat = dynamic_cast<TVirtualSlaveStat *>(lastPacketizer->GetSlaveStats()->GetValue(wrk));
TVirtualSlaveStat *curstat = dynamic_cast<TVirtualSlaveStat *>(fCurrent->GetSlaveStats()->GetValue(wrk));
if (oldstat && curstat)
*(curstat->GetProgressStatus()) += *(oldstat->GetProgressStatus());
}
}
// Need something to be processed
if (!fCurrent) {
HandleTimer(0); // Send last timer message
return elem;
}
// Get the next packet from the current packetizer
PDB(kPacketizer,2)
Info("GetNextPacket", "%s: asking current packetizer %p ... ", wrk->GetOrdinal(), fCurrent);
if (!(elem = fCurrent->GetNextPacket(wrk, r))) {
// We need to transfer the status info if we change packetizer now
TMap *oldStats = (lastPacketizer && lastPacketizer == fCurrent) ? lastPacketizer->GetSlaveStats() : 0;
// If the packetizer is done, move to next
fCurrent = (TVirtualPacketizer *) fPacketizersIter->Next();
if (fCurrent) {
// Transfer the status info
if (oldStats) {
TVirtualSlaveStat *oldstat = dynamic_cast<TVirtualSlaveStat *>(oldStats->GetValue(wrk));
TVirtualSlaveStat *curstat = dynamic_cast<TVirtualSlaveStat *>(fCurrent->GetSlaveStats()->GetValue(wrk));
if (oldstat && curstat)
*(curstat->GetProgressStatus()) += *(oldstat->GetProgressStatus());
}
PDB(kPacketizer,2)
Info("GetNextPacket", "%s: asking new packetizer %p ... ", wrk->GetOrdinal(), fCurrent);
elem = fCurrent->GetNextPacket(wrk, r);
}
}
if (fCurrent) {
// Save the packetizer
TPair *pair = dynamic_cast<TPair *>(fAssignedPack->FindObject(wrk));
if (pair) {
pair->SetValue(fCurrent);
} else {
fAssignedPack->Add(wrk, fCurrent);
}
PDB(kPacketizer,2)
Info("GetNextPacket", "assigned packetizer %p to %s (check: %p)",
fCurrent, wrk->GetOrdinal(), fAssignedPack->GetValue(wrk));
}
// Check the total number of entries
if (fProgressStatus->GetEntries() >= fTotalEntries) {
if (fProgressStatus->GetEntries() > fTotalEntries)
Error("GetNextPacket", "Processed too many entries!");
HandleTimer(0); // Send last timer message
SafeDelete(fProgress);
}
// Done
return elem;
}
////////////////////////////////////////////////////////////////////////////////
/// Create a packetizer for dataset 'dset'
/// Return null on failure.
TVirtualPacketizer *TPacketizerMulti::CreatePacketizer(TDSet *dset, TList *wrks,
Long64_t first, Long64_t num,
TList *input, TProofProgressStatus *st)
{
TVirtualPacketizer *packetizer = 0;
// Check inputs
if (!dset || !wrks || !input || !st) {
Error("CreatePacketizer", "invalid inputs: dset:%p wrks:%p input:%p st:%p",
dset, wrks, input, st);
return packetizer;
}
// This is for data-driven runs
if (dset->TestBit(TDSet::kEmpty)) {
Error("CreatePacketizer", "dataset is empty: protocol error?");
return packetizer;
}
TString packetizername;
TList *listOfMissingFiles = 0;
TMethodCall callEnv;
TClass *cl;
// Lookup - resolve the end-point urls to optmize the distribution.
// The lookup was previously called in the packetizer's constructor.
// A list for the missing files may already have been added to the
// output list; otherwise, if needed it will be created inside
if (!(listOfMissingFiles = (TList *) input->FindObject("MissingFiles"))) {
// Create it
listOfMissingFiles = new TList;
// and add it to the input list; it will be later moved to the output list
input->Add(listOfMissingFiles);
}
dset->Lookup(kTRUE, &listOfMissingFiles);
if (!(dset->GetListOfElements()) ||
!(dset->GetListOfElements()->GetSize())) {
Error("CreatePacketizer", "no files from the data set were found - skipping");
return packetizer;
}
if (TProof::GetParameter(input, "PROOF_Packetizer", packetizername) != 0) {
// Using standard packetizer TPacketizer
packetizername = "TPacketizer";
} else {
Info("CreatePacketizer", "using alternate packetizer: %s", packetizername.Data());
}
// Get linked to the related class
cl = TClass::GetClass(packetizername);
if (cl == 0) {
Error("CreatePacketizer", "class '%s' not found", packetizername.Data());
return packetizer;
}
// Init the constructor
callEnv.InitWithPrototype(cl, cl->GetName(),"TDSet*,TList*,Long64_t,Long64_t,TList*,TProofProgressStatus*");
if (!callEnv.IsValid()) {
Error("CreatePacketizer", "cannot find correct constructor for '%s'", cl->GetName());
return packetizer;
}
callEnv.ResetParam();
callEnv.SetParam((Long_t) dset);
callEnv.SetParam((Long_t) wrks);
callEnv.SetParam((Long64_t) first);
callEnv.SetParam((Long64_t) num);
callEnv.SetParam((Long_t) input);
callEnv.SetParam((Long_t) st);
// We are going to test validity during the packetizer initialization
dset->SetBit(TDSet::kValidityChecked);
dset->ResetBit(TDSet::kSomeInvalid);
// Get an instance of the packetizer
Long_t ret = 0;
callEnv.Execute(ret);
if ((packetizer = (TVirtualPacketizer *)ret) == 0) {
Error("CreatePacketizer", "cannot construct '%s'", cl->GetName());
return packetizer;
}
if (!packetizer->IsValid()) {
Error("CreatePacketizer",
"instantiated packetizer object '%s' is invalid", cl->GetName());
SafeDelete(packetizer);
}
// Add invalid elements to the list of missing elements
TDSetElement *elem = 0;
if (dset->TestBit(TDSet::kSomeInvalid)) {
TIter nxe(dset->GetListOfElements());
while ((elem = (TDSetElement *)nxe())) {
if (!elem->GetValid()) {
listOfMissingFiles->Add(elem->GetFileInfo(dset->GetType()));
dset->Remove(elem, kFALSE);
}
}
// The invalid elements have been removed
dset->ResetBit(TDSet::kSomeInvalid);
}
// Done
return packetizer;
}