Search in sources :

Example 6 with BinaryHeapOutputStream

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

the class OdbcMessageParser method encode.

/**
 * {@inheritDoc}
 */
@Override
public ClientMessage encode(ClientListenerResponse 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.
    if (ver.compareTo(OdbcConnectionContext.VER_2_1_5) < 0) {
        writer.writeByte((byte) (msg.status() == ClientListenerResponse.STATUS_SUCCESS ? ClientListenerResponse.STATUS_SUCCESS : ClientListenerResponse.STATUS_FAILED));
    } else
        writer.writeInt(msg.status());
    if (msg.status() != ClientListenerResponse.STATUS_SUCCESS) {
        writer.writeString(msg.error());
        return new ClientMessage(writer.array());
    }
    Object res0 = msg.response();
    if (res0 == null)
        return new ClientMessage(writer.array());
    else if (res0 instanceof OdbcQueryExecuteResult) {
        OdbcQueryExecuteResult res = (OdbcQueryExecuteResult) res0;
        if (log.isDebugEnabled())
            log.debug("Resulting query ID: " + res.queryId());
        writer.writeLong(res.queryId());
        Collection<OdbcColumnMeta> metas = res.columnsMetadata();
        writeResultsetMeta(writer, metas);
        writeAffectedRows(writer, res.affectedRows());
    } else if (res0 instanceof OdbcQueryExecuteBatchResult) {
        OdbcQueryExecuteBatchResult res = (OdbcQueryExecuteBatchResult) res0;
        writer.writeBoolean(res.errorMessage() == null);
        writeAffectedRows(writer, res.affectedRows());
        if (res.errorMessage() != null) {
            writer.writeLong(res.errorSetIdx());
            writer.writeString(res.errorMessage());
            if (ver.compareTo(OdbcConnectionContext.VER_2_1_5) >= 0)
                writer.writeInt(res.errorCode());
        }
    } else if (res0 instanceof OdbcStreamingBatchResult) {
        OdbcStreamingBatchResult res = (OdbcStreamingBatchResult) res0;
        writer.writeString(res.error());
        writer.writeInt(res.status());
        writer.writeLong(res.order());
    } 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 OdbcQueryMoreResultsResult) {
        OdbcQueryMoreResultsResult res = (OdbcQueryMoreResultsResult) 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();
        writeResultsetMeta(writer, columnsMeta);
    } 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 if (res0 instanceof OdbcQueryGetResultsetMetaResult) {
        OdbcQueryGetResultsetMetaResult res = (OdbcQueryGetResultsetMetaResult) res0;
        writeResultsetMeta(writer, res.columnsMetadata());
    } else
        assert false : "Should not reach here.";
    return new ClientMessage(writer.array());
}
Also used : BinaryHeapOutputStream(org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream) ClientMessage(org.apache.ignite.internal.processors.odbc.ClientMessage) Collection(java.util.Collection) BinaryWriterExImpl(org.apache.ignite.internal.binary.BinaryWriterExImpl)

Example 7 with BinaryHeapOutputStream

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

the class JdbcThinTcpIo method sendRequest.

/**
     * @param req Request.
     * @param cap Initial ouput stream capacity.
     * @return Server response.
     * @throws IOException On IO error.
     * @throws IgniteCheckedException On error.
     */
@SuppressWarnings("unchecked")
public <R extends JdbcResult> R sendRequest(JdbcRequest req, int cap) throws IOException, IgniteCheckedException {
    BinaryWriterExImpl writer = new BinaryWriterExImpl(null, new BinaryHeapOutputStream(cap), null, null);
    req.writeBinary(writer);
    send(writer.array());
    BinaryReaderExImpl reader = new BinaryReaderExImpl(null, new BinaryHeapInputStream(read()), null, null, false);
    JdbcResponse res = new JdbcResponse();
    res.readBinary(reader);
    if (res.status() != SqlListenerResponse.STATUS_SUCCESS)
        throw new IgniteCheckedException("Error server response: [req=" + req + ", resp=" + res + ']');
    return (R) res.response();
}
Also used : 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) JdbcResponse(org.apache.ignite.internal.processors.odbc.jdbc.JdbcResponse) BinaryWriterExImpl(org.apache.ignite.internal.binary.BinaryWriterExImpl)

Example 8 with BinaryHeapOutputStream

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

the class JdbcThinTcpIo method sendRequest.

/**
 * @param req Request.
 * @return Server response.
 * @throws IOException In case of IO error.
 */
@SuppressWarnings("unchecked")
JdbcResponse sendRequest(JdbcRequest req) throws IOException {
    int cap = guessCapacity(req);
    BinaryWriterExImpl writer = new BinaryWriterExImpl(null, new BinaryHeapOutputStream(cap), null, null);
    req.writeBinary(writer);
    send(writer.array());
    BinaryReaderExImpl reader = new BinaryReaderExImpl(null, new BinaryHeapInputStream(read()), null, null, false);
    JdbcResponse res = new JdbcResponse();
    res.readBinary(reader);
    return res;
}
Also used : BinaryReaderExImpl(org.apache.ignite.internal.binary.BinaryReaderExImpl) BinaryHeapOutputStream(org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream) BinaryHeapInputStream(org.apache.ignite.internal.binary.streams.BinaryHeapInputStream) JdbcResponse(org.apache.ignite.internal.processors.odbc.jdbc.JdbcResponse) BinaryWriterExImpl(org.apache.ignite.internal.binary.BinaryWriterExImpl) IpcClientTcpEndpoint(org.apache.ignite.internal.util.ipc.loopback.IpcClientTcpEndpoint)

Example 9 with BinaryHeapOutputStream

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

the class TcpClientChannel method handshakeReq.

/**
 * Send handshake request.
 */
private void handshakeReq(ProtocolVersion proposedVer, String user, String pwd, Map<String, String> userAttrs) throws ClientConnectionException {
    BinaryContext ctx = new BinaryContext(BinaryCachingMetadataHandler.create(), new IgniteConfiguration(), null);
    try (BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx, new BinaryHeapOutputStream(32), null, null)) {
        ProtocolContext protocolCtx = protocolContextFromVersion(proposedVer);
        // reserve an integer for the request size
        writer.writeInt(0);
        writer.writeByte((byte) ClientListenerRequest.HANDSHAKE);
        writer.writeShort(proposedVer.major());
        writer.writeShort(proposedVer.minor());
        writer.writeShort(proposedVer.patch());
        writer.writeByte(ClientListenerNioListener.THIN_CLIENT);
        if (protocolCtx.isFeatureSupported(BITMAP_FEATURES)) {
            byte[] features = ProtocolBitmaskFeature.featuresAsBytes(protocolCtx.features());
            writer.writeByteArray(features);
        }
        if (protocolCtx.isFeatureSupported(USER_ATTRIBUTES))
            writer.writeMap(userAttrs);
        boolean authSupported = protocolCtx.isFeatureSupported(AUTHORIZATION);
        if (authSupported && user != null && !user.isEmpty()) {
            writer.writeString(user);
            writer.writeString(pwd);
        }
        // actual size
        writer.out().writeInt(0, writer.out().position() - 4);
        write(writer.out().arrayCopy(), writer.out().position(), null);
    }
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) BinaryHeapOutputStream(org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream) BinaryContext(org.apache.ignite.internal.binary.BinaryContext) BinaryWriterExImpl(org.apache.ignite.internal.binary.BinaryWriterExImpl)

Example 10 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, ClientMessage msg) {
    BinaryContext ctx = new BinaryContext(BinaryCachingMetadataHandler.create(), new IgniteConfiguration(), null);
    BinaryMarshaller marsh = new BinaryMarshaller();
    marsh.setContext(new MarshallerContextImpl(null, null));
    ctx.configure(marsh);
    BinaryReaderExImpl reader = new BinaryReaderExImpl(ctx, new BinaryHeapInputStream(msg.payload()), null, true);
    byte cmd = reader.readByte();
    if (cmd != ClientListenerRequest.HANDSHAKE) {
        U.warn(log, "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, ses);
        ensureClientPermissions(clientType);
        if (connCtx.isVersionSupported(ver)) {
            connCtx.initializeFromHandshake(ses, ver, reader);
            ses.addMeta(CONN_CTX_META_KEY, connCtx);
        } else
            throw new IgniteCheckedException("Unsupported version: " + ver.asString());
        cancelHandshakeTimeout(ses);
        connCtx.handler().writeHandshake(writer);
        metrics.onHandshakeAccept(clientType);
    } catch (IgniteAccessControlException authEx) {
        metrics.onFailedAuth();
        writer.writeBoolean(false);
        writer.writeShort((short) 0);
        writer.writeShort((short) 0);
        writer.writeShort((short) 0);
        writer.doWriteString(authEx.getMessage());
        if (ver.compareTo(ClientConnectionContext.VER_1_1_0) >= 0)
            writer.writeInt(ClientStatus.AUTH_FAILED);
    } catch (IgniteCheckedException e) {
        U.warn(log, "Error during handshake [rmtAddr=" + ses.remoteAddress() + ", msg=" + e.getMessage() + ']');
        metrics.onGeneralReject();
        ClientListenerProtocolVersion currVer;
        if (connCtx == null)
            currVer = ClientListenerProtocolVersion.create(0, 0, 0);
        else
            currVer = connCtx.defaultVersion();
        writer.writeBoolean(false);
        writer.writeShort(currVer.major());
        writer.writeShort(currVer.minor());
        writer.writeShort(currVer.maintenance());
        writer.doWriteString(e.getMessage());
        if (ver.compareTo(ClientConnectionContext.VER_1_1_0) >= 0)
            writer.writeInt(ClientStatus.FAILED);
    }
    ses.send(new ClientMessage(writer.array()));
}
Also used : MarshallerContextImpl(org.apache.ignite.internal.MarshallerContextImpl) BinaryReaderExImpl(org.apache.ignite.internal.binary.BinaryReaderExImpl) BinaryHeapOutputStream(org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream) BinaryMarshaller(org.apache.ignite.internal.binary.BinaryMarshaller) IgniteAccessControlException(org.apache.ignite.internal.processors.authentication.IgniteAccessControlException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) BinaryHeapInputStream(org.apache.ignite.internal.binary.streams.BinaryHeapInputStream) BinaryContext(org.apache.ignite.internal.binary.BinaryContext) BinaryWriterExImpl(org.apache.ignite.internal.binary.BinaryWriterExImpl)

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