Skip to content

Commit

Permalink
fix: add online checks to client getter functions
Browse files Browse the repository at this point in the history
Produces more accurate client "get" updates (ie: cfm-service will report 0 ports\memory\etc for an "offline" device)
  • Loading branch information
scott-howe-1 committed Sep 23, 2024
1 parent 5846b4d commit a67d902
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 63 deletions.
57 changes: 36 additions & 21 deletions pkg/api/api_default_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ func (cfm *CfmApiService) BladesGetMemory(ctx context.Context, applianceId strin
}

// order returned uris by memory id
memoryIds := blade.GetAllMemoryIds()
memoryIds := blade.GetAllMemoryIds(ctx)
sort.Strings(memoryIds)

memory := blade.GetMemory(ctx)
Expand Down Expand Up @@ -422,12 +422,17 @@ func (cfm *CfmApiService) BladesGetMemoryById(ctx context.Context, applianceId s
return formatErrorResp(ctx, err.(*common.RequestError))
}

details, err := memory.GetDetails(ctx)
if err != nil {
return formatErrorResp(ctx, err.(*common.RequestError))
}
if memory != nil {
details, err := memory.GetDetails(ctx)
if err != nil {
return formatErrorResp(ctx, err.(*common.RequestError))
}

return openapi.Response(http.StatusOK, details), nil
return openapi.Response(http.StatusOK, details), nil

} else {
return openapi.Response(http.StatusOK, openapi.MemoryRegion{}), nil
}
}

// BladesGetPortById -
Expand All @@ -447,12 +452,17 @@ func (cfm *CfmApiService) BladesGetPortById(ctx context.Context, applianceId str
return formatErrorResp(ctx, err.(*common.RequestError))
}

details, err := port.GetDetails(ctx)
if err != nil {
return formatErrorResp(ctx, err.(*common.RequestError))
}
if port != nil {
details, err := port.GetDetails(ctx)
if err != nil {
return formatErrorResp(ctx, err.(*common.RequestError))
}

return openapi.Response(http.StatusOK, details), nil
return openapi.Response(http.StatusOK, details), nil

} else {
return openapi.Response(http.StatusOK, openapi.PortInformation{}), nil
}
}

// BladesGetPorts -
Expand All @@ -468,7 +478,7 @@ func (cfm *CfmApiService) BladesGetPorts(ctx context.Context, applianceId string
}

// order returned uris by port id
portIds := blade.GetAllPortIds()
portIds := blade.GetAllPortIds(ctx)
sort.Strings(portIds)

ports := blade.GetPorts(ctx)
Expand Down Expand Up @@ -502,12 +512,17 @@ func (cfm *CfmApiService) BladesGetResourceById(ctx context.Context, applianceId
return formatErrorResp(ctx, err.(*common.RequestError))
}

details, err := resource.GetDetails(ctx)
if err != nil {
return formatErrorResp(ctx, err.(*common.RequestError))
}
if resource != nil {
details, err := resource.GetDetails(ctx)
if err != nil {
return formatErrorResp(ctx, err.(*common.RequestError))
}

return openapi.Response(http.StatusOK, details), nil
return openapi.Response(http.StatusOK, details), nil

} else {
return openapi.Response(http.StatusOK, openapi.MemoryResourceBlock{}), nil
}
}

// BladesGetResources -
Expand All @@ -523,7 +538,7 @@ func (cfm *CfmApiService) BladesGetResources(ctx context.Context, applianceId st
}

// order returned uris by resourse id
resourceIds := blade.GetAllResourceIds()
resourceIds := blade.GetAllResourceIds(ctx)
sort.Strings(resourceIds)

resources := blade.GetResources(ctx)
Expand Down Expand Up @@ -632,7 +647,7 @@ func (cfm *CfmApiService) HostGetMemory(ctx context.Context, hostId string) (ope
}

// order returned uris by memory id
memoryIds := host.GetAllMemoryIds()
memoryIds := host.GetAllMemoryIds(ctx)
sort.Strings(memoryIds)

memory := host.GetMemory(ctx)
Expand Down Expand Up @@ -818,7 +833,7 @@ func (cfm *CfmApiService) HostsGetMemoryDevices(ctx context.Context, hostId stri
}

// order returned uris by memory device id
memdevIds := host.GetAllMemoryDeviceIds()
memdevIds := host.GetAllMemoryDeviceIds(ctx)
sort.Strings(memdevIds)

memdevs := host.GetMemoryDevices(ctx)
Expand Down Expand Up @@ -863,7 +878,7 @@ func (cfm *CfmApiService) HostsGetPorts(ctx context.Context, hostId string) (ope
}

// order returned uris by port id
portIds := host.GetAllPortIds()
portIds := host.GetAllPortIds(ctx)
sort.Strings(portIds)

ports := host.GetPorts(ctx)
Expand Down
14 changes: 7 additions & 7 deletions pkg/api/api_redfish_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func (cfm *CfmApiService) RedfishV1ChassisChassisIdPCIeDevicesGet(ctx context.Co
}

// order returned uris by memory device id
memdevIds := host.GetAllMemoryDeviceIds()
memdevIds := host.GetAllMemoryDeviceIds(ctx)
sort.Strings(memdevIds)

members := []redfishapi.OdataV4IdRef{}
Expand Down Expand Up @@ -588,7 +588,7 @@ func (cfm *CfmApiService) RedfishV1FabricsFabricIdConnectionsGet(ctx context.Con
if err != nil {
return formatRedfishErrorResp(ctx, err.(*common.RequestError))
}
memoryIds := blade.GetAllMemoryIds()
memoryIds := blade.GetAllMemoryIds(ctx)
sort.Strings(memoryIds)

for _, memId := range memoryIds {
Expand Down Expand Up @@ -652,7 +652,7 @@ func (cfm *CfmApiService) RedfishV1FabricsFabricIdConnectionsPost(ctx context.Co
endpointOdataId := connectionV131Connection.Links.TargetEndpoints[0].OdataId

for bladeId, blade := range appliance.GetBlades(ctx) {
for _, memoryId := range blade.GetAllMemoryIds() {
for _, memoryId := range blade.GetAllMemoryIds(ctx) {
if fmt.Sprintf("/redfish/v1/Systems/%s/MemoryDomains/%s/MemoryChunks/%s", fabricId, bladeId, memoryId) == memoryChunkOdataId {
targetBladeId = bladeId
r.MemoryId = memoryId
Expand All @@ -662,7 +662,7 @@ func (cfm *CfmApiService) RedfishV1FabricsFabricIdConnectionsPost(ctx context.Co
if r.MemoryId == "" {
continue
}
for _, portid := range blade.GetAllPortIds() {
for _, portid := range blade.GetAllPortIds(ctx) {
if fmt.Sprintf("/redfish/v1/Fabrics/%s/Endpoints/%s", fabricId, bladeId+strings.Replace(portid, "port", "up", 1)) == endpointOdataId {
r.PortId = portid
break
Expand Down Expand Up @@ -784,7 +784,7 @@ func (cfm *CfmApiService) RedfishV1FabricsFabricIdEndpointsGet(ctx context.Conte
members := []redfishapi.OdataV4IdRef{}
for _, bladeId := range bladeIds {
blade, _ := appliance.GetBladeById(ctx, bladeId)
for _, portid := range blade.GetAllPortIds() {
for _, portid := range blade.GetAllPortIds(ctx) {
members = append(members, redfishapi.OdataV4IdRef{
OdataId: fmt.Sprintf("/redfish/v1/Fabrics/%s/Endpoints/%s", fabricId, bladeId+strings.Replace(portid, "port", "up", 1))})
}
Expand Down Expand Up @@ -943,7 +943,7 @@ func (cfm *CfmApiService) RedfishV1FabricsFabricIdSwitchesSwitchIdPortsGet(ctx c
}

// order returned uris by port id
portIds := blade.GetAllPortIds()
portIds := blade.GetAllPortIds(ctx)
sort.Strings(portIds)
members := []redfishapi.OdataV4IdRef{}
for _, member := range portIds {
Expand Down Expand Up @@ -1377,7 +1377,7 @@ func (cfm *CfmApiService) RedfishV1SystemsComputerSystemIdMemoryDomainsMemoryDom
}

// order returned uris by memory id
memoryIds := blade.GetAllMemoryIds()
memoryIds := blade.GetAllMemoryIds(ctx)
sort.Strings(memoryIds)
members := []redfishapi.OdataV4IdRef{}
for _, member := range memoryIds {
Expand Down
81 changes: 60 additions & 21 deletions pkg/manager/blade.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (b *Blade) AssignMemory(ctx context.Context, r *RequestAssignMemory) (*open
if len(memory.resourceIds) != 0 {
resourcesToUpdate = memory.resourceIds
} else {
resourcesToUpdate = b.GetAllResourceIds()
resourcesToUpdate = b.GetAllResourceIds(ctx)
}
for _, resourceId := range resourcesToUpdate {
resource, err := b.GetResourceById(ctx, resourceId)
Expand Down Expand Up @@ -333,7 +333,7 @@ func (b *Blade) FreeMemoryById(ctx context.Context, memoryId string) (*openapi.M
if len(memory.resourceIds) != 0 {
resourcesToUpdate = memory.resourceIds
} else {
resourcesToUpdate = b.GetAllResourceIds()
resourcesToUpdate = b.GetAllResourceIds(ctx)
}
for _, resourceId := range resourcesToUpdate {
resource, err := b.GetResourceById(ctx, resourceId)
Expand Down Expand Up @@ -365,33 +365,30 @@ func (b *Blade) FreeMemoryById(ctx context.Context, memoryId string) (*openapi.M
return &memoryRegion, nil
}

func (b *Blade) GetAllMemoryIds() []string {
func (b *Blade) GetAllMemoryIds(ctx context.Context) []string {
var ids []string

// CACHE: Get
for id := range b.Memory {
for id := range b.GetMemory(ctx) {
ids = append(ids, id)
}

return ids
}

func (b *Blade) GetAllPortIds() []string {
func (b *Blade) GetAllPortIds(ctx context.Context) []string {
var ids []string

// CACHE: Get
for id := range b.Ports {
for id := range b.GetPorts(ctx) {
ids = append(ids, id)
}

return ids
}

func (b *Blade) GetAllResourceIds() []string {
func (b *Blade) GetAllResourceIds(ctx context.Context) []string {
var ids []string

// CACHE: Get
for id := range b.Resources {
for id := range b.GetResources(ctx) {
ids = append(ids, id)
}

Expand All @@ -402,6 +399,11 @@ func (b *Blade) GetMemoryById(ctx context.Context, memoryId string) (*BladeMemor
logger := klog.FromContext(ctx)
logger.V(4).Info(">>>>>> GetMemoryById: ", "memoryId", memoryId, "bladeId", b.Id, "applianceId", b.ApplianceId)

if !b.IsOnline(ctx) {
// If blade offline, not an error. Just no information to return.
return nil, nil
}

memory, ok := b.Memory[memoryId]
if !ok {
newErr := fmt.Errorf("memory [%s] not found on appliance [%s] blade [%s] ", memoryId, b.ApplianceId, b.Id)
Expand All @@ -418,6 +420,11 @@ func (b *Blade) GetMemory(ctx context.Context) map[string]*BladeMemory {
logger := klog.FromContext(ctx)
logger.V(4).Info(">>>>>> GetMemory: ", "bladeId", b.Id, "applianceId", b.ApplianceId)

if !b.IsOnline(ctx) {
// If blade offline, not an error. Just no information to return.
return make(map[string]*BladeMemory)
}

memory := b.Memory

logger.V(2).Info("success: get memory", "count", len(memory), "bladeId", b.Id, "applianceId", b.ApplianceId)
Expand All @@ -430,9 +437,12 @@ func (b *Blade) GetMemoryBackend(ctx context.Context) ([]string, error) {
logger := klog.FromContext(ctx)
logger.V(4).Info(">>>>>> GetMemoryBackend: ", "bladeId", b.Id, "applianceId", b.ApplianceId)

// HARDWARE: Get
if !b.IsOnline(ctx) {
// If blade offline, not an error. Just no information to return.
return make([]string, 0), nil
}

req := backend.GetMemoryRequest{}
// get memory ids from backend
response, err := b.backendOps.GetMemory(ctx, &backend.ConfigurationSettings{}, &req)
if err != nil || response == nil {
newErr := fmt.Errorf("get memory (backend) [%s] failure on blade [%s]: %w", b.backendOps.GetBackendInfo(ctx).BackendName, b.Id, err)
Expand All @@ -457,6 +467,11 @@ func (b *Blade) GetPortById(ctx context.Context, portId string) (*CxlBladePort,
logger := klog.FromContext(ctx)
logger.V(4).Info(">>>>>> GetPortById: ", "portId", portId, "bladeId", b.Id, "applianceId", b.ApplianceId)

if !b.IsOnline(ctx) {
// If blade offline, not an error. Just no information to return.
return nil, nil
}

port, ok := b.Ports[portId]
if !ok {
newErr := fmt.Errorf("port [%s] not found on appliance [%s] blade [%s] ", portId, b.ApplianceId, b.Id)
Expand All @@ -473,6 +488,11 @@ func (b *Blade) GetPorts(ctx context.Context) map[string]*CxlBladePort {
logger := klog.FromContext(ctx)
logger.V(4).Info(">>>>>> GetPorts: ", "bladeId", b.Id, "applianceId", b.ApplianceId)

if !b.IsOnline(ctx) {
// If blade offline, not an error. Just no information to return.
return make(map[string]*CxlBladePort)
}

ports := b.Ports

logger.V(2).Info("success: get ports(blade) (cache)", "count", len(ports), "bladeId", b.Id, "applianceId", b.ApplianceId)
Expand All @@ -485,6 +505,11 @@ func (b *Blade) GetPortsBackend(ctx context.Context) ([]string, error) {
logger := klog.FromContext(ctx)
logger.V(4).Info(">>>>>> GetPortsBackend: ", "bladeId", b.Id, "applianceId", b.ApplianceId)

if !b.IsOnline(ctx) {
// If blade offline, not an error. Just no information to return.
return make([]string, 0), nil
}

req := backend.GetPortsRequest{}
response, err := b.backendOps.GetPorts(ctx, &backend.ConfigurationSettings{}, &req)
if err != nil || response == nil {
Expand All @@ -502,6 +527,11 @@ func (b *Blade) GetResourceById(ctx context.Context, resourceId string) (*BladeR
logger := klog.FromContext(ctx)
logger.V(4).Info(">>>>>> GetResourceById: ", "resourceId", resourceId, "bladeId", b.Id, "applianceId", b.ApplianceId)

if !b.IsOnline(ctx) {
// If blade offline, not an error. Just no information to return.
return nil, nil
}

resource, ok := b.Resources[resourceId]
if !ok {
newErr := fmt.Errorf("resource [%s] not found on appliance [%s] blade [%s] ", resourceId, b.ApplianceId, b.Id)
Expand All @@ -518,6 +548,11 @@ func (b *Blade) GetResources(ctx context.Context) map[string]*BladeResource {
logger := klog.FromContext(ctx)
logger.V(4).Info(">>>>>> GetResources: ", "bladeId", b.Id, "applianceId", b.ApplianceId)

if !b.IsOnline(ctx) {
// If blade offline, not an error. Just no information to return.
return make(map[string]*BladeResource)
}

resources := b.Resources

logger.V(2).Info("success: get resources(cache)", "count", len(resources), "bladeId", b.Id, "applianceId", b.ApplianceId)
Expand All @@ -530,6 +565,11 @@ func (b *Blade) GetResourcesBackend(ctx context.Context) ([]string, error) {
logger := klog.FromContext(ctx)
logger.V(4).Info(">>>>>> GetResourcesBackend: ", "bladeId", b.Id, "applianceId", b.ApplianceId)

if !b.IsOnline(ctx) {
// If blade offline, not an error. Just no information to return.
return make([]string, 0), nil
}

req := backend.MemoryResourceBlocksRequest{}
response, err := b.backendOps.GetMemoryResourceBlocks(ctx, &backend.ConfigurationSettings{}, &req)
if err != nil || response == nil {
Expand All @@ -554,12 +594,7 @@ func (b *Blade) GetResourceTotals(ctx context.Context) (*ResponseResourceTotals,
TotalMemoryAllocatedMiB: 0,
}

if b.Status == common.OFFLINE {
return &response, nil
}

resources := b.GetResources(ctx)
for _, resource := range resources {
for _, resource := range b.GetResources(ctx) {
totals, err := resource.GetTotals(ctx)
if err != nil || totals == nil {
newErr := fmt.Errorf("failed to get resource totals: appliance [%s] blade [%s] resource [%s]: %w", b.ApplianceId, b.Id, resource.Id, err)
Expand All @@ -574,7 +609,7 @@ func (b *Blade) GetResourceTotals(ctx context.Context) (*ResponseResourceTotals,
response.TotalMemoryAvailableMiB = totalAvail
response.TotalMemoryAllocatedMiB = totalAlloc

logger.V(2).Info("success: get resource totals", "bladeId", b.Id, "applianceId", b.ApplianceId)
logger.V(2).Info("success: get resource totals", "totals", response, "bladeId", b.Id, "applianceId", b.ApplianceId)

return &response, nil
}
Expand All @@ -593,10 +628,14 @@ func (b *Blade) InvalidateCache() {
}
}

func (b *Blade) IsOnline(ctx context.Context) bool {
return b.Status == common.ONLINE
}

// UpdateConnectionStatusBackend - Query the blade root service to verify continued connection and update the object status accordingly.
func (b *Blade) UpdateConnectionStatusBackend(ctx context.Context) {
logger := klog.FromContext(ctx)
logger.V(4).Info(">>>>>> GetConnectionsStatusBackend: ", "bladeId", b.Id)
logger.V(4).Info(">>>>>> UpdateConnectionStatusBackend: ", "bladeId", b.Id)

req := backend.GetRootServiceRequest{}
response, err := b.backendOps.GetRootService(ctx, &backend.ConfigurationSettings{}, &req)
Expand Down
Loading

0 comments on commit a67d902

Please sign in to comment.