TProofMgr.cxx

Go to the documentation of this file.
00001 // @(#)root/proof:$Id: TProofMgr.cxx 32930 2010-04-09 16:18:32Z pcanal $
00002 // Author: G. Ganis, Nov 2005
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // TProofMgr                                                            //
00015 //                                                                      //
00016 // The PROOF manager interacts with the PROOF server coordinator to     //
00017 // create or destroy a PROOF session, attach to or detach from          //
00018 // existing one, and to monitor any client activity on the cluster.     //
00019 // At most one manager instance per server is allowed.                  //
00020 //                                                                      //
00021 //////////////////////////////////////////////////////////////////////////
00022 
00023 #include "TError.h"
00024 #include "TEnv.h"
00025 #include "TList.h"
00026 #include "TProof.h"
00027 #include "TProofMgr.h"
00028 #include "TProofMgrLite.h"
00029 #include "TROOT.h"
00030 
00031 ClassImp(TProofMgr)
00032 
00033 // Sub-list of TROOT::fProofs with managers
00034 TList TProofMgr::fgListOfManagers;
00035 TProofMgr_t TProofMgr::fgTXProofMgrHook = 0;
00036 
00037 //______________________________________________________________________________
00038 TProofMgr::TProofMgr(const char *url, Int_t, const char *alias)
00039           : TNamed("",""), fRemoteProtocol(-1), fServType(kXProofd),
00040             fSessions(0), fIntHandler(0)
00041 {
00042    // Create a PROOF manager for the standard (old) environment.
00043 
00044    fServType = kProofd;
00045 
00046    // AVoid problems with empty URLs
00047    fUrl = (!url || strlen(url) <= 0) ? TUrl("proof://localhost") : TUrl(url);
00048 
00049    // Correct URL protocol
00050    if (!strcmp(fUrl.GetProtocol(), TUrl("a").GetProtocol()))
00051       fUrl.SetProtocol("proof");
00052 
00053    // Check port
00054    if (fUrl.GetPort() == TUrl("a").GetPort()) {
00055       // For the time being we use 'rootd' service as default.
00056       // This will be changed to 'proofd' as soon as XRD will be able to
00057       // accept on multiple ports
00058       Int_t port = gSystem->GetServiceByName("proofd");
00059       if (port < 0) {
00060          if (gDebug > 0)
00061             Info("TProofMgr","service 'proofd' not found by GetServiceByName"
00062                               ": using default IANA assigned tcp port 1093");
00063          port = 1093;
00064       } else {
00065          if (gDebug > 1)
00066             Info("TProofMgr","port from GetServiceByName: %d", port);
00067       }
00068       fUrl.SetPort(port);
00069    }
00070 
00071    // Make sure that the user is defined
00072    if (strlen(fUrl.GetUser()) <= 0) {
00073       // Fill in the default user
00074       UserGroup_t *pw = gSystem->GetUserInfo();
00075       if (pw) {
00076          fUrl.SetUser(pw->fUser);
00077          delete pw;
00078       }
00079    }
00080 
00081    // Check and save the host FQDN ...
00082    if (strcmp(fUrl.GetHost(), "__lite__")) {
00083       if (strcmp(fUrl.GetHost(), fUrl.GetHostFQDN()))
00084          fUrl.SetHost(fUrl.GetHostFQDN());
00085    }
00086 
00087    SetName(fUrl.GetUrl(kTRUE));
00088    if (alias)
00089       SetAlias(alias);
00090    else
00091       SetAlias(fUrl.GetHost());
00092 }
00093 
00094 //______________________________________________________________________________
00095 TProofMgr::~TProofMgr()
00096 {
00097    // Destroy a TProofMgr instance
00098 
00099    SafeDelete(fSessions);
00100    SafeDelete(fIntHandler);
00101 
00102    fgListOfManagers.Remove(this);
00103    gROOT->GetListOfProofs()->Remove(this);
00104 }
00105 
00106 //______________________________________________________________________________
00107 TProof *TProofMgr::AttachSession(Int_t id, Bool_t gui)
00108 {
00109    // Dummy version provided for completeness. Just returns a pointer to
00110    // existing session 'id' (as shown by TProof::QuerySessions) or 0 if 'id' is
00111    // not valid. The boolena 'gui' should be kTRUE when invoked from the GUI.
00112 
00113    TProofDesc *d = GetProofDesc(id);
00114    if (d)
00115       return AttachSession(d, gui);
00116 
00117    Info("AttachSession","invalid proofserv id (%d)", id);
00118    return 0;
00119 }
00120 
00121 //______________________________________________________________________________
00122 TProof *TProofMgr::AttachSession(TProofDesc *d, Bool_t)
00123 {
00124    // Dummy version provided for completeness. Just returns a pointer to
00125    // existing session 'id' (as shown by TProof::QuerySessions) or 0 if 'id' is
00126    // not valid.
00127 
00128    if (!d) {
00129       Warning("AttachSession","invalid description object - do nothing");
00130       return 0;
00131    }
00132 
00133    if (d->GetProof())
00134       // Nothing to do if already in contact with proofserv
00135       return d->GetProof();
00136 
00137    Warning("AttachSession","session not available - do nothing");
00138    return 0;
00139 }
00140 
00141 //______________________________________________________________________________
00142 void TProofMgr::DetachSession(Int_t id, Option_t *opt)
00143 {
00144    // Detach session with 'id' from its proofserv. The 'id' is the number
00145    // shown by QuerySessions. The correspondent TProof object is deleted.
00146    // If id == 0 all the known sessions are detached.
00147    // Option opt="S" or "s" forces session shutdown.
00148 
00149    if (!IsValid()) {
00150       Warning("DetachSession","invalid TProofMgr - do nothing");
00151       return;
00152    }
00153 
00154    if (id > 0) {
00155 
00156       TProofDesc *d = GetProofDesc(id);
00157       if (d) {
00158          if (d->GetProof())
00159             d->GetProof()->Detach(opt);
00160          TProof *p = d->GetProof();
00161          fSessions->Remove(d);
00162          SafeDelete(p);
00163          delete d;
00164       }
00165 
00166    } else if (id == 0) {
00167 
00168       // Requesto to destroy all sessions
00169       if (fSessions) {
00170          // Delete PROOF sessions
00171          TIter nxd(fSessions);
00172          TProofDesc *d = 0;
00173          while ((d = (TProofDesc *)nxd())) {
00174             if (d->GetProof())
00175                d->GetProof()->Detach(opt);
00176             TProof *p = d->GetProof();
00177             fSessions->Remove(d);
00178             SafeDelete(p);
00179          }
00180          fSessions->Delete();
00181       }
00182    }
00183 
00184    return;
00185 }
00186 
00187 //______________________________________________________________________________
00188 void TProofMgr::DetachSession(TProof *p, Option_t *opt)
00189 {
00190    // Detach session 'p' from its proofserv. The instance 'p' is invalidated
00191    // and should be deleted by the caller
00192 
00193    if (!IsValid()) {
00194       Warning("DetachSession","invalid TProofMgr - do nothing");
00195       return;
00196    }
00197 
00198    if (p) {
00199       // Single session request
00200       TProofDesc *d = GetProofDesc(p);
00201       if (d) {
00202          if (d->GetProof())
00203             // The session is closed here
00204             d->GetProof()->Detach(opt);
00205          fSessions->Remove(d);
00206          delete d;
00207       }
00208    }
00209 
00210    return;
00211 }
00212 
00213 //______________________________________________________________________________
00214 TList *TProofMgr::QuerySessions(Option_t *opt)
00215 {
00216    // Get list of sessions accessible to this manager.
00217 
00218    if (opt && !strncasecmp(opt,"L",1))
00219       // Just return the existing list
00220       return fSessions;
00221 
00222    // Create list if not existing
00223    if (!fSessions) {
00224       fSessions = new TList();
00225       fSessions->SetOwner();
00226    }
00227 
00228    // Fill-in entries from the official list
00229    if (gROOT->GetListOfProofs()) {
00230       // Loop over
00231       TIter nxp(gROOT->GetListOfProofs());
00232       TObject *o = 0;
00233       TProof *p = 0;
00234       Int_t ns = 0;
00235       while ((o = nxp())) {
00236          if (o->InheritsFrom(TProof::Class())) {
00237             p = (TProof *)o;
00238             // Only those belonging to this server
00239             if (MatchUrl(p->GetUrl())) {
00240                if (!(fSessions->FindObject(p->GetSessionTag()))) {
00241                   Int_t st = (p->IsIdle()) ? TProofDesc::kIdle
00242                                           : TProofDesc::kRunning;
00243                   TProofDesc *d =
00244                      new TProofDesc(p->GetName(), p->GetTitle(), p->GetUrl(),
00245                                           ++ns, p->GetSessionID(), st, p);
00246                   fSessions->Add(d);
00247                }
00248             }
00249          }
00250       }
00251    }
00252 
00253    // Drop entries not existing any longer
00254    if (fSessions->GetSize() > 0) {
00255       TIter nxd(fSessions);
00256       TProofDesc *d = 0;
00257       while ((d = (TProofDesc *)nxd())) {
00258          if (d->GetProof()) {
00259             if (!(gROOT->GetListOfProofs()->FindObject(d->GetProof()))) {
00260                fSessions->Remove(d);
00261                SafeDelete(d);
00262             } else {
00263                if (opt && !strncasecmp(opt,"S",1))
00264                   d->Print("");
00265             }
00266          }
00267       }
00268    }
00269 
00270    // We are done
00271    return fSessions;
00272 }
00273 
00274 //______________________________________________________________________________
00275 Int_t TProofMgr::SendMsgToUsers(const char *, const char *)
00276 {
00277    // Send a message to connected users. Only superusers can do this.
00278    // The first argument specifies the message or the file from where to take
00279    // the message.
00280    // The second argument specifies the user to which to send the message: if
00281    // empty or null the message is send to all the connected users.
00282    // return 0 in case of success, -1 in case of error
00283 
00284    Warning("SendMsgToUsers","functionality not supported");
00285 
00286    return -1;
00287 }
00288 
00289 //______________________________________________________________________________
00290 Int_t TProofMgr::Reset(Bool_t, const char *)
00291 {
00292    // Send a cleanup request for the sessions associated with the current
00293    // user.
00294    // Not supported.
00295 
00296    Warning("Reset","functionality not supported");
00297 
00298    return -1;
00299 }
00300 
00301 //______________________________________________________________________________
00302 void TProofMgr::ShowWorkers()
00303 {
00304    // Show available workers
00305 
00306    AbstractMethod("ShowWorkers");
00307 }
00308 
00309 //______________________________________________________________________________
00310 TProofDesc *TProofMgr::GetProofDesc(Int_t id)
00311 {
00312    // Get TProofDesc instance corresponding to 'id'.
00313 
00314    TProofDesc *d = 0;
00315    if (id > 0) {
00316       // Retrieve an updated list
00317       QuerySessions("");
00318       if (fSessions) {
00319          TIter nxd(fSessions);
00320          while ((d = (TProofDesc *)nxd())) {
00321             if (d->MatchId(id))
00322                return d;
00323          }
00324       }
00325    }
00326 
00327    return d;
00328 }
00329 
00330 //______________________________________________________________________________
00331 TProofDesc *TProofMgr::GetProofDesc(TProof *p)
00332 {
00333    // Get TProofDesc instance corresponding to TProof object 'p'.
00334 
00335    TProofDesc *d = 0;
00336    if (p) {
00337       // Retrieve an updated list
00338       QuerySessions("");
00339       if (fSessions) {
00340          TIter nxd(fSessions);
00341          while ((d = (TProofDesc *)nxd())) {
00342             if (p == d->GetProof())
00343                return d;
00344          }
00345       }
00346    }
00347 
00348    return d;
00349 }
00350 
00351 //______________________________________________________________________________
00352 void TProofMgr::DiscardSession(TProof *p)
00353 {
00354    // Discard TProofDesc of session 'p' from the internal list
00355 
00356    if (p) {
00357       TProofDesc *d = 0;
00358       if (fSessions) {
00359          TIter nxd(fSessions);
00360          while ((d = (TProofDesc *)nxd())) {
00361             if (p == d->GetProof()) {
00362                fSessions->Remove(d);
00363                delete d;
00364                break;
00365             }
00366          }
00367       }
00368    }
00369 }
00370 
00371 //______________________________________________________________________________
00372 TProof *TProofMgr::CreateSession(const char *cfg,
00373                                  const char *cfgdir, Int_t loglevel)
00374 {
00375    // Create a new remote session (master and associated workers).
00376 
00377    // Create
00378    if (IsProofd())
00379       fUrl.SetOptions("std");
00380 
00381    // Create the instance
00382    TProof *p = new TProof(fUrl.GetUrl(), cfg, cfgdir, loglevel, 0, this);
00383 
00384    if (p && p->IsValid()) {
00385 
00386       // Save record about this session
00387       Int_t ns = 1;
00388       if (fSessions) {
00389          // To avoid ambiguities in case of removal of some elements
00390          if (fSessions->Last())
00391             ns = ((TProofDesc *)(fSessions->Last()))->GetLocalId() + 1;
00392       } else {
00393          // Create the list
00394          fSessions = new TList;
00395       }
00396 
00397       // Create the description class
00398       Int_t st = (p->IsIdle()) ? TProofDesc::kIdle : TProofDesc::kRunning ;
00399       TProofDesc *d =
00400          new TProofDesc(p->GetName(), p->GetTitle(), p->GetUrl(),
00401                                ns, p->GetSessionID(), st, p);
00402       fSessions->Add(d);
00403 
00404    } else {
00405       // Session creation failed
00406       if (gDebug > 0) Error("CreateSession", "PROOF session creation failed");
00407       SafeDelete(p);
00408    }
00409 
00410    // We are done
00411    return p;
00412 }
00413 
00414 //______________________________________________________________________________
00415 Bool_t TProofMgr::MatchUrl(const char *url)
00416 {
00417    // Checks if 'url' refers to the same 'user@host:port' entity as the URL
00418    // in memory
00419 
00420    TUrl u(url);
00421 
00422    // Correct URL protocol
00423    if (!strcmp(u.GetProtocol(), TUrl("a").GetProtocol()))
00424       u.SetProtocol("proof");
00425 
00426    // Correct port
00427    if (u.GetPort() == TUrl("a").GetPort()) {
00428       Int_t port = gSystem->GetServiceByName("proofd");
00429       if (port < 0)
00430          port = 1093;
00431       u.SetPort(port);
00432    }
00433 
00434    // Now we can check
00435    if (!strcmp(u.GetHostFQDN(), fUrl.GetHostFQDN()))
00436       if (u.GetPort() == fUrl.GetPort())
00437          if (strlen(u.GetUser()) <= 0 || !strcmp(u.GetUser(),fUrl.GetUser()))
00438             return kTRUE;
00439 
00440    // Match failed
00441    return kFALSE;
00442 }
00443 
00444 //________________________________________________________________________
00445 TList *TProofMgr::GetListOfManagers()
00446 {
00447    // Extract pointers to PROOF managers from TROOT::fProofs.
00448 
00449    // Update the list with new entries
00450    if (gROOT->GetListOfProofs()) {
00451       TIter nxp(gROOT->GetListOfProofs());
00452       TObject *o = 0;
00453       while ((o = nxp())) {
00454          if (o->InheritsFrom(TProofMgr::Class()) && !fgListOfManagers.FindObject(o))
00455             fgListOfManagers.Add(o);
00456       }
00457    }
00458 
00459    // Get rid of invalid entries and notify
00460    if (fgListOfManagers.GetSize() > 0) {
00461       TIter nxp(&fgListOfManagers);
00462       TObject *o = 0;
00463       Int_t nm = 0;
00464       while ((o = nxp())) {
00465          if (!(gROOT->GetListOfProofs()->FindObject(o))) {
00466             fgListOfManagers.Remove(o);
00467          } else {
00468             TProofMgr *p = (TProofMgr *)o;
00469             if (gDebug > 0)
00470                Printf("// #%d: \"%s\" (%s)", ++nm, p->GetName(), p->GetTitle());
00471          }
00472       }
00473    } else {
00474       if (gDebug > 0)
00475          Printf("No managers found");
00476    }
00477 
00478    // We are done
00479    return &fgListOfManagers;
00480 }
00481 
00482 //______________________________________________________________________________
00483 TProofMgr *TProofMgr::Create(const char *uin, Int_t loglevel,
00484                              const char *alias, Bool_t xpd)
00485 {
00486    // Static method returning the appropriate TProofMgr object using
00487    // the plugin manager.
00488    TProofMgr *m= 0;
00489 
00490    Bool_t isLite = kFALSE;
00491    // Resolve url; if empty the actions depend of the default
00492    TUrl u(uin);
00493    TString host = u.GetHost();
00494    if (host.IsNull()) {
00495       host = gEnv->GetValue("Proof.LocalDefault", "lite");
00496       if (host != "lite")
00497          u.SetUrl("localhost");
00498    }
00499    if (host == "lite" || host == "__lite__") {
00500 #ifndef WIN32
00501       isLite = kTRUE;
00502       u.SetHost("__lite__");
00503       u.SetProtocol("proof");
00504       u.SetPort(1093);
00505 #else
00506       ::Info("TProofMgr::Create","'lite' not yet supported on Windows");
00507       return m;
00508 #endif
00509    }
00510 
00511    if (!isLite) {
00512       // in case user gave as url: "machine.dom.ain", replace
00513       // "http" by "proof" and "80" by "1093"
00514       if (!strcmp(u.GetProtocol(), TUrl("a").GetProtocol()))
00515          u.SetProtocol("proof");
00516       if (u.GetPort() == TUrl("a").GetPort())
00517          u.SetPort(1093);
00518    }
00519 
00520    // Avoid multiple calls to GetUrl
00521    const char *url = u.GetUrl();
00522 
00523    // Make sure we do not have already a manager for this URL
00524    TList *lm = TProofMgr::GetListOfManagers();
00525    if (lm) {
00526       TIter nxm(lm);
00527       while ((m = (TProofMgr *)nxm())) {
00528          if (m->IsValid()) {
00529             if (m->MatchUrl(url)) return m;
00530          } else {
00531             fgListOfManagers.Remove(m);
00532             SafeDelete(m);
00533             break;
00534          }
00535       }
00536    }
00537 
00538    if (isLite) {
00539       // Init the lite version
00540       return new TProofMgrLite(url, loglevel, alias);
00541    }
00542 
00543    m = 0;
00544    Bool_t trystd = kTRUE;
00545 
00546    // If required, we assume first that the remote server is based on XrdProofd
00547    if (xpd) {
00548       TProofMgr_t cm = TProofMgr::GetXProofMgrHook();
00549       if (cm) {
00550          m = (TProofMgr *) (*cm)(url, loglevel, alias);
00551          // Update trystd flag
00552          trystd = (m && !(m->IsValid()) && m->IsProofd()) ? kTRUE : kFALSE;
00553       }
00554    }
00555 
00556    // If the first attempt failed, we instantiate an old interface
00557    if (trystd) {
00558       SafeDelete(m);
00559       m = new TProofMgr(url, loglevel, alias);
00560    }
00561 
00562    // Record the new manager, if any
00563    if (m) {
00564       fgListOfManagers.Add(m);
00565       if (m->IsValid() && !(m->IsProofd())) {
00566          R__LOCKGUARD2(gROOTMutex);
00567          gROOT->GetListOfProofs()->Add(m);
00568          gROOT->GetListOfSockets()->Add(m);
00569       }
00570    }
00571 
00572    // We are done
00573    return m;
00574 }
00575 
00576 //________________________________________________________________________
00577 TProofMgr_t TProofMgr::GetXProofMgrHook()
00578 {
00579    // Get the constructor hook fro TXProofMgr.
00580    // We do this without the plugin manager because it blocks the
00581    // CINT mutex breaking the parallel startup.
00582 
00583    if (!fgTXProofMgrHook) {
00584       // Load the appropriate library ...
00585       TString prooflib = "libProofx";
00586       char *p = 0;
00587       if ((p = gSystem->DynamicPathName(prooflib, kTRUE))) {
00588          delete[] p;
00589          if (gSystem->Load(prooflib) == -1)
00590             ::Error("TProofMgr::GetXProofMgrCtor",
00591                     "can't load %s", prooflib.Data());
00592       } else
00593          ::Error("TProofMgr::GetXProofMgrCtor",
00594                  "can't locate %s", prooflib.Data());
00595    }
00596 
00597    // Done
00598    return fgTXProofMgrHook;
00599 }
00600 
00601 //_____________________________________________________________________________
00602 void TProofMgr::SetTXProofMgrHook(TProofMgr_t pmh)
00603 {
00604    // Set hook to TXProofMgr ctor
00605 
00606    fgTXProofMgrHook = pmh;
00607 }
00608 
00609 
00610 //
00611 //  TProofDesc
00612 //
00613 
00614 ClassImp(TProofDesc)
00615 
00616 //________________________________________________________________________
00617 void TProofDesc::Print(Option_t *) const
00618 {
00619    // Dump the content to the screen.
00620    const char *st[] = { "unknown", "idle", "processsing", "shutting down"};
00621 
00622    Printf("// # %d", fLocalId);
00623    Printf("// alias: %s, url: \"%s\"", GetTitle(), GetUrl());
00624    Printf("// tag: %s", GetName());
00625    Printf("// status: %s, attached: %s (remote ID: %d)",st[fStatus+1], (fProof ? "YES" : "NO"), fRemoteId);
00626 }

Generated on Tue Jul 5 14:51:39 2011 for ROOT_528-00b_version by  doxygen 1.5.1