Search in sources :

Example 1 with BinaryOutputStream

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

Example 2 with BinaryOutputStream

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

the class TcpClientChannel method send.

/**
 * @param op Operation.
 * @param payloadWriter Payload writer to stream or {@code null} if request has no payload.
 * @return Request future.
 */
private ClientRequestFuture send(ClientOperation op, Consumer<PayloadOutputChannel> payloadWriter) throws ClientException {
    long id = reqId.getAndIncrement();
    PayloadOutputChannel payloadCh = new PayloadOutputChannel(this);
    try {
        if (closed())
            throw new ClientConnectionException("Channel is closed");
        ClientRequestFuture fut = new ClientRequestFuture();
        pendingReqs.put(id, fut);
        BinaryOutputStream req = payloadCh.out();
        // Reserve an integer for the request size.
        req.writeInt(0);
        req.writeShort(op.code());
        req.writeLong(id);
        if (payloadWriter != null)
            payloadWriter.accept(payloadCh);
        // Actual size.
        req.writeInt(0, req.position() - 4);
        write(req.array(), req.position(), payloadCh::close);
        return fut;
    } catch (Throwable t) {
        pendingReqs.remove(id);
        // Potential double-close is handled in PayloadOutputChannel.
        payloadCh.close();
        throw t;
    }
}
Also used : ClientConnectionException(org.apache.ignite.client.ClientConnectionException) BinaryOutputStream(org.apache.ignite.internal.binary.streams.BinaryOutputStream)

Example 3 with BinaryOutputStream

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

the class ClientCacheAffinityMapping method writeRequest.

/**
 * Writes caches affinity request to the output channel.
 *
 * @param ch Output channel.
 * @param cacheIds Cache IDs.
 */
public static void writeRequest(PayloadOutputChannel ch, Collection<Integer> cacheIds) {
    BinaryOutputStream out = ch.out();
    out.writeInt(cacheIds.size());
    for (int cacheId : cacheIds) out.writeInt(cacheId);
}
Also used : BinaryOutputStream(org.apache.ignite.internal.binary.streams.BinaryOutputStream)

Example 4 with BinaryOutputStream

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

the class TcpClientCache method writeCacheInfo.

/**
 * Write cache ID and flags.
 */
private void writeCacheInfo(PayloadOutputChannel payloadCh) {
    BinaryOutputStream out = payloadCh.out();
    out.writeInt(cacheId);
    byte flags = keepBinary ? KEEP_BINARY_FLAG_MASK : 0;
    TcpClientTransaction tx = transactions.tx();
    if (expiryPlc != null) {
        ProtocolContext protocolCtx = payloadCh.clientChannel().protocolCtx();
        if (!protocolCtx.isFeatureSupported(EXPIRY_POLICY)) {
            throw new ClientProtocolError(String.format("Expire policies are not supported by the server " + "version %s, required version %s", protocolCtx.version(), EXPIRY_POLICY.verIntroduced()));
        }
        flags |= WITH_EXPIRY_POLICY_FLAG_MASK;
    }
    if (tx != null) {
        if (tx.clientChannel() != payloadCh.clientChannel()) {
            throw new ClientException("Transaction context has been lost due to connection errors. " + "Cache operations are prohibited until current transaction closed.");
        }
        flags |= TRANSACTIONAL_FLAG_MASK;
    }
    out.writeByte(flags);
    if ((flags & WITH_EXPIRY_POLICY_FLAG_MASK) != 0) {
        out.writeLong(convertDuration(expiryPlc.getExpiryForCreation()));
        out.writeLong(convertDuration(expiryPlc.getExpiryForUpdate()));
        out.writeLong(convertDuration(expiryPlc.getExpiryForAccess()));
    }
    if ((flags & TRANSACTIONAL_FLAG_MASK) != 0)
        out.writeInt(tx.txId());
}
Also used : BinaryOutputStream(org.apache.ignite.internal.binary.streams.BinaryOutputStream) TcpClientTransaction(org.apache.ignite.internal.client.thin.TcpClientTransactions.TcpClientTransaction) ClientException(org.apache.ignite.client.ClientException)

Example 5 with BinaryOutputStream

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

the class ClientCacheEntryListenerHandler method startListen.

/**
 * Send request to the server and start
 */
public synchronized void startListen(CacheEntryUpdatedListener<K, V> locLsnr, ClientDisconnectListener disconnectLsnr, Factory<? extends CacheEntryEventFilter<? super K, ? super V>> rmtFilterFactory, int pageSize, long timeInterval, boolean includeExpired) {
    assert locLsnr != null;
    if (clientCh != null)
        throw new IllegalStateException("Listener was already started");
    this.locLsnr = locLsnr;
    this.disconnectLsnr = disconnectLsnr;
    Consumer<PayloadOutputChannel> qryWriter = payloadCh -> {
        BinaryOutputStream out = payloadCh.out();
        out.writeInt(ClientUtils.cacheId(jCacheAdapter.getName()));
        out.writeByte(keepBinary ? KEEP_BINARY_FLAG_MASK : 0);
        out.writeInt(pageSize);
        out.writeLong(timeInterval);
        out.writeBoolean(includeExpired);
        if (rmtFilterFactory == null)
            out.writeByte(GridBinaryMarshaller.NULL);
        else {
            utils.writeObject(out, rmtFilterFactory);
            out.writeByte(JAVA_PLATFORM);
        }
    };
    Function<PayloadInputChannel, T2<ClientChannel, Long>> qryReader = payloadCh -> {
        ClientChannel ch = payloadCh.clientChannel();
        Long rsrcId = payloadCh.in().readLong();
        ch.addNotificationListener(CONTINUOUS_QUERY_EVENT, rsrcId, this);
        return new T2<>(ch, rsrcId);
    };
    try {
        T2<ClientChannel, Long> params = ch.service(ClientOperation.QUERY_CONTINUOUS, qryWriter, qryReader);
        clientCh = params.get1();
        rsrcId = params.get2();
    } catch (ClientError e) {
        throw new ClientException(e);
    }
}
Also used : Factory(javax.cache.configuration.Factory) BinaryInputStream(org.apache.ignite.internal.binary.streams.BinaryInputStream) EventType(javax.cache.event.EventType) ClientDisconnectListener(org.apache.ignite.client.ClientDisconnectListener) CONTINUOUS_QUERY_EVENT(org.apache.ignite.internal.client.thin.ClientNotificationType.CONTINUOUS_QUERY_EVENT) U(org.apache.ignite.internal.util.typedef.internal.U) BinaryOutputStream(org.apache.ignite.internal.binary.streams.BinaryOutputStream) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Function(java.util.function.Function) ByteBuffer(java.nio.ByteBuffer) GridBinaryMarshaller(org.apache.ignite.internal.binary.GridBinaryMarshaller) ArrayList(java.util.ArrayList) T2(org.apache.ignite.internal.util.typedef.T2) Consumer(java.util.function.Consumer) List(java.util.List) CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) ClientException(org.apache.ignite.client.ClientException) BinaryByteBufferInputStream(org.apache.ignite.internal.binary.streams.BinaryByteBufferInputStream) Cache(javax.cache.Cache) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) JAVA_PLATFORM(org.apache.ignite.internal.client.thin.TcpClientCache.JAVA_PLATFORM) BinaryOutputStream(org.apache.ignite.internal.binary.streams.BinaryOutputStream) ClientException(org.apache.ignite.client.ClientException) T2(org.apache.ignite.internal.util.typedef.T2)

Aggregations

BinaryOutputStream (org.apache.ignite.internal.binary.streams.BinaryOutputStream)7 ArrayList (java.util.ArrayList)3 Consumer (java.util.function.Consumer)3 Function (java.util.function.Function)3 ClientException (org.apache.ignite.client.ClientException)3 BinaryInputStream (org.apache.ignite.internal.binary.streams.BinaryInputStream)3 U (org.apache.ignite.internal.util.typedef.internal.U)3 IOException (java.io.IOException)2 Array (java.lang.reflect.Array)2 SimpleEntry (java.util.AbstractMap.SimpleEntry)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 Objects (java.util.Objects)2 Set (java.util.Set)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2