-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdClThirdPartyCopyJob.cc
852 lines (730 loc) · 29.8 KB
/
XrdClThirdPartyCopyJob.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
//------------------------------------------------------------------------------
// 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/XrdClThirdPartyCopyJob.hh"
#include "XrdCl/XrdClConstants.hh"
#include "XrdCl/XrdClLog.hh"
#include "XrdCl/XrdClDefaultEnv.hh"
#include "XrdCl/XrdClUtils.hh"
#include "XrdCl/XrdClMessageUtils.hh"
#include "XrdCl/XrdClMonitor.hh"
#include "XrdCl/XrdClUglyHacks.hh"
#include "XrdCl/XrdClRedirectorRegistry.hh"
#include "XrdCl/XrdClDlgEnv.hh"
#include "XrdOuc/XrdOucTPC.hh"
#include "XrdSys/XrdSysTimer.hh"
#include <iostream>
#include <chrono>
#include <cctype>
#include <sstream>
#include <cstdlib>
#include <cstdio>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
namespace
{
//----------------------------------------------------------------------------
//! Handle an async response
//----------------------------------------------------------------------------
class TPCStatusHandler: public XrdCl::ResponseHandler
{
public:
//------------------------------------------------------------------------
// Constructor
//------------------------------------------------------------------------
TPCStatusHandler():
pSem( new XrdCl::Semaphore(0) ), pStatus(0)
{
}
//------------------------------------------------------------------------
// Destructor
//------------------------------------------------------------------------
virtual ~TPCStatusHandler()
{
delete pStatus;
delete pSem;
}
//------------------------------------------------------------------------
// Handle Response
//------------------------------------------------------------------------
virtual void HandleResponse( XrdCl::XRootDStatus *status,
XrdCl::AnyObject *response )
{
delete response;
pStatus = status;
pSem->Post();
}
//------------------------------------------------------------------------
// Get Mutex
//------------------------------------------------------------------------
XrdCl::Semaphore *GetSemaphore()
{
return pSem;
}
//------------------------------------------------------------------------
// Get status
//------------------------------------------------------------------------
XrdCl::XRootDStatus *GetStatus()
{
return pStatus;
}
private:
TPCStatusHandler(const TPCStatusHandler &other);
TPCStatusHandler &operator = (const TPCStatusHandler &other);
XrdCl::Semaphore *pSem;
XrdCl::XRootDStatus *pStatus;
};
class InitTimeoutCalc
{
public:
InitTimeoutCalc( uint16_t timeLeft ) :
hasInitTimeout( timeLeft ), start( time( 0 ) ), timeLeft( timeLeft )
{
}
XrdCl::XRootDStatus operator()()
{
if( !hasInitTimeout ) return XrdCl::XRootDStatus();
time_t now = time( 0 );
if( now - start > timeLeft )
return XrdCl::XRootDStatus( XrdCl::stError, XrdCl::errOperationExpired );
timeLeft -= ( now - start );
return XrdCl::XRootDStatus();
}
operator uint16_t()
{
return timeLeft;
}
private:
bool hasInitTimeout;
time_t start;
uint16_t timeLeft;
};
}
namespace XrdCl
{
//----------------------------------------------------------------------------
// Constructor
//----------------------------------------------------------------------------
ThirdPartyCopyJob::ThirdPartyCopyJob( uint16_t jobId,
PropertyList *jobProperties,
PropertyList *jobResults ):
CopyJob( jobId, jobProperties, jobResults ),
dstFile( File::DisableVirtRedirect ),
sourceSize( 0 ),
initTimeout( 0 ),
force( false ),
coerce( false ),
delegate( false ),
nbStrm( 0 ),
tpcLite( false )
{
Log *log = DefaultEnv::GetLog();
log->Debug( UtilityMsg, "Creating a third party copy job, from %s to %s",
GetSource().GetURL().c_str(), GetTarget().GetURL().c_str() );
}
//----------------------------------------------------------------------------
// Run the copy job
//----------------------------------------------------------------------------
XRootDStatus ThirdPartyCopyJob::Run( CopyProgressHandler *progress )
{
Log *log = DefaultEnv::GetLog();
XRootDStatus st = CanDo();
if( !st.IsOK() ) return st;
if( tpcLite )
{
//------------------------------------------------------------------------
// Run TPC-lite algorithm
//------------------------------------------------------------------------
XRootDStatus st = RunLite( progress );
if( !st.IsOK() ) return st;
}
else
{
//------------------------------------------------------------------------
// Run vanilla TPC algorithm
//------------------------------------------------------------------------
XRootDStatus st = RunTPC( progress );
if( !st.IsOK() ) return st;
}
//--------------------------------------------------------------------------
// Verify the checksums if needed
//--------------------------------------------------------------------------
if( checkSumMode != "none" )
{
log->Debug( UtilityMsg, "Attempting checksum calculation." );
std::string sourceCheckSum;
std::string targetCheckSum;
//------------------------------------------------------------------------
// Get the check sum at source
//------------------------------------------------------------------------
timeval oStart, oEnd;
XRootDStatus st;
if( checkSumMode == "end2end" || checkSumMode == "source" )
{
gettimeofday( &oStart, 0 );
if( !checkSumPreset.empty() )
{
sourceCheckSum = checkSumType + ":";
sourceCheckSum += checkSumPreset;
}
else
{
VirtualRedirector *redirector = 0;
std::string vrCheckSum;
if( GetSource().IsMetalink() &&
( redirector = RedirectorRegistry::Instance().Get( GetSource() ) ) &&
!( vrCheckSum = redirector->GetCheckSum( checkSumType ) ).empty() )
sourceCheckSum = vrCheckSum;
else
st = Utils::GetRemoteCheckSum( sourceCheckSum, checkSumType,
tpcSource.GetHostId(),
tpcSource.GetPath() );
}
gettimeofday( &oEnd, 0 );
if( !st.IsOK() )
return st;
pResults->Set( "sourceCheckSum", sourceCheckSum );
}
//------------------------------------------------------------------------
// Get the check sum at destination
//------------------------------------------------------------------------
timeval tStart, tEnd;
if( checkSumMode == "end2end" || checkSumMode == "target" )
{
gettimeofday( &tStart, 0 );
st = Utils::GetRemoteCheckSum( targetCheckSum, checkSumType,
realTarget.GetHostId(),
realTarget.GetPath() );
gettimeofday( &tEnd, 0 );
if( !st.IsOK() )
return st;
pResults->Set( "targetCheckSum", targetCheckSum );
}
//------------------------------------------------------------------------
// Compare and inform monitoring
//------------------------------------------------------------------------
if( checkSumMode == "end2end" )
{
bool match = false;
if( sourceCheckSum == targetCheckSum )
match = true;
Monitor *mon = DefaultEnv::GetMonitor();
if( mon )
{
Monitor::CheckSumInfo i;
i.transfer.origin = &GetSource();
i.transfer.target = &GetTarget();
i.cksum = sourceCheckSum;
i.oTime = Utils::GetElapsedMicroSecs( oStart, oEnd );
i.tTime = Utils::GetElapsedMicroSecs( tStart, tEnd );
i.isOK = match;
mon->Event( Monitor::EvCheckSum, &i );
}
if( !match )
return XRootDStatus( stError, errCheckSumError, 0 );
}
}
return XRootDStatus();
}
//----------------------------------------------------------------------------
// Check whether doing a third party copy is feasible for given
// job descriptor
//----------------------------------------------------------------------------
XRootDStatus ThirdPartyCopyJob::CanDo()
{
const URL &source = GetSource();
const URL &target = GetTarget();
//--------------------------------------------------------------------------
// We can only do a TPC if both source and destination are remote files
//--------------------------------------------------------------------------
if( source.IsLocalFile() || target.IsLocalFile() )
return XRootDStatus( stError, errNotSupported, 0,
"Cannot do a third-party-copy for local file." );
//--------------------------------------------------------------------------
// Check the initial settings
//--------------------------------------------------------------------------
Log *log = DefaultEnv::GetLog();
log->Debug( UtilityMsg, "Check if third party copy between %s and %s "
"is possible", source.GetURL().c_str(),
target.GetURL().c_str() );
if( target.GetProtocol() != "root" &&
target.GetProtocol() != "xroot" )
return XRootDStatus( stError, errNotSupported, 0, "Third-party-copy "
"is only supported for root/xroot protocol." );
pProperties->Get( "initTimeout", initTimeout );
InitTimeoutCalc timeLeft( initTimeout );
pProperties->Get( "checkSumMode", checkSumMode );
pProperties->Get( "checkSumType", checkSumType );
pProperties->Get( "checkSumPreset", checkSumPreset );
pProperties->Get( "force", force );
pProperties->Get( "coerce", coerce );
pProperties->Get( "delegate", delegate );
XrdCl::Env *env = XrdCl::DefaultEnv::GetEnv();
env->GetInt( "SubStreamsPerChannel", nbStrm );
bool tpcLiteOnly = false;
if( !delegate )
log->Info( UtilityMsg, "We are NOT using delegation" );
//--------------------------------------------------------------------------
// Check if we can open the source. Note in TPC-lite scenario it is optional
// for this step to be successful.
//--------------------------------------------------------------------------
File sourceFile;
// set WriteRecovery property
std::string value;
DefaultEnv::GetEnv()->GetString( "ReadRecovery", value );
sourceFile.SetProperty( "ReadRecovery", value );
XRootDStatus st;
URL sourceURL = source;
URL::ParamsMap params = sourceURL.GetParams();
params["tpc.stage"] = "placement";
sourceURL.SetParams( params );
log->Debug( UtilityMsg, "Trying to open %s for reading",
sourceURL.GetURL().c_str() );
st = sourceFile.Open( sourceURL.GetURL(), OpenFlags::Read, Access::None,
timeLeft );
if( st.IsOK() )
{
std::string sourceUrl;
sourceFile.GetProperty( "LastURL", sourceUrl );
tpcSource = sourceUrl;
VirtualRedirector *redirector = 0;
long long size = -1;
if( source.IsMetalink() &&
( redirector = RedirectorRegistry::Instance().Get( tpcSource ) ) &&
( size = redirector->GetSize() ) >= 0 )
sourceSize = size;
else
{
StatInfo *statInfo;
st = sourceFile.Stat( false, statInfo );
if (st.IsOK()) sourceSize = statInfo->GetSize();
delete statInfo;
}
}
else
{
log->Info( UtilityMsg, "Cannot open source file %s: %s",
source.GetURL().c_str(), st.ToStr().c_str() );
if( !delegate )
{
//----------------------------------------------------------------------
// If we cannot contact the source and there is no credential to delegate
// it cannot possibly work
//----------------------------------------------------------------------
st.status = stFatal;
return st;
}
tpcLiteOnly = true;
}
if( !timeLeft().IsOK() )
{
// we still want to send a close, but we time it out quickly
st = sourceFile.Close( 1 );
return XRootDStatus( stError, errOperationExpired );
}
st = sourceFile.Close( timeLeft );
if( !timeLeft().IsOK() )
return XRootDStatus( stError, errOperationExpired );
//--------------------------------------------------------------------------
// Now we need to check the destination !!!
//--------------------------------------------------------------------------
if( delegate )
DlgEnv::Instance().Enable();
else
DlgEnv::Instance().Disable();
//--------------------------------------------------------------------------
// Generate the destination CGI
//--------------------------------------------------------------------------
log->Debug( UtilityMsg, "Generating the destination TPC URL" );
tpcKey = GenerateKey();
char *cgiBuff = new char[2048];
const char *cgiP = XrdOucTPC::cgiC2Dst( tpcKey.c_str(),
tpcSource.GetHostId().c_str(),
tpcSource.GetPath().c_str(),
0, cgiBuff, 2048, nbStrm,
GetSource().GetHostId().c_str(),
GetSource().GetProtocol().c_str(),
GetTarget().GetProtocol().c_str() );
if( *cgiP == '!' )
{
log->Error( UtilityMsg, "Unable to setup target url: %s", cgiP+1 );
delete [] cgiBuff;
return XRootDStatus( stError, errNotSupported );
}
URL cgiURL; cgiURL.SetParams( cgiBuff );
delete [] cgiBuff;
realTarget = GetTarget();
params = realTarget.GetParams();
MessageUtils::MergeCGI( params, cgiURL.GetParams(), true );
std::ostringstream o; o << sourceSize;
params["oss.asize"] = o.str();
params["tpc.stage"] = "copy";
realTarget.SetParams( params );
log->Debug( UtilityMsg, "Target url is: %s", realTarget.GetURL().c_str() );
//--------------------------------------------------------------------------
// Open the target file
//--------------------------------------------------------------------------
// set WriteRecovery property
DefaultEnv::GetEnv()->GetString( "WriteRecovery", value );
dstFile.SetProperty( "WriteRecovery", value );
OpenFlags::Flags targetFlags = OpenFlags::Update;
if( force )
targetFlags |= OpenFlags::Delete;
else
targetFlags |= OpenFlags::New;
if( coerce )
targetFlags |= OpenFlags::Force;
st = dstFile.Open( realTarget.GetURL(), targetFlags, Access::None, timeLeft );
if( !st.IsOK() )
{
log->Error( UtilityMsg, "Unable to open target %s: %s",
realTarget.GetURL().c_str(), st.ToStr().c_str() );
if( st.code == errErrorResponse &&
st.errNo == kXR_FSError &&
st.GetErrorMessage().find( "tpc not supported" ) != std::string::npos )
return XRootDStatus( stError, errNotSupported, 0, // the open failed due to lack of TPC support on the server side
"Destination does not support third-party-copy." );
return st;
}
std::string lastUrl; dstFile.GetProperty( "LastURL", lastUrl );
realTarget = lastUrl;
if( !timeLeft().IsOK() )
{
// we still want to send a close, but we time it out fast
st = dstFile.Close( 1 );
return XRootDStatus( stError, errOperationExpired );
}
//--------------------------------------------------------------------------
// Verify if the destination supports TPC / TPC-lite
//--------------------------------------------------------------------------
st = Utils::CheckTPCLite( realTarget.GetHostId() );
if( !st.IsOK() )
{
// we still want to send a close, but we time it out fast
st = dstFile.Close( 1 );
return XRootDStatus( stError, errNotSupported, 0, // doesn't support TPC
"Destination does not support third-party-copy.");
}
//--------------------------------------------------------------------------
// if target supports TPC-lite and we have a credential to delegate we can
// go ahead and use TPC-lite
//--------------------------------------------------------------------------
tpcLite = ( st.code != suPartial ) && delegate;
if( !tpcLite && tpcLiteOnly ) // doesn't support TPC-lite and it was our only hope
{
st = dstFile.Close( 1 );
return XRootDStatus( stError, errNotSupported, 0, "Destination does not "
"support delegation." );
}
//--------------------------------------------------------------------------
// adjust the InitTimeout
//--------------------------------------------------------------------------
if( !timeLeft().IsOK() )
{
// we still want to send a close, but we time it out fast
st = dstFile.Close( 1 );
return XRootDStatus( stError, errOperationExpired );
}
//--------------------------------------------------------------------------
// If we don't use delegation the source has to support TPC
//--------------------------------------------------------------------------
if( !tpcLite )
{
st = Utils::CheckTPC( URL( tpcSource ).GetHostId(), timeLeft );
if( !st.IsOK() )
{
log->Error( UtilityMsg, "Source (%s) does not support TPC",
tpcSource.GetHostId().c_str() );
return XRootDStatus( stError, errNotSupported, 0, "Source does not "
"support third-party-copy" );
}
if( !timeLeft().IsOK() )
{
// we still want to send a close, but we time it out quickly
st = sourceFile.Close( 1 );
return XRootDStatus( stError, errOperationExpired );
}
}
initTimeout = uint16_t( timeLeft );
return XRootDStatus();
}
//----------------------------------------------------------------------------
// Run vanilla copy job
//----------------------------------------------------------------------------
XRootDStatus ThirdPartyCopyJob::RunTPC( CopyProgressHandler *progress )
{
Log *log = DefaultEnv::GetLog();
//--------------------------------------------------------------------------
// Generate the source CGI
//--------------------------------------------------------------------------
char *cgiBuff = new char[2048];
const char *cgiP = XrdOucTPC::cgiC2Src( tpcKey.c_str(),
realTarget.GetHostName().c_str(), -1, cgiBuff,
2048 );
if( *cgiP == '!' )
{
log->Error( UtilityMsg, "Unable to setup source url: %s", cgiP+1 );
delete [] cgiBuff;
return XRootDStatus( stError, errInvalidArgs );
}
URL cgiURL; cgiURL.SetParams( cgiBuff );
delete [] cgiBuff;
URL::ParamsMap params = tpcSource.GetParams();
MessageUtils::MergeCGI( params, cgiURL.GetParams(), true );
params["tpc.stage"] = "copy";
tpcSource.SetParams( params );
log->Debug( UtilityMsg, "Source url is: %s", tpcSource.GetURL().c_str() );
// Set the close timeout to the default value of the stream timeout
int closeTimeout = 0;
(void) DefaultEnv::GetEnv()->GetInt( "StreamTimeout", closeTimeout);
//--------------------------------------------------------------------------
// Set up the rendez-vous and open the source
//--------------------------------------------------------------------------
InitTimeoutCalc timeLeft( initTimeout );
XRootDStatus st = dstFile.Sync( timeLeft );
if( !st.IsOK() )
{
log->Error( UtilityMsg, "Unable set up rendez-vous: %s",
st.ToStr().c_str() );
XRootDStatus status = dstFile.Close( closeTimeout );
return st;
}
//--------------------------------------------------------------------------
// Calculate the time we have left to perform source open
//--------------------------------------------------------------------------
if( !timeLeft().IsOK() )
{
XRootDStatus status = dstFile.Close( closeTimeout );
return XRootDStatus( stError, errOperationExpired );
}
File sourceFile( File::DisableVirtRedirect );
// set ReadRecovery property
std::string value;
DefaultEnv::GetEnv()->GetString( "ReadRecovery", value );
sourceFile.SetProperty( "ReadRecovery", value );
st = sourceFile.Open( tpcSource.GetURL(), OpenFlags::Read, Access::None,
timeLeft );
if( !st.IsOK() )
{
log->Error( UtilityMsg, "Unable to open source %s: %s",
tpcSource.GetURL().c_str(), st.ToStr().c_str() );
XRootDStatus status = dstFile.Close( closeTimeout );
return st;
}
//--------------------------------------------------------------------------
// Do the copy and follow progress
//--------------------------------------------------------------------------
TPCStatusHandler statusHandler;
Semaphore *sem = statusHandler.GetSemaphore();
StatInfo *info = 0;
uint16_t tpcTimeout = 0;
pProperties->Get( "tpcTimeout", tpcTimeout );
st = dstFile.Sync( &statusHandler, tpcTimeout );
if( !st.IsOK() )
{
log->Error( UtilityMsg, "Unable start the copy: %s",
st.ToStr().c_str() );
XRootDStatus statusS = sourceFile.Close( closeTimeout );
XRootDStatus statusT = dstFile.Close( closeTimeout );
return st;
}
//--------------------------------------------------------------------------
// Stat the file every second until sync returns
//--------------------------------------------------------------------------
bool canceled = false;
while( 1 )
{
XrdSysTimer::Wait( 1000 );
if( progress )
{
st = dstFile.Stat( true, info );
if( st.IsOK() )
{
progress->JobProgress( pJobId, info->GetSize(), sourceSize );
delete info;
info = 0;
}
bool shouldCancel = progress->ShouldCancel( pJobId );
if( shouldCancel )
{
log->Debug( UtilityMsg, "Cancelation requested by progress handler" );
Buffer arg, *response = 0; arg.FromString( "ofs.tpc cancel" );
XRootDStatus st = dstFile.Fcntl( arg, response );
if( !st.IsOK() )
log->Debug( UtilityMsg, "Error while trying to cancel tpc: %s",
st.ToStr().c_str() );
delete response;
canceled = true;
break;
}
}
if( sem->CondWait() )
break;
}
//--------------------------------------------------------------------------
// Sync has returned so we can check if it was successful
//--------------------------------------------------------------------------
if( canceled )
sem->Wait();
st = *statusHandler.GetStatus();
if( !st.IsOK() )
{
log->Error( UtilityMsg, "Third party copy from %s to %s failed: %s",
GetSource().GetURL().c_str(), GetTarget().GetURL().c_str(),
st.ToStr().c_str() );
// Ignore close response
XRootDStatus statusS = sourceFile.Close( closeTimeout );
XRootDStatus statusT = dstFile.Close( closeTimeout );
return st;
}
XRootDStatus statusS = sourceFile.Close( closeTimeout );
XRootDStatus statusT = dstFile.Close( closeTimeout );
if ( !statusS.IsOK() || !statusT.IsOK() )
{
st = (statusS.IsOK() ? statusT : statusS);
log->Error( UtilityMsg, "Third party copy from %s to %s failed during "
"close of %s: %s", GetSource().GetURL().c_str(),
GetTarget().GetURL().c_str(),
(statusS.IsOK() ? "destination" : "source"), st.ToStr().c_str() );
return st;
}
log->Debug( UtilityMsg, "Third party copy from %s to %s successful",
GetSource().GetURL().c_str(), GetTarget().GetURL().c_str() );
pResults->Set( "size", sourceSize );
return XRootDStatus();
}
XRootDStatus ThirdPartyCopyJob::RunLite( CopyProgressHandler *progress )
{
Log *log = DefaultEnv::GetLog();
// Set the close timeout to the default value of the stream timeout
int closeTimeout = 0;
(void) DefaultEnv::GetEnv()->GetInt( "StreamTimeout", closeTimeout);
//--------------------------------------------------------------------------
// Set up the rendez-vous
//--------------------------------------------------------------------------
InitTimeoutCalc timeLeft( initTimeout );
XRootDStatus st = dstFile.Sync( timeLeft );
if( !st.IsOK() )
{
log->Error( UtilityMsg, "Unable set up rendez-vous: %s",
st.ToStr().c_str() );
XRootDStatus status = dstFile.Close( closeTimeout );
return st;
}
//--------------------------------------------------------------------------
// Do the copy and follow progress
//--------------------------------------------------------------------------
TPCStatusHandler statusHandler;
Semaphore *sem = statusHandler.GetSemaphore();
StatInfo *info = 0;
uint16_t tpcTimeout = 0;
pProperties->Get( "tpcTimeout", tpcTimeout );
st = dstFile.Sync( &statusHandler, tpcTimeout );
if( !st.IsOK() )
{
log->Error( UtilityMsg, "Unable start the copy: %s",
st.ToStr().c_str() );
XRootDStatus statusT = dstFile.Close( closeTimeout );
return st;
}
//--------------------------------------------------------------------------
// Stat the file every second until sync returns
//--------------------------------------------------------------------------
bool canceled = false;
while( 1 )
{
XrdSysTimer::Wait( 1000 );
if( progress )
{
st = dstFile.Stat( true, info );
if( st.IsOK() )
{
progress->JobProgress( pJobId, info->GetSize(), sourceSize );
delete info;
info = 0;
}
bool shouldCancel = progress->ShouldCancel( pJobId );
if( shouldCancel )
{
log->Debug( UtilityMsg, "Cancelation requested by progress handler" );
Buffer arg, *response = 0; arg.FromString( "ofs.tpc cancel" );
XRootDStatus st = dstFile.Fcntl( arg, response );
if( !st.IsOK() )
log->Debug( UtilityMsg, "Error while trying to cancel tpc: %s",
st.ToStr().c_str() );
delete response;
canceled = true;
break;
}
}
if( sem->CondWait() )
break;
}
//--------------------------------------------------------------------------
// Sync has returned so we can check if it was successful
//--------------------------------------------------------------------------
if( canceled )
sem->Wait();
st = *statusHandler.GetStatus();
if( !st.IsOK() )
{
log->Error( UtilityMsg, "Third party copy from %s to %s failed: %s",
GetSource().GetURL().c_str(), GetTarget().GetURL().c_str(),
st.ToStr().c_str() );
// Ignore close response
XRootDStatus statusT = dstFile.Close( closeTimeout );
return st;
}
st = dstFile.Close( closeTimeout );
if ( !st.IsOK() )
{
log->Error( UtilityMsg, "Third party copy from %s to %s failed during "
"close of %s: %s", GetSource().GetURL().c_str(),
GetTarget().GetURL().c_str(),
"destination", st.ToStr().c_str() );
return st;
}
log->Debug( UtilityMsg, "Third party copy from %s to %s successful",
GetSource().GetURL().c_str(), GetTarget().GetURL().c_str() );
pResults->Set( "size", sourceSize );
return XRootDStatus();
}
//----------------------------------------------------------------------------
// Generate a rendez-vous key
//----------------------------------------------------------------------------
std::string ThirdPartyCopyJob::GenerateKey()
{
static const int _10to9 = 1000000000;
char tpcKey[25];
auto tp = std::chrono::high_resolution_clock::now();
auto d = tp.time_since_epoch();
auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>( d );
auto s = std::chrono::duration_cast<std::chrono::seconds>( d );
uint32_t k1 = ns.count() - s.count() * _10to9;
uint32_t k2 = getpid() | (getppid() << 16);
uint32_t k3 = s.count();
snprintf( tpcKey, 25, "%08x%08x%08x", k1, k2, k3 );
return std::string(tpcKey);
}
}