xrootd
XrdClFileStateHandler.hh
Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN)
00003 // Author: Lukasz Janyst <ljanyst@cern.ch>
00004 //------------------------------------------------------------------------------
00005 // XRootD is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU Lesser General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // XRootD is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public License
00016 // along with XRootD.  If not, see <http://www.gnu.org/licenses/>.
00017 //------------------------------------------------------------------------------
00018 
00019 #ifndef __XRD_CL_FILE_STATE_HANDLER_HH__
00020 #define __XRD_CL_FILE_STATE_HANDLER_HH__
00021 
00022 #include "XrdCl/XrdClXRootDResponses.hh"
00023 #include "XrdCl/XrdClPostMasterInterfaces.hh"
00024 #include "XrdCl/XrdClFileSystem.hh"
00025 #include "XrdCl/XrdClMessageUtils.hh"
00026 #include "XrdSys/XrdSysPthread.hh"
00027 #include <list>
00028 #include <set>
00029 
00030 namespace XrdCl
00031 {
00032   class Message;
00033 
00034   //----------------------------------------------------------------------------
00036   //----------------------------------------------------------------------------
00037   class FileStateHandler
00038   {
00039     public:
00040       //------------------------------------------------------------------------
00042       //------------------------------------------------------------------------
00043       enum FileStatus
00044       {
00045         Closed,           
00046         Opened,           
00047         Error,            
00048         Recovering,       
00049         OpenInProgress,   
00050         CloseInProgress   
00051       };
00052 
00053       //------------------------------------------------------------------------
00055       //------------------------------------------------------------------------
00056       FileStateHandler();
00057 
00058       //------------------------------------------------------------------------
00060       //------------------------------------------------------------------------
00061       ~FileStateHandler();
00062 
00063       //------------------------------------------------------------------------
00073       //------------------------------------------------------------------------
00074       XRootDStatus Open( const std::string &url,
00075                          uint16_t           flags,
00076                          uint16_t           mode,
00077                          ResponseHandler   *handler,
00078                          uint16_t           timeout  = 0 );
00079 
00080       //------------------------------------------------------------------------
00087       //------------------------------------------------------------------------
00088       XRootDStatus Close( ResponseHandler *handler,
00089                           uint16_t         timeout = 0 );
00090 
00091       //------------------------------------------------------------------------
00101       //------------------------------------------------------------------------
00102       XRootDStatus Stat( bool             force,
00103                          ResponseHandler *handler,
00104                          uint16_t         timeout = 0 );
00105 
00106 
00107       //------------------------------------------------------------------------
00122       //------------------------------------------------------------------------
00123       XRootDStatus Read( uint64_t         offset,
00124                          uint32_t         size,
00125                          void            *buffer,
00126                          ResponseHandler *handler,
00127                          uint16_t         timeout = 0 );
00128 
00129       //------------------------------------------------------------------------
00139       //------------------------------------------------------------------------
00140       XRootDStatus Write( uint64_t         offset,
00141                           uint32_t         size,
00142                           const void      *buffer,
00143                           ResponseHandler *handler,
00144                           uint16_t         timeout = 0 );
00145 
00146 
00147       //------------------------------------------------------------------------
00154       //------------------------------------------------------------------------
00155       XRootDStatus Sync( ResponseHandler *handler,
00156                          uint16_t         timeout = 0 );
00157 
00158       //------------------------------------------------------------------------
00166       //------------------------------------------------------------------------
00167       XRootDStatus Truncate( uint64_t         size,
00168                              ResponseHandler *handler,
00169                              uint16_t         timeout = 0 );
00170 
00171       //------------------------------------------------------------------------
00180       //------------------------------------------------------------------------
00181       XRootDStatus VectorRead( const ChunkList &chunks,
00182                                void            *buffer,
00183                                ResponseHandler *handler,
00184                                uint16_t         timeout = 0 );
00185 
00186       //------------------------------------------------------------------------
00188       //------------------------------------------------------------------------
00189       void OnOpen( const XRootDStatus *status,
00190                    const OpenInfo     *openInfo,
00191                    const HostList     *hostList );
00192 
00193       //------------------------------------------------------------------------
00195       //------------------------------------------------------------------------
00196       void OnClose( const XRootDStatus *status );
00197 
00198       //------------------------------------------------------------------------
00200       //------------------------------------------------------------------------
00201       void OnStateError( XRootDStatus        *status,
00202                          Message             *message,
00203                          ResponseHandler     *userHandler,
00204                          MessageSendParams   &sendParams );
00205 
00206       //------------------------------------------------------------------------
00208       //------------------------------------------------------------------------
00209       void OnStateRedirection( RedirectInfo      *redirectInfo,
00210                                Message           *message,
00211                                ResponseHandler   *userHandler,
00212                                MessageSendParams &sendParams );
00213 
00214       //------------------------------------------------------------------------
00216       //------------------------------------------------------------------------
00217       void OnStateResponse( XRootDStatus *status,
00218                             Message      *message,
00219                             AnyObject    *response,
00220                             HostList     *hostList );
00221 
00222       //------------------------------------------------------------------------
00224       //------------------------------------------------------------------------
00225       bool IsOpen() const;
00226 
00227       //------------------------------------------------------------------------
00230       //------------------------------------------------------------------------
00231       void EnableReadRecovery( bool enable );
00232 
00233       //------------------------------------------------------------------------
00236       //------------------------------------------------------------------------
00237       void EnableWriteRecovery( bool enable );
00238 
00239       //------------------------------------------------------------------------
00241       //------------------------------------------------------------------------
00242       std::string GetDataServer() const;
00243 
00244       //------------------------------------------------------------------------
00246       //------------------------------------------------------------------------
00247       URL GetLastURL() const;
00248 
00249       //------------------------------------------------------------------------
00251       //------------------------------------------------------------------------
00252       void Lock()
00253       {
00254         pMutex.Lock();
00255       }
00256 
00257       //------------------------------------------------------------------------
00259       //------------------------------------------------------------------------
00260       void UnLock()
00261       {
00262         pMutex.UnLock();
00263       }
00264 
00265       //------------------------------------------------------------------------
00267       //------------------------------------------------------------------------
00268       void Tick( time_t now );
00269 
00270       //------------------------------------------------------------------------
00272       //------------------------------------------------------------------------
00273       void TimeOutRequests( time_t now );
00274 
00275       //------------------------------------------------------------------------
00277       //------------------------------------------------------------------------
00278       void AfterForkChild();
00279 
00280     private:
00281       //------------------------------------------------------------------------
00282       // Helper for queuing messages
00283       //------------------------------------------------------------------------
00284       struct RequestData
00285       {
00286         RequestData(): request(0), handler(0) {}
00287         RequestData( Message *r, ResponseHandler *h,
00288                      const MessageSendParams &p ):
00289           request(r), handler(h), params(p) {}
00290         Message           *request;
00291         ResponseHandler   *handler;
00292         MessageSendParams  params;
00293       };
00294       typedef std::list<RequestData> RequestList;
00295 
00296       //------------------------------------------------------------------------
00298       //------------------------------------------------------------------------
00299       Status SendOrQueue( const URL         &url,
00300                           Message           *msg,
00301                           ResponseHandler   *handler,
00302                           MessageSendParams &sendParams );
00303 
00304       //------------------------------------------------------------------------
00306       //------------------------------------------------------------------------
00307       bool IsRecoverable( const XRootDStatus &stataus ) const;
00308 
00309       //------------------------------------------------------------------------
00315       //------------------------------------------------------------------------
00316       Status RecoverMessage( RequestData rd, bool callbackOnFailure = true );
00317 
00318       //------------------------------------------------------------------------
00320       //------------------------------------------------------------------------
00321       Status RunRecovery();
00322 
00323       //------------------------------------------------------------------------
00325       //------------------------------------------------------------------------
00326       bool IsReadOnly() const;
00327 
00328       //------------------------------------------------------------------------
00330       //------------------------------------------------------------------------
00331       Status ReOpenFileAtServer( const URL &url, uint16_t timeout );
00332 
00333       //------------------------------------------------------------------------
00335       //------------------------------------------------------------------------
00336       void FailMessage( RequestData rd, XRootDStatus status );
00337 
00338       //------------------------------------------------------------------------
00340       //------------------------------------------------------------------------
00341       void FailQueuedMessages( XRootDStatus status );
00342 
00343       //------------------------------------------------------------------------
00345       //------------------------------------------------------------------------
00346       void ReSendQueuedMessages();
00347 
00348       //------------------------------------------------------------------------
00350       //------------------------------------------------------------------------
00351       void ReWriteFileHandle( Message *msg );
00352 
00353       //------------------------------------------------------------------------
00355       //------------------------------------------------------------------------
00356       void ResetMonitoringVars()
00357       {
00358         pOpenTime.tv_sec = 0; pOpenTime.tv_usec = 0;
00359         pRBytes      = 0;
00360         pVBytes      = 0;
00361         pWBytes      = 0;
00362         pVSegs       = 0;
00363         pRCount      = 0;
00364         pVCount      = 0;
00365         pWCount      = 0;
00366         pCloseReason = Status();
00367       }
00368 
00369       //------------------------------------------------------------------------
00371       //------------------------------------------------------------------------
00372       void MonitorClose( const XRootDStatus *status );
00373 
00374       mutable XrdSysMutex     pMutex;
00375       FileStatus              pFileState;
00376       XRootDStatus            pStatus;
00377       StatInfo               *pStatInfo;
00378       URL                    *pFileUrl;
00379       URL                    *pDataServer;
00380       URL                    *pLoadBalancer;
00381       URL                    *pStateRedirect;
00382       uint8_t                *pFileHandle;
00383       uint16_t                pOpenMode;
00384       uint16_t                pOpenFlags;
00385       RequestList             pToBeRecovered;
00386       std::set<Message*>      pInTheFly;
00387       uint64_t                pSessionId;
00388       bool                    pDoRecoverRead;
00389       bool                    pDoRecoverWrite;
00390 
00391       //------------------------------------------------------------------------
00392       // Monitoring variables
00393       //------------------------------------------------------------------------
00394       timeval                  pOpenTime;
00395       uint64_t                 pRBytes;
00396       uint64_t                 pVBytes;
00397       uint64_t                 pWBytes;
00398       uint64_t                 pVSegs;
00399       uint64_t                 pRCount;
00400       uint64_t                 pVCount;
00401       uint64_t                 pWCount;
00402       XRootDStatus             pCloseReason;
00403   };
00404 }
00405 
00406 #endif // __XRD_CL_FILE_STATE_HANDLER_HH__