Skip to content

Commit 99b8f9c

Browse files
authored
Merge pull request #222 from yuchen0cc/main
add bk_download bs config, audit; fix some warning
2 parents 279a7b3 + dfd0d15 commit 99b8f9c

File tree

7 files changed

+28
-21
lines changed

7 files changed

+28
-21
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ Default configure file `overlaybd.json` is installed to `/etc/overlaybd/`.
181181
| download.delay | The seconds waiting to start downloading task after the overlaybd device launched. |
182182
| download.delayExtra | A random extra delay is attached to delay, avoiding too many tasks started at the same time. |
183183
| download.maxMBps | The speed limit in MB/s for a downloading task. |
184+
| download.blockSize | The download block size from source, in byte. `262144` is default (256 KB). |
184185
| p2pConfig.enable | Whether p2p proxy is enabled or not. |
185186
| p2pConfig.address | The proxy for p2p download, the format is `localhost:<P2PConfig.Port>/<P2PConfig.APIKey>`, depending on dadip2p.yaml |
186187
| exporterConfig.enable | whether or not create a server to show Prometheus metrics. |

src/bk_download.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <sys/file.h>
2323
#include <photon/common/alog.h>
2424
#include <photon/common/alog-stdstring.h>
25+
#include <photon/common/alog-audit.h>
2526
#include <photon/fs/localfs.h>
2627
#include <photon/fs/throttled-file.h>
2728
#include <photon/thread/thread.h>
@@ -185,7 +186,7 @@ bool BkDownload::download_blob() {
185186
DEFER(delete dst;);
186187
dst->ftruncate(file_size);
187188

188-
size_t bs = 256 * 1024;
189+
size_t bs = block_size;
189190
off_t offset = 0;
190191
void *buff = nullptr;
191192
// buffer allocate, with 4K alignment
@@ -194,6 +195,7 @@ bool BkDownload::download_blob() {
194195
LOG_ERRNO_RETURN(0, false, "failed to allocate buffer with ", VALUE(bs));
195196
DEFER(free(buff));
196197

198+
LOG_INFO("download blob start. (`)", url);
197199
while (offset < file_size) {
198200
if (running != 1) {
199201
LOG_INFO("image file exit when background downloading");
@@ -216,7 +218,11 @@ bool BkDownload::download_blob() {
216218
again_read:
217219
if (!(retry--))
218220
LOG_ERROR_RETURN(EIO, false, "failed to read at ", VALUE(offset), VALUE(count));
219-
auto rlen = src->pread(buff, bs, offset);
221+
ssize_t rlen;
222+
{
223+
SCOPE_AUDIT("bk_download", AU_FILEOP(url, offset, rlen));
224+
rlen = src->pread(buff, bs, offset);
225+
}
220226
if (rlen < 0) {
221227
LOG_WARN("failed to read at ", VALUE(offset), VALUE(count), VALUE(errno), " retry...");
222228
goto again_read;

src/bk_download.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ class BkDownload {
4242
delete src_file;
4343
}
4444
BkDownload(ISwitchFile *sw_file, photon::fs::IFile *src_file, size_t file_size,
45-
const std::string dir, int32_t limit_MB_ps, int32_t try_cnt, ImageFile *image_file,
46-
std::string digest, int &running)
47-
: sw_file(sw_file), src_file(src_file), file_size(file_size), dir(dir),
48-
limit_MB_ps(limit_MB_ps), try_cnt(try_cnt), image_file(image_file), digest(digest), running(running) {
45+
const std::string &dir, const std::string &digest, const std::string &url,
46+
int &running, int32_t limit_MB_ps, int32_t try_cnt, uint32_t bs)
47+
: dir(dir), try_cnt(try_cnt), sw_file(sw_file), src_file(src_file),
48+
file_size(file_size), digest(digest), url(url), running(running),
49+
limit_MB_ps(limit_MB_ps), block_size(bs) {
4950
}
5051

5152
private:
@@ -55,12 +56,13 @@ class BkDownload {
5556

5657
ISwitchFile *sw_file = nullptr;
5758
photon::fs::IFile *src_file = nullptr;
58-
int32_t limit_MB_ps;
59-
ImageFile *image_file;
60-
std::string digest;
6159
size_t file_size;
62-
bool force_download = false;
60+
std::string digest;
61+
std::string url;
6362
int &running;
63+
int32_t limit_MB_ps;
64+
uint32_t block_size;
65+
bool force_download = false;
6466
};
6567

6668
void bk_download_proc(std::list<BKDL::BkDownload *> &, uint64_t, int &);

src/config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ struct DownloadConfig : public ConfigUtils::Config {
5151
APPCFG_PARA(delayExtra, int, 30);
5252
APPCFG_PARA(maxMBps, int, 100);
5353
APPCFG_PARA(tryCnt, int, 5);
54+
APPCFG_PARA(blockSize, uint32_t, 262144);
5455
};
5556

5657
struct ImageConfig : public ConfigUtils::Config {

src/image_file.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ IFile *ImageFile::__open_ro_target_file(const std::string &path) {
9898
IFile *ImageFile::__open_ro_target_remote(const std::string &dir, const std::string &data_digest,
9999
const uint64_t size, int layer_index) {
100100
std::string url;
101-
int64_t extra_range, rand_wait;
102101

103102
if (conf.repoBlobUrl() == "") {
104103
set_failed("empty repoBlobUrl");
@@ -126,7 +125,6 @@ IFile *ImageFile::__open_ro_target_remote(const std::string &dir, const std::str
126125
IFile *ImageFile::__open_ro_remote(const std::string &dir, const std::string &digest,
127126
const uint64_t size, int layer_index) {
128127
std::string url;
129-
int64_t extra_range, rand_wait;
130128

131129
if (conf.repoBlobUrl() == "") {
132130
set_failed("empty repoBlobUrl");
@@ -184,8 +182,8 @@ IFile *ImageFile::__open_ro_remote(const std::string &dir, const std::string &di
184182
LOG_WARN("failed to open source file, ignore download");
185183
} else {
186184
BKDL::BkDownload *obj =
187-
new BKDL::BkDownload(switch_file, srcfile, size, dir, conf.download().maxMBps(),
188-
conf.download().tryCnt(), this, digest, m_status);
185+
new BKDL::BkDownload(switch_file, srcfile, size, dir, digest, url, m_status,
186+
conf.download().maxMBps(), conf.download().tryCnt(), conf.download().blockSize());
189187
LOG_DEBUG("add to download list for `", dir);
190188
dl_list.push_back(obj);
191189
}
@@ -203,16 +201,17 @@ void ImageFile::start_bk_dl_thread() {
203201
uint64_t extra_range = conf.download().delayExtra();
204202
extra_range = (extra_range <= 0) ? 30 : extra_range;
205203
uint64_t delay_sec = (rand() % extra_range) + conf.download().delay();
206-
204+
LOG_INFO("background download is enabled, delay `, maxMBps `, tryCnt `, blockSize `",
205+
delay_sec, conf.download().maxMBps(), conf.download().tryCnt(), conf.download().blockSize());
207206
dl_thread_jh = photon::thread_enable_join(
208207
photon::thread_create11(&BKDL::bk_download_proc, dl_list, delay_sec, m_status));
209208
}
210209

211210
struct ParallelOpenTask {
212211
std::vector<IFile *> &files;
213212
int eno = 0;
214-
std::vector<ImageConfigNS::LayerConfig> &layers;
215213
int i = 0, nlayers;
214+
std::vector<ImageConfigNS::LayerConfig> &layers;
216215

217216
int get_next_job_index() {
218217
LOG_DEBUG("create job, layer_id: `", i);
@@ -372,8 +371,6 @@ LSMT::IFileRW *ImageFile::open_upper(ImageConfigNS::UpperConfig &upper) {
372371
IFile *target_file = NULL;
373372
LSMT::IFileRW *ret = NULL;
374373

375-
int dafa_file_flags = O_RDWR;
376-
377374
data_file = new_sure_file_by_path(upper.data().c_str(), O_RDWR, this);
378375
if (!data_file) {
379376
LOG_ERROR("open(`,flags), `:`", upper.data(), errno, strerror(errno));

src/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ ImageService *imgservice = nullptr;
5757

5858
class TCMULoop {
5959
protected:
60-
EventLoop *loop;
6160
struct tcmulib_context *ctx;
61+
EventLoop *loop;
6262
int fd;
6363

6464
int wait_for_readable(EventLoop *) {
@@ -232,8 +232,8 @@ void *handle(void *args) {
232232

233233
class TCMUDevLoop {
234234
protected:
235-
EventLoop *loop;
236235
struct tcmu_device *dev;
236+
EventLoop *loop;
237237
int fd;
238238
photon::ThreadPool<32> threadpool;
239239

src/sure_file.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class SureFile : public ForwardFile_Ownership {
9090
if (got_cnt == count)
9191
return count;
9292

93-
if ((ret < 0) && (!m_ifile->m_status < 1) && (errno == EPERM)) {
93+
if ((ret < 0) && (m_ifile->m_status < 1) && (errno == EPERM)) {
9494
// exit when booting. after boot, hang.
9595
m_ifile->set_auth_failed();
9696
LOG_ERROR_RETURN(0, -1, "authentication failed during image boot.");

0 commit comments

Comments
 (0)