diff options
author | Harald Musum <musum@yahoo-inc.com> | 2017-02-07 11:26:06 +0100 |
---|---|---|
committer | Harald Musum <musum@yahoo-inc.com> | 2017-02-07 11:26:06 +0100 |
commit | 3b4639840b12849864a49a13bbd044eeb151dd6c (patch) | |
tree | d18dc581effec09e72253e70852af1684428cf13 /configserver | |
parent | f6991aba3fa82e88be9bd013a7883777bf6d0942 (diff) |
Use model to find hosts and services instead of config
Diffstat (limited to 'configserver')
2 files changed, 59 insertions, 74 deletions
diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/application/ApplicationConvergenceChecker.java b/configserver/src/main/java/com/yahoo/vespa/config/server/application/ApplicationConvergenceChecker.java index f883796804c..92b81fcca59 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/application/ApplicationConvergenceChecker.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/application/ApplicationConvergenceChecker.java @@ -3,8 +3,10 @@ package com.yahoo.vespa.config.server.application; import com.fasterxml.jackson.databind.JsonNode; import com.google.inject.Inject; -import com.yahoo.cloud.config.ModelConfig; import com.yahoo.component.AbstractComponent; +import com.yahoo.config.model.api.HostInfo; +import com.yahoo.config.model.api.PortInfo; +import com.yahoo.config.model.api.ServiceInfo; import com.yahoo.slime.Cursor; import com.yahoo.vespa.config.server.http.JSONResponse; import org.glassfish.jersey.client.proxy.WebResourceFactory; @@ -31,7 +33,7 @@ public class ApplicationConvergenceChecker extends AbstractComponent { private final StateApiFactory stateApiFactory; private final Client client = ClientBuilder.newClient(); - private final static Set<String> serviceTypes = new HashSet<>(Arrays.asList( + private final static Set<String> serviceTypesToCheck = new HashSet<>(Arrays.asList( "container", "container-clustercontroller", "qrserver", @@ -51,28 +53,20 @@ public class ApplicationConvergenceChecker extends AbstractComponent { } public ServiceListResponse serviceListToCheckForConfigConvergence(Application application, URI uri) { - List<Service> services = new ArrayList<>(); - Long wantedGeneration = application.getApplicationGeneration(); - try { - // Note: Uses latest config model version to get config - ModelConfig config = application.getConfig(ModelConfig.class, ""); - config.hosts() - .forEach(host -> host.services().stream() - .filter(service -> serviceTypes.contains(service.type())) - .forEach(service -> getStatePort(service).ifPresent( - port -> services.add(new Service(host.name(), port, service.type()))))); - return new ServiceListResponse(200, services, uri, wantedGeneration); - } catch (IOException e) { - return new ServiceListResponse(500, services, uri, wantedGeneration); - } + List<ServiceInfo> servicesToCheck = new ArrayList<>(); + application.getModel().getHosts() + .forEach(host -> host.getServices().stream() + .filter(service -> serviceTypesToCheck.contains(service.getServiceType())) + .forEach(service -> getStatePort(service).ifPresent(port -> servicesToCheck.add(service)))); + return new ServiceListResponse(200, servicesToCheck, uri, application.getApplicationGeneration()); } public ServiceResponse serviceConvergenceCheck(Application application, String hostAndPortToCheck, URI uri) { Long wantedGeneration = application.getApplicationGeneration(); - if ( ! hostInApplication(application, hostAndPortToCheck)) - return ServiceResponse.createHostNotFoundInAppResponse(uri, hostAndPortToCheck, wantedGeneration); - try { + if (! hostInApplication(application, hostAndPortToCheck)) + return ServiceResponse.createHostNotFoundInAppResponse(uri, hostAndPortToCheck, wantedGeneration); + long currentGeneration = getServiceGeneration(URI.create("http://" + hostAndPortToCheck)); boolean converged = currentGeneration >= wantedGeneration; return ServiceResponse.createOkResponse(uri, hostAndPortToCheck, wantedGeneration, currentGeneration, converged); @@ -99,9 +93,10 @@ public class ApplicationConvergenceChecker extends AbstractComponent { StateApi createStateApi(Client client, URI serviceUri); } - private Optional<Integer> getStatePort(ModelConfig.Hosts.Services service) { return service.ports().stream() - .filter(port -> port.tags().contains("state")) - .map(ModelConfig.Hosts.Services.Ports::number) + private static Optional<Integer> getStatePort(ServiceInfo service) { + return service.getPorts().stream() + .filter(port -> port.getTags().contains("state")) + .map(PortInfo::getPort) .findFirst(); } @@ -119,19 +114,12 @@ public class ApplicationConvergenceChecker extends AbstractComponent { return generationFromContainerState(state.config()); } - private boolean hostInApplication(Application application, String hostPort) { - final ModelConfig config; - try { - config = application.getConfig(ModelConfig.class, ""); - } catch (IOException e) { - throw new RuntimeException(e); - } - final List<ModelConfig.Hosts> hosts = config.hosts(); - for (ModelConfig.Hosts host : hosts) { - if (hostPort.startsWith(host.name())) { - for (ModelConfig.Hosts.Services service : host.services()) { - for (ModelConfig.Hosts.Services.Ports port : service.ports()) { - if (hostPort.equals(host.name() + ":" + port.number())) { + private boolean hostInApplication(Application application, String hostPort) throws IOException { + for (HostInfo host : application.getModel().getHosts()) { + if (hostPort.startsWith(host.getHostname())) { + for (ServiceInfo service : host.getServices()) { + for (PortInfo port : service.getPorts()) { + if (hostPort.equals(host.getHostname() + ":" + port.getPort())) { return true; } } @@ -144,15 +132,18 @@ public class ApplicationConvergenceChecker extends AbstractComponent { static class ServiceListResponse extends JSONResponse { final Cursor debug; - private ServiceListResponse(int status, List<Service> services, URI uri, Long wantedGeneration) { + // Pre-condition: servicesToCheck has a state port + private ServiceListResponse(int status, List<ServiceInfo> servicesToCheck, URI uri, Long wantedGeneration) { super(status); Cursor serviceArray = object.setArray("services"); - for (Service s : services) { + for (ServiceInfo s : servicesToCheck) { Cursor service = serviceArray.addObject(); - service.setString("host", s.hostname); - service.setLong("port", s.port); - service.setString("type", s.type); - service.setString("url", uri.toString() + "/" + s.hostname + ":" + s.port); + String hostName = s.getHostName(); + int statePort = getStatePort(s).get(); + service.setString("host", hostName); + service.setLong("port", statePort); + service.setString("type", s.getServiceType()); + service.setString("url", uri.toString() + "/" + hostName + ":" + statePort); } debug = object.setObject("debug"); object.setString("url", uri.toString()); @@ -197,16 +188,4 @@ public class ApplicationConvergenceChecker extends AbstractComponent { } } - private static class Service { - private final String hostname; - private final int port; - private final String type; - - private Service(String hostname, int port, String type) { - this.hostname = hostname; - this.port = port; - this.type = type; - } - } - } diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/application/ApplicationConvergenceCheckerTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/application/ApplicationConvergenceCheckerTest.java index 4a9d17a44c7..73f68023266 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/application/ApplicationConvergenceCheckerTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/application/ApplicationConvergenceCheckerTest.java @@ -3,13 +3,15 @@ package com.yahoo.vespa.config.server.application; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import com.yahoo.cloud.config.ModelConfig; import com.yahoo.config.codegen.InnerCNode; import com.yahoo.config.model.api.FileDistribution; import com.yahoo.config.model.api.HostInfo; import com.yahoo.config.model.api.Model; +import com.yahoo.config.model.api.PortInfo; +import com.yahoo.config.model.api.ServiceInfo; import com.yahoo.config.provision.ApplicationId; import com.yahoo.config.provision.ApplicationName; +import com.yahoo.config.provision.ClusterSpec; import com.yahoo.config.provision.InstanceName; import com.yahoo.config.provision.ProvisionInfo; import com.yahoo.config.provision.TenantName; @@ -29,7 +31,11 @@ import org.xml.sax.SAXException; import java.io.IOException; import java.net.URI; +import java.util.Arrays; import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import java.util.Optional; import java.util.Set; @@ -53,7 +59,7 @@ public class ApplicationConvergenceCheckerTest { @Before public void setup() throws IOException, SAXException, InterruptedException { - Model mockModel = new MockModel(1337); + Model mockModel = new MockModel("localhost", 1337); application = new Application(mockModel, new ServerCache(), 3, Version.fromIntValues(0, 0, 0), MetricUpdater.createTestUpdater(), appId); } @@ -106,36 +112,24 @@ public class ApplicationConvergenceCheckerTest { } } + // Model with two services, one that does not have a state port private static class MockModel implements Model { + private final String hostname; private final int statePort; - MockModel(int statePort) { + + MockModel(String hostname, int statePort) { + this.hostname = hostname; this.statePort = statePort; } @Override public ConfigPayload getConfig(ConfigKey<?> configKey, ConfigDefinition targetDef, ConfigPayload override) { - if (configKey.equals(new ConfigKey<>(ModelConfig.class, ""))) { - return createModelConfig(); - } throw new UnsupportedOperationException(); } @Override public ConfigPayload getConfig(ConfigKey<?> configKey, InnerCNode targetDef, ConfigPayload override) { - return getConfig(configKey, (ConfigDefinition)null, override); - } - - private ConfigPayload createModelConfig() { - ModelConfig.Builder builder = new ModelConfig.Builder(); - ModelConfig.Hosts.Builder hostBuilder = new ModelConfig.Hosts.Builder(); - hostBuilder.name("localhost"); - ModelConfig.Hosts.Services.Builder serviceBuilder = new ModelConfig.Hosts.Services.Builder(); - serviceBuilder.type("container"); - serviceBuilder.ports(new ModelConfig.Hosts.Services.Ports.Builder().number(statePort).tags("state")); - hostBuilder.services(serviceBuilder); - builder.hosts(hostBuilder); - ModelConfig config = new ModelConfig(builder); - return ConfigPayload.fromInstance(config); + throw new UnsupportedOperationException(); } @Override @@ -145,7 +139,19 @@ public class ApplicationConvergenceCheckerTest { @Override public Collection<HostInfo> getHosts() { - throw new UnsupportedOperationException(); + ServiceInfo container = createServiceInfo(hostname, "container", "container", + ClusterSpec.Type.container, statePort, "state"); + ServiceInfo serviceNoStatePort = createServiceInfo(hostname, "logserver", "logserver", + ClusterSpec.Type.admin, 1234, "logtp"); + return Collections.singleton(new HostInfo(hostname, Arrays.asList(container, serviceNoStatePort))); + } + + ServiceInfo createServiceInfo(String hostname, String name, String type, ClusterSpec.Type clusterType, int port, String portTags) { + PortInfo portInfo = new PortInfo(port, Collections.singleton(portTags)); + Map<String, String> properties = new HashMap<>(); + properties.put("clustername", "default"); + properties.put("clustertype", clusterType.name()); + return new ServiceInfo(name, type, Collections.singleton(portInfo), properties, "", hostname); } @Override |