Search in sources :

Example 1 with Router

use of org.apache.flink.runtime.rest.handler.router.Router in project flink by apache.

the class HistoryServerStaticFileServerHandlerTest method testRespondWithFile.

@Test
public void testRespondWithFile() throws Exception {
    File webDir = tmp.newFolder("webDir");
    Router router = new Router().addGet("/:*", new HistoryServerStaticFileServerHandler(webDir));
    WebFrontendBootstrap webUI = new WebFrontendBootstrap(router, LoggerFactory.getLogger(HistoryServerStaticFileServerHandlerTest.class), tmp.newFolder("uploadDir"), null, "localhost", 0, new Configuration());
    int port = webUI.getServerPort();
    try {
        // verify that 404 message is returned when requesting a non-existent file
        Tuple2<Integer, String> notFound404 = HistoryServerTest.getFromHTTP("http://localhost:" + port + "/hello");
        Assert.assertThat(notFound404.f0, is(404));
        Assert.assertThat(notFound404.f1, containsString("not found"));
        // verify that a) a file can be loaded using the ClassLoader and b) that the
        // HistoryServer
        // index_hs.html is injected
        Tuple2<Integer, String> index = HistoryServerTest.getFromHTTP("http://localhost:" + port + "/index.html");
        Assert.assertThat(index.f0, is(200));
        Assert.assertThat(index.f1, containsString("Apache Flink Web Dashboard"));
        // verify that index.html is appended if the request path ends on '/'
        Tuple2<Integer, String> index2 = HistoryServerTest.getFromHTTP("http://localhost:" + port + "/");
        Assert.assertEquals(index, index2);
        // verify that a 405 message is returned when requesting a directory
        File dir = new File(webDir, "dir.json");
        dir.mkdirs();
        Tuple2<Integer, String> dirNotFound = HistoryServerTest.getFromHTTP("http://localhost:" + port + "/dir");
        Assert.assertThat(dirNotFound.f0, is(405));
        Assert.assertThat(dirNotFound.f1, containsString("not found"));
        // verify that a 403 message is returned when requesting a file outside the webDir
        tmp.newFile("secret");
        Tuple2<Integer, String> dirOutsideDirectory = HistoryServerTest.getFromHTTP("http://localhost:" + port + "/../secret");
        Assert.assertThat(dirOutsideDirectory.f0, is(403));
        Assert.assertThat(dirOutsideDirectory.f1, containsString("Forbidden"));
    } finally {
        webUI.shutdown();
    }
}
Also used : WebFrontendBootstrap(org.apache.flink.runtime.webmonitor.utils.WebFrontendBootstrap) Configuration(org.apache.flink.configuration.Configuration) Router(org.apache.flink.runtime.rest.handler.router.Router) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) File(java.io.File) Test(org.junit.Test)

Example 2 with Router

use of org.apache.flink.runtime.rest.handler.router.Router in project flink by apache.

the class LeaderRetrievalHandlerTest method testLeaderRetrievalGateway.

/**
 * Tests the behaviour of the LeaderRetrievalHandler under the following conditions.
 *
 * <p>1. No gateway resolved --> service unavailable 2. leader gateway
 *
 * @throws Exception
 */
@Test
public void testLeaderRetrievalGateway() throws Exception {
    final String restPath = "/testing";
    final Configuration configuration = new Configuration();
    final Router router = new Router();
    final Time timeout = Time.seconds(10L);
    final CompletableFuture<RestfulGateway> gatewayFuture = new CompletableFuture<>();
    final GatewayRetriever<RestfulGateway> gatewayRetriever = () -> gatewayFuture;
    final RestfulGateway gateway = new TestingRestfulGateway.Builder().build();
    final TestingHandler testingHandler = new TestingHandler(gatewayRetriever, timeout);
    router.addGet(restPath, testingHandler);
    WebFrontendBootstrap bootstrap = new WebFrontendBootstrap(router, log, null, null, "localhost", 0, configuration);
    try (HttpTestClient httpClient = new HttpTestClient("localhost", bootstrap.getServerPort())) {
        // 1. no leader gateway available --> Service unavailable
        httpClient.sendGetRequest(restPath, FutureUtils.toDuration(timeout));
        HttpTestClient.SimpleHttpResponse response = httpClient.getNextResponse(FutureUtils.toDuration(timeout));
        Assert.assertEquals(HttpResponseStatus.SERVICE_UNAVAILABLE, response.getStatus());
        // 2. with leader
        gatewayFuture.complete(gateway);
        httpClient.sendGetRequest(restPath, FutureUtils.toDuration(timeout));
        response = httpClient.getNextResponse(FutureUtils.toDuration(timeout));
        Assert.assertEquals(HttpResponseStatus.OK, response.getStatus());
        Assert.assertEquals(RESPONSE_MESSAGE, response.getContent());
    } finally {
        bootstrap.shutdown();
    }
}
Also used : WebFrontendBootstrap(org.apache.flink.runtime.webmonitor.utils.WebFrontendBootstrap) Configuration(org.apache.flink.configuration.Configuration) Router(org.apache.flink.runtime.rest.handler.router.Router) Time(org.apache.flink.api.common.time.Time) CompletableFuture(java.util.concurrent.CompletableFuture) HttpTestClient(org.apache.flink.runtime.webmonitor.testutils.HttpTestClient) Test(org.junit.Test)

Example 3 with Router

use of org.apache.flink.runtime.rest.handler.router.Router in project flink by apache.

the class HistoryServer method start.

// ------------------------------------------------------------------------
// Life-cycle
// ------------------------------------------------------------------------
void start() throws IOException, InterruptedException {
    synchronized (startupShutdownLock) {
        LOG.info("Starting history server.");
        Files.createDirectories(webDir.toPath());
        LOG.info("Using directory {} as local cache.", webDir);
        Router router = new Router();
        router.addGet("/:*", new HistoryServerStaticFileServerHandler(webDir));
        createDashboardConfigFile();
        executor.scheduleWithFixedDelay(getArchiveFetchingRunnable(), 0, refreshIntervalMillis, TimeUnit.MILLISECONDS);
        netty = new WebFrontendBootstrap(router, LOG, webDir, serverSSLFactory, webAddress, webPort, config);
    }
}
Also used : WebFrontendBootstrap(org.apache.flink.runtime.webmonitor.utils.WebFrontendBootstrap) Router(org.apache.flink.runtime.rest.handler.router.Router)

Example 4 with Router

use of org.apache.flink.runtime.rest.handler.router.Router in project flink by apache.

the class RestServerEndpoint method start.

/**
 * Starts this REST server endpoint.
 *
 * @throws Exception if we cannot start the RestServerEndpoint
 */
public final void start() throws Exception {
    synchronized (lock) {
        Preconditions.checkState(state == State.CREATED, "The RestServerEndpoint cannot be restarted.");
        log.info("Starting rest endpoint.");
        final Router router = new Router();
        final CompletableFuture<String> restAddressFuture = new CompletableFuture<>();
        handlers = initializeHandlers(restAddressFuture);
        /* sort the handlers such that they are ordered the following:
             * /jobs
             * /jobs/overview
             * /jobs/:jobid
             * /jobs/:jobid/config
             * /:*
             */
        Collections.sort(handlers, RestHandlerUrlComparator.INSTANCE);
        checkAllEndpointsAndHandlersAreUnique(handlers);
        handlers.forEach(handler -> registerHandler(router, handler, log));
        ChannelInitializer<SocketChannel> initializer = new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws ConfigurationException {
                RouterHandler handler = new RouterHandler(router, responseHeaders);
                // SSL should be the first handler in the pipeline
                if (isHttpsEnabled()) {
                    ch.pipeline().addLast("ssl", new RedirectingSslHandler(restAddress, restAddressFuture, sslHandlerFactory));
                }
                ch.pipeline().addLast(new HttpServerCodec()).addLast(new FileUploadHandler(uploadDir)).addLast(new FlinkHttpObjectAggregator(maxContentLength, responseHeaders));
                for (InboundChannelHandlerFactory factory : inboundChannelHandlerFactories) {
                    Optional<ChannelHandler> channelHandler = factory.createHandler(configuration, responseHeaders);
                    if (channelHandler.isPresent()) {
                        ch.pipeline().addLast(channelHandler.get());
                    }
                }
                ch.pipeline().addLast(new ChunkedWriteHandler()).addLast(handler.getName(), handler).addLast(new PipelineErrorHandler(log, responseHeaders));
            }
        };
        NioEventLoopGroup bossGroup = new NioEventLoopGroup(1, new ExecutorThreadFactory("flink-rest-server-netty-boss"));
        NioEventLoopGroup workerGroup = new NioEventLoopGroup(0, new ExecutorThreadFactory("flink-rest-server-netty-worker"));
        bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(initializer);
        Iterator<Integer> portsIterator;
        try {
            portsIterator = NetUtils.getPortRangeFromString(restBindPortRange);
        } catch (IllegalConfigurationException e) {
            throw e;
        } catch (Exception e) {
            throw new IllegalArgumentException("Invalid port range definition: " + restBindPortRange);
        }
        int chosenPort = 0;
        while (portsIterator.hasNext()) {
            try {
                chosenPort = portsIterator.next();
                final ChannelFuture channel;
                if (restBindAddress == null) {
                    channel = bootstrap.bind(chosenPort);
                } else {
                    channel = bootstrap.bind(restBindAddress, chosenPort);
                }
                serverChannel = channel.syncUninterruptibly().channel();
                break;
            } catch (final Exception e) {
                // otherwise
                if (!(e instanceof java.net.BindException)) {
                    throw e;
                }
            }
        }
        if (serverChannel == null) {
            throw new BindException("Could not start rest endpoint on any port in port range " + restBindPortRange);
        }
        log.debug("Binding rest endpoint to {}:{}.", restBindAddress, chosenPort);
        final InetSocketAddress bindAddress = (InetSocketAddress) serverChannel.localAddress();
        final String advertisedAddress;
        if (bindAddress.getAddress().isAnyLocalAddress()) {
            advertisedAddress = this.restAddress;
        } else {
            advertisedAddress = bindAddress.getAddress().getHostAddress();
        }
        port = bindAddress.getPort();
        log.info("Rest endpoint listening at {}:{}", advertisedAddress, port);
        restBaseUrl = new URL(determineProtocol(), advertisedAddress, port, "").toString();
        restAddressFuture.complete(restBaseUrl);
        state = State.RUNNING;
        startInternal();
    }
}
Also used : SocketChannel(org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel) NioServerSocketChannel(org.apache.flink.shaded.netty4.io.netty.channel.socket.nio.NioServerSocketChannel) InetSocketAddress(java.net.InetSocketAddress) IllegalConfigurationException(org.apache.flink.configuration.IllegalConfigurationException) RedirectingSslHandler(org.apache.flink.runtime.net.RedirectingSslHandler) ChannelHandler(org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandler) BindException(java.net.BindException) URL(java.net.URL) CompletableFuture(java.util.concurrent.CompletableFuture) ChannelInitializer(org.apache.flink.shaded.netty4.io.netty.channel.ChannelInitializer) NioEventLoopGroup(org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup) ChannelFuture(org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture) NioServerSocketChannel(org.apache.flink.shaded.netty4.io.netty.channel.socket.nio.NioServerSocketChannel) InboundChannelHandlerFactory(org.apache.flink.runtime.io.network.netty.InboundChannelHandlerFactory) Router(org.apache.flink.runtime.rest.handler.router.Router) BindException(java.net.BindException) ServerBootstrap(org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap) IllegalConfigurationException(org.apache.flink.configuration.IllegalConfigurationException) ConfigurationException(org.apache.flink.util.ConfigurationException) BindException(java.net.BindException) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) IOException(java.io.IOException) ExecutorThreadFactory(org.apache.flink.util.concurrent.ExecutorThreadFactory) PipelineErrorHandler(org.apache.flink.runtime.rest.handler.PipelineErrorHandler) RouterHandler(org.apache.flink.runtime.rest.handler.router.RouterHandler) ChunkedWriteHandler(org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedWriteHandler) HttpServerCodec(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpServerCodec)

Aggregations

Router (org.apache.flink.runtime.rest.handler.router.Router)4 WebFrontendBootstrap (org.apache.flink.runtime.webmonitor.utils.WebFrontendBootstrap)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 Configuration (org.apache.flink.configuration.Configuration)2 Test (org.junit.Test)2 File (java.io.File)1 IOException (java.io.IOException)1 BindException (java.net.BindException)1 InetSocketAddress (java.net.InetSocketAddress)1 URL (java.net.URL)1 Time (org.apache.flink.api.common.time.Time)1 IllegalConfigurationException (org.apache.flink.configuration.IllegalConfigurationException)1 InboundChannelHandlerFactory (org.apache.flink.runtime.io.network.netty.InboundChannelHandlerFactory)1 RedirectingSslHandler (org.apache.flink.runtime.net.RedirectingSslHandler)1 PipelineErrorHandler (org.apache.flink.runtime.rest.handler.PipelineErrorHandler)1 RouterHandler (org.apache.flink.runtime.rest.handler.router.RouterHandler)1 HttpTestClient (org.apache.flink.runtime.webmonitor.testutils.HttpTestClient)1 ServerBootstrap (org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap)1 ChannelFuture (org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture)1 ChannelHandler (org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandler)1