Search in sources :

Example 1 with Engine

use of com.linkedin.parseq.Engine in project rest.li by linkedin.

the class MockHttpServerFactory method create.

/**
 * Creates a {@link HttpServer} that contains a {@link RestLiServer} to be used for testing a set of Rest.li
 * resources.
 *
 * The {@link HttpServer} uses an empty {@link FilterChain} and uses "/" as the context path.
 *
 * If the server is run in async mode (by calling this function with the last parameter {@code true}), the
 * timeout used is {@link #ASYNC_TIMEOUT}.
 *
 * Both the async and sync servers will use {@link #NUM_THREADS} threads.
 *
 * @param port the port the server will run on on localhost
 * @param config the {@link RestLiConfig} to be used by the {@link RestLiServer}
 * @param beans beans you want to inject into your Rest.li resource.
 * @param enableAsync true if the server should be async, false otherwise
 */
private static HttpServer create(int port, RestLiConfig config, Map<String, ?> beans, boolean enableAsync) {
    final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(NUM_THREADS);
    final ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
    EngineBuilder engineBuilder = new EngineBuilder().setTaskExecutor(scheduler).setTimerScheduler(scheduler);
    com.linkedin.parseq.AsyncCallableTask.register(engineBuilder, executor);
    final Engine engine = engineBuilder.build();
    ResourceFactory resourceFactory = createResourceFactory(beans);
    TransportDispatcher dispatcher = new DelegatingTransportDispatcher(new RestLiServer(config, resourceFactory, engine));
    final FilterChain fc = FilterChains.empty().addLastRest(new SimpleLoggingFilter());
    final HttpServer server = new HttpServerFactory(fc).createServer(port, HttpServerFactory.DEFAULT_CONTEXT_PATH, NUM_THREADS, dispatcher, enableAsync ? HttpJettyServer.ServletType.ASYNC_EVENT : HttpJettyServer.ServletType.RAP, enableAsync ? ASYNC_TIMEOUT : -1);
    return new HttpServer() {

        @Override
        public void start() throws IOException {
            server.start();
        }

        @Override
        public void stop() throws IOException {
            server.stop();
            engine.shutdown();
            executor.shutdown();
            scheduler.shutdown();
        }

        @Override
        public void waitForStop() throws InterruptedException {
            server.waitForStop();
        }
    };
}
Also used : HttpServerFactory(com.linkedin.r2.transport.http.server.HttpServerFactory) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RestLiServer(com.linkedin.restli.server.RestLiServer) DelegatingTransportDispatcher(com.linkedin.restli.server.DelegatingTransportDispatcher) FilterChain(com.linkedin.r2.filter.FilterChain) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) HttpServer(com.linkedin.r2.transport.http.server.HttpServer) ResourceFactory(com.linkedin.restli.server.resources.ResourceFactory) InjectMockResourceFactory(com.linkedin.restli.server.mock.InjectMockResourceFactory) DelegatingTransportDispatcher(com.linkedin.restli.server.DelegatingTransportDispatcher) TransportDispatcher(com.linkedin.r2.transport.common.bridge.server.TransportDispatcher) EngineBuilder(com.linkedin.parseq.EngineBuilder) SimpleLoggingFilter(com.linkedin.r2.filter.logging.SimpleLoggingFilter) Engine(com.linkedin.parseq.Engine)

Example 2 with Engine

use of com.linkedin.parseq.Engine in project rest.li by linkedin.

the class RestliServlet method buildR2ServletFromServletParams.

private AbstractR2Servlet buildR2ServletFromServletParams(ServletConfig servletConfig) {
    ResourceFactory resourceFactory = new PrototypeResourceFactory();
    RestLiConfig config = new RestLiConfig();
    config.setResourcePackageNamesSet(getResourcePackageSet(servletConfig));
    final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(getParseqThreadPoolSize(servletConfig));
    Engine engine = new EngineBuilder().setTaskExecutor(scheduler).setTimerScheduler(scheduler).build();
    final RestLiServer restLiServer = new RestLiServer(config, resourceFactory, engine);
    DelegatingTransportDispatcher dispatcher = new DelegatingTransportDispatcher(restLiServer, restLiServer);
    boolean useAsync = getUseAsync(servletConfig);
    long asyncTimeOut = getAsyncTimeout(servletConfig);
    if (useAsync && servletConfig.getServletContext().getMajorVersion() < 3) {
        throw new IllegalArgumentException("This servlet is configured with useAsync=true, but the current servlet " + "context does not support the required Servlet API 3.0.");
    }
    if (!useAsync) {
        log.info("Initializing Rest.li with a thread based request handling.  Set useAsync=true on a Servlet API 3.0 container to enable Rest.li's async servlet.");
        return new RAPServlet(dispatcher);
    } else {
        log.info("Initializing Rest.li with an async request handling enabled.");
        return new AsyncR2Servlet(dispatcher, asyncTimeOut);
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RAPServlet(com.linkedin.r2.transport.http.server.RAPServlet) ResourceFactory(com.linkedin.restli.server.resources.ResourceFactory) PrototypeResourceFactory(com.linkedin.restli.server.resources.PrototypeResourceFactory) AsyncR2Servlet(com.linkedin.r2.transport.http.server.AsyncR2Servlet) PrototypeResourceFactory(com.linkedin.restli.server.resources.PrototypeResourceFactory) EngineBuilder(com.linkedin.parseq.EngineBuilder) Engine(com.linkedin.parseq.Engine)

Example 3 with Engine

use of com.linkedin.parseq.Engine in project rest.li by linkedin.

the class TestAsyncMethodInvocationPlanClass method testMultiplexedAsyncGet.

@Test(dataProvider = "multiplexerConfigurations")
public void testMultiplexedAsyncGet(MultiplexerRunMode multiplexerRunMode) throws URISyntaxException, IOException, InterruptedException {
    RestLiConfig config = new RestLiConfig();
    config.addResourcePackageNames("com.linkedin.restli.server.multiplexer.resources");
    config.setMultiplexerRunMode(multiplexerRunMode);
    SettablePromise<Trace> traceHolder = Promises.settable();
    Engine engine = engine(traceHolder);
    RestLiServer server = new RestLiServer(config, resourceFactory(), engine);
    IndividualRequest r0 = individualRequest("/users/0", null, Collections.<String, IndividualRequest>emptyMap());
    IndividualRequest r1 = individualRequest("/users/1", null, Collections.<String, IndividualRequest>emptyMap());
    IndividualRequest r2 = individualRequest("/users/2", null, ImmutableMap.of("0", r0, "1", r1));
    // request is seq(par(r0, r1), r2)
    RestRequest request = muxRestRequest(ImmutableMap.of("2", r2));
    CountDownLatch latch = new CountDownLatch(1);
    server.handleRequest(request, new RequestContext(), callback(latch));
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    assertTrue(traceHolder.await(5, TimeUnit.SECONDS));
    if (multiplexerRunMode == MultiplexerRunMode.SINGLE_PLAN) {
        // For multiplexed requests in SINGLE_PLAN mode there is only one plan with class "mux"
        assertEquals(traceHolder.get().getPlanClass(), "mux");
    } else {
        // For multiplexed requests in MULTIPLE_PLANS mode there are multiple plans, first one is with class "mux",
        // following 3 are with class "resource=users,method=get", last one will have class "resource=users,method=get"
        assertEquals(traceHolder.get().getPlanClass(), "resource=users,method=get");
    }
}
Also used : Trace(com.linkedin.parseq.trace.Trace) IndividualRequest(com.linkedin.restli.common.multiplexer.IndividualRequest) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiServer(com.linkedin.restli.server.RestLiServer) RequestContext(com.linkedin.r2.message.RequestContext) CountDownLatch(java.util.concurrent.CountDownLatch) RestLiConfig(com.linkedin.restli.server.RestLiConfig) Engine(com.linkedin.parseq.Engine) Test(org.testng.annotations.Test)

Example 4 with Engine

use of com.linkedin.parseq.Engine in project rest.li by linkedin.

the class TestAsyncMethodInvocationPlanClass method engine.

private Engine engine(SettablePromise<Trace> traceHolder) {
    ExecutorService taskScheduler = Executors.newFixedThreadPool(1);
    ScheduledExecutorService timerScheduler = Executors.newSingleThreadScheduledExecutor();
    return new EngineBuilder().setTaskExecutor(taskScheduler).setTimerScheduler(timerScheduler).setPlanCompletionListener(planCtx -> {
        try {
            traceHolder.done(planCtx.getRootTask().getTrace());
        } catch (PromiseResolvedException e) {
        // this is expected in MULTIPLE_PLANS mux mode
        }
    }).build();
}
Also used : Engine(com.linkedin.parseq.Engine) DataProvider(org.testng.annotations.DataProvider) URISyntaxException(java.net.URISyntaxException) Assert.assertEquals(org.testng.Assert.assertEquals) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) Test(org.testng.annotations.Test) Callback(com.linkedin.common.callback.Callback) RestLiConfig(com.linkedin.restli.server.RestLiConfig) RestResponse(com.linkedin.r2.message.rest.RestResponse) MultiplexerRunMode(com.linkedin.restli.server.multiplexer.MultiplexerRunMode) Trace(com.linkedin.parseq.trace.Trace) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) PromiseResolvedException(com.linkedin.parseq.promise.PromiseResolvedException) SettablePromise(com.linkedin.parseq.promise.SettablePromise) RestConstants(com.linkedin.restli.common.RestConstants) RestLiServer(com.linkedin.restli.server.RestLiServer) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) AllProtocolVersions(com.linkedin.restli.internal.common.AllProtocolVersions) StringMap(com.linkedin.data.template.StringMap) URI(java.net.URI) JacksonDataCodec(com.linkedin.data.codec.JacksonDataCodec) RestMethod(com.linkedin.r2.message.rest.RestMethod) IndividualRequestMap(com.linkedin.restli.common.multiplexer.IndividualRequestMap) ExecutorService(java.util.concurrent.ExecutorService) HttpMethod(com.linkedin.restli.common.HttpMethod) RestRequest(com.linkedin.r2.message.rest.RestRequest) ImmutableMap(com.google.common.collect.ImmutableMap) Promises(com.linkedin.parseq.promise.Promises) IOException(java.io.IOException) Executors(java.util.concurrent.Executors) ResourceFactory(com.linkedin.restli.server.resources.ResourceFactory) TimeUnit(java.util.concurrent.TimeUnit) MultiplexedRequestContent(com.linkedin.restli.common.multiplexer.MultiplexedRequestContent) CountDownLatch(java.util.concurrent.CountDownLatch) RequestContext(com.linkedin.r2.message.RequestContext) EngineBuilder(com.linkedin.parseq.EngineBuilder) Assert.assertTrue(org.testng.Assert.assertTrue) IndividualRequest(com.linkedin.restli.common.multiplexer.IndividualRequest) Collections(java.util.Collections) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) PromiseResolvedException(com.linkedin.parseq.promise.PromiseResolvedException) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) EngineBuilder(com.linkedin.parseq.EngineBuilder)

Example 5 with Engine

use of com.linkedin.parseq.Engine in project rest.li by linkedin.

the class TestAsyncMethodInvocationPlanClass method testAsyncGet.

@Test(dataProvider = "requestData")
public void testAsyncGet(String uri, String method, String expectedPlanClass) throws URISyntaxException, IOException, InterruptedException {
    RestLiConfig config = new RestLiConfig();
    config.addResourcePackageNames("com.linkedin.restli.server.multiplexer.resources");
    SettablePromise<Trace> traceHolder = Promises.settable();
    Engine engine = engine(traceHolder);
    RestLiServer server = new RestLiServer(config, resourceFactory(), engine);
    RestRequest request = new RestRequestBuilder(new URI(uri)).setMethod(method).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString()).build();
    CountDownLatch latch = new CountDownLatch(1);
    server.handleRequest(request, new RequestContext(), callback(latch));
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    assertTrue(traceHolder.await(5, TimeUnit.SECONDS));
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    assertTrue(traceHolder.await(5, TimeUnit.SECONDS));
    assertEquals(traceHolder.get().getPlanClass(), expectedPlanClass);
}
Also used : Trace(com.linkedin.parseq.trace.Trace) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiServer(com.linkedin.restli.server.RestLiServer) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) RestLiConfig(com.linkedin.restli.server.RestLiConfig) Engine(com.linkedin.parseq.Engine) Test(org.testng.annotations.Test)

Aggregations

Engine (com.linkedin.parseq.Engine)10 EngineBuilder (com.linkedin.parseq.EngineBuilder)7 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)7 RestLiServer (com.linkedin.restli.server.RestLiServer)4 Trace (com.linkedin.parseq.trace.Trace)3 RequestContext (com.linkedin.r2.message.RequestContext)3 RestRequest (com.linkedin.r2.message.rest.RestRequest)3 RestLiConfig (com.linkedin.restli.server.RestLiConfig)3 ResourceFactory (com.linkedin.restli.server.resources.ResourceFactory)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ExecutorService (java.util.concurrent.ExecutorService)3 Test (org.testng.annotations.Test)3 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)2 HttpServer (com.linkedin.r2.transport.http.server.HttpServer)2 IndividualRequest (com.linkedin.restli.common.multiplexer.IndividualRequest)2 URI (java.net.URI)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Callback (com.linkedin.common.callback.Callback)1 JacksonDataCodec (com.linkedin.data.codec.JacksonDataCodec)1 StringMap (com.linkedin.data.template.StringMap)1