xrootd
XrdClient.hh
Go to the documentation of this file.
1 
2 // //
3 // XrdClient //
4 // //
5 // Author: Fabrizio Furano (INFN Padova, 2004) //
6 // Adapted from TXNetFile (root.cern.ch) originally done by //
7 // Alvise Dorigo, Fabrizio Furano //
8 // INFN Padova, 2003 //
9 // //
10 // A UNIX reference client for xrootd. //
11 // //
13 
14 // $Id$
15 
16 #ifndef XRD_CLIENT_H
17 #define XRD_CLIENT_H
18 
19 
21 // //
22 // //
23 // Some features: //
24 // - Automatic server kind recognition (xrootd load balancer, xrootd //
25 // data server, old rootd) //
26 // - Fault tolerance for read/write operations (read/write timeouts //
27 // and retry) //
28 // - Internal connection timeout (tunable indipendently from the OS //
29 // one) //
30 // - Full implementation of the xrootd protocol //
31 // - handling of redirections from server //
32 // - Connection multiplexing //
33 // - Asynchronous operation mode //
34 // - High performance read caching with read-ahead //
35 // - Thread safe //
36 // - Tunable log verbosity level (0 = nothing, 3 = dump read/write //
37 // buffers too!) //
38 // - Many parameters configurable. But the default are OK for nearly //
39 // all the situations. //
40 // //
42 
44 #include "XrdOuc/XrdOucString.hh"
46 #include "XrdSys/XrdSysSemWait.hh"
47 #include "XrdVersion.hh"
48 #include <vector>
49 #include <string>
50 
51 
53 
55  bool inprogress;
56  bool opened;
59 };
60 
62  int stated;
63  long long size;
64  long id;
65  long flags;
66  long modtime;
67 };
68 
70  int CacheSize;
71 
72  // This does not take into account the 'suggestions'
73  // like async read or async readV
74  // We count only for functions which return data, eventually
75  // taken from the cache
76  long long ReadBytes;
77  long long WrittenBytes;
78  long long WriteRequests;
79 
80  long long ReadRequests;
81  long long ReadMisses;
82  long long ReadHits;
83  float ReadMissRate;
84 
85  long long ReadVRequests; // How many readVs (sync) were requested
86  long long ReadVSubRequests; // In how many sub-readVs they were split
87  long long ReadVSubChunks; // How many subchunks in total
88  long long ReadVBytes; // How many bytes were requested (sync)
89 
90  long long ReadVAsyncRequests; // How many readVs (async) were requested
91  long long ReadVAsyncSubRequests; // In how many sub-readVs they were split
92  long long ReadVAsyncSubChunks; // How many subchunks in total
93  long long ReadVAsyncBytes; // How many bytes were requested (async)
94 
95  long long ReadAsyncRequests;
96  long long ReadAsyncBytes;
97 };
98 
99 
100 class XrdClient : public XrdClientAbs {
101  friend void *FileOpenerThread(void*, XrdClientThread*);
102 
103 
104 private:
105 
106  struct XrdClientOpenInfo fOpenPars; // Just a container for the last parameters
107  // passed to a Open method
108 
109  // The open request can be in progress. Further requests must be delayed until
110  // finished.
112 
113  // Used to open a file in parallel
115 
116  // Used to limit the maximum number of concurrent opens
118 
120 
121  XrdSysCondVar *fReadWaitData; // Used to wait for outstanding data
122 
124 
126 
127  bool fUseCache;
128 
131 
132  bool TryOpen(kXR_unt16 mode,
133  kXR_unt16 options,
134  bool doitparallel);
135 
136  bool LowOpen(const char *file,
137  kXR_unt16 mode,
138  kXR_unt16 options,
139  char *additionalquery = 0);
140 
141  void TerminateOpenAttempt();
142 
143  void WaitForNewAsyncData();
144 
145  // Real implementation for ReadV
146  // To call it we need to be aware of the restrictions so the public
147  // interface should be ReadV()
148  kXR_int64 ReadVEach(char *buf, kXR_int64 *offsets, int *lens, int &nbuf);
149 
151  // This supposes that no options means read only
152  if (!fOpenPars.options) return false;
153 
154  if (fOpenPars.options & kXR_open_read) return false;
155 
156  return true;
157  }
158 
160 
161  void PrintCounters();
162 protected:
163 
165 
166  virtual bool OpenFileWhenRedirected(char *newfhandle,
167  bool &wasopen);
168 
169  virtual bool CanRedirOnError() {
170  // Can redir away on error if no file is opened
171  // or the file is opened in read mode
172 
173  if ( !fOpenPars.opened ) return true;
174 
175  return !IsOpenedForWrite();
176 
177  }
178 
179 
180 public:
181 
182  XrdClient(const char *url, XrdClientCallback *XrdCcb = 0, void *XrdCcbArg = 0);
183  virtual ~XrdClient();
184 
186  XrdClientMessage *unsolmsg);
187 
188  bool Close();
189 
190  // Ask the server to flush its cache
191  bool Sync();
192 
193  // Copy the whole file to the local filesystem. Not very efficient.
194  bool Copy(const char *localpath);
195 
196  // Returns low level information about the cache
197  bool GetCacheInfo(
198  // The actual cache size
199  int &size,
200 
201  // The number of bytes submitted since the beginning
202  long long &bytessubmitted,
203 
204  // The number of bytes found in the cache (estimate)
205  long long &byteshit,
206 
207  // The number of reads which did not find their data
208  // (estimate)
209  long long &misscount,
210 
211  // miss/totalreads ratio (estimate)
212  float &missrate,
213 
214  // number of read requests towards the cache
215  long long &readreqcnt,
216 
217  // ratio between bytes found / bytes submitted
218  float &bytesusefulness
219  );
220 
221 
222 
223  // Returns client-level information about the activity performed up to now
224  bool GetCounters( XrdClientCounters *cnt );
225 
226  // Quickly tells if the file is open
227  inline bool IsOpen() { return fOpenPars.opened; }
228 
229  // Tells if the file opening is in progress
230  bool IsOpen_inprogress();
231 
232  // Tells if the file is open, waiting for the completion of the parallel open
233  bool IsOpen_wait();
234 
235  // Open the file. See the xrootd documentation for mode and options
236  // If parallel, then the open is done by a separate thread, and
237  // all the operations are delayed until the open has finished
238  bool Open(kXR_unt16 mode, kXR_unt16 options, bool doitparallel=true);
239 
240  // Read a block of data. If no error occurs, it returns all the requested bytes.
241  int Read(void *buf, long long offset, int len);
242 
243  // Read multiple blocks of data compressed into a sinle one. It's up
244  // to the application to do the logistic (having the offset and len to find
245  // the position of the required buffer given the big one). If no error
246  // occurs, it returns all the requested bytes.
247  // NOTE: if buf == 0 then the req will be carried out asynchronously, i.e.
248  // the result of the request will only populate the internal cache. A subsequent read()
249  // of that chunk will get the data from the cache
250  kXR_int64 ReadV(char *buf, long long *offsets, int *lens, int nbuf);
251 
252  // Submit an asynchronous read request. Its result will only populate the cache
253  // (if any!!)
254  XReqErrorType Read_Async(long long offset, int len, bool updatecounters=true);
255 
256  // Get stat info about the file. Normally it tries to guess the file size variations
257  // unless force==true
258  bool Stat(struct XrdClientStatInfo *stinfo, bool force = false);
259 
260  // On-the-fly enabling/disabling of the cache
261  bool UseCache(bool u = TRUE);
262 
263  // To instantly remove all the chunks in the cache
265  if (fConnModule)
267  }
268 
269  // To remove pieces of data from the cache
270  void RemoveDataFromCache(long long begin_offs,
271  long long end_offs, bool remove_overlapped = false) {
272  if (fConnModule)
273  fConnModule->RemoveDataFromCache(begin_offs, end_offs, remove_overlapped);
274  }
275 
276  // To set at run time the cache/readahead parameters for this instance only
277  // If a parameter is < 0 then it's left untouched.
278  // To simply enable/disable the caching, just use UseCache(), not this function
279  void SetCacheParameters(int CacheSize, int ReadAheadSize, int RmPolicy);
280 
281  // To enable/disable different read ahead strategies. Defined in XrdClientReadAhead.hh
282  void SetReadAheadStrategy(int strategy);
283 
284  // To enable the trimming of the blocks to read. Blocksize will be rounded to a multiple of 512.
285  // Each read request will have the offset and length aligned with a multiple of blocksize
286  // This strategy is similar to a read ahead, but not quite. Here we see it as a transformation
287  // of the stream of the read accesses to request
288  void SetBlockReadTrimming(int blocksize);
289 
290  // Truncates the open file at a specified length
291  bool Truncate(long long len);
292 
293  // Write data to the file
294  bool Write(const void *buf, long long offset, int len);
295 
296  std::vector<std::string> fExcludedHosts;
297 
298 };
299 
300 #endif