@@ -102,7 +102,7 @@ IFile *ImageFile::__open_ro_target_file(const std::string &path) {
102102}
103103
104104IFile *ImageFile::__open_ro_target_remote (const std::string &dir, const std::string &data_digest,
105- const uint64_t size, int layer_index) {
105+ const uint64_t size, int layer_index) {
106106 std::string url;
107107
108108 if (conf.repoBlobUrl () == " " ) {
@@ -153,8 +153,7 @@ IFile *ImageFile::__open_ro_remote(const std::string &dir, const std::string &di
153153 LOG_ERROR_RETURN (0 , nullptr , " empty repoBlobUrl for remote layer" );
154154 }
155155 estring url = estring ().appends (" /" , conf.repoBlobUrl (),
156- (conf.repoBlobUrl ().back () != ' /' ) ? " /" : " " ,
157- digest);
156+ (conf.repoBlobUrl ().back () != ' /' ) ? " /" : " " , digest);
158157
159158 LOG_INFO (" open file from remotefs: `, size: `" , url, size);
160159 IFile *remote_file = image_service.global_fs .remote_fs ->open (url.c_str (), O_RDONLY);
@@ -164,8 +163,14 @@ IFile *ImageFile::__open_ro_remote(const std::string &dir, const std::string &di
164163 set_failed (" failed to open remote file " , url, " : " , err_msg);
165164 LOG_ERRNO_RETURN (0 , nullptr , " failed to open remote file `: `" , url, err_msg);
166165 }
167- remote_file->ioctl (SET_SIZE, size);
168- remote_file->ioctl (SET_LOCAL_DIR, dir);
166+ if (!dir.empty ()) {
167+ remote_file->ioctl (SET_SIZE, size);
168+ remote_file->ioctl (SET_LOCAL_DIR, dir);
169+ } else {
170+ LOG_WARN (
171+ " local dir of layer %d (%s) didn't set, skip background anyway" ,
172+ layer_index, digest.c_str ());
173+ }
169174
170175 IFile *tar_file = new_tar_file_adaptor (remote_file);
171176 if (!tar_file) {
@@ -183,15 +188,15 @@ IFile *ImageFile::__open_ro_remote(const std::string &dir, const std::string &di
183188 LOG_ERRNO_RETURN (0 , nullptr , " failed to open switch file `" , url);
184189 }
185190
186- if (conf.HasMember (" download" ) && conf.download ().enable () == 1 ) {
191+ if (conf.HasMember (" download" ) && conf.download ().enable () == 1 && !dir. empty () ) {
187192 // download from registry, verify sha256 after downloaded.
188193 IFile *srcfile = image_service.global_fs .srcfs ->open (url.c_str (), O_RDONLY);
189194 if (srcfile == nullptr ) {
190195 LOG_WARN (" failed to open source file, ignore download" );
191196 } else {
192- BKDL::BkDownload *obj =
193- new BKDL::BkDownload ( switch_file, srcfile, size, dir, digest, url, m_status,
194- conf. download (). maxMBps (), conf.download ().tryCnt (), conf.download ().blockSize ());
197+ BKDL::BkDownload *obj = new BKDL::BkDownload (
198+ switch_file, srcfile, size, dir, digest, url, m_status, conf. download (). maxMBps () ,
199+ conf.download ().tryCnt (), conf.download ().blockSize ());
195200 LOG_DEBUG (" add to download list for `" , dir);
196201 dl_list.push_back (obj);
197202 }
@@ -209,8 +214,8 @@ void ImageFile::start_bk_dl_thread() {
209214 uint64_t extra_range = conf.download ().delayExtra ();
210215 extra_range = (extra_range <= 0 ) ? 30 : extra_range;
211216 uint64_t delay_sec = (rand () % extra_range) + conf.download ().delay ();
212- LOG_INFO (" background download is enabled, delay `, maxMBps `, tryCnt `, blockSize `" ,
213- delay_sec, conf.download ().maxMBps (), conf.download ().tryCnt (), conf.download ().blockSize ());
217+ LOG_INFO (" background download is enabled, delay `, maxMBps `, tryCnt `, blockSize `" , delay_sec,
218+ conf.download ().maxMBps (), conf.download ().tryCnt (), conf.download ().blockSize ());
214219 dl_thread_jh = photon::thread_enable_join (
215220 photon::thread_create11 (&BKDL::bk_download_proc, dl_list, delay_sec, m_status));
216221}
@@ -256,30 +261,30 @@ void *do_parallel_open_files(ImageFile *imgfile, ParallelOpenTask &tm) {
256261 return nullptr ;
257262}
258263
259- int ImageFile::open_lower_layer (IFile *&file, ImageConfigNS::LayerConfig &layer,
260- int index) {
261- std::string opened;
264+ IFile *ImageFile::open_localfile (ImageConfigNS::LayerConfig &layer, std::string &opened) {
262265 if (layer.file () != " " ) {
263266 opened = layer.file ();
264- file = __open_ro_file (opened);
265- } else {
266- // open downloaded blob or remote blob
267- if (BKDL::check_downloaded (layer.dir ())) {
268- opened = layer.dir () + " /" + COMMIT_FILE_NAME;
269- file = __open_ro_file (opened);
270- } else {
271- auto sealed = layer.dir () + " /" + SEALED_FILE_NAME;
272- if (::access (sealed.c_str (), 0 ) == 0 ) {
273- // open sealed blob
274- opened = sealed;
275- file = __open_ro_file (opened);
276- } else {
277- opened = layer.digest ();
278- file = __open_ro_remote (layer.dir (), layer.digest (), layer.size (), index);
279- }
280- }
267+ return __open_ro_file (opened);
268+ }
269+ if (BKDL::check_downloaded (layer.dir ())) {
270+ opened = layer.dir () + " /" + COMMIT_FILE_NAME;
271+ return __open_ro_file (opened);
272+ }
273+ auto sealed = layer.dir () + " /" + SEALED_FILE_NAME;
274+ if (::access (sealed.c_str (), 0 ) == 0 ) {
275+ // open sealed blob
276+ opened = sealed;
277+ return __open_ro_file (opened);
278+ }
279+ return nullptr ;
280+ }
281+ int ImageFile::open_lower_layer (IFile *&file, ImageConfigNS::LayerConfig &layer, int index) {
282+ std::string opened;
283+ file = open_localfile (layer, opened); // try to open localfile if downloaded
284+ if (file == nullptr ) {
285+ opened = layer.digest ();
286+ file = __open_ro_remote (layer.dir (), layer.digest (), layer.size (), index);
281287 }
282-
283288 if (file == nullptr ) {
284289 return -1 ;
285290 }
@@ -305,8 +310,8 @@ int ImageFile::open_lower_layer(IFile *&file, ImageConfigNS::LayerConfig &layer,
305310 }
306311 target_file = new_gzfile (target_file, gz_index, true );
307312 if (image_service.global_conf .gzipCacheConfig ().enable () && layer.targetDigest () != " " ) {
308- target_file = image_service.global_fs .gzcache_fs ->
309- open_cached_gzip_file ( target_file, layer.targetDigest ().c_str ());
313+ target_file = image_service.global_fs .gzcache_fs ->open_cached_gzip_file (
314+ target_file, layer.targetDigest ().c_str ());
310315 }
311316 }
312317 if (target_file != nullptr ) {
@@ -395,7 +400,8 @@ LSMT::IFileRW *ImageFile::open_upper(ImageConfigNS::UpperConfig &upper) {
395400 }
396401
397402 if (upper.target () != " " ) {
398- LOG_INFO (" turboOCIv1 upper layer : `, `, `, `" , upper.index (), upper.data (), upper.target ());
403+ LOG_INFO (" turboOCIv1 upper layer : `, `, `, `" , upper.index (), upper.data (),
404+ upper.target ());
399405 target_file = open_localfile_adaptor (upper.target ().c_str (), O_RDWR, 0644 );
400406 if (!target_file) {
401407 LOG_ERROR (" open(`,flags), `:`" , upper.target (), errno, strerror (errno));
@@ -411,16 +417,16 @@ LSMT::IFileRW *ImageFile::open_upper(ImageConfigNS::UpperConfig &upper) {
411417 }
412418 ret = LSMT::open_warpfile_rw (idx_file, data_file, target_file, true );
413419 if (!ret) {
414- LOG_ERROR (" LSMT::open_warpfile_rw(`,`,`,`) return NULL" ,
415- ( uint64_t )data_file, (uint64_t )idx_file, (uint64_t )target_file, true );
420+ LOG_ERROR (" LSMT::open_warpfile_rw(`,`,`,`) return NULL" , ( uint64_t )data_file,
421+ (uint64_t )idx_file, (uint64_t )target_file, true );
416422 goto ERROR_EXIT;
417423 }
418424 } else {
419425 LOG_INFO (" overlaybd upper layer : ` , `" , upper.index (), upper.data ());
420426 ret = LSMT::open_file_rw (data_file, idx_file, true );
421427 if (!ret) {
422- LOG_ERROR (" LSMT::open_file_rw(`,`,`) return NULL" ,
423- ( uint64_t )data_file, (uint64_t )idx_file, true );
428+ LOG_ERROR (" LSMT::open_file_rw(`,`,`) return NULL" , ( uint64_t )data_file,
429+ (uint64_t )idx_file, true );
424430 goto ERROR_EXIT;
425431 }
426432 }
@@ -453,8 +459,7 @@ int ImageFile::init_image_file() {
453459 LOG_INFO (" Acceleration layer found at `, ignore the last lower" , accel_layer);
454460
455461 std::string trace_file = accel_layer + " /trace" ;
456- if (Prefetcher::detect_mode (trace_file) ==
457- Prefetcher::Mode::Replay) {
462+ if (Prefetcher::detect_mode (trace_file) == Prefetcher::Mode::Replay) {
458463 m_prefetcher = new_prefetcher (trace_file, concurrency);
459464 }
460465
@@ -519,8 +524,8 @@ void ImageFile::set_auth_failed() {
519524 }
520525}
521526
522- template <typename ...Ts>
523- void ImageFile::set_failed (const Ts&...xs) {
527+ template <typename ... Ts>
528+ void ImageFile::set_failed (const Ts &...xs) {
524529 if (m_status == 0 ) // only set exit in image boot phase
525530 {
526531 m_status = -1 ;
0 commit comments