diff options
author | Harald Musum <musum@verizonmedia.com> | 2020-06-10 22:20:39 +0200 |
---|---|---|
committer | Harald Musum <musum@verizonmedia.com> | 2020-06-10 22:20:39 +0200 |
commit | 5bb2383330c3fc8745c7ab64fdae860eb2ac34c1 (patch) | |
tree | 3ecaae502d30215dfb05decfdd8acc6f498a9098 /configserver | |
parent | 45cb4cc32358a45d1f6df24ceaa0f15019e34f0a (diff) |
Inject correct hostregistry
* Inject correct application host registry into SessionRepository
* Move Hosthandler logic into ApplicationRepository
Diffstat (limited to 'configserver')
5 files changed, 46 insertions, 57 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 ce6b4587ada..2bc3310442d 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 @@ -49,10 +49,10 @@ import com.yahoo.vespa.config.server.http.v2.PrepareResult; import com.yahoo.vespa.config.server.metrics.ApplicationMetricsRetriever; import com.yahoo.vespa.config.server.provision.HostProvisionerProvider; import com.yahoo.vespa.config.server.session.LocalSession; -import com.yahoo.vespa.config.server.session.SessionRepository; import com.yahoo.vespa.config.server.session.PrepareParams; import com.yahoo.vespa.config.server.session.RemoteSession; import com.yahoo.vespa.config.server.session.Session; +import com.yahoo.vespa.config.server.session.SessionRepository; import com.yahoo.vespa.config.server.session.SilentDeployLogger; import com.yahoo.vespa.config.server.tenant.ApplicationRolesStore; import com.yahoo.vespa.config.server.tenant.ContainerEndpointsCache; @@ -77,6 +77,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.logging.Level; @@ -738,6 +739,14 @@ public class ApplicationRepository implements com.yahoo.config.provision.Deploye return configserverConfig; } + public ApplicationId getApplicationIdForHostname(String hostname) { + Optional<ApplicationId> applicationId = tenantRepository.getAllTenantNames().stream() + .map(tenantName -> tenantRepository.getTenant(tenantName).getApplicationRepo().getApplicationIdForHostName(hostname)) + .filter(Objects::nonNull) + .findFirst(); + return applicationId.orElse(null); + } + private void validateThatLocalSessionIsNotActive(Tenant tenant, long sessionId) { LocalSession session = getLocalSession(tenant, sessionId); if (Session.Status.ACTIVATE.equals(session.getStatus())) { diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/application/TenantApplications.java b/configserver/src/main/java/com/yahoo/vespa/config/server/application/TenantApplications.java index d4fe35c14b1..9ed1e8ee88c 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/application/TenantApplications.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/application/TenantApplications.java @@ -19,6 +19,7 @@ import com.yahoo.vespa.config.server.ReloadHandler; import com.yahoo.vespa.config.server.ReloadListener; import com.yahoo.vespa.config.server.RequestHandler; import com.yahoo.vespa.config.server.deploy.TenantFileSystemDirs; +import com.yahoo.vespa.config.server.host.HostRegistries; import com.yahoo.vespa.config.server.host.HostRegistry; import com.yahoo.vespa.config.server.host.HostValidator; import com.yahoo.vespa.config.server.monitoring.MetricUpdater; @@ -419,5 +420,12 @@ public class TenantApplications implements RequestHandler, ReloadHandler, HostVa reloadListener.verifyHostsAreAvailable(tenant, newHosts); } + public HostRegistry<ApplicationId> getApplicationHostRegistry() { + return hostRegistry; + } + + public ApplicationId getApplicationIdForHostName(String hostname) { + return hostRegistry.getKeyForHost(hostname); + } } diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/HostHandler.java b/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/HostHandler.java index 2c888df6658..1ea41b85983 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/HostHandler.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/HostHandler.java @@ -3,61 +3,37 @@ package com.yahoo.vespa.config.server.http.v2; import com.google.inject.Inject; import com.yahoo.config.provision.ApplicationId; -import com.yahoo.config.provision.TenantName; import com.yahoo.config.provision.Zone; import com.yahoo.container.jdisc.HttpRequest; import com.yahoo.container.jdisc.HttpResponse; import com.yahoo.jdisc.Response; import com.yahoo.jdisc.application.BindingMatch; -import com.yahoo.vespa.config.server.GlobalComponentRegistry; -import com.yahoo.vespa.config.server.host.HostRegistries; -import com.yahoo.vespa.config.server.host.HostRegistry; +import com.yahoo.vespa.config.server.ApplicationRepository; import com.yahoo.vespa.config.server.http.HttpErrorResponse; import com.yahoo.vespa.config.server.http.HttpHandler; import com.yahoo.vespa.config.server.http.JSONResponse; -import java.util.logging.Level; - - /** * Handler for getting tenant and application for a given hostname. * * @author hmusum - * @since 5.19 */ public class HostHandler extends HttpHandler { - final HostRegistries hostRegistries; - private final Zone zone; + private final ApplicationRepository applicationRepository; @Inject - public HostHandler(HttpHandler.Context ctx, - GlobalComponentRegistry globalComponentRegistry) { + public HostHandler(HttpHandler.Context ctx, ApplicationRepository applicationRepository) { super(ctx); - this.hostRegistries = globalComponentRegistry.getHostRegistries(); - this.zone = globalComponentRegistry.getZone(); + this.applicationRepository = applicationRepository; } @Override public HttpResponse handleGET(HttpRequest request) { String hostname = getBindingMatch(request).group(2); - log.log(Level.FINE, "hostname=" + hostname); - - HostRegistry<TenantName> tenantHostRegistry = hostRegistries.getTenantHostRegistry(); - log.log(Level.FINE, "hosts in tenant host registry '" + tenantHostRegistry + "' " + tenantHostRegistry.getAllHosts()); - TenantName tenant = tenantHostRegistry.getKeyForHost(hostname); - if (tenant == null) return createError(hostname); - log.log(Level.FINE, "tenant=" + tenant); - HostRegistry<ApplicationId> applicationIdHostRegistry = hostRegistries.getApplicationHostRegistry(tenant); - ApplicationId applicationId; - if (applicationIdHostRegistry == null) return createError(hostname); - applicationId = applicationIdHostRegistry.getKeyForHost(hostname); - log.log(Level.FINE, "applicationId=" + applicationId); - if (applicationId == null) { - return createError(hostname); - } else { - log.log(Level.FINE, "hosts in application host registry '" + applicationIdHostRegistry + "' " + applicationIdHostRegistry.getAllHosts()); - return new HostResponse(Response.Status.OK, applicationId, zone); - } + ApplicationId applicationId = applicationRepository.getApplicationIdForHostname(hostname); + return (applicationId == null) + ? createError(hostname) + : new HostResponse(Response.Status.OK, applicationId, applicationRepository.zone()); } private HttpErrorResponse createError(String hostname) { diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/tenant/TenantRepository.java b/configserver/src/main/java/com/yahoo/vespa/config/server/tenant/TenantRepository.java index 35e6a58af9c..32e9f694027 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/tenant/TenantRepository.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/tenant/TenantRepository.java @@ -224,7 +224,7 @@ public class TenantRepository { SessionRepository sessionRepository = new SessionRepository(tenantName, componentRegistry, applicationRepo, reloadHandler, componentRegistry.getFlagSource(), - componentRegistry.getHostRegistries().createApplicationHostRegistry(tenantName), + applicationRepo, componentRegistry.getSessionPreparer()); log.log(Level.INFO, "Creating tenant '" + tenantName + "'"); Tenant tenant = new Tenant(tenantName, sessionRepository, requestHandler, diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/HostHandlerTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/HostHandlerTest.java index 457acf8c376..37181abfcf4 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/HostHandlerTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/HostHandlerTest.java @@ -12,8 +12,9 @@ import com.yahoo.config.provision.Zone; import com.yahoo.container.jdisc.HttpRequest; import com.yahoo.container.jdisc.HttpResponse; import com.yahoo.jdisc.Response; +import com.yahoo.vespa.config.server.ApplicationRepository; import com.yahoo.vespa.config.server.TestComponentRegistry; -import com.yahoo.vespa.config.server.host.HostRegistries; +import com.yahoo.vespa.config.server.application.OrchestratorMock; import com.yahoo.vespa.config.server.host.HostRegistry; import com.yahoo.vespa.config.server.http.HandlerTest; import com.yahoo.vespa.config.server.http.HttpErrorResponse; @@ -29,14 +30,13 @@ import org.junit.Test; import java.io.File; import java.io.IOException; +import java.time.Clock; import java.util.Collections; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; - /** * @author hmusum */ +// TODO: Try to move testing to ApplicationRepositoryTest and avoid all the low-level setup code here public class HostHandlerTest { private static final String urlPrefix = "http://myhost:14000/application/v2/host/"; private static File testApp = new File("src/test/apps/app"); @@ -44,9 +44,8 @@ public class HostHandlerTest { private HostHandler handler; private final static TenantName mytenant = TenantName.from("mytenant"); private final static String hostname = "testhost"; + private final static Zone zone = Zone.defaultZone(); private TenantRepository tenantRepository; - private HostRegistries hostRegistries; - private HostHandler hostHandler; static void addMockApplication(Tenant tenant, ApplicationId applicationId, long sessionId) { tenant.getApplicationRepo().createApplication(applicationId); @@ -61,32 +60,29 @@ public class HostHandlerTest { @Before public void setup() { - TestComponentRegistry componentRegistry = new TestComponentRegistry.Builder().build(); - tenantRepository = new TenantRepository(componentRegistry, false); - tenantRepository.addTenant(mytenant); - handler = createHostHandler(); - } - - private HostHandler createHostHandler() { final HostRegistry<TenantName> hostRegistry = new HostRegistry<>(); hostRegistry.update(mytenant, Collections.singletonList(hostname)); - TestComponentRegistry testComponentRegistry = new TestComponentRegistry.Builder().build(); - hostRegistries = testComponentRegistry.getHostRegistries(); - hostRegistries.createApplicationHostRegistry(mytenant).update(ApplicationId.from(mytenant, ApplicationName.defaultName(), InstanceName.defaultName()), Collections.singletonList(hostname)); - hostRegistries.getTenantHostRegistry().update(mytenant, Collections.singletonList(hostname)); - hostHandler = new HostHandler( - HostHandler.testOnlyContext(), - testComponentRegistry); - return hostHandler; + TestComponentRegistry componentRegistry = new TestComponentRegistry.Builder() + .zone(zone) + .build(); + tenantRepository = new TenantRepository(componentRegistry, false); + tenantRepository.addTenant(mytenant); + Tenant tenant = tenantRepository.getTenant(mytenant); + HostRegistry<ApplicationId> applicationHostRegistry = tenant.getApplicationRepo().getApplicationHostRegistry(); + applicationHostRegistry.update(ApplicationId.from(mytenant, ApplicationName.defaultName(), InstanceName.defaultName()), Collections.singletonList(hostname)); + ApplicationRepository applicationRepository = new ApplicationRepository(tenantRepository, + new SessionHandlerTest.MockProvisioner(), + new OrchestratorMock(), + Clock.systemUTC()); + handler = new HostHandler(HostHandler.testOnlyContext(), applicationRepository); } @Test public void require_correct_tenant_and_application_for_hostname() throws Exception { - assertThat(hostRegistries, is(hostHandler.hostRegistries)); long sessionId = 1; ApplicationId id = ApplicationId.from(mytenant, ApplicationName.defaultName(), InstanceName.defaultName()); addMockApplication(tenantRepository.getTenant(mytenant), id, sessionId); - assertApplicationForHost(hostname, mytenant, id, Zone.defaultZone()); + assertApplicationForHost(hostname, mytenant, id, zone); } @Test |