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