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"));
}
use of voldemort.versioning.VectorClock in project voldemort by voldemort.
the class RestServerAPITest method testGetAllWithConflictingVersions.
/**
* test getall with k1,k2. k1 has v1,v2 and k2 has v3
*/
@Test
public void testGetAllWithConflictingVersions() {
logger.info("\n\n******************** Testing Get All with multiple versions *******************\n\n");
Map<ByteArray, List<Versioned<byte[]>>> input = new HashMap<ByteArray, List<Versioned<byte[]>>>();
List<Versioned<byte[]>> valuesList2 = new ArrayList<Versioned<byte[]>>();
VectorClock vectorClock1 = new VectorClock();
vectorClock1.incrementVersion(voldemortConfig.getNodeId(), System.currentTimeMillis());
ByteArray key2 = new ByteArray("key22".getBytes());
Versioned<byte[]> value1 = new Versioned<byte[]>("value22".getBytes(), vectorClock1);
store.put(key2, value1, null);
valuesList2.add(value1);
VectorClock vectorClock2 = new VectorClock();
vectorClock2.incrementVersion(1, System.currentTimeMillis());
Versioned<byte[]> value2 = new Versioned<byte[]>("value23".getBytes(), vectorClock2);
store.put(key2, value2, null);
valuesList2.add(value2);
input.put(key2, valuesList2);
List<Versioned<byte[]>> valuesList3 = new ArrayList<Versioned<byte[]>>();
VectorClock vectorClock3 = new VectorClock();
vectorClock3.incrementVersion(voldemortConfig.getNodeId(), System.currentTimeMillis());
ByteArray key3 = new ByteArray("key23".getBytes());
Versioned<byte[]> value3 = new Versioned<byte[]>("value43".getBytes(), vectorClock3);
store.put(key3, value3, null);
valuesList3.add(value3);
input.put(key3, valuesList3);
Map<ByteArray, List<Versioned<byte[]>>> output = store.getAll(input.keySet(), null);
assertEquals(input, output);
// cleanup specific to this test case
deleteCreatedKeys(key2);
deleteCreatedKeys(key3);
}
Aggregations