Search in sources :

Example 1 with OChannelBinary

use of com.orientechnologies.orient.enterprise.channel.binary.OChannelBinary in project orientdb by orientechnologies.

the class OClientConnectionManager method pushDistribCfg2Clients.

/**
   * Pushes the distributed configuration to all the connected clients.
   */
public void pushDistribCfg2Clients(final ODocument iConfig) {
    if (iConfig == null)
        return;
    final Set<String> pushed = new HashSet<String>();
    for (OClientConnection c : connections.values()) {
        if (!c.getData().supportsPushMessages)
            continue;
        try {
            final String remoteAddress = c.getRemoteAddress();
            if (pushed.contains(remoteAddress))
                // ALREADY SENT: JUMP IT
                continue;
        } catch (Exception e) {
            // SOCKET EXCEPTION SKIP IT
            continue;
        }
        if (!(c.getProtocol() instanceof ONetworkProtocolBinary) || c.getData().serializationImpl == null)
            // INVOLVE ONLY BINARY PROTOCOLS
            continue;
        final ONetworkProtocolBinary p = (ONetworkProtocolBinary) c.getProtocol();
        final OChannelBinary channel = p.getChannel();
        final ORecordSerializer ser = ORecordSerializerFactory.instance().getFormat(c.getData().serializationImpl);
        if (ser == null)
            return;
        final byte[] content = ser.toStream(iConfig, false);
        try {
            // TRY ACQUIRING THE LOCK FOR MAXIMUM 3 SECS TO AVOID TO FREEZE CURRENT THREAD
            if (channel.tryAcquireWriteLock(TIMEOUT_PUSH)) {
                try {
                    channel.writeByte(OChannelBinaryProtocol.PUSH_DATA);
                    channel.writeInt(Integer.MIN_VALUE);
                    channel.writeByte(OChannelBinaryProtocol.REQUEST_PUSH_DISTRIB_CONFIG);
                    channel.writeBytes(content);
                    channel.flush();
                    pushed.add(c.getRemoteAddress());
                    OLogManager.instance().debug(this, "Sent updated cluster configuration to the remote client %s", c.getRemoteAddress());
                } finally {
                    channel.releaseWriteLock();
                }
            } else {
                OLogManager.instance().info(this, "Timeout on sending updated cluster configuration to the remote client %s", c.getRemoteAddress());
            }
        } catch (Exception e) {
            OLogManager.instance().warn(this, "Cannot push cluster configuration to the client %s", e, c.getRemoteAddress());
        }
    }
}
Also used : OChannelBinary(com.orientechnologies.orient.enterprise.channel.binary.OChannelBinary) ONetworkProtocolBinary(com.orientechnologies.orient.server.network.protocol.binary.ONetworkProtocolBinary) ORecordSerializer(com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer) OException(com.orientechnologies.common.exception.OException) OTokenSecurityException(com.orientechnologies.orient.enterprise.channel.binary.OTokenSecurityException) IOException(java.io.IOException)

Example 2 with OChannelBinary

use of com.orientechnologies.orient.enterprise.channel.binary.OChannelBinary in project orientdb by orientechnologies.

the class OLiveCommandResultListener method onLiveResult.

public void onLiveResult(int iToken, ORecordOperation iOp) throws OException {
    boolean sendFail = true;
    do {
        List<OClientConnection> connections = session.getConnections();
        if (connections.size() == 0) {
            try {
                ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.INSTANCE.get();
                OLogManager.instance().warn(this, "Unsubscribing live query for connection " + connection);
                OLiveQueryHook.unsubscribe(iToken, db);
            } catch (Exception e) {
                OLogManager.instance().warn(this, "Unsubscribing live query for connection " + connection, e);
            }
            break;
        }
        OClientConnection curConnection = connections.get(0);
        ONetworkProtocolBinary protocol = (ONetworkProtocolBinary) curConnection.getProtocol();
        OChannelBinary channel = protocol.getChannel();
        try {
            channel.acquireWriteLock();
            try {
                ByteArrayOutputStream content = new ByteArrayOutputStream();
                DataOutputStream out = new DataOutputStream(content);
                out.writeByte('r');
                out.writeByte(iOp.type);
                out.writeInt(iToken);
                out.writeByte(ORecordInternal.getRecordType(iOp.getRecord()));
                writeVersion(out, iOp.getRecord().getVersion());
                writeRID(out, (ORecordId) iOp.getRecord().getIdentity());
                writeBytes(out, protocol.getRecordBytes(connection, iOp.getRecord()));
                channel.writeByte(OChannelBinaryProtocol.PUSH_DATA);
                channel.writeInt(Integer.MIN_VALUE);
                channel.writeByte(OChannelBinaryProtocol.REQUEST_PUSH_LIVE_QUERY);
                channel.writeBytes(content.toByteArray());
                channel.flush();
            } finally {
                channel.releaseWriteLock();
            }
            sendFail = false;
        } catch (IOException e) {
            session.removeConnection(curConnection);
            connections = session.getConnections();
            if (connections.isEmpty()) {
                ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.INSTANCE.get();
                OLiveQueryHook.unsubscribe(iToken, db);
                break;
            }
        } catch (Exception e) {
            OLogManager.instance().warn(this, "Cannot push cluster configuration to the client %s", e, protocol.getRemoteAddress());
            protocol.getServer().getClientConnectionManager().disconnect(connection);
            OLiveQueryHook.unsubscribe(iToken, connection.getDatabase());
            break;
        }
    } while (sendFail);
}
Also used : OChannelBinary(com.orientechnologies.orient.enterprise.channel.binary.OChannelBinary) DataOutputStream(java.io.DataOutputStream) OClientConnection(com.orientechnologies.orient.server.OClientConnection) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) OException(com.orientechnologies.common.exception.OException) IOException(java.io.IOException)

Example 3 with OChannelBinary

use of com.orientechnologies.orient.enterprise.channel.binary.OChannelBinary in project orientdb by orientechnologies.

the class OLiveCommandResultListener method onUnsubscribe.

@Override
public void onUnsubscribe(int iLiveToken) {
    boolean sendFail = true;
    do {
        List<OClientConnection> connections = session.getConnections();
        if (connections.size() == 0) {
            break;
        }
        ONetworkProtocolBinary protocol = (ONetworkProtocolBinary) connections.get(0).getProtocol();
        OChannelBinary channel = protocol.getChannel();
        try {
            channel.acquireWriteLock();
            try {
                ByteArrayOutputStream content = new ByteArrayOutputStream();
                DataOutputStream out = new DataOutputStream(content);
                out.writeByte('u');
                out.writeInt(iLiveToken);
                channel.writeByte(OChannelBinaryProtocol.PUSH_DATA);
                channel.writeInt(Integer.MIN_VALUE);
                channel.writeByte(OChannelBinaryProtocol.REQUEST_PUSH_LIVE_QUERY);
                channel.writeBytes(content.toByteArray());
                channel.flush();
            } finally {
                channel.releaseWriteLock();
            }
            sendFail = false;
        } catch (IOException e) {
            connections = session.getConnections();
            if (connections.isEmpty()) {
                break;
            }
        } catch (Exception e) {
            OLogManager.instance().warn(this, "Cannot push cluster configuration to the client %s", e, protocol.getRemoteAddress());
            protocol.getServer().getClientConnectionManager().disconnect(connection);
            break;
        }
    } while (sendFail);
}
Also used : OChannelBinary(com.orientechnologies.orient.enterprise.channel.binary.OChannelBinary) DataOutputStream(java.io.DataOutputStream) OClientConnection(com.orientechnologies.orient.server.OClientConnection) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) OException(com.orientechnologies.common.exception.OException) IOException(java.io.IOException)

Aggregations

OException (com.orientechnologies.common.exception.OException)3 OChannelBinary (com.orientechnologies.orient.enterprise.channel.binary.OChannelBinary)3 IOException (java.io.IOException)3 OClientConnection (com.orientechnologies.orient.server.OClientConnection)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataOutputStream (java.io.DataOutputStream)2 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)1 ORecordSerializer (com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer)1 OTokenSecurityException (com.orientechnologies.orient.enterprise.channel.binary.OTokenSecurityException)1 ONetworkProtocolBinary (com.orientechnologies.orient.server.network.protocol.binary.ONetworkProtocolBinary)1