TProofResourcesStatic.cxx

Go to the documentation of this file.
00001 // @(#)root/proof:$Id: TProofResourcesStatic.cxx 36086 2010-10-05 16:15:41Z ganis $
00002 // Author: Paul Nilsson   7/12/2005
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2005, 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 // TProofResourcesStatic                                                //
00015 //                                                                      //
00016 // Implementation of PROOF static resources.                            //
00017 // The purpose of this class is to provide a standard interface to      //
00018 // static config files. It interprets Proof config files (proof.conf)   //
00019 // and sorts the contents into TProofNodeInfo objects. Master info will //
00020 // be placed in fMaster (of type TProofNodeInfo). Submaster info will   //
00021 // be put in fSubmasterList (a TList of TProofNodeInfo objects), while  //
00022 // workers (and condorworkers) will be placed in fWorkerList (a TList   //
00023 // of TProofNodeInfo objects).                                          //
00024 //                                                                      //
00025 //////////////////////////////////////////////////////////////////////////
00026 
00027 #include "Riostream.h"
00028 #include "TProofResourcesStatic.h"
00029 #include "TSystem.h"
00030 #include "TInetAddress.h"
00031 #include "TProofNodeInfo.h"
00032 #include "TProofDebug.h"
00033 #include "TUrl.h"
00034 #include "TList.h"
00035 #include "TObjArray.h"
00036 #include "TObjString.h"
00037 #include "TError.h"
00038 
00039 ClassImp(TProofResourcesStatic)
00040 
00041 
00042 //______________________________________________________________________________
00043 TProofResourcesStatic::TProofResourcesStatic()
00044 {
00045    // This ctor is used in TProofServ::Setup() in combination with GetWorkDir()
00046    // for a quick scan of the config file to retrieve the work directory.
00047 
00048    // Create master node info and submaster/worker lists, and set default values
00049    InitResources();
00050 }
00051 
00052 //______________________________________________________________________________
00053 TProofResourcesStatic::TProofResourcesStatic(const char *confDir,
00054                                              const char *fileName)
00055 {
00056    // Using this ctor will retrieve all information in the config file
00057    // and store it in fMaster, fSubmasterList and fWorkerList,
00058    // condorworkers will be stored in the fWorkerList.
00059 
00060    // Create master node info and submaster/worker lists, and set default values
00061    InitResources();
00062 
00063    // Open and read the PROOF config file
00064    if (!ReadConfigFile(confDir, fileName)) {
00065       PDB(kAll,1)
00066          Info("TProofResourcesStatic", "error encountered while reading config file");
00067       fValid = kFALSE;
00068    }
00069 }
00070 
00071 //______________________________________________________________________________
00072 TProofResourcesStatic::~TProofResourcesStatic()
00073 {
00074    // Destructor.
00075 
00076    delete fSubmasterList;
00077    delete fWorkerList;
00078    delete fMaster;
00079 }
00080 
00081 //______________________________________________________________________________
00082 void TProofResourcesStatic::InitResources()
00083 {
00084    // Create master node info and submaster/worker lists,
00085    // and set default values.
00086 
00087    // Create master
00088    fMaster = new TProofNodeInfo();
00089    fMaster->fNodeType = TProofNodeInfo::GetNodeType("master");
00090    fFoundMaster = kFALSE; // Set to kTRUE if the config file contains master info
00091 
00092    // Create workers
00093    fWorkerList = new TList();
00094    fWorkerList->SetOwner();
00095 
00096    // Create submaster
00097    fSubmasterList = new TList();
00098    fSubmasterList->SetOwner();
00099 
00100    // Assume that the config file will be ok
00101    fValid = kTRUE;
00102 }
00103 
00104 //______________________________________________________________________________
00105 TProofNodeInfo *TProofResourcesStatic::GetMaster()
00106 {
00107    // Get the master node. Only return the master info if it was set
00108    // in the config file.
00109 
00110    if (fFoundMaster)
00111       return fMaster;
00112 
00113    return 0;
00114 }
00115 
00116 //______________________________________________________________________________
00117 TList *TProofResourcesStatic::GetSubmasters()
00118 {
00119    // Get the list of submaster nodes.
00120 
00121    return fSubmasterList;
00122 }
00123 
00124 //______________________________________________________________________________
00125 TList *TProofResourcesStatic::GetWorkers(void)
00126 {
00127    // Get the list of worker nodes.
00128 
00129    return fWorkerList;
00130 }
00131 
00132 //______________________________________________________________________________
00133 Bool_t TProofResourcesStatic::ReadConfigFile(const char *confDir,
00134                                              const char *fileName)
00135 {
00136    // Read the PROOF config file and fill the master and worker list.
00137 
00138    Bool_t status = kTRUE;
00139 
00140    // Skip prefix (e.g. "sm:") if any
00141    const char *p = (const char *) strstr(fileName,":");
00142    if (p)
00143       fileName = p+1;
00144 
00145    // Use file specified by the cluster administrator, if any
00146    const char *cf = gSystem->Getenv("ROOTPROOFCONF");
00147    if (cf && !(gSystem->AccessPathName(cf, kReadPermission))) {
00148       fFileName = cf;
00149    } else {
00150       if (cf)
00151          PDB(kGlobal,1)
00152             Info("ReadConfigFile", "file %s cannot be read:"
00153                  " check existence and/or permissions", cf);
00154       if (fileName && strlen(fileName) > 0) {
00155          // Use user defined file or default
00156          // Add a proper path to the file name
00157          fFileName.Form("%s/.%s", gSystem->HomeDirectory(), fileName);
00158          PDB(kGlobal,2)
00159             Info("ReadConfigFile", "checking PROOF config file %s", fFileName.Data());
00160          if (gSystem->AccessPathName(fFileName, kReadPermission)) {
00161             fFileName.Form("%s/etc/proof/%s", confDir, fileName);
00162             PDB(kGlobal,2)
00163                Info("ReadConfigFile", "checking PROOF config file %s", fFileName.Data());
00164             if (gSystem->AccessPathName(fFileName, kReadPermission)) {
00165                PDB(kAll,1)
00166                   Info("ReadConfigFile", "no PROOF config file found");
00167                return kFALSE;
00168             }
00169          }
00170       } else {
00171          PDB(kAll,1)
00172             Info("ReadConfigFile", "no PROOF config file specified");
00173          return kFALSE;
00174       }
00175    }
00176    PDB(kGlobal,1)
00177       Info("ReadConfigFile", "using PROOF config file: %s", fFileName.Data());
00178 
00179    // Open the config file
00180    fstream infile(fFileName.Data(), std::ios::in);
00181    if (infile.is_open()) {
00182       Bool_t isMaster = kFALSE;
00183       Bool_t isSubmaster = kFALSE;
00184       Bool_t isWorker = kFALSE;
00185 
00186       // Each line in the file consists of several 'keywords', e.g.
00187       //   line = "master mypc image=local"
00188       //     keyword[0] = "master"
00189       //     keyword[1] = "mypc"
00190       //     keyword[2] = "image=local"
00191       // The last keyword has an option "image" with value "local"
00192       TString line = "";
00193       TString keyword = "";
00194 
00195       // Read the entire file into the allLines object
00196       TString allLines = "";
00197       allLines.ReadString(infile);
00198       TObjArray *lines = allLines.Tokenize("\n");
00199       Int_t numberOfLines = lines->GetEntries();
00200 
00201       // Process one line at the time
00202       for (Int_t j = 0; j < numberOfLines; j++) {
00203          TObjString *objLine = (TObjString *)lines->At(j);
00204          line = objLine->GetString();
00205          line = line.Strip(TString::kBoth);
00206 
00207          // Unless this line was empty or a comment, interpret the line
00208          if ( !((line(0,1) == "#") || (line == "")) ) {
00209             TProofNodeInfo *nodeinfo = 0;
00210 
00211             // Reset boolean (condorworkers are treated as a workers)
00212             isMaster = kFALSE;
00213             isSubmaster = kFALSE;
00214             isWorker = kFALSE;
00215 
00216             // Extract all words in the current line
00217             TObjArray *tokens = line.Tokenize(" ");
00218             Int_t n = tokens->GetEntries();
00219             TString option;
00220             TString value;
00221             for (Int_t i = 0; i < n; i++) {
00222 
00223                // Extrace one word from the current line
00224                keyword = ((TObjString *)tokens->At(i))->GetString();
00225 
00226                // Interpret this keyword
00227                switch (GetInfoType(keyword)) {
00228                case kNodeType: {
00229                   if (keyword == "master" || keyword == "node") {
00230                      nodeinfo = CreateNodeInfo(keyword);
00231                      isMaster = kTRUE;     // will be reset
00232                   }
00233                   // [either submaster, worker or condorworker]
00234                   else if (keyword == "submaster") {
00235                      // Get a submaster info node
00236                      nodeinfo = CreateNodeInfo(keyword);
00237                      isSubmaster = kTRUE;
00238                   } else {
00239                      // Get a worker or condorworker info node
00240                      nodeinfo = CreateNodeInfo(keyword);
00241                      isWorker = kTRUE;
00242                   }
00243                   break;
00244                }
00245                case kHost: {
00246                   // Store the host name
00247                   if (nodeinfo) {
00248                      nodeinfo->fNodeName = keyword;
00249 
00250                      // Set default image
00251                      if (isMaster) {
00252                         TString node = TUrl(nodeinfo->fNodeName).GetHost();
00253                         nodeinfo->fImage = strstr(nodeinfo->fNodeName, node.Data());
00254                      } else {
00255                         // If the node name contains an '@' sign, it should be removed
00256                         // before copying it into the [default] image info
00257                         // On what position is the '@' sign? (if there is one)
00258                         TString tmp = nodeinfo->fNodeName;
00259                         const Ssiz_t equalPosition = tmp.Index("@", 1, 0, TString::kExact);
00260 
00261                         // Extract the host
00262                         nodeinfo->fImage = tmp(equalPosition + 1, tmp.Length());
00263                      }
00264                   } else {
00265                      Error("ReadConfigFile","Command not recognized: %s (ignored)",
00266                            keyword.Data());
00267                   }
00268                   break;
00269                }
00270                case kOption: {
00271                   // On what position is the '=' sign?
00272                   const Ssiz_t equalPosition =
00273                      keyword.Index("=", 1, 0, TString::kExact);
00274 
00275                   // Extract the option and its value
00276                   TString tmp = keyword;
00277                   option = tmp(0, equalPosition);
00278                   value = tmp(equalPosition + 1, tmp.Length());
00279 
00280                   // Set the node info options
00281                   SetOption(nodeinfo, option, value);
00282                   break;
00283                }
00284                default:
00285                   break;
00286                } // end switch
00287 
00288             } // end if
00289 
00290             // Check if we found a good master
00291             if (isMaster) {
00292                // Check if the master can run on this node
00293                TString node = TUrl(nodeinfo->fNodeName).GetHost();
00294                TString host = gSystem->GetHostByName(gSystem->HostName()).GetHostName();
00295                TInetAddress inetaddr = gSystem->GetHostByName(node);
00296                if (!host.CompareTo(inetaddr.GetHostName()) || (node == "localhost")) {
00297                   fFoundMaster = kTRUE;
00298                   fMaster->Assign(*nodeinfo);
00299                }
00300             }
00301 
00302             // Store the submaster, worker or condorworker
00303             if (isWorker) {
00304                fWorkerList->Add(nodeinfo);
00305             }
00306             else if (isSubmaster) {
00307                fSubmasterList->Add(nodeinfo);
00308             }
00309          } // else
00310 
00311       } // while (! infile.eof() )
00312       infile.close();
00313 
00314       // Did the config file contain appropriate master information?
00315       if (!fFoundMaster) {
00316          Error("ReadConfigFile","No master info found in config file");
00317          status = kFALSE;
00318       }
00319    } // end if (infile.is_open())
00320    else {
00321       // Error: could not open file
00322       status = kFALSE;
00323    }
00324 
00325    return status;
00326 }
00327 
00328 
00329 //______________________________________________________________________________
00330 void TProofResourcesStatic::SetOption(TProofNodeInfo *nodeinfo,
00331                                       const TString &option,
00332                                       const TString &value)
00333 {
00334    // Static method to set the node info options.
00335 
00336    if (!nodeinfo) return;
00337    
00338    if (option == "workdir") {
00339       nodeinfo->fWorkDir = value;
00340    } else if (option == "image") {
00341       nodeinfo->fImage = value;
00342    } else if (option == "perf") {
00343       nodeinfo->fPerfIndex = value.Atoi();
00344    } else if (option == "config") {
00345       nodeinfo->fConfig = value;
00346    } else if (option == "msd") {
00347       nodeinfo->fMsd = value;
00348    } else if (option == "port") {
00349       nodeinfo->fPort = value.Atoi();
00350    } else {
00351       ::Error("SetOption","No such option [%s=%s]",option.Data(),value.Data());
00352    }
00353 }
00354 
00355 //______________________________________________________________________________
00356 TProofResourcesStatic::EInfoType TProofResourcesStatic::GetInfoType(const TString &word)
00357 {
00358    // Static method to determine the info type.
00359 
00360    EInfoType type = kNodeType;
00361 
00362    if ((word == "node") || (word == "master") || (word == "submaster") ||
00363        (word == "worker") || (word == "slave") ||
00364        (word == "condorworker") || (word == "condorslave")) {
00365       type = kNodeType;
00366    }
00367    else if (word.Contains("=", TString::kExact)) {
00368       type = kOption;
00369    } else {
00370       type = kHost;
00371    }
00372 
00373    return type;
00374 }
00375 
00376 //______________________________________________________________________________
00377 TProofNodeInfo *TProofResourcesStatic::CreateNodeInfo(const TString &name)
00378 {
00379    // Fill out the preliminary TProofNodeInfo structure.
00380 
00381    TProofNodeInfo *nodeInfo = new TProofNodeInfo();
00382    nodeInfo->fNodeType  = TProofNodeInfo::GetNodeType(name);
00383    nodeInfo->fNodeName  = name;
00384    nodeInfo->fPort      = -1;
00385    nodeInfo->fPerfIndex = 100;
00386 
00387    return nodeInfo;
00388 }

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