forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TXProofServ.cxx
1206 lines (1030 loc) · 38.5 KB
/
TXProofServ.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// @(#)root/proofx:$Id$
// Author: Gerardo Ganis 12/12/2005
/*************************************************************************
* Copyright (C) 1995-2005, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
//////////////////////////////////////////////////////////////////////////
// //
// TXProofServ //
// //
// TXProofServ is the XRD version of the PROOF server. It differs from //
// TXProofServ only for the underlying connection technology //
// //
//////////////////////////////////////////////////////////////////////////
#include "RConfigure.h"
#include "RConfig.h"
#include "Riostream.h"
#ifdef WIN32
#include <io.h>
typedef long off_t;
#endif
#include <sys/types.h>
#include <netinet/in.h>
#include <utime.h>
#include "TXProofServ.h"
#include "TObjString.h"
#include "TEnv.h"
#include "TError.h"
#include "TException.h"
#include "THashList.h"
#include "TInterpreter.h"
#include "TParameter.h"
#include "TProofDebug.h"
#include "TProof.h"
#include "TProofPlayer.h"
#include "TQueryResultManager.h"
#include "TRegexp.h"
#include "TClass.h"
#include "TROOT.h"
#include "TSystem.h"
#include "TPluginManager.h"
#include "TXSocketHandler.h"
#include "TXUnixSocket.h"
#include "compiledata.h"
#include "TProofNodeInfo.h"
#include "XProofProtocol.h"
#include <XrdClient/XrdClientConst.hh>
#include <XrdClient/XrdClientEnv.hh>
// debug hook
static volatile Int_t gProofServDebug = 1;
//----- SigPipe signal handler -------------------------------------------------
//______________________________________________________________________________
class TXProofServSigPipeHandler : public TSignalHandler {
TXProofServ *fServ;
public:
TXProofServSigPipeHandler(TXProofServ *s) : TSignalHandler(kSigInterrupt, kFALSE)
{ fServ = s; }
Bool_t Notify();
};
//______________________________________________________________________________
Bool_t TXProofServSigPipeHandler::Notify()
{
fServ->HandleSigPipe();
return kTRUE;
}
//----- Termination signal handler ---------------------------------------------
//______________________________________________________________________________
class TXProofServTerminationHandler : public TSignalHandler {
TXProofServ *fServ;
public:
TXProofServTerminationHandler(TXProofServ *s)
: TSignalHandler(kSigTermination, kFALSE) { fServ = s; }
Bool_t Notify();
};
//______________________________________________________________________________
Bool_t TXProofServTerminationHandler::Notify()
{
Printf("Received SIGTERM: terminating");
fServ->HandleTermination();
return kTRUE;
}
//----- Seg violation signal handler ---------------------------------------------
//______________________________________________________________________________
class TXProofServSegViolationHandler : public TSignalHandler {
TXProofServ *fServ;
public:
TXProofServSegViolationHandler(TXProofServ *s)
: TSignalHandler(kSigSegmentationViolation, kFALSE) { fServ = s; }
Bool_t Notify();
};
//______________________________________________________________________________
Bool_t TXProofServSegViolationHandler::Notify()
{
Printf("**** ");
Printf("**** Segmentation violation: terminating ****");
Printf("**** ");
fServ->HandleTermination();
return kTRUE;
}
//----- Input handler for messages from parent or master -----------------------
//______________________________________________________________________________
class TXProofServInputHandler : public TFileHandler {
TXProofServ *fServ;
public:
TXProofServInputHandler(TXProofServ *s, Int_t fd) : TFileHandler(fd, 1)
{ fServ = s; }
Bool_t Notify();
Bool_t ReadNotify() { return Notify(); }
};
//______________________________________________________________________________
Bool_t TXProofServInputHandler::Notify()
{
fServ->HandleSocketInput();
// This request has been completed: remove the client ID from the pipe
((TXUnixSocket *) fServ->GetSocket())->RemoveClientID();
return kTRUE;
}
ClassImp(TXProofServ)
// Hook to the constructor. This is needed to avoid using the plugin manager
// which may create problems in multi-threaded environments.
extern "C" {
TApplication *GetTXProofServ(Int_t *argc, char **argv, FILE *flog)
{ return new TXProofServ(argc, argv, flog); }
}
//______________________________________________________________________________
TXProofServ::TXProofServ(Int_t *argc, char **argv, FILE *flog)
: TProofServ(argc, argv, flog)
{
// Main constructor
fInterruptHandler = 0;
fInputHandler = 0;
fTerminated = kFALSE;
// TODO:
// Int_t useFIFO = 0;
/* if (GetParameter(fProof->GetInputList(), "PROOF_UseFIFO", useFIFO) != 0) {
if (useFIFO == 1)
Info("", "enablig use of FIFO (if allowed by the server)");
else
Warning("", "unsupported strategy index (%d): ignore", strategy);
}
*/
}
//______________________________________________________________________________
Int_t TXProofServ::CreateServer()
{
// Finalize the server setup. If master, create the TProof instance to talk
// the worker or submaster nodes.
// Return 0 on success, -1 on error
Bool_t xtest = (Argc() > 3 && !strcmp(Argv(3), "test")) ? kTRUE : kFALSE;
if (gProofDebugLevel > 0)
Info("CreateServer", "starting%s server creation", (xtest ? " test" : ""));
// Get file descriptor for log file
if (fLogFile) {
// Use the file already open by pmain
if ((fLogFileDes = fileno(fLogFile)) < 0) {
Error("CreateServer", "resolving the log file description number");
return -1;
}
// Hide the session start-up logs unless we are in verbose mode
if (gProofDebugLevel <= 0)
lseek(fLogFileDes, (off_t) 0, SEEK_END);
}
// Global location string in TXSocket
TXSocket::SetLocation((IsMaster()) ? "master" : "slave");
// Set debug level in XrdClient
EnvPutInt(NAME_DEBUG, gEnv->GetValue("XNet.Debug", 0));
// Get socket to be used to call back our xpd
if (xtest) {
// test session, just send the protocol version on the open pipe
// and exit
if (!(fSockPath = gSystem->Getenv("ROOTOPENSOCK"))) {
Error("CreateServer", "test: socket setup by xpd undefined");
return -1;
}
Int_t fpw = (Int_t) strtol(fSockPath.Data(), 0, 10);
int proto = htonl(kPROOF_Protocol);
fSockPath = "";
if (write(fpw, &proto, sizeof(proto)) != sizeof(proto)) {
Error("CreateServer", "test: sending protocol number");
return -1;
}
exit(0);
} else {
fSockPath = gEnv->GetValue("ProofServ.OpenSock", "");
if (fSockPath.Length() <= 0) {
Error("CreateServer", "socket setup by xpd undefined");
return -1;
}
TString entity = gEnv->GetValue("ProofServ.Entity", "");
if (entity.Length() > 0)
fSockPath.Insert(0,Form("%s/", entity.Data()));
}
// Get open socket descriptor, if any
Int_t sockfd = -1;
const char *opensock = gSystem->Getenv("ROOTOPENSOCK");
if (opensock && strlen(opensock) > 0) {
TSystem::ResetErrno();
sockfd = (Int_t) strtol(opensock, 0, 10);
if (TSystem::GetErrno() == ERANGE) {
sockfd = -1;
Warning("CreateServer", "socket descriptor: wrong conversion from '%s'", opensock);
}
if (sockfd > 0 && gProofDebugLevel > 0)
Info("CreateServer", "using open connection (descriptor %d)", sockfd);
}
// Get the sessions ID
Int_t psid = gEnv->GetValue("ProofServ.SessionID", -1);
if (psid < 0) {
Error("CreateServer", "Session ID undefined");
return -1;
}
// Call back the server
fSocket = new TXUnixSocket(fSockPath, psid, -1, this, sockfd);
if (!fSocket || !(fSocket->IsValid())) {
Error("CreateServer", "Failed to open connection to XrdProofd coordinator");
return -1;
}
// Set compression level, if any
fSocket->SetCompressionSettings(fCompressMsg);
// Set the title for debugging
TString tgt("client");
if (fOrdinal != "0") {
tgt = fOrdinal;
if (tgt.Last('.') != kNPOS) tgt.Remove(tgt.Last('.'));
}
fSocket->SetTitle(tgt);
// Set the this as reference of this socket
((TXSocket *)fSocket)->fReference = this;
// Get socket descriptor
Int_t sock = fSocket->GetDescriptor();
// Install message input handlers
fInputHandler =
TXSocketHandler::GetSocketHandler(new TXProofServInputHandler(this, sock), fSocket);
gSystem->AddFileHandler(fInputHandler);
// Get the client ID
Int_t cid = gEnv->GetValue("ProofServ.ClientID", -1);
if (cid < 0) {
Error("CreateServer", "Client ID undefined");
SendLogFile();
return -1;
}
((TXSocket *)fSocket)->SetClientID(cid);
// debug hooks
if (IsMaster()) {
// wait (loop) in master to allow debugger to connect
if (gEnv->GetValue("Proof.GdbHook",0) == 1) {
while (gProofServDebug)
;
}
} else {
// wait (loop) in slave to allow debugger to connect
if (gEnv->GetValue("Proof.GdbHook",0) == 2) {
while (gProofServDebug)
;
}
}
if (gProofDebugLevel > 0)
Info("CreateServer", "Service: %s, ConfDir: %s, IsMaster: %d",
fService.Data(), fConfDir.Data(), (Int_t)fMasterServ);
if (Setup() == -1) {
// Setup failure
LogToMaster();
SendLogFile();
Terminate(0);
return -1;
}
if (!fLogFile) {
RedirectOutput();
// If for some reason we failed setting a redirection file for the logs
// we cannot continue
if (!fLogFile || (fLogFileDes = fileno(fLogFile)) < 0) {
LogToMaster();
SendLogFile(-98);
Terminate(0);
return -1;
}
}
// Send message of the day to the client
if (IsMaster()) {
if (CatMotd() == -1) {
LogToMaster();
SendLogFile(-99);
Terminate(0);
return -1;
}
}
// Everybody expects iostream to be available, so load it...
ProcessLine("#include <iostream>", kTRUE);
ProcessLine("#include <string>",kTRUE); // for std::string iostream.
// Load user functions
const char *logon;
logon = gEnv->GetValue("Proof.Load", (char *)0);
if (logon) {
char *mac = gSystem->Which(TROOT::GetMacroPath(), logon, kReadPermission);
if (mac)
ProcessLine(Form(".L %s", logon), kTRUE);
delete [] mac;
}
// Execute logon macro
logon = gEnv->GetValue("Proof.Logon", (char *)0);
if (logon && !NoLogOpt()) {
char *mac = gSystem->Which(TROOT::GetMacroPath(), logon, kReadPermission);
if (mac)
ProcessFile(logon);
delete [] mac;
}
// Save current interpreter context
gInterpreter->SaveContext();
gInterpreter->SaveGlobalsContext();
// if master, start slave servers
if (IsMaster()) {
TString master;
if (fConfFile.BeginsWith("lite:")) {
master = "lite://";
} else {
master.Form("proof://%s@__master__", fUser.Data());
// Add port, if defined
Int_t port = gEnv->GetValue("ProofServ.XpdPort", -1);
if (port > -1) {
master += ":";
master += port;
}
}
// Make sure that parallel startup via threads is not active
// (it is broken for xpd because of the locks on gInterpreterMutex)
gEnv->SetValue("Proof.ParallelStartup", 0);
// Get plugin manager to load appropriate TProof from
TPluginManager *pm = gROOT->GetPluginManager();
if (!pm) {
Error("CreateServer", "no plugin manager found");
SendLogFile(-99);
Terminate(0);
return -1;
}
// Find the appropriate handler
TPluginHandler *h = pm->FindHandler("TProof", fConfFile);
if (!h) {
Error("CreateServer", "no plugin found for TProof with a"
" config file of '%s'", fConfFile.Data());
SendLogFile(-99);
Terminate(0);
return -1;
}
// load the plugin
if (h->LoadPlugin() == -1) {
Error("CreateServer", "plugin for TProof could not be loaded");
SendLogFile(-99);
Terminate(0);
return -1;
}
// Make instance of TProof
if (fConfFile.BeginsWith("lite:")) {
// Remove input and signal handlers to avoid spurious "signals"
// during startup
gSystem->RemoveFileHandler(fInputHandler);
fProof = reinterpret_cast<TProof*>(h->ExecPlugin(6, master.Data(),
0, 0,
fLogLevel,
fSessionDir.Data(), 0));
// Re-enable input and signal handlers
gSystem->AddFileHandler(fInputHandler);
} else {
fProof = reinterpret_cast<TProof*>(h->ExecPlugin(5, master.Data(),
fConfFile.Data(),
fConfDir.Data(),
fLogLevel,
fTopSessionTag.Data()));
}
// Save worker info
if (fProof) fProof->SaveWorkerInfo();
if (!fProof || (fProof && !fProof->IsValid())) {
Error("CreateServer", "plugin for TProof could not be executed");
FlushLogFile();
delete fProof;
fProof = 0;
SendLogFile(-99);
Terminate(0);
return -1;
}
// Find out if we are a master in direct contact only with workers
fEndMaster = fProof->IsEndMaster();
SendLogFile();
}
// Setup the shutdown timer
if (!fShutdownTimer) {
// Check activity on socket every 5 mins
fShutdownTimer = new TShutdownTimer(this, 300000);
fShutdownTimer->Start(-1, kFALSE);
}
// Check if schema evolution is effective: clients running versions <=17 do not
// support that: send a warning message
if (fProtocol <= 17) {
TString msg;
msg.Form("Warning: client version is too old: automatic schema evolution is ineffective.\n"
" This may generate compatibility problems between streamed objects.\n"
" The advise is to move to ROOT >= 5.21/02 .");
SendAsynMessage(msg.Data());
}
// Setup the idle timer
if (IsMaster() && !fIdleTOTimer) {
// Check activity on socket every 5 mins
Int_t idle_to = gEnv->GetValue("ProofServ.IdleTimeout", -1);
if (idle_to > 0) {
fIdleTOTimer = new TIdleTOTimer(this, idle_to * 1000);
fIdleTOTimer->Start(-1, kTRUE);
if (gProofDebugLevel > 0)
Info("CreateServer", " idle timer started (%d secs)", idle_to);
} else if (gProofDebugLevel > 0) {
Info("CreateServer", " idle timer not started (no idle timeout requested)");
}
}
// Done
return 0;
}
//______________________________________________________________________________
TXProofServ::~TXProofServ()
{
// Cleanup. Not really necessary since after this dtor there is no
// live anyway.
delete fSocket;
}
//______________________________________________________________________________
void TXProofServ::HandleUrgentData()
{
// Handle high priority data sent by the master or client.
// Real-time notification of messages
TProofServLogHandlerGuard hg(fLogFile, fSocket, "", fRealTimeLog);
// Get interrupt
Bool_t fw = kFALSE;
Int_t iLev = ((TXSocket *)fSocket)->GetInterrupt(fw);
if (iLev < 0) {
Error("HandleUrgentData", "error receiving interrupt");
return;
}
PDB(kGlobal, 2)
Info("HandleUrgentData", "got interrupt: %d\n", iLev);
if (fProof)
fProof->SetActive();
switch (iLev) {
case TProof::kPing:
PDB(kGlobal, 2)
Info("HandleUrgentData", "*** Ping");
// If master server, propagate interrupt to slaves
if (fw && IsMaster()) {
Int_t nbad = fProof->fActiveSlaves->GetSize() - fProof->Ping();
if (nbad > 0) {
Info("HandleUrgentData","%d slaves did not reply to ping",nbad);
}
}
// Touch the admin path to show we are alive
if (fAdminPath.IsNull()) {
fAdminPath = gEnv->GetValue("ProofServ.AdminPath", "");
}
if (!fAdminPath.IsNull()) {
if (!fAdminPath.EndsWith(".status")) {
// Update file time stamps
if (utime(fAdminPath.Data(), 0) != 0)
Info("HandleUrgentData", "problems touching path: %s", fAdminPath.Data());
else
PDB(kGlobal, 2)
Info("HandleUrgentData", "touching path: %s", fAdminPath.Data());
} else {
// Update the status in the file
// 0 idle
// 1 running
// 2 being terminated (currently unused)
// 3 queued
// 4 idle timed-out
Int_t uss_rc = UpdateSessionStatus(-1);
if (uss_rc != 0)
Error("HandleUrgentData", "problems updating status path: %s (errno: %d)", fAdminPath.Data(), -uss_rc);
}
} else {
Info("HandleUrgentData", "admin path undefined");
}
break;
case TProof::kHardInterrupt:
Info("HandleUrgentData", "*** Hard Interrupt");
// If master server, propagate interrupt to slaves
if (fw && IsMaster())
fProof->Interrupt(TProof::kHardInterrupt);
// Flush input socket
((TXSocket *)fSocket)->Flush();
if (IsMaster())
SendLogFile();
break;
case TProof::kSoftInterrupt:
Info("HandleUrgentData", "Soft Interrupt");
// If master server, propagate interrupt to slaves
if (fw && IsMaster())
fProof->Interrupt(TProof::kSoftInterrupt);
Interrupt();
if (IsMaster())
SendLogFile();
break;
case TProof::kShutdownInterrupt:
Info("HandleUrgentData", "Shutdown Interrupt");
// When returning for here connection are closed
HandleTermination();
break;
default:
Error("HandleUrgentData", "unexpected type: %d", iLev);
break;
}
if (fProof) fProof->SetActive(kFALSE);
}
//______________________________________________________________________________
void TXProofServ::HandleSigPipe()
{
// Called when the client is not alive anymore; terminate the session.
// Real-time notification of messages
Info("HandleSigPipe","got sigpipe ... do nothing");
}
//______________________________________________________________________________
void TXProofServ::HandleTermination()
{
// Called when the client is not alive anymore; terminate the session.
// If master server, propagate interrupt to slaves
// (shutdown interrupt send internally).
if (IsMaster()) {
// If not idle, try first to stop processing
if (!fIdle) {
// Remove pending requests
fWaitingQueries->Delete();
// Interrupt the current monitor
fProof->InterruptCurrentMonitor();
// Do not wait for ever, but al least 20 seconds
Long_t timeout = gEnv->GetValue("Proof.ShutdownTimeout", 60);
timeout = (timeout > 20) ? timeout : 20;
// Processing will be aborted
fProof->StopProcess(kTRUE, (Long_t) (timeout / 2));
// Receive end-of-processing messages, but do not wait for ever
fProof->Collect(TProof::kActive, timeout);
// Still not idle
if (!fIdle)
Warning("HandleTermination","processing could not be stopped");
}
// Close the session
if (fProof)
fProof->Close("S");
}
Terminate(0); // will not return from here....
}
//______________________________________________________________________________
Int_t TXProofServ::Setup()
{
// Print the ProofServ logo on standard output.
// Return 0 on success, -1 on error
char str[512];
if (IsMaster()) {
snprintf(str, 512, "**** Welcome to the PROOF server @ %s ****", gSystem->HostName());
} else {
snprintf(str, 512, "**** PROOF worker server @ %s started ****", gSystem->HostName());
}
if (fSocket->Send(str) != 1+static_cast<Int_t>(strlen(str))) {
Error("Setup", "failed to send proof server startup message");
return -1;
}
// Get client protocol
if ((fProtocol = gEnv->GetValue("ProofServ.ClientVersion", -1)) < 0) {
Error("Setup", "remote proof protocol missing");
return -1;
}
// The local user
fUser = gEnv->GetValue("ProofServ.Entity", "");
if (fUser.Length() >= 0) {
if (fUser.Contains(":"))
fUser.Remove(fUser.Index(":"));
if (fUser.Contains("@"))
fUser.Remove(fUser.Index("@"));
} else {
UserGroup_t *pw = gSystem->GetUserInfo();
if (pw) {
fUser = pw->fUser;
delete pw;
}
}
// Work dir and ...
if (IsMaster()) {
TString cf = gEnv->GetValue("ProofServ.ProofConfFile", "");
if (cf.Length() > 0)
fConfFile = cf;
}
fWorkDir = gEnv->GetValue("ProofServ.Sandbox", Form("~/%s", kPROOF_WorkDir));
// Get Session tag
if ((fSessionTag = gEnv->GetValue("ProofServ.SessionTag", "-1")) == "-1") {
Error("Setup", "Session tag missing");
return -1;
}
// Get top session tag, i.e. the tag of the PROOF session
if ((fTopSessionTag = gEnv->GetValue("ProofServ.TopSessionTag", "-1")) == "-1") {
fTopSessionTag = "";
// Try to extract it from log file path (for backward compatibility)
if (gSystem->Getenv("ROOTPROOFLOGFILE")) {
fTopSessionTag = gSystem->DirName(gSystem->Getenv("ROOTPROOFLOGFILE"));
Ssiz_t lstl;
if ((lstl = fTopSessionTag.Last('/')) != kNPOS) fTopSessionTag.Remove(0, lstl + 1);
if (fTopSessionTag.BeginsWith("session-")) {
fTopSessionTag.Remove(0, strlen("session-"));
} else {
fTopSessionTag = "";
}
}
if (fTopSessionTag.IsNull()) {
Error("Setup", "top session tag missing");
return -1;
}
}
// Make sure the process ID is in the tag
TString spid = Form("-%d", gSystem->GetPid());
if (!fSessionTag.EndsWith(spid)) {
Int_t nd = 0;
if ((nd = fSessionTag.CountChar('-')) >= 2) {
Int_t id = fSessionTag.Index("-", fSessionTag.Index("-") + 1);
if (id != kNPOS) fSessionTag.Remove(id);
} else if (nd != 1) {
Warning("Setup", "Wrong number of '-' in session tag: protocol error? %s", fSessionTag.Data());
}
// Add this process ID
fSessionTag += spid;
}
if (gProofDebugLevel > 0)
Info("Setup", "session tags: %s, %s", fTopSessionTag.Data(), fSessionTag.Data());
// Get Session dir (sandbox)
if ((fSessionDir = gEnv->GetValue("ProofServ.SessionDir", "-1")) == "-1") {
Error("Setup", "Session dir missing");
return -1;
}
// Goto to the main PROOF working directory
char *workdir = gSystem->ExpandPathName(fWorkDir.Data());
fWorkDir = workdir;
delete [] workdir;
if (gProofDebugLevel > 0)
Info("Setup", "working directory set to %s", fWorkDir.Data());
// Common setup
if (SetupCommon() != 0) {
Error("Setup", "common setup failed");
return -1;
}
// Send packages off immediately to reduce latency
fSocket->SetOption(kNoDelay, 1);
// Check every two hours if client is still alive
fSocket->SetOption(kKeepAlive, 1);
// Install SigPipe handler to handle kKeepAlive failure
gSystem->AddSignalHandler(new TXProofServSigPipeHandler(this));
// Install Termination handler
gSystem->AddSignalHandler(new TXProofServTerminationHandler(this));
// Install seg violation handler
gSystem->AddSignalHandler(new TXProofServSegViolationHandler(this));
if (gProofDebugLevel > 0)
Info("Setup", "successfully completed");
// Done
return 0;
}
//______________________________________________________________________________
TProofServ::EQueryAction TXProofServ::GetWorkers(TList *workers,
Int_t & /* prioritychange */,
Bool_t resume)
{
// Get list of workers to be used from now on.
// The list must be provided by the caller.
TProofServ::EQueryAction rc = kQueryStop;
// User config files, when enabled, override cluster-wide configuration
if (gEnv->GetValue("ProofServ.UseUserCfg", 0) != 0) {
Int_t pc = 1;
return TProofServ::GetWorkers(workers, pc);
}
// seqnum of the query for which we call getworkers
Bool_t dynamicStartup = gEnv->GetValue("Proof.DynamicStartup", kFALSE);
TString seqnum = (dynamicStartup) ? "" : XPD_GW_Static;
if (!fWaitingQueries->IsEmpty()) {
if (resume) {
seqnum += ((TProofQueryResult *)(fWaitingQueries->First()))->GetSeqNum();
} else {
seqnum += ((TProofQueryResult *)(fWaitingQueries->Last()))->GetSeqNum();
}
}
// Send request to the coordinator
TObjString *os = 0;
if (dynamicStartup) {
// We wait dynto seconds for the first worker to come; -1 means forever
Int_t dynto = gEnv->GetValue("Proof.DynamicStartupTimeout", -1);
Bool_t doto = (dynto > 0) ? kTRUE : kFALSE;
while (!(os = ((TXSocket *)fSocket)->SendCoordinator(kGetWorkers, seqnum.Data()))) {
if (doto > 0 && --dynto < 0) break;
// Another second
gSystem->Sleep(1000);
}
} else {
os = ((TXSocket *)fSocket)->SendCoordinator(kGetWorkers, seqnum.Data());
}
// The reply contains some information about the master (image, workdir)
// followed by the information about the workers; the tokens for each node
// are separated by '&'
if (os) {
TString fl(os->GetName());
if (fl.BeginsWith(XPD_GW_QueryEnqueued)) {
SendAsynMessage("+++ Query cannot be processed now: enqueued");
return kQueryEnqueued;
}
// Honour a max number of workers request (typically when running in valgrind)
Int_t nwrks = -1;
Bool_t pernode = kFALSE;
if (gSystem->Getenv("PROOF_NWORKERS")) {
TString s(gSystem->Getenv("PROOF_NWORKERS"));
if (s.EndsWith("x")) {
pernode = kTRUE;
s.ReplaceAll("x", "");
}
if (s.IsDigit()) {
nwrks = s.Atoi();
if (!dynamicStartup && (nwrks > 0)) {
// Notify, except in dynamic workers mode to avoid flooding
TString msg;
if (pernode) {
msg.Form("+++ Starting max %d workers per node following the setting of PROOF_NWORKERS", nwrks);
} else {
msg.Form("+++ Starting max %d workers following the setting of PROOF_NWORKERS", nwrks);
}
SendAsynMessage(msg);
} else {
nwrks = -1;
}
} else {
pernode = kFALSE;
}
}
TString tok;
Ssiz_t from = 0;
TList *nodecnt = (pernode) ? new TList : 0 ;
if (fl.Tokenize(tok, from, "&")) {
if (!tok.IsNull()) {
TProofNodeInfo *master = new TProofNodeInfo(tok);
if (!master) {
Error("GetWorkers", "no appropriate master line got from coordinator");
return kQueryStop;
} else {
// Set image if not yet done and available
if (fImage.IsNull() && strlen(master->GetImage()) > 0)
fImage = master->GetImage();
SafeDelete(master);
}
// Now the workers
while (fl.Tokenize(tok, from, "&")) {
if (!tok.IsNull()) {
if (nwrks == -1 || nwrks > 0) {
// We have the minimal set of information to start
rc = kQueryOK;
if (pernode && nodecnt) {
TProofNodeInfo *ni = new TProofNodeInfo(tok);
TParameter<Int_t> *p = 0;
Int_t nw = 0;
if (!(p = (TParameter<Int_t> *) nodecnt->FindObject(ni->GetNodeName().Data()))) {
p = new TParameter<Int_t>(ni->GetNodeName().Data(), nw);
nodecnt->Add(p);
}
nw = p->GetVal();
if (gDebug > 0)
Info("GetWorkers","%p: name: %s (%s) val: %d (nwrks: %d)",
p, p->GetName(), ni->GetNodeName().Data(), nw, nwrks);
if (nw < nwrks) {
if (workers) workers->Add(ni);
nw++;
p->SetVal(nw);
} else {
// Two many workers on this machine already
SafeDelete(ni);
}
} else {
if (workers)
workers->Add(new TProofNodeInfo(tok));
// Count down
if (nwrks != -1) nwrks--;
}
} else {
// Release this worker (to cleanup the session list in the coordinator and get a fresh
// and correct list next call)
TProofNodeInfo *ni = new TProofNodeInfo(tok);
ReleaseWorker(ni->GetOrdinal().Data());
}
}
}
}
}
// Cleanup
if (nodecnt) {
nodecnt->SetOwner(kTRUE);
SafeDelete(nodecnt);
}
}
// We are done
return rc;
}
//_____________________________________________________________________________
Bool_t TXProofServ::HandleError(const void *)
{
// Handle error on the input socket
// Try reconnection
if (fSocket && !fSocket->IsValid()) {
fSocket->Reconnect();
if (fSocket && fSocket->IsValid()) {
if (gDebug > 0)
Info("HandleError",
"%p: connection to local coordinator re-established", this);
FlushLogFile();
return kFALSE;
}
}
Printf("TXProofServ::HandleError: %p: got called ...", this);
// If master server, propagate interrupt to slaves
// (shutdown interrupt send internally).
if (IsMaster())
fProof->Close("S");
// Avoid communicating back anything to the coordinator (it is gone)
if (fSocket) ((TXSocket *)fSocket)->SetSessionID(-1);
Terminate(0);
Printf("TXProofServ::HandleError: %p: DONE ... ", this);
// We are done
return kTRUE;
}
//_____________________________________________________________________________
Bool_t TXProofServ::HandleInput(const void *in)
{
// Handle asynchronous input on the input socket
if (gDebug > 2)
Printf("TXProofServ::HandleInput %p, in: %p", this, in);
XHandleIn_t *hin = (XHandleIn_t *) in;
Int_t acod = (hin) ? hin->fInt1 : kXPD_msg;
// Act accordingly
if (acod == kXPD_ping || acod == kXPD_interrupt) {
// Interrupt or Ping
HandleUrgentData();
} else if (acod == kXPD_flush) {
// Flush stdout, so that we can access the full log file
Info("HandleInput","kXPD_flush: flushing log file (stdout)");
fflush(stdout);
} else if (acod == kXPD_urgent) {
// Get type
Int_t type = hin->fInt2;
switch (type) {
case TXSocket::kStopProcess:
{
// Abort or Stop ?
Bool_t abort = (hin->fInt3 != 0) ? kTRUE : kFALSE;
// Timeout
Int_t timeout = hin->fInt4;
// Act now
if (fProof)
fProof->StopProcess(abort, timeout);
else
if (fPlayer)
fPlayer->StopProcess(abort, timeout);
}
break;
default:
Info("HandleInput","kXPD_urgent: unknown type: %d", type);
}