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
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
168void 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
198Int_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}
unsigned char UChar_t
Definition: RtypesCore.h:36
const Bool_t kFALSE
Definition: RtypesCore.h:90
double Double_t
Definition: RtypesCore.h:57
R__EXTERN Int_t gDebug
Definition: RtypesCore.h:117
const Bool_t kTRUE
Definition: RtypesCore.h:89
const char Option_t
Definition: RtypesCore.h:64
char * Form(const char *fmt,...)
void Printf(const char *fmt,...)
static struct mg_connection * fc(struct mg_context *ctx)
Definition: civetweb.c:3728
Collection abstract base class.
Definition: TCollection.h:63
Class that contains a list of TFileInfo's and accumulated meta data information about its entries.
Class describing a generic file including meta information.
Definition: TFileInfo.h:36
Bool_t AddUrl(const char *url, Bool_t infront=kFALSE)
Add a new URL.
Definition: TFileInfo.cxx:295
TUrl * GetCurrentUrl() const
Return the current url.
Definition: TFileInfo.cxx:248
TObject * Next()
Definition: TCollection.h:249
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:877
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:694
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:891
void ResetBit(UInt_t f)
Definition: TObject.h:186
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:865
Stopwatch class.
Definition: TStopwatch.h:28
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
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:58
Basic string class.
Definition: TString.h:131
TString & Insert(Ssiz_t pos, const char *s)
Definition: TString.h:644
Int_t Atoi() const
Return integer value of string.
Definition: TString.cxx:1921
const char * Data() const
Definition: TString.h:364
Bool_t IsDigit() const
Returns true if all characters in string are digits (0-9) or white spaces, i.e.
Definition: TString.cxx:1763
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:687
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition: TString.h:610
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:619
This class represents a WWW compatible URL.
Definition: TUrl.h:35
const char * GetUrl(Bool_t withDeflt=kFALSE) const
Return full URL.
Definition: TUrl.cxx:387
const char * GetUser() const
Definition: TUrl.h:67
const char * GetHost() const
Definition: TUrl.h:69
const char * GetProtocol() const
Definition: TUrl.h:66
Int_t GetPort() const
Definition: TUrl.h:80
Bool_t IsStaged(const char *path)
Check if the file defined by 'path' is ready to be used.
Bool_t Matches(const char *s)
Returns kTRUE if stager 's' is compatible with current stager.
Int_t LocateCollection(TFileCollection *fc, Bool_t addDummyUrl=kFALSE)
Bulk locate request for a collection of files.
static void GetPrefix(const char *url, TString &pfx)
Isolate prefix in url.
Bool_t IsValid() const
void Print(Option_t *option="") const
Print basic info about this stager.
virtual ~TXNetFileStager()
Destructor.
Bool_t Stage(const char *path, Option_t *opt=0)
Issue a stage request for file defined by 'path'.
TXNetSystem * fSystem
TXNetFileStager(const char *stager="")
Constructor. Init a TXNetSystem instance to the XRD system.
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.
Bool_t IsOnline(const char *path)
Check if the file defined by 'path' is ready to be used.
Bool_t Prepare(const char *path, UChar_t opt=8, UChar_t prio=0)
Issue a prepare request for file defined by 'path'.
virtual Int_t Locate(const char *path, TString &endurl)
Get end-point url of a file.
static constexpr double s