Merge pull request #201 from GLYASAI/V5.0

modify the logic of PriChargeSverify
This commit is contained in:
barnettZQG 2019-01-24 18:44:57 +08:00 committed by GitHub
commit 811baa00ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 10 deletions

View File

@ -40,4 +40,6 @@ type TenantHandler interface {
TenantsSum() (int, error)
GetProtocols() ([]*dbmodel.RegionProcotols, *util.APIHandleError)
TransPlugins(tenantID, tenantName, fromTenant string, pluginList []string) *util.APIHandleError
GetServicesStatus(ids string) map[string]string
IsClosedStatus(status string) bool
}

View File

@ -72,24 +72,64 @@ func PubChargeSverify(tenant *model.Tenants, quantity int, reason string) *util.
// PriChargeSverify verifies that the resources requested in the private cloud are legal
func PriChargeSverify(tenant *model.Tenants, quantity int) *util.APIHandleError {
tenants, err := db.GetManager().TenantDao().GetALLTenants()
svcs, err := db.GetManager().TenantServiceDao().GetServicesByTenantID(tenant.UUID)
if err != nil {
return util.CreateAPIHandleError(500, fmt.Errorf("error getting all tenants"))
logrus.Errorf("error getting tenant: %v", err)
return util.CreateAPIHandleError(500, fmt.Errorf("error getting tenant: %v", err))
}
var lm int
for _, t := range tenants {
if t.UUID == tenant.UUID {
continue
var svcids []string
svcMap := make(map[string]*model.TenantServices)
for _, svc := range svcs {
svcids = append(svcids, svc.ServiceID)
svcMap[svc.ServiceID] = svc
}
// get services status
var usedMem int
if len(svcids) > 0 {
ss := handler.GetTenantManager().GetServicesStatus(strings.Join(svcids, ","))
logrus.Debugf("service status: %v", ss)
for k, v := range ss {
if !handler.GetTenantManager().IsClosedStatus(v) {
logrus.Debugf("not closed service: %s; status: %s", k, v)
if svc, ok := svcMap[k]; ok {
usedMem += svc.ContainerMemory
}
}
}
lm = t.LimitMemory + lm
}
_, allMem, err := handler.GetTenantManager().GetAllocatableResources()
t, err := db.GetManager().TenantDao().GetTenantByUUID(tenant.UUID)
if err != nil {
return util.CreateAPIHandleError(500, fmt.Errorf("error getting allocatable resources"))
logrus.Errorf("error getting tenant: %v", err)
return util.CreateAPIHandleError(500, fmt.Errorf("error getting tenant: %v", err))
}
logrus.Debugf("t.LimitMemory: %d", t.LimitMemory)
availMem := int64(t.LimitMemory)
logrus.Debugf("availMem: %d", availMem)
if availMem == 0 {
logrus.Debugf("available memory is zero.")
_, allMem, err := handler.GetTenantManager().GetAllocatableResources()
if err != nil {
logrus.Errorf("error getting allocatable resources: %v", err)
return util.CreateAPIHandleError(500, fmt.Errorf("error getting allocatable resources: %v", err))
}
availMem = allMem - int64(usedMem)
tenants, err := db.GetManager().TenantDao().GetALLTenants()
if err != nil {
logrus.Errorf("error getting all tenants: %v", err)
return util.CreateAPIHandleError(500, fmt.Errorf("error getting all tenants: %v", err))
}
for _, item := range tenants {
availMem = availMem - int64(item.LimitMemory)
}
} else {
logrus.Debugf("available memory isn't zero.")
availMem = availMem - int64(usedMem)
}
if int64(lm + quantity) < allMem {
if availMem >= int64(quantity) {
return util.CreateAPIHandleError(200, fmt.Errorf("success"))
} else {
return util.CreateAPIHandleError(200, fmt.Errorf("lack_of_memory"))

View File

@ -190,6 +190,11 @@ func (t *TenantAction) GetTenantsResources(tr *api_model.TenantResources) (map[s
}
serviceTenantCount[s.TenantID]++
}
var allocatedMemory int64
for _, v := range limits {
allocatedMemory = allocatedMemory + int64(v)
}
allMem = allMem - allocatedMemory
var result = make(map[string]map[string]interface{}, len(ids))
for k, v := range limits {
result[k] = map[string]interface{}{
@ -405,3 +410,11 @@ func (t *TenantAction) TransPlugins(tenantID, tenantName, fromTenant string, plu
}
return nil
}
func (t *TenantAction) GetServicesStatus(ids string) map[string]string {
return t.statusCli.GetStatuss(ids)
}
func (t *TenantAction) IsClosedStatus(status string) bool {
return t.statusCli.IsClosedStatus(status)
}