From b742e50f6e8af2c658ebfd8c8d48305817147e2b Mon Sep 17 00:00:00 2001 From: yuanruji Date: Wed, 22 Jan 2025 13:27:12 +0800 Subject: [PATCH] fix(dbm-services): fix include_or_exclue default val #9184 --- .../db-resource/internal/svr/apply/apply.go | 36 +++++++++++++------ .../internal/svr/apply/apply_base.go | 14 +++++--- .../db-resource/internal/svr/meta/location.go | 10 +++++- 3 files changed, 44 insertions(+), 16 deletions(-) diff --git a/dbm-services/common/db-resource/internal/svr/apply/apply.go b/dbm-services/common/db-resource/internal/svr/apply/apply.go index 40d0ba2d9e..4e921ab108 100644 --- a/dbm-services/common/db-resource/internal/svr/apply/apply.go +++ b/dbm-services/common/db-resource/internal/svr/apply/apply.go @@ -56,15 +56,28 @@ func applyGroupsInSameLocaltion(param RequestInputParam) (pickers []*PickerObjec logger.Error("get logic citys failed %s", err.Error()) return pickers, err } - // 根据请求,按照请求的分组,分别计算出每个分组的匹配的园区的优先级 - groupcampusNice, err := getGroupcampusNice(param, resourceReqList, idcCitys) - if err != nil { - logger.Error("order campus nice failed %s", err.Error()) - return pickers, err + var subzoneIds []string + specialSubZoneIds, isExclude := param.SpecialSubZoneIds() + // 如果有指定的子园区,那么就按照指定的子园区进行分配 + if !isExclude && len(specialSubZoneIds) > 0 { + subzoneIds = specialSubZoneIds + } else { + // 根据请求,按照请求的分组,分别计算出每个分组的匹配的园区的优先级 + groupcampusNice, errx := getGroupcampusNice(param, resourceReqList, idcCitys) + if errx != nil { + logger.Error("order campus nice failed %s", errx.Error()) + return pickers, errx + } + // 因为整个大的分组在需要分配机器在同一个园区,这里合并所有的分组的园区优先级 + // 合并之后再次排序,返回整体的园区优先级 + nsubzoneIds := sortgroupcampusNice(groupcampusNice) + // 如果需要排除指定的子园区,那么就排除指定的子园区 + if isExclude && len(specialSubZoneIds) > 0 { + subzoneIds, _ = lo.Difference(nsubzoneIds, specialSubZoneIds) + } else { + subzoneIds = nsubzoneIds + } } - // 因为整个大的分组在需要分配机器在同一个园区,这里合并所有的分组的园区优先级 - // 合并之后再次排序,返回整体的园区优先级 - subzoneIds := sortgroupcampusNice(groupcampusNice) logger.Info("sort subzone ids %v", subzoneIds) if len(subzoneIds) == 0 { return pickers, errno.ErrResourceinsufficient.Add("没有符合条件的资源") @@ -221,6 +234,7 @@ func CycleApply(param RequestInputParam) (pickers []*PickerObject, err error) { affinitys := lo.Uniq(param.GetAllAffinitys()) if param.GroupsInSameLocation && len(param.Details) > 1 && len(affinitys) == 1 && slices.Contains([]string{SAME_SUBZONE, SAME_SUBZONE_CROSS_SWTICH}, affinitys[0]) { + logger.Info("apply all groups in same location") return applyGroupsInSameLocaltion(param) } resourceReqList, err := param.SortDetails() @@ -525,10 +539,10 @@ func (o *SearchContext) MatchLocationSpec(db *gorm.DB) { } return } - if o.LocationSpec.IncludeOrExclude { - db.Where("sub_zone_id in (?)", o.LocationSpec.SubZoneIds) - } else { + if o.LocationSpec.IsExclude() { db.Where("sub_zone_id not in (?)", o.LocationSpec.SubZoneIds) + } else { + db.Where("sub_zone_id in (?)", o.LocationSpec.SubZoneIds) } } diff --git a/dbm-services/common/db-resource/internal/svr/apply/apply_base.go b/dbm-services/common/db-resource/internal/svr/apply/apply_base.go index 9efd12f768..d9d6102a6b 100644 --- a/dbm-services/common/db-resource/internal/svr/apply/apply_base.go +++ b/dbm-services/common/db-resource/internal/svr/apply/apply_base.go @@ -51,7 +51,7 @@ func (param *RequestInputParam) ParamCheck() (err error) { if a.LocationSpec.IsEmpty() { return fmt.Errorf("you need choose a city !!! ") } - if a.LocationSpec.IncludeOrExclude && len(a.LocationSpec.SubZoneIds) < 2 { + if !a.LocationSpec.IsExclude() && (len(a.LocationSpec.SubZoneIds) > 0 && len(a.LocationSpec.SubZoneIds) < 2) { return fmt.Errorf("because need cros subzone,you special subzones need more than 2 subzones") } case NONE: @@ -106,6 +106,12 @@ func (param RequestInputParam) BuildMessage() (msg string) { return msg } +// SpecialSubZoneIds get special subzone ids +func (param RequestInputParam) SpecialSubZoneIds() (specialZones []string, isExclude bool) { + d := param.Details[0] + return d.LocationSpec.SubZoneIds, d.LocationSpec.IsExclude() +} + // SortDetails 优先去匹配有明确需求的参数 func (param RequestInputParam) SortDetails() ([]ObjectDetail, error) { if len(param.Details) == 1 { @@ -300,10 +306,10 @@ func (a *ObjectDetail) GetMessage() (message string) { if !a.LocationSpec.IsEmpty() { message += fmt.Sprintf("city: %s \n\r", a.LocationSpec.City) if len(a.LocationSpec.SubZoneIds) > 0 { - if a.LocationSpec.IncludeOrExclude { - message += fmt.Sprintf("subzoneId must exist in the %v", a.LocationSpec.SubZoneIds) - } else { + if a.LocationSpec.IsExclude() { message += fmt.Sprintf("subzoneId must not exist in the %v", a.LocationSpec.SubZoneIds) + } else { + message += fmt.Sprintf("subzoneId must exist in the %v", a.LocationSpec.SubZoneIds) } } } diff --git a/dbm-services/common/db-resource/internal/svr/meta/location.go b/dbm-services/common/db-resource/internal/svr/meta/location.go index bf9a5eb1b8..ab43e48dc4 100644 --- a/dbm-services/common/db-resource/internal/svr/meta/location.go +++ b/dbm-services/common/db-resource/internal/svr/meta/location.go @@ -16,7 +16,7 @@ import "dbm-services/common/go-pubpkg/cmutil" type LocationSpec struct { City string `json:"city" validate:"required"` // 所属城市获取地域 SubZoneIds []string `json:"sub_zone_ids"` - IncludeOrExclude bool `json:"include_or_exclue"` + IncludeOrExclude *bool `json:"include_or_exclue"` } // IsEmpty whether the address location parameter is blank @@ -28,3 +28,11 @@ func (l LocationSpec) IsEmpty() bool { func (l LocationSpec) SubZoneIsEmpty() bool { return l.IsEmpty() || len(l.SubZoneIds) == 0 } + +// IsExclude TODO +func (l LocationSpec) IsExclude() bool { + if l.IncludeOrExclude == nil { + return false + } + return !*l.IncludeOrExclude +}