Search in sources :

Example 41 with Request

use of com.linkedin.r2.message.Request 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 42 with Request

use of com.linkedin.r2.message.Request 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 43 with Request

use of com.linkedin.r2.message.Request 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)

Example 44 with Request

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

the class ScatterGatherBuilder method buildRequestsKV.

@SuppressWarnings({ "unchecked", "deprecation" })
public <K> KVScatterGatherResult<K, T> buildRequestsKV(BatchGetKVRequest<K, T> request, RequestContext requestContext) throws ServiceUnavailableException {
    Set<K> idObjects = (Set<K>) request.getObjectIds();
    MapKeyResult<URI, K> mapKeyResult = mapKeys(request, idObjects);
    Map<URI, Collection<K>> batches = mapKeyResult.getMapResult();
    Collection<KVRequestInfo<K, T>> scatterGatherRequests = new ArrayList<KVRequestInfo<K, T>>(batches.size());
    for (Map.Entry<URI, Collection<K>> batch : batches.entrySet()) {
        BatchGetRequestBuilder<K, T> builder = new BatchGetRequestBuilder<K, T>(request.getBaseUriTemplate(), (Class<T>) request.getResourceProperties().getValueType().getType(), 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 KVRequestInfo<K, T>(builder.buildKV(), context));
    }
    return new KVScatterGatherResult<K, T>(scatterGatherRequests, mapKeyResult.getUnmappedKeys());
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) 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 45 with Request

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

the class ScatterGatherBuilder method buildRequests.

@SuppressWarnings("deprecation")
public <K> KVScatterGatherResult<K, UpdateStatus> buildRequests(BatchDeleteRequest<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);
    Map<URI, Collection<K>> batches = mapKeyResult.getMapResult();
    Collection<KVRequestInfo<K, UpdateStatus>> scatterGatherRequests = new ArrayList<KVRequestInfo<K, UpdateStatus>>(batches.size());
    for (Map.Entry<URI, Collection<K>> batch : batches.entrySet()) {
        TypeSpec<? extends RecordTemplate> value = request.getResourceProperties().getValueType();
        @SuppressWarnings("unchecked") Class<T> valueClass = (Class<T>) ((value == null) ? null : value.getType());
        BatchDeleteRequestBuilder<K, T> builder = new BatchDeleteRequestBuilder<K, T>(request.getBaseUriTemplate(), valueClass, 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)) {
                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());
        BatchRequest<BatchKVResponse<K, UpdateStatus>> build = builder.build();
        scatterGatherRequests.add(new KVRequestInfo<K, UpdateStatus>(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) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) RequestContext(com.linkedin.r2.message.RequestContext) HashSet(java.util.HashSet) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Test (org.testng.annotations.Test)209 RestRequest (com.linkedin.r2.message.rest.RestRequest)189 RequestContext (com.linkedin.r2.message.RequestContext)124 URI (java.net.URI)112 RestResponse (com.linkedin.r2.message.rest.RestResponse)111 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)105 ByteString (com.linkedin.data.ByteString)67 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)61 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)59 RestException (com.linkedin.r2.message.rest.RestException)54 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)50 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)47 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)44 FutureCallback (com.linkedin.common.callback.FutureCallback)41 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)38 HashMap (java.util.HashMap)38 Callback (com.linkedin.common.callback.Callback)34 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)34 ExecutionException (java.util.concurrent.ExecutionException)33 BeforeTest (org.testng.annotations.BeforeTest)31