LoadBalancerExpirer extends NodeRepositoryMaintainer { @Override protected boolean maintain() { expireReserved(); return removeInactive() & pruneReals(); } LoadBalancerExpirer(NodeRepository nodeRepository, Duration interval, LoadBalancerService service, Metric metric);   }
@Test public void expire_inactive() { LoadBalancerExpirer expirer = new LoadBalancerExpirer(tester.nodeRepository(), Duration.ofDays(1), tester.loadBalancerService(), new TestMetric()); Supplier<Map<LoadBalancerId, LoadBalancer>> loadBalancers = () -> tester.nodeRepository().database().readLoadBalancers((ignored) -> true); ClusterSpec.Id cluster1 = ClusterSpec.Id.from("qrs"); ClusterSpec.Id cluster2 = ClusterSpec.Id.from("qrs2"); ApplicationId app1 = ProvisioningTester.makeApplicationId(); ApplicationId app2 = ProvisioningTester.makeApplicationId(); LoadBalancerId lb1 = new LoadBalancerId(app1, cluster1); LoadBalancerId lb2 = new LoadBalancerId(app2, cluster1); LoadBalancerId lb3 = new LoadBalancerId(app2, cluster2); deployApplication(app1, cluster1); deployApplication(app2, cluster1, cluster2); assertEquals(3, loadBalancers.get().size()); removeApplication(app1); assertSame(LoadBalancer.State.inactive, loadBalancers.get().get(lb1).state()); assertNotSame(LoadBalancer.State.inactive, loadBalancers.get().get(lb2).state()); expirer.maintain(); assertEquals(3, tester.loadBalancerService().instances().size()); assertEquals(2, tester.loadBalancerService().instances().get(lb1).reals().size()); dirtyNodesOf(app1, cluster1); expirer.maintain(); assertEquals(Set.of(), tester.loadBalancerService().instances().get(lb1).reals()); assertEquals(Set.of(), loadBalancers.get().get(lb1).instance().reals()); expirer.maintain(); assertSame(LoadBalancer.State.inactive, loadBalancers.get().get(lb1).state()); assertTrue("Inactive load balancer not removed", tester.loadBalancerService().instances().containsKey(lb1)); tester.clock().advance(Duration.ofHours(1).plus(Duration.ofSeconds(1))); expirer.maintain(); assertFalse("Inactive load balancer removed", tester.loadBalancerService().instances().containsKey(lb1)); assertSame(LoadBalancer.State.active, loadBalancers.get().get(lb2).state()); assertTrue("Active load balancer is not removed", tester.loadBalancerService().instances().containsKey(lb2)); deployApplication(app2, cluster1); expirer.maintain(); assertSame(LoadBalancer.State.inactive, loadBalancers.get().get(lb3).state()); expirer.maintain(); assertEquals(2, tester.loadBalancerService().instances().size()); dirtyNodesOf(app2, cluster2); tester.clock().advance(Duration.ofHours(1).plus(Duration.ofSeconds(1))); expirer.maintain(); assertFalse("Inactive load balancer removed", tester.loadBalancerService().instances().containsKey(lb3)); }
@Test public void expire_reserved() { LoadBalancerExpirer expirer = new LoadBalancerExpirer(tester.nodeRepository(), Duration.ofDays(1), tester.loadBalancerService(), new TestMetric()); Supplier<Map<LoadBalancerId, LoadBalancer>> loadBalancers = () -> tester.nodeRepository().database().readLoadBalancers((ignored) -> true); ClusterSpec.Id cluster = ClusterSpec.Id.from("qrs"); ApplicationId app = ProvisioningTester.makeApplicationId(); LoadBalancerId lb = new LoadBalancerId(app, cluster); deployApplication(app, false, cluster); assertSame(LoadBalancer.State.reserved, loadBalancers.get().get(lb).state()); expirer.maintain(); assertSame(LoadBalancer.State.reserved, loadBalancers.get().get(lb).state()); dirtyNodesOf(app, cluster); tester.clock().advance(Duration.ofHours(1).plus(Duration.ofSeconds(1))); expirer.maintain(); assertSame(LoadBalancer.State.inactive, loadBalancers.get().get(lb).state()); expirer.maintain(); assertSame(LoadBalancer.State.inactive, loadBalancers.get().get(lb).state()); tester.clock().advance(Duration.ofHours(1).plus(Duration.ofSeconds(1))); expirer.maintain(); assertFalse("Inactive load balancer removed", loadBalancers.get().containsKey(lb)); }