ROOT  6.06/09
Reference Guide
TProofResourcesStatic.cxx
Go to the documentation of this file.
1 // @(#)root/proof:$Id$
2 // Author: Paul Nilsson 7/12/2005
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 //////////////////////////////////////////////////////////////////////////
13 // //
14 // TProofResourcesStatic //
15 // //
16 // Implementation of PROOF static resources. //
17 // The purpose of this class is to provide a standard interface to //
18 // static config files. It interprets Proof config files (proof.conf) //
19 // and sorts the contents into TProofNodeInfo objects. Master info will //
20 // be placed in fMaster (of type TProofNodeInfo). Submaster info will //
21 // be put in fSubmasterList (a TList of TProofNodeInfo objects), while //
22 // workers (and condorworkers) will be placed in fWorkerList (a TList //
23 // of TProofNodeInfo objects). //
24 // //
25 //////////////////////////////////////////////////////////////////////////
26 
27 #include "Riostream.h"
28 #include "TProofResourcesStatic.h"
29 #include "TSystem.h"
30 #include "TInetAddress.h"
31 #include "TProofNodeInfo.h"
32 #include "TProofDebug.h"
33 #include "TUrl.h"
34 #include "TList.h"
35 #include "TObjArray.h"
36 #include "TObjString.h"
37 #include "TError.h"
38 
40 
41 
42 ////////////////////////////////////////////////////////////////////////////////
43 /// This ctor is used in TProofServ::Setup() in combination with GetWorkDir()
44 /// for a quick scan of the config file to retrieve the work directory.
45 
47 {
48  // Create master node info and submaster/worker lists, and set default values
49  InitResources();
50 }
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 /// Using this ctor will retrieve all information in the config file
54 /// and store it in fMaster, fSubmasterList and fWorkerList,
55 /// condorworkers will be stored in the fWorkerList.
56 
58  const char *fileName)
59 {
60  // Create master node info and submaster/worker lists, and set default values
61  InitResources();
62 
63  // Open and read the PROOF config file
64  if (!ReadConfigFile(confDir, fileName)) {
65  PDB(kAll,1)
66  Info("TProofResourcesStatic", "error encountered while reading config file");
67  fValid = kFALSE;
68  }
69 }
70 
71 ////////////////////////////////////////////////////////////////////////////////
72 /// Destructor.
73 
75 {
76  delete fSubmasterList;
77  delete fWorkerList;
78  delete fMaster;
79 }
80 
81 ////////////////////////////////////////////////////////////////////////////////
82 /// Create master node info and submaster/worker lists,
83 /// and set default values.
84 
86 {
87  // Create master
88  fMaster = new TProofNodeInfo();
90  fFoundMaster = kFALSE; // Set to kTRUE if the config file contains master info
91 
92  // Create workers
93  fWorkerList = new TList();
95 
96  // Create submaster
97  fSubmasterList = new TList();
99 
100  // Assume that the config file will be ok
101  fValid = kTRUE;
102 }
103 
104 ////////////////////////////////////////////////////////////////////////////////
105 /// Get the master node. Only return the master info if it was set
106 /// in the config file.
107 
109 {
110  if (fFoundMaster)
111  return fMaster;
112 
113  return 0;
114 }
115 
116 ////////////////////////////////////////////////////////////////////////////////
117 /// Get the list of submaster nodes.
118 
120 {
121  return fSubmasterList;
122 }
123 
124 ////////////////////////////////////////////////////////////////////////////////
125 /// Get the list of worker nodes.
126 
128 {
129  return fWorkerList;
130 }
131 
132 ////////////////////////////////////////////////////////////////////////////////
133 /// Read the PROOF config file and fill the master and worker list.
134 
136  const char *fileName)
137 {
138  Bool_t status = kTRUE;
139 
140  // Skip prefix (e.g. "sm:") if any
141  const char *p = (const char *) strstr(fileName,":");
142  if (p)
143  fileName = p+1;
144 
145  // Use file specified by the cluster administrator, if any
146  const char *cf = gSystem->Getenv("ROOTPROOFCONF");
147  if (cf && !(gSystem->AccessPathName(cf, kReadPermission))) {
148  fFileName = cf;
149  } else {
150  if (cf)
151  PDB(kGlobal,1)
152  Info("ReadConfigFile", "file %s cannot be read:"
153  " check existence and/or permissions", cf);
154  if (fileName && strlen(fileName) > 0) {
155  // Use user defined file or default
156  // Add a proper path to the file name
157  fFileName.Form("%s/.%s", gSystem->HomeDirectory(), fileName);
158  PDB(kGlobal,2)
159  Info("ReadConfigFile", "checking PROOF config file %s", fFileName.Data());
161  fFileName.Form("%s/etc/proof/%s", confDir, fileName);
162  PDB(kGlobal,2)
163  Info("ReadConfigFile", "checking PROOF config file %s", fFileName.Data());
165  PDB(kAll,1)
166  Info("ReadConfigFile", "no PROOF config file found");
167  return kFALSE;
168  }
169  }
170  } else {
171  PDB(kAll,1)
172  Info("ReadConfigFile", "no PROOF config file specified");
173  return kFALSE;
174  }
175  }
176  PDB(kGlobal,1)
177  Info("ReadConfigFile", "using PROOF config file: %s", fFileName.Data());
178 
179  // Open the config file
180  std::fstream infile(fFileName.Data(), std::ios::in);
181  if (infile.is_open()) {
182  Bool_t isMaster = kFALSE;
183  Bool_t isSubmaster = kFALSE;
184  Bool_t isWorker = kFALSE;
185 
186  // Each line in the file consists of several 'keywords', e.g.
187  // line = "master mypc image=local"
188  // keyword[0] = "master"
189  // keyword[1] = "mypc"
190  // keyword[2] = "image=local"
191  // The last keyword has an option "image" with value "local"
192  TString line = "";
193  TString keyword = "";
194 
195  // Read the entire file into the allLines object
196  TString allLines = "";
197  allLines.ReadString(infile);
198  TObjArray *lines = allLines.Tokenize("\n");
199  Int_t numberOfLines = lines->GetEntries();
200 
201  // Process one line at the time
202  for (Int_t j = 0; j < numberOfLines; j++) {
203  TObjString *objLine = (TObjString *)lines->At(j);
204  line = objLine->GetString();
205  line = line.Strip(TString::kBoth);
206 
207  // Unless this line was empty or a comment, interpret the line
208  if ( !((line(0,1) == "#") || (line == "")) ) {
209  TProofNodeInfo *nodeinfo = 0;
210 
211  // Reset boolean (condorworkers are treated as a workers)
212  isMaster = kFALSE;
213  isSubmaster = kFALSE;
214  isWorker = kFALSE;
215 
216  // Extract all words in the current line
217  TObjArray *tokens = line.Tokenize(" ");
218  Int_t n = tokens->GetEntries();
219  TString option;
220  TString value;
221  for (Int_t i = 0; i < n; i++) {
222 
223  // Extrace one word from the current line
224  keyword = ((TObjString *)tokens->At(i))->GetString();
225 
226  // Interpret this keyword
227  switch (GetInfoType(keyword)) {
228  case kNodeType: {
229  if (keyword == "master" || keyword == "node") {
230  nodeinfo = CreateNodeInfo(keyword);
231  isMaster = kTRUE; // will be reset
232  }
233  // [either submaster, worker or condorworker]
234  else if (keyword == "submaster") {
235  // Get a submaster info node
236  nodeinfo = CreateNodeInfo(keyword);
237  isSubmaster = kTRUE;
238  } else {
239  // Get a worker or condorworker info node
240  nodeinfo = CreateNodeInfo(keyword);
241  isWorker = kTRUE;
242  }
243  break;
244  }
245  case kHost: {
246  // Store the host name
247  if (nodeinfo) {
248  nodeinfo->fNodeName = keyword;
249 
250  // Set default image
251  if (isMaster) {
252  TString node = TUrl(nodeinfo->fNodeName).GetHost();
253  nodeinfo->fImage = strstr(nodeinfo->fNodeName, node.Data());
254  } else {
255  // If the node name contains an '@' sign, it should be removed
256  // before copying it into the [default] image info
257  // On what position is the '@' sign? (if there is one)
258  TString tmp = nodeinfo->fNodeName;
259  const Ssiz_t equalPosition = tmp.Index("@", 1, 0, TString::kExact);
260 
261  // Extract the host
262  nodeinfo->fImage = tmp(equalPosition + 1, tmp.Length());
263  }
264  } else {
265  Error("ReadConfigFile","Command not recognized: %s (ignored)",
266  keyword.Data());
267  }
268  break;
269  }
270  case kOption: {
271  // On what position is the '=' sign?
272  const Ssiz_t equalPosition =
273  keyword.Index("=", 1, 0, TString::kExact);
274 
275  // Extract the option and its value
276  TString tmp = keyword;
277  option = tmp(0, equalPosition);
278  value = tmp(equalPosition + 1, tmp.Length());
279 
280  // Set the node info options
281  SetOption(nodeinfo, option, value);
282  break;
283  }
284  default:
285  break;
286  } // end switch
287 
288  } // end if
289 
290  // Check if we found a good master
291  if (isMaster) {
292  // Check if the master can run on this node
293  TString node = TUrl(nodeinfo->fNodeName).GetHost();
294  TString host = gSystem->GetHostByName(gSystem->HostName()).GetHostName();
295  TInetAddress inetaddr = gSystem->GetHostByName(node);
296  if (!host.CompareTo(inetaddr.GetHostName()) || (node == "localhost")) {
298  fMaster->Assign(*nodeinfo);
299  }
300  }
301 
302  // Store the submaster, worker or condorworker
303  if (isWorker) {
304  fWorkerList->Add(nodeinfo);
305  }
306  else if (isSubmaster) {
307  fSubmasterList->Add(nodeinfo);
308  }
309  } // else
310 
311  } // while (! infile.eof() )
312  infile.close();
313 
314  // Did the config file contain appropriate master information?
315  if (!fFoundMaster) {
316  Error("ReadConfigFile","No master info found in config file");
317  status = kFALSE;
318  }
319  } // end if (infile.is_open())
320  else {
321  // Error: could not open file
322  status = kFALSE;
323  }
324 
325  return status;
326 }
327 
328 
329 ////////////////////////////////////////////////////////////////////////////////
330 /// Static method to set the node info options.
331 
333  const TString &option,
334  const TString &value)
335 {
336  if (!nodeinfo) return;
337 
338  if (option == "workdir") {
339  nodeinfo->fWorkDir = value;
340  } else if (option == "image") {
341  nodeinfo->fImage = value;
342  } else if (option == "perf") {
343  nodeinfo->fPerfIndex = value.Atoi();
344  } else if (option == "config") {
345  nodeinfo->fConfig = value;
346  } else if (option == "msd") {
347  nodeinfo->fMsd = value;
348  } else if (option == "port") {
349  nodeinfo->fPort = value.Atoi();
350  } else {
351  ::Error("SetOption","No such option [%s=%s]",option.Data(),value.Data());
352  }
353 }
354 
355 ////////////////////////////////////////////////////////////////////////////////
356 /// Static method to determine the info type.
357 
359 {
361 
362  if ((word == "node") || (word == "master") || (word == "submaster") ||
363  (word == "worker") || (word == "slave") ||
364  (word == "condorworker") || (word == "condorslave")) {
365  type = kNodeType;
366  }
367  else if (word.Contains("=", TString::kExact)) {
368  type = kOption;
369  } else {
370  type = kHost;
371  }
372 
373  return type;
374 }
375 
376 ////////////////////////////////////////////////////////////////////////////////
377 /// Fill out the preliminary TProofNodeInfo structure.
378 
380 {
381  TProofNodeInfo *nodeInfo = new TProofNodeInfo();
382  nodeInfo->fNodeType = TProofNodeInfo::GetNodeType(name);
383  nodeInfo->fNodeName = name;
384  nodeInfo->fPort = -1;
385  nodeInfo->fPerfIndex = 100;
386 
387  return nodeInfo;
388 }
const char * GetHost() const
Definition: TUrl.h:76
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition: TSystem.cxx:1265
Bool_t ReadConfigFile(const char *confDir, const char *fileName)
Read the PROOF config file and fill the master and worker list.
An array of TObjects.
Definition: TObjArray.h:39
TList * GetSubmasters()
Get the list of submaster nodes.
const char * GetHostName() const
Definition: TInetAddress.h:75
static EInfoType GetInfoType(const TString &word)
Static method to determine the info type.
Ssiz_t Length() const
Definition: TString.h:390
TLine * line
Collectable string class.
Definition: TObjString.h:32
ClassImp(TProofResourcesStatic) TProofResourcesStatic
This ctor is used in TProofServ::Setup() in combination with GetWorkDir() for a quick scan of the con...
This class represents a WWW compatible URL.
Definition: TUrl.h:41
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:892
virtual ~TProofResourcesStatic()
Destructor.
This class represents an Internet Protocol (IP) address.
Definition: TInetAddress.h:40
virtual const char * HomeDirectory(const char *userName=0)
Return the user's home directory.
Definition: TSystem.cxx:881
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
TAlienJobStatus * status
Definition: TAlienJob.cxx:51
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
TProofNodeInfo * GetMaster()
Get the master node.
const char * Data() const
Definition: TString.h:349
TList * GetWorkers()
Get the list of worker nodes.
#define PDB(mask, level)
Definition: TProofDebug.h:58
static TString * ReadString(TBuffer &b, const TClass *clReq)
Read TString object from buffer.
Definition: TString.cxx:1258
virtual const char * Getenv(const char *env)
Get environment variable.
Definition: TSystem.cxx:1627
Int_t Atoi() const
Return integer value of string.
Definition: TString.cxx:1964
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
virtual TInetAddress GetHostByName(const char *server)
Get Internet Protocol (IP) address of host.
Definition: TSystem.cxx:2245
A doubly linked list.
Definition: TList.h:47
TString GetString() const
Definition: TObjString.h:50
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
ENodeType fNodeType
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition: TString.cxx:2321
TSubString Strip(EStripType s=kTrailing, char c= ' ') const
Return a substring of self stripped at beginning and/or end.
Definition: TString.cxx:1069
void InitResources()
Create master node info and submaster/worker lists, and set default values.
TObjArray * Tokenize(const TString &delim) const
This function is used to isolate sequential tokens in a TString.
Definition: TString.cxx:2240
int Ssiz_t
Definition: RtypesCore.h:63
virtual const char * HostName()
Return the system's host name.
Definition: TSystem.cxx:307
int type
Definition: TGX11.cxx:120
Int_t GetEntries() const
Return the number of objects in array (i.e.
Definition: TObjArray.cxx:493
#define name(a, b)
Definition: linkTestLib0.cpp:5
virtual void Add(TObject *obj)
Definition: TList.h:81
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:567
ENodeType GetNodeType() const
static void SetOption(TProofNodeInfo *nodeinfo, const TString &option, const TString &value)
Static method to set the node info options.
void Assign(const TProofNodeInfo &n)
Asssign content of node n to this node.
TObject * At(Int_t idx) const
Definition: TObjArray.h:167
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition: TString.h:582
const Bool_t kTRUE
Definition: Rtypes.h:91
float value
Definition: math.cpp:443
const Int_t n
Definition: legend1.C:16
static TProofNodeInfo * CreateNodeInfo(const TString &name)
Fill out the preliminary TProofNodeInfo structure.
int CompareTo(const char *cs, ECaseCompare cmp=kExact) const
Compare a string to char *cs2.
Definition: TString.cxx:385