Search in sources :

Example 11 with BinaryReaderExImpl

use of org.apache.ignite.internal.binary.BinaryReaderExImpl in project ignite by apache.

the class OdbcMessageParser method decode.

/**
 * {@inheritDoc}
 */
@Override
public ClientListenerRequest decode(byte[] msg) {
    assert msg != null;
    BinaryInputStream stream = new BinaryHeapInputStream(msg);
    BinaryReaderExImpl reader = new BinaryReaderExImpl(marsh.context(), stream, ctx.config().getClassLoader(), true);
    byte cmd = reader.readByte();
    ClientListenerRequest res;
    switch(cmd) {
        case OdbcRequest.QRY_EXEC:
            {
                String schema = reader.readString();
                String sql = reader.readString();
                int paramNum = reader.readInt();
                Object[] params = readParameterRow(reader, paramNum);
                int timeout = 0;
                if (ver.compareTo(OdbcConnectionContext.VER_2_3_2) >= 0)
                    timeout = reader.readInt();
                res = new OdbcQueryExecuteRequest(schema, sql, params, timeout);
                break;
            }
        case OdbcRequest.QRY_EXEC_BATCH:
            {
                String schema = reader.readString();
                String sql = reader.readString();
                int paramRowLen = reader.readInt();
                int rowNum = reader.readInt();
                boolean last = reader.readBoolean();
                Object[][] params = new Object[rowNum][];
                for (int i = 0; i < rowNum; ++i) params[i] = readParameterRow(reader, paramRowLen);
                int timeout = 0;
                if (ver.compareTo(OdbcConnectionContext.VER_2_3_2) >= 0)
                    timeout = reader.readInt();
                res = new OdbcQueryExecuteBatchRequest(schema, sql, last, params, timeout);
                break;
            }
        case OdbcRequest.QRY_FETCH:
            {
                long queryId = reader.readLong();
                int pageSize = reader.readInt();
                res = new OdbcQueryFetchRequest(queryId, pageSize);
                break;
            }
        case OdbcRequest.QRY_CLOSE:
            {
                long queryId = reader.readLong();
                res = new OdbcQueryCloseRequest(queryId);
                break;
            }
        case OdbcRequest.META_COLS:
            {
                String schema = reader.readString();
                String table = reader.readString();
                String column = reader.readString();
                res = new OdbcQueryGetColumnsMetaRequest(schema, table, column);
                break;
            }
        case OdbcRequest.META_TBLS:
            {
                String catalog = reader.readString();
                String schema = reader.readString();
                String table = reader.readString();
                String tableType = reader.readString();
                res = new OdbcQueryGetTablesMetaRequest(catalog, schema, table, tableType);
                break;
            }
        case OdbcRequest.META_PARAMS:
            {
                String schema = reader.readString();
                String sqlQuery = reader.readString();
                res = new OdbcQueryGetParamsMetaRequest(schema, sqlQuery);
                break;
            }
        case OdbcRequest.MORE_RESULTS:
            {
                long queryId = reader.readLong();
                int pageSize = reader.readInt();
                res = new OdbcQueryMoreResultsRequest(queryId, pageSize);
                break;
            }
        default:
            throw new IgniteException("Unknown ODBC command: [cmd=" + cmd + ']');
    }
    return res;
}
Also used : BinaryReaderExImpl(org.apache.ignite.internal.binary.BinaryReaderExImpl) BinaryInputStream(org.apache.ignite.internal.binary.streams.BinaryInputStream) ClientListenerRequest(org.apache.ignite.internal.processors.odbc.ClientListenerRequest) IgniteException(org.apache.ignite.IgniteException) BinaryHeapInputStream(org.apache.ignite.internal.binary.streams.BinaryHeapInputStream)

Example 12 with BinaryReaderExImpl

use of org.apache.ignite.internal.binary.BinaryReaderExImpl in project ignite by apache.

the class TcpClientChannel method handshakeRes.

/**
 * Receive and handle handshake response.
 */
private void handshakeRes(ByteBuffer buf, ProtocolVersion proposedVer, String user, String pwd, Map<String, String> userAttrs) throws ClientConnectionException, ClientAuthenticationException, ClientProtocolError {
    BinaryInputStream res = BinaryByteBufferInputStream.create(buf);
    try (BinaryReaderExImpl reader = ClientUtils.createBinaryReader(null, res)) {
        boolean success = res.readBoolean();
        if (success) {
            byte[] features = EMPTY_BYTES;
            if (ProtocolContext.isFeatureSupported(proposedVer, BITMAP_FEATURES))
                features = reader.readByteArray();
            protocolCtx = new ProtocolContext(proposedVer, ProtocolBitmaskFeature.enumSet(features));
            if (protocolCtx.isFeatureSupported(PARTITION_AWARENESS)) {
                // Reading server UUID
                srvNodeId = reader.readUuid();
            }
        } else {
            ProtocolVersion srvVer = new ProtocolVersion(res.readShort(), res.readShort(), res.readShort());
            String err = reader.readString();
            int errCode = ClientStatus.FAILED;
            if (res.remaining() > 0)
                errCode = reader.readInt();
            if (errCode == ClientStatus.AUTH_FAILED)
                throw new ClientAuthenticationException(err);
            else if (proposedVer.equals(srvVer))
                throw new ClientProtocolError(err);
            else if (!supportedVers.contains(srvVer) || (!ProtocolContext.isFeatureSupported(srvVer, AUTHORIZATION) && !F.isEmpty(user)))
                // authentication and authentication is required.
                throw new ClientProtocolError(String.format("Protocol version mismatch: client %s / server %s. Server details: %s", proposedVer, srvVer, err));
            else {
                // Retry with server version.
                handshake(srvVer, user, pwd, userAttrs);
            }
        }
    } catch (IOException e) {
        throw handleIOError(e);
    }
}
Also used : ClientAuthenticationException(org.apache.ignite.client.ClientAuthenticationException) BinaryReaderExImpl(org.apache.ignite.internal.binary.BinaryReaderExImpl) BinaryInputStream(org.apache.ignite.internal.binary.streams.BinaryInputStream) IOException(java.io.IOException)

Example 13 with BinaryReaderExImpl

use of org.apache.ignite.internal.binary.BinaryReaderExImpl 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)

Example 14 with BinaryReaderExImpl

use of org.apache.ignite.internal.binary.BinaryReaderExImpl in project ignite by apache.

the class ClientCacheAffinityMapping method readResponse.

/**
 * Reads caches affinity response from the input channel and creates {@code ClientCacheAffinityMapping} instance
 * from this response.
 *
 * @param ch Input channel.
 */
public static ClientCacheAffinityMapping readResponse(PayloadInputChannel ch) {
    try (BinaryReaderExImpl in = ClientUtils.createBinaryReader(null, ch.in())) {
        long topVer = in.readLong();
        int minorTopVer = in.readInt();
        ClientCacheAffinityMapping aff = new ClientCacheAffinityMapping(new AffinityTopologyVersion(topVer, minorTopVer));
        int mappingsCnt = in.readInt();
        for (int i = 0; i < mappingsCnt; i++) {
            boolean applicable = in.readBoolean();
            int cachesCnt = in.readInt();
            if (applicable) {
                // Partition awareness is applicable for this caches.
                Map<Integer, Map<Integer, Integer>> cacheKeyCfg = U.newHashMap(cachesCnt);
                for (int j = 0; j < cachesCnt; j++) cacheKeyCfg.put(in.readInt(), readCacheKeyConfiguration(in));
                UUID[] partToNode = readNodePartitions(in);
                for (Map.Entry<Integer, Map<Integer, Integer>> keyCfg : cacheKeyCfg.entrySet()) aff.cacheAffinity.put(keyCfg.getKey(), new CacheAffinityInfo(keyCfg.getValue(), partToNode));
            } else {
                // Partition awareness is not applicable for this caches.
                for (int j = 0; j < cachesCnt; j++) aff.cacheAffinity.put(in.readInt(), NOT_APPLICABLE_CACHE_AFFINITY_INFO);
            }
        }
        return aff;
    } catch (IOException e) {
        throw new ClientError(e);
    }
}
Also used : BinaryReaderExImpl(org.apache.ignite.internal.binary.BinaryReaderExImpl) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IOException(java.io.IOException) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map)

Example 15 with BinaryReaderExImpl

use of org.apache.ignite.internal.binary.BinaryReaderExImpl in project ignite by apache.

the class ClientUtils method binaryMetadata.

/**
 * Deserialize binary type metadata from stream.
 */
BinaryMetadata binaryMetadata(BinaryInputStream in) throws IOException {
    try (BinaryReaderExImpl reader = createBinaryReader(in)) {
        int typeId = reader.readInt();
        String typeName = reader.readString();
        String affKeyFieldName = reader.readString();
        Map<String, BinaryFieldMetadata> fields = ClientUtils.map(in, unused -> reader.readString(), unused2 -> new BinaryFieldMetadata(reader.readInt(), reader.readInt()));
        boolean isEnum = reader.readBoolean();
        Map<String, Integer> enumValues = isEnum ? ClientUtils.map(in, unsed -> reader.readString(), unsed2 -> reader.readInt()) : null;
        Collection<BinarySchema> schemas = ClientUtils.collection(in, unused -> new BinarySchema(reader.readInt(), new ArrayList<>(ClientUtils.collection(in, unused2 -> reader.readInt()))));
        return new BinaryMetadata(typeId, typeName, fields, affKeyFieldName, schemas, isEnum, enumValues);
    }
}
Also used : CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) BinaryObject(org.apache.ignite.binary.BinaryObject) PlatformExpiryPolicy.convertDuration(org.apache.ignite.internal.processors.platform.cache.expiry.PlatformExpiryPolicy.convertDuration) Arrays(java.util.Arrays) Array(java.lang.reflect.Array) CacheKeyConfiguration(org.apache.ignite.cache.CacheKeyConfiguration) MutableSingletonList(org.apache.ignite.internal.util.MutableSingletonList) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) BinaryOutputStream(org.apache.ignite.internal.binary.streams.BinaryOutputStream) CacheRebalanceMode(org.apache.ignite.cache.CacheRebalanceMode) BinaryRawWriter(org.apache.ignite.binary.BinaryRawWriter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BinaryUtils(org.apache.ignite.internal.binary.BinaryUtils) Map(java.util.Map) QueryEntity(org.apache.ignite.cache.QueryEntity) BinaryMetadata(org.apache.ignite.internal.binary.BinaryMetadata) ClientCacheConfiguration(org.apache.ignite.client.ClientCacheConfiguration) BinaryRawWriterEx(org.apache.ignite.internal.binary.BinaryRawWriterEx) BinaryContext(org.apache.ignite.internal.binary.BinaryContext) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) BinaryFieldMetadata(org.apache.ignite.internal.binary.BinaryFieldMetadata) Objects(java.util.Objects) Nullable(org.jetbrains.annotations.Nullable) BinaryObjectImpl(org.apache.ignite.internal.binary.BinaryObjectImpl) BinarySchema(org.apache.ignite.internal.binary.BinarySchema) BinaryHeapInputStream(org.apache.ignite.internal.binary.streams.BinaryHeapInputStream) U(org.apache.ignite.internal.util.typedef.internal.U) HashMap(java.util.HashMap) BinaryWriterExImpl(org.apache.ignite.internal.binary.BinaryWriterExImpl) Function(java.util.function.Function) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) BinaryReaderExImpl(org.apache.ignite.internal.binary.BinaryReaderExImpl) PartitionLossPolicy(org.apache.ignite.cache.PartitionLossPolicy) PlatformExpiryPolicy(org.apache.ignite.internal.processors.platform.cache.expiry.PlatformExpiryPolicy) CacheWriteSynchronizationMode(org.apache.ignite.cache.CacheWriteSynchronizationMode) BiConsumer(java.util.function.BiConsumer) LinkedList(java.util.LinkedList) SimpleEntry(java.util.AbstractMap.SimpleEntry) EXPIRY_POLICY(org.apache.ignite.internal.client.thin.ProtocolVersionFeature.EXPIRY_POLICY) LinkedHashSet(java.util.LinkedHashSet) BinaryThreadLocalContext(org.apache.ignite.internal.binary.BinaryThreadLocalContext) BinaryInputStream(org.apache.ignite.internal.binary.streams.BinaryInputStream) QueryIndexType(org.apache.ignite.cache.QueryIndexType) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) IOException(java.io.IOException) BinaryReaderHandles(org.apache.ignite.internal.binary.BinaryReaderHandles) Consumer(java.util.function.Consumer) QUERY_ENTITY_PRECISION_AND_SCALE(org.apache.ignite.internal.client.thin.ProtocolVersionFeature.QUERY_ENTITY_PRECISION_AND_SCALE) QueryIndex(org.apache.ignite.cache.QueryIndex) CacheMode(org.apache.ignite.cache.CacheMode) BinaryReaderExImpl(org.apache.ignite.internal.binary.BinaryReaderExImpl) ArrayList(java.util.ArrayList) BinaryMetadata(org.apache.ignite.internal.binary.BinaryMetadata) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BinaryFieldMetadata(org.apache.ignite.internal.binary.BinaryFieldMetadata) BinarySchema(org.apache.ignite.internal.binary.BinarySchema)

Aggregations

BinaryReaderExImpl (org.apache.ignite.internal.binary.BinaryReaderExImpl)19 BinaryHeapInputStream (org.apache.ignite.internal.binary.streams.BinaryHeapInputStream)15 BinaryWriterExImpl (org.apache.ignite.internal.binary.BinaryWriterExImpl)10 BinaryInputStream (org.apache.ignite.internal.binary.streams.BinaryInputStream)9 BinaryHeapOutputStream (org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream)8 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 HashMap (java.util.HashMap)3 BinaryContext (org.apache.ignite.internal.binary.BinaryContext)3 JdbcResponse (org.apache.ignite.internal.processors.odbc.jdbc.JdbcResponse)3 Array (java.lang.reflect.Array)2 SQLException (java.sql.SQLException)2 SimpleEntry (java.util.AbstractMap.SimpleEntry)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 LinkedList (java.util.LinkedList)2