Search in sources :

Example 96 with ORecordId

use of com.orientechnologies.orient.core.id.ORecordId in project orientdb by orientechnologies.

the class ONeOperatorTest method test.

@Test
public void test() {
    ONeOperator op = new ONeOperator(-1);
    Assert.assertTrue(op.execute(null, 1));
    Assert.assertTrue(op.execute(1, null));
    Assert.assertTrue(op.execute(null, null));
    Assert.assertFalse(op.execute(1, 1));
    Assert.assertTrue(op.execute(1, 0));
    Assert.assertTrue(op.execute(0, 1));
    Assert.assertTrue(op.execute("aaa", "zzz"));
    Assert.assertTrue(op.execute("zzz", "aaa"));
    Assert.assertFalse(op.execute("aaa", "aaa"));
    Assert.assertTrue(op.execute(1, 1.1));
    Assert.assertTrue(op.execute(1.1, 1));
    Assert.assertFalse(op.execute(BigDecimal.ONE, 1));
    Assert.assertFalse(op.execute(1, BigDecimal.ONE));
    Assert.assertTrue(op.execute(1.1, BigDecimal.ONE));
    Assert.assertTrue(op.execute(2, BigDecimal.ONE));
    Assert.assertTrue(op.execute(BigDecimal.ONE, 0.999999));
    Assert.assertTrue(op.execute(BigDecimal.ONE, 0));
    Assert.assertTrue(op.execute(BigDecimal.ONE, 2));
    Assert.assertTrue(op.execute(BigDecimal.ONE, 1.0001));
    Assert.assertFalse(op.execute(new ORecordId(1, 10), new ORecordId((short) 1, 10)));
    Assert.assertTrue(op.execute(new ORecordId(1, 10), new ORecordId((short) 1, 20)));
    Assert.assertTrue(op.execute(new Object(), new Object()));
}
Also used : ONeOperator(com.orientechnologies.orient.core.sql.parser.ONeOperator) ORecordId(com.orientechnologies.orient.core.id.ORecordId) Test(org.testng.annotations.Test)

Example 97 with ORecordId

use of com.orientechnologies.orient.core.id.ORecordId 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 98 with ORecordId

use of com.orientechnologies.orient.core.id.ORecordId 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());
    switch(recordStatus) {
        case REMOVED:
            // RECYCLE THE RID AND OVERWRITE IT WITH THE NEW CONTENT
            ODatabaseRecordThreadLocal.INSTANCE.get().getStorage().recyclePosition(rid, new byte[] {}, version, recordType);
            // CREATE A RECORD TO CALL ALL THE HOOKS (LIKE INDEXES FOR UNIQUE CONSTRAINTS)
            final ORecord loadedRecordInstance = Orient.instance().getRecordFactoryManager().newInstance(recordType);
            ORecordInternal.fill(loadedRecordInstance, rid, version, content, true);
            loadedRecordInstance.save();
            return new OPlaceholder(rid, loadedRecordInstance.getVersion());
        case ALLOCATED:
        case PRESENT:
            final OStorageOperationResult<ORawBuffer> loadedRecord = ODatabaseRecordThreadLocal.INSTANCE.get().getStorage().readRecord(rid, null, true, false, null);
            if (loadedRecord.getResult() != null) {
                // ALREADY PRESENT
                record = forceUpdate(iManager, database, requestId, loadedRecord.getResult());
                return new OPlaceholder(record);
            }
        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, "+-> assigned 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);
}
Also used : OPlaceholder(com.orientechnologies.orient.core.db.record.OPlaceholder) ORawBuffer(com.orientechnologies.orient.core.storage.ORawBuffer) OPaginatedCluster(com.orientechnologies.orient.core.storage.impl.local.paginated.OPaginatedCluster) ORecord(com.orientechnologies.orient.core.record.ORecord) ORecordId(com.orientechnologies.orient.core.id.ORecordId)

Example 99 with ORecordId

use of com.orientechnologies.orient.core.id.ORecordId 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)
        return null;
    final OPlaceholder badResult = (OPlaceholder) iBadResponse;
    final OPlaceholder goodResult = (OPlaceholder) iGoodResponse;
    ORemoteTask result = null;
    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 ODistributedResponse response = dManager.sendRequest(iRequest.getDatabaseName(), null, nodes, new OReadRecordTask(toUpdateRid), dManager.getNextMessageIdCounter(), ODistributedRequest.EXECUTION_MODE.RESPONSE, 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)
                    result = new OFixUpdateRecordTask(toUpdateRid, toUpdateRecord.toStream(), toUpdateRecord.getVersion(), ORecordInternal.getRecordType(toUpdateRecord));
            }
            // CREATE LAST RECORD
            result = new OCreateRecordTask((ORecordId) goodResult.getIdentity(), content, version, recordType);
        } else if (badResult.getIdentity().getClusterId() == goodResult.getIdentity().getClusterId() && badResult.getIdentity().getClusterPosition() > goodResult.getIdentity().getClusterPosition()) {
        } else
            // ANY OTHER CASE JUST DELETE IT
            result = new OFixCreateRecordTask(new ORecordId(badResult.getIdentity()), badResult.getVersion());
    }
    return result;
}
Also used : OPlaceholder(com.orientechnologies.orient.core.db.record.OPlaceholder) ORemoteTask(com.orientechnologies.orient.server.distributed.task.ORemoteTask) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ORawBuffer(com.orientechnologies.orient.core.storage.ORawBuffer) ORecord(com.orientechnologies.orient.core.record.ORecord)

Example 100 with ORecordId

use of com.orientechnologies.orient.core.id.ORecordId in project orientdb by orientechnologies.

the class LocalPaginatedStorageUpdateCrashRestoreIT method compareDocuments.

private void compareDocuments(long lastTs) {
    long minTs = Long.MAX_VALUE;
    baseDocumentTx.activateOnCurrentThread();
    int clusterId = baseDocumentTx.getClusterIdByName("TestClass");
    OStorage baseStorage = baseDocumentTx.getStorage();
    OPhysicalPosition[] physicalPositions = baseStorage.ceilingPhysicalPositions(clusterId, new OPhysicalPosition(0));
    int recordsRestored = 0;
    int recordsTested = 0;
    while (physicalPositions.length > 0) {
        final ORecordId rid = new ORecordId(clusterId);
        for (OPhysicalPosition physicalPosition : physicalPositions) {
            rid.setClusterPosition(physicalPosition.clusterPosition);
            baseDocumentTx.activateOnCurrentThread();
            ODocument baseDocument = baseDocumentTx.load(rid);
            testDocumentTx.activateOnCurrentThread();
            List<ODocument> testDocuments = testDocumentTx.query(new OSQLSynchQuery<ODocument>("select from TestClass where id  = " + baseDocument.field("id")));
            Assert.assertTrue(!testDocuments.isEmpty());
            ODocument testDocument = testDocuments.get(0);
            if (testDocument.field("timestamp").equals(baseDocument.field("timestamp")) && testDocument.field("stringValue").equals(baseDocument.field("stringValue"))) {
                recordsRestored++;
            } else {
                if (((Long) baseDocument.field("timestamp")) < minTs)
                    minTs = baseDocument.field("timestamp");
            }
            recordsTested++;
            if (recordsTested % 10000 == 0)
                System.out.println(recordsTested + " were tested, " + recordsRestored + " were restored ...");
        }
        physicalPositions = baseStorage.higherPhysicalPositions(clusterId, physicalPositions[physicalPositions.length - 1]);
    }
    System.out.println(recordsRestored + " records were restored. Total records " + recordsTested + ". lost records " + (recordsTested - recordsRestored));
    long maxInterval = minTs == Long.MAX_VALUE ? 0 : lastTs - minTs;
    System.out.println("Lost records max interval (ms) : " + maxInterval);
    assertThat(recordsTested - recordsRestored).isLessThan(120);
    assertThat(maxInterval).isLessThan(2000);
}
Also used : OStorage(com.orientechnologies.orient.core.storage.OStorage) OPhysicalPosition(com.orientechnologies.orient.core.storage.OPhysicalPosition) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

ORecordId (com.orientechnologies.orient.core.id.ORecordId)431 Test (org.testng.annotations.Test)153 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)139 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)120 ORID (com.orientechnologies.orient.core.id.ORID)71 HashSet (java.util.HashSet)63 OIndexCursor (com.orientechnologies.orient.core.index.OIndexCursor)42 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)37 ORecord (com.orientechnologies.orient.core.record.ORecord)37 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)34 OIndexTxAwareMultiValue (com.orientechnologies.orient.core.index.OIndexTxAwareMultiValue)30 OIndexTxAwareOneValue (com.orientechnologies.orient.core.index.OIndexTxAwareOneValue)30 HashMap (java.util.HashMap)29 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)28 IOException (java.io.IOException)25 Child (com.orientechnologies.orient.test.domain.business.Child)24 OException (com.orientechnologies.common.exception.OException)23 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)23 Map (java.util.Map)22 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)21