Search in sources :

Example 1 with OStorageOperationResult

use of com.orientechnologies.orient.core.storage.OStorageOperationResult in project orientdb by orientechnologies.

the class ODistributedTransactionManager method createUndoTasksFromTx.

/**
   * Create undo content for distributed 2-phase rollback. This list of undo tasks is sent to all the nodes to revert a transaction
   * and it's also applied locally.
   *
   * @param iTx Current transaction
   *
   * @return List of remote undo tasks
   */
protected List<OAbstractRemoteTask> createUndoTasksFromTx(final OTransaction iTx) {
    final List<OAbstractRemoteTask> undoTasks = new ArrayList<OAbstractRemoteTask>();
    for (ORecordOperation op : iTx.getAllRecordEntries()) {
        OAbstractRemoteTask undoTask = null;
        final ORecord record = op.getRecord();
        switch(op.type) {
            case ORecordOperation.CREATED:
                // CREATE UNDO TASK LATER ONCE THE RID HAS BEEN ASSIGNED
                break;
            case ORecordOperation.UPDATED:
            case ORecordOperation.DELETED:
                // CREATE UNDO TASK WITH THE PREVIOUS RECORD CONTENT/VERSION
                final ORecordId rid = (ORecordId) record.getIdentity();
                final AtomicReference<ORecord> previousRecord = new AtomicReference<ORecord>();
                OScenarioThreadLocal.executeAsDefault(new Callable<Object>() {

                    @Override
                    public Object call() throws Exception {
                        final ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.INSTANCE.get();
                        final ORecordOperation txEntry = db.getTransaction().getRecordEntry(rid);
                        if (txEntry != null && txEntry.type == ORecordOperation.DELETED)
                            // GET DELETED RECORD FROM TX
                            previousRecord.set(txEntry.getRecord());
                        else {
                            final OStorageOperationResult<ORawBuffer> loadedBuffer = ODatabaseRecordThreadLocal.INSTANCE.get().getStorage().getUnderlying().readRecord(rid, null, true, false, null);
                            if (loadedBuffer != null) {
                                // LOAD THE RECORD FROM THE STORAGE AVOIDING USING THE DB TO GET THE TRANSACTIONAL CHANGES
                                final ORecord loaded = Orient.instance().getRecordFactoryManager().newInstance(loadedBuffer.getResult().recordType);
                                ORecordInternal.fill(loaded, rid, loadedBuffer.getResult().version, loadedBuffer.getResult().getBuffer(), false);
                                previousRecord.set(loaded);
                            } else
                                // RECORD NOT FOUND ON LOCAL STORAGE, ASK TO DB BECAUSE IT COULD BE SHARDED AND RESIDE ON ANOTHER SERVER
                                previousRecord.set(db.load(rid));
                        }
                        return null;
                    }
                });
                if (previousRecord.get() == null)
                    throw new ORecordNotFoundException(rid);
                if (op.type == ORecordOperation.UPDATED)
                    undoTask = new OFixUpdateRecordTask(previousRecord.get(), ORecordVersionHelper.clearRollbackMode(previousRecord.get().getVersion()));
                else
                    undoTask = new OResurrectRecordTask(previousRecord.get());
                break;
            default:
                continue;
        }
        if (undoTask != null)
            undoTasks.add(undoTask);
    }
    return undoTasks;
}
Also used : OStorageOperationResult(com.orientechnologies.orient.core.storage.OStorageOperationResult) AtomicReference(java.util.concurrent.atomic.AtomicReference) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OException(com.orientechnologies.common.exception.OException) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) IOException(java.io.IOException) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) ORecordOperation(com.orientechnologies.orient.core.db.record.ORecordOperation) ORecord(com.orientechnologies.orient.core.record.ORecord)

Aggregations

ONeedRetryException (com.orientechnologies.common.concur.ONeedRetryException)1 OException (com.orientechnologies.common.exception.OException)1 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)1 ORecordOperation (com.orientechnologies.orient.core.db.record.ORecordOperation)1 ORecordId (com.orientechnologies.orient.core.id.ORecordId)1 ORecord (com.orientechnologies.orient.core.record.ORecord)1 OStorageOperationResult (com.orientechnologies.orient.core.storage.OStorageOperationResult)1 IOException (java.io.IOException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1