Search in sources :

Example 61 with RestException

use of com.linkedin.r2.message.rest.RestException in project voldemort by voldemort.

the class R2Store method getSerializerInfoXml.

public String getSerializerInfoXml() throws VoldemortException {
    RestRequestBuilder rb = null;
    try {
        String base64Key = new String(Base64.encodeBase64(getName().getBytes("UTF-8")));
        rb = new RestRequestBuilder(new URI(this.restBootstrapURL + "/" + SCHEMATA_STORE_NAME + "/" + base64Key));
        rb.setHeader("Accept", "binary");
        rb.setHeader(RestMessageHeaders.X_VOLD_REQUEST_ORIGIN_TIME_MS, String.valueOf(System.currentTimeMillis()));
        if (this.routingTypeCode != null) {
            rb.setHeader(RestMessageHeaders.X_VOLD_ROUTING_TYPE_CODE, this.routingTypeCode);
        }
        if (this.zoneId != INVALID_ZONE_ID) {
            rb.setHeader(RestMessageHeaders.X_VOLD_ZONE_ID, String.valueOf(this.zoneId));
        }
        RestResponse response = fetchGetResponse(rb, FETCH_SCHEMA_TIMEOUT_MS);
        return response.getEntity().asString("UTF-8");
    } catch (ExecutionException e) {
        if (e.getCause() instanceof RestException) {
            RestException exception = (RestException) e.getCause();
            if (logger.isDebugEnabled()) {
                logger.debug("REST EXCEPTION STATUS : " + exception.getResponse().getStatus());
            }
        } else {
            throw new VoldemortException("Unknown HTTP request execution exception: " + e.getMessage(), e);
        }
    } catch (InterruptedException e) {
        if (logger.isDebugEnabled()) {
            logger.debug("Operation interrupted : " + e.getMessage(), e);
        }
        throw new VoldemortException("Operation interrupted exception: " + e.getMessage(), e);
    } catch (URISyntaxException e) {
        throw new VoldemortException("Illegal HTTP URL" + e.getMessage(), e);
    } catch (UnsupportedEncodingException e) {
        throw new VoldemortException("Unsupported Encoding exception while encoding the key" + e.getMessage(), e);
    }
    return null;
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) RestException(com.linkedin.r2.message.rest.RestException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ByteString(com.linkedin.data.ByteString) URISyntaxException(java.net.URISyntaxException) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) VoldemortException(voldemort.VoldemortException)

Example 62 with RestException

use of com.linkedin.r2.message.rest.RestException in project voldemort by voldemort.

the class R2Store method delete.

@Override
public boolean delete(ByteArray key, Version version) throws VoldemortException {
    try {
        // Create the REST request with this byte array
        String base64Key = RestUtils.encodeVoldemortKey(key.get());
        RestRequestBuilder rb = new RestRequestBuilder(new URI(this.restBootstrapURL + "/" + getName() + "/" + base64Key));
        // Create a HTTP POST request
        rb.setMethod(DELETE);
        rb.setHeader(CONTENT_LENGTH, "0");
        String timeoutStr = Long.toString(this.config.getTimeoutConfig().getOperationTimeout(VoldemortOpCode.DELETE_OP_CODE));
        rb.setHeader(RestMessageHeaders.X_VOLD_REQUEST_TIMEOUT_MS, timeoutStr);
        rb.setHeader(RestMessageHeaders.X_VOLD_REQUEST_ORIGIN_TIME_MS, String.valueOf(System.currentTimeMillis()));
        if (this.routingTypeCode != null) {
            rb.setHeader(RestMessageHeaders.X_VOLD_ROUTING_TYPE_CODE, this.routingTypeCode);
        }
        if (this.zoneId != INVALID_ZONE_ID) {
            rb.setHeader(RestMessageHeaders.X_VOLD_ZONE_ID, String.valueOf(this.zoneId));
        }
        // Serialize the Vector clock
        VectorClock vc = (VectorClock) version;
        // doing the put.
        if (vc != null && vc.getEntries().size() != 0) {
            String serializedVC = null;
            if (!vc.getEntries().isEmpty()) {
                serializedVC = RestUtils.getSerializedVectorClock(vc);
            }
            if (serializedVC != null && serializedVC.length() > 0) {
                rb.setHeader(RestMessageHeaders.X_VOLD_VECTOR_CLOCK, serializedVC);
            }
        }
        RestRequest request = rb.build();
        Future<RestResponse> f = client.restRequest(request);
        // This will block
        RestResponse response = f.get();
        final ByteString entity = response.getEntity();
        if (entity == null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Empty response !");
            }
        }
    } catch (ExecutionException e) {
        if (e.getCause() instanceof RestException) {
            RestException exception = (RestException) e.getCause();
            if (logger.isDebugEnabled()) {
                logger.debug("REST EXCEPTION STATUS : " + exception.getResponse().getStatus());
            }
            if (exception.getResponse().getStatus() == NOT_FOUND.getCode()) {
                return false;
            }
        } else {
            throw new VoldemortException("Unknown HTTP request execution exception: " + e.getMessage(), e);
        }
    } catch (InterruptedException e) {
        if (logger.isDebugEnabled()) {
            logger.debug("Operation interrupted : " + e.getMessage());
        }
        throw new VoldemortException("Operation Interrupted: " + e.getMessage(), e);
    } catch (URISyntaxException e) {
        throw new VoldemortException("Illegal HTTP URL" + e.getMessage(), e);
    }
    return true;
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) ByteString(com.linkedin.data.ByteString) VectorClock(voldemort.versioning.VectorClock) RestException(com.linkedin.r2.message.rest.RestException) ByteString(com.linkedin.data.ByteString) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) VoldemortException(voldemort.VoldemortException) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ExecutionException(java.util.concurrent.ExecutionException)

Example 63 with RestException

use of com.linkedin.r2.message.rest.RestException in project voldemort by voldemort.

the class CoordinatorAdminClient method deleteStoreClientConfig.

public boolean deleteStoreClientConfig(List<String> storeNames, String coordinatorUrl) {
    String responseMessage = null;
    Boolean success = false;
    try {
        // Create the REST request
        StringBuilder URIStringBuilder = new StringBuilder().append(coordinatorUrl).append(URL_SEPARATOR).append(STORE_CLIENT_CONFIG_OPS).append(URL_SEPARATOR).append(Joiner.on(",").join(storeNames));
        RestRequestBuilder requestBuilder = new RestRequestBuilder(new URI(URIStringBuilder.toString()));
        String timeoutStr = Long.toString(this.config.getTimeoutConfig().getOperationTimeout(VoldemortOpCode.GET_OP_CODE));
        // Create a HTTP POST request
        requestBuilder.setMethod(requestType.DELETE.toString());
        requestBuilder.setHeader(RestMessageHeaders.X_VOLD_REQUEST_TIMEOUT_MS, timeoutStr);
        requestBuilder = setCommonRequestHeader(requestBuilder);
        RestRequest request = requestBuilder.build();
        Future<RestResponse> future = client.restRequest(request);
        // This will block
        RestResponse response = future.get();
        final ByteString entity = response.getEntity();
        if (entity == null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Empty response !");
            }
            responseMessage = "Received empty response from " + coordinatorUrl;
        } else {
            responseMessage = entity.asString("UTF-8");
            success = true;
        }
    } catch (Exception e) {
        if (e.getCause() instanceof RestException) {
            responseMessage = ((RestException) e.getCause()).getResponse().getEntity().asString("UTF-8");
        } else {
            responseMessage = "An exception other than RestException happens!";
        }
        handleRequestAndResponseException(e);
    } finally {
        System.out.println(responseMessage);
    }
    return success;
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) ByteString(com.linkedin.data.ByteString) RestException(com.linkedin.r2.message.rest.RestException) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ByteString(com.linkedin.data.ByteString) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) RestException(com.linkedin.r2.message.rest.RestException) VoldemortException(voldemort.VoldemortException) ExecutionException(java.util.concurrent.ExecutionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 64 with RestException

use of com.linkedin.r2.message.rest.RestException in project voldemort by voldemort.

the class CoordinatorAdminClient method getStoreClientConfigString.

public String getStoreClientConfigString(List<String> storeNames, String coordinatorUrl) {
    try {
        // Create the REST request
        StringBuilder URIStringBuilder = new StringBuilder().append(coordinatorUrl).append(URL_SEPARATOR).append(STORE_CLIENT_CONFIG_OPS).append(URL_SEPARATOR).append(Joiner.on(",").join(storeNames));
        RestRequestBuilder requestBuilder = new RestRequestBuilder(new URI(URIStringBuilder.toString()));
        String timeoutStr = Long.toString(this.config.getTimeoutConfig().getOperationTimeout(VoldemortOpCode.GET_OP_CODE));
        requestBuilder.setMethod(requestType.GET.toString());
        requestBuilder.setHeader(RestMessageHeaders.X_VOLD_REQUEST_TIMEOUT_MS, timeoutStr);
        requestBuilder = setCommonRequestHeader(requestBuilder);
        RestRequest request = requestBuilder.build();
        Future<RestResponse> future = client.restRequest(request);
        // This will block
        RestResponse response = future.get();
        ByteString entity = response.getEntity();
        return entity.asString("UTF-8");
    } catch (Exception e) {
        if (e.getCause() instanceof RestException) {
            return ((RestException) e.getCause()).getResponse().getEntity().asString("UTF-8");
        }
        handleRequestAndResponseException(e);
    }
    return null;
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) ByteString(com.linkedin.data.ByteString) RestException(com.linkedin.r2.message.rest.RestException) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ByteString(com.linkedin.data.ByteString) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) RestException(com.linkedin.r2.message.rest.RestException) VoldemortException(voldemort.VoldemortException) ExecutionException(java.util.concurrent.ExecutionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

RestException (com.linkedin.r2.message.rest.RestException)62 RestResponse (com.linkedin.r2.message.rest.RestResponse)48 Test (org.testng.annotations.Test)44 RestRequest (com.linkedin.r2.message.rest.RestRequest)33 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)31 URI (java.net.URI)23 BeforeTest (org.testng.annotations.BeforeTest)21 ExecutionException (java.util.concurrent.ExecutionException)20 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)19 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)19 RequestContext (com.linkedin.r2.message.RequestContext)18 Callback (com.linkedin.common.callback.Callback)16 URISyntaxException (java.net.URISyntaxException)16 ByteString (com.linkedin.data.ByteString)15 AfterTest (org.testng.annotations.AfterTest)12 MultiPartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.MultiPartMIMEFullReaderCallback)11 SinglePartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback)11 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)11 RequestExecutionReport (com.linkedin.restli.server.RequestExecutionReport)11 RestLiResponseAttachments (com.linkedin.restli.server.RestLiResponseAttachments)11