-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdSsiServReal.cc
307 lines (264 loc) · 10.5 KB
/
XrdSsiServReal.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
/******************************************************************************/
/* */
/* X r d S s i S e r v R e a l . c c */
/* */
/* (c) 2013 by the Board of Trustees of the Leland Stanford, Jr., University */
/* 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 <stdio.h>
#include <string.h>
#include "XrdSsi/XrdSsiReqAgent.hh"
#include "XrdSsi/XrdSsiResource.hh"
#include "XrdSsi/XrdSsiScale.hh"
#include "XrdSsi/XrdSsiServReal.hh"
#include "XrdSsi/XrdSsiSessReal.hh"
#include "XrdSsi/XrdSsiTrace.hh"
#include "XrdSsi/XrdSsiUtils.hh"
/******************************************************************************/
/* S t a t i c s & G l o b a l s */
/******************************************************************************/
namespace XrdSsi
{
XrdSsiScale sidScale;
}
using namespace XrdSsi;
/******************************************************************************/
/* D e s t r u c t o r */
/******************************************************************************/
XrdSsiServReal::~XrdSsiServReal()
{
XrdSsiSessReal *sP;
// Free pointer to the manager node
//
if (manNode) {free(manNode); manNode = 0;}
// Delete all free session objects
//
while((sP = freeSes))
{freeSes = sP->nextSess;
delete sP;
}
}
/******************************************************************************/
/* Private: A l l o c */
/******************************************************************************/
XrdSsiSessReal *XrdSsiServReal::Alloc(const char *sName, int uent, bool hold)
{
XrdSsiSessReal *sP;
// Reuse or allocate a new session object and return it
//
myMutex.Lock();
actvSes++;
if ((sP = freeSes))
{freeCnt--;
freeSes = sP->nextSess;
myMutex.UnLock();
sP->InitSession(this, sName, uent, hold);
} else {
myMutex.UnLock();
if (!(sP = new XrdSsiSessReal(this, sName, uent, hold)))
{myMutex.Lock(); actvSes--; myMutex.UnLock();}
}
return sP;
}
/******************************************************************************/
/* Private: G e n U R L */
/******************************************************************************/
bool XrdSsiServReal::GenURL(XrdSsiResource *rP, char *buff, int blen, int uEnt)
{
static const char affTab[] = "\0\0n\0w\0s\0S";
const char *xUsr, *xAt, *iSep, *iVal, *tVar, *tVal, *uVar, *uVal;
const char *aVar, *aVal, *qVal = "";
char uBuff[8];
int n;
// Preprocess avoid list, if any
//
if (rP->hAvoid.length() == 0) tVar = tVal = "";
else {tVar = "&tried=";
tVal = rP->hAvoid.c_str();
qVal = "?";
}
// Preprocess affinity
//
if (!(rP->affinity)) aVar = aVal = "";
else {aVar = "&cms.aff=";
aVal = &affTab[rP->affinity*2];
qVal = "?";
}
// Check if we need to add a user name
//
if (rP->rUser.length() == 0) uVar = uVal = "";
else {uVar = "&ssi.user=";
uVal = rP->rUser.c_str();
qVal = "?";
}
// Preprocess the cgi information
//
if (rP->rInfo.length() == 0) iSep = iVal = "";
else {iVal = rP->rInfo.c_str();
iSep = "&ssi.cgi=";
qVal = "?";
}
// Check if we need to qualify the host with a user index
//
if (uEnt == 0) xUsr = xAt = "";
else {snprintf(uBuff, sizeof(uBuff), "%d", uEnt);
uVal = uBuff;
xAt = "@";
}
// Generate appropriate url
// ? t a u i
n = snprintf(buff, blen, "xroot://%s%s%s/%s%s%s%s%s%s%s%s%s%s",
xUsr, xAt, manNode, rP->rName.c_str(), qVal,
tVar, tVal, aVar, aVal,
uVar, uVal, iSep, iVal);
// Return overflow or not
//
return n < blen;
}
/******************************************************************************/
/* P r o c e s s R e q u e s t */
/******************************************************************************/
void XrdSsiServReal::ProcessRequest(XrdSsiRequest &reqRef,
XrdSsiResource &resRef)
{
static const uint32_t useCache = XrdSsiResource::Reusable
| XrdSsiResource::Discard;
XrdSysMutexHelper mHelp;
XrdSsiSessReal *sObj;
std::string resKey;
int uEnt;
bool hold = (resRef.rOpts & XrdSsiResource::Reusable) != 0;
char epURL[4096];
// Validate the resource name
//
if (resRef.rName.length() == 0)
{XrdSsiUtils::RetErr(reqRef, "Resource name missing.", EINVAL);
return;
}
// Check if this is a reusable resource. Reusable resources are a bit more
// complicated to pull off. In any case, we need to hold the cache lock.
//
if (resRef.rOpts & useCache)
{mHelp.Lock(&rcMutex);
if (ResReuse(reqRef, resRef, resKey)) return;
}
// Get a sid entry number
//
if ((uEnt = sidScale.getEnt()) < 0)
{XrdSsiUtils::RetErr(reqRef, "Out of stream resources.", ENOSR);
return;
}
// Construct url
//
if (!GenURL(&resRef, epURL, sizeof(epURL), uEnt))
{XrdSsiUtils::RetErr(reqRef, "Resource url is too long.", ENAMETOOLONG);
sidScale.retEnt(uEnt);
return;
}
// Obtain a new session object
//
if (!(sObj = Alloc(resRef.rName.c_str(), uEnt, hold)))
{XrdSsiUtils::RetErr(reqRef, "Insufficient memory.", ENOMEM);
sidScale.retEnt(uEnt);
return;
}
// Now just provision this resource which will execute the request should it
// be successful. If Provision() fails, we need to delete the session object
// because its file object now is in an usable state (funky client interface).
//
if (!(sObj->Provision(&reqRef, epURL))) Recycle(sObj, false);
// If this was started with a reusable resource, put the session in the cache.
// The resource key was constructed by the call to ResReuse() and teh cache
// mutex is still held at this point (will be released upon return).
//
if (hold) resCache[resKey] = sObj;
}
/******************************************************************************/
/* R e c y c l e */
/******************************************************************************/
void XrdSsiServReal::Recycle(XrdSsiSessReal *sObj, bool reuse)
{
EPNAME("Recycle");
static const char *tident = "ServReal";
// Clear all pending events (likely not needed)
//
sObj->ClrEvent();
// Add to queue unless we have too many of these
//
myMutex.Lock();
actvSes--;
DEBUG("reuse=" <<reuse <<"; sessions: free=" <<freeCnt <<" active=" <<actvSes);
if (!reuse || freeCnt >= freeMax) {myMutex.UnLock(); delete sObj;}
else {sObj->nextSess = freeSes;
freeSes = sObj;
freeCnt++;
myMutex.UnLock();
}
}
/******************************************************************************/
/* Private: R e s R e u s e */
/******************************************************************************/
// Called with rcMutex held!
bool XrdSsiServReal::ResReuse(XrdSsiRequest &reqRef,
XrdSsiResource &resRef,
std::string &resKey)
{
std::map<std::string, XrdSsiSessReal *>::iterator it;
XrdSsiSessReal *sesP;
// Construct lookup key
//
resKey.reserve(resRef.rUser.size() + resRef.rName.size() + 2);
resKey = resRef.rUser;
resKey += "@";
resKey += resRef.rName;
// Find the cache entry
//
it = resCache.find(resKey);
if (it == resCache.end()) return false;
// Entry found, check if this session can actually be reused
//
sesP = it->second;
if (resRef.rOpts & XrdSsiResource::Discard || !sesP->Run(&reqRef))
{resCache.erase(it);
sesP->UnHold();
return false;
}
// All done, the request should have been sent off via Reusable() call.
//
return true;
}
/******************************************************************************/
/* S t o p */
/******************************************************************************/
bool XrdSsiServReal::Stop()
{
// Make sure we are clean
//
myMutex.Lock();
if (actvSes) {myMutex.UnLock(); return false;}
myMutex.UnLock();
delete this;
return true;
}