-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdClXCpCtx.cc
199 lines (169 loc) · 5.16 KB
/
XrdClXCpCtx.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
//------------------------------------------------------------------------------
// Copyright (c) 2011-2017 by European Organization for Nuclear Research (CERN)
// Author: Michal Simon <michal.simon@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/XrdClXCpCtx.hh"
#include "XrdCl/XrdClXCpSrc.hh"
#include "XrdCl/XrdClLog.hh"
#include "XrdCl/XrdClDefaultEnv.hh"
#include "XrdCl/XrdClConstants.hh"
#include <algorithm>
namespace XrdCl
{
XCpCtx::XCpCtx( const std::vector<std::string> &urls, uint64_t blockSize, uint8_t parallelSrc, uint64_t chunkSize, uint64_t parallelChunks, int64_t fileSize ) :
pUrls( std::deque<std::string>( urls.begin(), urls.end() ) ), pBlockSize( blockSize ),
pParallelSrc( parallelSrc ), pChunkSize( chunkSize ), pParallelChunks( parallelChunks ),
pOffset( 0 ), pFileSize( -1 ), pFileSizeCV( 0 ), pDataReceived( 0 ), pDone( false ),
pDoneCV( 0 ), pRefCount( 1 )
{
SetFileSize( fileSize );
}
XCpCtx::~XCpCtx()
{
// at this point there's no concurrency
// this object dies as the last one
while( !pSink.IsEmpty() )
{
ChunkInfo *chunk = pSink.Get();
if( chunk )
XCpSrc::DeleteChunk( chunk );
}
}
bool XCpCtx::GetNextUrl( std::string & url )
{
XrdSysMutexHelper lck( pMtx );
if( pUrls.empty() ) return false;
url = pUrls.front();
pUrls.pop();
return true;
}
XCpSrc* XCpCtx::WeakestLink( XCpSrc *exclude )
{
uint64_t transferRate = -1; // set transferRate to max uint64 value
XCpSrc *ret = 0;
std::list<XCpSrc*>::iterator itr;
for( itr = pSources.begin() ; itr != pSources.end() ; ++itr )
{
XCpSrc *src = *itr;
if( src == exclude ) continue;
uint64_t tmp = src->TransferRate();
if( src->HasData() && tmp < transferRate )
{
ret = src;
transferRate = tmp;
}
}
return ret;
}
void XCpCtx::PutChunk( ChunkInfo* chunk )
{
pSink.Put( chunk );
}
std::pair<uint64_t, uint64_t> XCpCtx::GetBlock()
{
XrdSysMutexHelper lck( pMtx );
uint64_t blkSize = pBlockSize, offset = pOffset;
if( pOffset + blkSize > uint64_t( pFileSize ) )
blkSize = pFileSize - pOffset;
pOffset += blkSize;
return std::make_pair( offset, blkSize );
}
void XCpCtx::SetFileSize( int64_t size )
{
XrdSysMutexHelper lck( pMtx );
if( pFileSize < 0 && size >= 0 )
{
XrdSysCondVarHelper lck( pFileSizeCV );
pFileSize = size;
pFileSizeCV.Broadcast();
if( pBlockSize > uint64_t( pFileSize ) / pParallelSrc )
pBlockSize = pFileSize / pParallelSrc;
if( pBlockSize < pChunkSize )
pBlockSize = pChunkSize;
}
}
XRootDStatus XCpCtx::Initialize()
{
for( uint8_t i = 0; i < pParallelSrc; ++i )
{
XCpSrc *src = new XCpSrc( pChunkSize, pParallelChunks, pFileSize, this );
pSources.push_back( src );
src->Start();
}
if( pSources.empty() )
{
Log *log = DefaultEnv::GetLog();
log->Error( UtilityMsg, "Failed to initialize (failed to create new threads)" );
return XRootDStatus( stError, errInternal, EAGAIN, "XCpCtx: failed to create new threads." );
}
return XRootDStatus();
}
XRootDStatus XCpCtx::GetChunk( XrdCl::ChunkInfo &ci )
{
// if we received all the data we are done here
if( pDataReceived == uint64_t( pFileSize ) )
{
XrdSysCondVarHelper lck( pDoneCV );
pDone = true;
pDoneCV.Broadcast();
return XRootDStatus( stOK, suDone );
}
// if we don't have active sources it means we failed
if( GetRunning() == 0 )
{
XrdSysCondVarHelper lck( pDoneCV );
pDone = true;
pDoneCV.Broadcast();
return XRootDStatus( stError, errNoMoreReplicas );
}
ChunkInfo *chunk = pSink.Get();
if( chunk )
{
pDataReceived += chunk->length;
ci = *chunk;
delete chunk;
return XRootDStatus( stOK, suContinue );
}
return XRootDStatus( stOK, suRetry );
}
void XCpCtx::NotifyIdleSrc()
{
pDoneCV.Broadcast();
}
bool XCpCtx::AllDone()
{
XrdSysCondVarHelper lck( pDoneCV );
if( !pDone )
pDoneCV.Wait( 60 );
return pDone;
}
size_t XCpCtx::GetRunning()
{
// count active sources
size_t nbRunning = 0;
std::list<XCpSrc*>::iterator itr;
for( itr = pSources.begin() ; itr != pSources.end() ; ++ itr)
if( (*itr)->IsRunning() )
++nbRunning;
return nbRunning;
}
} /* namespace XrdCl */