Skip to content

Commit

Permalink
fix(dbm-services): fix include_or_exclue default val #9184
Browse files Browse the repository at this point in the history
  • Loading branch information
ymakedaq authored and zhangzhw8 committed Feb 8, 2025
1 parent 58ab4a5 commit c0a11c2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
36 changes: 25 additions & 11 deletions dbm-services/common/db-resource/internal/svr/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("没有符合条件的资源")
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}
}

Expand Down
14 changes: 10 additions & 4 deletions dbm-services/common/db-resource/internal/svr/apply/apply_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion dbm-services/common/db-resource/internal/svr/meta/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

0 comments on commit c0a11c2

Please sign in to comment.