-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdCmsCluster.cc
1856 lines (1625 loc) · 66.7 KB
/
XrdCmsCluster.cc
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
/******************************************************************************/
/* */
/* X r d C m s C l u s t e r . c c */
/* */
/* (c) 2007 by the Board of Trustees of the Leland Stanford, Jr., University */
/* All Rights Reserved */
/* Produced by Andrew Hanushevsky for Stanford University under contract */
/* DE-AC02-76-SFO0515 with the Department of Energy */
/* */
/* This file is part of the XRootD software suite. */
/* */
/* XRootD is free software: you can redistribute it and/or modify it under */
/* the terms of the GNU Lesser General Public License as published by the */
/* Free Software Foundation, either version 3 of the License, or (at your */
/* option) any later version. */
/* */
/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
/* License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public License */
/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
/* */
/* The copyright holder's institutional names and contributor's names may not */
/* be used to endorse or promote products derived from this software without */
/* specific prior written permission of the institution or contributor. */
/******************************************************************************/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/types.h>
#include "XProtocol/YProtocol.hh"
#include "Xrd/XrdJob.hh"
#include "Xrd/XrdLink.hh"
#include "Xrd/XrdScheduler.hh"
#include "XrdCms/XrdCmsBaseFS.hh"
#include "XrdCms/XrdCmsBlackList.hh"
#include "XrdCms/XrdCmsCache.hh"
#include "XrdCms/XrdCmsConfig.hh"
#include "XrdCms/XrdCmsCluster.hh"
#include "XrdCms/XrdCmsClustID.hh"
#include "XrdCms/XrdCmsNode.hh"
#include "XrdCms/XrdCmsRole.hh"
#include "XrdCms/XrdCmsRRQ.hh"
#include "XrdCms/XrdCmsState.hh"
#include "XrdCms/XrdCmsSelect.hh"
#include "XrdCms/XrdCmsTrace.hh"
#include "XrdCms/XrdCmsTypes.hh"
#include "XrdOuc/XrdOucPup.hh"
#include "XrdSys/XrdSysPlatform.hh"
#include "XrdSys/XrdSysPthread.hh"
#include "XrdSys/XrdSysTimer.hh"
using namespace XrdCms;
/******************************************************************************/
/* G l o b a l O b j e c t s */
/******************************************************************************/
XrdCmsCluster XrdCms::Cluster;
/******************************************************************************/
/* L o c a l S t r u c t u r e s */
/******************************************************************************/
class XrdCmsDrop : XrdJob
{
public:
void DoIt() {Cluster.STMutex.Lock();
int rc = Cluster.Drop(nodeEnt, nodeInst, this);
Cluster.STMutex.UnLock();
if (!rc) delete this;
}
XrdCmsDrop(int nid, int inst) : XrdJob("drop node")
{nodeEnt = nid;
nodeInst = inst;
Sched->Schedule((XrdJob *)this, time(0)+Config.DRPDelay);
}
~XrdCmsDrop() {}
int nodeEnt;
int nodeInst;
};
/******************************************************************************/
/* C o n s t r u c t o r */
/******************************************************************************/
XrdCmsCluster::XrdCmsCluster()
{
memset((void *)NodeTab, 0, sizeof(NodeTab));
memset((void *)AltMans, (int)' ', sizeof(AltMans));
AltMend = AltMans;
AltMent = -1;
NodeCnt = 0;
STHi = -1;
SelWcnt = 0;
SelWtot = 0;
SelRcnt = 0;
SelRtot = 0;
SelTcnt = 0;
doReset = 0;
resetMask = 0;
peerHost = 0;
peerMask = ~peerHost;
}
/******************************************************************************/
/* A d d */
/******************************************************************************/
XrdCmsNode *XrdCmsCluster::Add(XrdLink *lp, int port, int Status, int sport,
const char *theNID, const char *theIF)
{
EPNAME("Add")
const char *act = "";
XrdCmsNode *nP = 0;
XrdCmsClustID *cidP = 0;
XrdSysMutexHelper STMHelper(STMutex);
int tmp, Slot, Free = -1, Bump1 = -1, Bump2 = -1, Bump3 = -1, aSet = 0;
bool Special = (Status & (CMS_isMan|CMS_isPeer));
bool SpecAlt = (Special && !(Status & CMS_isSuper));
bool Hidden = false;
// Find available slot for this node. Here are the priorities:
// Slot = Reconnecting node
// Free = Available slot ( 1st in table)
// Bump1 = Disconnected server (last in table)
// Bump2 = Connected server (last in table) if new one is managr/peer
// Bump3 = Disconnected managr/peer ( 1st in table) if new one is managr/peer
//
for (Slot = 0; Slot < STMax; Slot++)
if (NodeTab[Slot])
{if (NodeTab[Slot]->isNode(lp, theNID, port)) break;
/*Conn*/ if (NodeTab[Slot]->isConn)
{if (!NodeTab[Slot]->isPerm && Special)
Bump2 = Slot; // Last conn Server
/*Disc*/ } else {
if ( NodeTab[Slot]->isPerm)
{if (Bump3 < 0 && Special) Bump3 = Slot;}// 1st disc Man/Pr
else Bump1 = Slot; // Last disc Server
}
} else if (Free < 0) Free = Slot; // 1st free slot
// Check if node is already logged in or is a relogin
//
if (Slot < STMax)
{if (NodeTab[Slot] && NodeTab[Slot]->isBound)
{Say.Emsg("Cluster", lp->ID, "already logged in.");
return 0;
} else { // Rehook node to previous unconnected entry
nP = NodeTab[Slot];
nP->Link = lp;
nP->isOffline = 0;
nP->isBad &= ~XrdCmsNode::isSuspend;
nP->isConn = 1;
nP->Instance++;
nP->setName(lp, theIF, port); // Just in case it changed
act = "Reconnect ";
}
}
// First see if this node may be an alternate
//
if (!nP && SpecAlt)
{if ((cidP = XrdCmsClustID::Find(theNID)) && !(cidP->IsEmpty()))
{if (!(nP = AddAlt(cidP, lp, port, Status, sport, theNID, theIF)))
return 0;
aSet = 1; Slot = nP->NodeID;
if (nP != NodeTab[Slot]) {Hidden = true; act = "Alternate ";}
}
}
// Reuse an old ID if we must or redirect the incomming node
//
if (!nP)
{if (Free >= 0) Slot = Free;
else {if (Bump1 >= 0) Slot = Bump1;
else Slot = (Bump2 >= 0 ? Bump2 : Bump3);
if (Slot < 0)
{if (Status & CMS_isPeer) Say.Emsg("Cluster", "Add peer", lp->ID,
"failed; too many subscribers.");
else {sendAList(lp);
DEBUG(lp->ID <<" redirected; too many subscribers.");
}
return 0;
}
if (Status & CMS_isMan) {setAltMan(Slot, lp, sport); aSet=1;}
if (NodeTab[Slot] && !(Status & CMS_isPeer))
sendAList(NodeTab[Slot]->Link);
DEBUG(lp->ID << " bumps " << NodeTab[Slot]->Ident <<" #" <<Slot);
NodeTab[Slot]->Lock();
Remove("redirected", NodeTab[Slot], -1);
act = "Shoved ";
}
NodeTab[Slot] = nP = new XrdCmsNode(lp, theIF, theNID, port, 0, Slot);
if (!cidP) cidP = XrdCmsClustID::AddID(theNID);
if ((cidP->AddNode(nP, SpecAlt))) nP->cidP = cidP;
else {delete nP; NodeTab[Slot] = 0; return 0;}
}
// Indicate whether this snode can be redirected
//
nP->isPerm = (Status & (CMS_isMan | CMS_isPeer)) ? 1 : 0;
// Assign new server
//
if (!aSet && (Status & CMS_isSuper)) setAltMan(Slot, lp, sport);
if (Slot > STHi) STHi = Slot;
nP->isBound = 1;
nP->isConn = 1;
nP->isNoStage = 0 != (Status & CMS_noStage);
nP->isBad |= (Status & CMS_Suspend ? XrdCmsNode::isSuspend : 0);
nP->isMan = 0 != (Status & CMS_isMan);
nP->isPeer = 0 != (Status & CMS_isPeer);
nP->isBad |= XrdCmsNode::isDisabled;
nP->subsPort = sport;
// If this is an actual non-hidden node, count it
//
if (!Hidden)
{NodeCnt++;
if (Config.SUPLevel
&& (tmp = NodeCnt*Config.SUPLevel/100) > Config.SUPCount)
{Config.SUPCount=tmp; CmsState.Set(tmp);}
} else nP->isMan |= 0x02;
// Compute new peer mask, as needed
//
if (nP->isPeer) peerHost |= nP->NodeMask;
else peerHost &= ~nP->NodeMask;
peerMask = ~peerHost;
// Document login
//
if (QTRACE(Debug))
{DEBUG(act <<nP->Ident <<" to cluster " <<nP->myNID <<" slot "
<<Slot <<'.' <<nP->Instance <<" (nodecnt=" <<NodeCnt
<<" supn=" <<Config.SUPCount <<")");
}
// Compute new state of all nodes if we are a reporting manager.
//
if (Config.asManager() && !Hidden)
CmsState.Update(XrdCmsState::Counts,
nP->isBad & XrdCmsNode::isSuspend ? 0 : 1,
nP->isNoStage ? 0 : 1);
// All done
//
return nP;
}
/******************************************************************************/
/* Private: A d d A l t */
/******************************************************************************/
XrdCmsNode *XrdCmsCluster::AddAlt(XrdCmsClustID *cidP, XrdLink *lp,
int port, int Status, int sport,
const char *theNID, const char *theIF)
{
EPNAME("AddAlt")
XrdCmsNode *pP, *nP = 0;
int slot = cidP->Slot();
// Check if this node is already in the alternate table
//
if (cidP->Exists(lp, theNID, port))
{Say.Emsg(epname, lp->ID, "already logged in.");
return 0;
}
// Add this node if there is room
//
if (cidP->Avail())
{nP = new XrdCmsNode(lp, theIF, theNID, port, 0, slot);
if (!(cidP->AddNode(nP, true))) {delete nP; nP = 0;}
}
// Check if we were actually able to add this node
//
if (!nP)
{Say.Emsg(epname, "Add alternate manager", lp->ID,
"failed; too many subscribers.");
return 0;
}
// Check if the existing lead dead and we can substiture this one
//
if ((pP = NodeTab[slot]) && !(pP->isBound))
{setAltMan(nP->NodeID, nP->Link, sport);
Say.Emsg("AddAlt", nP->Ident, "replacing dropped", pP->Ident);
NodeTab[slot] = nP;
delete pP;
}
// Hook the node to the cluster table and return
//
nP->cidP = cidP;
return nP;
}
/******************************************************************************/
/* B l a c k L i s t */
/******************************************************************************/
void XrdCmsCluster::BlackList(XrdOucTList *blP)
{
XrdCmsNode *nP;
int i;
bool inBL;
// Obtain a lock on the table
//
STMutex.Lock();
// Run through the table looking to put or out of the blacklist
//
for (i = 0; i <= STHi; i++)
{if ((nP = NodeTab[i]))
{inBL = (blP && XrdCmsBlackList::Present(nP->Name(), blP));
if ((inBL && (nP->isBad & XrdCmsNode::isBlisted))
|| (!inBL && !(nP->isBad & XrdCmsNode::isBlisted)))
continue;
nP->Lock();
STMutex.UnLock();
if (inBL)
{nP->isBad |= XrdCmsNode::isBlisted;
Say.Emsg("Manager", nP->Name(), "blacklisted.");
} else {
nP->isBad &= ~XrdCmsNode::isBlisted;
Say.Emsg("Manager", nP->Name(), "removed from blacklist.");
}
nP->UnLock();
STMutex.Lock();
}
}
STMutex.UnLock();
}
/******************************************************************************/
/* B r o a d c a s t */
/******************************************************************************/
SMask_t XrdCmsCluster::Broadcast(SMask_t smask, const struct iovec *iod,
int iovcnt, int iotot)
{
EPNAME("Broadcast")
int i;
XrdCmsNode *nP;
SMask_t bmask, unQueried(0);
// Obtain a lock on the table and screen out peer nodes
//
STMutex.Lock();
bmask = smask & peerMask;
// Run through the table looking for nodes to send messages to
//
for (i = 0; i <= STHi; i++)
{if ((nP = NodeTab[i]) && nP->isNode(bmask))
{nP->Lock();
STMutex.UnLock();
if (nP->Send(iod, iovcnt, iotot) < 0)
{unQueried |= nP->Mask();
DEBUG(nP->Ident <<" is unreachable");
}
nP->UnLock();
STMutex.Lock();
}
}
STMutex.UnLock();
return unQueried;
}
/******************************************************************************/
SMask_t XrdCmsCluster::Broadcast(SMask_t smask, XrdCms::CmsRRHdr &Hdr,
char *Data, int Dlen)
{
struct iovec ioV[3], *iovP = &ioV[1];
unsigned short Temp;
int Blen;
// Construct packed data for the character argument. If data is a string then
// Dlen must include the null byte if it is specified at all.
//
Blen = XrdOucPup::Pack(&iovP, Data, Temp, (Dlen ? strlen(Data)+1 : Dlen));
Hdr.datalen = htons(static_cast<unsigned short>(Blen));
// Complete the iovec and send off the data
//
ioV[0].iov_base = (char *)&Hdr; ioV[0].iov_len = sizeof(Hdr);
return Broadcast(smask, ioV, 3, Blen+sizeof(Hdr));
}
/******************************************************************************/
SMask_t XrdCmsCluster::Broadcast(SMask_t smask, XrdCms::CmsRRHdr &Hdr,
void *Data, int Dlen)
{
struct iovec ioV[2] = {{(char *)&Hdr, sizeof(Hdr)},
{(char *)Data, (size_t)Dlen}};
// Send of the data as eveything was constructed properly
//
Hdr.datalen = htons(static_cast<unsigned short>(Dlen));
return Broadcast(smask, ioV, 2, Dlen+sizeof(Hdr));
}
/******************************************************************************/
/* B r o a d s e n d */
/******************************************************************************/
int XrdCmsCluster::Broadsend(SMask_t Who, XrdCms::CmsRRHdr &Hdr,
void *Data, int Dlen)
{
EPNAME("Broadsend");
static int Start = 0;
XrdCmsNode *nP;
struct iovec ioV[2] = {{(char *)&Hdr, sizeof(Hdr)},
{(char *)Data, (size_t)Dlen}};
int i, Beg, Fin, ioTot = Dlen+sizeof(Hdr);
// Send of the data as eveything was constructed properly
//
Hdr.datalen = htons(static_cast<unsigned short>(Dlen));
// Obtain a lock on the table and get the starting and ending position. Note
// that the mechnism we use will necessarily skip newly added nodes.
//
STMutex.Lock();
Beg = Start = (Start <= STHi ? Start+1 : 0);
Fin = STHi;
// Run through the table looking for a node to send the message to
//
do{for (i = Beg; i <= Fin; i++)
{if ((nP = NodeTab[i]) && nP->isNode(Who))
{nP->Lock();
STMutex.UnLock();
if (nP->Send(ioV, 2, ioTot) >= 0) {nP->UnLock(); return 1;}
DEBUG(nP->Ident <<" is unreachable");
nP->UnLock();
STMutex.Lock();
}
}
if (!Beg) break;
Fin = Beg-1; Beg = 0;
} while(1);
// Did not send to anyone
//
STMutex.UnLock();
return 0;
}
/******************************************************************************/
/* g e t M a s k */
/******************************************************************************/
SMask_t XrdCmsCluster::getMask(const XrdNetAddr *addr)
{
int i;
XrdCmsNode *nP;
SMask_t smask(0);
// Obtain a lock on the table
//
STMutex.Lock();
// Run through the table looking for a node with matching IP address
//
for (i = 0; i <= STHi; i++)
if ((nP = NodeTab[i]) && nP->isNode(addr))
{smask = nP->NodeMask; break;}
// All done
//
STMutex.UnLock();
return smask;
}
/******************************************************************************/
SMask_t XrdCmsCluster::getMask(const char *Cid)
{
return XrdCmsClustID::Mask(Cid);
}
/******************************************************************************/
/* L i s t */
/******************************************************************************/
XrdCmsSelected *XrdCmsCluster::List(SMask_t mask, CmsLSOpts opts, bool &oksel)
{
static const int iSize = XrdCmsSelected::IdentSize;
XrdCmsNode *nP;
XrdCmsSelected *sipp = 0, *sip;
XrdNetIF::ifType ifType = (XrdNetIF::ifType)(opts & LS_IFMASK);
XrdNetIF::ifType ifGet = ifType;
int i, destLen;
bool retName = (opts & LS_IDNT) != 0;
bool retAny = (opts & LS_ANY ) != 0;
bool retDest = retName || (opts & LS_IPO);
// If only one wanted, the select appropriately
//
oksel = false;
STMutex.Lock();
for (i = 0; i <= STHi; i++)
if ((nP=NodeTab[i]) && (nP->NodeMask & mask))
{oksel = true;
if (retDest)
{ if (nP->netIF.HasDest(ifType)) ifGet = ifType;
else if (!retAny) continue;
else {ifGet = (XrdNetIF::ifType)(ifType ^ XrdNetIF::PrivateIF);
if (!nP->netIF.HasDest(ifGet)) continue;
}
}
sip = new XrdCmsSelected(sipp);
if (retDest) destLen = nP->netIF.GetDest(sip->Ident, iSize,
ifGet, retName);
else if (nP->myNlen >= XrdCmsSelected::IdentSize) destLen = 0;
else {strcpy(sip->Ident, nP->myName); destLen = nP->myNlen;}
if (!destLen) {delete sip; continue;}
sip->IdentLen = destLen;
sip->Mask = nP->NodeMask;
sip->Id = nP->NodeID;
sip->Port = nP->netIF.Port();
sip->RefTotW = nP->RefTotW;
sip->RefTotR = nP->RefTotR;
sip->Shrin = nP->Shrin;
sip->Share = nP->Share;
sip->RoleID = nP->RoleID;
sip->Status = (nP->isOffline ? XrdCmsSelected::Offline : 0);
if (nP->isBad & (XrdCmsNode::isDisabled | XrdCmsNode::isBlisted))
sip->Status |= XrdCmsSelected::Disable;
if (nP->isNoStage) sip->Status |= XrdCmsSelected::NoStage;
if (nP->isBad & XrdCmsNode::isSuspend)
sip->Status |= XrdCmsSelected::Suspend;
if (nP->isRW ) sip->Status |= XrdCmsSelected::isRW;
if (nP->isMan ) sip->Status |= XrdCmsSelected::isMangr;
//??? nP->UnLock();
sipp = sip;
}
STMutex.UnLock();
// Return result
//
return sipp;
}
/******************************************************************************/
/* L o c a t e */
/******************************************************************************/
int XrdCmsCluster::Locate(XrdCmsSelect &Sel)
{
EPNAME("Locate");
XrdCmsPInfo pinfo;
SMask_t qfVec(0);
char *Path;
int retc = 0;
// Check if this is a locate for all current servers
//
if (*Sel.Path.Val != '*') Path = Sel.Path.Val;
else {if (*(Sel.Path.Val+1) == '\0')
{Sel.Vec.hf = ~0LL; Sel.Vec.pf = Sel.Vec.wf = 0;
return 0;
}
Path = Sel.Path.Val+1;
}
// Find out who serves this path
//
if (!Cache.Paths.Find(Path, pinfo) || !pinfo.rovec)
{Sel.Vec.hf = Sel.Vec.pf = Sel.Vec.wf = 0;
return -1;
} else Sel.Vec.wf = pinfo.rwvec;
// Check if this was a non-lookup request
//
if (*Sel.Path.Val == '*')
{Sel.Vec.hf = pinfo.rovec; Sel.Vec.pf = 0;
Sel.Vec.wf = pinfo.rwvec;
return 0;
}
// Complete the request info object if we have one
//
if (Sel.InfoP)
{Sel.InfoP->rwVec = pinfo.rwvec;
Sel.InfoP->isLU = 1;
}
// If we are running a shared file system preform an optional restricted
// pre-selection and then do a standard selection.
//
if (baseFS.isDFS())
{SMask_t amask, smask, pmask;
amask = pmask = pinfo.rovec;
smask = (Sel.Opts & XrdCmsSelect::Online ? 0 : pinfo.ssvec & amask);
Sel.Resp.DLen = 0;
if (!(retc = SelDFS(Sel, amask, pmask, smask, 1)))
return (Sel.Opts & XrdCmsSelect::Asap && Sel.InfoP
? Cache.WT4File(Sel,Sel.Vec.hf) : Config.LUPDelay);
if (retc < 0) return -1;
return 0;
}
// First check if we have seen this file before. If so, get nodes that have it.
// A Refresh request kills this because it's as if we hadn't seen it before.
// If the file was found but either a query is in progress or we have a server
// bounce; the client must wait.
//
if (Sel.Opts & XrdCmsSelect::Refresh
|| !(retc = Cache.GetFile(Sel, pinfo.rovec)))
{Cache.AddFile(Sel, 0);
qfVec = pinfo.rovec; Sel.Vec.hf = 0;
} else qfVec = Sel.Vec.bf;
// Compute the delay, if any
//
if ((!qfVec && retc >= 0) || (Sel.Vec.hf && Sel.InfoP)) retc = 0;
else if (!(retc = Cache.WT4File(Sel, Sel.Vec.hf))) retc = -2;
// Check if we have to ask any nodes if they have the file
//
if (qfVec)
{CmsStateRequest QReq = {{Sel.Path.Hash, kYR_state, kYR_raw, 0}};
if (Sel.Opts & XrdCmsSelect::Refresh)
QReq.Hdr.modifier |= CmsStateRequest::kYR_refresh;
TRACE(Files, "seeking " <<Sel.Path.Val);
qfVec = Cluster.Broadcast(qfVec, QReq.Hdr,
(void *)Sel.Path.Val, Sel.Path.Len+1);
if (qfVec) Cache.UnkFile(Sel, qfVec);
}
return retc;
}
/******************************************************************************/
/* M o n P e r f */
/******************************************************************************/
void *XrdCmsCluster::MonPerf()
{
CmsUsageRequest Usage = {{0, kYR_usage, 0, 0}};
struct iovec ioV[] = {{(char *)&Usage, sizeof(Usage)}};
int ioVnum = sizeof(ioV)/sizeof(struct iovec);
int ioVtot = sizeof(Usage);
SMask_t allNodes(~0);
int uInterval = Config.AskPing*Config.AskPerf;
// Sleep for the indicated amount of time, then ask for load on each server
//
while(uInterval)
{XrdSysTimer::Snooze(uInterval);
Broadcast(allNodes, ioV, ioVnum, ioVtot);
}
return (void *)0;
}
/******************************************************************************/
/* M o n R e f s */
/******************************************************************************/
void *XrdCmsCluster::MonRefs()
{
XrdCmsNode *nP;
int i, snooze_interval = 10*60, loopmax, loopcnt = 0;
int resetW, resetR, resetWR;
// Compute snooze interval
//
if ((loopmax = Config.RefReset / snooze_interval) <= 1)
{if (!Config.RefReset) loopmax = 0;
else {loopmax = 1; snooze_interval = Config.RefReset;}
}
// Sleep for the snooze interval. If a reset was requested then do a selective
// reset unless we reached our snooze maximum and enough selections have gone
// by; in which case, do a global reset.
//
do {XrdSysTimer::Snooze(snooze_interval);
loopcnt++;
STMutex.Lock();
resetW = (SelWcnt >= Config.RefTurn);
resetR = (SelRcnt >= Config.RefTurn);
resetWR = (loopmax && loopcnt >= loopmax && (resetW || resetR));
if (doReset || resetWR)
{for (i = 0; i <= STHi; i++)
if ((nP = NodeTab[i])
&& (resetWR || (doReset && nP->isNode(resetMask))) )
{nP->Lock();
if (resetW || doReset) nP->RefW=0;
if (resetR || doReset) nP->RefR=0;
nP->Shrem = nP->Share;
nP->UnLock();
}
if (resetWR)
{if (resetW) {SelWtot += SelWcnt; SelWcnt = 0;}
if (resetR) {SelRtot += SelRcnt; SelRcnt = 0;}
loopcnt = 0;
}
if (doReset) {doReset = 0; resetMask = 0;}
}
STMutex.UnLock();
} while(1);
return (void *)0;
}
/******************************************************************************/
/* R e m o v e */
/******************************************************************************/
// Warning! The node object must be locked upon entry. The lock is released
// prior to returning to the caller. This entry obtains the node
// table lock. When immed != 0 then the node is immediately dropped.
// When immed if < 0 then the caller already holds the STMutex and it
// is not released upon exit.
void XrdCmsCluster::Remove(const char *reason, XrdCmsNode *theNode, int immed)
{
EPNAME("Remove_Node")
struct theLocks
{XrdSysMutex *myMutex;
XrdCmsNode *myNode;
int myNID;
int myInst;
bool hasLK;
bool doDrop;
char myIdent[510];
theLocks(XrdSysMutex *mtx, XrdCmsNode *node, int immed)
: myMutex(mtx), myNode(node), hasLK(immed < 0),
doDrop(false)
{strlcpy(myIdent, node->Ident, sizeof(myIdent));
myNID = node->ID(myInst);
if (!hasLK)
{myNode->UnLock();
myMutex->Lock();
myNode->Lock();
}
}
~theLocks()
{if (myNode)
{if (doDrop)
{myNode->DropTime = 0;
myNode->DropJob = 0;
myNode->isBound = 0;
myNode->UnLock(); delete myNode;
} else myNode->UnLock();
}
if (!hasLK) myMutex->UnLock();
}
} LockHandler(&STMutex, theNode, immed);
XrdCmsNode *altNode = 0;
int Inst, NodeID = theNode->ID(Inst);
// The LockHandler makes sure that the proper locks are obtained in a deadlock
// free order. However, this may require that the node lock be released and
// then re-aquired. We check if we are still dealing with same node at entry.
// If not, issue message and high-tail it out.
//
if (LockHandler.myNID != NodeID || LockHandler.myInst != Inst)
{Say.Emsg("Manager", LockHandler.myIdent, "removal aborted.");
DEBUG(LockHandler.myIdent <<" node " <<NodeID <<'.' <<Inst <<" != "
<< LockHandler.myNID <<'.' <<LockHandler.myInst <<" at entry.");
}
// Mark node as being offline and remove any drop job from it
//
theNode->isOffline = 1;
// If the node is connected the simply close the connection. This will cause
// the connection handler to re-initiate the node removal. The LockHandler
// destructor will release the node table and node object locks as needed.
// This condition exists only if one node is being displaced by another node.
//
if (theNode->isConn)
{theNode->Disc(reason, 0);
theNode->isGone = 1;
return;
}
// If we are not the primary node, then get rid of this node post-haste
//
if (!(NodeTab[NodeID] == theNode))
{Say.Emsg("Remove_Node", theNode->Ident, "dropped as alternate.");
LockHandler.doDrop = true;
return;
}
// If the node is part of the cluster, do not count it anymore and
// indicate new state of this nodes if we are a reporting manager
//
if (theNode->isBound)
{theNode->isBound = 0;
NodeCnt--;
if (Config.asManager())
CmsState.Update(XrdCmsState::Counts,
theNode->isBad & XrdCmsNode::isSuspend ? 0 : -1,
theNode->isNoStage ? 0 : -1);
}
// If we have a working alternate, substitute it here and immediately drop
// the former primary. This allows the cache to remain warm.
//
if (theNode->isMan && theNode->cidP && !(theNode->cidP->IsSingle())
&& (altNode = theNode->cidP->RemNode(theNode)))
{if (altNode->isBound) NodeCnt++;
NodeTab[NodeID] = altNode;
if (Config.asManager())
CmsState.Update(XrdCmsState::Counts,
altNode->isBad & XrdCmsNode::isSuspend ? 0 : 1,
altNode->isNoStage ? 0 : 1);
setAltMan(altNode->NodeID, altNode->Link, altNode->subsPort);
Say.Emsg("Manager",altNode->Ident,"replacing dropped",theNode->Ident);
LockHandler.doDrop = true;
return;
}
// If this is an immediate drop request, do so now. Drop() will delete
// the node object, so remove the node lock and tell LockHandler that.
//
if (immed || !Config.DRPDelay)
{theNode->UnLock();
LockHandler.myNode = 0;
Drop(NodeID, Inst);
return;
}
// If a drop job is already scheduled, update the instance field. Otherwise,
// Schedule a node drop at a future time.
//
theNode->DropTime = time(0)+Config.DRPDelay;
if (theNode->DropJob) theNode->DropJob->nodeInst = Inst;
else theNode->DropJob = new XrdCmsDrop(NodeID, Inst);
// Document removal
//
if (reason)
Say.Emsg("Manager", theNode->Ident, "scheduled for removal;", reason);
else DEBUG(theNode->Ident <<" node " <<NodeID <<'.' <<Inst);
}
/******************************************************************************/
/* R e s e t R e f */
/******************************************************************************/
void XrdCmsCluster::ResetRef(SMask_t smask)
{
// Obtain a lock on the table
//
STMutex.Lock();
// Inform the reset thread that we need a reset
//
doReset = 1;
resetMask |= smask;
// Unlock table and exit
//
STMutex.UnLock();
}
/******************************************************************************/
/* S e l e c t */
/******************************************************************************/
int XrdCmsCluster::Select(XrdCmsSelect &Sel)
{
EPNAME("Select");
XrdCmsPInfo pinfo;
const char *Amode;
int dowt = 0, retc = 0, isRW, fRD, noSel = (Sel.Opts & XrdCmsSelect::Defer);
SMask_t amask, smask, pmask;
// Establish some local options
//
if (Sel.Opts & XrdCmsSelect::Write)
{isRW = 1; Amode = "write";
if (Config.RWDelay)
if (Sel.Opts & XrdCmsSelect::Create && Config.RWDelay < 2) fRD = 1;
else fRD = 0;
else fRD = 1;
}
else {isRW = 0; Amode = "read"; fRD = 1;}
// Find out who serves this path
//
if (!Cache.Paths.Find(Sel.Path.Val, pinfo)
|| (amask = ((isRW ? pinfo.rwvec : pinfo.rovec) & ~Sel.nmask)) == 0)
{Sel.Resp.DLen = snprintf(Sel.Resp.Data, sizeof(Sel.Resp.Data)-1,
"No servers have %s access to the file", Amode)+1;
return -1;
}
// If we are running a shared file system preform an optional restricted
// pre-selection and then do a standard selection.
//
if (baseFS.isDFS())
{pmask = amask;
smask = (Sel.Opts & XrdCmsSelect::Online ? 0 : pinfo.ssvec & amask);
if (baseFS.Trim())
{Sel.Resp.DLen = 0;
if (!(retc = SelDFS(Sel, amask, pmask, smask, isRW)))
return (fRD ? Cache.WT4File(Sel,Sel.Vec.hf) : Config.LUPDelay);
if (retc < 0) return -1;
} else if (noSel) return 0;
return SelNode(Sel, pmask, smask);
}
// If either a refresh is wanted or we didn't find the file, re-prime the cache
// which will force the client to wait. Otherwise, compute the primary and
// secondary selections. If there are none, the client may have to wait if we
// have servers that we can query regarding the file. Note that for files being
// opened in write mode, only one writable copy may exist unless this is a
// meta-operation (e.g., remove) in which case the file itself remain unmodified
// or a replica request, in which case we select a new target server.
//
if (!(Sel.Opts & XrdCmsSelect::Refresh)
&& (retc = Cache.GetFile(Sel, pinfo.rovec)))
{if (isRW)
{ if (retc<0) return Config.LUPDelay;
else if (Sel.Opts & XrdCmsSelect::Replica)
{pmask = amask & ~(Sel.Vec.hf | Sel.Vec.bf); smask = 0;
if (!pmask && !Sel.Vec.bf) return SelFail(Sel,eNoRep);
}
else if (Sel.Vec.bf) pmask = smask = 0;
else if (Sel.Vec.hf)
{if (Sel.Opts & XrdCmsSelect::NewFile) return SelFail(Sel,eExists);
if (!(Sel.Opts & XrdCmsSelect::isMeta) && Config.DoMWChk
&& Multiple(Sel.Vec.hf)) return SelFail(Sel,eDups);
if (!(pmask = Sel.Vec.hf & amask)) return SelFail(Sel,eROfs);
smask = 0;
}
else if (Sel.Opts & (XrdCmsSelect::Trunc | XrdCmsSelect::NewFile))
{pmask = amask; smask = 0;}
else if ((smask = pinfo.ssvec & amask)) pmask = 0;
else pmask = smask = 0;
} else {
pmask = Sel.Vec.hf & amask;
if (Sel.Opts & XrdCmsSelect::Online) {pmask &= ~Sel.Vec.pf; smask=0;}
else smask = (retc < 0 ? 0 : pinfo.ssvec & amask);
}
if (Sel.Vec.hf & Sel.nmask) Cache.UnkFile(Sel, Sel.nmask);
} else {
Cache.AddFile(Sel, 0);
Sel.Vec.bf = pinfo.rovec;
Sel.Vec.hf = Sel.Vec.pf = pmask = smask = 0;
retc = 0;
}
// A wait is required if we don't have any primary or seconday servers
//
dowt = (!pmask && !smask);
// If we can query additional servers, do so now. The client will be placed
// in the callback queue only if we have no possible selections
//
if (Sel.Vec.bf)
{CmsStateRequest QReq = {{Sel.Path.Hash, kYR_state, kYR_raw, 0}};
if (Sel.Opts & XrdCmsSelect::Refresh)
QReq.Hdr.modifier |= CmsStateRequest::kYR_refresh;
if (dowt) retc= (fRD ? Cache.WT4File(Sel,Sel.Vec.hf) : Config.LUPDelay);
TRACE(Files, "seeking " <<Sel.Path.Val);
amask = Cluster.Broadcast(Sel.Vec.bf, QReq.Hdr,
(void *)Sel.Path.Val,Sel.Path.Len+1);
if (amask) Cache.UnkFile(Sel, amask);
if (dowt) return retc;
} else if (dowt && retc < 0 && !noSel)
return (fRD ? Cache.WT4File(Sel,Sel.Vec.hf) : Config.LUPDelay);
// Broadcast a freshen up request if wanted
//
if ((Sel.Opts & XrdCmsSelect::Freshen) && (amask = pmask & ~Sel.Vec.bf))