From 8247353d3772d16f49ad7ca8cea9ef0e67e74214 Mon Sep 17 00:00:00 2001 From: Ola Aunrønning Date: Wed, 27 Mar 2019 14:07:37 +0100 Subject: Added test. Misc fixes --- .../maintenance/ResourceMeterMaintainer.java | 24 +++-------- .../integration/NodeRepositoryClientMock.java | 8 ++-- .../maintenance/CostReportMaintainerTest.java | 4 +- .../maintenance/ResourceMeterMaintainerTest.java | 46 ++++++++++++++++++++++ 4 files changed, 57 insertions(+), 25 deletions(-) create mode 100644 controller-server/src/test/java/com/yahoo/vespa/hosted/controller/maintenance/ResourceMeterMaintainerTest.java (limited to 'controller-server') diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/maintenance/ResourceMeterMaintainer.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/maintenance/ResourceMeterMaintainer.java index 08128cd42b2..b2f8b659045 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/maintenance/ResourceMeterMaintainer.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/maintenance/ResourceMeterMaintainer.java @@ -1,10 +1,9 @@ // Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.hosted.controller.maintenance; -import com.fasterxml.jackson.databind.ObjectMapper; import com.yahoo.config.provision.SystemName; import com.yahoo.vespa.hosted.controller.Controller; -import com.yahoo.vespa.hosted.controller.api.identifiers.ApplicationId; +import com.yahoo.config.provision.ApplicationId; import com.yahoo.vespa.hosted.controller.api.integration.noderepository.NodeOwner; import com.yahoo.vespa.hosted.controller.api.integration.noderepository.NodeRepositoryClientInterface; import com.yahoo.vespa.hosted.controller.api.integration.noderepository.NodeRepositoryNode; @@ -12,9 +11,6 @@ import com.yahoo.vespa.hosted.controller.api.integration.resource.ResourceSnapsh import com.yahoo.vespa.hosted.controller.api.integration.resource.ResourceSnapshotConsumer; import com.yahoo.vespa.hosted.controller.api.integration.resource.ResourceAllocation; -import java.io.File; -import java.io.IOException; -import java.io.UncheckedIOException; import java.time.Clock; import java.time.Duration; import java.time.Instant; @@ -23,15 +19,14 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.logging.Logger; import java.util.stream.Collectors; import static com.yahoo.yolean.Exceptions.uncheck; /** - * @author olaa * Creates a ResourceSnapshot per application, which is then passed on to a ResourceSnapshotConsumer - * Also write to file the raw data used to create the ResourceSnapshot. + * TODO: Write JSON blob of node repo somewhere + * @author olaa */ public class ResourceMeterMaintainer extends Maintainer { @@ -64,7 +59,7 @@ public class ResourceMeterMaintainer extends Maintainer { e -> new ResourceSnapshot(e.getValue(), timeStamp)) ); - writeRawData(nodes, timeStamp); + resourceSnapshotConsumer.consume(resourceSnapshots); } @@ -91,16 +86,7 @@ public class ResourceMeterMaintainer extends Maintainer { } private ApplicationId applicationIdFromNodeOwner(NodeOwner owner) { - return new ApplicationId(String.join(".", owner.getTenant(), owner.getApplication(), owner.getInstance())); - } - - private void writeRawData(List nodes, Instant timeStamp) { - try { - new ObjectMapper().writeValue(new File(String.format("/path/to/file-%s", timeStamp.toString())), nodes); - } catch (IOException e) { - throw new UncheckedIOException("Unable to write nodes to file", e); - } - + return ApplicationId.from(owner.getTenant(), owner.getApplication(), owner.getInstance()); } } diff --git a/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/integration/NodeRepositoryClientMock.java b/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/integration/NodeRepositoryClientMock.java index daddc46589d..ccd09cb9261 100644 --- a/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/integration/NodeRepositoryClientMock.java +++ b/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/integration/NodeRepositoryClientMock.java @@ -56,8 +56,8 @@ public class NodeRepositoryClientMock implements NodeRepositoryClientInterface { node.setMinDiskAvailableGb(500d); node.setMinMainMemoryAvailableGb(24d); NodeOwner owner = new NodeOwner(); - owner.tenant = "lsbe"; - owner.application = "local-search"; + owner.tenant = "tenant1"; + owner.application = "app1"; owner.instance = "default"; node.setOwner(owner); NodeMembership membership = new NodeMembership(); @@ -76,8 +76,8 @@ public class NodeRepositoryClientMock implements NodeRepositoryClientInterface { node.setMinDiskAvailableGb(500d); node.setMinMainMemoryAvailableGb(24d); NodeOwner owner = new NodeOwner(); - owner.tenant = "mediasearch"; - owner.application = "imagesearch"; + owner.tenant = "tenant2"; + owner.application = "app2"; owner.instance = "default"; node.setOwner(owner); NodeMembership membership = new NodeMembership(); diff --git a/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/maintenance/CostReportMaintainerTest.java b/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/maintenance/CostReportMaintainerTest.java index 01f3f55c679..890af974a0e 100644 --- a/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/maintenance/CostReportMaintainerTest.java +++ b/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/maintenance/CostReportMaintainerTest.java @@ -34,8 +34,8 @@ public class CostReportMaintainerTest { .build(); - tester.createTenant("lsbe", "local-search", 1L); - tester.createTenant("mediasearch", "msbe", 2L); + tester.createTenant("tenant1", "app1", 1L); + tester.createTenant("tenant2", "app2", 2L); CostReportMaintainer maintainer = new CostReportMaintainer( tester.controller(), Duration.ofDays(1), diff --git a/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/maintenance/ResourceMeterMaintainerTest.java b/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/maintenance/ResourceMeterMaintainerTest.java new file mode 100644 index 00000000000..9daad35f83a --- /dev/null +++ b/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/maintenance/ResourceMeterMaintainerTest.java @@ -0,0 +1,46 @@ +// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.hosted.controller.maintenance; + +import com.yahoo.vespa.hosted.controller.ControllerTester; +import com.yahoo.config.provision.ApplicationId; +import com.yahoo.vespa.hosted.controller.api.integration.resource.ResourceSnapshot; +import com.yahoo.vespa.hosted.controller.api.integration.stubs.MockResourceSnapshotConsumer; +import com.yahoo.vespa.hosted.controller.integration.NodeRepositoryClientMock; +import org.junit.Test; + +import java.time.Duration; +import java.util.Map; + +import static org.junit.Assert.*; + +/** + * @author olaa + */ +public class ResourceMeterMaintainerTest { + + private final double DELTA = 0.00001; + NodeRepositoryClientMock nodeRepository = new NodeRepositoryClientMock(); + MockResourceSnapshotConsumer snapshotConsumer = new MockResourceSnapshotConsumer(); + + @Test + public void testMaintainer() { + ControllerTester tester = new ControllerTester(); + ResourceMeterMaintainer resourceMeterMaintainer = new ResourceMeterMaintainer(tester.controller(), Duration.ofMinutes(5), new JobControl(tester.curator()), nodeRepository, tester.clock(), snapshotConsumer); + resourceMeterMaintainer.maintain(); + Map consumedResources = snapshotConsumer.consumedResources(); + + assertEquals(2, consumedResources.size()); + + ResourceSnapshot app1 = consumedResources.get(ApplicationId.from("tenant1", "app1", "default")); + ResourceSnapshot app2 = consumedResources.get(ApplicationId.from("tenant2", "app2", "default")); + + assertEquals(96, app1.getResourceAllocation().getCpuCores(), DELTA); + assertEquals(96, app1.getResourceAllocation().getMemoryGb(), DELTA); + assertEquals(2000, app1.getResourceAllocation().getDiskGb(), DELTA); + + assertEquals(160, app2.getResourceAllocation().getCpuCores(), DELTA); + assertEquals(96, app2.getResourceAllocation().getMemoryGb(), DELTA); + assertEquals(2000, app2.getResourceAllocation().getDiskGb(), DELTA); + + } +} \ No newline at end of file -- cgit v1.2.3