-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathXrootdSimple.cc
More file actions
284 lines (210 loc) · 7.76 KB
/
XrootdSimple.cc
File metadata and controls
284 lines (210 loc) · 7.76 KB
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
// $Id$
// XrootdSimple.cc -- Exercise performance of XRootD providing flat files
// as blocked lookup tables, 100M entries per file.
//
// 20151103 Michael Kelsey
// 20151113 Fix server configs per Andy H., move all temp files into data dir
// 20151113 Instead of "localhost", use call to gethostname() to get string
// 20151116 Remove debugging option from XRootD services.
// 20160217 Suppress sparse indexing for now, requires invasive changes
// 20160224 Move destructor action to cleanup() function
#include "XrootdSimple.hh"
#include "XrdCl/XrdClFile.hh"
#include "XrdCl/XrdClXRootDResponses.hh"
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// Constructor and destructor
XrootdSimple::XrootdSimple(int verbose)
: IndexTester("xrootd", verbose), entriesPerFile(10000000),
dirName("/tmp/xrootd_simple"), xrdConfigName("xrootd.conf"),
xrdServerPid(0), cmsdServerPid(0), xrdManagerPid(0), cmsdManagerPid(0) {
const size_t namelen = sysconf(_SC_HOST_NAME_MAX)+1;
char hostname[namelen];
gethostname(hostname, namelen);
localHostName = hostname;
}
XrootdSimple::~XrootdSimple() {
cleanup();
}
void XrootdSimple::cleanup() {
killServer();
killManager();
//*** deleteTempFiles(); // Do we really want to do this?
}
// Interface to base class for constructing and accessing servers
void XrootdSimple::create(objectId_t asize) {
SetIndexSpacing(1); // Override user request for sparse indices
if (verboseLevel) cout << "XrootdSimple::create " << asize << endl;
if (!writeXrdConfigFile()) {
cerr << "Unable to create XRootD configuration file!" << endl;
::exit(1);
}
if (!createTempFiles(asize/entriesPerFile+1)) {
cerr << "Index file creation failed!" << endl;
::exit(1);
}
if (!(launchServer() && launchManager())) {
cerr << "Unable to launch XRootD services!" << endl;
::exit(1);
}
sleep(10); // Wait for services to be ready for access
}
chunkId_t XrootdSimple::value(objectId_t index) {
if (verboseLevel>1) cout << "XrootdSimple::value " << index << endl;
size_t ifile = index / entriesPerFile;
size_t offset = index % entriesPerFile * sizeof(int);
string xrdFile = dirName+"/"+getTempFilename(ifile);
string xrdPath = localHostName+":10940/"+xrdFile;
if (verboseLevel>2) cout << "... accessing " << xrdPath << endl;
XrdCl::File blockFile;
if (!blockFile.Open(xrdPath,XrdCl::OpenFlags::Read).IsOK()) {
cerr << "Unable to access " << xrdPath << endl;
return -1;
}
if (verboseLevel>2) cout << "... reading at offset " << offset << endl;
chunkId_t readValue = 0; // Input buffer from XRD
uint32_t readLen = 0;
if (!blockFile.Read(offset, sizeof(chunkId_t), &readValue, readLen).IsOK()) {
cerr << "Unable to read " << xrdPath << " at offset " << offset << endl;
return -1;
}
if (verboseLevel>2) cout << "... closing file" << endl;
if (!blockFile.Close().IsOK()) {
cerr << "Unable to properly close " << xrdPath << endl;
}
return readValue;
}
// Create flat files locally (before XRootD starts) for lookup tables
bool XrootdSimple::createTempFiles(size_t nfiles) {
if (verboseLevel>1) {
cout << "XrootdSimple::createTempFiles " << nfiles << " in " << dirName
<< endl;
}
for (size_t ifile=0; ifile<nfiles; ifile++) {
if (!createTempFile(ifile)) return false; // Abandon if file fails
}
return true;
}
bool XrootdSimple::createTempFile(size_t ifile) {
string fname = dirName+"/"+getTempFilename(ifile);
// FIXME: We should be creating actual lookup files!
static const int zero=0; // Passed to fwrite as pointer
FILE* outf = fopen(fname.c_str(), "w");
for (size_t i=0; i<entriesPerFile; i++) {
fwrite(&zero,sizeof(int),1,outf);
}
fclose(outf); // Close and reopen for future access
return true;
}
// Create configuration file used to launch XRootD service processes
bool XrootdSimple::writeXrdConfigFile() {
string xrdConfigPath = dirName+"/"+xrdConfigName;
if (verboseLevel>1)
cout << "XrootdSimple::writeXrdConfigFile " << xrdConfigPath << endl;
if (!createTempDir()) return false;
ofstream confFile(xrdConfigPath.c_str());
if (!confFile) {
cerr << "Failed to create " << xrdConfigPath << endl;
return false;
}
confFile << "if named manager\n"
<< "all.role manager\n"
<< "xrd.port 10940 if exec xrootd\n"
<< "else\n"
<< "all.role server\n"
<< "xrd.port any\n"
<< "fi\n"
<< "all.manager " << localHostName << ":10950\n"
<< "all.adminpath " << dirName << "\n"
<< "all.pidpath " << dirName << "\n"
<< "all.export " << dirName << "\n"
<< "cms.delay startup 5"
<< endl;
return true;
}
bool XrootdSimple::launchServer() {
if (verboseLevel>1) cout << "XrootdSimple::launchServer" << endl;
if (cmsdServerPid>0 || xrdServerPid>0) killServer();
cmsdServerPid = launchService("cmsd", "server");
xrdServerPid = launchService("xrootd", "server");
return (xrdServerPid > 0 && cmsdServerPid > 0);
}
bool XrootdSimple::launchManager() {
if (verboseLevel>1) cout << "XrootdSimple::launchManager" << endl;
if (cmsdManagerPid>0 || xrdManagerPid>0) killManager();
cmsdManagerPid = launchService("cmsd", "manager");
xrdManagerPid = launchService("xrootd", "manager");
return (xrdManagerPid > 0 && cmsdManagerPid > 0);
}
pid_t XrootdSimple::launchService(const char* svcExec, const char* svcName) {
if (verboseLevel>1)
cout << "XrootdSimple::launchService " << svcExec << " " << svcName << endl;
pid_t svcPid = fork();
if (svcPid > 0) {
if (verboseLevel>2) {
cout << "Successfully forked child " << svcExec << " [" << svcName << "]"
<< " (PID " << svcPid << ")" << endl;
}
return svcPid;
} else if (svcPid < 0) {
cerr << "fork (" << svcExec << " [" << svcName << "] failed!" << endl;
return svcPid;
}
string cfn = dirName + "/" + xrdConfigName;
string log = dirName + "/" + svcExec + "." + svcName + ".log";
string env = dirName + "/" + svcExec + "." + svcName + ".env";
if (verboseLevel>1) {
cout << svcExec << " -d -n " << svcName << " -c " << cfn
<< " -l " << log << " -s " << env << endl;
}
// Config file, log files, and internal files all go to data directory
execlp(svcExec, svcExec, (verboseLevel?"-d":(const char*)0),
"-n", svcName, "-c", cfn.c_str(), "-l", log.c_str(), "-s", env.c_str(),
(const char*)0);
cerr << "FATAL ERROR in XrootdSimple: ";
perror("execlp");
return -1;
}
// Kill temporary XRootD services
void XrootdSimple::killServer() {
killService(xrdServerPid);
killService(cmsdServerPid);
}
void XrootdSimple::killManager() {
killService(xrdManagerPid);
killService(cmsdManagerPid);
}
void XrootdSimple::killService(pid_t& svcPid) {
if (svcPid <= 0) return; // Avoid unnecessary work
// FIXME: Expand error handling to identify what happened and why
if (kill(svcPid,SIGKILL) < 0) perror("kill");
if (waitpid(svcPid,0,0) < 0) perror ("waitpidd");
svcPid = 0;
}
// Remove flat files fom local file system (must do after killing services!)
void XrootdSimple::deleteTempFiles() {;}
// Create temporary directory if necessary
bool XrootdSimple::createTempDir() {
if (verboseLevel>1) cout << "XrootdSimple::createTempDir " << dirName << endl;
// Create directory, returns EEXIST if already present
if (mkdir(dirName.c_str(), 0755)==0 || errno==EEXIST) return true;
cerr << "Error creating temp directory: ";
perror("mkdir");
return false;
}
// Generate full filename for given file (block) index
// Format is <temp-directory>/objectChunk_NNNNN.idx
string XrootdSimple::getTempFilename(size_t ifile) {
char fname[22];
snprintf(fname, sizeof(fname), "objectChunk_%05lu.idx", ifile);
return string(fname);
}