00001 //------------------------------------------------------------------------------ 00002 // Copyright (c) 2011-2014 by European Organization for Nuclear Research (CERN) 00003 // Author: Lukasz Janyst <ljanyst@cern.ch> 00004 //------------------------------------------------------------------------------ 00005 // This file is part of the XRootD software suite. 00006 // 00007 // XRootD is free software: you can redistribute it and/or modify 00008 // it under the terms of the GNU Lesser General Public License as published by 00009 // the Free Software Foundation, either version 3 of the License, or 00010 // (at your option) any later version. 00011 // 00012 // XRootD is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 // 00017 // You should have received a copy of the GNU Lesser General Public License 00018 // along with XRootD. If not, see <http://www.gnu.org/licenses/>. 00019 // 00020 // In applying this licence, CERN does not waive the privileges and immunities 00021 // granted to it by virtue of its status as an Intergovernmental Organization 00022 // or submit itself to any jurisdiction. 00023 //------------------------------------------------------------------------------ 00024 00025 #ifndef __XRD_CL_FILE_STATE_HANDLER_HH__ 00026 #define __XRD_CL_FILE_STATE_HANDLER_HH__ 00027 00028 #include "XrdCl/XrdClXRootDResponses.hh" 00029 #include "XrdCl/XrdClPostMasterInterfaces.hh" 00030 #include "XrdCl/XrdClFileSystem.hh" 00031 #include "XrdCl/XrdClMessageUtils.hh" 00032 #include "XrdSys/XrdSysPthread.hh" 00033 #include "XrdCl/XrdClLocalFileHandler.hh" 00034 #include <list> 00035 #include <set> 00036 00037 #include <sys/uio.h> 00038 00039 namespace XrdCl 00040 { 00041 class ResponseHandlerHolder; 00042 class Message; 00043 00044 //---------------------------------------------------------------------------- 00046 //---------------------------------------------------------------------------- 00047 class FileStateHandler 00048 { 00049 public: 00050 //------------------------------------------------------------------------ 00052 //------------------------------------------------------------------------ 00053 enum FileStatus 00054 { 00055 Closed, 00056 Opened, 00057 Error, 00058 Recovering, 00059 OpenInProgress, 00060 CloseInProgress 00061 }; 00062 00063 //------------------------------------------------------------------------ 00065 //------------------------------------------------------------------------ 00066 FileStateHandler(); 00067 00068 //------------------------------------------------------------------------ 00073 //------------------------------------------------------------------------ 00074 FileStateHandler( bool useVirtRedirector ); 00075 00076 //------------------------------------------------------------------------ 00078 //------------------------------------------------------------------------ 00079 ~FileStateHandler(); 00080 00081 //------------------------------------------------------------------------ 00091 //------------------------------------------------------------------------ 00092 XRootDStatus Open( const std::string &url, 00093 uint16_t flags, 00094 uint16_t mode, 00095 ResponseHandler *handler, 00096 uint16_t timeout = 0 ); 00097 00098 //------------------------------------------------------------------------ 00105 //------------------------------------------------------------------------ 00106 XRootDStatus Close( ResponseHandler *handler, 00107 uint16_t timeout = 0 ); 00108 00109 //------------------------------------------------------------------------ 00119 //------------------------------------------------------------------------ 00120 XRootDStatus Stat( bool force, 00121 ResponseHandler *handler, 00122 uint16_t timeout = 0 ); 00123 00124 00125 //------------------------------------------------------------------------ 00140 //------------------------------------------------------------------------ 00141 XRootDStatus Read( uint64_t offset, 00142 uint32_t size, 00143 void *buffer, 00144 ResponseHandler *handler, 00145 uint16_t timeout = 0 ); 00146 00147 //------------------------------------------------------------------------ 00157 //------------------------------------------------------------------------ 00158 XRootDStatus Write( uint64_t offset, 00159 uint32_t size, 00160 const void *buffer, 00161 ResponseHandler *handler, 00162 uint16_t timeout = 0 ); 00163 00164 00165 //------------------------------------------------------------------------ 00172 //------------------------------------------------------------------------ 00173 XRootDStatus Sync( ResponseHandler *handler, 00174 uint16_t timeout = 0 ); 00175 00176 //------------------------------------------------------------------------ 00184 //------------------------------------------------------------------------ 00185 XRootDStatus Truncate( uint64_t size, 00186 ResponseHandler *handler, 00187 uint16_t timeout = 0 ); 00188 00189 //------------------------------------------------------------------------ 00198 //------------------------------------------------------------------------ 00199 XRootDStatus VectorRead( const ChunkList &chunks, 00200 void *buffer, 00201 ResponseHandler *handler, 00202 uint16_t timeout = 0 ); 00203 00204 //------------------------------------------------------------------------ 00212 //------------------------------------------------------------------------ 00213 XRootDStatus VectorWrite( const ChunkList &chunks, 00214 ResponseHandler *handler, 00215 uint16_t timeout = 0 ); 00216 00217 //------------------------------------------------------------------------ 00227 //------------------------------------------------------------------------ 00228 XRootDStatus WriteV( uint64_t offset, 00229 const struct iovec *iov, 00230 int iovcnt, 00231 ResponseHandler *handler, 00232 uint16_t timeout = 0 ); 00233 00234 //------------------------------------------------------------------------ 00245 //------------------------------------------------------------------------ 00246 XRootDStatus Fcntl( const Buffer &arg, 00247 ResponseHandler *handler, 00248 uint16_t timeout = 0 ); 00249 00250 //------------------------------------------------------------------------ 00259 //------------------------------------------------------------------------ 00260 XRootDStatus Visa( ResponseHandler *handler, 00261 uint16_t timeout = 0 ); 00262 00263 //------------------------------------------------------------------------ 00265 //------------------------------------------------------------------------ 00266 void OnOpen( const XRootDStatus *status, 00267 const OpenInfo *openInfo, 00268 const HostList *hostList ); 00269 00270 //------------------------------------------------------------------------ 00272 //------------------------------------------------------------------------ 00273 void OnClose( const XRootDStatus *status ); 00274 00275 //------------------------------------------------------------------------ 00277 //------------------------------------------------------------------------ 00278 void OnStateError( XRootDStatus *status, 00279 Message *message, 00280 ResponseHandler *userHandler, 00281 MessageSendParams &sendParams ); 00282 00283 //------------------------------------------------------------------------ 00285 //------------------------------------------------------------------------ 00286 void OnStateRedirection( const std::string &redirectUrl, 00287 Message *message, 00288 ResponseHandler *userHandler, 00289 MessageSendParams &sendParams ); 00290 00291 //------------------------------------------------------------------------ 00293 //------------------------------------------------------------------------ 00294 void OnStateResponse( XRootDStatus *status, 00295 Message *message, 00296 AnyObject *response, 00297 HostList *hostList ); 00298 00299 //------------------------------------------------------------------------ 00301 //------------------------------------------------------------------------ 00302 bool IsOpen() const; 00303 00304 //------------------------------------------------------------------------ 00308 //------------------------------------------------------------------------ 00309 bool SetProperty( const std::string &name, const std::string &value ); 00310 00311 //------------------------------------------------------------------------ 00315 //------------------------------------------------------------------------ 00316 bool GetProperty( const std::string &name, std::string &value ) const; 00317 00318 //------------------------------------------------------------------------ 00320 //------------------------------------------------------------------------ 00321 void Lock() 00322 { 00323 pMutex.Lock(); 00324 } 00325 00326 //------------------------------------------------------------------------ 00328 //------------------------------------------------------------------------ 00329 void UnLock() 00330 { 00331 pMutex.UnLock(); 00332 } 00333 00334 //------------------------------------------------------------------------ 00336 //------------------------------------------------------------------------ 00337 void Tick( time_t now ); 00338 00339 //------------------------------------------------------------------------ 00341 //------------------------------------------------------------------------ 00342 void TimeOutRequests( time_t now ); 00343 00344 //------------------------------------------------------------------------ 00346 //------------------------------------------------------------------------ 00347 void AfterForkChild(); 00348 00349 private: 00350 //------------------------------------------------------------------------ 00351 // Helper for queuing messages 00352 //------------------------------------------------------------------------ 00353 struct RequestData 00354 { 00355 RequestData(): request(0), handler(0) {} 00356 RequestData( Message *r, ResponseHandler *h, 00357 const MessageSendParams &p ): 00358 request(r), handler(h), params(p) {} 00359 Message *request; 00360 ResponseHandler *handler; 00361 MessageSendParams params; 00362 }; 00363 typedef std::list<RequestData> RequestList; 00364 00365 //------------------------------------------------------------------------ 00367 //------------------------------------------------------------------------ 00368 Status SendOrQueue( const URL &url, 00369 Message *msg, 00370 ResponseHandler *handler, 00371 MessageSendParams &sendParams ); 00372 00373 //------------------------------------------------------------------------ 00375 //------------------------------------------------------------------------ 00376 bool IsRecoverable( const XRootDStatus &stataus ) const; 00377 00378 //------------------------------------------------------------------------ 00384 //------------------------------------------------------------------------ 00385 Status RecoverMessage( RequestData rd, bool callbackOnFailure = true ); 00386 00387 //------------------------------------------------------------------------ 00389 //------------------------------------------------------------------------ 00390 Status RunRecovery(); 00391 00392 //------------------------------------------------------------------------ 00393 // Send a close and ignore the response 00394 //------------------------------------------------------------------------ 00395 Status SendClose( uint16_t timeout ); 00396 00397 //------------------------------------------------------------------------ 00399 //------------------------------------------------------------------------ 00400 bool IsReadOnly() const; 00401 00402 //------------------------------------------------------------------------ 00404 //------------------------------------------------------------------------ 00405 Status ReOpenFileAtServer( const URL &url, uint16_t timeout ); 00406 00407 //------------------------------------------------------------------------ 00409 //------------------------------------------------------------------------ 00410 void FailMessage( RequestData rd, XRootDStatus status ); 00411 00412 //------------------------------------------------------------------------ 00414 //------------------------------------------------------------------------ 00415 void FailQueuedMessages( XRootDStatus status ); 00416 00417 //------------------------------------------------------------------------ 00419 //------------------------------------------------------------------------ 00420 void ReSendQueuedMessages(); 00421 00422 //------------------------------------------------------------------------ 00424 //------------------------------------------------------------------------ 00425 void ReWriteFileHandle( Message *msg ); 00426 00427 //------------------------------------------------------------------------ 00429 //------------------------------------------------------------------------ 00430 void ResetMonitoringVars() 00431 { 00432 pOpenTime.tv_sec = 0; pOpenTime.tv_usec = 0; 00433 pRBytes = 0; 00434 pVRBytes = 0; 00435 pWBytes = 0; 00436 pVSegs = 0; 00437 pRCount = 0; 00438 pVRCount = 0; 00439 pWCount = 0; 00440 pCloseReason = Status(); 00441 } 00442 00443 //------------------------------------------------------------------------ 00445 //------------------------------------------------------------------------ 00446 void MonitorClose( const XRootDStatus *status ); 00447 00448 //------------------------------------------------------------------------ 00454 //------------------------------------------------------------------------ 00455 XRootDStatus IssueRequest( const URL &url, 00456 Message *msg, 00457 ResponseHandler *handler, 00458 MessageSendParams &sendParams ); 00459 00460 mutable XrdSysMutex pMutex; 00461 FileStatus pFileState; 00462 XRootDStatus pStatus; 00463 StatInfo *pStatInfo; 00464 URL *pFileUrl; 00465 URL *pDataServer; 00466 URL *pLoadBalancer; 00467 URL *pStateRedirect; 00468 uint8_t *pFileHandle; 00469 uint16_t pOpenMode; 00470 uint16_t pOpenFlags; 00471 RequestList pToBeRecovered; 00472 std::set<Message*> pInTheFly; 00473 uint64_t pSessionId; 00474 bool pDoRecoverRead; 00475 bool pDoRecoverWrite; 00476 bool pFollowRedirects; 00477 bool pUseVirtRedirector; 00478 00479 //------------------------------------------------------------------------ 00480 // Monitoring variables 00481 //------------------------------------------------------------------------ 00482 timeval pOpenTime; 00483 uint64_t pRBytes; 00484 uint64_t pVRBytes; 00485 uint64_t pWBytes; 00486 uint64_t pVWBytes; 00487 uint64_t pVSegs; 00488 uint64_t pRCount; 00489 uint64_t pVRCount; 00490 uint64_t pWCount; 00491 uint64_t pVWCount; 00492 XRootDStatus pCloseReason; 00493 00494 //------------------------------------------------------------------------ 00495 // Holds the OpenHanlder used to issue reopen 00496 // (there is only only OpenHandler reopening a file at a time) 00497 //------------------------------------------------------------------------ 00498 ResponseHandlerHolder *pReOpenHandler; 00499 00500 //------------------------------------------------------------------------ 00501 // Responsible for file:// operations on the local filesystem 00502 //------------------------------------------------------------------------ 00503 LocalFileHandler *pLFileHandler; 00504 }; 00505 } 00506 00507 #endif // __XRD_CL_FILE_STATE_HANDLER_HH__