Logo ROOT  
Reference Guide
TXNetFileStager.cxx
Go to the documentation of this file.
1 // @(#)root/netx:$Id$
2 // Author: A. Peters, G. Ganis 7/2/2007
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2002, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 //////////////////////////////////////////////////////////////////////////
13 // //
14 // TXNetFileStager //
15 // //
16 // Interface to the 'XRD' staging capabilities. //
17 // //
18 //////////////////////////////////////////////////////////////////////////
19 
20 #include "TError.h"
21 #include "TUrl.h"
22 #include "TXNetFileStager.h"
23 #include "TXNetSystem.h"
24 #include "TFileCollection.h"
25 #include "TStopwatch.h"
26 #include "TFileInfo.h"
27 
28 ////////////////////////////////////////////////////////////////////////////////
29 /// Constructor. Init a TXNetSystem instance to the XRD system.
30 
32 {
33  fSystem = 0;
34  if (url && strlen(url) > 0) {
35  GetPrefix(url, fPrefix);
36 
38  }
39 }
40 
41 ////////////////////////////////////////////////////////////////////////////////
42 /// Destructor
43 
45 {
46  if (fSystem)
47  delete fSystem;
48  fSystem = 0;
49  fPrefix = "";
50 }
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 /// Check if the file defined by 'path' is ready to be used.
54 
56 {
57  if (!IsValid()) {
58  GetPrefix(path, fPrefix);
59  fSystem = new TXNetSystem(path);
60  }
61 
62  if (IsValid()) {
63  TString p(path);
64  if (!p.BeginsWith("root:"))
65  p.Insert(0, fPrefix);
66  return (fSystem->IsOnline(p));
67  }
68 
69  // Failure
70  Warning("IsStaged","TXNetSystem not initialized");
71  return kFALSE;
72 }
73 
74 ////////////////////////////////////////////////////////////////////////////////
75 /// Issue a stage request for file defined by 'path'. The string 'opt'
76 /// defines 'option' and 'priority' for 'Prepare': the format is
77 /// opt = "option=o priority=p".
78 
80 {
81  if (IsValid()) {
82  UChar_t o = 8;
83  UChar_t p = 0;
84  // Parse options, if any
85  if (opt && strlen(opt) > 0) {
86  TString xo(opt), io;
87  Ssiz_t from = 0;
88  while (xo.Tokenize(io, from, "[ ,|]")) {
89  if (io.Contains("option=")) {
90  io.ReplaceAll("option=","");
91  if (io.IsDigit()) {
92  Int_t i = io.Atoi();
93  if (i >= 0 && i <= 255)
94  o = (UChar_t) i;
95  }
96  }
97  if (io.Contains("priority=")) {
98  io.ReplaceAll("priority=","");
99  if (io.IsDigit()) {
100  Int_t i = io.Atoi();
101  if (i >= 0 && i <= 255)
102  p = (UChar_t) i;
103  }
104  }
105  }
106  }
107  // Run prepare
108  return fSystem->Prepare(paths, o, p);
109  }
110 
111  // Failure
112  Warning("Stage","TXNetSystem not initialized");
113  return kFALSE;
114 
115 }
116 
117 ////////////////////////////////////////////////////////////////////////////////
118 /// Issue a stage request for file defined by 'path'. The string 'opt'
119 /// defines 'option' and 'priority' for 'Prepare': the format is
120 /// opt = "option=o priority=p".
121 
122 Bool_t TXNetFileStager::Stage(const char *path, Option_t *opt)
123 {
124  if (!IsValid()) {
125  GetPrefix(path, fPrefix);
126  fSystem = new TXNetSystem(path);
127  }
128 
129  if (IsValid()) {
130  UChar_t o = 8; // XrdProtocol.hh
131  UChar_t p = 0;
132  // Parse options
133  TString xo(opt), io;
134  Ssiz_t from = 0;
135  while (xo.Tokenize(io, from, "[ ,|]")) {
136  if (io.Contains("option=")) {
137  io.ReplaceAll("option=","");
138  if (io.IsDigit()) {
139  Int_t i = io.Atoi();
140  if (i >= 0 && i <= 255)
141  o = (UChar_t) i;
142  }
143  }
144  if (io.Contains("priority=")) {
145  io.ReplaceAll("priority=","");
146  if (io.IsDigit()) {
147  Int_t i = io.Atoi();
148  if (i >= 0 && i <= 255)
149  p = (UChar_t) i;
150  }
151  }
152  }
153  // Make user the full path is used
154  TString pp(path);
155  if (!pp.BeginsWith("root:"))
156  pp.Insert(0, fPrefix);
157  return fSystem->Prepare(pp, o, p);
158  }
159 
160  // Failure
161  Warning("Stage","TXNetSystem not initialized");
162  return kFALSE;
163 }
164 
165 ////////////////////////////////////////////////////////////////////////////////
166 /// Isolate prefix in url
167 
168 void TXNetFileStager::GetPrefix(const char *url, TString &pfx)
169 {
170  if (gDebug > 1)
171  ::Info("TXNetFileStager::GetPrefix", "enter: %s", url);
172 
173  TUrl u(url);
174  pfx = Form("%s://", u.GetProtocol());
175  if (strlen(u.GetUser()) > 0)
176  pfx += Form("%s@", u.GetUser());
177  pfx += u.GetHost();
178  if (u.GetPort() != TUrl("root://host").GetPort())
179  pfx += Form(":%d", u.GetPort());
180  pfx += "/";
181 
182  if (gDebug > 1)
183  ::Info("TXNetFileStager::GetPrefix", "found prefix: %s", pfx.Data());
184 }
185 
186 ////////////////////////////////////////////////////////////////////////////////
187 /// Print basic info about this stager
188 
190 {
191  Printf("+++ stager: %s %s", GetName(), fPrefix.Data());
192 }
193 
194 ////////////////////////////////////////////////////////////////////////////////
195 /// Get actual end-point url for a path
196 /// Returns 0 in case of success and 1 if any error occured
197 
198 Int_t TXNetFileStager::Locate(const char *path, TString &eurl)
199 {
200  if (!IsValid()) {
201  GetPrefix(path, fPrefix);
202  fSystem = new TXNetSystem(path);
203  }
204 
205  if (IsValid())
206  return fSystem->Locate(path, eurl);
207 
208  // Unable to initialize TXNetSystem
209  return -1;
210 }
211 
212 ////////////////////////////////////////////////////////////////////////////////
213 /// Bulk locate request for a collection of files. A noop prepare command is
214 /// issued beforehand to fill redirector's cache, then Locate() is issued on
215 /// each file. Results are saved back to the input collection: when a file is
216 /// found, the staged bit is set to on, and its endpoint URL is added, if
217 /// different from the redirector's URL. If a file is not found, the staged
218 /// bit is set to off.
219 /// If addDummyUrl is kTRUE, in case file is not staged or redirector is
220 /// identical to endpoint URL, a dummy URL is prepended, respectively:
221 /// "noop://redir" and "noop://none".
222 /// If the collection contains URLs with "anchors" (i.e., #fileName.root),
223 /// they are ignored by xrootd.
224 /// The Locate() command preserves anchors, but needs single paths to be full
225 /// URLs beginning with root://.
226 /// Returns < 0 in case of errors, and the number of files processed in case
227 /// of success.
228 
230  Bool_t addDummyUrl)
231 {
232  if (!fc) {
233  Error("Locate", "No input collection given!");
234  return -1;
235  }
236 
237  // Fill redirector's cache with an empty prepare request
238  //Int_t TXNetSystem::Prepare(TCollection *paths,
239  // UChar_t opt, UChar_t prio, TString *bufout)
240 
241  Int_t count = 0;
242 
243  TStopwatch ts;
244  Double_t timeTaken_s;
245  TFileInfo *fi;
246 
247  Int_t rv = fSystem->Prepare(fc->GetList(), 0, 0, NULL);
248  // o p
249 
250  TIter it(fc->GetList());
251 
252  timeTaken_s = ts.RealTime();
253  if (gDebug > 0) {
254  Info("Locate", "Bulk xprep done in %.1lfs (returned %d)",
255  ts.RealTime(), rv);
256  }
257 
258  ts.Start();
259  TString surl, endp;
260 
261  while ((fi = dynamic_cast<TFileInfo *>(it.Next())) != NULL) {
262 
263  surl = fi->GetCurrentUrl()->GetUrl();
264 
265  if (!IsValid()) {
266  GetPrefix(surl.Data(), fPrefix);
267  if (gDebug > 0) {
268  Info("Locate", "Stager non initialized, doing it now for %s",
269  fPrefix.Data());
270  }
271  fSystem = new TXNetSystem(surl.Data());
272  }
273 
274  // Locating (0=success, 1=error -- 1 includes when file is not staged)
275  if (fSystem->Locate(surl.Data(), endp)) {
276  // File not staged
278 
279  if (addDummyUrl)
280  fi->AddUrl("noop://none", kTRUE);
281 
282  if (gDebug > 1)
283  Info("Locate", "Not found: %s", surl.Data());
284  }
285  else {
286  // File staged. Returned endpoint contains the same anchor and options.
287  // We just check if it is equal to one of our current URLs.
288 
290  if (surl != endp) {
291  fi->AddUrl(endp.Data(), kTRUE);
292  }
293  else if (addDummyUrl) {
294  // Returned URL identical to redirector's URL
295  fi->AddUrl("noop://redir", kTRUE);
296  }
297 
298  if (gDebug > 1)
299  Info("Locate", "Found: %s --> %s", surl.Data(), endp.Data());
300  }
301 
302  count++;
303  }
304 
305  timeTaken_s += ts.RealTime();
306  if (gDebug > 0) {
307  Info("Locate", "All locates finished in %.1lfs", ts.RealTime());
308  Info("Locate", "Mass prepare and locates took %.1lfs", timeTaken_s);
309  }
310 
311  return count;
312 }
313 
314 ////////////////////////////////////////////////////////////////////////////////
315 /// Returns kTRUE if stager 's' is compatible with current stager.
316 /// Avoids multiple instantiations of the potentially the same TXNetSystem.
317 
319 {
320  if (IsValid()) {
321  TString pfx;
322  GetPrefix(s, pfx);
323  return ((fPrefix == pfx) ? kTRUE : kFALSE);
324  }
325 
326  // Not valid
327  return kFALSE;
328 }
TFileInfo
Class describing a generic file including meta information.
Definition: TFileInfo.h:39
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TFileInfo::GetCurrentUrl
TUrl * GetCurrentUrl() const
Return the current url.
Definition: TFileInfo.cxx:249
TXNetFileStager::fPrefix
TString fPrefix
Definition: TXNetFileStager.h:32
Option_t
const char Option_t
Definition: RtypesCore.h:66
TString::Atoi
Int_t Atoi() const
Return integer value of string.
Definition: TString.cxx:1921
fc
static struct mg_connection * fc(struct mg_context *ctx)
Definition: civetweb.c:3728
TString::Data
const char * Data() const
Definition: TString.h:369
Form
char * Form(const char *fmt,...)
TStopwatch.h
TObject::Info
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:864
TObject::Error
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:890
TXNetSystem
Definition: TXNetSystem.h:49
TUrl::GetPort
Int_t GetPort() const
Definition: TUrl.h:78
TGeant4Unit::s
static constexpr double s
Definition: TGeant4SystemOfUnits.h:162
TXNetSystem::IsOnline
Bool_t IsOnline(const char *path)
Check if the file defined by 'path' is ready to be used.
Definition: TXNetSystem.cxx:498
TXNetFileStager::IsValid
Bool_t IsValid() const
Definition: TXNetFileStager.h:48
TString::Contains
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:624
TString
Basic string class.
Definition: TString.h:136
TXNetFileStager::IsStaged
Bool_t IsStaged(const char *path)
Check if the file defined by 'path' is ready to be used.
Definition: TXNetFileStager.cxx:55
bool
TString::ReplaceAll
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:692
TStopwatch::RealTime
Double_t RealTime()
Stop the stopwatch (if it is running) and return the realtime (in seconds) passed between the start a...
Definition: TStopwatch.cxx:110
TFileInfo::AddUrl
Bool_t AddUrl(const char *url, Bool_t infront=kFALSE)
Add a new URL.
Definition: TFileInfo.cxx:296
TXNetFileStager.h
TXNetSystem::Locate
virtual Int_t Locate(const char *path, TString &endurl)
Get end-point url of a file.
Definition: TXNetSystem.cxx:655
TObject::ResetBit
void ResetBit(UInt_t f)
Definition: TObject.h:171
TFileInfo.h
TFileStager
Definition: TFileStager.h:36
TObject::SetBit
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:696
TXNetSystem.h
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TXNetFileStager::Locate
Int_t Locate(const char *path, TString &endpath)
Get actual end-point url for a path Returns 0 in case of success and 1 if any error occured.
Definition: TXNetFileStager.cxx:198
TFileCollection.h
gDebug
Int_t gDebug
Definition: TROOT.cxx:590
TString::Insert
TString & Insert(Ssiz_t pos, const char *s)
Definition: TString.h:649
TString::BeginsWith
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition: TString.h:615
TStopwatch::Start
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:58
TXNetSystem::Prepare
Bool_t Prepare(const char *path, UChar_t opt=8, UChar_t prio=0)
Issue a prepare request for file defined by 'path'.
Definition: TXNetSystem.cxx:519
TObject::Warning
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:876
TUrl::GetUrl
const char * GetUrl(Bool_t withDeflt=kFALSE) const
Return full URL.
Definition: TUrl.cxx:387
TUrl
This class represents a WWW compatible URL.
Definition: TUrl.h:33
TString::IsDigit
Bool_t IsDigit() const
Returns true if all characters in string are digits (0-9) or white spaces, i.e.
Definition: TString.cxx:1763
TXNetFileStager::~TXNetFileStager
virtual ~TXNetFileStager()
Destructor.
Definition: TXNetFileStager.cxx:44
Printf
void Printf(const char *fmt,...)
TUrl::GetProtocol
const char * GetProtocol() const
Definition: TUrl.h:64
TIter::Next
TObject * Next()
Definition: TCollection.h:249
TFileInfo::kStaged
@ kStaged
Definition: TFileInfo.h:58
TXNetFileStager::GetPrefix
static void GetPrefix(const char *url, TString &pfx)
Isolate prefix in url.
Definition: TXNetFileStager.cxx:168
TXNetFileStager::LocateCollection
Int_t LocateCollection(TFileCollection *fc, Bool_t addDummyUrl=kFALSE)
Bulk locate request for a collection of files.
Definition: TXNetFileStager.cxx:229
Double_t
double Double_t
Definition: RtypesCore.h:59
TXNetFileStager::Stage
Bool_t Stage(const char *path, Option_t *opt=0)
Issue a stage request for file defined by 'path'.
Definition: TXNetFileStager.cxx:122
TXNetFileStager::fSystem
TXNetSystem * fSystem
Definition: TXNetFileStager.h:33
UChar_t
unsigned char UChar_t
Definition: RtypesCore.h:38
TStopwatch
Stopwatch class.
Definition: TStopwatch.h:28
TUrl::GetHost
const char * GetHost() const
Definition: TUrl.h:67
TXNetFileStager::Matches
Bool_t Matches(const char *s)
Returns kTRUE if stager 's' is compatible with current stager.
Definition: TXNetFileStager.cxx:318
TIter
Definition: TCollection.h:233
TCollection
Collection abstract base class.
Definition: TCollection.h:63
TXNetFileStager::Print
void Print(Option_t *option="") const
Print basic info about this stager.
Definition: TXNetFileStager.cxx:189
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
TUrl::GetUser
const char * GetUser() const
Definition: TUrl.h:65
TUrl.h
TXNetFileStager::TXNetFileStager
TXNetFileStager(const char *stager="")
Constructor. Init a TXNetSystem instance to the XRD system.
Definition: TXNetFileStager.cxx:31
int
TFileCollection
Class that contains a list of TFileInfo's and accumulated meta data information about its entries.
Definition: TFileCollection.h:39
TError.h