Search in sources :

Example 36 with VectorClock

use of voldemort.versioning.VectorClock in project voldemort by voldemort.

the class RestRequestValidator method hasVectorClock.

/**
     * Retrieve and validate vector clock value from the REST request.
     * "X_VOLD_VECTOR_CLOCK" is the vector clock header.
     * 
     * @return true if present, false if missing
     */
protected boolean hasVectorClock(boolean isVectorClockOptional) {
    boolean result = false;
    String vectorClockHeader = this.request.getHeader(RestMessageHeaders.X_VOLD_VECTOR_CLOCK);
    if (vectorClockHeader != null) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            VectorClockWrapper vcWrapper = mapper.readValue(vectorClockHeader, VectorClockWrapper.class);
            this.parsedVectorClock = new VectorClock(vcWrapper.getVersions(), vcWrapper.getTimestamp());
            result = true;
        } catch (Exception e) {
            logger.error("Exception while parsing and constructing vector clock", e);
            RestErrorHandler.writeErrorResponse(this.messageEvent, HttpResponseStatus.BAD_REQUEST, "Invalid Vector Clock");
        }
    } else if (!isVectorClockOptional) {
        logger.error("Error when validating request. Missing Vector Clock");
        RestErrorHandler.writeErrorResponse(this.messageEvent, HttpResponseStatus.BAD_REQUEST, "Missing Vector Clock");
    } else {
        result = true;
    }
    return result;
}
Also used : VectorClock(voldemort.versioning.VectorClock) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) VoldemortException(voldemort.VoldemortException)

Example 37 with VectorClock

use of voldemort.versioning.VectorClock in project voldemort by voldemort.

the class RestUtils method deserializeVectorClock.

public static VectorClock deserializeVectorClock(String serializedVC) {
    VectorClock vc = null;
    if (serializedVC == null) {
        return null;
    }
    try {
        VectorClockWrapper vcWrapper = mapper.readValue(serializedVC, VectorClockWrapper.class);
        vc = new VectorClock(vcWrapper.getVersions(), vcWrapper.getTimestamp());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return vc;
}
Also used : VectorClock(voldemort.versioning.VectorClock) IOException(java.io.IOException) MappingException(voldemort.xml.MappingException) JDOMException(org.jdom.JDOMException)

Example 38 with VectorClock

use of voldemort.versioning.VectorClock in project voldemort by voldemort.

the class GetAllResponseSender method sendResponse.

/**
     * Sends nested multipart response. Outer multipart wraps all the keys
     * requested. Each key has a separate multipart for the versioned values.
     */
@Override
public void sendResponse(StoreStats performanceStats, boolean isFromLocalZone, long startTimeInMs) throws Exception {
    /*
         * Pay attention to the code below. Note that in this method we wrap a multiPart object with a mimeMessage.
         * However when writing to the outputStream we only send the multiPart object and not the entire
         * mimeMessage. This is intentional.
         *
         * In the earlier version of this code we used to create a multiPart object and just send that multiPart
         * across the wire.
         *
         * However, we later discovered that upon setting the content of a MimeBodyPart, JavaMail internally creates
         * a DataHandler object wrapping the object you passed in. The part's Content-Type header is not updated
         * immediately. In order to get the headers updated, one needs to to call MimeMessage.saveChanges() on the
         * enclosing message, which cascades down the MIME structure into a call to MimeBodyPart.updateHeaders()
         * on the body part. It's this updateHeaders call that transfers the content type from the
         * DataHandler to the part's MIME Content-Type header.
         *
         * To make sure that the Content-Type headers are being updated (without changing too much code), we decided
         * to wrap the multiPart in a mimeMessage, call mimeMessage.saveChanges() and then just send the multiPart.
         * This is to make sure multiPart's headers are updated accurately.
         */
    MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));
    // multiPartKeys is the outer multipart
    MimeMultipart multiPartKeys = new MimeMultipart();
    ByteArrayOutputStream keysOutputStream = new ByteArrayOutputStream();
    for (Entry<ByteArray, List<Versioned<byte[]>>> entry : versionedResponses.entrySet()) {
        ByteArray key = entry.getKey();
        String base64Key = RestUtils.encodeVoldemortKey(key.get());
        String contentLocationKey = "/" + this.storeName + "/" + base64Key;
        // Create the individual body part - for each key requested
        MimeBodyPart keyBody = new MimeBodyPart();
        try {
            // Add the right headers
            keyBody.addHeader(CONTENT_TYPE, "application/octet-stream");
            keyBody.addHeader(CONTENT_TRANSFER_ENCODING, "binary");
            keyBody.addHeader(CONTENT_LOCATION, contentLocationKey);
        } catch (MessagingException me) {
            logger.error("Exception while constructing key body headers", me);
            keysOutputStream.close();
            throw me;
        }
        // multiPartValues is the inner multipart
        MimeMultipart multiPartValues = new MimeMultipart();
        for (Versioned<byte[]> versionedValue : entry.getValue()) {
            byte[] responseValue = versionedValue.getValue();
            VectorClock vectorClock = (VectorClock) versionedValue.getVersion();
            String eTag = RestUtils.getSerializedVectorClock(vectorClock);
            numVectorClockEntries += vectorClock.getVersionMap().size();
            // Create the individual body part - for each versioned value of
            // a key
            MimeBodyPart valueBody = new MimeBodyPart();
            try {
                // Add the right headers
                valueBody.addHeader(CONTENT_TYPE, "application/octet-stream");
                valueBody.addHeader(CONTENT_TRANSFER_ENCODING, "binary");
                valueBody.addHeader(RestMessageHeaders.X_VOLD_VECTOR_CLOCK, eTag);
                valueBody.setContent(responseValue, "application/octet-stream");
                valueBody.addHeader(RestMessageHeaders.CONTENT_LENGTH, Integer.toString(responseValue.length));
                multiPartValues.addBodyPart(valueBody);
            } catch (MessagingException me) {
                logger.error("Exception while constructing value body part", me);
                keysOutputStream.close();
                throw me;
            }
        }
        try {
            // Add the inner multipart as the content of the outer body part
            keyBody.setContent(multiPartValues);
            multiPartKeys.addBodyPart(keyBody);
        } catch (MessagingException me) {
            logger.error("Exception while constructing key body part", me);
            keysOutputStream.close();
            throw me;
        }
    }
    message.setContent(multiPartKeys);
    message.saveChanges();
    try {
        multiPartKeys.writeTo(keysOutputStream);
    } catch (Exception e) {
        logger.error("Exception while writing mutipart to output stream", e);
        throw e;
    }
    ChannelBuffer responseContent = ChannelBuffers.dynamicBuffer();
    responseContent.writeBytes(keysOutputStream.toByteArray());
    // Create the Response object
    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
    // Set the right headers
    response.setHeader(CONTENT_TYPE, "multipart/binary");
    response.setHeader(CONTENT_TRANSFER_ENCODING, "binary");
    // Copy the data into the payload
    response.setContent(responseContent);
    response.setHeader(CONTENT_LENGTH, response.getContent().readableBytes());
    // Write the response to the Netty Channel
    if (logger.isDebugEnabled()) {
        String keyStr = getKeysHexString(this.versionedResponses.keySet());
        debugLog("GET_ALL", this.storeName, keyStr, startTimeInMs, System.currentTimeMillis(), numVectorClockEntries);
    }
    this.messageEvent.getChannel().write(response);
    if (performanceStats != null && isFromLocalZone) {
        recordStats(performanceStats, startTimeInMs, Tracked.GET_ALL);
    }
    keysOutputStream.close();
}
Also used : MessagingException(javax.mail.MessagingException) VectorClock(voldemort.versioning.VectorClock) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) Properties(java.util.Properties) MessagingException(javax.mail.MessagingException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) ByteArray(voldemort.utils.ByteArray) List(java.util.List) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 39 with VectorClock

use of voldemort.versioning.VectorClock in project voldemort by voldemort.

the class DynamicTimeoutStoreClient method putWithCustomTimeout.

/**
     * Performs a put operation with the specified composite request object
     * 
     * @param requestWrapper A composite request object containing the key and
     *        value
     * @return Version of the value for the successful put
     */
public Version putWithCustomTimeout(CompositeVoldemortRequest<K, V> requestWrapper) {
    validateTimeout(requestWrapper.getRoutingTimeoutInMs());
    List<Versioned<V>> versionedValues;
    long startTime = System.currentTimeMillis();
    String keyHexString = "";
    if (logger.isDebugEnabled()) {
        ByteArray key = (ByteArray) requestWrapper.getKey();
        keyHexString = RestUtils.getKeyHexString(key);
        logger.debug("PUT requested for key: " + keyHexString + " , for store: " + this.storeName + " at time(in ms): " + startTime + " . Nested GET and PUT VERSION requests to follow ---");
    }
    // We use the full timeout for doing the Get. In this, we're being
    // optimistic that the subsequent put might be faster such that all the
    // steps might finish within the allotted time
    requestWrapper.setResolveConflicts(true);
    versionedValues = getWithCustomTimeout(requestWrapper);
    Versioned<V> versioned = getItemOrThrow(requestWrapper.getKey(), null, versionedValues);
    long endTime = System.currentTimeMillis();
    if (versioned == null)
        versioned = Versioned.value(requestWrapper.getRawValue(), new VectorClock());
    else
        versioned.setObject(requestWrapper.getRawValue());
    // This should not happen unless there's a bug in the
    // getWithCustomTimeout
    long timeLeft = requestWrapper.getRoutingTimeoutInMs() - (endTime - startTime);
    if (timeLeft <= 0) {
        throw new StoreTimeoutException("PUT request timed out");
    }
    CompositeVersionedPutVoldemortRequest<K, V> putVersionedRequestObject = new CompositeVersionedPutVoldemortRequest<K, V>(requestWrapper.getKey(), versioned, timeLeft);
    putVersionedRequestObject.setRequestOriginTimeInMs(requestWrapper.getRequestOriginTimeInMs());
    Version result = putVersionedWithCustomTimeout(putVersionedRequestObject);
    long endTimeInMs = System.currentTimeMillis();
    if (logger.isDebugEnabled()) {
        logger.debug("PUT response received for key: " + keyHexString + " , for store: " + this.storeName + " at time(in ms): " + endTimeInMs);
    }
    return result;
}
Also used : Versioned(voldemort.versioning.Versioned) VectorClock(voldemort.versioning.VectorClock) CompositeVersionedPutVoldemortRequest(voldemort.store.CompositeVersionedPutVoldemortRequest) Version(voldemort.versioning.Version) StoreTimeoutException(voldemort.store.StoreTimeoutException) ByteArray(voldemort.utils.ByteArray)

Example 40 with VectorClock

use of voldemort.versioning.VectorClock in project voldemort by voldemort.

the class VersionedSerializer method toObject.

public Versioned<T> toObject(byte[] bytes) {
    VectorClock vectorClock = getVectorClock(bytes);
    int size = 1;
    if (vectorClock != null)
        size = vectorClock.sizeInBytes();
    T t = innerSerializer.toObject(ByteUtils.copy(bytes, size, bytes.length));
    return new Versioned<T>(t, vectorClock);
}
Also used : Versioned(voldemort.versioning.Versioned) VectorClock(voldemort.versioning.VectorClock)

Aggregations

VectorClock (voldemort.versioning.VectorClock)162 Versioned (voldemort.versioning.Versioned)73 Test (org.junit.Test)65 ByteArray (voldemort.utils.ByteArray)65 ArrayList (java.util.ArrayList)33 IOException (java.io.IOException)25 VoldemortException (voldemort.VoldemortException)24 List (java.util.List)22 HashMap (java.util.HashMap)21 ObsoleteVersionException (voldemort.versioning.ObsoleteVersionException)21 Version (voldemort.versioning.Version)16 Node (voldemort.cluster.Node)15 AbstractByteArrayStoreTest (voldemort.store.AbstractByteArrayStoreTest)11 StoreDefinition (voldemort.store.StoreDefinition)11 AdminClient (voldemort.client.protocol.admin.AdminClient)10 VoldemortServer (voldemort.server.VoldemortServer)10 Pair (voldemort.utils.Pair)10 ClockEntry (voldemort.versioning.ClockEntry)10 File (java.io.File)8 Map (java.util.Map)8