From c3cad7ef01ef57e8782434524a54e0bed44980f1 Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Tue, 20 Apr 2021 17:06:19 +0800 Subject: [PATCH] fix(region): fetch cached image from glance by default --- pkg/compute/hostdrivers/kvm.go | 45 ++++++++++++------- pkg/compute/options/options.go | 1 + pkg/hostman/storageman/imagecache_local.go | 3 +- .../storageman/remotefile/remotefile.go | 13 ++++-- 4 files changed, 42 insertions(+), 20 deletions(-) diff --git a/pkg/compute/hostdrivers/kvm.go b/pkg/compute/hostdrivers/kvm.go index 550a26a494..29f3523257 100644 --- a/pkg/compute/hostdrivers/kvm.go +++ b/pkg/compute/hostdrivers/kvm.go @@ -28,6 +28,7 @@ import ( "yunion.io/x/pkg/errors" "yunion.io/x/pkg/utils" + "yunion.io/x/onecloud/pkg/apis" api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudcommon/cmdline" "yunion.io/x/onecloud/pkg/cloudcommon/db" @@ -38,6 +39,7 @@ import ( "yunion.io/x/onecloud/pkg/compute/options" "yunion.io/x/onecloud/pkg/httperrors" "yunion.io/x/onecloud/pkg/mcclient" + "yunion.io/x/onecloud/pkg/mcclient/auth" "yunion.io/x/onecloud/pkg/util/httputils" "yunion.io/x/onecloud/pkg/util/k8s/tokens" ) @@ -157,19 +159,10 @@ func (self *SKVMHostDriver) CheckAndSetCacheImage(ctx context.Context, host *mod params := task.GetParams() imageId, err := params.GetString("image_id") if err != nil { - return err + return errors.Wrap(err, "Get image_id from params") } format, _ := params.GetString("format") isForce := jsonutils.QueryBoolean(params, "is_force", false) - obj, err := models.CachedimageManager.FetchById(imageId) - if err != nil { - return err - } - cacheImage := obj.(*models.SCachedimage) - srcHostCacheImage, err := cacheImage.ChooseSourceStoragecacheInRange(api.HOST_TYPE_HYPERVISOR, []string{host.Id}, []interface{}{host.GetZone()}) - if err != nil { - return err - } type contentStruct struct { ImageId string @@ -183,17 +176,39 @@ func (self *SKVMHostDriver) CheckAndSetCacheImage(ctx context.Context, host *mod content.ImageId = imageId content.Format = format - if srcHostCacheImage != nil { - err = srcHostCacheImage.AddDownloadRefcount() + if options.Options.ImageCacheFromHost { + obj, err := models.CachedimageManager.FetchById(imageId) + if err != nil { + return errors.Wrapf(err, "Fetch cached image by image_id %s", imageId) + } + cacheImage := obj.(*models.SCachedimage) + srcHostCacheImage, err := cacheImage.ChooseSourceStoragecacheInRange(api.HOST_TYPE_HYPERVISOR, []string{host.Id}, []interface{}{host.GetZone()}) if err != nil { return err } - srcHost, err := srcHostCacheImage.GetHost() + if srcHostCacheImage != nil { + err = srcHostCacheImage.AddDownloadRefcount() + if err != nil { + return err + } + srcHost, err := srcHostCacheImage.GetHost() + if err != nil { + return err + } + content.SrcUrl = fmt.Sprintf("%s/download/images/%s", srcHost.ManagerUri, imageId) + } + } else { + // from glance service + glanceURL, err := auth.GetServiceURL(apis.SERVICE_TYPE_IMAGE, "", host.GetZone().GetName(), "") if err != nil { - return err + return errors.Wrapf(err, "Get %s service url", apis.SERVICE_TYPE_IMAGE) + } + content.SrcUrl = fmt.Sprintf("%s/images/%s", glanceURL, imageId) + if content.Format != "" { + content.SrcUrl = fmt.Sprintf("%s?format=%s", content.SrcUrl, content.Format) } - content.SrcUrl = fmt.Sprintf("%s/download/images/%s", srcHost.ManagerUri, imageId) } + url := fmt.Sprintf("%s/disks/image_cache", host.ManagerUri) if isForce { diff --git a/pkg/compute/options/options.go b/pkg/compute/options/options.go index fea412855e..d3f68b91e7 100644 --- a/pkg/compute/options/options.go +++ b/pkg/compute/options/options.go @@ -50,6 +50,7 @@ type ComputeOptions struct { LoadbalancerPendingDeleteCheckInterval int `default:"3600" help:"Interval between checks of pending deleted loadbalancer objects, defaults to 1h"` ImageCacheStoragePolicy string `default:"least_used" choices:"best_fit|least_used" help:"Policy to choose storage for image cache, best_fit or least_used"` + ImageCacheFromHost bool `default:"false" help:"Download cached image from host"` MetricsRetentionDays int32 `default:"30" help:"Retention days for monitoring metrics in influxdb"` DefaultBandwidth int `default:"1000" help:"Default bandwidth"` diff --git a/pkg/hostman/storageman/imagecache_local.go b/pkg/hostman/storageman/imagecache_local.go index 3f3e38dd0f..378592f9a2 100644 --- a/pkg/hostman/storageman/imagecache_local.go +++ b/pkg/hostman/storageman/imagecache_local.go @@ -26,6 +26,7 @@ import ( "yunion.io/x/log" + "yunion.io/x/onecloud/pkg/apis" "yunion.io/x/onecloud/pkg/hostman/hostutils" "yunion.io/x/onecloud/pkg/hostman/storageman/remotefile" "yunion.io/x/onecloud/pkg/mcclient/auth" @@ -174,7 +175,7 @@ func (l *SLocalImageCache) prepare(ctx context.Context, zone, srcUrl, format str l.consumerCount++ return true, true } - url, err := auth.GetServiceURL("image", "", zone, "") + url, err := auth.GetServiceURL(apis.SERVICE_TYPE_IMAGE, "", zone, "") if err != nil { log.Errorf("Failed to acquire image %s", err) return false, true diff --git a/pkg/hostman/storageman/remotefile/remotefile.go b/pkg/hostman/storageman/remotefile/remotefile.go index 787ee44f08..96eeccb5a3 100644 --- a/pkg/hostman/storageman/remotefile/remotefile.go +++ b/pkg/hostman/storageman/remotefile/remotefile.go @@ -82,14 +82,17 @@ func NewRemoteFile( func (r *SRemoteFile) Fetch() bool { if len(r.preChksum) > 0 { + log.Infof("Fetch remote file with precheck sum: %s", r.preChksum) return r.fetch(r.preChksum) } else if fileutils2.Exists(r.localPath) { if !r.VerifyIntegrity() { + log.Warningf("Local path %s file mistmatch, refetch", r.localPath) return r.fetch("") } else { return true } } else { + log.Infof("Fetch remote file %s to %s", r.downloadUrl, r.tmpPath) return r.fetch("") } } @@ -118,10 +121,11 @@ func (r *SRemoteFile) VerifyIntegrity() bool { return false } if localChksum == r.chksum { - log.Infof("identical chksum, skip download") + log.Infof("Identical chksum, skip download") return true } } + log.Warningf("Integrity mistmatch, fetch from remote") return r.fetch("") } @@ -138,9 +142,10 @@ func (r *SRemoteFile) fetch(preChksum string) bool { if fetchSucc { if len(r.chksum) > 0 && fileutils2.Exists(r.tmpPath) { if localChksum, err := fileutils2.MD5(r.tmpPath); err != nil { - log.Errorln(err) + log.Errorf("TmpPath %s MD5SUM error: %v", r.tmpPath, err) fetchSucc = false } else if r.chksum != localChksum { + log.Errorf("remote checksum %s != local checksum %s", r.chksum, localChksum) fetchSucc = false } } @@ -210,7 +215,7 @@ func (r *SRemoteFile) downloadInternal(getData bool, preChksum string) bool { resp, err := httputils.Request(httpCli, r.ctx, httputils.THttpMethod(method), url, header, nil, false) if err != nil { - log.Errorln(err) + log.Errorf("Request %s to url %s error: %v", method, url, err) return false } else { defer resp.Body.Close() @@ -255,7 +260,7 @@ func (r *SRemoteFile) downloadInternal(getData bool, preChksum string) bool { _, err = io.Copy(fi, reader) close(finishChan) if err != nil { - log.Errorln(err) + log.Errorf("Copy to tmpPath %s from reader error: %v", r.tmpPath, err) return false } }