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