diff options
author | Ola Aunrønning <olaa@verizonmedia.com> | 2019-09-02 16:41:03 +0200 |
---|---|---|
committer | Ola Aunrønning <olaa@verizonmedia.com> | 2019-09-02 16:41:03 +0200 |
commit | 11af4d659577a171b68ecffb1da6e37ab988026a (patch) | |
tree | 20cd8e9eed6d1698f1e80d21aea9fd4f331b92dc /configserver | |
parent | 27d80aa43044d15eb011a03767fc620ef325305b (diff) |
Extract metric retrieval out of ApplicationRepository
Diffstat (limited to 'configserver')
2 files changed, 83 insertions, 49 deletions
diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java b/configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java index de5e77fe849..c165705dc8c 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java @@ -44,9 +44,7 @@ import com.yahoo.vespa.config.server.http.LogRetriever; import com.yahoo.vespa.config.server.http.SimpleHttpFetcher; import com.yahoo.vespa.config.server.http.v2.MetricsResponse; import com.yahoo.vespa.config.server.http.v2.PrepareResult; -import com.yahoo.vespa.config.server.metrics.ClusterInfo; -import com.yahoo.vespa.config.server.metrics.MetricsAggregator; -import com.yahoo.vespa.config.server.metrics.MetricsRetriever; +import com.yahoo.vespa.config.server.metrics.ClusterMetricsRetriever; import com.yahoo.vespa.config.server.provision.HostProvisionerProvider; import com.yahoo.vespa.config.server.session.LocalSession; import com.yahoo.vespa.config.server.session.LocalSessionRepo; @@ -60,7 +58,6 @@ import com.yahoo.vespa.config.server.tenant.Rotations; import com.yahoo.vespa.config.server.tenant.Tenant; import com.yahoo.vespa.config.server.tenant.TenantRepository; import com.yahoo.vespa.curator.Lock; -import com.yahoo.vespa.model.admin.metricsproxy.MetricsProxyContainer; import com.yahoo.vespa.orchestrator.Orchestrator; import java.io.File; @@ -74,15 +71,10 @@ import java.time.Duration; import java.time.Instant; import java.util.Arrays; import java.util.Collection; -import java.util.HashMap; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Optional; import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ForkJoinPool; -import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Collectors; @@ -651,23 +643,8 @@ public class ApplicationRepository implements com.yahoo.config.provision.Deploye // ---------------- Metrics ------------------------------------------------------------------------ public MetricsResponse getMetrics(ApplicationId applicationId) { - var clusters = getClustersOfApplication(applicationId); - var clusterMetrics = new ConcurrentHashMap<ClusterInfo, MetricsAggregator>(); - ForkJoinPool pool = new ForkJoinPool(5); - pool.submit(() -> - clusters.parallelStream().forEach(cluster -> { - var metrics = MetricsRetriever.requestMetricsForCluster(cluster); - clusterMetrics.put(cluster, metrics); - })); - pool.shutdown(); - - try { - pool.awaitTermination(1, TimeUnit.MINUTES); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - - return new MetricsResponse(200, applicationId, clusterMetrics); + Application application = getApplication(applicationId); + return ClusterMetricsRetriever.getMetrics(application); } // ---------------- Misc operations ---------------------------------------------------------------- @@ -801,29 +778,6 @@ public class ApplicationRepository implements com.yahoo.config.provision.Deploye return port; } - /** Finds the hosts of an application, grouped by cluster name */ - private Collection<ClusterInfo> getClustersOfApplication(ApplicationId applicationId) { - Application application = getApplication(applicationId); - Map<String, ClusterInfo> clusters = new HashMap<>(); - application.getModel().getHosts().stream() - .filter(host -> host.getServices().stream().noneMatch(serviceInfo -> serviceInfo.getServiceType().equalsIgnoreCase("logserver"))) - .forEach(hostInfo -> { - ClusterInfo clusterInfo = createClusterInfo(hostInfo); - URI host = URI.create("http://" + hostInfo.getHostname() + ":" + MetricsProxyContainer.BASEPORT + "/metrics/v1/values?consumer=Vespa"); - clusters.computeIfAbsent(clusterInfo.getClusterId(), c -> clusterInfo).addHost(host); - } - ); - return clusters.values(); - - } - - private ClusterInfo createClusterInfo(HostInfo hostInfo) { - return hostInfo.getServices().stream() - .map(ClusterInfo::fromServiceInfo) - .filter(Optional::isPresent) - .findFirst().get().orElseThrow(); - } - /** Returns version to use when deploying application in given environment */ static Version decideVersion(ApplicationId application, Environment environment, Version sessionVersion, boolean bootstrap) { if ( environment.isManuallyDeployed() diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/metrics/ClusterMetricsRetriever.java b/configserver/src/main/java/com/yahoo/vespa/config/server/metrics/ClusterMetricsRetriever.java new file mode 100644 index 00000000000..50d5dd41d9c --- /dev/null +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/metrics/ClusterMetricsRetriever.java @@ -0,0 +1,80 @@ +// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.config.server.metrics; + +import com.yahoo.config.model.api.HostInfo; +import com.yahoo.config.model.api.ServiceInfo; +import com.yahoo.vespa.config.server.application.Application; +import com.yahoo.vespa.config.server.http.v2.MetricsResponse; +import com.yahoo.vespa.model.admin.metricsproxy.MetricsProxyContainer; + +import java.net.URI; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.TimeUnit; +import java.util.function.Predicate; + +/** + * @author olaa + * + * Retrieves metrics for given application, grouped by cluster + */ +public class ClusterMetricsRetriever { + + public static MetricsResponse getMetrics(Application application) { + var clusters = getClustersOfApplication(application); + var clusterMetrics = new ConcurrentHashMap<ClusterInfo, MetricsAggregator>(); + + Runnable retrieveMetricsJob = () -> + clusters.parallelStream().forEach(cluster -> { + MetricsAggregator metrics = MetricsRetriever.requestMetricsForCluster(cluster); + clusterMetrics.put(cluster, metrics); + }); + + ForkJoinPool threadPool = new ForkJoinPool(5); + threadPool.submit(retrieveMetricsJob); + threadPool.shutdown(); + + try { + threadPool.awaitTermination(1, TimeUnit.MINUTES); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + + return new MetricsResponse(200, application.getId(), clusterMetrics); + } + + /** Finds the hosts of an application, grouped by cluster name */ + private static Collection<ClusterInfo> getClustersOfApplication(Application application) { + Map<String, ClusterInfo> clusters = new HashMap<>(); + + application.getModel().getHosts().stream() + .filter(host -> host.getServices().stream().noneMatch(isLogserver())) + .forEach(hostInfo -> { + ClusterInfo clusterInfo = createClusterInfo(hostInfo); + URI metricsProxyURI = createMetricsProxyURI(hostInfo.getHostname()); + clusters.computeIfAbsent(clusterInfo.getClusterId(), c -> clusterInfo).addHost(metricsProxyURI); + } + ); + return clusters.values(); + + } + + private static Predicate<ServiceInfo> isLogserver() { + return serviceInfo -> serviceInfo.getServiceType().equalsIgnoreCase("logserver"); + } + + private static URI createMetricsProxyURI(String hostname) { + return URI.create("http://" + hostname + ":" + MetricsProxyContainer.BASEPORT + "/metrics/v1/values?consumer=Vespa"); + } + + private static ClusterInfo createClusterInfo(HostInfo hostInfo) { + return hostInfo.getServices().stream() + .map(ClusterInfo::fromServiceInfo) + .filter(Optional::isPresent) + .findFirst().get().orElseThrow(); + } +} |