diff options
author | Valerij Fredriksen <freva@users.noreply.github.com> | 2019-03-06 14:01:00 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-06 14:01:00 +0100 |
commit | 5a80339c63c738c0cf4c37b144972c64d0decf78 (patch) | |
tree | 28ed1166a375fbf885c6204a6408e3e58b6d59bb /configserver | |
parent | 9d0e8407ca0babd1b3fe45d02453b2bfacb3602b (diff) | |
parent | eeada5698e292cdbf311a487d981a35b4c0cd96e (diff) |
Merge pull request #8684 from vespa-engine/freva/add-log-with-hostname-to-application-handler
Get logs from given hostname rather than logserver node
Diffstat (limited to 'configserver')
3 files changed, 46 insertions, 27 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 a31e0bf2fa4..61bbc145740 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 @@ -28,8 +28,8 @@ import com.yahoo.path.Path; import com.yahoo.slime.Slime; import com.yahoo.transaction.NestedTransaction; import com.yahoo.vespa.config.server.application.Application; -import com.yahoo.vespa.config.server.application.ConfigConvergenceChecker; import com.yahoo.vespa.config.server.application.ApplicationSet; +import com.yahoo.vespa.config.server.application.ConfigConvergenceChecker; import com.yahoo.vespa.config.server.application.FileDistributionStatus; import com.yahoo.vespa.config.server.application.HttpProxy; import com.yahoo.vespa.config.server.application.TenantApplications; @@ -98,6 +98,7 @@ public class ApplicationRepository implements com.yahoo.config.provision.Deploye private final ConfigserverConfig configserverConfig; private final FileDistributionStatus fileDistributionStatus; private final Orchestrator orchestrator; + private final LogRetriever logRetriever = new LogRetriever(); @Inject public ApplicationRepository(TenantRepository tenantRepository, @@ -459,9 +460,8 @@ public class ApplicationRepository implements com.yahoo.config.provision.Deploye // ---------------- Logs ---------------------------------------------------------------- - public HttpResponse getLogs(ApplicationId applicationId, String apiParams) { - String logServerURI = getLogServerURI(applicationId) + apiParams; - LogRetriever logRetriever = new LogRetriever(); + public HttpResponse getLogs(ApplicationId applicationId, Optional<String> hostname, String apiParams) { + String logServerURI = getLogServerURI(applicationId, hostname) + apiParams; return logRetriever.getLogs(logServerURI); } @@ -682,15 +682,26 @@ public class ApplicationRepository implements com.yahoo.config.provision.Deploye } } - private String getLogServerURI(ApplicationId applicationId) { + private String getLogServerURI(ApplicationId applicationId, Optional<String> hostname) { Application application = getApplication(applicationId); Collection<HostInfo> hostInfos = application.getModel().getHosts(); + // In ServiceInfo: node-admin does not have + // 1) Correct ports + // 2) logserver + // Assume if hostname is set that this is node-admin hostname + // TODO: Fix and simplify this once the above to problems have been fixed + if (hostname.isPresent()) { + HostInfo logServerHostInfo = hostInfos.stream() + .filter(host -> host.getHostname().equalsIgnoreCase(hostname.get())) + .findFirst().orElseThrow(() -> + new IllegalArgumentException("Host " + hostname.get() + " does not belong to " + applicationId)); + return "http://" + logServerHostInfo.getHostname() + ":8080/logs"; + } + HostInfo logServerHostInfo = hostInfos.stream() .filter(host -> host.getServices().stream() - .filter(serviceInfo -> - serviceInfo.getServiceType().equalsIgnoreCase("logserver")) - .count() > 0) + .anyMatch(serviceInfo -> serviceInfo.getServiceType().equalsIgnoreCase("logserver"))) .findFirst().orElseThrow(() -> new IllegalArgumentException("Could not find HostInfo for LogServer")); ServiceInfo containerServiceInfo = logServerHostInfo.getServices().stream() @@ -698,8 +709,7 @@ public class ApplicationRepository implements com.yahoo.config.provision.Deploye .findFirst().orElseThrow(() -> new IllegalArgumentException("No container running on logserver host")); int port = containerServiceInfo.getPorts().stream() - .filter(portInfo -> portInfo.getTags().stream() - .filter(tag -> tag.equalsIgnoreCase("http")).count() > 0) + .filter(portInfo -> portInfo.getTags().stream().anyMatch(tag -> tag.equalsIgnoreCase("http"))) .findFirst().orElseThrow(() -> new IllegalArgumentException("Could not find HTTP port")) .getPort(); diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandler.java b/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandler.java index be99212c176..e04f83cc648 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandler.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandler.java @@ -2,7 +2,6 @@ package com.yahoo.vespa.config.server.http.v2; import com.google.inject.Inject; - import com.yahoo.config.application.api.ApplicationFile; import com.yahoo.config.provision.ApplicationId; import com.yahoo.config.provision.ApplicationName; @@ -23,6 +22,7 @@ import com.yahoo.vespa.config.server.http.NotFoundException; import com.yahoo.vespa.config.server.tenant.Tenant; import java.time.Duration; +import java.util.Optional; /** * Operations on applications (delete, wait for config convergence, restart, application content etc.) @@ -97,9 +97,9 @@ public class ApplicationHandler extends HttpHandler { } if (isLogRequest(request)) { - String apiParams = request.getUri().getQuery(); - apiParams = apiParams == null ? "" : "?" + apiParams; - return applicationRepository.getLogs(applicationId, apiParams); + Optional<String> hostname = Optional.ofNullable(request.getProperty("hostname")); + String apiParams = Optional.ofNullable(request.getUri().getQuery()).map(q -> "?" + q).orElse(""); + return applicationRepository.getLogs(applicationId, hostname, apiParams); } if (isIsSuspendedRequest(request)) { diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/ApplicationRepositoryTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/ApplicationRepositoryTest.java index 90cd80604ca..5d356ddf88e 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/ApplicationRepositoryTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/ApplicationRepositoryTest.java @@ -1,8 +1,7 @@ // Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.config.server; -import com.github.tomakehurst.wiremock.WireMockServer; -import com.github.tomakehurst.wiremock.client.WireMock; +import com.github.tomakehurst.wiremock.junit.WireMockRule; import com.google.common.io.Files; import com.yahoo.cloud.config.ConfigserverConfig; import com.yahoo.component.Version; @@ -49,9 +48,8 @@ import java.util.Set; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.get; -import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; -import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -85,6 +83,9 @@ public class ApplicationRepositoryTest { @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); + @Rule + public final WireMockRule wireMock = new WireMockRule(options().port(8080), true); + @Before public void setup() { Curator curator = new MockCurator(); @@ -150,17 +151,25 @@ public class ApplicationRepositoryTest { @Test public void getLogs() { - WireMockServer wireMock = new WireMockServer(wireMockConfig().port(8080)); - wireMock.start(); - WireMock.configureFor("localhost", wireMock.port()); - stubFor(get(urlEqualTo("/logs")) - .willReturn(aResponse() - .withStatus(200))); - wireMock.start(); + wireMock.stubFor(get(urlEqualTo("/logs")).willReturn(aResponse().withStatus(200))); + deployApp(testAppLogServerWithContainer); + HttpResponse response = applicationRepository.getLogs(applicationId(), Optional.empty(), ""); + assertEquals(200, response.getStatus()); + } + + @Test + public void getLogsForHostname() { + wireMock.stubFor(get(urlEqualTo("/logs")).willReturn(aResponse().withStatus(200))); + deployApp(testAppLogServerWithContainer); + HttpResponse response = applicationRepository.getLogs(applicationId(), Optional.of("localhost"), ""); + assertEquals(200, response.getStatus()); + } + + @Test(expected = IllegalArgumentException.class) + public void refuseToGetLogsFromHostnameNotInApplication() { deployApp(testAppLogServerWithContainer); - HttpResponse response = applicationRepository.getLogs(applicationId(), ""); + HttpResponse response = applicationRepository.getLogs(applicationId(), Optional.of("host123.fake.yahoo.com"), ""); assertEquals(200, response.getStatus()); - wireMock.stop(); } @Test |