-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdClClassicCopyJob.cc
2880 lines (2520 loc) · 107 KB
/
XrdClClassicCopyJob.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) 2011-2014 by European Organization for Nuclear Research (CERN)
// Author: Lukasz Janyst <ljanyst@cern.ch>
//------------------------------------------------------------------------------
// 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 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/>.
//
// In applying this licence, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
//------------------------------------------------------------------------------
#include "XrdCl/XrdClClassicCopyJob.hh"
#include "XrdCl/XrdClConstants.hh"
#include "XrdCl/XrdClLog.hh"
#include "XrdCl/XrdClDefaultEnv.hh"
#include "XrdCl/XrdClFile.hh"
#include "XrdCl/XrdClMonitor.hh"
#include "XrdCl/XrdClUtils.hh"
#include "XrdCl/XrdClCheckSumManager.hh"
#include "XrdCks/XrdCksCalc.hh"
#include "XrdCl/XrdClRedirectorRegistry.hh"
#include "XrdCl/XrdClZipArchive.hh"
#include "XrdCl/XrdClZipOperations.hh"
#include "XrdCl/XrdClPostMaster.hh"
#include "XrdCl/XrdClJobManager.hh"
#include "XrdCl/XrdClXRootDTransport.hh"
#include "XrdClXCpCtx.hh"
#include "XrdCl/XrdClCheckSumHelper.hh"
#include "XrdSys/XrdSysE2T.hh"
#include "XrdSys/XrdSysPthread.hh"
#include <memory>
#include <mutex>
#include <queue>
#include <algorithm>
#include <chrono>
#include <thread>
#include <vector>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cerrno>
#include <unistd.h>
#if __cplusplus < 201103L
#include <ctime>
#endif
namespace
{
struct Result
{
template<typename ... Args>
inline Result( Args&&... args )
{
Get() = XrdCl::XRootDStatus( std::forward<Args>(args)... );
}
inline operator XrdCl::XRootDStatus&()
{
return Get();
}
inline static XrdCl::XRootDStatus& Get()
{
static XrdCl::XRootDStatus status;
return status;
}
Result( Result&& ) = delete;
Result( const Result& ) = delete;
Result& operator=( Result&& ) = delete;
Result& operator=( Result& ) = delete;
};
inline XrdCl::XRootDStatus& SourceError( XrdCl::XRootDStatus &status )
{
std::string msg = status.GetErrorMessage();
msg += " (source)";
status.SetErrorMessage( msg );
Result::Get() = status;
return status;
}
inline XrdCl::XRootDStatus& DestinationError( XrdCl::XRootDStatus &status )
{
std::string msg = status.GetErrorMessage();
msg += " (destination)";
status.SetErrorMessage( msg );
Result::Get() = status;
return status;
}
//----------------------------------------------------------------------------
//! Helper timer class
//----------------------------------------------------------------------------
template<typename U = std::ratio<1, 1>>
class mytimer_t
{
public:
mytimer_t() : start( clock_t::now() ){ }
void reset(){ start = clock_t::now(); }
uint64_t elapsed() const
{
return std::chrono::duration_cast<unit_t>( clock_t::now() - start ).count();
}
private:
typedef std::chrono::high_resolution_clock clock_t;
typedef std::chrono::duration<uint64_t, U> unit_t;
std::chrono::time_point<clock_t> start;
};
using timer_sec_t = mytimer_t<>;
using timer_nsec_t = mytimer_t<std::nano>;
inline XrdCl::XRootDStatus Translate( std::vector<XrdCl::XAttr> &in,
std::vector<XrdCl::xattr_t> &out )
{
std::vector<XrdCl::xattr_t> ret;
ret.reserve( in.size() );
std::vector<XrdCl::XAttr>::iterator itr = in.begin();
for( ; itr != in.end() ; ++itr )
{
if( !itr->status.IsOK() ) return itr->status;
XrdCl::xattr_t xa( itr->name, itr->value );
ret.push_back( std::move( xa ) );
}
out.swap( ret );
return XrdCl::XRootDStatus();
}
//----------------------------------------------------------------------------
//! Helper function for retrieving extended-attributes
//----------------------------------------------------------------------------
inline XrdCl::XRootDStatus GetXAttr( XrdCl::File &file,
std::vector<XrdCl::xattr_t> &xattrs )
{
std::vector<XrdCl::XAttr> rsp;
XrdCl::XRootDStatus st = file.ListXAttr( rsp );
if( !st.IsOK() ) return st;
return Translate( rsp, xattrs );
}
//----------------------------------------------------------------------------
//! Helper function for retrieving extended-attributes
//----------------------------------------------------------------------------
inline XrdCl::XRootDStatus GetXAttr( const std::string &url,
std::vector<XrdCl::xattr_t> &xattrs )
{
XrdCl::URL u( url );
XrdCl::FileSystem fs( u );
std::vector<XrdCl::XAttr> rsp;
XrdCl::XRootDStatus st = fs.ListXAttr( u.GetPath(), rsp );
if( !st.IsOK() ) return st;
return Translate( rsp, xattrs );
}
inline XrdCl::XRootDStatus SetXAttr( XrdCl::File &file,
const std::vector<XrdCl::xattr_t> &xattrs )
{
std::vector<XrdCl::XAttrStatus> rsp;
file.SetXAttr( xattrs, rsp );
std::vector<XrdCl::XAttrStatus>::iterator itr = rsp.begin();
for( ; itr != rsp.end() ; ++itr )
if( !itr->status.IsOK() ) return itr->status;
return XrdCl::XRootDStatus();
}
//----------------------------------------------------------------------------
//! Abstract chunk source
//----------------------------------------------------------------------------
class Source
{
public:
//------------------------------------------------------------------------
// Destructor
//------------------------------------------------------------------------
Source( const std::string &checkSumType = "",
const std::vector<std::string> &addcks = std::vector<std::string>() ) :
pCkSumHelper( 0 ),
pContinue( false )
{
if( !checkSumType.empty() )
pCkSumHelper = new XrdCl::CheckSumHelper( "source", checkSumType );
for( auto &type : addcks )
pAddCksHelpers.push_back( new XrdCl::CheckSumHelper( "source", type ) );
};
virtual ~Source()
{
delete pCkSumHelper;
for( auto ptr : pAddCksHelpers )
delete ptr;
}
//------------------------------------------------------------------------
//! Initialize the source
//------------------------------------------------------------------------
virtual XrdCl::XRootDStatus Initialize() = 0;
//------------------------------------------------------------------------
//! Get size
//------------------------------------------------------------------------
virtual int64_t GetSize() = 0;
//------------------------------------------------------------------------
//! Start reading from the source at given offset
//------------------------------------------------------------------------
virtual XrdCl::XRootDStatus StartAt( uint64_t offset ) = 0;
//------------------------------------------------------------------------
//! Get a data chunk from the source
//!
//! @param ci chunk information
//! @return status of the operation
//! suContinue - there are some chunks left
//! suDone - no chunks left
//------------------------------------------------------------------------
virtual XrdCl::XRootDStatus GetChunk( XrdCl::PageInfo &ci ) = 0;
//------------------------------------------------------------------------
//! Get check sum
//------------------------------------------------------------------------
virtual XrdCl::XRootDStatus GetCheckSum( std::string &checkSum,
std::string &checkSumType ) = 0;
//------------------------------------------------------------------------
//! Get additional checksums
//------------------------------------------------------------------------
virtual std::vector<std::string> GetAddCks() = 0;
//------------------------------------------------------------------------
//! Get extended attributes
//------------------------------------------------------------------------
virtual XrdCl::XRootDStatus GetXAttr( std::vector<XrdCl::xattr_t> &xattrs ) = 0;
//------------------------------------------------------------------------
//! Try different server
//------------------------------------------------------------------------
virtual XrdCl::XRootDStatus TryOtherServer()
{
return XrdCl::XRootDStatus( XrdCl::stError, XrdCl::errNotImplemented );
}
protected:
XrdCl::CheckSumHelper *pCkSumHelper;
std::vector<XrdCl::CheckSumHelper*> pAddCksHelpers;
bool pContinue;
};
//----------------------------------------------------------------------------
//! Abstract chunk destination
//----------------------------------------------------------------------------
class Destination
{
public:
//------------------------------------------------------------------------
//! Constructor
//------------------------------------------------------------------------
Destination( const std::string &checkSumType = "" ):
pPosc( false ), pForce( false ), pCoerce( false ), pMakeDir( false ),
pContinue( false ), pCkSumHelper( 0 )
{
if( !checkSumType.empty() )
pCkSumHelper = new XrdCl::CheckSumHelper( "destination", checkSumType );
}
//------------------------------------------------------------------------
//! Destructor
//------------------------------------------------------------------------
virtual ~Destination()
{
delete pCkSumHelper;
}
//------------------------------------------------------------------------
//! Initialize the destination
//------------------------------------------------------------------------
virtual XrdCl::XRootDStatus Initialize() = 0;
//------------------------------------------------------------------------
//! Finalize the destination
//------------------------------------------------------------------------
virtual XrdCl::XRootDStatus Finalize() = 0;
//------------------------------------------------------------------------
//! Put a data chunk at a destination
//!
//! @param ci chunk information
//! @return status of the operation
//------------------------------------------------------------------------
virtual XrdCl::XRootDStatus PutChunk( XrdCl::PageInfo &&ci ) = 0;
//------------------------------------------------------------------------
//! Flush chunks that might have been queues
//------------------------------------------------------------------------
virtual XrdCl::XRootDStatus Flush() = 0;
//------------------------------------------------------------------------
//! Get check sum
//------------------------------------------------------------------------
virtual XrdCl::XRootDStatus GetCheckSum( std::string &checkSum,
std::string &checkSumType ) = 0;
//------------------------------------------------------------------------
//! Set extended attributes
//------------------------------------------------------------------------
virtual XrdCl::XRootDStatus SetXAttr( const std::vector<XrdCl::xattr_t> &xattrs ) = 0;
//------------------------------------------------------------------------
//! Get size
//------------------------------------------------------------------------
virtual int64_t GetSize() = 0;
//------------------------------------------------------------------------
//! Set POSC
//------------------------------------------------------------------------
void SetPOSC( bool posc )
{
pPosc = posc;
}
//------------------------------------------------------------------------
//! Set force
//------------------------------------------------------------------------
void SetForce( bool force )
{
pForce = force;
}
//------------------------------------------------------------------------
//! Set continue
//------------------------------------------------------------------------
void SetContinue( bool continue_ )
{
pContinue = continue_;
}
//------------------------------------------------------------------------
//! Set coerce
//------------------------------------------------------------------------
void SetCoerce( bool coerce )
{
pCoerce = coerce;
}
//------------------------------------------------------------------------
//! Set makedir
//------------------------------------------------------------------------
void SetMakeDir( bool makedir )
{
pMakeDir = makedir;
}
//------------------------------------------------------------------------
//! Get last URL
//------------------------------------------------------------------------
virtual const std::string& GetLastURL() const
{
static const std::string empty;
return empty;
}
//------------------------------------------------------------------------
//! Get write-recovery redirector
//------------------------------------------------------------------------
virtual const std::string& GetWrtRecoveryRedir() const
{
static const std::string empty;
return empty;
}
protected:
bool pPosc;
bool pForce;
bool pCoerce;
bool pMakeDir;
bool pContinue;
XrdCl::CheckSumHelper *pCkSumHelper;
};
//----------------------------------------------------------------------------
//! StdIn source
//----------------------------------------------------------------------------
class StdInSource: public Source
{
public:
//------------------------------------------------------------------------
//! Constructor
//------------------------------------------------------------------------
StdInSource( const std::string &ckSumType, uint32_t chunkSize, const std::vector<std::string> &addcks ):
Source( ckSumType, addcks ),
pCurrentOffset(0),
pChunkSize( chunkSize )
{
}
//------------------------------------------------------------------------
//! Destructor
//------------------------------------------------------------------------
virtual ~StdInSource()
{
}
//------------------------------------------------------------------------
//! Initialize the source
//------------------------------------------------------------------------
virtual XrdCl::XRootDStatus Initialize()
{
if( pCkSumHelper )
{
auto st = pCkSumHelper->Initialize();
if( !st.IsOK() ) return st;
for( auto cksHelper : pAddCksHelpers )
{
st = cksHelper->Initialize();
if( !st.IsOK() ) return st;
}
}
return XrdCl::XRootDStatus();
}
//------------------------------------------------------------------------
//! Get size
//------------------------------------------------------------------------
virtual int64_t GetSize()
{
return -1;
}
//------------------------------------------------------------------------
//! Start reading from the source at given offset
//------------------------------------------------------------------------
virtual XrdCl::XRootDStatus StartAt( uint64_t )
{
return XrdCl::XRootDStatus( XrdCl::stError, XrdCl::errNotSupported, ENOTSUP,
"Cannot continue from stdin!" );
}
//------------------------------------------------------------------------
//! Get a data chunk from the source
//------------------------------------------------------------------------
virtual XrdCl::XRootDStatus GetChunk( XrdCl::PageInfo &ci )
{
using namespace XrdCl;
Log *log = DefaultEnv::GetLog();
uint32_t toRead = pChunkSize;
char *buffer = new char[toRead];
int64_t bytesRead = 0;
uint32_t offset = 0;
while( toRead )
{
int64_t bRead = read( 0, buffer+offset, toRead );
if( bRead == -1 )
{
log->Debug( UtilityMsg, "Unable to read from stdin: %s",
XrdSysE2T( errno ) );
delete [] buffer;
return XRootDStatus( stError, errOSError, errno );
}
if( bRead == 0 )
break;
bytesRead += bRead;
offset += bRead;
toRead -= bRead;
}
if( bytesRead == 0 )
{
delete [] buffer;
return XRootDStatus( stOK, suDone );
}
if( pCkSumHelper )
pCkSumHelper->Update( buffer, bytesRead );
for( auto cksHelper : pAddCksHelpers )
cksHelper->Update( buffer, bytesRead );
ci = XrdCl::PageInfo( pCurrentOffset, bytesRead, buffer );
pCurrentOffset += bytesRead;
return XRootDStatus( stOK, suContinue );
}
//------------------------------------------------------------------------
//! Get check sum
//------------------------------------------------------------------------
virtual XrdCl::XRootDStatus GetCheckSumImpl( XrdCl::CheckSumHelper *cksHelper,
std::string &checkSum,
std::string &checkSumType )
{
using namespace XrdCl;
if( cksHelper )
return cksHelper->GetCheckSum( checkSum, checkSumType );
return XRootDStatus( stError, errCheckSumError );
}
//------------------------------------------------------------------------
//! Get check sum
//------------------------------------------------------------------------
virtual XrdCl::XRootDStatus GetCheckSum( std::string &checkSum,
std::string &checkSumType )
{
return GetCheckSumImpl( pCkSumHelper, checkSum, checkSumType );
}
//------------------------------------------------------------------------
//! Get additional checksums
//------------------------------------------------------------------------
std::vector<std::string> GetAddCks()
{
std::vector<std::string> ret;
for( auto cksHelper : pAddCksHelpers )
{
std::string type = cksHelper->GetType();
std::string cks;
GetCheckSumImpl( cksHelper, cks, type );
ret.push_back( type + ":" + cks );
}
return ret;
}
//------------------------------------------------------------------------
//! Get extended attributes
//------------------------------------------------------------------------
virtual XrdCl::XRootDStatus GetXAttr( std::vector<XrdCl::xattr_t> &xattrs )
{
return XrdCl::XRootDStatus();
}
private:
StdInSource(const StdInSource &other);
StdInSource &operator = (const StdInSource &other);
uint64_t pCurrentOffset;
uint32_t pChunkSize;
};
//----------------------------------------------------------------------------
//! XRootDSource
//----------------------------------------------------------------------------
class XRootDSource: public Source
{
struct CancellableJob : public XrdCl::Job
{
virtual void Cancel() = 0;
std::mutex mtx;
};
//----------------------------------------------------------------------------
// On-connect callback job, a lambda would be more elegant, but we still have
// to support SLC6
//----------------------------------------------------------------------------
template<typename READER>
struct OnConnJob : public CancellableJob
{
OnConnJob( XRootDSource *self, READER *reader ) : self( self ), reader( reader )
{
}
void Run( void* )
{
std::unique_lock<std::mutex> lck( mtx );
if( !self || !reader ) return;
// add new chunks to the queue
if( self->pNbConn < self->pMaxNbConn )
self->FillQueue( reader );
}
void Cancel()
{
std::unique_lock<std::mutex> lck( mtx );
self = 0;
reader = 0;
}
private:
XRootDSource *self;
READER *reader;
};
public:
//------------------------------------------------------------------------
//! Try different server
//------------------------------------------------------------------------
XrdCl::XRootDStatus TryOtherServer()
{
return pFile->TryOtherServer();
}
//------------------------------------------------------------------------
//! Constructor
//------------------------------------------------------------------------
XRootDSource( const XrdCl::URL *url,
uint32_t chunkSize,
uint8_t parallelChunks,
const std::string &ckSumType,
const std::vector<std::string> &addcks,
bool doserver ):
Source( ckSumType, addcks ),
pUrl( url ), pFile( new XrdCl::File() ), pSize( -1 ),
pCurrentOffset( 0 ), pChunkSize( chunkSize ),
pParallel( parallelChunks ),
pNbConn( 0 ), pUsePgRead( false ),
pDoServer( doserver )
{
int val = XrdCl::DefaultSubStreamsPerChannel;
XrdCl::DefaultEnv::GetEnv()->GetInt( "SubStreamsPerChannel", val );
pMaxNbConn = val - 1; // account for the control stream
}
//------------------------------------------------------------------------
//! Destructor
//------------------------------------------------------------------------
virtual ~XRootDSource()
{
if( pDataConnCB )
pDataConnCB->Cancel();
CleanUpChunks();
if( pFile->IsOpen() )
XrdCl::XRootDStatus status = pFile->Close();
delete pFile;
}
//------------------------------------------------------------------------
//! Initialize the source
//------------------------------------------------------------------------
virtual XrdCl::XRootDStatus Initialize()
{
using namespace XrdCl;
Log *log = DefaultEnv::GetLog();
log->Debug( UtilityMsg, "Opening %s for reading",
pUrl->GetURL().c_str() );
std::string value;
DefaultEnv::GetEnv()->GetString( "ReadRecovery", value );
pFile->SetProperty( "ReadRecovery", value );
XRootDStatus st = pFile->Open( pUrl->GetURL(), OpenFlags::Read );
if( !st.IsOK() )
return st;
StatInfo *statInfo;
st = pFile->Stat( false, statInfo );
if( !st.IsOK() )
return st;
pSize = statInfo->GetSize();
delete statInfo;
if( pUrl->IsLocalFile() && !pUrl->IsMetalink() && pCkSumHelper && !pContinue )
{
st = pCkSumHelper->Initialize();
if( !st.IsOK() ) return st;
for( auto cksHelper : pAddCksHelpers )
{
st = cksHelper->Initialize();
if( !st.IsOK() ) return st;
}
}
//----------------------------------------------------------------------
// Figere out the actual data server we are talking to
//----------------------------------------------------------------------
if( !pUrl->IsLocalFile() ||
( pUrl->IsLocalFile() && pUrl->IsMetalink() ) )
{
pFile->GetProperty( "LastURL", pDataServer );
}
if( ( !pUrl->IsLocalFile() && !pFile->IsSecure() ) ||
( pUrl->IsLocalFile() && pUrl->IsMetalink() ) )
{
//--------------------------------------------------------------------
// Decide whether we can use PgRead
//--------------------------------------------------------------------
int val = XrdCl::DefaultCpUsePgWrtRd;
XrdCl::DefaultEnv::GetEnv()->GetInt( "CpUsePgWrtRd", val );
pUsePgRead = XrdCl::Utils::HasPgRW( pDataServer ) && ( val == 1 );
}
//----------------------------------------------------------------------
// Print the IPv4/IPv6 stack to the stderr if we are running in server
// mode
//----------------------------------------------------------------------
if( pDoServer && !pUrl->IsLocalFile() )
{
AnyObject obj;
DefaultEnv::GetPostMaster()->QueryTransport( pDataServer, TransportQuery::IpStack, obj );
std::string *ipstack = nullptr;
obj.Get( ipstack );
std::cerr << "!-!" << *ipstack << std::endl;
delete ipstack;
}
SetOnDataConnectHandler( pFile );
return XRootDStatus();
}
//------------------------------------------------------------------------
//! Get size
//------------------------------------------------------------------------
virtual int64_t GetSize()
{
return pSize;
}
//------------------------------------------------------------------------
//! Start reading from the source at given offset
//------------------------------------------------------------------------
virtual XrdCl::XRootDStatus StartAt( uint64_t offset )
{
pCurrentOffset = offset;
pContinue = true;
return XrdCl::XRootDStatus();
}
//------------------------------------------------------------------------
//! Get a data chunk from the source
//!
//! @param ci chunk information
//! @return status of the operation
//! suContinue - there are some chunks left
//! suDone - no chunks left
//------------------------------------------------------------------------
virtual XrdCl::XRootDStatus GetChunk( XrdCl::PageInfo &ci )
{
return GetChunkImpl( pFile, ci );
}
//------------------------------------------------------------------------
//! Get extended attributes
//------------------------------------------------------------------------
virtual XrdCl::XRootDStatus GetXAttr( std::vector<XrdCl::xattr_t> &xattrs )
{
return ::GetXAttr( *pFile, xattrs );
}
//------------------------------------------------------------------------
// Clean up the chunks that are flying
//------------------------------------------------------------------------
void CleanUpChunks()
{
while( !pChunks.empty() )
{
ChunkHandler *ch = pChunks.front();
pChunks.pop();
ch->sem->Wait();
delete [] (char *)ch->chunk.GetBuffer();
delete ch;
}
}
//------------------------------------------------------------------------
// Get check sum
//------------------------------------------------------------------------
virtual XrdCl::XRootDStatus GetCheckSum( std::string &checkSum,
std::string &checkSumType )
{
return GetCheckSumImpl( pCkSumHelper, checkSum, checkSumType );
}
XrdCl::XRootDStatus GetCheckSumImpl( XrdCl::CheckSumHelper *cksHelper,
std::string &checkSum,
std::string &checkSumType )
{
if( pUrl->IsMetalink() )
{
XrdCl::RedirectorRegistry ®istry = XrdCl::RedirectorRegistry::Instance();
XrdCl::VirtualRedirector *redirector = registry.Get( *pUrl );
checkSum = redirector->GetCheckSum( checkSumType );
if( !checkSum.empty() ) return XrdCl::XRootDStatus();
}
if( pUrl->IsLocalFile() )
{
if( pContinue )
// in case of --continue option we have to calculate the checksum from scratch
return XrdCl::Utils::GetLocalCheckSum( checkSum, checkSumType, pUrl->GetPath() );
if( cksHelper )
return cksHelper->GetCheckSum( checkSum, checkSumType );
return XrdCl::XRootDStatus( XrdCl::stError, XrdCl::errCheckSumError );
}
std::string dataServer; pFile->GetProperty( "DataServer", dataServer );
std::string lastUrl; pFile->GetProperty( "LastURL", lastUrl );
return XrdCl::Utils::GetRemoteCheckSum( checkSum, checkSumType, XrdCl::URL( lastUrl ) );
}
//------------------------------------------------------------------------
//! Get additional checksums
//------------------------------------------------------------------------
std::vector<std::string> GetAddCks()
{
std::vector<std::string> ret;
for( auto cksHelper : pAddCksHelpers )
{
std::string type = cksHelper->GetType();
std::string cks;
GetCheckSumImpl( cksHelper, cks, type );
ret.push_back( cks );
}
return ret;
}
private:
XRootDSource(const XRootDSource &other);
XRootDSource &operator = (const XRootDSource &other);
protected:
//------------------------------------------------------------------------
// Fill the queue with in-the-fly read requests
//------------------------------------------------------------------------
template<typename READER>
inline void FillQueue( READER *reader )
{
//----------------------------------------------------------------------
// Get the number of connected streams
//----------------------------------------------------------------------
uint16_t parallel = pParallel;
if( pNbConn < pMaxNbConn )
{
pNbConn = XrdCl::DefaultEnv::GetPostMaster()->
NbConnectedStrm( pDataServer );
}
if( pNbConn ) parallel *= pNbConn;
while( pChunks.size() < parallel && pCurrentOffset < pSize )
{
uint64_t chunkSize = pChunkSize;
if( pCurrentOffset + chunkSize > (uint64_t)pSize )
chunkSize = pSize - pCurrentOffset;
char *buffer = new char[chunkSize];
ChunkHandler *ch = new ChunkHandler();
ch->status = pUsePgRead
? reader->PgRead( pCurrentOffset, chunkSize, buffer, ch )
: reader->Read( pCurrentOffset, chunkSize, buffer, ch );
pChunks.push( ch );
pCurrentOffset += chunkSize;
if( !ch->status.IsOK() )
{
ch->sem->Post();
break;
}
}
}
//------------------------------------------------------------------------
// Set the on-connect handler for data streams
//------------------------------------------------------------------------
template<typename READER>
void SetOnDataConnectHandler( READER *reader )
{
// we need to create the object anyway as it contains our mutex now
pDataConnCB.reset( new OnConnJob<READER>( this, reader ) );
// check if it is a local file
if( pDataServer.empty() ) return;
XrdCl::DefaultEnv::GetPostMaster()->SetOnDataConnectHandler( pDataServer, pDataConnCB );
}
//------------------------------------------------------------------------
//! Get a data chunk from the source
//!
//! @param reader : reader to read data from
//! @param ci : chunk information
//! @return : status of the operation
//! suContinue - there are some chunks left
//! suDone - no chunks left
//------------------------------------------------------------------------
template<typename READER>
XrdCl::XRootDStatus GetChunkImpl( READER *reader, XrdCl::PageInfo &ci )
{
//----------------------------------------------------------------------
// Sanity check
//----------------------------------------------------------------------
using namespace XrdCl;
Log *log = DefaultEnv::GetLog();
//----------------------------------------------------------------------
// Fill the queue
//----------------------------------------------------------------------
std::unique_lock<std::mutex> lck( pDataConnCB->mtx );
FillQueue( reader );
//----------------------------------------------------------------------
// Pick up a chunk from the front and wait for status
//----------------------------------------------------------------------
if( pChunks.empty() )
return XRootDStatus( stOK, suDone );
std::unique_ptr<ChunkHandler> ch( pChunks.front() );
pChunks.pop();
lck.unlock();
ch->sem->Wait();
if( !ch->status.IsOK() )
{
log->Debug( UtilityMsg, "Unable read %d bytes at %ld from %s: %s",
ch->chunk.GetLength(), ch->chunk.GetOffset(),
pUrl->GetURL().c_str(), ch->status.ToStr().c_str() );
delete [] (char *)ch->chunk.GetBuffer();
CleanUpChunks();
return ch->status;
}
ci = std::move( ch->chunk );
// if it is a local file update the checksum
if( pUrl->IsLocalFile() && !pUrl->IsMetalink() && !pContinue )
{
if( pCkSumHelper )
pCkSumHelper->Update( ci.GetBuffer(), ci.GetLength() );
for( auto cksHelper : pAddCksHelpers )
cksHelper->Update( ci.GetBuffer(), ci.GetLength() );
}
return XRootDStatus( stOK, suContinue );
}
//------------------------------------------------------------------------
// Asynchronous chunk handler
//------------------------------------------------------------------------
class ChunkHandler: public XrdCl::ResponseHandler
{
public:
ChunkHandler(): sem( new XrdSysSemaphore(0) ) {}
virtual ~ChunkHandler() { delete sem; }
virtual void HandleResponse( XrdCl::XRootDStatus *statusval,
XrdCl::AnyObject *response )
{
this->status = *statusval;
delete statusval;
if( response )
{
chunk = ToChunk( response );
delete response;
}
sem->Post();
}
XrdCl::PageInfo ToChunk( XrdCl::AnyObject *response )
{
if( response->Has<XrdCl::PageInfo>() )
{
XrdCl::PageInfo *resp = nullptr;
response->Get( resp );
return std::move( *resp );
}
else
{
XrdCl::ChunkInfo *resp = nullptr;
response->Get( resp );
return XrdCl::PageInfo( resp->GetOffset(), resp->GetLength(),
resp->GetBuffer() );
}
}
XrdSysSemaphore *sem;
XrdCl::PageInfo chunk;
XrdCl::XRootDStatus status;
};