ResourceConsumptionLog { static String doLog(ResourceConsumptionEvents.ResourceConsumptionEvent event) { if (event instanceof CapacityGroupAllocationEvent) { CapacityGroupAllocationEvent changeEvent = (CapacityGroupAllocationEvent) event; CompositeResourceConsumption consumption = changeEvent.getCapacityGroupConsumption(); StringBuilder sb = new StringBuilder("Resource consumption change: group="); sb.append(consumption.getConsumerName()); if (consumption.isAboveLimit()) { sb.append(" [above limit] "); } else { sb.append(" [below limit] "); } sb.append("actual="); ResourceDimensions.format(consumption.getCurrentConsumption(), sb); sb.append(", max="); ResourceDimensions.format(consumption.getMaxConsumption(), sb); sb.append(", limit="); ResourceDimensions.format(consumption.getAllowedConsumption(), sb); if (consumption.getAttributes().isEmpty()) { sb.append(", attrs={}"); } else { sb.append(", attrs={"); consumption.getAttributes().forEach((k, v) -> sb.append(k).append('=').append(v).append(',')); sb.setCharAt(sb.length() - 1, '}'); } String message = sb.toString(); if (consumption.isAboveLimit()) { logger.warn(message); } else { logger.info(message); } return message; } if (event instanceof CapacityGroupUndefinedEvent) { String message = "Capacity group not defined: group=" + event.getCapacityGroup(); logger.warn(message); return message; } if (event instanceof CapacityGroupRemovedEvent) { String message = "Capacity group no longer defined: group=" + event.getCapacityGroup(); logger.info(message); return message; } String message = "Unrecognized resource consumption event type " + event.getClass(); logger.error(message); return message; } @Inject  ResourceConsumptionLog(ResourceConsumptionService resourceConsumptionService); @Activator Observable<Void> enterActiveMode(); @PreDestroy void shutdown();  }
@Test public void testAllocationEventLogging() throws Exception { CompositeResourceConsumption consumption = new CompositeResourceConsumption( "myCapacityGroup", ResourceConsumption.ConsumptionLevel.CapacityGroup, new ResourceDimension(1, 0, 1, 10, 10, 0), new ResourceDimension(2, 0, 2, 20, 20, 0), new ResourceDimension(3, 0, 3, 30, 30, 0), Collections.singletonMap("attrKey", "attrValue"), Collections.emptyMap(), false ); CapacityGroupAllocationEvent event = new CapacityGroupAllocationEvent( "myCapacityGroup", System.currentTimeMillis(), consumption ); String result = ResourceConsumptionLog.doLog(event); String expected = "Resource consumption change: group=myCapacityGroup [below limit] actual=[cpu=1.0, memoryMB=1, diskMB=10, networkMbs=10, gpu=0, opportunisticCpu=0], max=[cpu=2.0, memoryMB=2, diskMB=20, networkMbs=20, gpu=0, opportunisticCpu=0], limit=[cpu=3.0, memoryMB=3, diskMB=30, networkMbs=30, gpu=0, opportunisticCpu=0], attrs={attrKey=attrValue}"; assertThat(result).isEqualTo(expected); }
@Test public void testGroupUndefinedEvent() throws Exception { CapacityGroupUndefinedEvent event = new CapacityGroupUndefinedEvent("myCapacityGroup", System.currentTimeMillis()); String result = ResourceConsumptionLog.doLog(event); String expected = "Capacity group not defined: group=myCapacityGroup"; assertThat(result).isEqualTo(expected); }
@Test public void testGroupRemovedEvent() throws Exception { CapacityGroupRemovedEvent event = new CapacityGroupRemovedEvent("myCapacityGroup", System.currentTimeMillis()); String result = ResourceConsumptionLog.doLog(event); String expected = "Capacity group no longer defined: group=myCapacityGroup"; assertThat(result).isEqualTo(expected); }