Search in sources :

Example 56 with RequestContext

use of com.linkedin.r2.message.RequestContext in project rest.li by linkedin.

the class TestHttpNettyClient method testHeaderSize.

public void testHeaderSize(int headerSize, int expectedResult) throws InterruptedException, IOException, TimeoutException {
    TestServer testServer = new TestServer();
    HttpNettyClient client = new HttpClientBuilder(_eventLoop, _scheduler).setRequestTimeout(5000000).setIdleTimeout(10000).setShutdownTimeout(500).setMaxHeaderSize(TEST_MAX_HEADER_SIZE).buildRest();
    RestRequest r = new RestRequestBuilder(testServer.getResponseWithHeaderSizeURI(headerSize)).build();
    FutureCallback<RestResponse> cb = new FutureCallback<RestResponse>();
    TransportCallback<RestResponse> callback = new TransportCallbackAdapter<RestResponse>(cb);
    client.restRequest(r, new RequestContext(), new HashMap<String, String>(), callback);
    try {
        RestResponse response = cb.get(300, TimeUnit.SECONDS);
        if (expectedResult == TOO_LARGE) {
            Assert.fail("Max header size exceeded, expected exception. ");
        }
    } catch (ExecutionException e) {
        if (expectedResult == RESPONSE_OK) {
            Assert.fail("Unexpected ExecutionException, header was <= max header size.");
        }
        verifyCauseChain(e, RemoteInvocationException.class, TooLongFrameException.class);
    }
    testServer.shutdown();
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(com.linkedin.common.callback.FutureCallback)

Example 57 with RequestContext

use of com.linkedin.r2.message.RequestContext in project rest.li by linkedin.

the class RestLiIntTestServer method createServer.

public static HttpServer createServer(final Engine engine, int port, boolean useAsyncServletApi, int asyncTimeOut, List<? extends Filter> filters, final FilterChain filterChain, final boolean forceUseRestServer) {
    RestLiConfig config = new RestLiConfig();
    config.addResourcePackageNames(RESOURCE_PACKAGE_NAMES);
    config.setServerNodeUri(URI.create("http://localhost:" + port));
    config.setDocumentationRequestHandler(new DefaultDocumentationRequestHandler());
    config.addDebugRequestHandlers(new ParseqTraceDebugRequestHandler());
    config.setFilters(filters);
    GroupMembershipMgr membershipMgr = new HashGroupMembershipMgr();
    GroupMgr groupMgr = new HashMapGroupMgr(membershipMgr);
    GroupsRestApplication app = new GroupsRestApplication(groupMgr, membershipMgr);
    SimpleBeanProvider beanProvider = new SimpleBeanProvider();
    beanProvider.add("GroupsRestApplication", app);
    //using InjectMockResourceFactory to keep examples spring-free
    ResourceFactory factory = new InjectMockResourceFactory(beanProvider);
    //Todo this will have to change further to accomodate streaming tests - this is temporary
    final TransportDispatcher dispatcher;
    if (forceUseRestServer) {
        dispatcher = new DelegatingTransportDispatcher(new RestLiServer(config, factory, engine));
    } else {
        final StreamRequestHandler streamRequestHandler = new RestLiServer(config, factory, engine);
        dispatcher = new TransportDispatcher() {

            @Override
            public void handleRestRequest(RestRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<RestResponse> callback) {
                throw new UnsupportedOperationException("This server only accepts streaming");
            }

            @Override
            public void handleStreamRequest(StreamRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<StreamResponse> callback) {
                try {
                    streamRequestHandler.handleRequest(req, requestContext, new TransportCallbackAdapter<>(callback));
                } catch (Exception e) {
                    final Exception ex = RestException.forError(RestStatus.INTERNAL_SERVER_ERROR, e);
                    callback.onResponse(TransportResponseImpl.<StreamResponse>error(ex));
                }
            }
        };
    }
    return new HttpServerFactory(filterChain).createServer(port, HttpServerFactory.DEFAULT_CONTEXT_PATH, HttpServerFactory.DEFAULT_THREAD_POOL_SIZE, dispatcher, useAsyncServletApi ? HttpJettyServer.ServletType.ASYNC_EVENT : HttpJettyServer.ServletType.RAP, asyncTimeOut, !forceUseRestServer);
}
Also used : HttpServerFactory(com.linkedin.r2.transport.http.server.HttpServerFactory) TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.server.TransportCallbackAdapter) DelegatingTransportDispatcher(com.linkedin.restli.server.DelegatingTransportDispatcher) DefaultDocumentationRequestHandler(com.linkedin.restli.docgen.DefaultDocumentationRequestHandler) DelegatingTransportDispatcher(com.linkedin.restli.server.DelegatingTransportDispatcher) TransportDispatcher(com.linkedin.r2.transport.common.bridge.server.TransportDispatcher) ParseqTraceDebugRequestHandler(com.linkedin.restli.server.ParseqTraceDebugRequestHandler) HashGroupMembershipMgr(com.linkedin.restli.examples.groups.server.impl.HashGroupMembershipMgr) GroupMembershipMgr(com.linkedin.restli.examples.groups.server.api.GroupMembershipMgr) HashMapGroupMgr(com.linkedin.restli.examples.groups.server.impl.HashMapGroupMgr) GroupsRestApplication(com.linkedin.restli.examples.groups.server.rest.impl.GroupsRestApplication) RequestContext(com.linkedin.r2.message.RequestContext) RestLiServer(com.linkedin.restli.server.RestLiServer) SimpleBeanProvider(com.linkedin.restli.server.mock.SimpleBeanProvider) RestResponse(com.linkedin.r2.message.rest.RestResponse) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) InjectMockResourceFactory(com.linkedin.restli.server.mock.InjectMockResourceFactory) ResourceFactory(com.linkedin.restli.server.resources.ResourceFactory) RestException(com.linkedin.r2.message.rest.RestException) IOException(java.io.IOException) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) HashMapGroupMgr(com.linkedin.restli.examples.groups.server.impl.HashMapGroupMgr) GroupMgr(com.linkedin.restli.examples.groups.server.api.GroupMgr) StreamRequestHandler(com.linkedin.r2.transport.common.StreamRequestHandler) RestRequest(com.linkedin.r2.message.rest.RestRequest) InjectMockResourceFactory(com.linkedin.restli.server.mock.InjectMockResourceFactory) HashGroupMembershipMgr(com.linkedin.restli.examples.groups.server.impl.HashGroupMembershipMgr) RestLiConfig(com.linkedin.restli.server.RestLiConfig)

Example 58 with RequestContext

use of com.linkedin.r2.message.RequestContext in project rest.li by linkedin.

the class AllPartitionsRequestBuilder method sendRequests.

/**
   * A convenience function for caller to issue search request with one call.
   * If finer-grain control is required, users should call buildRequestContexts instead and send requests by themselves
   *
   * @param client - the RestClient to use
   * @param request - the query-all request
   * @param requestContext - the original request context
   * @param callback - callback to be used for each request
   * @return the partition cover which informs the caller the RequestContexts, the total number of partitions and the available partition number
   * @throws ServiceUnavailableException
   */
public HostSet sendRequests(RestClient client, Request<T> request, RequestContext requestContext, Callback<Response<T>> callback) throws ServiceUnavailableException {
    URI serviceUri;
    try {
        serviceUri = new URI(D2_URI_PREFIX + request.getServiceName());
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException(e);
    }
    final Collection<RequestContext> queryAllRequestContext = new ArrayList<RequestContext>();
    final HostSet uriResult = _mapper.getAllPartitionsMultipleHosts(serviceUri, 1);
    for (URI targetHost : uriResult.getAllHosts()) {
        RequestContext context = requestContext.clone();
        KeyMapper.TargetHostHints.setRequestContextTargetHost(context, targetHost);
        queryAllRequestContext.add(context);
        client.sendRequest(request, context, callback);
    }
    return uriResult;
}
Also used : ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) HostSet(com.linkedin.d2.balancer.util.HostSet)

Example 59 with RequestContext

use of com.linkedin.r2.message.RequestContext in project rest.li by linkedin.

the class ScatterGatherBuilder method buildRequestsV2.

// return value contains the request info and the unmapped keys (also the cause)
// V2 is here to differentiate it from the older API
@SuppressWarnings("deprecation")
public ScatterGatherResult<T> buildRequestsV2(BatchGetRequest<T> request, RequestContext requestContext) throws ServiceUnavailableException {
    Set<Object> idObjects = request.getObjectIds();
    MapKeyResult<URI, Object> mapKeyResult = mapKeys(request, idObjects);
    Map<URI, Collection<Object>> batches = mapKeyResult.getMapResult();
    Collection<RequestInfo<T>> scatterGatherRequests = new ArrayList<RequestInfo<T>>(batches.size());
    for (Map.Entry<URI, Collection<Object>> batch : batches.entrySet()) {
        BatchGetRequestBuilder<Object, T> builder = new BatchGetRequestBuilder<Object, T>(request.getBaseUriTemplate(), request.getResponseDecoder(), request.getResourceSpec(), request.getRequestOptions());
        builder.ids(batch.getValue());
        for (Map.Entry<String, Object> param : request.getQueryParamsObjects().entrySet()) {
            if (!param.getKey().equals(RestConstants.QUERY_BATCH_IDS_PARAM)) {
                // keep all non-batch query parameters since they could be request specific
                builder.setParam(param.getKey(), param.getValue());
            }
        }
        for (Map.Entry<String, String> header : request.getHeaders().entrySet()) {
            builder.setHeader(header.getKey(), header.getValue());
        }
        RequestContext context = requestContext.clone();
        KeyMapper.TargetHostHints.setRequestContextTargetHost(context, batch.getKey());
        scatterGatherRequests.add(new RequestInfo<T>(builder.build(), context));
    }
    return new ScatterGatherResult<T>(scatterGatherRequests, mapKeyResult.getUnmappedKeys());
}
Also used : ArrayList(java.util.ArrayList) URI(java.net.URI) Collection(java.util.Collection) RequestContext(com.linkedin.r2.message.RequestContext) HashMap(java.util.HashMap) Map(java.util.Map)

Example 60 with RequestContext

use of com.linkedin.r2.message.RequestContext in project rest.li by linkedin.

the class ScatterGatherBuilder method buildRequests.

@SuppressWarnings("deprecation")
public <K> KVScatterGatherResult<K, UpdateStatus> buildRequests(BatchUpdateRequest<K, T> request, RequestContext requestContext) throws ServiceUnavailableException {
    Set<Object> idObjects = request.getObjectIds();
    Collection<K> ids = new HashSet<K>(idObjects.size());
    for (Object o : idObjects) {
        @SuppressWarnings("unchecked") K k = (K) o;
        ids.add(k);
    }
    MapKeyResult<URI, K> mapKeyResult = mapKeys(request, ids);
    @SuppressWarnings("unchecked") TypeSpec<T> valueType = (TypeSpec<T>) request.getResourceProperties().getValueType();
    Map<URI, Map<K, T>> batches = keyMapToInput(mapKeyResult, request);
    Collection<KVRequestInfo<K, UpdateStatus>> scatterGatherRequests = new ArrayList<KVRequestInfo<K, UpdateStatus>>(batches.size());
    for (Map.Entry<URI, Map<K, T>> batch : batches.entrySet()) {
        BatchUpdateRequestBuilder<K, T> builder = new BatchUpdateRequestBuilder<K, T>(request.getBaseUriTemplate(), valueType.getType(), request.getResourceSpec(), request.getRequestOptions());
        builder.inputs(batch.getValue());
        for (Map.Entry<String, Object> param : request.getQueryParamsObjects().entrySet()) {
            if (!param.getKey().equals(RestConstants.QUERY_BATCH_IDS_PARAM)) {
                builder.setParam(param.getKey(), param.getValue());
            }
        }
        for (Map.Entry<String, String> header : request.getHeaders().entrySet()) {
            builder.setHeader(header.getKey(), header.getValue());
        }
        RequestContext context = requestContext.clone();
        KeyMapper.TargetHostHints.setRequestContextTargetHost(context, batch.getKey());
        scatterGatherRequests.add(new KVRequestInfo<K, UpdateStatus>(builder.build(), context));
    }
    return new KVScatterGatherResult<K, UpdateStatus>(scatterGatherRequests, mapKeyResult.getUnmappedKeys());
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) ArrayList(java.util.ArrayList) URI(java.net.URI) RequestContext(com.linkedin.r2.message.RequestContext) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Map(java.util.Map) TypeSpec(com.linkedin.restli.common.TypeSpec)

Aggregations

RequestContext (com.linkedin.r2.message.RequestContext)249 Test (org.testng.annotations.Test)201 RestRequest (com.linkedin.r2.message.rest.RestRequest)148 URI (java.net.URI)139 RestResponse (com.linkedin.r2.message.rest.RestResponse)106 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)102 HashMap (java.util.HashMap)73 ByteString (com.linkedin.data.ByteString)66 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)63 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)55 FutureCallback (com.linkedin.common.callback.FutureCallback)51 ArrayList (java.util.ArrayList)41 Callback (com.linkedin.common.callback.Callback)39 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)39 RestException (com.linkedin.r2.message.rest.RestException)38 Map (java.util.Map)37 CountDownLatch (java.util.concurrent.CountDownLatch)37 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)36 TrackerClient (com.linkedin.d2.balancer.clients.TrackerClient)31 BeforeTest (org.testng.annotations.BeforeTest)31