Search in sources :

Example 1 with OAbstractRecordReplicatedTask

use of com.orientechnologies.orient.server.distributed.task.OAbstractRecordReplicatedTask in project orientdb by orientechnologies.

the class OTxTask method execute.

@Override
public Object execute(final ODistributedRequestId requestId, final OServer iServer, ODistributedServerManager iManager, final ODatabaseDocumentInternal database) throws Exception {
    ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "Executing transaction db=%s (reqId=%s)...", database.getName(), requestId);
    ODatabaseRecordThreadLocal.INSTANCE.set(database);
    final ODistributedDatabase ddb = iManager.getMessageService().getDatabase(database.getName());
    // CREATE A CONTEXT OF TX
    reqContext = ddb.registerTxContext(requestId);
    final ODistributedConfiguration dCfg = iManager.getDatabaseConfiguration(database.getName());
    result = new OTxTaskResult();
    if (tasks.size() == 0)
        // RETURN AFTER REGISTERED THE CONTEXT
        return result;
    database.begin();
    try {
        final OTransactionOptimistic tx = (OTransactionOptimistic) database.getTransaction();
        // REGISTER CREATE FIRST TO RESOLVE TEMP RIDS
        for (OAbstractRecordReplicatedTask task : tasks) {
            if (task instanceof OCreateRecordTask) {
                final OCreateRecordTask createRT = (OCreateRecordTask) task;
                final ORecordId rid = createRT.getRid();
                if (rid != null && rid.isPersistent()) {
                    if (rid.getRecord() != null)
                        // ALREADY CREATED: SKIP REGISTERING IN TX
                        continue;
                }
                final int clId = createRT.clusterId > -1 ? createRT.clusterId : createRT.getRid().isValid() ? createRT.getRid().getClusterId() : -1;
                final String clusterName = clId > -1 ? database.getClusterNameById(clId) : null;
                if (dCfg.isServerContainingCluster(iManager.getLocalNodeName(), clusterName))
                    tx.addRecord(createRT.getRecord(), ORecordOperation.CREATED, clusterName);
            }
        }
        final List<ORecordId> rids2Lock = new ArrayList<ORecordId>();
        // LOCK ALL THE RECORDS FIRST (ORDERED TO AVOID DEADLOCK)
        for (OAbstractRecordReplicatedTask task : tasks) rids2Lock.add(task.getRid());
        Collections.sort(rids2Lock);
        for (ORecordId rid : rids2Lock) reqContext.lock(rid, getRecordLock());
        for (OAbstractRecordReplicatedTask task : tasks) {
            final Object taskResult;
            // CHECK LOCAL CLUSTER IS AVAILABLE ON CURRENT SERVER
            if (!task.checkForClusterAvailability(iManager.getLocalNodeName(), dCfg))
                // SKIP EXECUTION BECAUSE THE CLUSTER IS NOT ON LOCAL NODE: THIS CAN HAPPENS IN CASE OF DISTRIBUTED TX WITH SHARDING
                taskResult = NON_LOCAL_CLUSTER;
            else {
                task.setLockRecords(false);
                task.checkRecordExists();
                taskResult = task.execute(requestId, iServer, iManager, database);
                reqContext.addUndoTask(task.getUndoTask(requestId));
            }
            result.results.add(taskResult);
        }
        database.commit();
        // SEND BACK CHANGED VALUE TO UPDATE
        for (int i = 0; i < result.results.size(); ++i) {
            final Object currentResult = result.results.get(i);
            if (currentResult == NON_LOCAL_CLUSTER)
                // SKIP IT
                continue;
            final OAbstractRecordReplicatedTask task = tasks.get(i);
            if (task instanceof OCreateRecordTask) {
                // SEND RID + VERSION
                final OCreateRecordTask t = (OCreateRecordTask) task;
                result.results.set(i, new OPlaceholder(t.getRecord()));
            } else if (task instanceof OUpdateRecordTask) {
                // SEND VERSION
                result.results.set(i, task.getRecord().getVersion());
            }
        }
        return result;
    } catch (Throwable e) {
        // if (e instanceof ODistributedRecordLockedException)
        // ddb.dumpLocks();
        ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "Rolling back transaction on local server db=%s (reqId=%s error=%s)...", database.getName(), requestId, e);
        database.rollback();
        // ddb.popTxContext(requestId);
        reqContext.unlock();
        if (!(e instanceof ONeedRetryException || e instanceof OTransactionException || e instanceof ORecordDuplicatedException || e instanceof ORecordNotFoundException))
            // DUMP ONLY GENERIC EXCEPTIONS
            ODistributedServerLog.info(this, getNodeSource(), null, DIRECTION.NONE, "Error on distributed transaction commit", e);
        return e;
    }
}
Also used : OPlaceholder(com.orientechnologies.orient.core.db.record.OPlaceholder) OAbstractRecordReplicatedTask(com.orientechnologies.orient.server.distributed.task.OAbstractRecordReplicatedTask) ArrayList(java.util.ArrayList) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) ORecordDuplicatedException(com.orientechnologies.orient.core.storage.ORecordDuplicatedException) OTransactionOptimistic(com.orientechnologies.orient.core.tx.OTransactionOptimistic) OTransactionException(com.orientechnologies.orient.core.exception.OTransactionException) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException)

Example 2 with OAbstractRecordReplicatedTask

use of com.orientechnologies.orient.server.distributed.task.OAbstractRecordReplicatedTask in project orientdb by orientechnologies.

the class OAbstract2pcTask method toString.

@Override
public String toString() {
    final StringBuilder buffer = new StringBuilder();
    buffer.append("tx[");
    buffer.append(tasks.size());
    buffer.append("]{");
    for (int i = 0; i < tasks.size(); ++i) {
        final OAbstractRecordReplicatedTask task = tasks.get(i);
        if (i > 0)
            buffer.append(',');
        buffer.append(task);
    }
    buffer.append("}");
    return buffer.toString();
}
Also used : OAbstractRecordReplicatedTask(com.orientechnologies.orient.server.distributed.task.OAbstractRecordReplicatedTask)

Example 3 with OAbstractRecordReplicatedTask

use of com.orientechnologies.orient.server.distributed.task.OAbstractRecordReplicatedTask in project orientdb by orientechnologies.

the class OAbstract2pcTask method getPartitionKey.

/**
   * Return the partition keys of all the sub-tasks.
   */
@Override
public int[] getPartitionKey() {
    if (tasks.size() == 1)
        // ONE TASK, USE THE INNER TASK'S PARTITION KEY
        return tasks.get(0).getPartitionKey();
    // MULTIPLE PARTITION
    final int[] partitions = new int[tasks.size()];
    for (int i = 0; i < tasks.size(); ++i) {
        final OAbstractRecordReplicatedTask task = tasks.get(i);
        partitions[i] = task.getPartitionKey()[0];
    }
    return partitions;
}
Also used : OAbstractRecordReplicatedTask(com.orientechnologies.orient.server.distributed.task.OAbstractRecordReplicatedTask)

Example 4 with OAbstractRecordReplicatedTask

use of com.orientechnologies.orient.server.distributed.task.OAbstractRecordReplicatedTask in project orientdb by orientechnologies.

the class OAbstract2pcTask method toStream.

@Override
public void toStream(final DataOutput out) throws IOException {
    out.writeInt(tasks.size());
    for (OAbstractRecordReplicatedTask task : tasks) {
        out.writeByte(task.getFactoryId());
        task.toStream(out);
    }
    if (lastLSN != null) {
        out.writeBoolean(true);
        lastLSN.toStream(out);
    } else
        out.writeBoolean(false);
}
Also used : OAbstractRecordReplicatedTask(com.orientechnologies.orient.server.distributed.task.OAbstractRecordReplicatedTask)

Example 5 with OAbstractRecordReplicatedTask

use of com.orientechnologies.orient.server.distributed.task.OAbstractRecordReplicatedTask in project orientdb by orientechnologies.

the class OAbstract2pcTask method getFixTask.

@Override
public ORemoteTask getFixTask(final ODistributedRequest iRequest, final ORemoteTask iOriginalTask, final Object iBadResponse, final Object iGoodResponse, final String executorNodeName, final ODistributedServerManager dManager) {
    if (!(iGoodResponse instanceof OTxTaskResult)) {
        // TODO: MANAGE ERROR ON LOCAL NODE
        ODistributedServerLog.debug(this, getNodeSource(), null, DIRECTION.NONE, "Error on creating fix-task for request: '%s' because good response is not expected type: %s", iRequest, iBadResponse);
        return null;
    }
    final OCompleted2pcTask fixTask = new OCompleted2pcTask(iRequest.getId(), false, null);
    for (int i = 0; i < tasks.size(); ++i) {
        final OAbstractRecordReplicatedTask t = tasks.get(i);
        final Object badResult = iBadResponse == null ? null : iBadResponse instanceof Throwable ? iBadResponse : ((OTxTaskResult) iBadResponse).results.get(i);
        final Object goodResult = ((OTxTaskResult) iGoodResponse).results.get(i);
        final ORemoteTask undoTask = t.getFixTask(iRequest, t, badResult, goodResult, executorNodeName, dManager);
        if (undoTask != null)
            fixTask.addFixTask(undoTask);
    }
    return fixTask;
}
Also used : OAbstractRecordReplicatedTask(com.orientechnologies.orient.server.distributed.task.OAbstractRecordReplicatedTask) ORemoteTask(com.orientechnologies.orient.server.distributed.task.ORemoteTask)

Aggregations

OAbstractRecordReplicatedTask (com.orientechnologies.orient.server.distributed.task.OAbstractRecordReplicatedTask)6 ORecordId (com.orientechnologies.orient.core.id.ORecordId)2 ONeedRetryException (com.orientechnologies.common.concur.ONeedRetryException)1 OPlaceholder (com.orientechnologies.orient.core.db.record.OPlaceholder)1 ORecordNotFoundException (com.orientechnologies.orient.core.exception.ORecordNotFoundException)1 OTransactionException (com.orientechnologies.orient.core.exception.OTransactionException)1 ORecordDuplicatedException (com.orientechnologies.orient.core.storage.ORecordDuplicatedException)1 OTransactionOptimistic (com.orientechnologies.orient.core.tx.OTransactionOptimistic)1 ORemoteTask (com.orientechnologies.orient.server.distributed.task.ORemoteTask)1 ArrayList (java.util.ArrayList)1