Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add bk_download bs config, audit; fix some warning #222

Merged
merged 1 commit into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ Default configure file `overlaybd.json` is installed to `/etc/overlaybd/`.
| download.delay | The seconds waiting to start downloading task after the overlaybd device launched. |
| download.delayExtra | A random extra delay is attached to delay, avoiding too many tasks started at the same time. |
| download.maxMBps | The speed limit in MB/s for a downloading task. |
| download.blockSize | The download block size from source, in byte. `262144` is default (256 KB). |
| p2pConfig.enable | Whether p2p proxy is enabled or not. |
| p2pConfig.address | The proxy for p2p download, the format is `localhost:<P2PConfig.Port>/<P2PConfig.APIKey>`, depending on dadip2p.yaml |
| exporterConfig.enable | whether or not create a server to show Prometheus metrics. |
Expand Down
10 changes: 8 additions & 2 deletions src/bk_download.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <sys/file.h>
#include <photon/common/alog.h>
#include <photon/common/alog-stdstring.h>
#include <photon/common/alog-audit.h>
#include <photon/fs/localfs.h>
#include <photon/fs/throttled-file.h>
#include <photon/thread/thread.h>
Expand Down Expand Up @@ -185,7 +186,7 @@ bool BkDownload::download_blob() {
DEFER(delete dst;);
dst->ftruncate(file_size);

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

LOG_INFO("download blob start. (`)", url);
while (offset < file_size) {
if (running != 1) {
LOG_INFO("image file exit when background downloading");
Expand All @@ -216,7 +218,11 @@ bool BkDownload::download_blob() {
again_read:
if (!(retry--))
LOG_ERROR_RETURN(EIO, false, "failed to read at ", VALUE(offset), VALUE(count));
auto rlen = src->pread(buff, bs, offset);
ssize_t rlen;
{
SCOPE_AUDIT("bk_download", AU_FILEOP(url, offset, rlen));
rlen = src->pread(buff, bs, offset);
}
if (rlen < 0) {
LOG_WARN("failed to read at ", VALUE(offset), VALUE(count), VALUE(errno), " retry...");
goto again_read;
Expand Down
18 changes: 10 additions & 8 deletions src/bk_download.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ class BkDownload {
delete src_file;
}
BkDownload(ISwitchFile *sw_file, photon::fs::IFile *src_file, size_t file_size,
const std::string dir, int32_t limit_MB_ps, int32_t try_cnt, ImageFile *image_file,
std::string digest, int &running)
: sw_file(sw_file), src_file(src_file), file_size(file_size), dir(dir),
limit_MB_ps(limit_MB_ps), try_cnt(try_cnt), image_file(image_file), digest(digest), running(running) {
const std::string &dir, const std::string &digest, const std::string &url,
int &running, int32_t limit_MB_ps, int32_t try_cnt, uint32_t bs)
: dir(dir), try_cnt(try_cnt), sw_file(sw_file), src_file(src_file),
file_size(file_size), digest(digest), url(url), running(running),
limit_MB_ps(limit_MB_ps), block_size(bs) {
}

private:
Expand All @@ -55,12 +56,13 @@ class BkDownload {

ISwitchFile *sw_file = nullptr;
photon::fs::IFile *src_file = nullptr;
int32_t limit_MB_ps;
ImageFile *image_file;
std::string digest;
size_t file_size;
bool force_download = false;
std::string digest;
std::string url;
int &running;
int32_t limit_MB_ps;
uint32_t block_size;
bool force_download = false;
};

void bk_download_proc(std::list<BKDL::BkDownload *> &, uint64_t, int &);
Expand Down
1 change: 1 addition & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ struct DownloadConfig : public ConfigUtils::Config {
APPCFG_PARA(delayExtra, int, 30);
APPCFG_PARA(maxMBps, int, 100);
APPCFG_PARA(tryCnt, int, 5);
APPCFG_PARA(blockSize, uint32_t, 262144);
};

struct ImageConfig : public ConfigUtils::Config {
Expand Down
13 changes: 5 additions & 8 deletions src/image_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ IFile *ImageFile::__open_ro_target_file(const std::string &path) {
IFile *ImageFile::__open_ro_target_remote(const std::string &dir, const std::string &data_digest,
const uint64_t size, int layer_index) {
std::string url;
int64_t extra_range, rand_wait;

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

if (conf.repoBlobUrl() == "") {
set_failed("empty repoBlobUrl");
Expand Down Expand Up @@ -184,8 +182,8 @@ IFile *ImageFile::__open_ro_remote(const std::string &dir, const std::string &di
LOG_WARN("failed to open source file, ignore download");
} else {
BKDL::BkDownload *obj =
new BKDL::BkDownload(switch_file, srcfile, size, dir, conf.download().maxMBps(),
conf.download().tryCnt(), this, digest, m_status);
new BKDL::BkDownload(switch_file, srcfile, size, dir, digest, url, m_status,
conf.download().maxMBps(), conf.download().tryCnt(), conf.download().blockSize());
LOG_DEBUG("add to download list for `", dir);
dl_list.push_back(obj);
}
Expand All @@ -203,16 +201,17 @@ void ImageFile::start_bk_dl_thread() {
uint64_t extra_range = conf.download().delayExtra();
extra_range = (extra_range <= 0) ? 30 : extra_range;
uint64_t delay_sec = (rand() % extra_range) + conf.download().delay();

LOG_INFO("background download is enabled, delay `, maxMBps `, tryCnt `, blockSize `",
delay_sec, conf.download().maxMBps(), conf.download().tryCnt(), conf.download().blockSize());
dl_thread_jh = photon::thread_enable_join(
photon::thread_create11(&BKDL::bk_download_proc, dl_list, delay_sec, m_status));
}

struct ParallelOpenTask {
std::vector<IFile *> &files;
int eno = 0;
std::vector<ImageConfigNS::LayerConfig> &layers;
int i = 0, nlayers;
std::vector<ImageConfigNS::LayerConfig> &layers;

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

int dafa_file_flags = O_RDWR;

data_file = new_sure_file_by_path(upper.data().c_str(), O_RDWR, this);
if (!data_file) {
LOG_ERROR("open(`,flags), `:`", upper.data(), errno, strerror(errno));
Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ ImageService *imgservice = nullptr;

class TCMULoop {
protected:
EventLoop *loop;
struct tcmulib_context *ctx;
EventLoop *loop;
int fd;

int wait_for_readable(EventLoop *) {
Expand Down Expand Up @@ -232,8 +232,8 @@ void *handle(void *args) {

class TCMUDevLoop {
protected:
EventLoop *loop;
struct tcmu_device *dev;
EventLoop *loop;
int fd;
photon::ThreadPool<32> threadpool;

Expand Down
2 changes: 1 addition & 1 deletion src/sure_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class SureFile : public ForwardFile_Ownership {
if (got_cnt == count)
return count;

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