use of com.orientechnologies.orient.core.db.record.OPlaceholder in project orientdb by orientechnologies.
the class OUpdateRecordTask method executeRecordTask.
@Override
public Object executeRecordTask(ODistributedRequestId requestId, final OServer iServer, ODistributedServerManager iManager, final ODatabaseDocumentInternal database) throws Exception {
if (ODistributedServerLog.isDebugEnabled())
ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "Updating record %s/%s v.%d reqId=%s...", database.getName(), rid.toString(), version, requestId);
prepareUndoOperation();
if (previousRecord == null) {
// RESURRECT/CREATE IT
final OPlaceholder ph = (OPlaceholder) new OCreateRecordTask().init(rid, content, version, recordType).executeRecordTask(requestId, iServer, iManager, database);
record = ph.getRecord();
if (record == null)
ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "+-> Error on updating record %s", rid);
} else {
// UPDATE IT
final int loadedVersion = previousRecord.getVersion();
if (loadedVersion == version + 1 && Arrays.equals(content, previousRecordContent)) {
// OPERATION ALREADY EXECUTED
record = previousRecord;
} else {
final ORecord loadedRecord = previousRecord.copy();
if (loadedRecord instanceof ODocument) {
// APPLY CHANGES FIELD BY FIELD TO MARK DIRTY FIELDS FOR INDEXES/HOOKS
final ODocument newDocument = (ODocument) getRecord();
final ODocument loadedDocument = (ODocument) loadedRecord;
loadedDocument.merge(newDocument, false, false).getVersion();
ORecordInternal.setVersion(loadedDocument, version);
} else
ORecordInternal.fill(loadedRecord, rid, version, content, true);
loadedRecord.setDirty();
record = database.save(loadedRecord);
if (record == null)
ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "+-> Error on updating record %s", rid);
if (version < 0 && ODistributedServerLog.isDebugEnabled())
ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "+-> Reverted %s from version %d to %d", rid, loadedVersion, record.getVersion());
}
}
if (record == null)
throw new ODistributedException("Cannot update record " + rid);
if (ODistributedServerLog.isDebugEnabled())
ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "+-> updated record %s/%s v.%d [%s]", database.getName(), rid.toString(), record.getVersion(), record);
return record.getVersion();
}
use of com.orientechnologies.orient.core.db.record.OPlaceholder in project orientdb by orientechnologies.
the class OCreateRecordTask method getFixTask.
@Override
public ORemoteTask getFixTask(final ODistributedRequest iRequest, ORemoteTask iOriginalTask, final Object iBadResponse, final Object iGoodResponse, final String executorNode, final ODistributedServerManager dManager) {
if (iBadResponse == null || (iBadResponse instanceof Throwable && !(iBadResponse instanceof ONeedRetryException)))
return null;
ORemoteTask result = null;
final OPlaceholder goodResult = (OPlaceholder) iGoodResponse;
if (iBadResponse instanceof ONeedRetryException)
return ((OCreateRecordTask) dManager.getTaskFactoryManager().getFactoryByServerName(executorNode).createTask(OCreateRecordTask.FACTORYID)).init((ORecordId) goodResult.getIdentity(), content, ORecordVersionHelper.setRollbackMode(version), recordType);
final OPlaceholder badResult = (OPlaceholder) iBadResponse;
if (!badResult.equals(goodResult)) {
// CREATE RECORD FAILED TO HAVE THE SAME RIDS. FORCE REALIGNING OF DATA CLUSTERS
if (badResult.getIdentity().getClusterId() == goodResult.getIdentity().getClusterId() && badResult.getIdentity().getClusterPosition() < goodResult.getIdentity().getClusterPosition()) {
final long minPos = Math.max(badResult.getIdentity().getClusterPosition() - 1, 0);
for (long pos = minPos; pos < goodResult.getIdentity().getClusterPosition(); ++pos) {
// UPDATE INTERMEDIATE RECORDS
final ORecordId toUpdateRid = new ORecordId(goodResult.getIdentity().getClusterId(), pos);
final ORecord toUpdateRecord;
if (dManager.getLocalNodeName().equals(executorNode)) {
// SAME SERVER: LOAD THE RECORD FROM ANOTHER NODE
final ODistributedConfiguration dCfg = dManager.getDatabaseConfiguration(iRequest.getDatabaseName());
final List<String> nodes = dCfg.getServers(ODatabaseRecordThreadLocal.INSTANCE.get().getClusterNameById(clusterId), dManager.getLocalNodeName());
final OReadRecordTask task = (OReadRecordTask) dManager.getTaskFactoryManager().getFactoryByServerNames(nodes).createTask(OReadRecordTask.FACTORYID);
task.init(toUpdateRid);
final ODistributedResponse response = dManager.sendRequest(iRequest.getDatabaseName(), null, nodes, task, dManager.getNextMessageIdCounter(), ODistributedRequest.EXECUTION_MODE.RESPONSE, null, null, null);
final ORawBuffer remoteReadRecord = (ORawBuffer) response.getPayload();
if (remoteReadRecord != null) {
toUpdateRecord = Orient.instance().getRecordFactoryManager().newInstance(recordType);
ORecordInternal.fill(toUpdateRecord, toUpdateRid, remoteReadRecord.version, remoteReadRecord.buffer, false);
} else
toUpdateRecord = null;
} else
// LOAD IT LOCALLY
toUpdateRecord = toUpdateRid.getRecord();
if (toUpdateRecord != null)
try {
new OFixUpdateRecordTask().init(toUpdateRid, toUpdateRecord.toStream(), toUpdateRecord.getVersion(), ORecordInternal.getRecordType(toUpdateRecord)).execute(iRequest.getId(), dManager.getServerInstance(), dManager, ODatabaseRecordThreadLocal.INSTANCE.get());
} catch (Exception e) {
throw OException.wrapException(new ODistributedOperationException("Cannot create record " + rid + " because assigned RID is different"), e);
}
}
// CREATE LAST RECORD
final OCreateRecordTask task = (OCreateRecordTask) dManager.getTaskFactoryManager().getFactoryByServerName(executorNode).createTask(OCreateRecordTask.FACTORYID);
task.init((ORecordId) goodResult.getIdentity(), content, version, recordType);
result = task;
} else if (badResult.getIdentity().getClusterId() == goodResult.getIdentity().getClusterId() && badResult.getIdentity().getClusterPosition() > goodResult.getIdentity().getClusterPosition()) {
} else {
// ANY OTHER CASE JUST DELETE IT
final OFixCreateRecordTask task = (OFixCreateRecordTask) dManager.getTaskFactoryManager().getFactoryByServerName(executorNode).createTask(OFixCreateRecordTask.FACTORYID);
task.init(new ORecordId(badResult.getIdentity()), badResult.getVersion());
result = task;
}
}
return result;
}
use of com.orientechnologies.orient.core.db.record.OPlaceholder in project orientdb by orientechnologies.
the class OCreateRecordTask method executeRecordTask.
@Override
public Object executeRecordTask(final ODistributedRequestId requestId, final OServer iServer, final ODistributedServerManager iManager, final ODatabaseDocumentInternal database) throws Exception {
if (ODistributedServerLog.isDebugEnabled())
ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "Creating record %s/%s v.%d reqId=%s...", database.getName(), rid.toString(), version, requestId);
if (!rid.isPersistent())
throw new ODistributedException("Record " + rid + " has not been saved on owner node first (temporary rid)");
final OPaginatedCluster cluster = (OPaginatedCluster) ODatabaseRecordThreadLocal.INSTANCE.get().getStorage().getClusterById(rid.getClusterId());
final OPaginatedCluster.RECORD_STATUS recordStatus = cluster.getRecordStatus(rid.getClusterPosition());
if (ODistributedServerLog.isDebugEnabled())
ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "Found record %s/%s status=%s reqId=%s...", database.getName(), rid.toString(), recordStatus, requestId);
switch(recordStatus) {
case REMOVED:
{
// RECYCLE THE RID AND OVERWRITE IT WITH THE NEW CONTENT
getRecord();
ODatabaseRecordThreadLocal.INSTANCE.get().recycle(record);
}
case ALLOCATED:
getRecord();
// FORCE CREATION
if (record.getVersion() < 0)
// INCREMENT THE VERSION IN CASE OF ROLLBACK
ORecordInternal.setVersion(record, record.getVersion() + 1);
record.save();
break;
case PRESENT:
{
getRecord();
record.save();
break;
}
case NOT_EXISTENT:
{
// try {
ORecordId newRid;
do {
getRecord();
if (clusterId > -1)
record.save(database.getClusterNameById(clusterId), true);
else if (rid.getClusterId() != -1)
record.save(database.getClusterNameById(rid.getClusterId()), true);
else
record.save();
newRid = (ORecordId) record.getIdentity();
if (newRid.getClusterPosition() >= rid.getClusterPosition())
break;
// CREATE AN HOLE
record.delete();
record = null;
} while (newRid.getClusterPosition() < rid.getClusterPosition());
if (!rid.equals(newRid)) {
ODistributedServerLog.warn(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "Record %s has been saved with the RID %s instead of the expected %s reqId=%s", record, newRid, rid, requestId);
// DELETE THE INVALID RECORD FIRST
record.delete();
throw new ODistributedException("Record " + rid + " has been saved with the different RID " + newRid + " on server " + iManager.getLocalNodeName());
}
ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "+-> assigning new rid %s/%s v.%d reqId=%s", database.getName(), rid.toString(), record.getVersion(), requestId);
}
}
// IMPROVED TRANSPORT BY AVOIDING THE RECORD CONTENT, BUT JUST RID + VERSION
return new OPlaceholder(record);
}
use of com.orientechnologies.orient.core.db.record.OPlaceholder 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. IT WILL BE CLOSED BY 2PC MESSAGE
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(iManager, requestId, OMultiValue.getSingletonList(iManager.getLocalNodeName())));
}
result.results.add(taskResult);
}
database.commit();
// LOCK NEW RECORDS BECAUSE ONLY AFTER COMMIT THE RID IS AVAILABLE
for (int i = 0; i < tasks.size(); ++i) {
final OAbstractRecordReplicatedTask task = tasks.get(i);
if (task instanceof OCreateRecordTask)
// LOCK THE NEW CREATED RECORD RIGHT AFTER HAVING THE RID
reqContext.lock(((OPlaceholder) result.results.get(i)).getIdentity(), getRecordLock());
}
} 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();
// KEEP THE LOCKS WAITING FOR THE FINAL MSG FROM THE COORDINATOR
// // REMOVE THE CONTEXT
// ddb.popTxContext(requestId);
// reqContext.destroy();
// ALREADY ROLLED BACK (ON STORAGE), REMOVE UNDO TASKS
reqContext.clearUndo();
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;
}
// 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;
}
use of com.orientechnologies.orient.core.db.record.OPlaceholder in project orientdb by orientechnologies.
the class ODistributedTransactionManager method processCommitResult.
protected boolean processCommitResult(final String localNodeName, final OTransaction iTx, final OTxTask txTask, final Set<String> involvedClusters, final Iterable<ORecordOperation> tmpEntries, final Collection<String> nodes, final int autoRetryDelay, final ODistributedRequestId reqId, final ODistributedResponse dResponse, final boolean isLastRetry) throws InterruptedException {
final Object result = dResponse.getPayload();
if (result instanceof OTxTaskResult) {
final OTxTaskResult txResult = (OTxTaskResult) result;
final List<Object> list = txResult.results;
for (int i = 0; i < txTask.getTasks().size(); ++i) {
final Object o = list.get(i);
final OAbstractRecordReplicatedTask task = txTask.getTasks().get(i);
if (task instanceof OCreateRecordTask) {
final OCreateRecordTask t = (OCreateRecordTask) task;
iTx.updateIdentityAfterCommit(t.getRid(), ((OPlaceholder) o).getIdentity());
final ORecord rec = iTx.getRecord(t.getRid());
if (rec != null)
ORecordInternal.setVersion(rec, ((OPlaceholder) o).getVersion());
} else if (task instanceof OUpdateRecordTask) {
final OUpdateRecordTask t = (OUpdateRecordTask) task;
ORecordInternal.setVersion(iTx.getRecord(t.getRid()), (Integer) o);
} else if (task instanceof ODeleteRecordTask) {
}
}
// RESET DIRTY FLAGS TO AVOID CALLING AUTO-SAVE
for (ORecordOperation op : tmpEntries) {
final ORecord record = op.getRecord();
if (record != null)
ORecordInternal.unsetDirty(record);
}
if (ODistributedServerLog.isDebugEnabled())
ODistributedServerLog.debug(this, localNodeName, null, ODistributedServerLog.DIRECTION.NONE, "Distributed transaction %s completed", reqId);
sendTxCompleted(localNodeName, involvedClusters, nodes, reqId, true, txTask.getPartitionKey());
} else if (result instanceof ODistributedRecordLockedException) {
if (ODistributedServerLog.isDebugEnabled())
ODistributedServerLog.debug(this, localNodeName, nodes.toString(), ODistributedServerLog.DIRECTION.IN, "Distributed transaction %s error: record %s is locked by %s", reqId, ((ODistributedRecordLockedException) result).getRid(), ((ODistributedRecordLockedException) result).getLockHolder());
// if this is the last retry (and it failed), we don't need to wait anymore
if (autoRetryDelay > 0 && !isLastRetry)
Thread.sleep(autoRetryDelay / 2 + new Random().nextInt(autoRetryDelay));
// RETRY
return false;
} else if (result instanceof Exception) {
// EXCEPTION: LOG IT AND ADD AS NESTED EXCEPTION
if (ODistributedServerLog.isDebugEnabled())
ODistributedServerLog.debug(this, localNodeName, nodes.toString(), ODistributedServerLog.DIRECTION.IN, "Distributed transaction %s received error: %s", reqId, result, result.toString());
// LET TO THE CALLER TO UNDO IT
if (result instanceof OTransactionException || result instanceof ONeedRetryException)
throw (RuntimeException) result;
if (result instanceof ORecordNotFoundException) {
// REPAIR THE RECORD IMMEDIATELY
localDistributedDatabase.getDatabaseRepairer().repairRecord((ORecordId) ((ORecordNotFoundException) result).getRid());
// RETRY
return false;
}
throw OException.wrapException(new OTransactionException("Error on committing distributed transaction"), (Exception) result);
} else {
// UNKNOWN RESPONSE TYPE
if (ODistributedServerLog.isDebugEnabled())
ODistributedServerLog.info(this, localNodeName, nodes.toString(), ODistributedServerLog.DIRECTION.IN, "Distributed transaction %s error, received unknown response type: %s", reqId, result);
// ROLLBACK TX
storage.executeUndoOnLocalServer(dResponse.getRequestId(), txTask);
sendTxCompleted(localNodeName, involvedClusters, nodes, dResponse.getRequestId(), false, txTask.getPartitionKey());
throw new OTransactionException("Error on committing distributed transaction, received unknown response type " + result);
}
return true;
}
Aggregations