use of voldemort.store.PersistenceFailureException in project voldemort by voldemort.
the class RocksdbStorageEngineAPITest method generatePutWithConflictionVersions.
public List<Versioned<byte[]>> generatePutWithConflictionVersions(ByteArray key) {
List<Versioned<byte[]>> versionedList = new ArrayList<Versioned<byte[]>>();
VectorClock vectorClock1 = new VectorClock();
vectorClock1.incrementVersion(voldemortConfig.getNodeId(), System.currentTimeMillis());
Versioned<byte[]> value1 = new Versioned<byte[]>("valueOne".getBytes(), vectorClock1);
try {
// Do put
this.rocksDbStore.put(key, value1, null);
} catch (PersistenceFailureException pfe) {
Assert.fail("initial put failed unexpectedly. Exception: " + pfe.getMessage());
}
versionedList.add(value1);
VectorClock vectorClock2 = new VectorClock();
vectorClock2.incrementVersion(1, System.currentTimeMillis());
Versioned<byte[]> value2 = new Versioned<byte[]>("valueTwo".getBytes(), vectorClock2);
try {
// Do put
this.rocksDbStore.put(key, value2, null);
} catch (PersistenceFailureException pfe) {
Assert.fail("initial put failed unexpectedly. Exception: " + pfe.getMessage());
}
versionedList.add(value2);
return versionedList;
}
use of voldemort.store.PersistenceFailureException in project voldemort by voldemort.
the class BdbStorageEngine method getAndLock.
@Override
public KeyLockHandle<byte[]> getAndLock(ByteArray key) {
long startTimeNs = -1;
if (logger.isTraceEnabled())
startTimeNs = System.nanoTime();
StoreUtils.assertValidKey(key);
DatabaseEntry keyEntry = new DatabaseEntry(key.get());
DatabaseEntry valueEntry = new DatabaseEntry();
Transaction transaction = null;
List<Versioned<byte[]>> vals = null;
KeyLockHandle<byte[]> handle;
try {
transaction = environment.beginTransaction(null, null);
// do a get for the existing values
OperationStatus status = getBdbDatabase().get(transaction, keyEntry, valueEntry, LockMode.RMW);
if (OperationStatus.SUCCESS == status) {
vals = StoreBinaryFormat.fromByteArray(valueEntry.getData());
} else {
vals = new ArrayList<Versioned<byte[]>>(0);
}
handle = new KeyLockHandle<byte[]>(vals, transaction);
} catch (DatabaseException e) {
this.bdbEnvironmentStats.reportException(e);
// Unless we return out properly from this method, we need to ensure
// the transaction handle is closed on exception..
attemptAbort(transaction);
logger.error("Error in getAndLock for store " + this.getName(), e);
throw new PersistenceFailureException(e);
} finally {
if (logger.isTraceEnabled()) {
logger.trace("Completed getAndLock (" + getName() + ") to key " + key + " (keyRef: " + System.identityHashCode(key) + " in " + (System.nanoTime() - startTimeNs) + " ns at " + System.currentTimeMillis());
}
}
return handle;
}
use of voldemort.store.PersistenceFailureException in project voldemort by voldemort.
the class MysqlStorageEngine method put.
@Override
public void put(ByteArray key, Versioned<byte[]> value, byte[] transforms) throws PersistenceFailureException {
StoreUtils.assertValidKey(key);
boolean doCommit = false;
Connection conn = null;
PreparedStatement insert = null;
PreparedStatement select = null;
ResultSet results = null;
String insertSql = "insert into " + getName() + " (key_, version_, value_) values (?, ?, ?)";
String selectSql = "select version_ from " + getName() + " where key_ = ?";
try {
conn = datasource.getConnection();
conn.setAutoCommit(false);
// check for superior versions
select = conn.prepareStatement(selectSql);
select.setBytes(1, key.get());
results = select.executeQuery();
while (results.next()) {
VectorClock version = new VectorClock(results.getBytes("version_"));
Occurred occurred = value.getVersion().compare(version);
if (occurred == Occurred.BEFORE)
throw new ObsoleteVersionException("Attempt to put version " + value.getVersion() + " which is superceeded by " + version + ".");
else if (occurred == Occurred.AFTER)
delete(conn, key.get(), version.toBytes());
}
// Okay, cool, now put the value
insert = conn.prepareStatement(insertSql);
insert.setBytes(1, key.get());
VectorClock clock = (VectorClock) value.getVersion();
insert.setBytes(2, clock.toBytes());
insert.setBytes(3, value.getValue());
insert.executeUpdate();
doCommit = true;
} catch (SQLException e) {
if (e.getErrorCode() == MYSQL_ERR_DUP_KEY || e.getErrorCode() == MYSQL_ERR_DUP_ENTRY) {
throw new ObsoleteVersionException("Key or value already used.");
} else {
throw new PersistenceFailureException("Fix me!", e);
}
} finally {
if (conn != null) {
try {
if (doCommit)
conn.commit();
else
conn.rollback();
} catch (SQLException e) {
}
}
tryClose(results);
tryClose(insert);
tryClose(select);
tryClose(conn);
}
}
use of voldemort.store.PersistenceFailureException in project voldemort by voldemort.
the class MysqlStorageEngine method entries.
@Override
public ClosableIterator<Pair<ByteArray, Versioned<byte[]>>> entries() {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
String select = "select key_, version_, value_ from " + getName();
try {
conn = datasource.getConnection();
stmt = conn.prepareStatement(select);
rs = stmt.executeQuery();
return new MysqlClosableIterator(conn, stmt, rs);
} catch (SQLException e) {
throw new PersistenceFailureException("Fix me!", e);
}
}
use of voldemort.store.PersistenceFailureException in project voldemort by voldemort.
the class MysqlStorageEngine method getAll.
@Override
public Map<ByteArray, List<Versioned<byte[]>>> getAll(Iterable<ByteArray> keys, Map<ByteArray, byte[]> transforms) throws VoldemortException {
StoreUtils.assertValidKeys(keys);
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
String select = "select version_, value_ from " + getName() + " where key_ = ?";
try {
conn = datasource.getConnection();
stmt = conn.prepareStatement(select);
Map<ByteArray, List<Versioned<byte[]>>> result = StoreUtils.newEmptyHashMap(keys);
for (ByteArray key : keys) {
stmt.setBytes(1, key.get());
rs = stmt.executeQuery();
List<Versioned<byte[]>> found = Lists.newArrayList();
while (rs.next()) {
byte[] version = rs.getBytes("version_");
byte[] value = rs.getBytes("value_");
found.add(new Versioned<byte[]>(value, new VectorClock(version)));
}
if (found.size() > 0)
result.put(key, found);
}
return result;
} catch (SQLException e) {
throw new PersistenceFailureException("Fix me!", e);
} finally {
tryClose(rs);
tryClose(stmt);
tryClose(conn);
}
}
Aggregations