diff --git a/launcher/instancemanager.go b/launcher/instancemanager.go index 8315aeb2..9fe69b42 100644 --- a/launcher/instancemanager.go +++ b/launcher/instancemanager.go @@ -216,9 +216,9 @@ func (im *instanceManager) setupInstance( } instanceInfo.UID = uint32(storedInstance.UID) + requestedState, requestedStorage := getReqDiskSize(service.Config, node.nodeConfig.ResourceRatios) - if err = im.setupInstanceStateStorage(&instanceInfo, service, - getStorageRequestRatio(service.Config.ResourceRatios, node.nodeConfig.ResourceRatios)); err != nil { + if err = im.setupInstanceStateStorage(&instanceInfo, service, requestedState, requestedStorage); err != nil { return aostypes.InstanceInfo{}, err } @@ -278,7 +278,7 @@ func (im *instanceManager) getInstanceCheckSum(instance aostypes.InstanceIdent) func (im *instanceManager) setupInstanceStateStorage( instanceInfo *aostypes.InstanceInfo, serviceInfo imagemanager.ServiceInfo, - requestRation float64, + requestedState, requestedStorage uint64, ) error { stateStorageParams := storagestate.SetupParams{ InstanceIdent: instanceInfo.InstanceIdent, @@ -293,9 +293,6 @@ func (im *instanceManager) setupInstanceStateStorage( stateStorageParams.StorageQuota = *serviceInfo.Config.Quotas.StorageLimit } - requestedStorage := uint64(float64(stateStorageParams.StorageQuota)*requestRation + 0.5) - requestedState := uint64(float64(stateStorageParams.StateQuota)*requestRation + 0.5) - if requestedStorage > im.availableStorage { return aoserrors.Errorf("not enough storage space") } diff --git a/launcher/launcher.go b/launcher/launcher.go index 2662d18f..274673c1 100644 --- a/launcher/launcher.go +++ b/launcher/launcher.go @@ -687,46 +687,70 @@ func (launcher *Launcher) getLayersForService(digests []string) ([]imagemanager. return layers, nil } -func getStorageRequestRatio( - serviceRatios *aostypes.ResourceRatiosInfo, nodeRatios *aostypes.ResourceRatiosInfo, -) float64 { - if serviceRatios != nil && serviceRatios.Storage != nil { - return float64(*serviceRatios.Storage) / 100 +func getReqDiskSize(serviceConfig aostypes.ServiceConfig, nodeRatios *aostypes.ResourceRatiosInfo, +) (stateSize, storageSize uint64) { + stateQuota := serviceConfig.Quotas.StateLimit + storageQuota := serviceConfig.Quotas.StorageLimit + + if serviceConfig.RequestedResources != nil && serviceConfig.RequestedResources.Storage != nil { + stateSize = clampResource(*serviceConfig.RequestedResources.Storage, stateQuota) + storageSize = clampResource(*serviceConfig.RequestedResources.Storage, storageQuota) + } else { + stateSize = getReqStorageFromNodeConf(stateQuota, nodeRatios) + storageSize = getReqStorageFromNodeConf(storageQuota, nodeRatios) } - if nodeRatios != nil && nodeRatios.Storage != nil { - return float64(*nodeRatios.Storage) / 100 + return +} + +func getReqCPUFromNodeConf(cpuQuota *uint64, nodeRatios *aostypes.ResourceRatiosInfo) uint64 { + ratio := defaultResourceRation / 100.0 + + if nodeRatios != nil && nodeRatios.CPU != nil { + ratio = float64(*nodeRatios.CPU) / 100.0 + } + + if cpuQuota != nil { + return uint64(float64(*cpuQuota)*ratio + 0.5) } - return defaultResourceRation / 100 + return 0 } -func getCPURequestRatio( - serviceRatios *aostypes.ResourceRatiosInfo, nodeRatios *aostypes.ResourceRatiosInfo, -) float64 { - if serviceRatios != nil && serviceRatios.CPU != nil { - return float64(*serviceRatios.CPU) / 100 +func getReqRAMFromNodeConf(ramQuota *uint64, nodeRatios *aostypes.ResourceRatiosInfo) uint64 { + ratio := defaultResourceRation / 100.0 + + if nodeRatios != nil && nodeRatios.RAM != nil { + ratio = float64(*nodeRatios.RAM) / 100.0 } - if nodeRatios != nil && nodeRatios.CPU != nil { - return float64(*nodeRatios.CPU) / 100 + if ramQuota != nil { + return uint64(float64(*ramQuota)*ratio + 0.5) } - return defaultResourceRation / 100 + return 0 } -func getRAMRequestRatio( - serviceRatios *aostypes.ResourceRatiosInfo, nodeRatios *aostypes.ResourceRatiosInfo, -) float64 { - if serviceRatios != nil && serviceRatios.RAM != nil { - return float64(*serviceRatios.RAM) / 100 +func getReqStorageFromNodeConf(storageQuota *uint64, nodeRatios *aostypes.ResourceRatiosInfo) uint64 { + ratio := defaultResourceRation / 100.0 + + if nodeRatios != nil && nodeRatios.Storage != nil { + ratio = float64(*nodeRatios.Storage) / 100.0 } - if nodeRatios != nil && nodeRatios.CPU != nil { - return float64(*nodeRatios.CPU) / 100 + if storageQuota != nil { + return uint64(float64(*storageQuota)*ratio + 0.5) + } + + return 0 +} + +func clampResource(value uint64, quota *uint64) uint64 { + if quota != nil && value > *quota { + return *quota } - return defaultResourceRation / 100 + return value } func instanceIdentLogFields(instance aostypes.InstanceIdent, extraFields log.Fields) log.Fields { diff --git a/launcher/nodehandler.go b/launcher/nodehandler.go index 9c3cd1f3..175dd7c5 100644 --- a/launcher/nodehandler.go +++ b/launcher/nodehandler.go @@ -311,10 +311,12 @@ func (node *nodeHandler) getRequestedCPU( instanceIdent aostypes.InstanceIdent, serviceConfig aostypes.ServiceConfig, ) uint64 { requestedCPU := uint64(0) + cpuQuota := serviceConfig.Quotas.CPUDMIPSLimit - if serviceConfig.Quotas.CPUDMIPSLimit != nil { - requestedCPU = uint64(float64(*serviceConfig.Quotas.CPUDMIPSLimit)*getCPURequestRatio( - serviceConfig.ResourceRatios, node.nodeConfig.ResourceRatios) + 0.5) + if serviceConfig.RequestedResources != nil && serviceConfig.RequestedResources.CPU != nil { + requestedCPU = clampResource(*serviceConfig.RequestedResources.CPU, cpuQuota) + } else { + requestedCPU = getReqCPUFromNodeConf(cpuQuota, node.nodeConfig.ResourceRatios) } if node.needRebalancing { @@ -336,10 +338,12 @@ func (node *nodeHandler) getRequestedRAM( instanceIdent aostypes.InstanceIdent, serviceConfig aostypes.ServiceConfig, ) uint64 { requestedRAM := uint64(0) + ramQuota := serviceConfig.Quotas.RAMLimit - if serviceConfig.Quotas.RAMLimit != nil { - requestedRAM = uint64(float64(*serviceConfig.Quotas.RAMLimit)*getRAMRequestRatio( - serviceConfig.ResourceRatios, node.nodeConfig.ResourceRatios) + 0.5) + if serviceConfig.RequestedResources != nil && serviceConfig.RequestedResources.RAM != nil { + requestedRAM = clampResource(*serviceConfig.RequestedResources.RAM, ramQuota) + } else { + requestedRAM = getReqRAMFromNodeConf(ramQuota, node.nodeConfig.ResourceRatios) } if node.needRebalancing {