Search in sources :

Example 6 with ByteString

use of com.linkedin.data.ByteString in project voldemort by voldemort.

the class R2Store method put.

@Override
public void put(ByteArray key, Versioned<byte[]> value, byte[] transform) throws VoldemortException {
    RestResponse response = null;
    try {
        byte[] payload = value.getValue();
        // 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(POST);
        rb.setEntity(payload);
        rb.setHeader(CONTENT_TYPE, "binary");
        rb.setHeader(CONTENT_LENGTH, "" + payload.length);
        String timeoutStr = Long.toString(this.config.getTimeoutConfig().getOperationTimeout(VoldemortOpCode.PUT_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) value.getVersion();
        // doing the put.
        if (vc != null) {
            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
        response = f.get();
        String serializedUpdatedVC = response.getHeader(RestMessageHeaders.X_VOLD_VECTOR_CLOCK);
        if (serializedUpdatedVC == null || serializedUpdatedVC.length() == 0) {
            if (logger.isDebugEnabled()) {
                logger.debug("Received empty vector clock in the response");
            }
        } else {
            VectorClock updatedVC = RestUtils.deserializeVectorClock(serializedUpdatedVC);
            VectorClock originalVC = (VectorClock) value.getVersion();
            originalVC.copyFromVectorClock(updatedVC);
        }
        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());
            }
            int httpErrorStatus = exception.getResponse().getStatus();
            if (httpErrorStatus == BAD_REQUEST.getCode()) {
                throw new VoldemortException("Bad request: " + e.getMessage(), e);
            } else if (httpErrorStatus == PRECONDITION_FAILED.getCode()) {
                throw new ObsoleteVersionException(e.getMessage());
            } else if (httpErrorStatus == REQUEST_TIMEOUT.getCode() || httpErrorStatus == INTERNAL_SERVER_ERROR.getCode()) {
                throw new InsufficientOperationalNodesException(e.getMessage());
            }
        }
        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("Unknown Voldemort exception: " + e.getMessage());
    } catch (URISyntaxException e) {
        throw new VoldemortException("Illegal HTTP URL" + e.getMessage());
    }
}
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) ObsoleteVersionException(voldemort.versioning.ObsoleteVersionException) InsufficientOperationalNodesException(voldemort.store.InsufficientOperationalNodesException) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ExecutionException(java.util.concurrent.ExecutionException)

Example 7 with ByteString

use of com.linkedin.data.ByteString in project voldemort by voldemort.

the class R2Store method getAll.

@Override
public Map<ByteArray, List<Versioned<byte[]>>> getAll(Iterable<ByteArray> keys, Map<ByteArray, byte[]> transforms) throws VoldemortException {
    Map<ByteArray, List<Versioned<byte[]>>> resultMap = new HashMap<ByteArray, List<Versioned<byte[]>>>();
    int numberOfKeys = 0;
    try {
        Iterator<ByteArray> it = keys.iterator();
        StringBuilder keyArgs = null;
        while (it.hasNext()) {
            ByteArray key = it.next();
            String base64Key = RestUtils.encodeVoldemortKey(key.get());
            if (keyArgs == null) {
                keyArgs = new StringBuilder();
                keyArgs.append(base64Key);
            } else {
                keyArgs.append("," + base64Key);
            }
            numberOfKeys++;
        }
        // TODO a common way to handle getAll with any number of keys
        if (numberOfKeys == 1) {
            List<Versioned<byte[]>> resultList = new ArrayList<Versioned<byte[]>>();
            it = keys.iterator();
            ByteArray key = it.next();
            byte[] singleKeyTransforms = null;
            if (transforms != null) {
                singleKeyTransforms = transforms.get(key);
            }
            resultList = this.get(key, singleKeyTransforms);
            resultMap.put(key, resultList);
        } else {
            RestRequestBuilder rb = new RestRequestBuilder(new URI(this.restBootstrapURL + "/" + getName() + "/" + keyArgs.toString()));
            rb.setMethod(GET);
            rb.setHeader("Accept", MULTIPART_CONTENT_TYPE);
            String timeoutStr = Long.toString(this.config.getTimeoutConfig().getOperationTimeout(VoldemortOpCode.GET_ALL_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));
            }
            RestRequest request = rb.build();
            Future<RestResponse> f = client.restRequest(request);
            // This will block
            RestResponse response = f.get();
            // Parse the response
            final ByteString entity = response.getEntity();
            String contentType = response.getHeader(CONTENT_TYPE);
            if (entity != null) {
                if (contentType.equalsIgnoreCase(MULTIPART_CONTENT_TYPE)) {
                    resultMap = parseGetAllResults(entity);
                } else {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Did not receive a multipart response");
                    }
                }
            } else {
                if (logger.isDebugEnabled()) {
                    logger.debug("Did not get any 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());
            }
        } 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);
    }
    return resultMap;
}
Also used : Versioned(voldemort.versioning.Versioned) HashMap(java.util.HashMap) RestResponse(com.linkedin.r2.message.rest.RestResponse) ByteString(com.linkedin.data.ByteString) ArrayList(java.util.ArrayList) 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) ByteArray(voldemort.utils.ByteArray) List(java.util.List) ArrayList(java.util.ArrayList) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ExecutionException(java.util.concurrent.ExecutionException)

Example 8 with ByteString

use of com.linkedin.data.ByteString in project voldemort by voldemort.

the class CoordinatorAdminClient method putStoreClientConfigString.

public boolean putStoreClientConfigString(String storeClientConfigAvro, 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);
        RestRequestBuilder requestBuilder = new RestRequestBuilder(new URI(URIStringBuilder.toString()));
        byte[] payload = storeClientConfigAvro.getBytes("UTF-8");
        // Create a HTTP POST request
        requestBuilder.setMethod(requestType.POST.toString());
        requestBuilder.setEntity(payload);
        requestBuilder.setHeader(CONTENT_TYPE, "binary");
        requestBuilder.setHeader(CONTENT_LENGTH, "" + payload.length);
        String timeoutStr = Long.toString(this.config.getTimeoutConfig().getOperationTimeout(VoldemortOpCode.PUT_OP_CODE));
        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 9 with ByteString

use of com.linkedin.data.ByteString in project rest.li by linkedin.

the class BackupRequestsClient method streamRequest.

@Override
public void streamRequest(StreamRequest request, RequestContext requestContext, Callback<StreamResponse> callback) {
    // Currently only support backup requests with IS_FULL_REQUEST.
    if (!isFullRequest(requestContext)) {
        _d2Client.streamRequest(request, requestContext, callback);
        return;
    }
    if (!isBuffered(requestContext)) {
        final FullEntityObserver observer = new FullEntityObserver(new Callback<ByteString>() {

            @Override
            public void onError(Throwable e) {
                LOG.warn("Failed to record request's entity for retrying backup request.");
            }

            @Override
            public void onSuccess(ByteString result) {
                requestContext.putLocalAttr(R2Constants.BACKUP_REQUEST_BUFFERED_BODY, result);
            }
        });
        request.getEntityStream().addObserver(observer);
    }
    if (_isD2Async) {
        requestAsync(request, requestContext, _d2Client::streamRequest, callback);
        return;
    }
    _d2Client.streamRequest(request, requestContext, decorateCallbackSync(request, requestContext, _d2Client::streamRequest, callback));
}
Also used : ByteString(com.linkedin.data.ByteString) FullEntityObserver(com.linkedin.r2.message.stream.entitystream.FullEntityObserver)

Example 10 with ByteString

use of com.linkedin.data.ByteString in project rest.li by linkedin.

the class AbstractDefaultDataTranslator method translate.

protected Object translate(List<Object> path, Object value, DataSchema dataSchema) {
    dataSchema = dataSchema.getDereferencedDataSchema();
    DataSchema.Type type = dataSchema.getType();
    Object result;
    switch(type) {
        case NULL:
            if (value != Data.NULL) {
                throw new IllegalArgumentException(message(path, "value must be null for null schema"));
            }
            result = value;
            break;
        case BOOLEAN:
            result = ((Boolean) value).booleanValue();
            break;
        case INT:
            result = ((Number) value).intValue();
            break;
        case LONG:
            result = ((Number) value).longValue();
            break;
        case FLOAT:
            result = ((Number) value).floatValue();
            break;
        case DOUBLE:
            result = ((Number) value).doubleValue();
            break;
        case STRING:
            result = (String) value;
            break;
        case BYTES:
            Class<?> clazz = value.getClass();
            if (clazz != String.class && clazz != ByteString.class) {
                throw new IllegalArgumentException(message(path, "bytes value %1$s is not a String or ByteString", value));
            }
            result = value;
            break;
        case ENUM:
            String enumValue = (String) value;
            EnumDataSchema enumDataSchema = (EnumDataSchema) dataSchema;
            if (!enumDataSchema.getSymbols().contains(enumValue)) {
                throw new IllegalArgumentException(message(path, "enum value %1$s not one of %2$s", value, enumDataSchema.getSymbols()));
            }
            result = value;
            break;
        case FIXED:
            clazz = value.getClass();
            ByteString byteString;
            if (clazz == String.class) {
                byteString = ByteString.copyAvroString((String) value, true);
            } else if (clazz == ByteString.class) {
                byteString = (ByteString) value;
            } else {
                throw new IllegalArgumentException(message(path, "fixed value %1$s is not a String or ByteString", value));
            }
            FixedDataSchema fixedDataSchema = (FixedDataSchema) dataSchema;
            if (fixedDataSchema.getSize() != byteString.length()) {
                throw new IllegalArgumentException(message(path, "ByteString size %1$d != FixedDataSchema size %2$d", byteString.length(), fixedDataSchema.getSize()));
            }
            result = byteString;
            break;
        case MAP:
            DataMap map = (DataMap) value;
            DataSchema valueDataSchema = ((MapDataSchema) dataSchema).getValues();
            Map<String, Object> resultMap = new DataMap(map.size() * 2);
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                String key = entry.getKey();
                path.add(key);
                Object entryAvroValue = translate(path, entry.getValue(), valueDataSchema);
                path.remove(path.size() - 1);
                resultMap.put(key, entryAvroValue);
            }
            result = resultMap;
            break;
        case ARRAY:
            DataList list = (DataList) value;
            DataList resultList = new DataList(list.size());
            DataSchema elementDataSchema = ((ArrayDataSchema) dataSchema).getItems();
            for (int i = 0; i < list.size(); i++) {
                path.add(i);
                Object entryAvroValue = translate(path, list.get(i), elementDataSchema);
                path.remove(path.size() - 1);
                resultList.add(entryAvroValue);
            }
            result = resultList;
            break;
        case RECORD:
            DataMap recordMap = (DataMap) value;
            RecordDataSchema recordDataSchema = (RecordDataSchema) dataSchema;
            DataMap resultRecordMap = new DataMap(recordDataSchema.getFields().size() * 2);
            for (RecordDataSchema.Field field : recordDataSchema.getFields()) {
                String fieldName = field.getName();
                Object fieldValue = recordMap.get(fieldName);
                path.add(fieldName);
                Object resultFieldValue = translateField(path, fieldValue, field);
                path.remove(path.size() - 1);
                if (resultFieldValue != null) {
                    resultRecordMap.put(fieldName, resultFieldValue);
                }
            }
            result = resultRecordMap;
            break;
        case UNION:
            result = translateUnion(path, value, (UnionDataSchema) dataSchema);
            break;
        default:
            throw new IllegalStateException(message(path, "schema type unknown %1$s", type));
    }
    return result;
}
Also used : ByteString(com.linkedin.data.ByteString) MapDataSchema(com.linkedin.data.schema.MapDataSchema) ByteString(com.linkedin.data.ByteString) DataMap(com.linkedin.data.DataMap) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) DataList(com.linkedin.data.DataList) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataMap(com.linkedin.data.DataMap) Map(java.util.Map)

Aggregations

ByteString (com.linkedin.data.ByteString)152 Test (org.testng.annotations.Test)77 ByteArrayOutputStream (java.io.ByteArrayOutputStream)33 MimeMultipart (javax.mail.internet.MimeMultipart)31 MimeBodyPart (javax.mail.internet.MimeBodyPart)26 DataMap (com.linkedin.data.DataMap)25 RestResponse (com.linkedin.r2.message.rest.RestResponse)25 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)22 FullEntityReader (com.linkedin.r2.message.stream.entitystream.FullEntityReader)22 RestRequest (com.linkedin.r2.message.rest.RestRequest)21 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)21 URI (java.net.URI)21 CountDownLatch (java.util.concurrent.CountDownLatch)20 RequestContext (com.linkedin.r2.message.RequestContext)18 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)18 Callback (com.linkedin.common.callback.Callback)17 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)14 RestException (com.linkedin.r2.message.rest.RestException)12 HashMap (java.util.HashMap)12 DataList (com.linkedin.data.DataList)11