xrootd
XrdClient.hh
Go to the documentation of this file.
00001 
00002 //                                                                      //
00003 // XrdClient                                                            //
00004 //                                                                      //
00005 // Author: Fabrizio Furano (INFN Padova, 2004)                          //
00006 // Adapted from TXNetFile (root.cern.ch) originally done by             //
00007 //  Alvise Dorigo, Fabrizio Furano                                      //
00008 //          INFN Padova, 2003                                           //
00009 //                                                                      //
00010 // A UNIX reference client for xrootd.                                  //
00011 //                                                                      //
00013 
00014 //         $Id$
00015 
00016 #ifndef XRD_CLIENT_H
00017 #define XRD_CLIENT_H
00018 
00019 
00021 //                                                                      //
00022 //                                                                      //
00023 // Some features:                                                       //
00024 //  - Automatic server kind recognition (xrootd load balancer, xrootd   //
00025 //    data server, old rootd)                                           //
00026 //  - Fault tolerance for read/write operations (read/write timeouts    //
00027 //    and retry)                                                        //
00028 //  - Internal connection timeout (tunable indipendently from the OS    //
00029 //    one)                                                              //
00030 //  - Full implementation of the xrootd protocol                        //
00031 //  - handling of redirections from server                              //
00032 //  - Connection multiplexing                                           //
00033 //  - Asynchronous operation mode                                       //
00034 //  - High performance read caching with read-ahead                     //
00035 //  - Thread safe                                                       //
00036 //  - Tunable log verbosity level (0 = nothing, 3 = dump read/write     //
00037 //    buffers too!)                                                     //
00038 //  - Many parameters configurable. But the default are OK for nearly   //
00039 //    all the situations.                                               //
00040 //                                                                      //
00042 
00043 #include "XrdClient/XrdClientAbs.hh"
00044 #include "XrdOuc/XrdOucString.hh"
00045 #include "XrdClient/XrdClientThread.hh"
00046 #include "XrdSys/XrdSysSemWait.hh"
00047 #include "XrdVersion.hh"
00048 #include <vector>
00049 #include <string>
00050 
00051 
00052 class XrdClientReadAheadMgr;
00053 
00054 struct XrdClientOpenInfo {
00055     bool      inprogress;
00056     bool      opened;
00057     kXR_unt16 mode;
00058     kXR_unt16 options;
00059 };
00060 
00061 struct XrdClientStatInfo {
00062     int stated;
00063     long long size;
00064     long id;
00065     long flags;
00066     long modtime;
00067 };
00068 
00069 struct XrdClientCounters {
00070    int CacheSize;
00071 
00072    //  This does not take into account the 'suggestions'
00073    // like async read or async readV
00074    //  We count only for functions which return data, eventually
00075    // taken from the cache
00076    long long ReadBytes;
00077    long long WrittenBytes;
00078    long long WriteRequests;
00079 
00080    long long ReadRequests;
00081    long long ReadMisses;
00082    long long ReadHits;
00083    float     ReadMissRate;
00084 
00085    long long ReadVRequests;     // How many readVs (sync) were requested
00086    long long ReadVSubRequests;  // In how many sub-readVs they were split
00087    long long ReadVSubChunks;    // How many subchunks in total
00088    long long ReadVBytes;        // How many bytes were requested (sync)
00089        
00090    long long ReadVAsyncRequests;     // How many readVs (async) were requested
00091    long long ReadVAsyncSubRequests;  // In how many sub-readVs they were split
00092    long long ReadVAsyncSubChunks;    // How many subchunks in total
00093    long long ReadVAsyncBytes;        // How many bytes were requested (async)
00094 
00095    long long ReadAsyncRequests;
00096    long long ReadAsyncBytes;
00097 };
00098 
00099 
00100 class XrdClient : public XrdClientAbs {
00101     friend void *FileOpenerThread(void*, XrdClientThread*);
00102     
00103 
00104 private:
00105 
00106     struct XrdClientOpenInfo    fOpenPars;   // Just a container for the last parameters
00107     // passed to a Open method
00108 
00109     // The open request can be in progress. Further requests must be delayed until
00110     //  finished.
00111     XrdSysCondVar              *fOpenProgCnd;
00112 
00113     // Used to open a file in parallel
00114     XrdClientThread            *fOpenerTh;
00115 
00116     // Used to limit the maximum number of concurrent opens
00117     static XrdSysSemWait        fConcOpenSem;
00118 
00119     bool                        fOpenWithRefresh;
00120 
00121     XrdSysCondVar              *fReadWaitData;  // Used to wait for outstanding data   
00122 
00123     struct XrdClientStatInfo    fStatInfo;
00124 
00125     long                        fReadTrimBlockSize;
00126 
00127     bool                        fUseCache;
00128 
00129     XrdOucString                fInitialUrl;
00130     XrdClientUrlInfo            fUrl;
00131 
00132     bool                        TryOpen(kXR_unt16 mode,
00133                                         kXR_unt16 options,
00134                                         bool doitparallel);
00135 
00136     bool                        LowOpen(const char *file,
00137                                         kXR_unt16 mode,
00138                                         kXR_unt16 options,
00139                                         char *additionalquery = 0);
00140 
00141     void                        TerminateOpenAttempt();
00142 
00143     void                        WaitForNewAsyncData();
00144 
00145     // Real implementation for ReadV 
00146     // To call it we need to be aware of the restrictions so the public
00147     // interface should be ReadV()
00148     kXR_int64                   ReadVEach(char *buf, kXR_int64 *offsets, int *lens, int &nbuf);
00149 
00150     bool                        IsOpenedForWrite() {
00151        // This supposes that no options means read only
00152        if (!fOpenPars.options) return false;
00153        
00154        if (fOpenPars.options & kXR_open_read) return false;
00155        
00156        return true;
00157     }
00158 
00159     XrdClientReadAheadMgr       *fReadAheadMgr;
00160 
00161     void                         PrintCounters();
00162 protected:
00163 
00164     XrdClientCounters           fCounters;
00165 
00166     virtual bool                OpenFileWhenRedirected(char *newfhandle,
00167                                                        bool &wasopen);
00168 
00169     virtual bool                CanRedirOnError() {
00170       // Can redir away on error if no file is opened
00171       // or the file is opened in read mode
00172 
00173       if ( !fOpenPars.opened ) return true;
00174 
00175       return !IsOpenedForWrite();
00176 
00177     }
00178 
00179 
00180 public:
00181 
00182     XrdClient(const char *url, XrdClientCallback *XrdCcb = 0, void *XrdCcbArg = 0);
00183     virtual ~XrdClient();
00184 
00185     UnsolRespProcResult         ProcessUnsolicitedMsg(XrdClientUnsolMsgSender *sender,
00186                                                       XrdClientMessage *unsolmsg);
00187 
00188     bool                        Close();
00189 
00190     // Ask the server to flush its cache
00191     bool                        Sync();
00192 
00193     // Copy the whole file to the local filesystem. Not very efficient.
00194     bool                        Copy(const char *localpath);
00195 
00196     // Returns low level information about the cache
00197     bool                        GetCacheInfo(
00198                                             // The actual cache size
00199                                             int &size,
00200                                             
00201                                             // The number of bytes submitted since the beginning
00202                                             long long &bytessubmitted,
00203                                             
00204                                             // The number of bytes found in the cache (estimate)
00205                                             long long &byteshit,
00206                                             
00207                                             // The number of reads which did not find their data
00208                                             // (estimate)
00209                                             long long &misscount,
00210                                             
00211                                             // miss/totalreads ratio (estimate)
00212                                             float &missrate,
00213                                             
00214                                             // number of read requests towards the cache
00215                                             long long &readreqcnt,
00216                                             
00217                                             // ratio between bytes found / bytes submitted
00218                                             float &bytesusefulness
00219        );
00220 
00221 
00222 
00223     // Returns client-level information about the activity performed up to now
00224     bool                        GetCounters( XrdClientCounters *cnt );
00225 
00226     // Quickly tells if the file is open
00227     inline bool                 IsOpen() { return fOpenPars.opened; }
00228 
00229     // Tells if the file opening is in progress
00230     bool                        IsOpen_inprogress();
00231 
00232     // Tells if the file is open, waiting for the completion of the parallel open
00233     bool                        IsOpen_wait();
00234 
00235     // Open the file. See the xrootd documentation for mode and options
00236     // If parallel, then the open is done by a separate thread, and
00237     // all the operations are delayed until the open has finished
00238     bool                        Open(kXR_unt16 mode, kXR_unt16 options, bool doitparallel=true);
00239 
00240     // Read a block of data. If no error occurs, it returns all the requested bytes.
00241     int                         Read(void *buf, long long offset, int len);
00242 
00243     // Read multiple blocks of data compressed into a sinle one. It's up
00244     // to the application to do the logistic (having the offset and len to find
00245     // the position of the required buffer given the big one). If no error 
00246     // occurs, it returns all the requested bytes.
00247     // NOTE: if buf == 0 then the req will be carried out asynchronously, i.e.
00248     // the result of the request will only populate the internal cache. A subsequent read()
00249     // of that chunk will get the data from the cache
00250     kXR_int64                   ReadV(char *buf, long long *offsets, int *lens, int nbuf);
00251 
00252     // Submit an asynchronous read request. Its result will only populate the cache
00253     //  (if any!!)
00254     XReqErrorType               Read_Async(long long offset, int len, bool updatecounters=true);
00255 
00256     // Get stat info about the file. Normally it tries to guess the file size variations
00257     // unless force==true
00258     bool                        Stat(struct XrdClientStatInfo *stinfo, bool force = false);
00259 
00260     // On-the-fly enabling/disabling of the cache
00261     bool                        UseCache(bool u = TRUE);
00262 
00263     // To instantly remove all the chunks in the cache
00264     void                        RemoveAllDataFromCache() {
00265         if (fConnModule)
00266             fConnModule->RemoveAllDataFromCache();
00267     }
00268 
00269     // To remove pieces of data from the cache
00270     void                        RemoveDataFromCache(long long begin_offs,
00271                                                     long long end_offs, bool remove_overlapped = false) {
00272        if (fConnModule)
00273           fConnModule->RemoveDataFromCache(begin_offs, end_offs, remove_overlapped);
00274     }
00275 
00276     // To set at run time the cache/readahead parameters for this instance only
00277     // If a parameter is < 0 then it's left untouched.
00278     // To simply enable/disable the caching, just use UseCache(), not this function
00279     void                        SetCacheParameters(int CacheSize, int ReadAheadSize, int RmPolicy);
00280 
00281     // To enable/disable different read ahead strategies. Defined in XrdClientReadAhead.hh
00282     void                        SetReadAheadStrategy(int strategy);
00283     
00284     // To enable the trimming of the blocks to read. Blocksize will be rounded to a multiple of 512.
00285     // Each read request will have the offset and length aligned with a multiple of blocksize
00286     // This strategy is similar to a read ahead, but not quite. Here we see it as a transformation
00287     // of the stream of the read accesses to request
00288     void                        SetBlockReadTrimming(int blocksize);
00289     
00290     // Truncates the open file at a specified length
00291     bool                        Truncate(long long len);
00292 
00293     // Write data to the file
00294     bool                        Write(const void *buf, long long offset, int len);
00295 
00296     std::vector<std::string> fExcludedHosts;
00297 
00298 };
00299 
00300 #endif