Search in sources :

Example 11 with ORecordOperation

use of com.orientechnologies.orient.core.db.record.ORecordOperation in project orientdb by orientechnologies.

the class OLiveQueryShotdownTest method testShutDown.

@Test
public void testShutDown() throws Exception {
    bootServer();
    ODatabaseDocument db = new ODatabaseDocumentTx("remote:localhost/" + OLiveQueryShotdownTest.class.getSimpleName());
    db.open("admin", "admin");
    db.getMetadata().getSchema().createClass("Test");
    final CountDownLatch error = new CountDownLatch(1);
    try {
        db.command(new OLiveQuery("live select from Test", new OLiveResultListener() {

            @Override
            public void onUnsubscribe(int iLiveToken) {
            }

            @Override
            public void onLiveResult(int iLiveToken, ORecordOperation iOp) throws OException {
            }

            @Override
            public void onError(int iLiveToken) {
                error.countDown();
            }
        })).execute();
        shutdownServer();
        assertTrue("onError method never called on shutdow", error.await(2, TimeUnit.SECONDS));
    } finally {
    //      db.close();
    }
}
Also used : ORecordOperation(com.orientechnologies.orient.core.db.record.ORecordOperation) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OLiveQuery(com.orientechnologies.orient.core.sql.query.OLiveQuery) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OException(com.orientechnologies.common.exception.OException) CountDownLatch(java.util.concurrent.CountDownLatch) OLiveResultListener(com.orientechnologies.orient.core.sql.query.OLiveResultListener) Test(org.junit.Test)

Example 12 with ORecordOperation

use of com.orientechnologies.orient.core.db.record.ORecordOperation in project orientdb by orientechnologies.

the class OLiveCommandResultListenerTest method testNetworkError.

@Test
public void testNetworkError() throws IOException {
    Mockito.when(channelBinary.writeInt(Mockito.anyInt())).thenThrow(new IOException("Mock Exception"));
    OLiveCommandResultListener listener = new OLiveCommandResultListener(server, connection, 20, new TestResultListener());
    OLiveQueryHook.subscribe(10, rawListener, db);
    assertTrue(OLiveQueryHook.getOpsReference(db).getQueueThread().hasToken(10));
    ORecordOperation op = new ORecordOperation(new ODocument(), ORecordOperation.CREATED);
    listener.onLiveResult(10, op);
    assertFalse(OLiveQueryHook.getOpsReference(db).getQueueThread().hasToken(10));
}
Also used : ORecordOperation(com.orientechnologies.orient.core.db.record.ORecordOperation) OLiveCommandResultListener(com.orientechnologies.orient.server.network.protocol.binary.OLiveCommandResultListener) IOException(java.io.IOException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test)

Example 13 with ORecordOperation

use of com.orientechnologies.orient.core.db.record.ORecordOperation in project orientdb by orientechnologies.

the class SQLLiveSelectTest method liveQueryTest.

@Test
public void liveQueryTest() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(6);
    final List<ORecordOperation> ops = Collections.synchronizedList(new ArrayList());
    OResultSet<ODocument> tokens = database.query(new OLiveQuery<Object>("live select from LiveClass", new OLiveResultListener() {

        @Override
        public void onLiveResult(int iLiveToken, ORecordOperation iOp) throws OException {
            ops.add(iOp);
            latch.countDown();
        }

        @Override
        public void onError(int iLiveToken) {
        }

        @Override
        public void onUnsubscribe(int iLiveToken) {
        }
    }));
    Assert.assertEquals(tokens.size(), 1);
    ODocument tokenDoc = tokens.get(0);
    Integer token = tokenDoc.field("token");
    Assert.assertNotNull(token);
    database.command(new OCommandSQL("insert into liveclass set name = 'foo', surname = 'bar'")).execute();
    database.command(new OCommandSQL("insert into liveclass set name = 'foo', surname = 'baz'")).execute();
    database.command(new OCommandSQL("insert into liveclass set name = 'foo'")).execute();
    database.command(new OCommandSQL("update liveclass set name = 'updated'")).execute();
    latch.await();
    Assert.assertEquals(ops.size(), 6);
    for (ORecordOperation doc : ops) {
        if (doc.type == ORecordOperation.CREATED) {
            Assert.assertEquals(((ODocument) doc.record).field("name"), "foo");
        } else if (doc.type == ORecordOperation.UPDATED) {
            Assert.assertEquals(((ODocument) doc.record).field("name"), "updated");
        } else {
            Assert.fail();
        }
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) ORecordOperation(com.orientechnologies.orient.core.db.record.ORecordOperation) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) OLiveResultListener(com.orientechnologies.orient.core.sql.query.OLiveResultListener) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 14 with ORecordOperation

use of com.orientechnologies.orient.core.db.record.ORecordOperation in project orientdb by orientechnologies.

the class ODistributedTransactionManager method getInvolvedClusters.

protected Set<String> getInvolvedClusters(final List<ORecordOperation> uResult) {
    final Set<String> involvedClusters = new HashSet<String>();
    for (ORecordOperation op : uResult) {
        final ORecord record = op.getRecord();
        involvedClusters.add(storage.getClusterNameByRID((ORecordId) record.getIdentity()));
    }
    return involvedClusters;
}
Also used : ORecordOperation(com.orientechnologies.orient.core.db.record.ORecordOperation) ORecord(com.orientechnologies.orient.core.record.ORecord) ORecordId(com.orientechnologies.orient.core.id.ORecordId)

Example 15 with ORecordOperation

use of com.orientechnologies.orient.core.db.record.ORecordOperation 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

ORecordOperation (com.orientechnologies.orient.core.db.record.ORecordOperation)27 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)13 ORecordId (com.orientechnologies.orient.core.id.ORecordId)11 ORecord (com.orientechnologies.orient.core.record.ORecord)11 OException (com.orientechnologies.common.exception.OException)7 IOException (java.io.IOException)7 OLiveResultListener (com.orientechnologies.orient.core.sql.query.OLiveResultListener)5 Test (org.testng.annotations.Test)5 OTransactionException (com.orientechnologies.orient.core.exception.OTransactionException)4 ORID (com.orientechnologies.orient.core.id.ORID)4 ONeedRetryException (com.orientechnologies.common.concur.ONeedRetryException)3 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)3 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)3 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)3 ORecordNotFoundException (com.orientechnologies.orient.core.exception.ORecordNotFoundException)3 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Test (org.junit.Test)3 ODatabaseException (com.orientechnologies.orient.core.exception.ODatabaseException)2 OStorageException (com.orientechnologies.orient.core.exception.OStorageException)2