/
XrdPfcCommand.cc
385 lines (302 loc) · 12.5 KB
/
XrdPfcCommand.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
//----------------------------------------------------------------------------------
// Copyright (c) 2014 by Board of Trustees of the Leland Stanford, Jr., University
// Author: Alja Mrak-Tadel
//----------------------------------------------------------------------------------
// 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 General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with XRootD. If not, see <http://www.gnu.org/licenses/>.
//----------------------------------------------------------------------------------
#include "XrdPfcInfo.hh"
#include "XrdPfc.hh"
#include "XrdPfcTrace.hh"
#include "XrdOfs/XrdOfsConfigPI.hh"
#include "XrdOss/XrdOss.hh"
#include "XrdOuc/XrdOuca2x.hh"
#include "XrdOuc/XrdOucArgs.hh"
#include "XrdOuc/XrdOucEnv.hh"
#include "XrdOuc/XrdOucStream.hh"
#include "XrdSys/XrdSysLogger.hh"
#include "XrdSys/XrdSysFallocate.hh"
#include <algorithm>
#include <cstring>
#include <iostream>
#include <fcntl.h>
#include <vector>
#include <sys/time.h>
using namespace XrdPfc;
//______________________________________________________________________________
const int MAX_ACCESSES = 20;
const long long ONE_MB = 1024ll * 1024;
const long long ONE_GB = 1024ll * 1024 * 1024;
void Cache::ExecuteCommandUrl(const std::string& command_url)
{
static const char *top_epfx = "ExecuteCommandUrl ";
SplitParser cp(command_url, "/");
std::string token = cp.get_token();
if (token != "xrdpfc_command")
{
TRACE(Error, top_epfx << "First token is NOT xrdpfc_command.");
return;
}
// Get the command
token = cp.get_token();
//================================================================
// create_file
//================================================================
if (token == "create_file")
{
static const char* err_prefix = "ExecuteCommandUrl: /xrdpfc_command/create_file: ";
static const char* usage =
"Usage: create_file/ [-h] [-s filesize] [-b blocksize] [-t access_time] [-d access_duration]/<path>\n"
" Creates a cache file with given parameters. Data in file is random.\n"
" Useful for cache purge testing.\n"
"Notes:\n"
" . If no options are needed one should still leave a space between / separators, ie., '/ /'\n"
" . Default filesize=1G, blocksize=<as configured>, access_time=-10, access_duration=10.\n"
" . -t and -d can be given multiple times to record several accesses.\n"
" . Negative arguments given to -t are interpreted as relative to now.\n";
const Configuration &conf = m_configuration;
token = cp.get_token();
TRACE(Debug, err_prefix << "Entered with argument string '" << token <<"'.");
std::vector<char*> argv;
SplitParser ap(token, " ");
int argc = ap.fill_argv(argv);
long long file_size = ONE_GB;
long long block_size = conf.m_bufferSize;
int access_time [MAX_ACCESSES];
int access_duration[MAX_ACCESSES];
int at_count = 0, ad_count = 0;
XrdOucArgs Spec(&m_log, err_prefix, "hvs:b:t:d:",
"help", 1, "h",
"verbose", 1, "v",
"size", 1, "s",
"blocksize", 1, "b",
"time", 1, "t",
"duration", 1, "d",
(const char *) 0);
time_t time_now = time(0);
Spec.Set(argc, &argv[0]);
char theOpt;
while ((theOpt = Spec.getopt()) != (char) -1)
{
switch (theOpt)
{
case 'h': {
m_log.Say(err_prefix, " -- printing help, no action will be taken\n", usage);
return;
}
case 's': {
if (XrdOuca2x::a2sz(m_log, "Error getting filesize", Spec.getarg(),
&file_size, 0ll, 32 * ONE_GB))
return;
break;
}
case 'b': {
if (XrdOuca2x::a2sz(m_log, "Error getting blocksize", Spec.getarg(),
&block_size, 0ll, 64 * ONE_MB))
return;
break;
}
case 't': {
if (XrdOuca2x::a2i(m_log, "Error getting access time", Spec.getarg(),
&access_time[at_count++], INT_MIN, INT_MAX))
return;
break;
}
case 'd': {
if (XrdOuca2x::a2i(m_log, "Error getting access duration", Spec.getarg(),
&access_duration[ad_count++], 0, 24 * 3600))
return;
break;
}
default: {
TRACE(Error, err_prefix << "Unhandled command argument.");
return;
}
}
}
if (Spec.getarg())
{
TRACE(Error, err_prefix << "Options must take up all the arguments.");
return;
}
if (at_count < 1) access_time [at_count++] = time_now - 10;
if (ad_count < 1) access_duration[ad_count++] = 10;
if (at_count != ad_count)
{
TRACE(Error, err_prefix << "Options -t and -d must be given the same number of times.");
return;
}
std::string file_path (cp.get_reminder_with_delim());
std::string cinfo_path(file_path + Info::s_infoExtension);
TRACE(Debug, err_prefix << "Command arguments parsed successfully. Proceeding to create file " << file_path);
// Check if cinfo exists ... bail out if it does.
{
struct stat infoStat;
if (GetOss()->Stat(cinfo_path.c_str(), &infoStat) == XrdOssOK)
{
TRACE(Error, err_prefix << "cinfo file already exists for '" << file_path << "'. Refusing to overwrite.");
return;
}
}
TRACE(Debug, err_prefix << "Command arguments parsed successfully, proceeding to execution.");
{
const char *myUser = conf.m_username.c_str();
XrdOucEnv myEnv;
// Create the data file.
char size_str[32]; sprintf(size_str, "%lld", file_size);
myEnv.Put("oss.asize", size_str);
myEnv.Put("oss.cgroup", conf.m_data_space.c_str());
int cret;
if ((cret = GetOss()->Create(myUser, file_path.c_str(), 0600, myEnv, XRDOSS_mkpath)) != XrdOssOK)
{
TRACE(Error, err_prefix << "Create failed for data file " << file_path << ", " << ERRNO_AND_ERRSTR(-cret));
return;
}
XrdOssDF *myFile = GetOss()->newFile(myUser);
if ((cret = myFile->Open(file_path.c_str(), O_RDWR, 0600, myEnv)) != XrdOssOK)
{
TRACE(Error, err_prefix << "Open failed for data file " << file_path << ", " << ERRNO_AND_ERRSTR(-cret));
delete myFile;
return;
}
// Create the info file.
myEnv.Put("oss.asize", "64k"); // TODO: Calculate? Get it from configuration? Do not know length of access lists ...
myEnv.Put("oss.cgroup", conf.m_meta_space.c_str());
if ((cret = GetOss()->Create(myUser, cinfo_path.c_str(), 0600, myEnv, XRDOSS_mkpath)) != XrdOssOK)
{
TRACE(Error, err_prefix << "Create failed for info file " << cinfo_path << ", " << ERRNO_AND_ERRSTR(-cret));
myFile->Close(); delete myFile;
return;
}
XrdOssDF *myInfoFile = GetOss()->newFile(myUser);
if ((cret = myInfoFile->Open(cinfo_path.c_str(), O_RDWR, 0600, myEnv)) != XrdOssOK)
{
TRACE(Error, err_prefix << "Open failed for info file " << cinfo_path << ", " << ERRNO_AND_ERRSTR(-cret));
delete myInfoFile;
myFile->Close(); delete myFile;
return;
}
// Allocate space for the data file.
if ((cret = posix_fallocate(myFile->getFD(), 0, file_size)))
{
TRACE(Error, err_prefix << "posix_fallocate failed for data file " << file_path << ", " << ERRNO_AND_ERRSTR(cret));
}
// Fill up cinfo.
Info myInfo(m_trace, false);
myInfo.SetBufferSize(block_size);
myInfo.SetFileSizeAndCreationTime(file_size);
myInfo.SetAllBitsSynced();
for (int i = 0; i < at_count; ++i)
{
time_t att_time = access_time[i] >= 0 ? access_time[i] : time_now + access_time[i];
myInfo.WriteIOStatSingle(file_size, att_time, att_time + access_duration[i]);
}
myInfo.Write(myInfoFile, cinfo_path.c_str());
myInfoFile->Close(); delete myInfoFile;
myFile->Close(); delete myFile;
TRACE(Info, err_prefix << "Created file '" << file_path << "', size=" << (file_size>>20) << "MB.");
{
XrdSysCondVarHelper lock(&m_writeQ.condVar);
m_writeQ.writes_between_purges += file_size;
}
}
}
//================================================================
// remove_file
//================================================================
else if (token == "remove_file")
{
static const char* err_prefix = "ExecuteCommandUrl: /xrdpfc_command/remove_file: ";
static const char* usage =
"Usage: remove_file/ [-h] /<path>\n"
" Removes given file from the cache unless it is currently open.\n"
" Useful for removal of stale files or duplicate files in a caching cluster.\n"
"Notes:\n"
" . If no options are needed one should still leave a space between / separators, ie., '/ /'\n";
token = cp.get_token();
TRACE(Debug, err_prefix << "Entered with argument string '" << token <<"'.");
std::vector<char*> argv;
SplitParser ap(token, " ");
int argc = ap.fill_argv(argv);
XrdOucArgs Spec(&m_log, err_prefix, "hvs:b:t:d:",
"help", 1, "h",
(const char *) 0);
Spec.Set(argc, &argv[0]);
char theOpt;
while ((theOpt = Spec.getopt()) != (char) -1)
{
switch (theOpt)
{
case 'h': {
m_log.Say(err_prefix, " -- printing help, no action will be taken\n", usage);
return;
}
default: {
TRACE(Error, err_prefix << "Unhandled command argument.");
return;
}
}
}
if (Spec.getarg())
{
TRACE(Error, err_prefix << "Options must take up all the arguments.");
return;
}
std::string f_name(cp.get_reminder());
TRACE(Debug, err_prefix << "file argument '" << f_name << "'.");
int ret = UnlinkFile(f_name, true);
TRACE(Info, err_prefix << "returned with status " << ret);
}
//================================================================
// unknown command
//================================================================
else
{
TRACE(Error, top_epfx << "Unknown or empty command '" << token << "'");
}
}
//==============================================================================
// Example python script to use /xrdpfc_command/
//==============================================================================
/*
from XRootD import client
from XRootD.client.flags import OpenFlags
import sys
import time
#-------------------------------------------------------------------------------
port = int( sys.argv[1] );
g_srv = "root://localhost:%d/" % port
g_com = "/xrdpfc_command/create_file/"
g_dir = "/store/user/matevz/"
#-------------------------------------------------------------------------------
def xxsend(args, file) :
url = g_srv + g_com + args + g_dir + file
print "Opening ", url
with client.File() as f:
status, response = f.open(url, OpenFlags.READ)
print '%r' % status
print '%r' % response
#-------------------------------------------------------------------------------
pfx1 = "AAAA"
pfx2 = "BBBB"
for i in range(1, 1024 + 1):
atime = -10000 + i
xxsend("-s 4g -t %d -d 10" % atime,
"%s-%04d" % (pfx1, i))
time.sleep(0.01)
for i in range(1, 512 + 1):
atime = -5000 + i
xxsend("-s 4g -t %d -d 10" % atime,
"%s-%04d" % (pfx2, i))
time.sleep(0.01)
*/