Search in sources :

Example 6 with OChannelBinaryAsynchClient

use of com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient in project orientdb by orientechnologies.

the class OStorageRemote method updateRecord.

public OStorageOperationResult<Integer> updateRecord(final ORecordId iRid, final boolean updateContent, final byte[] iContent, final int iVersion, final byte iRecordType, final int iMode, final ORecordCallback<Integer> iCallback) {
    final OSBTreeCollectionManager collectionManager = ODatabaseRecordThreadLocal.INSTANCE.get().getSbTreeCollectionManager();
    Integer resVersion = asyncNetworkOperation(new OStorageRemoteOperationWrite() {

        @Override
        public void execute(final OChannelBinaryAsynchClient network, final OStorageRemoteSession session, int mode) throws IOException {
            try {
                beginRequest(network, OChannelBinaryProtocol.REQUEST_RECORD_UPDATE, session);
                network.writeRID(iRid);
                network.writeBoolean(updateContent);
                network.writeBytes(iContent);
                network.writeVersion(iVersion);
                network.writeByte(iRecordType);
                network.writeByte((byte) mode);
            } finally {
                endRequest(network);
            }
        }
    }, new OStorageRemoteOperationRead<Integer>() {

        @Override
        public Integer execute(OChannelBinaryAsynchClient network, OStorageRemoteSession session) throws IOException {
            try {
                beginResponse(network, session);
                final Integer r = network.readVersion();
                final Map<OBonsaiCollectionPointer, OPair<Long, Long>> collectionChanges = readCollectionChanges(network);
                updateCollection(collectionChanges, collectionManager);
                return r;
            } finally {
                endResponse(network);
            }
        }
    }, iMode, iRid, iCallback, "Error on update record " + iRid);
    if (resVersion == null)
        // Returning given version in case of no answer from server
        resVersion = iVersion;
    return new OStorageOperationResult<Integer>(resVersion);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OIOException(com.orientechnologies.common.io.OIOException) IOException(java.io.IOException) OChannelBinaryAsynchClient(com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient) OSBTreeCollectionManager(com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeCollectionManager)

Example 7 with OChannelBinaryAsynchClient

use of com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient in project orientdb by orientechnologies.

the class OStorageRemote method command.

/**
   * Execute the command remotely and get the results back.
   */
public Object command(final OCommandRequestText iCommand) {
    if (!(iCommand instanceof OSerializableStream))
        throw new OCommandExecutionException("Cannot serialize the command to be executed to the server side.");
    final boolean live = iCommand instanceof OLiveQuery;
    final ODatabaseDocument database = ODatabaseRecordThreadLocal.INSTANCE.get();
    return networkOperation(new OStorageRemoteOperation<Object>() {

        @Override
        public Object execute(final OChannelBinaryAsynchClient network, OStorageRemoteSession session) throws IOException {
            Object result = null;
            session.commandExecuting = true;
            try {
                final boolean asynch = iCommand instanceof OCommandRequestAsynch && ((OCommandRequestAsynch) iCommand).isAsynchronous();
                try {
                    beginRequest(network, OChannelBinaryProtocol.REQUEST_COMMAND, session);
                    if (live) {
                        network.writeByte((byte) 'l');
                    } else {
                        // ASYNC / SYNC
                        network.writeByte((byte) (asynch ? 'a' : 's'));
                    }
                    network.writeBytes(OStreamSerializerAnyStreamable.INSTANCE.toStream(iCommand));
                } finally {
                    endRequest(network);
                }
                try {
                    beginResponse(network, session);
                    // Collection of prefetched temporary record (nested projection record), to refer for avoid garbage collection.
                    List<ORecord> temporaryResults = new ArrayList<ORecord>();
                    boolean addNextRecord = true;
                    if (asynch) {
                        byte status;
                        // ASYNCH: READ ONE RECORD AT TIME
                        while ((status = network.readByte()) > 0) {
                            final ORecord record = (ORecord) OChannelBinaryProtocol.readIdentifiable(network);
                            if (record == null)
                                continue;
                            switch(status) {
                                case 1:
                                    // PUT AS PART OF THE RESULT SET. INVOKE THE LISTENER
                                    if (addNextRecord) {
                                        addNextRecord = iCommand.getResultListener().result(record);
                                        database.getLocalCache().updateRecord(record);
                                    }
                                    break;
                                case 2:
                                    if (record.getIdentity().getClusterId() == -2)
                                        temporaryResults.add(record);
                                    // PUT IN THE CLIENT LOCAL CACHE
                                    database.getLocalCache().updateRecord(record);
                            }
                        }
                    } else {
                        result = readSynchResult(network, database, temporaryResults);
                        if (live) {
                            final ODocument doc = ((List<ODocument>) result).get(0);
                            final Integer token = doc.field("token");
                            final Boolean unsubscribe = doc.field("unsubscribe");
                            if (token != null) {
                                if (Boolean.TRUE.equals(unsubscribe)) {
                                    if (OStorageRemote.this.asynchEventListener != null)
                                        OStorageRemote.this.asynchEventListener.unregisterLiveListener(token);
                                } else {
                                    final OLiveResultListener listener = (OLiveResultListener) iCommand.getResultListener();
                                    ODatabaseDocumentInternal current = ODatabaseRecordThreadLocal.INSTANCE.get();
                                    final ODatabaseDocument dbCopy = current.copy();
                                    ORemoteConnectionPool pool = OStorageRemote.this.connectionManager.getPool(network.getServerURL());
                                    OStorageRemote.this.asynchEventListener.registerLiveListener(pool, token, new OLiveResultListener() {

                                        @Override
                                        public void onUnsubscribe(int iLiveToken) {
                                            listener.onUnsubscribe(iLiveToken);
                                            dbCopy.close();
                                        }

                                        @Override
                                        public void onLiveResult(int iLiveToken, ORecordOperation iOp) throws OException {
                                            dbCopy.activateOnCurrentThread();
                                            listener.onLiveResult(iLiveToken, iOp);
                                        }

                                        @Override
                                        public void onError(int iLiveToken) {
                                            listener.onError(iLiveToken);
                                            dbCopy.close();
                                        }
                                    });
                                }
                            } else {
                                throw new OStorageException("Cannot execute live query, returned null token");
                            }
                        }
                    }
                    if (!temporaryResults.isEmpty()) {
                        if (result instanceof OBasicResultSet<?>) {
                            ((OBasicResultSet<?>) result).setTemporaryRecordCache(temporaryResults);
                        }
                    }
                    return result;
                } finally {
                    endResponse(network);
                }
            } finally {
                session.commandExecuting = false;
                if (iCommand.getResultListener() != null && !live)
                    iCommand.getResultListener().end();
            }
        }
    }, "Error on executing command: " + iCommand);
}
Also used : OIOException(com.orientechnologies.common.io.OIOException) IOException(java.io.IOException) OChannelBinaryAsynchClient(com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient) OCommandRequestAsynch(com.orientechnologies.orient.core.command.OCommandRequestAsynch) OLiveResultListener(com.orientechnologies.orient.core.sql.query.OLiveResultListener) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ORecordOperation(com.orientechnologies.orient.core.db.record.ORecordOperation) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OLiveQuery(com.orientechnologies.orient.core.sql.query.OLiveQuery) ORecord(com.orientechnologies.orient.core.record.ORecord) OSerializableStream(com.orientechnologies.orient.core.serialization.OSerializableStream) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 8 with OChannelBinaryAsynchClient

use of com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient in project orientdb by orientechnologies.

the class ORemoteConnectionPushListenerTest method testConnectionPoolListenerPropagate.

@Test
public void testConnectionPoolListenerPropagate() {
    OChannelBinaryAsynchClient chann = Mockito.mock(OChannelBinaryAsynchClient.class);
    OStorageRemoteAsynchEventListener listener = Mockito.mock(OStorageRemoteAsynchEventListener.class);
    ORemoteConnectionPool pool = Mockito.mock(ORemoteConnectionPool.class);
    ORemoteConnectionPushListener poolListener = new ORemoteConnectionPushListener();
    poolListener.addListener(pool, chann, listener);
    poolListener.onRequest((byte) 10, null);
    Mockito.verify(listener, VerificationModeFactory.only()).onRequest(Mockito.anyByte(), Mockito.anyObject());
}
Also used : OChannelBinaryAsynchClient(com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient) Test(org.testng.annotations.Test)

Example 9 with OChannelBinaryAsynchClient

use of com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient in project orientdb by orientechnologies.

the class OStorageRemoteAsyncOperationTest method testSyncCall.

@Test
public void testSyncCall() {
    final CallStatus status = new CallStatus();
    storage.asyncNetworkOperation(new OStorageRemoteOperationWrite() {

        @Override
        public void execute(OChannelBinaryAsynchClient network, OStorageRemoteSession session, int mode) throws IOException {
            assertNull(status.status);
            status.status = "write";
        }
    }, new OStorageRemoteOperationRead<Object>() {

        @Override
        public Object execute(OChannelBinaryAsynchClient network, OStorageRemoteSession session) throws IOException {
            assertEquals(status.status, "write");
            status.status = "read";
            return null;
        }
    }, 0, new ORecordId(-1, -1), null, "");
    assertEquals(status.status, "read");
}
Also used : IOException(java.io.IOException) OChannelBinaryAsynchClient(com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient) ORecordId(com.orientechnologies.orient.core.id.ORecordId) Test(org.junit.Test)

Example 10 with OChannelBinaryAsynchClient

use of com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient in project orientdb by orientechnologies.

the class OStorageRemoteAsyncOperationTest method testAsyncRead.

@Test
public void testAsyncRead() throws InterruptedException {
    final CallStatus status = new CallStatus();
    final CountDownLatch callBackWait = new CountDownLatch(1);
    final CountDownLatch readDone = new CountDownLatch(1);
    final CountDownLatch callBackDone = new CountDownLatch(1);
    final Object res = new Object();
    storage.asyncNetworkOperation(new OStorageRemoteOperationWrite() {

        @Override
        public void execute(OChannelBinaryAsynchClient network, OStorageRemoteSession session, int mode) throws IOException {
            assertNull(status.status);
            status.status = "write";
        }
    }, new OStorageRemoteOperationRead<Object>() {

        @Override
        public Object execute(OChannelBinaryAsynchClient network, OStorageRemoteSession session) throws IOException {
            try {
                if (callBackWait.await(10, TimeUnit.MILLISECONDS))
                    readDone.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return res;
        }
    }, 1, new ORecordId(-1, -1), new ORecordCallback<Object>() {

        @Override
        public void call(ORecordId iRID, Object iParameter) {
            callBackDone.countDown();
        }
    }, "");
    // SBLCK THE CALLBAC THAT SHOULD BE IN ANOTHER THREAD
    callBackWait.countDown();
    boolean called = readDone.await(10, TimeUnit.MILLISECONDS);
    if (!called)
        fail("Read not called");
    called = callBackDone.await(10, TimeUnit.MILLISECONDS);
    if (!called)
        fail("Callback not called");
}
Also used : IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) OChannelBinaryAsynchClient(com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient) ORecordId(com.orientechnologies.orient.core.id.ORecordId) Test(org.junit.Test)

Aggregations

OChannelBinaryAsynchClient (com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient)21 IOException (java.io.IOException)12 OIOException (com.orientechnologies.common.io.OIOException)9 OModificationOperationProhibitedException (com.orientechnologies.common.concur.lock.OModificationOperationProhibitedException)6 OException (com.orientechnologies.common.exception.OException)6 OOfflineNodeException (com.orientechnologies.common.concur.OOfflineNodeException)4 OInterruptedException (com.orientechnologies.common.concur.lock.OInterruptedException)4 ORecordId (com.orientechnologies.orient.core.id.ORecordId)4 OTokenException (com.orientechnologies.orient.core.metadata.security.OTokenException)4 ODistributedRedirectException (com.orientechnologies.orient.enterprise.channel.binary.ODistributedRedirectException)4 OTokenSecurityException (com.orientechnologies.orient.enterprise.channel.binary.OTokenSecurityException)4 NamingException (javax.naming.NamingException)4 Test (org.testng.annotations.Test)4 Test (org.junit.Test)3 OSBTreeCollectionManager (com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeCollectionManager)2 OChannelListener (com.orientechnologies.orient.enterprise.channel.binary.OChannelListener)2 HashSet (java.util.HashSet)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 OCommandRequestAsynch (com.orientechnologies.orient.core.command.OCommandRequestAsynch)1 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)1