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