Search in sources :

Example 1 with BinaryHeapOutputStream

use of org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream in project ignite by apache.

the class SqlListenerNioListener method onHandshake.

/**
     * Perform handshake.
     *
     * @param ses Session.
     * @param msg Message bytes.
     */
private void onHandshake(GridNioSession ses, byte[] msg) {
    BinaryInputStream stream = new BinaryHeapInputStream(msg);
    BinaryReaderExImpl reader = new BinaryReaderExImpl(null, stream, null, true);
    byte cmd = reader.readByte();
    if (cmd != SqlListenerRequest.HANDSHAKE) {
        log.error("Unexpected SQL client request (will close session): " + ses.remoteAddress());
        ses.close();
        return;
    }
    short verMajor = reader.readShort();
    short verMinor = reader.readShort();
    short verMaintenance = reader.readShort();
    SqlListenerProtocolVersion ver = SqlListenerProtocolVersion.create(verMajor, verMinor, verMaintenance);
    String errMsg = null;
    if (SUPPORTED_VERS.contains(ver)) {
        // Prepare context.
        SqlListenerConnectionContext connCtx = prepareContext(ver, reader);
        ses.addMeta(CONN_CTX_META_KEY, connCtx);
    } else {
        log.warning("Unsupported version: " + ver.toString());
        errMsg = "Unsupported version.";
    }
    // Send response.
    BinaryWriterExImpl writer = new BinaryWriterExImpl(null, new BinaryHeapOutputStream(8), null, null);
    if (errMsg == null)
        writer.writeBoolean(true);
    else {
        writer.writeBoolean(false);
        writer.writeShort(CURRENT_VER.major());
        writer.writeShort(CURRENT_VER.minor());
        writer.writeShort(CURRENT_VER.maintenance());
        writer.doWriteString(errMsg);
    }
    ses.send(writer.array());
}
Also used : BinaryReaderExImpl(org.apache.ignite.internal.binary.BinaryReaderExImpl) BinaryInputStream(org.apache.ignite.internal.binary.streams.BinaryInputStream) BinaryHeapOutputStream(org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream) BinaryHeapInputStream(org.apache.ignite.internal.binary.streams.BinaryHeapInputStream) BinaryWriterExImpl(org.apache.ignite.internal.binary.BinaryWriterExImpl)

Example 2 with BinaryHeapOutputStream

use of org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream in project ignite by apache.

the class OdbcMessageParser method encode.

/** {@inheritDoc} */
@Override
public byte[] encode(SqlListenerResponse msg0) {
    assert msg0 != null;
    assert msg0 instanceof OdbcResponse;
    OdbcResponse msg = (OdbcResponse) msg0;
    // Creating new binary writer
    BinaryWriterExImpl writer = new BinaryWriterExImpl(marsh.context(), new BinaryHeapOutputStream(INIT_CAP), BinaryThreadLocalContext.get().schemaHolder(), null);
    // Writing status.
    writer.writeByte((byte) msg.status());
    if (msg.status() != SqlListenerResponse.STATUS_SUCCESS) {
        writer.writeString(msg.error());
        return writer.array();
    }
    Object res0 = msg.response();
    if (res0 == null)
        return writer.array();
    else if (res0 instanceof OdbcQueryExecuteResult) {
        OdbcQueryExecuteResult res = (OdbcQueryExecuteResult) res0;
        if (log.isDebugEnabled())
            log.debug("Resulting query ID: " + res.getQueryId());
        writer.writeLong(res.getQueryId());
        Collection<OdbcColumnMeta> metas = res.getColumnsMetadata();
        assert metas != null;
        writer.writeInt(metas.size());
        for (OdbcColumnMeta meta : metas) meta.write(writer);
    } else if (res0 instanceof OdbcQueryFetchResult) {
        OdbcQueryFetchResult res = (OdbcQueryFetchResult) res0;
        if (log.isDebugEnabled())
            log.debug("Resulting query ID: " + res.queryId());
        writer.writeLong(res.queryId());
        Collection<?> items0 = res.items();
        assert items0 != null;
        writer.writeBoolean(res.last());
        writer.writeInt(items0.size());
        for (Object row0 : items0) {
            if (row0 != null) {
                Collection<?> row = (Collection<?>) row0;
                writer.writeInt(row.size());
                for (Object obj : row) SqlListenerUtils.writeObject(writer, obj, true);
            }
        }
    } else if (res0 instanceof OdbcQueryCloseResult) {
        OdbcQueryCloseResult res = (OdbcQueryCloseResult) res0;
        if (log.isDebugEnabled())
            log.debug("Resulting query ID: " + res.getQueryId());
        writer.writeLong(res.getQueryId());
    } else if (res0 instanceof OdbcQueryGetColumnsMetaResult) {
        OdbcQueryGetColumnsMetaResult res = (OdbcQueryGetColumnsMetaResult) res0;
        Collection<OdbcColumnMeta> columnsMeta = res.meta();
        assert columnsMeta != null;
        writer.writeInt(columnsMeta.size());
        for (OdbcColumnMeta columnMeta : columnsMeta) columnMeta.write(writer);
    } else if (res0 instanceof OdbcQueryGetTablesMetaResult) {
        OdbcQueryGetTablesMetaResult res = (OdbcQueryGetTablesMetaResult) res0;
        Collection<OdbcTableMeta> tablesMeta = res.meta();
        assert tablesMeta != null;
        writer.writeInt(tablesMeta.size());
        for (OdbcTableMeta tableMeta : tablesMeta) tableMeta.writeBinary(writer);
    } else if (res0 instanceof OdbcQueryGetParamsMetaResult) {
        OdbcQueryGetParamsMetaResult res = (OdbcQueryGetParamsMetaResult) res0;
        byte[] typeIds = res.typeIds();
        SqlListenerUtils.writeObject(writer, typeIds, true);
    } else
        assert false : "Should not reach here.";
    return writer.array();
}
Also used : BinaryHeapOutputStream(org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream) Collection(java.util.Collection) BinaryWriterExImpl(org.apache.ignite.internal.binary.BinaryWriterExImpl)

Example 3 with BinaryHeapOutputStream

use of org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream in project ignite by apache.

the class JdbcThinTcpIo method handshake.

/**
     * @throws IOException On error.
     * @throws IgniteCheckedException On error.
     */
public void handshake() throws IOException, IgniteCheckedException {
    BinaryWriterExImpl writer = new BinaryWriterExImpl(null, new BinaryHeapOutputStream(HANDSHAKE_MSG_SIZE), null, null);
    writer.writeByte((byte) SqlListenerRequest.HANDSHAKE);
    writer.writeShort(CURRENT_VER.major());
    writer.writeShort(CURRENT_VER.minor());
    writer.writeShort(CURRENT_VER.maintenance());
    writer.writeByte(SqlListenerNioListener.JDBC_CLIENT);
    writer.writeBoolean(distributedJoins);
    writer.writeBoolean(enforceJoinOrder);
    send(writer.array());
    BinaryReaderExImpl reader = new BinaryReaderExImpl(null, new BinaryHeapInputStream(read()), null, null, false);
    boolean accepted = reader.readBoolean();
    if (accepted)
        return;
    short maj = reader.readShort();
    short min = reader.readShort();
    short maintenance = reader.readShort();
    String err = reader.readString();
    SqlListenerProtocolVersion ver = SqlListenerProtocolVersion.create(maj, min, maintenance);
    throw new IgniteCheckedException("Handshake failed [driverProtocolVer=" + CURRENT_VER + ", remoteNodeProtocolVer=" + ver + ", err=" + err + ']');
}
Also used : SqlListenerProtocolVersion(org.apache.ignite.internal.processors.odbc.SqlListenerProtocolVersion) BinaryReaderExImpl(org.apache.ignite.internal.binary.BinaryReaderExImpl) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) BinaryHeapOutputStream(org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream) BinaryHeapInputStream(org.apache.ignite.internal.binary.streams.BinaryHeapInputStream) BinaryWriterExImpl(org.apache.ignite.internal.binary.BinaryWriterExImpl)

Example 4 with BinaryHeapOutputStream

use of org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream in project ignite by apache.

the class ClientListenerNioListener method onHandshake.

/**
 * Perform handshake.
 *
 * @param ses Session.
 * @param msg Message bytes.
 */
private void onHandshake(GridNioSession ses, byte[] msg) {
    BinaryInputStream stream = new BinaryHeapInputStream(msg);
    BinaryReaderExImpl reader = new BinaryReaderExImpl(null, stream, null, true);
    byte cmd = reader.readByte();
    if (cmd != ClientListenerRequest.HANDSHAKE) {
        log.error("Unexpected client request (will close session): " + ses.remoteAddress());
        ses.close();
        return;
    }
    short verMajor = reader.readShort();
    short verMinor = reader.readShort();
    short verMaintenance = reader.readShort();
    ClientListenerProtocolVersion ver = ClientListenerProtocolVersion.create(verMajor, verMinor, verMaintenance);
    BinaryWriterExImpl writer = new BinaryWriterExImpl(null, new BinaryHeapOutputStream(8), null, null);
    byte clientType = reader.readByte();
    ClientListenerConnectionContext connCtx = null;
    try {
        connCtx = prepareContext(clientType);
        ensureClientPermissions(clientType);
        if (connCtx.isVersionSupported(ver)) {
            connCtx.initializeFromHandshake(ver, reader);
            ses.addMeta(CONN_CTX_META_KEY, connCtx);
        } else {
            log.warning("Unsupported version: " + ver.toString());
            throw new IgniteCheckedException("Unsupported version.");
        }
        connCtx.handler().writeHandshake(writer);
    } catch (IgniteCheckedException e) {
        log.error("Error on handshake. " + e.getMessage(), e);
        ClientListenerProtocolVersion currVer;
        if (connCtx == null)
            currVer = ClientListenerProtocolVersion.create(0, 0, 0);
        else
            currVer = connCtx.currentVersion();
        writer.writeBoolean(false);
        writer.writeShort(currVer.major());
        writer.writeShort(currVer.minor());
        writer.writeShort(currVer.maintenance());
        writer.doWriteString(e.getMessage());
    }
    ses.send(writer.array());
}
Also used : BinaryReaderExImpl(org.apache.ignite.internal.binary.BinaryReaderExImpl) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) BinaryInputStream(org.apache.ignite.internal.binary.streams.BinaryInputStream) BinaryHeapOutputStream(org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream) BinaryHeapInputStream(org.apache.ignite.internal.binary.streams.BinaryHeapInputStream) BinaryWriterExImpl(org.apache.ignite.internal.binary.BinaryWriterExImpl)

Example 5 with BinaryHeapOutputStream

use of org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream in project ignite by apache.

the class TimeoutTest method testServerClosesThinClientConnectionOnHandshakeTimeout.

/**
 * Test that server closes thin client connection in case of handshake timeout.
 */
@Test
public void testServerClosesThinClientConnectionOnHandshakeTimeout() {
    try (Ignite ignite = startGrid(0)) {
        long ts0 = System.currentTimeMillis();
        Socket s = new Socket();
        s.connect(new InetSocketAddress(clientHost(ignite.cluster().localNode()), clientPort(ignite.cluster().localNode())), 0);
        s.setSoTimeout(TIMEOUT * 2);
        OutputStream os = s.getOutputStream();
        try (BinaryOutputStream bos = new BinaryHeapOutputStream(32)) {
            // Size.
            bos.writeInt(1000);
            os.write(bos.arrayCopy());
            os.flush();
            InputStream is = s.getInputStream();
            // Connection and stream closed by server after timeout.
            assertEquals(-1, is.read());
            long ts1 = System.currentTimeMillis();
            assertTrue("Unexpected timeout [ts0=" + ts0 + ", ts1=" + ts1 + ']', ts1 - ts0 >= TIMEOUT && ts1 - ts0 < TIMEOUT * 2);
        } finally {
            s.close();
        }
    } catch (Exception e) {
        fail("Exception while sending message: " + e.getMessage());
    }
}
Also used : BinaryOutputStream(org.apache.ignite.internal.binary.streams.BinaryOutputStream) BinaryHeapOutputStream(org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream) InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) BinaryOutputStream(org.apache.ignite.internal.binary.streams.BinaryOutputStream) BinaryHeapOutputStream(org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream) OutputStream(java.io.OutputStream) Ignite(org.apache.ignite.Ignite) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) ClientException(org.apache.ignite.client.ClientException) ClientConnectionException(org.apache.ignite.client.ClientConnectionException) IgniteException(org.apache.ignite.IgniteException) Test(org.junit.Test)

Aggregations

BinaryHeapOutputStream (org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream)15 BinaryWriterExImpl (org.apache.ignite.internal.binary.BinaryWriterExImpl)12 BinaryHeapInputStream (org.apache.ignite.internal.binary.streams.BinaryHeapInputStream)9 BinaryReaderExImpl (org.apache.ignite.internal.binary.BinaryReaderExImpl)8 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)3 BinaryContext (org.apache.ignite.internal.binary.BinaryContext)3 SQLException (java.sql.SQLException)2 Collection (java.util.Collection)2 MarshallerContextImpl (org.apache.ignite.internal.MarshallerContextImpl)2 BinaryMarshaller (org.apache.ignite.internal.binary.BinaryMarshaller)2 BinaryInputStream (org.apache.ignite.internal.binary.streams.BinaryInputStream)2 ClientListenerProtocolVersion (org.apache.ignite.internal.processors.odbc.ClientListenerProtocolVersion)2 ClientMessage (org.apache.ignite.internal.processors.odbc.ClientMessage)2 JdbcResponse (org.apache.ignite.internal.processors.odbc.jdbc.JdbcResponse)2 IpcClientTcpEndpoint (org.apache.ignite.internal.util.ipc.loopback.IpcClientTcpEndpoint)2 IgniteProductVersion (org.apache.ignite.lang.IgniteProductVersion)2 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 InetSocketAddress (java.net.InetSocketAddress)1