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 "XrdCl/XrdClOptional.hh" 00035 #include <list> 00036 #include <set> 00037 #include <vector> 00038 00039 #include <sys/uio.h> 00040 00041 namespace XrdCl 00042 { 00043 class ResponseHandlerHolder; 00044 class Message; 00045 00046 //---------------------------------------------------------------------------- 00048 //---------------------------------------------------------------------------- 00049 class FileStateHandler 00050 { 00051 public: 00052 //------------------------------------------------------------------------ 00054 //------------------------------------------------------------------------ 00055 enum FileStatus 00056 { 00057 Closed, 00058 Opened, 00059 Error, 00060 Recovering, 00061 OpenInProgress, 00062 CloseInProgress 00063 }; 00064 00065 //------------------------------------------------------------------------ 00067 //------------------------------------------------------------------------ 00068 FileStateHandler(); 00069 00070 //------------------------------------------------------------------------ 00075 //------------------------------------------------------------------------ 00076 FileStateHandler( bool useVirtRedirector ); 00077 00078 //------------------------------------------------------------------------ 00080 //------------------------------------------------------------------------ 00081 ~FileStateHandler(); 00082 00083 //------------------------------------------------------------------------ 00093 //------------------------------------------------------------------------ 00094 XRootDStatus Open( const std::string &url, 00095 uint16_t flags, 00096 uint16_t mode, 00097 ResponseHandler *handler, 00098 uint16_t timeout = 0 ); 00099 00100 //------------------------------------------------------------------------ 00107 //------------------------------------------------------------------------ 00108 XRootDStatus Close( ResponseHandler *handler, 00109 uint16_t timeout = 0 ); 00110 00111 //------------------------------------------------------------------------ 00121 //------------------------------------------------------------------------ 00122 XRootDStatus Stat( bool force, 00123 ResponseHandler *handler, 00124 uint16_t timeout = 0 ); 00125 00126 00127 //------------------------------------------------------------------------ 00142 //------------------------------------------------------------------------ 00143 XRootDStatus Read( uint64_t offset, 00144 uint32_t size, 00145 void *buffer, 00146 ResponseHandler *handler, 00147 uint16_t timeout = 0 ); 00148 00149 //------------------------------------------------------------------------ 00159 //------------------------------------------------------------------------ 00160 XRootDStatus Write( uint64_t offset, 00161 uint32_t size, 00162 const void *buffer, 00163 ResponseHandler *handler, 00164 uint16_t timeout = 0 ); 00165 00166 //------------------------------------------------------------------------ 00176 //------------------------------------------------------------------------ 00177 XRootDStatus Write( uint64_t offset, 00178 Buffer &&buffer, 00179 ResponseHandler *handler, 00180 uint16_t timeout = 0 ); 00181 00182 //------------------------------------------------------------------------ 00195 //------------------------------------------------------------------------ 00196 XRootDStatus Write( uint64_t offset, 00197 uint32_t size, 00198 Optional<uint64_t> fdoff, 00199 int fd, 00200 ResponseHandler *handler, 00201 uint16_t timeout = 0 ); 00202 00203 00204 //------------------------------------------------------------------------ 00211 //------------------------------------------------------------------------ 00212 XRootDStatus Sync( ResponseHandler *handler, 00213 uint16_t timeout = 0 ); 00214 00215 //------------------------------------------------------------------------ 00223 //------------------------------------------------------------------------ 00224 XRootDStatus Truncate( uint64_t size, 00225 ResponseHandler *handler, 00226 uint16_t timeout = 0 ); 00227 00228 //------------------------------------------------------------------------ 00237 //------------------------------------------------------------------------ 00238 XRootDStatus VectorRead( const ChunkList &chunks, 00239 void *buffer, 00240 ResponseHandler *handler, 00241 uint16_t timeout = 0 ); 00242 00243 //------------------------------------------------------------------------ 00251 //------------------------------------------------------------------------ 00252 XRootDStatus VectorWrite( const ChunkList &chunks, 00253 ResponseHandler *handler, 00254 uint16_t timeout = 0 ); 00255 00256 //------------------------------------------------------------------------ 00266 //------------------------------------------------------------------------ 00267 XRootDStatus WriteV( uint64_t offset, 00268 const struct iovec *iov, 00269 int iovcnt, 00270 ResponseHandler *handler, 00271 uint16_t timeout = 0 ); 00272 00273 //------------------------------------------------------------------------ 00284 //------------------------------------------------------------------------ 00285 XRootDStatus Fcntl( const Buffer &arg, 00286 ResponseHandler *handler, 00287 uint16_t timeout = 0 ); 00288 00289 //------------------------------------------------------------------------ 00298 //------------------------------------------------------------------------ 00299 XRootDStatus Visa( ResponseHandler *handler, 00300 uint16_t timeout = 0 ); 00301 00302 //------------------------------------------------------------------------ 00313 //------------------------------------------------------------------------ 00314 XRootDStatus SetXAttr( const std::vector<xattr_t> &attrs, 00315 ResponseHandler *handler, 00316 uint16_t timeout = 0 ); 00317 00318 //------------------------------------------------------------------------ 00329 //------------------------------------------------------------------------ 00330 XRootDStatus GetXAttr( const std::vector<std::string> &attrs, 00331 ResponseHandler *handler, 00332 uint16_t timeout = 0 ); 00333 00334 //------------------------------------------------------------------------ 00345 //------------------------------------------------------------------------ 00346 XRootDStatus DelXAttr( const std::vector<std::string> &attrs, 00347 ResponseHandler *handler, 00348 uint16_t timeout = 0 ); 00349 00350 //------------------------------------------------------------------------ 00360 //------------------------------------------------------------------------ 00361 XRootDStatus ListXAttr( ResponseHandler *handler, 00362 uint16_t timeout = 0 ); 00363 00364 //------------------------------------------------------------------------ 00366 //------------------------------------------------------------------------ 00367 void OnOpen( const XRootDStatus *status, 00368 const OpenInfo *openInfo, 00369 const HostList *hostList ); 00370 00371 //------------------------------------------------------------------------ 00373 //------------------------------------------------------------------------ 00374 void OnClose( const XRootDStatus *status ); 00375 00376 //------------------------------------------------------------------------ 00378 //------------------------------------------------------------------------ 00379 void OnStateError( XRootDStatus *status, 00380 Message *message, 00381 ResponseHandler *userHandler, 00382 MessageSendParams &sendParams ); 00383 00384 //------------------------------------------------------------------------ 00386 //------------------------------------------------------------------------ 00387 void OnStateRedirection( const std::string &redirectUrl, 00388 Message *message, 00389 ResponseHandler *userHandler, 00390 MessageSendParams &sendParams ); 00391 00392 //------------------------------------------------------------------------ 00394 //------------------------------------------------------------------------ 00395 void OnStateResponse( XRootDStatus *status, 00396 Message *message, 00397 AnyObject *response, 00398 HostList *hostList ); 00399 00400 //------------------------------------------------------------------------ 00402 //------------------------------------------------------------------------ 00403 bool IsOpen() const; 00404 00405 //------------------------------------------------------------------------ 00409 //------------------------------------------------------------------------ 00410 bool SetProperty( const std::string &name, const std::string &value ); 00411 00412 //------------------------------------------------------------------------ 00416 //------------------------------------------------------------------------ 00417 bool GetProperty( const std::string &name, std::string &value ) const; 00418 00419 //------------------------------------------------------------------------ 00421 //------------------------------------------------------------------------ 00422 void Lock() 00423 { 00424 pMutex.Lock(); 00425 } 00426 00427 //------------------------------------------------------------------------ 00429 //------------------------------------------------------------------------ 00430 void UnLock() 00431 { 00432 pMutex.UnLock(); 00433 } 00434 00435 //------------------------------------------------------------------------ 00437 //------------------------------------------------------------------------ 00438 void Tick( time_t now ); 00439 00440 //------------------------------------------------------------------------ 00442 //------------------------------------------------------------------------ 00443 void TimeOutRequests( time_t now ); 00444 00445 //------------------------------------------------------------------------ 00447 //------------------------------------------------------------------------ 00448 void AfterForkChild(); 00449 00450 private: 00451 //------------------------------------------------------------------------ 00452 // Helper for queuing messages 00453 //------------------------------------------------------------------------ 00454 struct RequestData 00455 { 00456 RequestData(): request(0), handler(0) {} 00457 RequestData( Message *r, ResponseHandler *h, 00458 const MessageSendParams &p ): 00459 request(r), handler(h), params(p) {} 00460 Message *request; 00461 ResponseHandler *handler; 00462 MessageSendParams params; 00463 }; 00464 typedef std::list<RequestData> RequestList; 00465 00466 //------------------------------------------------------------------------ 00473 //------------------------------------------------------------------------ 00474 template<typename T> 00475 Status XAttrOperationImpl( kXR_char subcode, 00476 kXR_char options, 00477 const std::vector<T> &attrs, 00478 ResponseHandler *handler, 00479 uint16_t timeout = 0 ); 00480 00481 //------------------------------------------------------------------------ 00483 //------------------------------------------------------------------------ 00484 Status SendOrQueue( const URL &url, 00485 Message *msg, 00486 ResponseHandler *handler, 00487 MessageSendParams &sendParams ); 00488 00489 //------------------------------------------------------------------------ 00491 //------------------------------------------------------------------------ 00492 bool IsRecoverable( const XRootDStatus &stataus ) const; 00493 00494 //------------------------------------------------------------------------ 00500 //------------------------------------------------------------------------ 00501 Status RecoverMessage( RequestData rd, bool callbackOnFailure = true ); 00502 00503 //------------------------------------------------------------------------ 00505 //------------------------------------------------------------------------ 00506 Status RunRecovery(); 00507 00508 //------------------------------------------------------------------------ 00509 // Send a close and ignore the response 00510 //------------------------------------------------------------------------ 00511 XRootDStatus SendClose( uint16_t timeout ); 00512 00513 //------------------------------------------------------------------------ 00515 //------------------------------------------------------------------------ 00516 bool IsReadOnly() const; 00517 00518 //------------------------------------------------------------------------ 00520 //------------------------------------------------------------------------ 00521 XRootDStatus ReOpenFileAtServer( const URL &url, uint16_t timeout ); 00522 00523 //------------------------------------------------------------------------ 00525 //------------------------------------------------------------------------ 00526 void FailMessage( RequestData rd, XRootDStatus status ); 00527 00528 //------------------------------------------------------------------------ 00530 //------------------------------------------------------------------------ 00531 void FailQueuedMessages( XRootDStatus status ); 00532 00533 //------------------------------------------------------------------------ 00535 //------------------------------------------------------------------------ 00536 void ReSendQueuedMessages(); 00537 00538 //------------------------------------------------------------------------ 00540 //------------------------------------------------------------------------ 00541 void ReWriteFileHandle( Message *msg ); 00542 00543 //------------------------------------------------------------------------ 00545 //------------------------------------------------------------------------ 00546 void ResetMonitoringVars() 00547 { 00548 pOpenTime.tv_sec = 0; pOpenTime.tv_usec = 0; 00549 pRBytes = 0; 00550 pVRBytes = 0; 00551 pWBytes = 0; 00552 pVSegs = 0; 00553 pRCount = 0; 00554 pVRCount = 0; 00555 pWCount = 0; 00556 pCloseReason = Status(); 00557 } 00558 00559 //------------------------------------------------------------------------ 00561 //------------------------------------------------------------------------ 00562 void MonitorClose( const XRootDStatus *status ); 00563 00564 //------------------------------------------------------------------------ 00570 //------------------------------------------------------------------------ 00571 XRootDStatus IssueRequest( const URL &url, 00572 Message *msg, 00573 ResponseHandler *handler, 00574 MessageSendParams &sendParams ); 00575 00576 mutable XrdSysMutex pMutex; 00577 FileStatus pFileState; 00578 XRootDStatus pStatus; 00579 StatInfo *pStatInfo; 00580 URL *pFileUrl; 00581 URL *pDataServer; 00582 URL *pLoadBalancer; 00583 URL *pStateRedirect; 00584 uint8_t *pFileHandle; 00585 uint16_t pOpenMode; 00586 uint16_t pOpenFlags; 00587 RequestList pToBeRecovered; 00588 std::set<Message*> pInTheFly; 00589 uint64_t pSessionId; 00590 bool pDoRecoverRead; 00591 bool pDoRecoverWrite; 00592 bool pFollowRedirects; 00593 bool pUseVirtRedirector; 00594 00595 //------------------------------------------------------------------------ 00596 // Monitoring variables 00597 //------------------------------------------------------------------------ 00598 timeval pOpenTime; 00599 uint64_t pRBytes; 00600 uint64_t pVRBytes; 00601 uint64_t pWBytes; 00602 uint64_t pVWBytes; 00603 uint64_t pVSegs; 00604 uint64_t pRCount; 00605 uint64_t pVRCount; 00606 uint64_t pWCount; 00607 uint64_t pVWCount; 00608 XRootDStatus pCloseReason; 00609 00610 //------------------------------------------------------------------------ 00611 // Holds the OpenHanlder used to issue reopen 00612 // (there is only only OpenHandler reopening a file at a time) 00613 //------------------------------------------------------------------------ 00614 ResponseHandlerHolder *pReOpenHandler; 00615 00616 //------------------------------------------------------------------------ 00617 // Responsible for file:// operations on the local filesystem 00618 //------------------------------------------------------------------------ 00619 LocalFileHandler *pLFileHandler; 00620 }; 00621 } 00622 00623 #endif // __XRD_CL_FILE_STATE_HANDLER_HH__