AutoscalingMaintainer extends NodeRepositoryMaintainer { @Override protected boolean maintain() { boolean success = true; if ( ! nodeRepository().zone().environment().isProduction()) return success; activeNodesByApplication().forEach((applicationId, nodes) -> autoscale(applicationId, nodes)); return success; } AutoscalingMaintainer(NodeRepository nodeRepository,
                                 NodeMetricsDb metricsDb,
                                 Deployer deployer,
                                 Metric metric,
                                 Duration interval);   }
@Test public void autoscaling_discards_metric_values_from_before_rescaling() { ApplicationId app1 = AutoscalingMaintainerTester.makeApplicationId("app1"); ClusterSpec cluster1 = AutoscalingMaintainerTester.containerClusterSpec(); NodeResources lowResources = new NodeResources(4, 4, 10, 0.1); NodeResources highResources = new NodeResources(8, 8, 20, 0.1); Capacity app1Capacity = Capacity.from(new ClusterResources(2, 1, lowResources), new ClusterResources(4, 2, highResources)); var tester = new AutoscalingMaintainerTester(new MockDeployer.ApplicationContext(app1, cluster1, app1Capacity)); tester.deploy(app1, cluster1, app1Capacity); tester.clock().advance(Duration.ofSeconds(1)); tester.addMeasurements(Metric.cpu, 0.9f, 500, app1); tester.addMeasurements(Metric.memory, 0.9f, 500, app1); tester.addMeasurements(Metric.disk, 0.9f, 500, app1); tester.clock().advance(Duration.ofSeconds(1)); Instant firstMaintenanceTime = tester.clock().instant(); tester.maintainer().maintain(); assertTrue(tester.deployer().lastDeployTime(app1).isPresent()); assertEquals(firstMaintenanceTime.toEpochMilli(), tester.deployer().lastDeployTime(app1).get().toEpochMilli()); assertEquals(1, tester.nodeMetricsDb().getEvents(app1).size()); assertEquals(app1, tester.nodeMetricsDb().getEvents(app1).get(0).application()); assertEquals(0, tester.nodeMetricsDb().getEvents(app1).get(0).generation()); assertEquals(firstMaintenanceTime.toEpochMilli(), tester.nodeMetricsDb().getEvents(app1).get(0).time().toEpochMilli()); tester.clock().advance(Duration.ofSeconds(1)); tester.addMeasurements(Metric.cpu, 0.9f, 500, app1); tester.addMeasurements(Metric.memory, 0.9f, 500, app1); tester.addMeasurements(Metric.disk, 0.9f, 500, app1); tester.clock().advance(Duration.ofSeconds(1)); tester.maintainer().maintain(); assertEquals(firstMaintenanceTime.toEpochMilli(), tester.deployer().lastDeployTime(app1).get().toEpochMilli()); tester.clock().advance(Duration.ofSeconds(1)); tester.addMeasurements(Metric.cpu, 0.1f, 500, app1); tester.addMeasurements(Metric.memory, 0.1f, 500, app1); tester.addMeasurements(Metric.disk, 0.1f, 500, app1); tester.clock().advance(Duration.ofSeconds(1)); tester.maintainer().maintain(); assertEquals(firstMaintenanceTime.toEpochMilli(), tester.deployer().lastDeployTime(app1).get().toEpochMilli()); tester.clock().advance(Duration.ofSeconds(1)); tester.addMeasurements(Metric.generation, 0, 1, app1); tester.addMeasurements(Metric.cpu, 0.1f, 500, app1); tester.addMeasurements(Metric.memory, 0.1f, 500, app1); tester.addMeasurements(Metric.disk, 0.1f, 500, app1); Instant lastMaintenanceTime = tester.clock().instant(); tester.maintainer().maintain(); assertEquals(lastMaintenanceTime.toEpochMilli(), tester.deployer().lastDeployTime(app1).get().toEpochMilli()); assertEquals(2, tester.nodeMetricsDb().getEvents(app1).size()); assertEquals(1, tester.nodeMetricsDb().getEvents(app1).get(1).generation()); }