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;
}
}
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();
}
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;
}
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);
}
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;
}
Aggregations