-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdFileCacheFile.cc
493 lines (385 loc) · 12.4 KB
/
XrdFileCacheFile.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
#include "XrdFileCacheFile.hh"
using namespace XrdFileCache;
File::File(XrdOucCacheIO &io, std::string &path, long long off, long long size) :
m_input(io),
m_output(0),
m_infoFile(0),
m_temp_filename(path),
m_offset(off),
m_fileSize(size),
m_block_cond(0),
m_num_reads(0)
{
clLog()->Debug(XrdCl::AppMsg, "File::File() %s", m_input.Path());
}
File::~File()
{
// see if we have to shut down
clLog()->Info(XrdCl::AppMsg, "File::~File() %p %s", (void*) this, m_input.Path());
clLog()->Info(XrdCl::AppMsg, "File::~File close data file %p",(void*)this );
if (m_output)
{
m_output->Close();
delete m_output;
m_output = 0;
}
if (m_infoFile)
{
m_cfi.AppendIOStat(&m_stats, m_infoFile);
m_cfi.WriteHeader(m_infoFile);
clLog()->Info(XrdCl::AppMsg, "File::~File close info file");
m_infoFile->Close();
delete m_infoFile;
m_infoFile = 0;
}
}
//==============================================================================
bool File::Open()
{
// clLog()->Debug(XrdCl::AppMsg, "File::Open() open file for disk cache %s", m_input.Path());
XrdOss &m_output_fs = *Factory::GetInstance().GetOss();
// Create the data file itself.
XrdOucEnv myEnv;
m_output_fs.Create(Factory::GetInstance().RefConfiguration().m_username.c_str(), m_temp_filename.c_str(), 0600, myEnv, XRDOSS_mkpath);
m_output = m_output_fs.newFile(Factory::GetInstance().RefConfiguration().m_username.c_str());
if (m_output)
{
int res = m_output->Open(m_temp_filename.c_str(), O_RDWR, 0600, myEnv);
if (res < 0)
{
clLog()->Error(XrdCl::AppMsg, "File::Open() can't get data-FD for %s %s", m_temp_filename.c_str(), m_input.Path());
delete m_output;
m_output = 0;
return false;
}
}
else
{
clLog()->Error(XrdCl::AppMsg, "File::Open() can't get data holder ");
return false;
}
// Create the info file
std::string ifn = m_temp_filename + Info::m_infoExtension;
m_output_fs.Create(Factory::GetInstance().RefConfiguration().m_username.c_str(), ifn.c_str(), 0600, myEnv, XRDOSS_mkpath);
m_infoFile = m_output_fs.newFile(Factory::GetInstance().RefConfiguration().m_username.c_str());
if (m_infoFile)
{
int res = m_infoFile->Open(ifn.c_str(), O_RDWR, 0600, myEnv);
if (res < 0)
{
clLog()->Error(XrdCl::AppMsg, "File::Open() can't get info-FD %s %s", ifn.c_str(), m_input.Path());
delete m_infoFile;
m_infoFile = 0;
return false;
}
}
else
{
return false;
}
if (m_cfi.Read(m_infoFile) <= 0)
{
int ss = (m_fileSize - 1)/m_cfi.GetBufferSize() + 1;
clLog()->Info(XrdCl::AppMsg, "Creating new file info with size %lld. Reserve space for %d blocks %s", m_fileSize, ss, m_input.Path());
m_cfi.ResizeBits(ss);
RecordDownloadInfo();
}
else
{
clLog()->Debug(XrdCl::AppMsg, "Info file read from disk: %s", m_input.Path());
}
return true;
}
//==============================================================================
// Read and helpers
//==============================================================================
namespace
{
bool overlap(int blk, // block to query
long long blk_size, //
long long req_off, // offset of user request
int req_size, // size of user request
// output:
long_long &off, // offset in user buffer
long long &blk_off, // offset in block
long long &size) // size to copy
{
const long long beg = blk * blk_size;
const long long end = beg + blk_size;
const long long req_end = req_off + req_size;
if (req_off < end && req_end > beg)
{
const long long ovlp_beg = std::max(beg, req_off);
const long long ovlp_end = std::min(end, req_end);
off = ovlp_beg - req_off;
blk_off = ovlp_beg - beg;
size = ovlp_end - ovlp_beg;
return true;
}
else
{
return false;
}
}
}
//------------------------------------------------------------------------------
Block* File::RequestBlock(int i)
{
// Must be called w/ block_map locked.
// Checks on size etc should be done before.
//
// Reference count is 0 so increase it in calling function if you want to
// catch the block while still in memory.
XrdCl::XrdClFile &client = ((XrdPosixFile*)m_input).clFile;
const long long BS = m_cfi.GetBufferSize();
const int last_block = m_cfi.GetSizeInBits() - 1;
long long off = i * BS;
long long this_bs = (i == last_block) ? m_input.FSize() - off : BS;
Block *b = new Block(this, off, this_bs);
m_block_map[i] = b;
client.Read(off, this_bs, b->get_buff(), new BlockRH(b));
}
//------------------------------------------------------------------------------
int File::RequestBlocksDirect(DirectRH *handler, std::list<int>& blocks,
long long req_buf, long long req_off, long long req_size)
{
XrdCl::XrdClFile &client = ((XrdPosixFile*)m_input).clFile;
const long long BS = m_cfi.GetBufferSize();
// XXX Use readv to load more at the same time.
long long total = 0;
for (IntList_i ii = blocks ; ii != blocks.end(); ++ii)
{
// overlap and request
long_long off; // offset in user buffer
long long blk_off; // offset in block
long long size; // size to copy
overlap(*ii, BS, req_off, req_size, off, blk_off, size);
client.Read(*ii * BS + blk_off, size, req_buf + off, handler);
total += size;
}
return total;
}
//------------------------------------------------------------------------------
int File::ReadBlocksFromDisk(std::list<int>& blocks,
long long req_buf, long long req_off, long long req_size)
{
const long long BS = m_cfi.GetBufferSize();
long long total = 0;
// XXX Coalesce adjacent reads.
for (IntList_i ii = blocks ; ii != blocks.end(); ++ii)
{
// overlap and read
long_long off; // offset in user buffer
long long blk_off; // offset in block
long long size; // size to copy
overlap(*ii, BS, req_off, req_size, off, blk_off, size);
long long rs = m_output.Read(req_buf + off, *ii * BS + blk_off, size);
if (rs < 0)
return rs;
total += rs;
}
return total;
}
//------------------------------------------------------------------------------
int File::Read(char* buff, long long offset, int size);
{
XrdCl::XrdClFile &client = ((XrdPosixFile*)m_input).clFile;
const long long BS = m_cfi.GetBufferSize();
// lock
// loop over reqired blocks:
// - if on disk, ok;
// - if in ram or incoming, inc ref-count
// - if not available, request and inc ref count
// before requesting the hell and more (esp. for sparse readv) assess if
// passing the req to client is actually better.
// unlock
const int MaxBlocksForRead = 16; // Should be config var! Or even mutable for low client counts.
m_block_cond.Lock();
// XXX Check for blocks to free? Later ...
inc_num_readers();
const int idx_first = off / BS;
const int idx_last = (off + size - 1) / BS;
BlockList_t blks_to_process, blks_processed;
IntList_t blks_on_disk, blks_direct;
int bmap_size = m_block_map.size();
for (int i = idx_first; blockIdx <= idx_last; ++blockIdx)
{
BlockMap_i bi = m_block_map.find(i);
// In RAM or incoming?
if (bi != m_block_map.end())
{
// XXXX if failed before -- retry if timestamp sufficient or fail?
// XXXX Or just push it and handle errors in one place later?
(*bi)->inc_ref_cont();
blks_to_process.push_front(*bi);
}
// On disk?
else if (m_cfi.TestBit(i))
{
blks_on_disk.push_back(i);
}
// Then we have to get it ...
else
{
// Is there room for one more RAM Block?
if (size < MaxBlocksForRead)
{
Block *b = RequestBlock(i);
b->inc_ref_cont();
blks_to_process.push_back(b);
++size;
}
// Nope ... read this directly without caching.
else
{
blks_direct.push_back(i);
}
}
}
m_block_cond.UnLock();
// First, send out any direct requests.
// XXX Could send them all out in a single vector read.
DirectRH *direct_handler = 0;
int direct_size = 0;
if ( ! blks_direct.empty())
{
direct_handler = new DirectRH(blks_direct.size());
direct_size = RequestBlocksDirect(direct_handler, blks_direct, *ii, buff, offset, size);
}
long long bytes_read = 0;
// Second, read blocks from disk.
int rc = ReadBlocksFromDisk(blks_on_disk, buff, offset, size);
if (rc >= 0)
{
bytes_read += rc;
}
else
{
bytes_read = rc;
}
// Third, loop over blocks that are available or incoming
while ( ! blks_to_process.empty() && bytes_read >= 0)
{
BlockList_t finished;
{
XrdSysConVarHelper _lck(m_block_cond);
BlockList_i bi = blks_to_process.begin();
while (bi != blks_to_process.end())
{
if ((*bi)->is_finished())
{
finished.push_back(*bi);
BlockList_i bj = bi++;
blks_to_process.erase(bj);
}
else
{
++bi;
}
}
if (finished.empty())
{
m_block_cond.Wait();
continue;
}
}
bi = finished.begin();
while (bi != finished.end())
{
if ((*bi)->is_ok())
{
// XXXX memcpy !
}
else // it has failed ... krap up.
{
bytes_read = -1;
errno = (*bi)->errno;
break;
}
}
std::copy(finished.begin(), finished.end(), std::back_inserter(blks_processed));
}
// Fourth, make sure all direct requests have arrived
if (m_direct_handler != 0)
{
XrdSysCondVarHelper _lck(direct_handler->m_cond);
if (direct_handler->m_to_wait > 0)
{
direct_handler->m_cond.Wait();
}
if (direct_handler->m_errno == 0)
{
bytes_read += direct_size;
}
else
{
errno = direct_handler->m_errno;
bytes_read = -1;
}
delete m_direct_handler; m_direct_handler = 0;
}
// Last, stamp and release blocks, release file.
{
XrdSysConVarHelper _lck(m_block_cond);
// XXXX stamp file
// blks_to_process can be non-empty, if we're exiting with an error.
std::copy(blks_to_process.begin(), blks_to_process.end(), std::back_inserter(blks_processed));
for (BlockList_i bi = blks_processed.begin(); bi != blks_processed.end(); ++bi)
{
(*bi)->dec_ref_count();
// XXXX stamp block
}
dec_num_readers();
}
return bytes_read;
}
//------------------------------------------------------------------------------
void File::ProcessBlockResponse(Block* b, XrdCl::XRootDStatus *status)
{
m_block_cond.Lock();
if (status->IsOK())
{
b->m_downloaded = true;
b->inc_ref_count();
// XXX Add to write queue (if needed)
// write_queue->QueueBlock(b);
}
else
{
// how long to keep?
// when to retry?
XrdPosixMap::Result(*status);
b->set_error_and_free(errno);
errno = 0;
// ???
b->inc_ref_count();
}
m_block_cond.Broadcast();
m_block_cond.UnLock();
}
//==============================================================================
//==============================================================================
void BlockResponseHandler::HandleResponse(XrdCl::XRootDStatus *status,
XrdCl::AnyObject *response)
{
m_block->m_file->ProcessBlockResponse(m_block, status);
delete status;
delete response;
delete this;
}
//------------------------------------------------------------------------------
void DirectResponseHandler::HandleResponse(XrdCl::XRootDStatus *status,
XrdCl::AnyObject *response)
{
XrdSysCondVarHelper _lck(m_cond);
--m_to_wait;
if ( ! status->IsOK())
{
XrdPosixMap::Result(*status);
m_errno = errno;
}
if (m_to_wait == 0)
{
m_cond.Signal();
}
}