diff --git a/pkg/apis/compute/storage_const.go b/pkg/apis/compute/storage_const.go index b19bf8d307..11d1edd9ef 100644 --- a/pkg/apis/compute/storage_const.go +++ b/pkg/apis/compute/storage_const.go @@ -112,8 +112,9 @@ const ( const ( STORAGE_ENABLED = "enabled" // STORAGE_DISABLED = "disabled" - STORAGE_OFFLINE = "offline" - STORAGE_ONLINE = "online" + STORAGE_OFFLINE = "offline" // 离线 + STORAGE_ONLINE = "online" // 在线 + STORAGE_UNMOUNT = "unmount" // 待挂载 DISK_TYPE_ROTATE = "rotate" DISK_TYPE_SSD = "ssd" diff --git a/pkg/compute/models/hoststorages.go b/pkg/compute/models/hoststorages.go index 8ebf9d6d5d..7a6ceab7bb 100644 --- a/pkg/compute/models/hoststorages.go +++ b/pkg/compute/models/hoststorages.go @@ -231,7 +231,8 @@ func (self *SHoststorage) PostDelete(ctx context.Context, userCred mcclient.Toke func (self *SHoststorage) SyncStorageStatus(userCred mcclient.TokenCredential) { storage := self.GetStorage() status := api.STORAGE_OFFLINE - for _, host := range storage.GetAttachedHosts() { + hosts, _ := storage.GetAttachedHosts() + for _, host := range hosts { if host.HostStatus == api.HOST_ONLINE { status = api.STORAGE_ONLINE } diff --git a/pkg/compute/models/storages.go b/pkg/compute/models/storages.go index 2972040cf3..907680e57e 100644 --- a/pkg/compute/models/storages.go +++ b/pkg/compute/models/storages.go @@ -132,7 +132,7 @@ func (self *SStorage) PostUpdate(ctx context.Context, userCred mcclient.TokenCre self.SEnabledStatusInfrasResourceBase.PostUpdate(ctx, userCred, query, data) if data.Contains("cmtbound") || data.Contains("capacity") { - hosts := self.GetAttachedHosts() + hosts, _ := self.GetAttachedHosts() for _, host := range hosts { if err := host.ClearSchedDescCache(); err != nil { log.Errorf("clear host %s sched cache failed %v", host.GetName(), err) @@ -226,7 +226,7 @@ func (manager *SStorageManager) ValidateCreateData( func (self *SStorage) CustomizeCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) error { self.SetEnabled(true) - self.SetStatus(userCred, api.STORAGE_OFFLINE, "CustomizeCreate") + self.SetStatus(userCred, api.STORAGE_UNMOUNT, "CustomizeCreate") return self.SEnabledStatusInfrasResourceBase.CustomizeCreate(ctx, userCred, ownerId, query, data) } @@ -566,7 +566,7 @@ func (self *SStorage) GetFreeCapacity() int64 { return int64(float32(self.GetCapacity())*self.GetOvercommitBound()) - self.GetUsedCapacity(tristate.None) } -func (self *SStorage) GetAttachedHosts() []SHost { +func (self *SStorage) GetAttachedHosts() ([]SHost, error) { hosts := HostManager.Query().SubQuery() hoststorages := HoststorageManager.Query().SubQuery() @@ -577,15 +577,14 @@ func (self *SStorage) GetAttachedHosts() []SHost { hostList := make([]SHost, 0) err := db.FetchModelObjects(HostManager, q, &hostList) if err != nil { - log.Errorf("GetAttachedHosts fail %s", err) - return nil + return nil, errors.Wrapf(err, "GetAttachedHosts") } - return hostList + return hostList, nil } func (self *SStorage) SyncStatusWithHosts() { - hosts := self.GetAttachedHosts() - if hosts == nil { + hosts, err := self.GetAttachedHosts() + if err != nil { return } total := 0 @@ -614,6 +613,9 @@ func (self *SStorage) SyncStatusWithHosts() { } else { status = api.STORAGE_OFFLINE } + if len(hosts) == 0 { + status = api.STORAGE_UNMOUNT + } if status != self.Status { self.SetStatus(nil, status, "SyncStatusWithHosts") } @@ -1354,7 +1356,7 @@ func (manager *SStorageManager) InitializeData() error { for _, s := range storages { if len(s.ZoneId) == 0 { zoneId := "" - hosts := s.GetAttachedHosts() + hosts, _ := s.GetAttachedHosts() if hosts != nil && len(hosts) > 0 { zoneId = hosts[0].ZoneId } else { @@ -1583,7 +1585,7 @@ func (self *SStorage) IsPrepaidRecycleResource() bool { if !self.IsLocal() { return false } - hosts := self.GetAttachedHosts() + hosts, _ := self.GetAttachedHosts() if len(hosts) != 1 { return false } @@ -1641,7 +1643,7 @@ func (self *SStorage) StartDeleteRbdDisks(ctx context.Context, userCred mcclient func (storage *SStorage) PerformChangeOwner(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformChangeDomainOwnerInput) (jsonutils.JSONObject, error) { // not allow to perform public for locally connected storage if storage.IsLocal() { - hosts := storage.GetAttachedHosts() + hosts, _ := storage.GetAttachedHosts() if len(hosts) > 0 { return nil, errors.Wrap(httperrors.ErrForbidden, "not allow to change owner for local storage") } @@ -1665,7 +1667,7 @@ func (storage *SStorage) GetChangeOwnerRequiredDomainIds() []string { func (storage *SStorage) PerformPublic(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformPublicDomainInput) (jsonutils.JSONObject, error) { // not allow to perform public for locally connected storage if storage.IsLocal() { - hosts := storage.GetAttachedHosts() + hosts, _ := storage.GetAttachedHosts() if len(hosts) > 0 { return nil, errors.Wrap(httperrors.ErrForbidden, "not allow to perform public for local storage") } @@ -1680,7 +1682,7 @@ func (storage *SStorage) performPublicInternal(ctx context.Context, userCred mcc func (storage *SStorage) PerformPrivate(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformPrivateInput) (jsonutils.JSONObject, error) { // not allow to perform private for locally conencted storage if storage.IsLocal() { - hosts := storage.GetAttachedHosts() + hosts, _ := storage.GetAttachedHosts() if len(hosts) > 0 { return nil, errors.Wrap(httperrors.ErrForbidden, "not allow to perform private for local storage") } diff --git a/pkg/scheduler/algorithm/predicates/disk_schedtag_predicate.go b/pkg/scheduler/algorithm/predicates/disk_schedtag_predicate.go index 83fcf4ade4..065d79444a 100644 --- a/pkg/scheduler/algorithm/predicates/disk_schedtag_predicate.go +++ b/pkg/scheduler/algorithm/predicates/disk_schedtag_predicate.go @@ -93,7 +93,7 @@ func (p *DiskSchedtagPredicate) IsResourceMatchInput(input ISchedtagCustomer, re func (p *DiskSchedtagPredicate) IsResourceFitInput(u *core.Unit, c core.Candidater, res ISchedtagCandidateResource, input ISchedtagCustomer) core.PredicateFailureReason { storage := res.(*api.CandidateStorage) - if storage.Status == computeapi.STORAGE_OFFLINE || storage.Enabled.IsFalse() { + if storage.Status != computeapi.STORAGE_ONLINE || storage.Enabled.IsFalse() { return &FailReason{ fmt.Sprintf("Storage status is %s, enable is %v", storage.Status, storage.Enabled), StorageEnable,