-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdPfc.cc
1071 lines (867 loc) · 28.5 KB
/
XrdPfc.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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//----------------------------------------------------------------------------------
// Copyright (c) 2014 by Board of Trustees of the Leland Stanford, Jr., University
// Author: Alja Mrak-Tadel, Matevz Tadel, Brian Bockelman
//----------------------------------------------------------------------------------
// 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 <fcntl.h>
#include <sstream>
#include <algorithm>
#include <sys/statvfs.h>
#include "XrdCl/XrdClConstants.hh"
#include "XrdCl/XrdClURL.hh"
#include "XrdOuc/XrdOucEnv.hh"
#include "XrdOuc/XrdOucUtils.hh"
#include "XrdSys/XrdSysPthread.hh"
#include "XrdSys/XrdSysTimer.hh"
#include "XrdSys/XrdSysTrace.hh"
#include "XrdXrootd/XrdXrootdGStream.hh"
#include "XrdOss/XrdOss.hh"
#include "XrdPfc.hh"
#include "XrdPfcTrace.hh"
#include "XrdPfcInfo.hh"
#include "XrdPfcIOEntireFile.hh"
#include "XrdPfcIOFileBlock.hh"
using namespace XrdPfc;
Cache * Cache::m_instance = 0;
XrdScheduler *Cache::schedP = 0;
void *ResourceMonitorHeartBeatThread(void*)
{
Cache::GetInstance().ResourceMonitorHeartBeat();
return 0;
}
void *PurgeThread(void*)
{
Cache::GetInstance().Purge();
return 0;
}
void *ProcessWriteTaskThread(void*)
{
Cache::GetInstance().ProcessWriteTasks();
return 0;
}
void *PrefetchThread(void*)
{
Cache::GetInstance().Prefetch();
return 0;
}
//==============================================================================
extern "C"
{
XrdOucCache *XrdOucGetCache(XrdSysLogger *logger,
const char *config_filename,
const char *parameters,
XrdOucEnv *env)
{
XrdSysError err(logger, "");
err.Say("++++++ Proxy file cache initialization started.");
if ( ! env ||
! (XrdPfc::Cache::schedP = (XrdScheduler*) env->GetPtr("XrdScheduler*")))
{
XrdPfc::Cache::schedP = new XrdScheduler;
XrdPfc::Cache::schedP->Start();
}
Cache &instance = Cache::CreateInstance(logger, env);
if (! instance.Config(config_filename, parameters))
{
err.Say("Config Proxy file cache initialization failed.");
return 0;
}
err.Say("------ Proxy file cache initialization completed.");
{
pthread_t tid;
for (int wti = 0; wti < instance.RefConfiguration().m_wqueue_threads; ++wti)
{
XrdSysThread::Run(&tid, ProcessWriteTaskThread, 0, 0, "XrdPfc WriteTasks ");
}
if (instance.RefConfiguration().m_prefetch_max_blocks > 0)
{
XrdSysThread::Run(&tid, PrefetchThread, 0, 0, "XrdPfc Prefetch ");
}
XrdSysThread::Run(&tid, ResourceMonitorHeartBeatThread, 0, 0, "XrdPfc ResourceMonitorHeartBeat");
XrdSysThread::Run(&tid, PurgeThread, 0, 0, "XrdPfc Purge");
}
return &instance;
}
}
//==============================================================================
void Configuration::calculate_fractional_usages(long long du, long long fu,
double &frac_du, double &frac_fu)
{
// Calculate fractional disk / file usage and clamp them to [0, 1].
// Fractional total usage above LWM:
// - can be > 1 if usage is above HWM;
// - can be < 0 if triggered via age-based-purging.
frac_du = (double) (du - m_diskUsageLWM) / (m_diskUsageHWM - m_diskUsageLWM);
// Fractional file usage above baseline.
// - can be > 1 if file usage is above max;
// - can be < 0 if file usage is below baseline.
frac_fu = (double) (fu - m_fileUsageBaseline) / (m_fileUsageMax - m_fileUsageBaseline);
frac_du = std::min( std::max( frac_du, 0.0), 1.0 );
frac_fu = std::min( std::max( frac_fu, 0.0), 1.0 );
}
//==============================================================================
Cache &Cache::CreateInstance(XrdSysLogger *logger, XrdOucEnv *env)
{
assert (m_instance == 0);
m_instance = new Cache(logger, env);
return *m_instance;
}
Cache& Cache::GetInstance() { return *m_instance; }
const Cache& Cache::TheOne() { return *m_instance; }
const Configuration& Cache::Conf() { return m_instance->RefConfiguration(); }
bool Cache::Decide(XrdOucCacheIO* io)
{
if (! m_decisionpoints.empty())
{
XrdCl::URL url(io->Path());
std::string filename = url.GetPath();
std::vector<Decision*>::const_iterator it;
for (it = m_decisionpoints.begin(); it != m_decisionpoints.end(); ++it)
{
XrdPfc::Decision *d = *it;
if (! d) continue;
if (! d->Decide(filename, *m_oss))
{
return false;
}
}
}
return true;
}
Cache::Cache(XrdSysLogger *logger, XrdOucEnv *env) :
XrdOucCache("pfc"),
m_env(env),
m_log(logger, "XrdPfc_"),
m_trace(new XrdSysTrace("XrdPfc", logger)),
m_traceID("Cache"),
m_oss(0),
m_gstream(0),
m_prefetch_condVar(0),
m_prefetch_enabled(false),
m_RAM_used(0),
m_RAM_write_queue(0),
m_RAM_std_size(0),
m_isClient(false),
m_in_purge(false),
m_active_cond(0),
m_stats_n_purge_cond(0),
m_fs_state(0),
m_last_scan_duration(0),
m_last_purge_duration(0),
m_spt_state(SPTS_Idle)
{
// Default log level is Warning.
m_trace->What = 2;
}
XrdOucCacheIO *Cache::Attach(XrdOucCacheIO *io, int Options)
{
const char* tpfx = "Attach() ";
if (Cache::GetInstance().Decide(io))
{
TRACE(Info, tpfx << io->Path());
IO *cio;
if (Cache::GetInstance().RefConfiguration().m_hdfsmode)
{
cio = new IOFileBlock(io, *this);
}
else
{
IOEntireFile *ioef = new IOEntireFile(io, *this);
if ( ! ioef->HasFile())
{
delete ioef;
// TODO - redirect instead. But this is kind of an awkward place for it.
// errno is set during IOEntireFile construction.
TRACE(Error, tpfx << "Failed opening local file, falling back to remote access " << io->Path());
return io;
}
cio = ioef;
}
TRACE_PC(Debug, const char* loc = io->Location(), tpfx << io->Path() << " location: " <<
((loc && loc[0] != 0) ? loc : "<deferred open>"));
return cio;
}
else
{
TRACE(Info, tpfx << "decision decline " << io->Path());
}
return io;
}
void Cache::AddWriteTask(Block* b, bool fromRead)
{
TRACE(Dump, "AddWriteTask() bOff=" << b->m_offset);
{
XrdSysMutexHelper lock(&m_RAM_mutex);
m_RAM_write_queue += b->get_size();
}
m_writeQ.condVar.Lock();
if (fromRead)
m_writeQ.queue.push_back(b);
else
m_writeQ.queue.push_front(b);
m_writeQ.size++;
m_writeQ.condVar.Signal();
m_writeQ.condVar.UnLock();
}
void Cache::RemoveWriteQEntriesFor(File *file)
{
std::list<Block*> removed_blocks;
long long sum_size = 0;
m_writeQ.condVar.Lock();
std::list<Block*>::iterator i = m_writeQ.queue.begin();
while (i != m_writeQ.queue.end())
{
if ((*i)->m_file == file)
{
TRACE(Dump, "Remove entries for " << (void*)(*i) << " path " << file->lPath());
std::list<Block*>::iterator j = i++;
removed_blocks.push_back(*j);
sum_size += (*j)->get_size();
m_writeQ.queue.erase(j);
--m_writeQ.size;
}
else
{
++i;
}
}
m_writeQ.condVar.UnLock();
{
XrdSysMutexHelper lock(&m_RAM_mutex);
m_RAM_write_queue -= sum_size;
}
file->BlocksRemovedFromWriteQ(removed_blocks);
}
void Cache::ProcessWriteTasks()
{
std::vector<Block*> blks_to_write(m_configuration.m_wqueue_blocks);
while (true)
{
m_writeQ.condVar.Lock();
while (m_writeQ.size == 0)
{
m_writeQ.condVar.Wait();
}
// MT -- optimize to pop several blocks if they are available (or swap the list).
// This makes sense especially for smallish block sizes.
int n_pushed = std::min(m_writeQ.size, m_configuration.m_wqueue_blocks);
long long sum_size = 0;
for (int bi = 0; bi < n_pushed; ++bi)
{
Block* block = m_writeQ.queue.front();
m_writeQ.queue.pop_front();
m_writeQ.writes_between_purges += block->get_size();
sum_size += block->get_size();
blks_to_write[bi] = block;
TRACE(Dump, "ProcessWriteTasks for block " << (void*)(block) << " path " << block->m_file->lPath());
}
m_writeQ.size -= n_pushed;
m_writeQ.condVar.UnLock();
{
XrdSysMutexHelper lock(&m_RAM_mutex);
m_RAM_write_queue -= sum_size;
}
for (int bi = 0; bi < n_pushed; ++bi)
{
Block* block = blks_to_write[bi];
block->m_file->WriteBlockToDisk(block);
}
}
}
//==============================================================================
char* Cache::RequestRAM(long long size)
{
static const size_t s_block_align = sysconf(_SC_PAGESIZE);
bool std_size = (size == m_configuration.m_bufferSize);
m_RAM_mutex.Lock();
long long total = m_RAM_used + size;
if (total <= m_configuration.m_RamAbsAvailable)
{
m_RAM_used = total;
if (std_size && m_RAM_std_size > 0)
{
char *buf = m_RAM_std_blocks.back();
m_RAM_std_blocks.pop_back();
--m_RAM_std_size;
m_RAM_mutex.UnLock();
return buf;
}
else
{
m_RAM_mutex.UnLock();
char *buf;
if (posix_memalign((void**) &buf, s_block_align, (size_t) size))
{
// Report out of mem? Probably should report it at least the first time,
// then periodically.
return 0;
}
return buf;
}
}
m_RAM_mutex.UnLock();
return 0;
}
void Cache::ReleaseRAM(char* buf, long long size)
{
bool std_size = (size == m_configuration.m_bufferSize);
{
XrdSysMutexHelper lock(&m_RAM_mutex);
m_RAM_used -= size;
if (std_size && m_RAM_std_size < m_configuration.m_RamKeepStdBlocks)
{
m_RAM_std_blocks.push_back(buf);
++m_RAM_std_size;
return;
}
}
free(buf);
}
File* Cache::GetFile(const std::string& path, IO* io, long long off, long long filesize)
{
// Called from virtual IO::Attach
TRACE(Debug, "GetFile " << path << ", io " << io);
ActiveMap_i it;
{
XrdSysCondVarHelper lock(&m_active_cond);
while (true)
{
it = m_active.find(path);
// File is not open or being opened. Mark it as being opened and
// proceed to opening it outside of while loop.
if (it == m_active.end())
{
it = m_active.insert(std::make_pair(path, (File*) 0)).first;
break;
}
if (it->second != 0)
{
it->second->AddIO(io);
inc_ref_cnt(it->second, false, true);
return it->second;
}
else
{
// Wait for some change in m_active, then recheck.
m_active_cond.Wait();
}
}
}
if (filesize == 0)
{
struct stat st;
int res = io->Fstat(st);
if (res < 0) {
errno = res;
TRACE(Error, "GetFile, could not get valid stat");
} else if (res > 0) {
errno = ENOTSUP;
TRACE(Error, "GetFile, stat returned positive value, this should NOT happen here");
} else {
filesize = st.st_size;
}
}
File *file = 0;
if (filesize >= 0)
{
file = File::FileOpen(path, off, filesize);
}
{
XrdSysCondVarHelper lock(&m_active_cond);
if (file)
{
inc_ref_cnt(file, false, true);
it->second = file;
file->AddIO(io);
}
else
{
m_active.erase(it);
}
m_active_cond.Broadcast();
}
return file;
}
void Cache::ReleaseFile(File* f, IO* io)
{
// Called from virtual IO::DetachFinalize.
TRACE(Debug, "ReleaseFile " << f->GetLocalPath() << ", io " << io);
{
XrdSysCondVarHelper lock(&m_active_cond);
f->RemoveIO(io);
}
dec_ref_cnt(f, true);
}
namespace
{
class DiskSyncer : public XrdJob
{
private:
File *m_file;
bool m_high_debug;
public:
DiskSyncer(File *f, bool high_debug, const char *desc = "") :
XrdJob(desc),
m_file(f),
m_high_debug(high_debug)
{}
void DoIt()
{
m_file->Sync();
Cache::GetInstance().FileSyncDone(m_file, m_high_debug);
delete this;
}
};
class CommandExecutor : public XrdJob
{
private:
std::string m_command_url;
public:
CommandExecutor(const std::string& command, const char *desc = "") :
XrdJob(desc),
m_command_url(command)
{}
void DoIt()
{
Cache::GetInstance().ExecuteCommandUrl(m_command_url);
delete this;
}
};
}
//==============================================================================
void Cache::schedule_file_sync(File* f, bool ref_cnt_already_set, bool high_debug)
{
DiskSyncer* ds = new DiskSyncer(f, high_debug);
if ( ! ref_cnt_already_set) inc_ref_cnt(f, true, high_debug);
schedP->Schedule(ds);
}
void Cache::FileSyncDone(File* f, bool high_debug)
{
dec_ref_cnt(f, high_debug);
}
void Cache::inc_ref_cnt(File* f, bool lock, bool high_debug)
{
// called from GetFile() or SheduleFileSync();
int tlvl = high_debug ? TRACE_Debug : TRACE_Dump;
if (lock) m_active_cond.Lock();
int rc = f->inc_ref_cnt();
if (lock) m_active_cond.UnLock();
TRACE_INT(tlvl, "inc_ref_cnt " << f->GetLocalPath() << ", cnt at exit = " << rc);
}
void Cache::dec_ref_cnt(File* f, bool high_debug)
{
// Called from ReleaseFile() or DiskSync callback.
int tlvl = high_debug ? TRACE_Debug : TRACE_Dump;
int cnt;
{
XrdSysCondVarHelper lock(&m_active_cond);
cnt = f->get_ref_cnt();
if (f->is_in_emergency_shutdown())
{
// In this case file has been already removed from m_active map and
// does not need to be synced.
if (cnt == 1)
{
TRACE_INT(tlvl, "dec_ref_cnt " << f->GetLocalPath() << " is in shutdown, ref_cnt = " << cnt
<< " -- deleting File object without further ado");
delete f;
}
else
{
TRACE_INT(tlvl, "dec_ref_cnt " << f->GetLocalPath() << " is in shutdown, ref_cnt = " << cnt
<< " -- waiting");
}
return;
}
}
TRACE_INT(tlvl, "dec_ref_cnt " << f->GetLocalPath() << ", cnt at entry = " << cnt);
if (cnt == 1)
{
if (f->FinalizeSyncBeforeExit())
{
// Note, here we "reuse" the existing reference count for the
// final sync.
TRACE(Debug, "dec_ref_cnt " << f->GetLocalPath() << ", scheduling final sync");
schedule_file_sync(f, true, true);
return;
}
}
{
XrdSysCondVarHelper lock(&m_active_cond);
cnt = f->dec_ref_cnt();
TRACE_INT(tlvl, "dec_ref_cnt " << f->GetLocalPath() << ", cnt after sync_check and dec_ref_cnt = " << cnt);
if (cnt == 0)
{
ActiveMap_i it = m_active.find(f->GetLocalPath());
m_active.erase(it);
m_closed_files_stats.insert(std::make_pair(f->GetLocalPath(), f->DeltaStatsFromLastCall()));
if (m_gstream)
{
const Info::AStat *as = f->GetLastAccessStats();
char buf[4096];
int len = snprintf(buf, 4096, "{\"event\":\"file_close\","
"\"lfn\":\"%s\",\"size\":%lld,\"blk_size\":%d,\"n_blks\":%d,\"n_blks_done\":%d,"
"\"access_cnt\":%lu,\"attach_t\":%lld,\"detach_t\":%lld,\"remotes\":%s,"
"\"b_hit\":%lld,\"b_miss\":%lld,\"b_bypass\":%lld}",
f->GetLocalPath().c_str(), f->GetFileSize(), f->GetBlockSize(),
f->GetNBlocks(), f->GetNDownloadedBlocks(),
(unsigned long) f->GetAccessCnt(), (long long) as->AttachTime, (long long) as->DetachTime,
f->GetRemoteLocations().c_str(),
as->BytesHit, as->BytesMissed, as->BytesBypassed
);
bool suc = false;
if (len < 4096)
{
suc = m_gstream->Insert(buf, len + 1);
}
if ( ! suc)
{
TRACE(Error, "Failed g-stream insertion of file_close record, len=" << len);
}
}
delete f;
}
}
}
bool Cache::IsFileActiveOrPurgeProtected(const std::string& path)
{
XrdSysCondVarHelper lock(&m_active_cond);
return m_active.find(path) != m_active.end() ||
m_purge_delay_set.find(path) != m_purge_delay_set.end();
}
//==============================================================================
//=== PREFETCH
//==============================================================================
void Cache::RegisterPrefetchFile(File* file)
{
// Can be called with other locks held.
if ( ! m_prefetch_enabled)
{
return;
}
m_prefetch_condVar.Lock();
m_prefetchList.push_back(file);
m_prefetch_condVar.Signal();
m_prefetch_condVar.UnLock();
}
void Cache::DeRegisterPrefetchFile(File* file)
{
// Can be called with other locks held.
if ( ! m_prefetch_enabled)
{
return;
}
m_prefetch_condVar.Lock();
for (PrefetchList::iterator it = m_prefetchList.begin(); it != m_prefetchList.end(); ++it)
{
if (*it == file)
{
m_prefetchList.erase(it);
break;
}
}
m_prefetch_condVar.UnLock();
}
File* Cache::GetNextFileToPrefetch()
{
m_prefetch_condVar.Lock();
while (m_prefetchList.empty())
{
m_prefetch_condVar.Wait();
}
// std::sort(m_prefetchList.begin(), m_prefetchList.end(), myobject);
size_t l = m_prefetchList.size();
int idx = rand() % l;
File* f = m_prefetchList[idx];
m_prefetch_condVar.UnLock();
return f;
}
void Cache::Prefetch()
{
const long long limit_RAM = m_configuration.m_RamAbsAvailable * 7 / 10;
while (true)
{
m_RAM_mutex.Lock();
bool doPrefetch = (m_RAM_used < limit_RAM);
m_RAM_mutex.UnLock();
if (doPrefetch)
{
File* f = GetNextFileToPrefetch();
f->Prefetch();
}
else
{
XrdSysTimer::Wait(5);
}
}
}
//==============================================================================
//=== Virtuals from XrdOucCache
//==============================================================================
//------------------------------------------------------------------------------
//! Get the path to a file that is complete in the local cache. By default, the
//! file must be complete in the cache (i.e. no blocks are missing). This can
//! be overridden. This path can be used to access the file on the local node.
//!
//! @return 0 - the file is complete and the local path to the file is in
//! the buffer, if it has been supllied.
//!
//! @return <0 - the request could not be fulfilled. The return value is
//! -errno describing why. If a buffer was supplied and a
//! path could be generated it is returned only if "why" is
//! ForCheck or ForInfo. Otherwise, a null path is returned.
//!
//! @return >0 - Reserved for future use.
int Cache::LocalFilePath(const char *curl, char *buff, int blen,
LFP_Reason why, bool forall)
{
static const mode_t groupReadable = S_IRUSR | S_IWUSR | S_IRGRP;
static const mode_t worldReadable = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
static const char *lfpReason[] = { "ForAccess", "ForInfo", "ForPath" };
TRACE(Debug, "LocalFilePath '" << curl << "', why=" << lfpReason[why]);
if (buff && blen > 0) buff[0] = 0;
XrdCl::URL url(curl);
std::string f_name = url.GetPath();
std::string i_name = f_name + Info::s_infoExtension;
if (why == ForPath)
{
int ret = m_oss->Lfn2Pfn(f_name.c_str(), buff, blen);
TRACE(Info, "LocalFilePath '" << curl << "', why=" << lfpReason[why] << " -> " << ret);
return ret;
}
{
XrdSysCondVarHelper lock(&m_active_cond);
m_purge_delay_set.insert(f_name);
}
struct stat sbuff, sbuff2;
if (m_oss->Stat(f_name.c_str(), &sbuff) == XrdOssOK &&
m_oss->Stat(i_name.c_str(), &sbuff2) == XrdOssOK)
{
if (S_ISDIR(sbuff.st_mode))
{
TRACE(Info, "LocalFilePath '" << curl << "', why=" << lfpReason[why] << " -> EISDIR");
return -EISDIR;
}
else
{
bool read_ok = false;
bool is_complete = false;
// Lock and check if the file is active. If NOT, keep the lock
// and add dummy access after successful reading of info file.
// If it IS active, just release the lock, this ongoing access will
// assure the file continues to exist.
// XXXX How can I just loop over the cinfo file when active?
// Can I not get is_complete from the existing file?
// Do I still want to inject access record?
// Oh, it writes only if not active .... still let's try to use existing File.
m_active_cond.Lock();
bool is_active = m_active.find(f_name) != m_active.end();
if (is_active) m_active_cond.UnLock();
XrdOssDF* infoFile = m_oss->newFile(m_configuration.m_username.c_str());
XrdOucEnv myEnv;
int res = infoFile->Open(i_name.c_str(), O_RDWR, 0600, myEnv);
if (res >= 0)
{
Info info(m_trace, 0);
if (info.Read(infoFile, i_name.c_str()))
{
read_ok = true;
is_complete = info.IsComplete();
// Add full-size access if reason is for access.
if ( ! is_active && is_complete && why == ForAccess)
{
info.WriteIOStatSingle(info.GetFileSize());
info.Write(infoFile, i_name.c_str());
}
}
infoFile->Close();
}
delete infoFile;
if ( ! is_active) m_active_cond.UnLock();
if (read_ok)
{
if ((is_complete || why == ForInfo) && buff != 0)
{
int res2 = m_oss->Lfn2Pfn(f_name.c_str(), buff, blen);
if (res2 < 0)
return res2;
// Normally, files are owned by us but when direct cache access
// is wanted and possible, make sure the file is world readable.
if (why == ForAccess)
{mode_t mode = (forall ? worldReadable : groupReadable);
if (((sbuff.st_mode & worldReadable) != mode)
&& (m_oss->Chmod(f_name.c_str(), mode) != XrdOssOK))
{is_complete = false;
*buff = 0;
}
}
}
TRACE(Info, "LocalFilePath '" << curl << "', why=" << lfpReason[why] <<
(is_complete ? " -> FILE_COMPLETE_IN_CACHE" : " -> EREMOTE"));
return is_complete ? 0 : -EREMOTE;
}
}
}
TRACE(Info, "LocalFilePath '" << curl << "', why=" << lfpReason[why] << " -> ENOENT");
return -ENOENT;
}
//______________________________________________________________________________
//! Preapare the cache for a file open request. This method is called prior to
//! actually opening a file. This method is meant to allow defering an open
//! request or implementing the full I/O stack in the cache layer.
//! @return <0 Error has occurred, return value is -errno; fail open request.
//! =0 Continue with open() request.
//! >0 Defer open but treat the file as actually being open. Use the
//! XrdOucCacheIO::Open() method to open the file at a later time.
//------------------------------------------------------------------------------
int Cache::Prepare(const char *curl, int oflags, mode_t mode)
{
XrdCl::URL url(curl);
std::string f_name = url.GetPath();
std::string i_name = f_name + Info::s_infoExtension;
// Do not allow write access.
if (oflags & (O_WRONLY | O_RDWR | O_APPEND | O_CREAT))
{
TRACE(Warning, "Prepare write access requested on file " << f_name << ". Denying access.");
return -EROFS;
}
// Intercept xrdpfc_command requests.
if (m_configuration.m_allow_xrdpfc_command && strncmp("/xrdpfc_command/", f_name.c_str(), 16) == 0)
{
// Schedule a job to process command request.
{
CommandExecutor *ce = new CommandExecutor(f_name, "CommandExecutor");
schedP->Schedule(ce);
}
return -EAGAIN;
}
{
XrdSysCondVarHelper lock(&m_active_cond);
m_purge_delay_set.insert(f_name);
}
struct stat sbuff;
int res = m_oss->Stat(i_name.c_str(), &sbuff);
if (res == 0)
{
TRACE(Dump, "Prepare defer open " << f_name);
return 1;
}
else
{
return 0;
}
}
//______________________________________________________________________________
// virtual method of XrdOucCache.
//!
//! @return <0 - Stat failed, value is -errno.
//! =0 - Stat succeeded, sbuff holds stat information.
//! >0 - Stat could not be done, forward operation to next level.
//------------------------------------------------------------------------------
int Cache::Stat(const char *curl, struct stat &sbuff)
{
XrdCl::URL url(curl);
std::string f_name = url.GetPath();
{
XrdSysCondVarHelper lock(&m_active_cond);
m_purge_delay_set.insert(f_name);
}
if (m_oss->Stat(f_name.c_str(), &sbuff) == XrdOssOK)
{
if (S_ISDIR(sbuff.st_mode))
{
return 0;
}
else
{
bool success = false;
XrdOssDF* infoFile = m_oss->newFile(m_configuration.m_username.c_str());
XrdOucEnv myEnv;
f_name += Info::s_infoExtension;
int res = infoFile->Open(f_name.c_str(), O_RDONLY, 0600, myEnv);
if (res >= 0)
{
Info info(m_trace, 0);
if (info.Read(infoFile, f_name.c_str()))
{
sbuff.st_size = info.GetFileSize();
success = true;
}
}
infoFile->Close();
delete infoFile;
return success ? 0 : 1;
}
}
return 1;
}
//______________________________________________________________________________
// virtual method of XrdOucCache.