xrootd
XrdClientConn.hh
Go to the documentation of this file.
1 
2 // //
3 // XrdClientConn //
4 // //
5 // Author: Fabrizio Furano (INFN Padova, 2004) //
6 // Adapted from TXNetFile (root.cern.ch) originally done by //
7 // Alvise Dorigo, Fabrizio Furano //
8 // INFN Padova, 2003 //
9 // //
10 // High level handler of connections to xrootd. //
11 // //
13 
14 #ifndef XRD_CONN_H
15 #define XRD_CONN_H
16 
17 
19 
20 #include "time.h"
25 #include "XrdOuc/XrdOucHash.hh"
26 #include "XrdSys/XrdSysPthread.hh"
27 
28 #define ConnectionManager XrdClientConn::GetConnectionMgr()
29 
30 class XrdClientAbs;
31 class XrdSecProtocol;
32 
34 
35 public:
36 
43  };
48  };
49 
50  // To keep info about an open session
51  struct SessionIDInfo {
52  char id[16];
53  };
54 
58 
59  XrdOucString fRedirOpaque; // Opaque info returned by the server when
60 
61  // redirecting. To be used in the next opens
62  XrdClientConn();
63  virtual ~XrdClientConn();
64 
65  inline bool CacheWillFit(long long bytes) {
66  if (!fMainReadCache)
67  return FALSE;
68  return fMainReadCache->WillFit(bytes);
69  }
70 
71  bool CheckHostDomain(XrdOucString hostToCheck);
72  short Connect(XrdClientUrlInfo Host2Conn,
73  XrdClientAbsUnsolMsgHandler *unsolhandler);
74  void Disconnect(bool ForcePhysicalDisc);
75  virtual bool GetAccessToSrv();
77 
79 
80 
81  static XrdClientPhyConnection *GetPhyConn(int LogConnID);
82 
83 
84  // --------- Cache related stuff
85 
86  long GetDataFromCache(const void *buffer,
87  long long begin_offs,
88  long long end_offs,
89  bool PerfCalc,
90  XrdClientIntvList &missingblks,
91  long &outstandingblks );
92 
94  long long begin_offs,
95  long long end_offs);
96 
97  bool SubmitRawDataToCache(const void *buffer,
98  long long begin_offs,
99  long long end_offs);
100 
101  void SubmitPlaceholderToCache(long long begin_offs,
102  long long end_offs) {
103  if (fMainReadCache)
104  fMainReadCache->PutPlaceholder(begin_offs, end_offs);
105  }
106 
107 
108  void RemoveAllDataFromCache(bool keepwriteblocks=true) {
109  if (fMainReadCache)
110  fMainReadCache->RemoveItems(keepwriteblocks);
111  }
112 
113  void RemoveDataFromCache(long long begin_offs,
114  long long end_offs, bool remove_overlapped = false) {
115  if (fMainReadCache)
116  fMainReadCache->RemoveItems(begin_offs, end_offs, remove_overlapped);
117  }
118 
120  if (fMainReadCache)
122  }
123 
124  void PrintCache() {
125  if (fMainReadCache)
127  }
128 
129 
131  // The actual cache size
132  int &size,
133 
134  // The number of bytes submitted since the beginning
135  long long &bytessubmitted,
136 
137  // The number of bytes found in the cache (estimate)
138  long long &byteshit,
139 
140  // The number of reads which did not find their data
141  // (estimate)
142  long long &misscount,
143 
144  // miss/totalreads ratio (estimate)
145  float &missrate,
146 
147  // number of read requests towards the cache
148  long long &readreqcnt,
149 
150  // ratio between bytes found / bytes submitted
151  float &bytesusefulness
152  ) {
153  if (!fMainReadCache) return false;
154 
155  fMainReadCache->GetInfo(size,
156  bytessubmitted,
157  byteshit,
158  misscount,
159  missrate,
160  readreqcnt,
161  bytesusefulness);
162  return true;
163  }
164 
165 
166  void SetCacheSize(int CacheSize) {
167  if (!fMainReadCache && CacheSize)
169 
170  if (fMainReadCache)
171  fMainReadCache->SetSize(CacheSize);
172  }
173 
174  void SetCacheRmPolicy(int RmPolicy) {
175  if (fMainReadCache)
177  }
178 
179  void UnPinCacheBlk(long long begin_offs, long long end_offs) {
180  fMainReadCache->UnPinCacheBlk(begin_offs, end_offs);
181  // Also use this to signal the possibility to proceed for a hard checkpoint
183  }
184 
185 
186  // -------------------
187 
188 
189  int GetLogConnID() const { return fLogConnID; }
190 
192 
194 
195  inline XrdClientUrlInfo *GetLBSUrl() { return fLBSUrl; }
196  inline XrdClientUrlInfo *GetMetaUrl() { return fMetaUrl; }
197  inline XrdClientUrlInfo GetCurrentUrl() { return fUrl; }
198  inline XrdClientUrlInfo GetRedirUrl() { return fREQUrl; }
199 
200  XErrorCode GetOpenError() const { return fOpenError; }
202  virtual XReqErrorType GoToMetaManager();
203  bool IsConnected() const { return fConnected; }
204  bool IsPhyConnConnected();
205 
208 
211 
213  memset(&LastServerError, 0, sizeof(LastServerError));
215  }
216 
218 
219  virtual bool SendGenCommand(ClientRequest *req,
220  const void *reqMoreData,
221  void **answMoreDataAllocated,
222  void *answMoreData, bool HasToAlloc,
223  char *CmdName, int substreamid = 0);
224 
225  int GetOpenSockFD() const { return fOpenSockFD; }
226 
227  void SetClientHostDomain(const char *src) { fgClientHostDomain = src; }
228  void SetConnected(bool conn) { fConnected = conn; }
229 
230  void SetOpenError(XErrorCode err) { fOpenError = err; }
231 
232  // Gets a parallel stream id to use to set the return path for a re
233  int GetParallelStreamToUse(int reqsperstream);
234  int GetParallelStreamCount(); // Returns the total number of connected streams
235 
237 
238  void SetRequestedDestHost(char *newh, kXR_int32 port) {
239  fREQUrl = fUrl;
240  fREQUrl.Host = newh;
241  fREQUrl.Port = port;
243  }
244 
245  // Puts this instance in pause state for wsec seconds.
246  // A value <= 0 revokes immediately the pause state
248  // Lock mutex
249  fREQWait->Lock();
250 
251  if (wsec > 0)
252  fREQWaitTimeLimit = time(0) + wsec;
253  else {
254  fREQWaitTimeLimit = 0;
255  fREQWait->Broadcast();
256  }
257 
258  // UnLock mutex
259  fREQWait->UnLock();
260  }
261 
262  // Puts this instance in connect-pause state for wsec seconds.
263  // Any future connection attempt will not happen before wsec
264  // and the first one will be towards the given host
266  // Lock mutex
268 
269  if (wsec > 0)
270  fREQConnectWaitTimeLimit = time(0) + wsec;
271  else {
274  }
275 
276  // UnLock mutex
278  }
279 
280  void SetSID(kXR_char *sid);
281  inline void SetUrl(XrdClientUrlInfo thisUrl) { fUrl = thisUrl; }
282 
283  // Sends the request to the server, through logconn with ID LogConnID
284  // The request is sent with a streamid 'child' of the current one, then marked as pending
285  // Its answer will be caught asynchronously
287  const void* reqMoreData,
288  int substreamid = 0);
289 
291  { return fgConnectionMgr;} //Instance of the conn manager
292 
296  }
297 
298  void GetSessionID(SessionIDInfo &sess) {sess = mySessionID;}
299 
300  long GetServerProtocol() { return fServerProto; }
301 
302  short GetMaxRedirCnt() const { return fMaxGlobalRedirCnt; }
303  void SetMaxRedirCnt(short mx) {fMaxGlobalRedirCnt = mx; }
304  short GetRedirCnt() const { return fGlobalRedirCnt; }
305 
306  bool DoWriteSoftCheckPoint();
307  bool DoWriteHardCheckPoint();
308  void UnPinCacheBlk();
309 
310 
311  // To give a max number of seconds for an operation to complete, no matter what happens inside
312  // e.g. redirections, sleeps, failed connection attempts etc.
313  void SetOpTimeLimit(int delta_secs);
314  bool IsOpTimeLimitElapsed(time_t timenow);
315 
316 
317 protected:
318  void SetLogConnID(int cid) { fLogConnID = cid; }
320 
321 
322 
323  // The handler which first tried to connect somewhere
325 
326  XrdClientUrlInfo fUrl; // The current URL
327  XrdClientUrlInfo *fLBSUrl; // Needed to save the load balancer url
328  XrdClientUrlInfo fREQUrl; // For explicitly requested redirs
329 
330  short fGlobalRedirCnt; // Number of redirections
331 
332 private:
333 
334  static XrdOucString fgClientHostDomain; // Save the client's domain name
336  bool fGettingAccessToSrv; // To avoid recursion in desperate situations
337  time_t fGlobalRedirLastUpdateTimestamp; // Timestamp of last redirection
338 
339  int fLogConnID; // Logical connection ID used
340  kXR_unt16 fPrimaryStreamid; // Streamid used for normal communication
341  // NB it's a copy of the one contained in
342  // the logconn
343 
346 
347  // The time limit for a transaction
348  time_t fOpTimeLimit;
349 
350  XrdClientAbs *fRedirHandler; // Pointer to a class inheriting from
351  // XrdClientAbs providing methods
352  // to handle a redir at higher level
353 
354  XrdOucString fRedirInternalToken; // Token returned by the server when
355  // redirecting. To be used in the next logins
356 
357  XrdSysCondVar *fREQWaitResp; // For explicitly requested delayed async responses
359  fREQWaitRespData; // For explicitly requested delayed async responses
360 
361  time_t fREQWaitTimeLimit; // For explicitly requested pause state
362  XrdSysCondVar *fREQWait; // For explicitly requested pause state
363  time_t fREQConnectWaitTimeLimit; // For explicitly requested delayed reconnect
364  XrdSysCondVar *fREQConnectWait; // For explicitly requested delayed reconnect
365 
366  long fServerProto; // The server protocol
367  ERemoteServerType fServerType; // Server type as returned by doHandShake()
368  SessionIDInfo mySessionID; // Login session ID
369 
370 
371  static XrdSysMutex fSessionIDRMutex; // Mutex for the Repo
373  fSessionIDRepo; // The repository of session IDs, shared.
374  // Association between
375  // <hostname>:<port>.<user> and a SessionIDInfo struct
376 
377  int fOpenSockFD; // Descriptor of the underlying socket
378  static XrdClientConnectionMgr *fgConnectionMgr; //Instance of the Connection Manager
379 
381  XrdClientVector<ClientRequest> fWriteReqsToRetry; // To store the write reqs to retry in case of a disconnection
382 
383  bool CheckErrorStatus(XrdClientMessage *, short &, char *);
384  void CheckPort(int &port);
385  void CheckREQPauseState();
387  bool CheckResp(struct ServerResponseHeader *resp, const char *method);
389  const void *reqMoreData,
390  void **answMoreDataAllocated,
391  void *answMoreData,
392  bool HasToAlloc,
393  int substreamid = 0);
394  XrdSecProtocol *DoAuthentication(char *plist, int plsiz);
395 
396  ERemoteServerType DoHandShake(short log);
397 
398  bool DoLogin();
399  bool DomainMatcher(XrdOucString dom, XrdOucString domlist);
400 
402 
404  ClientRequest *);
406 
407  // Sends a close request, without waiting for an answer
408  // useful (?) to be sent just before closing a badly working stream
409  bool PanicClose();
410 
412 
414  ClientRequest *, bool, void**,
416 
417 // void ClearSessionID();
418 
420  const void* reqMoreData,
421  short LogConnID,
422  int substreamid = 0);
423 
424  bool WaitResp(int secsmax);
425 
426  XrdClientUrlInfo *fMetaUrl; // Meta manager url
427  bool fLBSIsMeta; // Is current redirector a meta manager?
428 
429 };
430 
431 
432 
433 #endif