xrootd
XrdFileCacheFile.hh
Go to the documentation of this file.
1#ifndef __XRDFILECACHE_FILE_HH__
2#define __XRDFILECACHE_FILE_HH__
3//----------------------------------------------------------------------------------
4// Copyright (c) 2014 by Board of Trustees of the Leland Stanford, Jr., University
5// Author: Alja Mrak-Tadel, Matevz Tadel
6//----------------------------------------------------------------------------------
7// XRootD is free software: you can redistribute it and/or modify
8// it under the terms of the GNU Lesser General Public License as published by
9// the Free Software Foundation, either version 3 of the License, or
10// (at your option) any later version.
11//
12// XRootD is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU Lesser General Public License
18// along with XRootD. If not, see <http://www.gnu.org/licenses/>.
19//----------------------------------------------------------------------------------
20
23
25#include "XrdOuc/XrdOucIOVec.hh"
26
27#include "XrdFileCacheInfo.hh"
28#include "XrdFileCacheStats.hh"
29
30#include <string>
31#include <map>
32
33class XrdJob;
34class XrdOucIOVec;
35
36namespace XrdCl
37{
38class Log;
39}
40
41namespace XrdFileCache
42{
43class BlockResponseHandler;
44class DirectResponseHandler;
45class IO;
46
47struct ReadVBlockListRAM;
48struct ReadVChunkListRAM;
49struct ReadVBlockListDisk;
50struct ReadVChunkListDisk;
51}
52
53
54namespace XrdFileCache
55{
56
57class File;
58
59class Block
60{
61public:
62 std::vector<char> m_buff;
63 long long m_offset;
65 IO *m_io; // IO that handled current request, used for == / != comparisons only
66
68 int m_errno; // stores negative errno
71
72 Block(File *f, IO *io, long long off, int size, bool m_prefetch) :
73 m_offset(off), m_file(f), m_io(io), m_refcnt(0),
75 {
76 m_buff.resize(size);
77 }
78
79 char* get_buff(long long pos = 0) { return &m_buff[pos]; }
80 int get_size() { return (int) m_buff.size(); }
81 long long get_offset() { return m_offset; }
82
83 IO* get_io() const { return m_io; }
84
85 bool is_finished() { return m_downloaded || m_errno != 0; }
86 bool is_ok() { return m_downloaded; }
87 bool is_failed() { return m_errno != 0; }
88
89 void set_downloaded() { m_downloaded = true; }
90 void set_error(int err) { m_errno = err; }
91
93 {
94 m_errno = 0;
95 m_io = io;
96 }
97};
98
99// ================================================================
100
102{
103public:
106
107 BlockResponseHandler(Block *b, bool prefetch) :
108 m_block(b), m_for_prefetch(prefetch) {}
109
110 virtual void Done(int result);
111};
112
113// ================================================================
114
116{
117public:
121
122 DirectResponseHandler(int to_wait) : m_cond(0), m_to_wait(to_wait), m_errno(0) {}
123
124 bool is_finished() { XrdSysCondVarHelper _lck(m_cond); return m_to_wait == 0; }
125 bool is_ok() { XrdSysCondVarHelper _lck(m_cond); return m_to_wait == 0 && m_errno == 0; }
126 bool is_failed() { XrdSysCondVarHelper _lck(m_cond); return m_errno != 0; }
127
128 virtual void Done(int result);
129};
130
131// ================================================================
132
133class File
134{
135public:
136 //------------------------------------------------------------------------
138 //------------------------------------------------------------------------
139 File(const std::string &path, long long offset, long long fileSize);
140
141 //------------------------------------------------------------------------
143 //------------------------------------------------------------------------
144 static File* FileOpen(const std::string &path, long long offset, long long fileSize);
145
146 //------------------------------------------------------------------------
148 //------------------------------------------------------------------------
150
153
155 void BlocksRemovedFromWriteQ(std::list<Block*>&);
156
158 bool Open();
159
161 int ReadV(IO *io, const XrdOucIOVec *readV, int n);
162
164 int Read (IO *io, char* buff, long long offset, int size);
165
166 //----------------------------------------------------------------------
168 //----------------------------------------------------------------------
169 bool isOpen() const { return m_is_open; }
170
171 //----------------------------------------------------------------------
174 //----------------------------------------------------------------------
175 bool ioActive(IO *io);
176
177 //----------------------------------------------------------------------
180 //----------------------------------------------------------------------
182
183 //----------------------------------------------------------------------
186 //----------------------------------------------------------------------
188
189 //----------------------------------------------------------------------
191 //----------------------------------------------------------------------
192 void Sync();
193
194 //----------------------------------------------------------------------
196 //----------------------------------------------------------------------
197 Stats& GetStats() { return m_stats; }
198
201
202 void Prefetch();
203
204 float GetPrefetchScore() const;
205
207 const char* lPath() const;
208
209 std::string& GetLocalPath() { return m_filename; }
210
213
214 long long GetFileSize() { return m_fileSize; }
215
216 void AddIO(IO *io);
219 void RemoveIO(IO *io);
220
221 //========================================================================
222 // The following methods are called under Cache's m_active lock
223 //========================================================================
224
225 int get_ref_cnt() { return m_ref_cnt; }
226 int inc_ref_cnt() { return ++m_ref_cnt; }
227 int dec_ref_cnt() { return --m_ref_cnt; }
228
231
232private:
234
236
239
243
244 std::string m_filename;
245 long long m_offset;
246 long long m_fileSize;
247
248 // IO objects attached to this file.
249
251 {
255
257 };
258
259 typedef std::map<IO*, IODetails> IoMap_t;
260 typedef IoMap_t::iterator IoMap_i;
261
265
266 // fsync
267 std::vector<int> m_writes_during_sync;
270
271 typedef std::list<int> IntList_t;
272 typedef IntList_t::iterator IntList_i;
273
274 typedef std::list<Block*> BlockList_t;
275 typedef BlockList_t::iterator BlockList_i;
276
277 typedef std::map<int, Block*> BlockMap_t;
278 typedef BlockMap_t::iterator BlockMap_i;
279
280
282
284
286
288
291 float m_prefetchScore; // cached
292
294
295 static const char *m_traceID;
296 bool overlap(int blk, // block to query
297 long long blk_size, //
298 long long req_off, // offset of user request
299 int req_size, // size of user request
300 // output:
301 long long &off, // offset in user buffer
302 long long &blk_off, // offset in block
303 long long &size);
304
305 // Read
306 Block* PrepareBlockRequest(int i, IO *io, bool prefetch);
307
308 void ProcessBlockRequest (Block *b, bool prefetch);
309 void ProcessBlockRequests(BlockList_t& blks, bool prefetch);
310
312 char* buff, long long req_off, long long req_size);
313
315 char* req_buf, long long req_off, long long req_size);
316
317 // VRead
318 bool VReadValidate (const XrdOucIOVec *readV, int n);
319 void VReadPreProcess (IO *io, const XrdOucIOVec *readV, int n,
320 BlockList_t& blks_to_request,
321 ReadVBlockListRAM& blks_to_process,
322 ReadVBlockListDisk& blks_on_disk,
323 std::vector<XrdOucIOVec>& chunkVec);
324 int VReadFromDisk (const XrdOucIOVec *readV, int n,
325 ReadVBlockListDisk& blks_on_disk);
326 int VReadProcessBlocks(IO *io, const XrdOucIOVec *readV, int n,
327 std::vector<ReadVChunkListRAM>& blks_to_process,
328 std::vector<ReadVChunkListRAM>& blks_rocessed);
329
330 long long BufferSize();
331
335
337
338 int offsetIdx(int idx);
339};
340
341}
342
343#endif
Definition: XrdFileCacheFile.hh:102
virtual void Done(int result)
bool m_for_prefetch
Definition: XrdFileCacheFile.hh:105
Block * m_block
Definition: XrdFileCacheFile.hh:104
BlockResponseHandler(Block *b, bool prefetch)
Definition: XrdFileCacheFile.hh:107
Definition: XrdFileCacheFile.hh:60
IO * m_io
Definition: XrdFileCacheFile.hh:65
Block(File *f, IO *io, long long off, int size, bool m_prefetch)
Definition: XrdFileCacheFile.hh:72
void set_downloaded()
Definition: XrdFileCacheFile.hh:89
long long m_offset
Definition: XrdFileCacheFile.hh:63
int get_size()
Definition: XrdFileCacheFile.hh:80
long long get_offset()
Definition: XrdFileCacheFile.hh:81
int m_refcnt
Definition: XrdFileCacheFile.hh:67
bool is_failed()
Definition: XrdFileCacheFile.hh:87
bool m_prefetch
Definition: XrdFileCacheFile.hh:70
bool is_finished()
Definition: XrdFileCacheFile.hh:85
void set_error(int err)
Definition: XrdFileCacheFile.hh:90
File * m_file
Definition: XrdFileCacheFile.hh:64
void reset_error_and_set_io(IO *io)
Definition: XrdFileCacheFile.hh:92
bool m_downloaded
Definition: XrdFileCacheFile.hh:69
int m_errno
Definition: XrdFileCacheFile.hh:68
char * get_buff(long long pos=0)
Definition: XrdFileCacheFile.hh:79
std::vector< char > m_buff
Definition: XrdFileCacheFile.hh:62
IO * get_io() const
Definition: XrdFileCacheFile.hh:83
bool is_ok()
Definition: XrdFileCacheFile.hh:86
Definition: XrdFileCacheFile.hh:116
DirectResponseHandler(int to_wait)
Definition: XrdFileCacheFile.hh:122
int m_to_wait
Definition: XrdFileCacheFile.hh:119
bool is_failed()
Definition: XrdFileCacheFile.hh:126
XrdSysCondVar m_cond
Definition: XrdFileCacheFile.hh:118
bool is_ok()
Definition: XrdFileCacheFile.hh:125
virtual void Done(int result)
bool is_finished()
Definition: XrdFileCacheFile.hh:124
int m_errno
Definition: XrdFileCacheFile.hh:120
Definition: XrdFileCacheFile.hh:134
void StopPrefetchingOnIO(IO *io)
BlockList_t::iterator BlockList_i
Definition: XrdFileCacheFile.hh:275
int ReadBlocksFromDisk(IntList_t &blocks, char *req_buf, long long req_off, long long req_size)
int m_prefetchHitCnt
Definition: XrdFileCacheFile.hh:290
const char * lPath() const
Log path.
bool m_in_shutdown
file is in emergency shutdown due to irrecoverable error or unlink request
Definition: XrdFileCacheFile.hh:238
void dec_ref_count(Block *)
bool m_in_sync
Definition: XrdFileCacheFile.hh:269
static const char * m_traceID
Definition: XrdFileCacheFile.hh:295
void initiate_emergency_shutdown()
bool Open()
Open file handle for data file and info file on local disk.
~File()
Destructor.
Info m_cfi
download status of file blocks and access statistics
Definition: XrdFileCacheFile.hh:242
long long GetFileSize()
Definition: XrdFileCacheFile.hh:214
int ReadV(IO *io, const XrdOucIOVec *readV, int n)
Vector read from disk if block is already downloaded, else ReadV from client.
std::list< Block * > BlockList_t
Definition: XrdFileCacheFile.hh:274
bool VReadValidate(const XrdOucIOVec *readV, int n)
XrdSysTrace * GetTrace()
void RemoveIO(IO *io)
bool ioActive(IO *io)
Initiate close. Return true if still IO active. Used in XrdPosixXrootd::Close()
int offsetIdx(int idx)
int m_ios_in_detach
Number of IO objects to which we replied false to ioActive() and will be removed soon.
Definition: XrdFileCacheFile.hh:264
void AddIO(IO *io)
XrdOssDF * m_output
file handle for data file on disk
Definition: XrdFileCacheFile.hh:240
bool overlap(int blk, long long blk_size, long long req_off, int req_size, long long &off, long long &blk_off, long long &size)
File(const std::string &path, long long offset, long long fileSize)
Constructor.
int get_ref_cnt()
Definition: XrdFileCacheFile.hh:225
bool FinalizeSyncBeforeExit()
Returns true if any of blocks need sync. Called from Cache::dec_ref_cnt on zero ref cnt.
IntList_t::iterator IntList_i
Definition: XrdFileCacheFile.hh:272
int VReadProcessBlocks(IO *io, const XrdOucIOVec *readV, int n, std::vector< ReadVChunkListRAM > &blks_to_process, std::vector< ReadVChunkListRAM > &blks_rocessed)
int m_prefetchReadCnt
Definition: XrdFileCacheFile.hh:289
std::string m_filename
filename of data file on disk
Definition: XrdFileCacheFile.hh:244
int dec_ref_cnt()
Definition: XrdFileCacheFile.hh:227
void ProcessBlockRequest(Block *b, bool prefetch)
XrdSysError * GetLog()
void BlocksRemovedFromWriteQ(std::list< Block * > &)
Handle removal of a set of blocks from Cache's write queue.
Block * PrepareBlockRequest(int i, IO *io, bool prefetch)
int m_non_flushed_cnt
Definition: XrdFileCacheFile.hh:268
PrefetchState_e
Definition: XrdFileCacheFile.hh:233
@ kOff
Definition: XrdFileCacheFile.hh:233
@ kComplete
Definition: XrdFileCacheFile.hh:233
@ kHold
Definition: XrdFileCacheFile.hh:233
@ kOn
Definition: XrdFileCacheFile.hh:233
@ kStopped
Definition: XrdFileCacheFile.hh:233
bool m_detachTimeIsLogged
Definition: XrdFileCacheFile.hh:293
IoMap_t m_io_map
Definition: XrdFileCacheFile.hh:262
bool select_current_io_or_disable_prefetching(bool skip_current)
int VReadFromDisk(const XrdOucIOVec *readV, int n, ReadVBlockListDisk &blks_on_disk)
XrdSysCondVar m_downloadCond
Definition: XrdFileCacheFile.hh:283
Stats & GetStats()
Reference to prefetch statistics.
Definition: XrdFileCacheFile.hh:197
float GetPrefetchScore() const
BlockMap_t::iterator BlockMap_i
Definition: XrdFileCacheFile.hh:278
PrefetchState_e m_prefetchState
Definition: XrdFileCacheFile.hh:287
std::vector< int > m_writes_during_sync
Definition: XrdFileCacheFile.hh:267
void ProcessBlockRequests(BlockList_t &blks, bool prefetch)
std::string & GetLocalPath()
Definition: XrdFileCacheFile.hh:209
std::map< IO *, IODetails > IoMap_t
Definition: XrdFileCacheFile.hh:259
int inc_ref_cnt()
Definition: XrdFileCacheFile.hh:226
bool isOpen() const
Data and cinfo files are open.
Definition: XrdFileCacheFile.hh:169
long long BufferSize()
void ProcessBlockResponse(BlockResponseHandler *brh, int res)
IoMap_t::iterator IoMap_i
Definition: XrdFileCacheFile.hh:260
Stats m_stats
cache statistics, used in IO detach
Definition: XrdFileCacheFile.hh:285
static File * FileOpen(const std::string &path, long long offset, long long fileSize)
Static constructor that also does Open. Returns null ptr if Open fails.
int GetPrefetchCountOnIO(IO *io)
int m_ref_cnt
number of references from IO or sync
Definition: XrdFileCacheFile.hh:235
BlockMap_t m_block_map
Definition: XrdFileCacheFile.hh:281
long long m_fileSize
size of cached disk file for block-based operation
Definition: XrdFileCacheFile.hh:246
int Read(IO *io, char *buff, long long offset, int size)
Normal read.
IoMap_i m_current_io
IO object to be used for prefetching.
Definition: XrdFileCacheFile.hh:263
std::map< int, Block * > BlockMap_t
Definition: XrdFileCacheFile.hh:277
XrdOssDF * m_infoFile
file handle for data-info file on disk
Definition: XrdFileCacheFile.hh:241
bool m_is_open
open state (presumably not needed anymore)
Definition: XrdFileCacheFile.hh:237
void free_block(Block *)
void BlockRemovedFromWriteQ(Block *)
Handle removal of a block from Cache's write queue.
int RequestBlocksDirect(IO *io, DirectResponseHandler *handler, IntList_t &blocks, char *buff, long long req_off, long long req_size)
void inc_ref_count(Block *)
void RequestSyncOfDetachStats()
Flags that detach stats should be written out in final sync. Called from CacheIO upon Detach.
bool is_in_emergency_shutdown()
Definition: XrdFileCacheFile.hh:230
std::list< int > IntList_t
Definition: XrdFileCacheFile.hh:271
void Sync()
Sync file cache inf o and output data with disk.
long long m_offset
offset of cached file for block-based / hdfs operation
Definition: XrdFileCacheFile.hh:245
void WriteBlockToDisk(Block *b)
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)
float m_prefetchScore
Definition: XrdFileCacheFile.hh:291
Base cache-io class that implements XrdOucCacheIO abstract methods.
Definition: XrdFileCacheIO.hh:17
Status of cached file. Can be read from and written into a binary file.
Definition: XrdFileCacheInfo.hh:49
Statistics of disk cache utilisation.
Definition: XrdFileCacheStats.hh:31
Definition: XrdJob.hh:43
Definition: XrdOss.hh:60
Definition: XrdOucCache.hh:94
Definition: XrdSysPthread.hh:130
Definition: XrdSysPthread.hh:79
Definition: XrdSysError.hh:90
Definition: XrdSysTrace.hh:49
Definition: XrdClAnyObject.hh:26
Definition: XrdFileCache.hh:40
Definition: XrdFileCacheFile.hh:251
int m_active_prefetches
Definition: XrdFileCacheFile.hh:252
IODetails()
Definition: XrdFileCacheFile.hh:256
bool m_allow_prefetching
Definition: XrdFileCacheFile.hh:253
bool m_ioactive_false_reported
Definition: XrdFileCacheFile.hh:254
Definition: XrdOucIOVec.hh:41