XRootD
XrdCmsClustID.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d C m s C l u s t I D . c c */
4 /* */
5 /* (c) 2014 by the Board of Trustees of the Leland Stanford, Jr., University */
6 /* Produced by Andrew Hanushevsky for Stanford University under contract */
7 /* DE-AC02-76-SFO0515 with the Deprtment of Energy */
8 /* */
9 /* This file is part of the XRootD software suite. */
10 /* */
11 /* XRootD is free software: you can redistribute it and/or modify it under */
12 /* the terms of the GNU Lesser General Public License as published by the */
13 /* Free Software Foundation, either version 3 of the License, or (at your */
14 /* option) any later version. */
15 /* */
16 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
17 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
18 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
19 /* License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public License */
22 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
23 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
24 /* */
25 /* The copyright holder's institutional names and contributor's names may not */
26 /* be used to endorse or promote products derived from this software without */
27 /* specific prior written permission of the institution or contributor. */
28 /******************************************************************************/
29 
30 #include <cstdio>
31 
32 #include "XrdCms/XrdCmsClustID.hh"
33 #include "XrdCms/XrdCmsNode.hh"
34 #include "XrdCms/XrdCmsTrace.hh"
35 #include "XrdOuc/XrdOucHash.hh"
36 #include "XrdSys/XrdSysPthread.hh"
37 
38 using namespace XrdCms;
39 
40 /******************************************************************************/
41 /* L o c a l S t a t i c O b j e c t s */
42 /******************************************************************************/
43 
44 namespace
45 {
46 XrdSysMutex cidMtx;
47 
49 
50 XrdCmsClustID *cidFree = new XrdCmsClustID();
51 };
52 
53 /******************************************************************************/
54 /* Static: A d d I D */
55 /******************************************************************************/
56 
58 {
59  XrdCmsClustID *cidObj;
60  const char *cHN;
61  char *clustID;
62 
63 // Massage the clusterid (it's in bi-compatible format)
64 //
65  if ((cHN = rindex(cID, ' ')) && *(cHN+1)) cID = cHN+1;
66  clustID = strdup(cID);
67 
68 // Lock ourselves
69 //
70  cidMtx.Lock();
71 
72 // Allocate a new cluster ID object if we don't have one ready
73 //
74  if (!cidFree) cidFree = new XrdCmsClustID();
75 
76 // Attempt to add this object to our cid table
77 //
78  if (!(cidObj = cidTab.Add(clustID, cidFree, 0, Hash_keep)))
79  {cidObj = cidFree;
80  cidObj->cidName = clustID;
81  cidFree = new XrdCmsClustID();
82  } else free(clustID);
83 
84 // We can unlock now
85 //
86  cidMtx.UnLock();
87 
88 // Return the entry
89 //
90  return cidObj;
91 }
92 
93 /******************************************************************************/
94 /* A d d N o d e */
95 /******************************************************************************/
96 
97 bool XrdCmsClustID::AddNode(XrdCmsNode *nP, bool isMan)
98 {
99  EPNAME("AddNode");
100  XrdSysMutexHelper cidHelper(cidMtx);
101  int iNum, sNum;
102 
103 // For servers we only add the identification mask
104 //
105  if (!isMan)
106  {cidMask |= nP->Mask();
107  DEBUG("srv " <<nP->Ident <<" cluster " <<cidName <<" anum=" <<npNum);
108  return true;
109  }
110 
111 // Make sure we have enough space in the table
112 //
113  if (npNum >= altMax)
114  {Say.Emsg("ClustID",cidName,"alternate table full; rejecting",nP->Name());
115  return false;
116  }
117 
118 // Make sure the slot numbers match for this node
119 //
120  sNum = nP->ID(iNum);
121  if (npNum > 0 && ntSlot != sNum)
122  {char buff[256];
123  sprintf(buff,"cluster slot mismatch: %d != %d; rejecting",sNum,ntSlot);
124  Say.Emsg("ClustID", cidName, buff, nP->Name());
125  return false;
126  }
127 
128 // Add the entry to the table
129 //
130  ntSlot = sNum;
131  cidMask |= nP->Mask();
132  nodeP[npNum++] = nP;
133  DEBUG("man " <<nP->Ident <<" cluster " <<cidName <<" anum=" <<npNum);
134  return true;
135 }
136 
137 /******************************************************************************/
138 /* E x i s t s */
139 /******************************************************************************/
140 
141 bool XrdCmsClustID::Exists(XrdLink *lp, const char *nid, int port)
142 {
143  XrdSysMutexHelper cidHelper(cidMtx);
144 
145 // Simply scan the table to see if this node is present
146 //
147  for (int i = 0; i <npNum; i++)
148  {if (nodeP[i]->isNode(lp, nid, port)) return true;}
149  return false;
150 }
151 
152 /******************************************************************************/
153 /* F i n d */
154 /******************************************************************************/
155 
157 {
158  XrdCmsClustID *cidObj;
159  const char *cHN;
160 
161 // Massage the clusterid (it's in bi-compatible format)
162 //
163  if ((cHN = rindex(cID, ' ')) && *(cHN+1)) cID = cHN+1;
164 
165 // Lock ourselves
166 //
167  cidMtx.Lock();
168 
169 // Attempt to find the cluster object
170 //
171  cidObj = cidTab.Find(cID);
172 
173 // We can unlock now
174 //
175  cidMtx.UnLock();
176 
177 // Return the entry
178 //
179  return cidObj;
180 }
181 
182 /******************************************************************************/
183 /* M a s k */
184 /******************************************************************************/
185 
186 SMask_t XrdCmsClustID::Mask(const char *cID)
187 {
188  XrdCmsClustID *cidObj;
189  SMask_t theMask;
190  const char *cHN;
191 
192 // Massage the clusterid (it's in bi-compatible format)
193 //
194  if ((cHN = rindex(cID, ' ')) && *(cHN+1)) cID = cHN+1;
195 
196 // Lock ourselves
197 //
198  cidMtx.Lock();
199 
200 // Attempt to find the cluster object
201 //
202  if ((cidObj = cidTab.Find(cID))) theMask = cidObj->cidMask;
203  else theMask = 0;
204 
205 // We can unlock now
206 //
207  cidMtx.UnLock();
208 
209 // Return the mask
210 //
211  return theMask;
212 }
213 
214 /******************************************************************************/
215 /* R e m N o d e */
216 /******************************************************************************/
217 
219 {
220  EPNAME("RemNode");
221  bool didRM = false;
222 
223 // For servers we only need to remove the mask
224 //
225  if (!(nP->isMan | nP->isPeer))
226  {cidMask &= ~(nP->Mask());
227  DEBUG("srv " <<nP->Ident <<" cluster " <<cidName <<" anum=" <<npNum);
228  return 0;
229  }
230 
231 // Find the node to remove. This may require a fill in.
232 //
233  for (int i = 0; i < npNum; i++)
234  if (nP == nodeP[i])
235  {npNum--;
236  if (i < npNum && npNum) nodeP[i] = nodeP[npNum];
237  else nodeP[i] = 0;
238  didRM = true;
239  break;
240  }
241 
242 // If there are no more nodes in this table, then remove the id mask
243 //
244  if (!npNum) cidMask &= ~(nP->Mask());
245 
246 // Do some debugging and return what we have in the table
247 //
248  DEBUG("man " <<nP->Ident <<" cluster " <<cidName <<" anum=" <<npNum
249  <<(didRM ? "" : " n/p"));
250  return (npNum ? nodeP[0] : 0);
251 }
#define DEBUG(x)
Definition: XrdBwmTrace.hh:54
#define EPNAME(x)
Definition: XrdBwmTrace.hh:56
unsigned long long SMask_t
Definition: XrdCmsTypes.hh:33
@ Hash_keep
Definition: XrdOucHash.hh:55
static XrdCmsClustID * AddID(const char *cID)
static SMask_t Mask(const char *cID)
XrdCmsNode * RemNode(XrdCmsNode *nP)
static XrdCmsClustID * Find(const char *cID)
bool Exists(XrdLink *lp, const char *nid, int port)
bool AddNode(XrdCmsNode *nP, bool isMan)
char * Ident
Definition: XrdCmsNode.hh:61
char isPeer
Definition: XrdCmsNode.hh:68
SMask_t Mask()
Definition: XrdCmsNode.hh:160
int ID(int &INum)
Definition: XrdCmsNode.hh:139
char isMan
Definition: XrdCmsNode.hh:67
char * Name()
Definition: XrdCmsNode.hh:158
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
Definition: XrdSysError.cc:95
XrdSysError Say