Invert neste map once, and store it
@@ -4,9 +4,13 @@ package com.yahoo.vespa.service.monitor;
import com.yahoo.vespa.applicationmodel.ApplicationInstance;
import com.yahoo.vespa.applicationmodel.ApplicationInstanceReference;
import com.yahoo.vespa.applicationmodel.HostName;
+import com.yahoo.vespa.applicationmodel.ServiceCluster;
import com.yahoo.vespa.applicationmodel.ServiceInstance;
+import java.util.Arrays;
+import java.util.Collection;
import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -20,9 +24,11 @@ import java.util.stream.Collectors;
public class ServiceModel {
private final Map<ApplicationInstanceReference, ApplicationInstance> applications;
+ private final Map<HostName, ApplicationInstance> applicationsByHostName;
public ServiceModel(Map<ApplicationInstanceReference, ApplicationInstance> applications) {
this.applications = Collections.unmodifiableMap(applications);
+ this.applicationsByHostName = Collections.unmodifiableMap(applicationsByHostNames(applications.values()));
}
public Map<ApplicationInstanceReference, ApplicationInstance> getAllApplicationInstances() {
@@ -33,6 +39,10 @@ public class ServiceModel {
return Optional.ofNullable(applications.get(reference));
}
+ public Map<HostName, ApplicationInstance> getApplicationsByHostName() {
+ return applicationsByHostName;
+ }
+
public Map<HostName, List<ServiceInstance>> getServiceInstancesByHostName() {
return applications.values().stream()
.flatMap(application -> application.serviceClusters().stream())
@@ -40,4 +50,17 @@ public class ServiceModel {
.collect(Collectors.groupingBy(ServiceInstance::hostName, Collectors.toList()));
}
+ private static Map<HostName, ApplicationInstance> applicationsByHostNames(Collection<ApplicationInstance> applications) {
+ Map<HostName, ApplicationInstance> hosts = new HashMap<>();
+ for (ApplicationInstance application : applications)
+ for (ServiceCluster cluster : application.serviceClusters())
+ for (ServiceInstance instance : cluster.serviceInstances()) {
+ ApplicationInstance previous = hosts.put(instance.hostName(), application);
+ if (previous != null && ! previous.equals(application))
+ throw new IllegalStateException("Major assumption broken: Multiple application instances contain host " +
+ instance.hostName().s() + ": " + Arrays.asList(previous, application));
+ }
+ return hosts;
+ }
+
}
