-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdPfcFile.hh
371 lines (278 loc) · 12.7 KB
/
XrdPfcFile.hh
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
#ifndef __XRDPFC_FILE_HH__
#define __XRDPFC_FILE_HH__
//----------------------------------------------------------------------------------
// Copyright (c) 2014 by Board of Trustees of the Leland Stanford, Jr., University
// Author: Alja Mrak-Tadel, Matevz Tadel
//----------------------------------------------------------------------------------
// 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 "XrdCl/XrdClXRootDResponses.hh"
#include "XrdCl/XrdClDefaultEnv.hh"
#include "XrdOuc/XrdOucCache.hh"
#include "XrdOuc/XrdOucIOVec.hh"
#include "XrdPfcInfo.hh"
#include "XrdPfcStats.hh"
#include <string>
#include <map>
#include <set>
class XrdJob;
class XrdOucIOVec;
namespace XrdCl
{
class Log;
}
namespace XrdPfc
{
class BlockResponseHandler;
class DirectResponseHandler;
class IO;
struct ReadVBlockListRAM;
struct ReadVChunkListRAM;
struct ReadVBlockListDisk;
struct ReadVChunkListDisk;
}
namespace XrdPfc
{
class File;
class Block
{
public:
File *m_file;
IO *m_io; // IO that handled current request, used for == / != comparisons only
char *m_buff;
long long m_offset;
int m_size;
int m_req_size;
int m_refcnt;
int m_errno; // stores negative errno
bool m_downloaded;
bool m_prefetch;
bool m_req_cksum_net;
vCkSum_t m_cksum_vec;
Block(File *f, IO *io, char *buf, long long off, int size, int rsize, bool m_prefetch, bool cks_net) :
m_file(f), m_io(io), m_buff(buf), m_offset(off), m_size(size), m_req_size(rsize),
m_refcnt(0), m_errno(0), m_downloaded(false), m_prefetch(m_prefetch),
m_req_cksum_net(cks_net)
{}
char* get_buff() { return m_buff; }
int get_size() { return m_size; }
int get_req_size() { return m_req_size; }
long long get_offset() { return m_offset; }
IO* get_io() const { return m_io; }
bool is_finished() { return m_downloaded || m_errno != 0; }
bool is_ok() { return m_downloaded; }
bool is_failed() { return m_errno != 0; }
void set_downloaded() { m_downloaded = true; }
void set_error(int err) { m_errno = err; }
void reset_error_and_set_io(IO *io)
{
m_errno = 0;
m_io = io;
}
bool req_cksum_net() const { return m_req_cksum_net; }
bool has_cksums() const { return ! m_cksum_vec.empty(); }
vCkSum_t& ref_cksum_vec() { return m_cksum_vec; }
};
// ================================================================
class BlockResponseHandler : public XrdOucCacheIOCB
{
public:
Block *m_block;
BlockResponseHandler(Block *b) : m_block(b) {}
virtual void Done(int result);
};
// ================================================================
class DirectResponseHandler : public XrdOucCacheIOCB
{
public:
XrdSysCondVar m_cond;
int m_to_wait;
int m_errno;
DirectResponseHandler(int to_wait) : m_cond(0), m_to_wait(to_wait), m_errno(0) {}
bool is_finished() { XrdSysCondVarHelper _lck(m_cond); return m_to_wait == 0; }
bool is_ok() { XrdSysCondVarHelper _lck(m_cond); return m_to_wait == 0 && m_errno == 0; }
bool is_failed() { XrdSysCondVarHelper _lck(m_cond); return m_errno != 0; }
virtual void Done(int result);
};
// ================================================================
class File
{
public:
//------------------------------------------------------------------------
//! Constructor.
//------------------------------------------------------------------------
File(const std::string &path, long long offset, long long fileSize);
//------------------------------------------------------------------------
//! Static constructor that also does Open. Returns null ptr if Open fails.
//------------------------------------------------------------------------
static File* FileOpen(const std::string &path, long long offset, long long fileSize);
//------------------------------------------------------------------------
//! Destructor.
//------------------------------------------------------------------------
~File();
//! Handle removal of a block from Cache's write queue.
void BlockRemovedFromWriteQ(Block*);
//! Handle removal of a set of blocks from Cache's write queue.
void BlocksRemovedFromWriteQ(std::list<Block*>&);
//! Open file handle for data file and info file on local disk.
bool Open();
//! Vector read from disk if block is already downloaded, else ReadV from client.
int ReadV(IO *io, const XrdOucIOVec *readV, int n);
//! Normal read.
int Read (IO *io, char* buff, long long offset, int size);
//----------------------------------------------------------------------
//! \brief Data and cinfo files are open.
//----------------------------------------------------------------------
bool isOpen() const { return m_is_open; }
//----------------------------------------------------------------------
//! \brief Notification from IO that it has been updated (remote open).
//----------------------------------------------------------------------
void ioUpdated(IO *io);
//----------------------------------------------------------------------
//! \brief Initiate close. Return true if still IO active.
//! Used in XrdPosixXrootd::Close()
//----------------------------------------------------------------------
bool ioActive(IO *io);
//----------------------------------------------------------------------
//! \brief Flags that detach stats should be written out in final sync.
//! Called from CacheIO upon Detach.
//----------------------------------------------------------------------
void RequestSyncOfDetachStats();
//----------------------------------------------------------------------
//! \brief Returns true if any of blocks need sync.
//! Called from Cache::dec_ref_cnt on zero ref cnt
//----------------------------------------------------------------------
bool FinalizeSyncBeforeExit();
//----------------------------------------------------------------------
//! Sync file cache inf o and output data with disk
//----------------------------------------------------------------------
void Sync();
void ProcessBlockResponse(BlockResponseHandler* brh, int res);
void WriteBlockToDisk(Block* b);
void Prefetch();
float GetPrefetchScore() const;
//! Log path
const char* lPath() const;
std::string& GetLocalPath() { return m_filename; }
XrdSysError* GetLog();
XrdSysTrace* GetTrace();
long long GetFileSize() { return m_file_size; }
void AddIO(IO *io);
int GetPrefetchCountOnIO(IO *io);
void StopPrefetchingOnIO(IO *io);
void RemoveIO(IO *io);
Stats DeltaStatsFromLastCall();
std::string GetRemoteLocations() const;
const Info::AStat* GetLastAccessStats() const { return m_cfi.GetLastAccessStats(); }
size_t GetAccessCnt() const { return m_cfi.GetAccessCnt(); }
int GetBlockSize() const { return m_cfi.GetBufferSize(); }
int GetNBlocks() const { return m_cfi.GetNBlocks(); }
int GetNDownloadedBlocks() const { return m_cfi.GetNDownloadedBlocks(); }
// These three methods are called under Cache's m_active lock
int get_ref_cnt() { return m_ref_cnt; }
int inc_ref_cnt() { return ++m_ref_cnt; }
int dec_ref_cnt() { return --m_ref_cnt; }
void initiate_emergency_shutdown();
bool is_in_emergency_shutdown() { return m_in_shutdown; }
private:
enum PrefetchState_e { kOff=-1, kOn, kHold, kStopped, kComplete };
int m_ref_cnt; //!< number of references from IO or sync
bool m_is_open; //!< open state (presumably not needed anymore)
bool m_in_shutdown; //!< file is in emergency shutdown due to irrecoverable error or unlink request
XrdOssDF *m_data_file; //!< file handle for data file on disk
XrdOssDF *m_info_file; //!< file handle for data-info file on disk
Info m_cfi; //!< download status of file blocks and access statistics
std::string m_filename; //!< filename of data file on disk
long long m_offset; //!< offset of cached file for block-based / hdfs operation
long long m_file_size; //!< size of cached disk file for block-based operation
// IO objects attached to this file.
struct IODetails
{
time_t m_attach_time;
int m_active_prefetches;
bool m_allow_prefetching;
IODetails(time_t at) :
m_attach_time (at),
m_active_prefetches (0),
m_allow_prefetching (true)
{}
};
typedef std::map<IO*, IODetails> IoMap_t;
typedef IoMap_t::iterator IoMap_i;
IoMap_t m_io_map;
IoMap_i m_current_io; //!< IO object to be used for prefetching.
int m_ios_in_detach; //!< Number of IO objects to which we replied false to ioActive() and will be removed soon.
// fsync
std::vector<int> m_writes_during_sync;
int m_non_flushed_cnt;
bool m_in_sync;
typedef std::list<int> IntList_t;
typedef IntList_t::iterator IntList_i;
typedef std::list<Block*> BlockList_t;
typedef BlockList_t::iterator BlockList_i;
typedef std::map<int, Block*> BlockMap_t;
typedef BlockMap_t::iterator BlockMap_i;
typedef std::set<Block*> BlockSet_t;
typedef BlockSet_t::iterator BlockSet_i;
BlockMap_t m_block_map;
XrdSysCondVar m_state_cond;
Stats m_stats; //!< cache statistics for this instance
Stats m_last_stats; //!< copy of cache stats during last purge cycle, used for per directory stat reporting
std::set<std::string> m_remote_locations; //!< Gathered in AddIO / ioUpdate / ioActive.
void insert_remote_location(const std::string &loc);
PrefetchState_e m_prefetch_state;
int m_prefetch_read_cnt;
int m_prefetch_hit_cnt;
float m_prefetch_score; // cached
bool m_detach_time_logged;
static const char *m_traceID;
bool overlap(int blk, // block to query
long long blk_size, //
long long req_off, // offset of user request
int req_size, // size of user request
// output:
long long &off, // offset in user buffer
long long &blk_off, // offset in block
long long &size);
// Read
Block* PrepareBlockRequest(int i, IO *io, bool prefetch);
void ProcessBlockRequest (Block *b);
void ProcessBlockRequests(BlockList_t& blks);
int RequestBlocksDirect(IO *io, DirectResponseHandler *handler, IntList_t& blocks,
char* buff, long long req_off, long long req_size);
int ReadBlocksFromDisk(IntList_t& blocks,
char* req_buf, long long req_off, long long req_size);
// VRead
bool VReadValidate (const XrdOucIOVec *readV, int n);
void VReadPreProcess (IO *io, const XrdOucIOVec *readV, int n,
BlockList_t& blks_to_request,
ReadVBlockListRAM& blks_to_process,
ReadVBlockListDisk& blks_on_disk,
std::vector<XrdOucIOVec>& chunkVec);
int VReadFromDisk (const XrdOucIOVec *readV, int n,
ReadVBlockListDisk& blks_on_disk);
int VReadProcessBlocks(IO *io, const XrdOucIOVec *readV, int n,
std::vector<ReadVChunkListRAM>& blks_to_process,
std::vector<ReadVChunkListRAM>& blks_processed,
long long& bytes_hit,
long long& bytes_missed);
long long BufferSize();
void inc_ref_count(Block*);
void dec_ref_count(Block*);
void free_block(Block*);
bool select_current_io_or_disable_prefetching(bool skip_current);
int offsetIdx(int idx);
};
}
#endif