From 0f34ad1955e9193461e5c669df3600ae4ca71c6b Mon Sep 17 00:00:00 2001 From: Jon Marius Venstad Date: Tue, 2 Jan 2018 15:57:26 +0100 Subject: Revert "Revert "Jvenstad/zone cleanup 4"" --- .../controller/api/identifiers/DeploymentId.java | 2 +- .../controller/api/integration/MetricsService.java | 2 +- .../api/integration/zone/ZoneFilter.java | 22 +++++++ .../api/integration/zone/ZoneFilterMock.java | 72 +++++++++++++++++++++ .../controller/api/integration/zone/ZoneId.java | 73 ++++++++++++++++++++++ .../controller/api/integration/zone/ZoneList.java | 34 ++++++++++ .../api/integration/zone/ZoneRegistry.java | 6 +- .../controller/api/identifiers/IdentifierTest.java | 4 +- 8 files changed, 206 insertions(+), 9 deletions(-) create mode 100644 controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/zone/ZoneFilter.java create mode 100644 controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/zone/ZoneFilterMock.java create mode 100644 controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/zone/ZoneId.java create mode 100644 controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/zone/ZoneList.java (limited to 'controller-api') diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/identifiers/DeploymentId.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/identifiers/DeploymentId.java index f1c584c7a3c..04dd91670c1 100644 --- a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/identifiers/DeploymentId.java +++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/identifiers/DeploymentId.java @@ -1,7 +1,7 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.hosted.controller.api.identifiers; -import com.yahoo.config.provision.ZoneId; +import com.yahoo.vespa.hosted.controller.api.integration.zone.ZoneId; import java.util.Objects; diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/MetricsService.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/MetricsService.java index d3e1b881bbd..20e9710f092 100644 --- a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/MetricsService.java +++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/MetricsService.java @@ -2,7 +2,7 @@ package com.yahoo.vespa.hosted.controller.api.integration; import com.yahoo.config.provision.ApplicationId; -import com.yahoo.config.provision.ZoneId; +import com.yahoo.vespa.hosted.controller.api.integration.zone.ZoneId; import java.util.Map; diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/zone/ZoneFilter.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/zone/ZoneFilter.java new file mode 100644 index 00000000000..f718b86ca40 --- /dev/null +++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/zone/ZoneFilter.java @@ -0,0 +1,22 @@ +package com.yahoo.vespa.hosted.controller.api.integration.zone; + +/** + * A ZoneId list which can be filtered in various ways; elements can be accessed after at least one filter. + * + * The methods here return instances of {@link ZoneList}, which extends ZoneFilter, but with accessors and additional filters. + * This forces the developer to consider which of the filters in this class to apply, prior to processing any zones. + * + * @author jvenstad + */ +public interface ZoneFilter { + + /** Negates the next filter. */ + ZoneFilter not(); + + /** All zones from the initial pool. */ + ZoneList all(); + + /** Zones where which are managed by the controller. */ + ZoneList controllerManaged(); + +} diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/zone/ZoneFilterMock.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/zone/ZoneFilterMock.java new file mode 100644 index 00000000000..e68bf0ccc24 --- /dev/null +++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/zone/ZoneFilterMock.java @@ -0,0 +1,72 @@ +package com.yahoo.vespa.hosted.controller.api.integration.zone; + +import com.yahoo.config.provision.Environment; +import com.yahoo.config.provision.RegionName; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.function.Predicate; +import java.util.stream.Collectors; + +/** + * A Zones.List implementation which assumes all zones are controllerManaged. + * + * @author jvenstad + */ +public class ZoneFilterMock implements ZoneList { + + private final java.util.List zones; + private final boolean negate; + + private ZoneFilterMock(java.util.List zones, boolean negate) { + this.negate = negate; + this.zones = zones; + } + + public static ZoneFilter from(Collection zones) { + return new ZoneFilterMock(new ArrayList<>(zones), false); + } + + @Override + public ZoneList not() { + return new ZoneFilterMock(zones, ! negate); + } + + @Override + public ZoneList all() { + return filter(zoneId -> true); + } + + @Override + public ZoneList controllerManaged() { + return all(); + } + + @Override + public ZoneList in(Environment environment) { + return filter(zoneId -> zoneId.environment() == environment); + } + + @Override + public ZoneList in(RegionName region) { + return filter(zoneId -> zoneId.region().equals(region)); + } + + @Override + public ZoneList zones(ZoneId... zones) { + return filter(zoneId -> new HashSet<>(Arrays.asList(zones)).contains(zoneId)); + } + + @Override + public java.util.List ids() { + return Collections.unmodifiableList(zones); + } + + private ZoneFilterMock filter(Predicate condition) { + return new ZoneFilterMock(zones.stream().filter(negate ? condition.negate() : condition).collect(Collectors.toList()), false); + } + +} diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/zone/ZoneId.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/zone/ZoneId.java new file mode 100644 index 00000000000..21ac7a654b8 --- /dev/null +++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/zone/ZoneId.java @@ -0,0 +1,73 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.hosted.controller.api.integration.zone; + +import com.yahoo.config.provision.Environment; +import com.yahoo.config.provision.RegionName; + +import java.util.Objects; + +/** + * Unique identifier for a Zone; use when referencing them. + * + * Serialised form is 'environment.region'. + * + * @author jvenstad + */ +public class ZoneId { + // TODO: Replace usages of environment + region with usages of this. + + private final Environment environment; + private final RegionName region; + + private ZoneId(Environment environment, RegionName region) { + this.environment = Objects.requireNonNull(environment); + this.region = Objects.requireNonNull(region); + } + + public static ZoneId from(Environment environment, RegionName region) { + return new ZoneId(environment, region); + } + + public static ZoneId from(String environment, String region) { + return from(Environment.from(environment), RegionName.from(region)); + } + /** Create from a serialised ZoneId. Inverse of {@code ZoneId.value()}. */ + public static ZoneId from(String value) { + String[] parts = value.split("\\."); + return from(parts[0], parts[1]); + } + + public Environment environment() { + return environment; + } + + public RegionName region() { + return region; + } + + /** Returns the serialised value of this. Inverse of {@code ZoneId.from(String value)}. */ + public String value() { + return environment + "." + region; + } + + @Override + public String toString() { + return "zone " + value(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if ( ! (o instanceof ZoneId)) return false; + ZoneId id = (ZoneId) o; + return environment == id.environment && + Objects.equals(region, id.region); + } + + @Override + public int hashCode() { + return Objects.hash(environment, region); + } + +} + diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/zone/ZoneList.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/zone/ZoneList.java new file mode 100644 index 00000000000..cd263769864 --- /dev/null +++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/zone/ZoneList.java @@ -0,0 +1,34 @@ +package com.yahoo.vespa.hosted.controller.api.integration.zone; + +import com.yahoo.config.provision.Environment; +import com.yahoo.config.provision.RegionName; + +import java.util.List; + +/** + * Provides filters for and access to a list of ZoneIds. + * + * This is typically offered after an initial filter from {@link ZoneFilter} has been applied. + * This forces the developer to consider which zones to process. + * + * @author jvenstad + */ +public interface ZoneList extends ZoneFilter { + + /** Negates the next filter. */ + @Override + ZoneList not(); + + /** Zones in the given environment. */ + ZoneList in(Environment environment); + + /** Zones in the given region. */ + ZoneList in(RegionName region); + + /** Only the given zones — combine with not() for best effect! */ + ZoneList zones(ZoneId... zones); + + /** Returns the id of all zones in this list as — you guessed it — a list. */ + List ids(); + +} diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/zone/ZoneRegistry.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/zone/ZoneRegistry.java index af7c464b8d7..03207d86983 100644 --- a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/zone/ZoneRegistry.java +++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/zone/ZoneRegistry.java @@ -1,11 +1,9 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.hosted.controller.api.integration.zone; -import com.yahoo.config.provision.ApplicationId; import com.yahoo.config.provision.Environment; import com.yahoo.config.provision.RegionName; import com.yahoo.config.provision.SystemName; -import com.yahoo.config.provision.ZoneId; import com.yahoo.vespa.hosted.controller.api.identifiers.DeploymentId; import java.net.URI; @@ -24,7 +22,7 @@ public interface ZoneRegistry { boolean hasZone(ZoneId zoneId); /** Returns a list containing the id of all zones in this registry. */ - List zones(); + ZoneFilter zones(); /** Returns the default region for the given environment, if one is configured. */ Optional getDefaultRegion(Environment environment); @@ -39,7 +37,7 @@ public interface ZoneRegistry { /** Returns a list with all known config servers in the given zone, with a secure connection URL. */ List getConfigServerSecureUris(ZoneId zoneId); - /** Returns a URL with the logs for the given deployment, if loggin is configured for its zone. */ + /** Returns a URL with the logs for the given deployment, if logging is configured for its zone. */ Optional getLogServerUri(DeploymentId deploymentId); /** Returns the time to live for deployments in the given zone, or empty if this is infinite. */ diff --git a/controller-api/src/test/java/com/yahoo/vespa/hosted/controller/api/identifiers/IdentifierTest.java b/controller-api/src/test/java/com/yahoo/vespa/hosted/controller/api/identifiers/IdentifierTest.java index aa3d1be879e..0ba607a235b 100644 --- a/controller-api/src/test/java/com/yahoo/vespa/hosted/controller/api/identifiers/IdentifierTest.java +++ b/controller-api/src/test/java/com/yahoo/vespa/hosted/controller/api/identifiers/IdentifierTest.java @@ -1,9 +1,7 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.hosted.controller.api.identifiers; -import com.yahoo.config.provision.Environment; -import com.yahoo.config.provision.RegionName; -import com.yahoo.config.provision.ZoneId; +import com.yahoo.vespa.hosted.controller.api.integration.zone.ZoneId; import org.junit.Test; import static org.junit.Assert.assertEquals; -- cgit v1.2.3