use of voldemort.versioning.VectorClock in project YCSB by brianfrankcooper.
the class VoldemortClient method update.
@Override
public Status update(String table, String key, HashMap<String, ByteIterator> values) {
if (checkStore(table) == Status.ERROR) {
return Status.ERROR;
}
Versioned<HashMap<String, String>> versionedValue = storeClient.get(key);
HashMap<String, String> value = new HashMap<String, String>();
VectorClock version;
if (versionedValue != null) {
version = ((VectorClock) versionedValue.getVersion()).incremented(0, 1);
value = versionedValue.getValue();
for (Entry<String, ByteIterator> entry : values.entrySet()) {
value.put(entry.getKey(), entry.getValue().toString());
}
} else {
version = new VectorClock();
StringByteIterator.putAllAsStrings(value, values);
}
storeClient.put(key, Versioned.value(value, version));
return Status.OK;
}
use of voldemort.versioning.VectorClock in project voldemort by voldemort.
the class KratiStorageEngine method assembleValues.
/**
* Store the versioned values
*
* @param values list of versioned bytes
* @return the list of versioned values rolled into an array of bytes
*/
private byte[] assembleValues(List<Versioned<byte[]>> values) throws IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
DataOutputStream dataStream = new DataOutputStream(stream);
for (Versioned<byte[]> value : values) {
byte[] object = value.getValue();
dataStream.writeInt(object.length);
dataStream.write(object);
VectorClock clock = (VectorClock) value.getVersion();
dataStream.writeInt(clock.sizeInBytes());
dataStream.write(clock.toBytes());
}
return stream.toByteArray();
}
use of voldemort.versioning.VectorClock 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());
}
}
use of voldemort.versioning.VectorClock in project voldemort by voldemort.
the class SampleRESTClient method main.
public static void main(String[] args) {
// Create the client
Properties props = new Properties();
props.setProperty(ClientConfig.BOOTSTRAP_URLS_PROPERTY, "http://localhost:8080");
props.setProperty(ClientConfig.ROUTING_TIMEOUT_MS_PROPERTY, "1500");
RESTClientFactoryConfig mainConfig = new RESTClientFactoryConfig(props, null);
RESTClientFactory factory = new RESTClientFactory(mainConfig);
StoreClient<String, String> storeClient = factory.getStoreClient("test");
try {
// Sample put
System.out.println("First valid put");
storeClient.put("a", "Howdy!!!!");
System.out.println("Second valid put");
storeClient.put("b", "Partner!!!!");
// Do a sample get operation:
Versioned<String> versionedValue = storeClient.get("a");
System.out.println("Received response : " + versionedValue);
Version obsoleteVersion = ((VectorClock) versionedValue.getVersion()).clone();
// Do a versioned put operation:
System.out.println("First versioned put");
versionedValue.setObject("New Value !!!");
System.out.println("************* original version : " + versionedValue.getVersion());
Version putVersion = storeClient.put("a", versionedValue);
System.out.println("************* Updated version : " + putVersion);
// Obsolete version put
System.out.println("Obsolete put");
Versioned<String> obsoleteVersionedValue = new Versioned<String>("Obsolete value", obsoleteVersion);
try {
storeClient.put("a", obsoleteVersionedValue);
System.err.println(" **************** Should not reach this point **************** ");
} catch (Exception e) {
System.out.println("Exception occurred as expected: " + e.getMessage());
}
// Do a get again on the last versioned put operation:
versionedValue = storeClient.get("a");
System.out.println("Received response on the versioned put: " + versionedValue);
System.out.println("Versioned put based on the last put ");
Versioned<String> newVersionedPut = new Versioned<String>("Yet another value !!!", putVersion);
storeClient.put("a", newVersionedPut);
// Do a get again on the last versioned put operation:
versionedValue = storeClient.get("a");
System.out.println("Received response on the (second) versioned put: " + versionedValue);
List<String> keyList = new ArrayList<String>();
keyList.add("a");
keyList.add("b");
System.out.println("Received response : " + storeClient.getAll(keyList));
} finally {
factory.close();
}
}
use of voldemort.versioning.VectorClock in project voldemort by voldemort.
the class RestClientTest method testDeleteVersion.
@Override
@Test
public void testDeleteVersion() {
VectorClock vc = new VectorClock();
vc.incrementVersion(this.nodeId, System.currentTimeMillis());
VectorClock initialVC = vc.clone();
assertFalse("Delete of non-existant key should be false.", client.delete("k", vc));
client.put("k", new Versioned<String>("v", vc));
assertFalse("Delete of a lesser version should be false.", client.delete("k", initialVC));
assertNotNull("After failed delete, value should still be there.", client.get("k"));
assertTrue("Delete of k, with the current version should succeed.", client.delete("k", initialVC.incremented(this.nodeId, time.getMilliseconds())));
assertNull("After a successful delete(k), get(k) should return null.", client.get("k"));
}
Aggregations