-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdFileCacheIOFileBlock.cc
210 lines (182 loc) · 6.92 KB
/
XrdFileCacheIOFileBlock.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
//----------------------------------------------------------------------------------
// Copyright (c) 2014 by Board of Trustees of the Leland Stanford, Jr., University
// Author: Alja Mrak-Tadel, Matevz Tadel, Brian Bockelman
//----------------------------------------------------------------------------------
// 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 <math.h>
#include <sstream>
#include <stdio.h>
#include <iostream>
#include <assert.h>
#include "XrdFileCacheIOFileBlock.hh"
#include "XrdFileCache.hh"
#include "XrdFileCacheStats.hh"
#include "XrdSys/XrdSysError.hh"
#include "XrdSfs/XrdSfsInterface.hh"
using namespace XrdFileCache;
//______________________________________________________________________________
IOFileBlock::IOFileBlock(XrdOucCacheIO2 &io, XrdOucCacheStats &statsGlobal, Cache & cache)
: IO(io, statsGlobal, cache)
{
m_blocksize = Cache::GetInstance().RefConfiguration().m_hdfsbsize;
GetBlockSizeFromPath();
}
//______________________________________________________________________________
XrdOucCacheIO* IOFileBlock::Detach()
{
clLog()->Info(XrdCl::AppMsg, "IOFileBlock::Detach() %s", m_io.Path());
XrdOucCacheIO * io = &m_io;
for (std::map<int, File*>::iterator it = m_blocks.begin(); it != m_blocks.end(); ++it)
{
m_statsGlobal.Add(it->second->GetStats());
delete it->second;
}
m_cache.Detach(this); // This will delete us!
return io;
}
//______________________________________________________________________________
void IOFileBlock::GetBlockSizeFromPath()
{
const static std::string tag = "hdfsbsize=";
std::string path= m_io.Path();
size_t pos1 = path.find(tag);
size_t t = tag.length();
if ( pos1 != path.npos)
{
pos1 += t;
size_t pos2 = path.find("&", pos1 );
if (pos2 != path.npos )
{
std::string bs = path.substr(pos1, pos2 - pos1);
m_blocksize = atoi(bs.c_str());
}
else {
m_blocksize = atoi(path.substr(pos1).c_str());
}
clLog()->Debug(XrdCl::AppMsg, "FileBlock::GetBlockSizeFromPath(), blocksize = %lld. %s", m_blocksize, m_io.Path());
}
}
//______________________________________________________________________________
File* IOFileBlock::newBlockFile(long long off, int blocksize, XrdOucCacheIO2* io)
{
XrdCl::URL url(io->Path());
std::string fname = Cache::GetInstance().RefConfiguration().m_cache_dir + url.GetPath();
std::stringstream ss;
ss << fname;
char offExt[64];
// filename like <origpath>___<size>_<offset>
sprintf(&offExt[0],"___%lld_%lld", m_blocksize, off );
ss << &offExt[0];
fname = ss.str();
clLog()->Debug(XrdCl::AppMsg, "FileBlock::FileBlock(), create XrdFileCacheFile. %s", m_io.Path());
File* prefetch = new File(*io, fname, off, blocksize);
return prefetch;
}
//______________________________________________________________________________
bool IOFileBlock::ioActive()
{
bool res = false;
for (std::map<int, File*>::iterator it = m_blocks.begin(); it != m_blocks.end(); ++it) {
if (it->second->InitiateClose())
res = true;
}
return res;
}
//______________________________________________________________________________
int IOFileBlock::Read (char *buff, long long off, int size)
{
// protect from reads over the file size
if (off >= m_io.FSize())
return 0;
if (off < 0)
{
errno = EINVAL;
return -1;
}
if (off + size > m_io.FSize())
size = m_io.FSize() - off;
long long off0 = off;
int idx_first = off0/m_blocksize;
int idx_last = (off0+size-1)/m_blocksize;
int bytes_read = 0;
clLog()->Debug(XrdCl::AppMsg, "IOFileBlock::Read() %lld@%d block range [%d-%d] \n %s", off, size, idx_first, idx_last, m_io.Path());
for (int blockIdx = idx_first; blockIdx <= idx_last; ++blockIdx )
{
// locate block
File* fb;
m_mutex.Lock();
std::map<int, File*>::iterator it = m_blocks.find(blockIdx);
if ( it != m_blocks.end() )
{
fb = it->second;
}
else
{
size_t pbs = m_blocksize;
// check if this is last block
int lastIOFileBlock = (m_io.FSize()-1)/m_blocksize;
if (blockIdx == lastIOFileBlock )
{
pbs = m_io.FSize() - blockIdx*m_blocksize;
clLog()->Debug(XrdCl::AppMsg, "IOFileBlock::Read() last block, change output file size to %lld \n %s", pbs, m_io.Path());
}
fb = newBlockFile(blockIdx*m_blocksize, pbs, &m_io);
m_blocks.insert(std::pair<int,File*>(blockIdx, (File*) fb));
}
m_mutex.UnLock();
// edit size if read request is reaching more than a block
int readBlockSize = size;
if (idx_first != idx_last)
{
if (blockIdx == idx_first)
{
readBlockSize = (blockIdx + 1) *m_blocksize - off0;
clLog()->Debug(XrdCl::AppMsg, "Read partially till the end of the block %s", m_io.Path());
}
else if (blockIdx == idx_last)
{
readBlockSize = (off0+size) - blockIdx*m_blocksize;
clLog()->Debug(XrdCl::AppMsg, "Read partially from beginning of block %s", m_io.Path());
}
else
{
readBlockSize = m_blocksize;
}
}
assert(readBlockSize);
clLog()->Info(XrdCl::AppMsg, "IOFileBlock::Read() block[%d] read-block-size[%d], offset[%lld] %s", blockIdx, readBlockSize, off, m_io.Path());
long long min = blockIdx*m_blocksize;
if ( off < min) { assert(0); }
assert(off+readBlockSize <= (min + m_blocksize));
int retvalBlock = fb->Read(buff, off, readBlockSize);
clLog()->Debug(XrdCl::AppMsg, "IOFileBlock::Read() Block read returned %d %s", retvalBlock , m_io.Path());
if (retvalBlock == readBlockSize )
{
bytes_read += retvalBlock;
buff += retvalBlock;
off += retvalBlock;
}
else if (retvalBlock > 0) {
clLog()->Warning(XrdCl::AppMsg, "IOFileBlock::Read() incomplete read, missing bytes %d %s", readBlockSize-retvalBlock, m_io.Path());
return bytes_read + retvalBlock;
}
else
{
clLog()->Error(XrdCl::AppMsg, "IOFileBlock::Read() read error, retval %d %s", retvalBlock, m_io.Path());
return retvalBlock;
}
}
return bytes_read;
}