From 895df0d40efa5181edb6d6d8807fe1d3535dd7f2 Mon Sep 17 00:00:00 2001 From: GLYASAI Date: Thu, 24 Jan 2019 14:25:42 +0800 Subject: [PATCH 1/2] [REV] allocatable memory should subtract allocated memory --- api/handler/tenant.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/api/handler/tenant.go b/api/handler/tenant.go index a531a5906..a20323a12 100644 --- a/api/handler/tenant.go +++ b/api/handler/tenant.go @@ -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{}{ From 96ca1a8cd4930647ca98f80ae7359ce19bc39fb5 Mon Sep 17 00:00:00 2001 From: GLYASAI Date: Thu, 24 Jan 2019 16:15:45 +0800 Subject: [PATCH 2/2] [REV] modify the logic of PriChargeSverify --- api/handler/TenantHandler.go | 2 + api/handler/cloud/chargesverify.go | 60 +++++++++++++++++++++++++----- api/handler/tenant.go | 8 ++++ 3 files changed, 60 insertions(+), 10 deletions(-) diff --git a/api/handler/TenantHandler.go b/api/handler/TenantHandler.go index 15307f918..5e0721e7c 100644 --- a/api/handler/TenantHandler.go +++ b/api/handler/TenantHandler.go @@ -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 } diff --git a/api/handler/cloud/chargesverify.go b/api/handler/cloud/chargesverify.go index 29fdb66c0..a9d7be458 100644 --- a/api/handler/cloud/chargesverify.go +++ b/api/handler/cloud/chargesverify.go @@ -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")) diff --git a/api/handler/tenant.go b/api/handler/tenant.go index a20323a12..385853406 100644 --- a/api/handler/tenant.go +++ b/api/handler/tenant.go @@ -410,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) +}