Repository: reef Updated Branches: refs/heads/master 8e140c720 -> a59070fa0
[REEF-1528] Refactor dependency between yarn runtime and reef-webserver projects * Revert the dependency between reef-webserver and reef-runtime * Fix tests * Add NoOp implementation for the HttpServer * Change default implementation of TrackingURLProvider to HttpTrackingURLProvider * Change HttpTrackingURLProvider to return empty URL when http server is not configured (DefaultHttpServerImpl is configured) * Minor code clean-up JIRA: [REEF-1528](https://issues.apache.org/jira/browse/REEF-1528) Project: http://git-wip-us.apache.org/repos/asf/reef/repo Commit: http://git-wip-us.apache.org/repos/asf/reef/commit/a59070fa Tree: http://git-wip-us.apache.org/repos/asf/reef/tree/a59070fa Diff: http://git-wip-us.apache.org/repos/asf/reef/diff/a59070fa Branch: refs/heads/master Commit: a59070fa0829eb8d211f8140e3df9f1b70e4a6e1 Parents: 8e140c7 Author: Boris Shulman <[email protected]> Authored: Mon Aug 15 15:32:18 2016 -0700 Committer: Sergiy Matusevych <[email protected]> Committed: Thu Aug 18 14:43:35 2016 -0700 ---------------------------------------------------------------------- .../common/driver/EvaluatorRequestorImpl.java | 3 +- lang/java/reef-runtime-yarn/pom.xml | 10 ++ .../yarn/driver/DefaultTrackingURLProvider.java | 33 ------ .../yarn/driver/HttpTrackingURLProvider.java | 72 ++++++++++++ .../yarn/driver/TrackingURLProvider.java | 2 +- .../runtime/yarn/driver/TestTrackingUri.java | 116 +++++++++++++++++++ .../driver/YarnResourceRequestHandlerTest.java | 33 +++--- lang/java/reef-webserver/pom.xml | 5 - .../webserver/HttpHandlerConfiguration.java | 2 - .../org/apache/reef/webserver/HttpServer.java | 3 + .../reef/webserver/HttpTrackingURLProvider.java | 67 ----------- .../reef/webserver/NoOpHttpServerImpl.java | 49 ++++++++ .../apache/reef/webserver/TestTrackingUri.java | 114 ------------------ 13 files changed, 269 insertions(+), 240 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/reef/blob/a59070fa/lang/java/reef-common/src/main/java/org/apache/reef/runtime/common/driver/EvaluatorRequestorImpl.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-common/src/main/java/org/apache/reef/runtime/common/driver/EvaluatorRequestorImpl.java b/lang/java/reef-common/src/main/java/org/apache/reef/runtime/common/driver/EvaluatorRequestorImpl.java index 58f5a8a..b2faf98 100644 --- a/lang/java/reef-common/src/main/java/org/apache/reef/runtime/common/driver/EvaluatorRequestorImpl.java +++ b/lang/java/reef-common/src/main/java/org/apache/reef/runtime/common/driver/EvaluatorRequestorImpl.java @@ -47,6 +47,7 @@ public final class EvaluatorRequestorImpl implements EvaluatorRequestor { /** * @param resourceCatalog * @param resourceRequestHandler + * @param loggingScopeFactory */ @Inject public EvaluatorRequestorImpl(final ResourceCatalog resourceCatalog, @@ -98,7 +99,7 @@ public final class EvaluatorRequestorImpl implements EvaluatorRequestor { relaxLocality = false; } - try (LoggingScope ls = loggingScopeFactory.evaluatorSubmit(req.getNumber())) { + try (LoggingScope ls = this.loggingScopeFactory.evaluatorSubmit(req.getNumber())) { final ResourceRequestEvent request = ResourceRequestEventImpl .newBuilder() .setResourceCount(req.getNumber()) http://git-wip-us.apache.org/repos/asf/reef/blob/a59070fa/lang/java/reef-runtime-yarn/pom.xml ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-yarn/pom.xml b/lang/java/reef-runtime-yarn/pom.xml index fc1b51a..4492457 100644 --- a/lang/java/reef-runtime-yarn/pom.xml +++ b/lang/java/reef-runtime-yarn/pom.xml @@ -42,6 +42,11 @@ under the License. </dependency> <dependency> <groupId>${project.groupId}</groupId> + <artifactId>reef-webserver</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> <artifactId>reef-utils-hadoop</artifactId> <version>${project.version}</version> </dependency> @@ -65,6 +70,11 @@ under the License. <artifactId>mockito-all</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> </dependencies> <build> http://git-wip-us.apache.org/repos/asf/reef/blob/a59070fa/lang/java/reef-runtime-yarn/src/main/java/org/apache/reef/runtime/yarn/driver/DefaultTrackingURLProvider.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-yarn/src/main/java/org/apache/reef/runtime/yarn/driver/DefaultTrackingURLProvider.java b/lang/java/reef-runtime-yarn/src/main/java/org/apache/reef/runtime/yarn/driver/DefaultTrackingURLProvider.java deleted file mode 100644 index af2442a..0000000 --- a/lang/java/reef-runtime-yarn/src/main/java/org/apache/reef/runtime/yarn/driver/DefaultTrackingURLProvider.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.reef.runtime.yarn.driver; - -import javax.inject.Inject; - -final class DefaultTrackingURLProvider implements TrackingURLProvider { - - @Inject - DefaultTrackingURLProvider() { - } - - @Override - public String getTrackingUrl() { - return ""; - } -} http://git-wip-us.apache.org/repos/asf/reef/blob/a59070fa/lang/java/reef-runtime-yarn/src/main/java/org/apache/reef/runtime/yarn/driver/HttpTrackingURLProvider.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-yarn/src/main/java/org/apache/reef/runtime/yarn/driver/HttpTrackingURLProvider.java b/lang/java/reef-runtime-yarn/src/main/java/org/apache/reef/runtime/yarn/driver/HttpTrackingURLProvider.java new file mode 100644 index 0000000..d7829bb --- /dev/null +++ b/lang/java/reef-runtime-yarn/src/main/java/org/apache/reef/runtime/yarn/driver/HttpTrackingURLProvider.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.reef.runtime.yarn.driver; + +import org.apache.reef.webserver.NoOpHttpServerImpl; +import org.apache.reef.webserver.HttpServer; + +import javax.inject.Inject; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Http TrackingURLProvider. + */ +public final class HttpTrackingURLProvider implements TrackingURLProvider { + /** + * Standard Java logger. + */ + private static final Logger LOG = Logger.getLogger(HttpTrackingURLProvider.class.getName()); + + /** + * HttpServer. + */ + private final HttpServer httpServer; + + /** + * Constructor of HttpTrackingURLProvider. + * + * @param httpServer + */ + @Inject + public HttpTrackingURLProvider(final HttpServer httpServer) { + this.httpServer = httpServer instanceof NoOpHttpServerImpl ? null : httpServer; + } + + /** + * get tracking URI. + * + * @return + */ + @Override + public String getTrackingUrl() { + if (this.httpServer == null) { + return ""; + } + + try { + return InetAddress.getLocalHost().getHostAddress() + ":" + httpServer.getPort(); + } catch (final UnknownHostException e) { + LOG.log(Level.WARNING, "Cannot get host address.", e); + throw new RuntimeException("Cannot get host address.", e); + } + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/a59070fa/lang/java/reef-runtime-yarn/src/main/java/org/apache/reef/runtime/yarn/driver/TrackingURLProvider.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-yarn/src/main/java/org/apache/reef/runtime/yarn/driver/TrackingURLProvider.java b/lang/java/reef-runtime-yarn/src/main/java/org/apache/reef/runtime/yarn/driver/TrackingURLProvider.java index 068abd6..47d1e40 100644 --- a/lang/java/reef-runtime-yarn/src/main/java/org/apache/reef/runtime/yarn/driver/TrackingURLProvider.java +++ b/lang/java/reef-runtime-yarn/src/main/java/org/apache/reef/runtime/yarn/driver/TrackingURLProvider.java @@ -23,7 +23,7 @@ import org.apache.reef.tang.annotations.DefaultImplementation; /** * Implement this interface to set the tracking URL reported to YARN. */ -@DefaultImplementation(DefaultTrackingURLProvider.class) +@DefaultImplementation(HttpTrackingURLProvider.class) public interface TrackingURLProvider { String getTrackingUrl(); } http://git-wip-us.apache.org/repos/asf/reef/blob/a59070fa/lang/java/reef-runtime-yarn/src/main/test/java/org/apache/reef/runtime/yarn/driver/TestTrackingUri.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-yarn/src/main/test/java/org/apache/reef/runtime/yarn/driver/TestTrackingUri.java b/lang/java/reef-runtime-yarn/src/main/test/java/org/apache/reef/runtime/yarn/driver/TestTrackingUri.java new file mode 100644 index 0000000..c97c02c --- /dev/null +++ b/lang/java/reef-runtime-yarn/src/main/test/java/org/apache/reef/runtime/yarn/driver/TestTrackingUri.java @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.reef.runtime.yarn.driver; + +import org.apache.reef.tang.Injector; +import org.apache.reef.tang.JavaConfigurationBuilder; +import org.apache.reef.tang.Tang; +import org.apache.reef.tang.exceptions.BindException; +import org.apache.reef.tang.exceptions.InjectionException; +import org.apache.reef.wake.remote.ports.parameters.TcpPortRangeBegin; +import org.apache.reef.webserver.HttpHandlerConfiguration; +import org.apache.reef.webserver.HttpServer; +import org.apache.reef.webserver.HttpServerImpl; +import org.junit.Assert; +import org.junit.Test; + +import java.net.UnknownHostException; + +/** + * Tracking Uri test. + */ +public class TestTrackingUri { + /** + * Get Default Tracking URI. + * + * @throws InjectionException + * @throws UnknownHostException + */ + @Test + public void testDefaultTrackingUri() throws InjectionException, UnknownHostException { + final String uri = Tang.Factory.getTang().newInjector().getInstance(TrackingURLProvider.class).getTrackingUrl(); + Assert.assertEquals(uri, ""); + } + + /** + * Get Tracking URI with specified port number and HttpTrackingURLProvider. + * + * @throws InjectionException + * @throws UnknownHostException + * @throws BindException + */ + @Test + public void testHttpTrackingUri() throws InjectionException, UnknownHostException, BindException { + final JavaConfigurationBuilder cb = Tang.Factory.getTang().newConfigurationBuilder() + .bindNamedParameter(TcpPortRangeBegin.class, "8888") + .bindImplementation(TrackingURLProvider.class, HttpTrackingURLProvider.class) + .bindImplementation(HttpServer.class, HttpServerImpl.class); + + final Injector injector = Tang.Factory.getTang().newInjector(cb.build()); + final String uri = injector.getInstance(TrackingURLProvider.class).getTrackingUrl(); + final int port = injector.getInstance(HttpServer.class).getPort(); + verifyUri(uri, port); + } + + /** + * Get Tracking URI with HttpTrackingURLProvider and defualt port number. + * + * @throws InjectionException + * @throws UnknownHostException + * @throws BindException + */ + @Test + public void testHttpTrackingUriDefaultPort() throws InjectionException, UnknownHostException, BindException { + final JavaConfigurationBuilder cb = Tang.Factory.getTang().newConfigurationBuilder() + .bindImplementation(HttpServer.class, HttpServerImpl.class) + .bindImplementation(TrackingURLProvider.class, HttpTrackingURLProvider.class); + + final Injector injector = Tang.Factory.getTang().newInjector(cb.build()); + final String uri = injector.getInstance(TrackingURLProvider.class).getTrackingUrl(); + final int port = injector.getInstance(HttpServer.class).getPort(); + verifyUri(uri, port); + } + + /** + * Http Tracking URI using default binding test. + * + * @throws InjectionException + * @throws UnknownHostException + * @throws BindException + */ + @Test + public void testHttpTrackingUriDefaultBinding() throws InjectionException, UnknownHostException, BindException { + final Injector injector = Tang.Factory.getTang().newInjector(HttpHandlerConfiguration.CONF.build()); + final String uri = injector.getInstance(TrackingURLProvider.class).getTrackingUrl(); + final int port = injector.getInstance(HttpServer.class).getPort(); + verifyUri(uri, port); + } + + /** + * Verify if URI is correct. + * + * @param uri + * @param port + */ + private void verifyUri(final String uri, final int port) { + final String[] parts = uri.split(":"); + Assert.assertTrue(parts.length == 2); + Assert.assertEquals(port, Integer.parseInt(parts[1])); + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/a59070fa/lang/java/reef-runtime-yarn/src/main/test/java/org/apache/reef/runtime/yarn/driver/YarnResourceRequestHandlerTest.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-runtime-yarn/src/main/test/java/org/apache/reef/runtime/yarn/driver/YarnResourceRequestHandlerTest.java b/lang/java/reef-runtime-yarn/src/main/test/java/org/apache/reef/runtime/yarn/driver/YarnResourceRequestHandlerTest.java index 66a8b16..a7a9d59 100644 --- a/lang/java/reef-runtime-yarn/src/main/test/java/org/apache/reef/runtime/yarn/driver/YarnResourceRequestHandlerTest.java +++ b/lang/java/reef-runtime-yarn/src/main/test/java/org/apache/reef/runtime/yarn/driver/YarnResourceRequestHandlerTest.java @@ -23,6 +23,9 @@ import org.apache.reef.driver.evaluator.EvaluatorRequest; import org.apache.reef.driver.evaluator.EvaluatorRequestor; import org.apache.reef.runtime.common.driver.EvaluatorRequestorImpl; import org.apache.hadoop.yarn.client.api.AMRMClient; +import org.apache.reef.tang.Tang; +import org.apache.reef.tang.exceptions.InjectionException; +import org.apache.reef.util.logging.LoggingScopeFactory; import org.junit.Assert; import org.junit.Test; import org.mockito.Mockito; @@ -35,7 +38,6 @@ public final class YarnResourceRequestHandlerTest { private final MockContainerRequestHandler containerRequestHandler = new MockContainerRequestHandler(); private final YarnResourceRequestHandler resourceRequestHandler = new YarnResourceRequestHandler(containerRequestHandler, applicationMasterRegistration); private final ResourceCatalog resourceCatalog = Mockito.mock(ResourceCatalog.class); - private final EvaluatorRequestor evaluatorRequestor = new EvaluatorRequestorImpl(resourceCatalog, resourceRequestHandler); private class MockContainerRequestHandler implements YarnContainerRequestHandler { private AMRMClient.ContainerRequest[] requests; @@ -54,7 +56,10 @@ public final class YarnResourceRequestHandlerTest { * Tests whether the amount of memory is transferred correctly. */ @Test - public void testDifferentMemory() { + public void testDifferentMemory() throws InjectionException { + final LoggingScopeFactory loggingScopeFactory = Tang.Factory.getTang().newInjector().getInstance(LoggingScopeFactory.class); + final EvaluatorRequestor evaluatorRequestor = new EvaluatorRequestorImpl(resourceCatalog, resourceRequestHandler, loggingScopeFactory); + final EvaluatorRequest requestOne = EvaluatorRequest.newBuilder() .setNumber(1) .setMemory(64) @@ -65,31 +70,30 @@ public final class YarnResourceRequestHandlerTest { .setMemory(128) .setNumberOfCores(2) .build(); - { + evaluatorRequestor.submit(requestOne); Assert.assertEquals("Request in REEF and YARN form should have the same amount of memory", requestOne.getMegaBytes(), containerRequestHandler.getRequests()[0].getCapability().getMemory() ); - } - { + evaluatorRequestor.submit(requestTwo); Assert.assertEquals("Request in REEF and YARN form should have the same amount of memory", requestTwo.getMegaBytes(), containerRequestHandler.getRequests()[0].getCapability().getMemory() ); - } - { + evaluatorRequestor.submit(requestOne); Assert.assertNotEquals("Request in REEF and YARN form should have the same amount of memory", requestTwo.getMegaBytes(), containerRequestHandler.getRequests()[0].getCapability().getMemory() ); - } } @Test - public void testEvaluatorCount() { + public void testEvaluatorCount() throws InjectionException { + final LoggingScopeFactory loggingScopeFactory = Tang.Factory.getTang().newInjector().getInstance(LoggingScopeFactory.class); + final EvaluatorRequestor evaluatorRequestor = new EvaluatorRequestorImpl(resourceCatalog, resourceRequestHandler, loggingScopeFactory); final EvaluatorRequest requestOne = EvaluatorRequest.newBuilder() .setNumber(1) .setMemory(64) @@ -100,28 +104,23 @@ public final class YarnResourceRequestHandlerTest { .setMemory(128) .setNumberOfCores(2) .build(); - { + evaluatorRequestor.submit(requestOne); Assert.assertEquals("Request in REEF and YARN form should have the same number of Evaluators", requestOne.getNumber(), containerRequestHandler.getRequests().length ); - } - { + evaluatorRequestor.submit(requestTwo); Assert.assertEquals("Request in REEF and YARN form should have the same number of Evaluators", requestTwo.getNumber(), containerRequestHandler.getRequests().length ); - } - { + evaluatorRequestor.submit(requestTwo); Assert.assertNotEquals("Request in REEF and YARN form should have the same number of Evaluators", requestOne.getNumber(), containerRequestHandler.getRequests().length ); - } } - - } http://git-wip-us.apache.org/repos/asf/reef/blob/a59070fa/lang/java/reef-webserver/pom.xml ---------------------------------------------------------------------- diff --git a/lang/java/reef-webserver/pom.xml b/lang/java/reef-webserver/pom.xml index 3231309..1c18026 100644 --- a/lang/java/reef-webserver/pom.xml +++ b/lang/java/reef-webserver/pom.xml @@ -107,11 +107,6 @@ under the License. </dependency> <dependency> <groupId>${project.groupId}</groupId> - <artifactId>reef-runtime-yarn</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>${project.groupId}</groupId> <artifactId>reef-common</artifactId> <version>${project.version}</version> </dependency> http://git-wip-us.apache.org/repos/asf/reef/blob/a59070fa/lang/java/reef-webserver/src/main/java/org/apache/reef/webserver/HttpHandlerConfiguration.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-webserver/src/main/java/org/apache/reef/webserver/HttpHandlerConfiguration.java b/lang/java/reef-webserver/src/main/java/org/apache/reef/webserver/HttpHandlerConfiguration.java index 778e202..7c72681 100644 --- a/lang/java/reef-webserver/src/main/java/org/apache/reef/webserver/HttpHandlerConfiguration.java +++ b/lang/java/reef-webserver/src/main/java/org/apache/reef/webserver/HttpHandlerConfiguration.java @@ -18,7 +18,6 @@ */ package org.apache.reef.webserver; -import org.apache.reef.runtime.yarn.driver.TrackingURLProvider; import org.apache.reef.tang.formats.ConfigurationModule; import org.apache.reef.tang.formats.ConfigurationModuleBuilder; import org.apache.reef.tang.formats.OptionalParameter; @@ -38,6 +37,5 @@ public final class HttpHandlerConfiguration extends ConfigurationModuleBuilder { */ public static final ConfigurationModule CONF = new HttpHandlerConfiguration().merge(HttpRuntimeConfiguration.CONF) .bindSetEntry(HttpEventHandlers.class, HTTP_HANDLERS) - .bindImplementation(TrackingURLProvider.class, HttpTrackingURLProvider.class) .build(); } http://git-wip-us.apache.org/repos/asf/reef/blob/a59070fa/lang/java/reef-webserver/src/main/java/org/apache/reef/webserver/HttpServer.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-webserver/src/main/java/org/apache/reef/webserver/HttpServer.java b/lang/java/reef-webserver/src/main/java/org/apache/reef/webserver/HttpServer.java index d49d774..43c3e8f 100644 --- a/lang/java/reef-webserver/src/main/java/org/apache/reef/webserver/HttpServer.java +++ b/lang/java/reef-webserver/src/main/java/org/apache/reef/webserver/HttpServer.java @@ -18,9 +18,12 @@ */ package org.apache.reef.webserver; +import org.apache.reef.tang.annotations.DefaultImplementation; + /** * HttpServer interface. */ +@DefaultImplementation(NoOpHttpServerImpl.class) public interface HttpServer { /** http://git-wip-us.apache.org/repos/asf/reef/blob/a59070fa/lang/java/reef-webserver/src/main/java/org/apache/reef/webserver/HttpTrackingURLProvider.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-webserver/src/main/java/org/apache/reef/webserver/HttpTrackingURLProvider.java b/lang/java/reef-webserver/src/main/java/org/apache/reef/webserver/HttpTrackingURLProvider.java deleted file mode 100644 index 2835421..0000000 --- a/lang/java/reef-webserver/src/main/java/org/apache/reef/webserver/HttpTrackingURLProvider.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.reef.webserver; - -import org.apache.reef.runtime.yarn.driver.TrackingURLProvider; - -import javax.inject.Inject; -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Http TrackingURLProvider. - */ -public final class HttpTrackingURLProvider implements TrackingURLProvider { - /** - * Standard Java logger. - */ - private static final Logger LOG = Logger.getLogger(HttpTrackingURLProvider.class.getName()); - - /** - * HttpServer. - */ - private final HttpServer httpServer; - - /** - * Constructor of HttpTrackingURLProvider. - * - * @param httpServer - */ - @Inject - public HttpTrackingURLProvider(final HttpServer httpServer) { - this.httpServer = httpServer; - } - - /** - * get tracking URI. - * - * @return - */ - @Override - public String getTrackingUrl() { - try { - return InetAddress.getLocalHost().getHostAddress() + ":" + httpServer.getPort(); - } catch (final UnknownHostException e) { - LOG.log(Level.WARNING, "Cannot get host address.", e); - throw new RuntimeException("Cannot get host address.", e); - } - } -} http://git-wip-us.apache.org/repos/asf/reef/blob/a59070fa/lang/java/reef-webserver/src/main/java/org/apache/reef/webserver/NoOpHttpServerImpl.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-webserver/src/main/java/org/apache/reef/webserver/NoOpHttpServerImpl.java b/lang/java/reef-webserver/src/main/java/org/apache/reef/webserver/NoOpHttpServerImpl.java new file mode 100644 index 0000000..9958b73 --- /dev/null +++ b/lang/java/reef-webserver/src/main/java/org/apache/reef/webserver/NoOpHttpServerImpl.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.reef.webserver; + +import javax.inject.Inject; + +/** + * Empty implementation for the HttpServer. + * It has no functionality and serves only as an injectable placeholder. + */ +public final class NoOpHttpServerImpl implements HttpServer { + @Inject + NoOpHttpServerImpl() { + } + + @Override + public void start() throws Exception { + } + + @Override + public void stop() throws Exception { + } + + @Override + public int getPort() { + return 0; + } + + @Override + public void addHttpHandler(final HttpHandler httpHandler) { + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/a59070fa/lang/java/reef-webserver/src/test/java/org/apache/reef/webserver/TestTrackingUri.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-webserver/src/test/java/org/apache/reef/webserver/TestTrackingUri.java b/lang/java/reef-webserver/src/test/java/org/apache/reef/webserver/TestTrackingUri.java deleted file mode 100644 index a2ac62e..0000000 --- a/lang/java/reef-webserver/src/test/java/org/apache/reef/webserver/TestTrackingUri.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.reef.webserver; - -import org.apache.reef.runtime.yarn.driver.TrackingURLProvider; -import org.apache.reef.tang.Injector; -import org.apache.reef.tang.JavaConfigurationBuilder; -import org.apache.reef.tang.Tang; -import org.apache.reef.tang.exceptions.BindException; -import org.apache.reef.tang.exceptions.InjectionException; -import org.apache.reef.wake.remote.ports.parameters.TcpPortRangeBegin; -import org.junit.Assert; -import org.junit.Test; - -import java.net.UnknownHostException; - -/** - * Tracking Uri test. - */ -public class TestTrackingUri { - /** - * Get Default Tracking URI. - * - * @throws InjectionException - * @throws UnknownHostException - */ - @Test - public void testDefaultTrackingUri() throws InjectionException, UnknownHostException { - final String uri = Tang.Factory.getTang().newInjector().getInstance(TrackingURLProvider.class).getTrackingUrl(); - Assert.assertEquals(uri, ""); - } - - /** - * Get Tracking URI with specified port number and HttpTrackingURLProvider. - * - * @throws InjectionException - * @throws UnknownHostException - * @throws BindException - */ - @Test - public void testHttpTrackingUri() throws InjectionException, UnknownHostException, BindException { - final JavaConfigurationBuilder cb = Tang.Factory.getTang().newConfigurationBuilder() - .bindNamedParameter(TcpPortRangeBegin.class, "8888") - .bindImplementation(TrackingURLProvider.class, HttpTrackingURLProvider.class) - .bindImplementation(HttpServer.class, HttpServerImpl.class); - - final Injector injector = Tang.Factory.getTang().newInjector(cb.build()); - final String uri = injector.getInstance(TrackingURLProvider.class).getTrackingUrl(); - final int port = injector.getInstance(HttpServer.class).getPort(); - verifyUri(uri, port); - } - - /** - * Get Tracking URI with HttpTrackingURLProvider and defualt port number. - * - * @throws InjectionException - * @throws UnknownHostException - * @throws BindException - */ - @Test - public void testHttpTrackingUriDefaultPort() throws InjectionException, UnknownHostException, BindException { - final JavaConfigurationBuilder cb = Tang.Factory.getTang().newConfigurationBuilder() - .bindImplementation(HttpServer.class, HttpServerImpl.class) - .bindImplementation(TrackingURLProvider.class, HttpTrackingURLProvider.class); - - final Injector injector = Tang.Factory.getTang().newInjector(cb.build()); - final String uri = injector.getInstance(TrackingURLProvider.class).getTrackingUrl(); - final int port = injector.getInstance(HttpServer.class).getPort(); - verifyUri(uri, port); - } - - /** - * Http Tracking URI using default binding test. - * - * @throws InjectionException - * @throws UnknownHostException - * @throws BindException - */ - @Test - public void testHttpTrackingUriDefaultBinding() throws InjectionException, UnknownHostException, BindException { - final Injector injector = Tang.Factory.getTang().newInjector(HttpHandlerConfiguration.CONF.build()); - final String uri = injector.getInstance(TrackingURLProvider.class).getTrackingUrl(); - final int port = injector.getInstance(HttpServer.class).getPort(); - verifyUri(uri, port); - } - - /** - * Verify if URI is correct. - * - * @param uri - * @param port - */ - private void verifyUri(final String uri, final int port) { - final String[] parts = uri.split(":"); - Assert.assertTrue(parts.length == 2); - Assert.assertEquals(port, Integer.parseInt(parts[1])); - } -}
