Search in sources :

Example 6 with PersistenceFailureException

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;
}
Also used : Versioned(voldemort.versioning.Versioned) VectorClock(voldemort.versioning.VectorClock) ArrayList(java.util.ArrayList) PersistenceFailureException(voldemort.store.PersistenceFailureException)

Example 7 with PersistenceFailureException

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;
}
Also used : Transaction(com.sleepycat.je.Transaction) Versioned(voldemort.versioning.Versioned) OperationStatus(com.sleepycat.je.OperationStatus) AsyncOperationStatus(voldemort.server.protocol.admin.AsyncOperationStatus) DatabaseEntry(com.sleepycat.je.DatabaseEntry) DatabaseException(com.sleepycat.je.DatabaseException) PersistenceFailureException(voldemort.store.PersistenceFailureException)

Example 8 with PersistenceFailureException

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);
    }
}
Also used : ObsoleteVersionException(voldemort.versioning.ObsoleteVersionException) SQLException(java.sql.SQLException) VectorClock(voldemort.versioning.VectorClock) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) PersistenceFailureException(voldemort.store.PersistenceFailureException) Occurred(voldemort.versioning.Occurred)

Example 9 with PersistenceFailureException

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);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) PersistenceFailureException(voldemort.store.PersistenceFailureException)

Example 10 with PersistenceFailureException

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);
    }
}
Also used : Versioned(voldemort.versioning.Versioned) SQLException(java.sql.SQLException) VectorClock(voldemort.versioning.VectorClock) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) PersistenceFailureException(voldemort.store.PersistenceFailureException) ResultSet(java.sql.ResultSet) ByteArray(voldemort.utils.ByteArray) List(java.util.List)

Aggregations

PersistenceFailureException (voldemort.store.PersistenceFailureException)26 Versioned (voldemort.versioning.Versioned)13 ByteArray (voldemort.utils.ByteArray)9 Connection (java.sql.Connection)6 PreparedStatement (java.sql.PreparedStatement)6 SQLException (java.sql.SQLException)6 DatabaseEntry (com.sleepycat.je.DatabaseEntry)5 DatabaseException (com.sleepycat.je.DatabaseException)5 OperationStatus (com.sleepycat.je.OperationStatus)5 ResultSet (java.sql.ResultSet)5 Test (org.junit.Test)5 AsyncOperationStatus (voldemort.server.protocol.admin.AsyncOperationStatus)5 Transaction (com.sleepycat.je.Transaction)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 RocksDBException (org.rocksdb.RocksDBException)4 ObsoleteVersionException (voldemort.versioning.ObsoleteVersionException)4 VectorClock (voldemort.versioning.VectorClock)4 Occurred (voldemort.versioning.Occurred)3 LockTimeoutException (com.sleepycat.je.LockTimeoutException)2