Search in sources :

Example 1 with ClientConnectionException

use of org.apache.ignite.client.ClientConnectionException in project ignite by apache.

the class ReliableChannel method applyOnDefaultChannel.

/**
 * Apply specified {@code function} on any of available channel.
 */
private <T> T applyOnDefaultChannel(Function<ClientChannel, T> function, ClientOperation op, int attemptsLimit, Consumer<Integer> attemptsCallback) {
    ClientConnectionException failure = null;
    for (int attempt = 0; attempt < attemptsLimit; attempt++) {
        ClientChannelHolder hld = null;
        ClientChannel c = null;
        try {
            if (closed)
                throw new ClientException("Channel is closed");
            curChannelsGuard.readLock().lock();
            try {
                hld = channels.get(curChIdx);
            } finally {
                curChannelsGuard.readLock().unlock();
            }
            c = hld.getOrCreateChannel();
            if (c != null) {
                attemptsCallback.accept(attempt + 1);
                return function.apply(c);
            }
        } catch (ClientConnectionException e) {
            if (failure == null)
                failure = e;
            else
                failure.addSuppressed(e);
            onChannelFailure(hld, c);
            if (op != null && !shouldRetry(op, attempt, e))
                break;
        }
    }
    throw failure;
}
Also used : ClientConnectionException(org.apache.ignite.client.ClientConnectionException) ClientException(org.apache.ignite.client.ClientException)

Example 2 with ClientConnectionException

use of org.apache.ignite.client.ClientConnectionException in project ignite by apache.

the class ReliableChannel method handleServiceAsync.

/**
 * Handles serviceAsync results and retries as needed.
 */
private <T> void handleServiceAsync(final CompletableFuture<T> fut, ClientOperation op, Consumer<PayloadOutputChannel> payloadWriter, Function<PayloadInputChannel, T> payloadReader, int attemptsLimit, ClientConnectionException failure) {
    ClientChannel ch;
    // Workaround to store used attempts value within lambda body.
    int[] attemptsCnt = new int[1];
    try {
        ch = applyOnDefaultChannel(channel -> channel, null, attemptsLimit, v -> attemptsCnt[0] = v);
    } catch (Throwable ex) {
        if (failure != null) {
            failure.addSuppressed(ex);
            fut.completeExceptionally(failure);
            return;
        }
        fut.completeExceptionally(ex);
        return;
    }
    ch.serviceAsync(op, payloadWriter, payloadReader).handle((res, err) -> {
        if (err == null) {
            fut.complete(res);
            return null;
        }
        ClientConnectionException failure0 = failure;
        if (err instanceof ClientConnectionException) {
            try {
                // Will try to reinit channels if topology changed.
                onChannelFailure(ch);
            } catch (Throwable ex) {
                fut.completeExceptionally(ex);
                return null;
            }
            if (failure0 == null)
                failure0 = (ClientConnectionException) err;
            else
                failure0.addSuppressed(err);
            int attempt = attemptsCnt[0];
            int leftAttempts = attemptsLimit - attempt;
            // If it is a first retry then reset attempts (as for initialization we use only 1 attempt).
            if (failure == null)
                leftAttempts = getRetryLimit() - 1;
            if (leftAttempts > 0 && shouldRetry(op, attempt, failure0)) {
                handleServiceAsync(fut, op, payloadWriter, payloadReader, leftAttempts, failure0);
                return null;
            }
        } else {
            fut.completeExceptionally(err instanceof ClientException ? err : new ClientException(err));
            return null;
        }
        fut.completeExceptionally(failure0);
        return null;
    });
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) BiFunction(java.util.function.BiFunction) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) U(org.apache.ignite.internal.util.typedef.internal.U) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ClientRetryPolicy(org.apache.ignite.client.ClientRetryPolicy) ClientException(org.apache.ignite.client.ClientException) Map(java.util.Map) ClientConnectionException(org.apache.ignite.client.ClientConnectionException) IgniteBinary(org.apache.ignite.IgniteBinary) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ClientAuthorizationException(org.apache.ignite.client.ClientAuthorizationException) ClientAuthenticationException(org.apache.ignite.client.ClientAuthenticationException) GridNioClientConnectionMultiplexer(org.apache.ignite.internal.client.thin.io.gridnioserver.GridNioClientConnectionMultiplexer) F(org.apache.ignite.internal.util.typedef.F) HostAndPortRange(org.apache.ignite.internal.util.HostAndPortRange) Collection(java.util.Collection) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IgniteClientFuture(org.apache.ignite.client.IgniteClientFuture) Set(java.util.Set) UUID(java.util.UUID) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) ClientConnectionMultiplexer(org.apache.ignite.internal.client.thin.io.ClientConnectionMultiplexer) Consumer(java.util.function.Consumer) List(java.util.List) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) ForkJoinPool(java.util.concurrent.ForkJoinPool) ClientOperationType(org.apache.ignite.client.ClientOperationType) ClientRetryPolicyContext(org.apache.ignite.client.ClientRetryPolicyContext) ClientConnectorConfiguration(org.apache.ignite.configuration.ClientConnectorConfiguration) ClientConnectionException(org.apache.ignite.client.ClientConnectionException) ClientException(org.apache.ignite.client.ClientException)

Example 3 with ClientConnectionException

use of org.apache.ignite.client.ClientConnectionException in project ignite by apache.

the class TcpClientChannel method addNotificationListener.

/**
 * {@inheritDoc}
 */
@Override
public void addNotificationListener(ClientNotificationType type, Long rsrcId, NotificationListener lsnr) {
    Queue<T2<ByteBuffer, Exception>> pendingQueue = null;
    notificationLsnrsGuard.writeLock().lock();
    try {
        if (closed())
            throw new ClientConnectionException("Channel is closed");
        Map<Long, NotificationListener> lsnrs = notificationLsnrs[type.ordinal()];
        if (lsnrs == null)
            notificationLsnrs[type.ordinal()] = lsnrs = new ConcurrentHashMap<>();
        lsnrs.put(rsrcId, lsnr);
        if (type.keepNotificationsWithoutListener())
            pendingQueue = pendingNotifications[type.ordinal()].remove(rsrcId);
    } finally {
        notificationLsnrsGuard.writeLock().unlock();
    }
    // Drain pending notifications queue.
    if (pendingQueue != null)
        pendingQueue.forEach(n -> lsnr.acceptNotification(n.get1(), n.get2()));
}
Also used : BinaryCachingMetadataHandler(org.apache.ignite.internal.binary.BinaryCachingMetadataHandler) Arrays(java.util.Arrays) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) ClientFeatureNotSupportedByServerException(org.apache.ignite.client.ClientFeatureNotSupportedByServerException) BinaryOutputStream(org.apache.ignite.internal.binary.streams.BinaryOutputStream) ClientListenerNioListener(org.apache.ignite.internal.processors.odbc.ClientListenerNioListener) BITMAP_FEATURES(org.apache.ignite.internal.client.thin.ProtocolVersionFeature.BITMAP_FEATURES) ByteBuffer(java.nio.ByteBuffer) ClientException(org.apache.ignite.client.ClientException) Map(java.util.Map) ClientListenerRequest(org.apache.ignite.internal.processors.odbc.ClientListenerRequest) ClientReconnectedException(org.apache.ignite.client.ClientReconnectedException) ClientStatus(org.apache.ignite.internal.processors.platform.client.ClientStatus) EnumSet(java.util.EnumSet) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ClientAuthenticationException(org.apache.ignite.client.ClientAuthenticationException) V1_5_0(org.apache.ignite.internal.client.thin.ProtocolVersion.V1_5_0) V1_7_0(org.apache.ignite.internal.client.thin.ProtocolVersion.V1_7_0) V1_1_0(org.apache.ignite.internal.client.thin.ProtocolVersion.V1_1_0) BinaryContext(org.apache.ignite.internal.binary.BinaryContext) V1_3_0(org.apache.ignite.internal.client.thin.ProtocolVersion.V1_3_0) Collection(java.util.Collection) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) USER_ATTRIBUTES(org.apache.ignite.internal.client.thin.ProtocolBitmaskFeature.USER_ATTRIBUTES) UUID(java.util.UUID) PARTITION_AWARENESS(org.apache.ignite.internal.client.thin.ProtocolVersionFeature.PARTITION_AWARENESS) InetSocketAddress(java.net.InetSocketAddress) ClientConnectionMultiplexer(org.apache.ignite.internal.client.thin.io.ClientConnectionMultiplexer) Nullable(org.jetbrains.annotations.Nullable) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) Queue(java.util.Queue) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ClientConnection(org.apache.ignite.internal.client.thin.io.ClientConnection) AUTHORIZATION(org.apache.ignite.internal.client.thin.ProtocolVersionFeature.AUTHORIZATION) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) U(org.apache.ignite.internal.util.typedef.internal.U) CompletableFuture(java.util.concurrent.CompletableFuture) BinaryWriterExImpl(org.apache.ignite.internal.binary.BinaryWriterExImpl) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Function(java.util.function.Function) LATEST_VER(org.apache.ignite.internal.client.thin.ProtocolVersion.LATEST_VER) BinaryHeapOutputStream(org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream) ClientConnectionStateHandler(org.apache.ignite.internal.client.thin.io.ClientConnectionStateHandler) BinaryReaderExImpl(org.apache.ignite.internal.binary.BinaryReaderExImpl) ClientFlag(org.apache.ignite.internal.processors.platform.client.ClientFlag) ClientConnectionException(org.apache.ignite.client.ClientConnectionException) ClientAuthorizationException(org.apache.ignite.client.ClientAuthorizationException) BinaryInputStream(org.apache.ignite.internal.binary.streams.BinaryInputStream) F(org.apache.ignite.internal.util.typedef.F) V1_4_0(org.apache.ignite.internal.client.thin.ProtocolVersion.V1_4_0) V1_6_0(org.apache.ignite.internal.client.thin.ProtocolVersion.V1_6_0) Executor(java.util.concurrent.Executor) V1_0_0(org.apache.ignite.internal.client.thin.ProtocolVersion.V1_0_0) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) V1_2_0(org.apache.ignite.internal.client.thin.ProtocolVersion.V1_2_0) IOException(java.io.IOException) T2(org.apache.ignite.internal.util.typedef.T2) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) BinaryByteBufferInputStream(org.apache.ignite.internal.binary.streams.BinaryByteBufferInputStream) ClientMessageHandler(org.apache.ignite.internal.client.thin.io.ClientMessageHandler) ForkJoinPool(java.util.concurrent.ForkJoinPool) ClientConnectionException(org.apache.ignite.client.ClientConnectionException) AtomicLong(java.util.concurrent.atomic.AtomicLong) T2(org.apache.ignite.internal.util.typedef.T2)

Example 4 with ClientConnectionException

use of org.apache.ignite.client.ClientConnectionException in project ignite by apache.

the class TcpClientChannel method processNextMessage.

/**
 * Process next message from the input stream and complete corresponding future.
 */
private void processNextMessage(ByteBuffer buf) throws ClientProtocolError, ClientConnectionException {
    BinaryInputStream dataInput = BinaryByteBufferInputStream.create(buf);
    if (protocolCtx == null) {
        // Process handshake.
        pendingReqs.remove(-1L).onDone(buf);
        return;
    }
    Long resId = dataInput.readLong();
    int status = 0;
    ClientOperation notificationOp = null;
    if (protocolCtx.isFeatureSupported(PARTITION_AWARENESS)) {
        short flags = dataInput.readShort();
        if ((flags & ClientFlag.AFFINITY_TOPOLOGY_CHANGED) != 0) {
            long topVer = dataInput.readLong();
            int minorTopVer = dataInput.readInt();
            srvTopVer = new AffinityTopologyVersion(topVer, minorTopVer);
            for (Consumer<ClientChannel> lsnr : topChangeLsnrs) lsnr.accept(this);
        }
        if ((flags & ClientFlag.NOTIFICATION) != 0) {
            short notificationCode = dataInput.readShort();
            notificationOp = ClientOperation.fromCode(notificationCode);
            if (notificationOp == null || notificationOp.notificationType() == null)
                throw new ClientProtocolError(String.format("Unexpected notification code [%d]", notificationCode));
        }
        if ((flags & ClientFlag.ERROR) != 0)
            status = dataInput.readInt();
    } else
        status = dataInput.readInt();
    int hdrSize = dataInput.position();
    int msgSize = buf.limit();
    ByteBuffer res;
    Exception err;
    if (status == 0) {
        err = null;
        res = msgSize > hdrSize ? buf : null;
    } else if (status == ClientStatus.SECURITY_VIOLATION) {
        err = new ClientAuthorizationException();
        res = null;
    } else {
        String errMsg = ClientUtils.createBinaryReader(null, dataInput).readString();
        err = new ClientServerError(errMsg, status, resId);
        res = null;
    }
    if (notificationOp == null) {
        // Respone received.
        ClientRequestFuture pendingReq = pendingReqs.remove(resId);
        if (pendingReq == null)
            throw new ClientProtocolError(String.format("Unexpected response ID [%s]", resId));
        pendingReq.onDone(res, err);
    } else {
        // Notification received.
        ClientNotificationType notificationType = notificationOp.notificationType();
        asyncContinuationExecutor.execute(() -> {
            NotificationListener lsnr = null;
            notificationLsnrsGuard.readLock().lock();
            try {
                Map<Long, NotificationListener> lsrns = notificationLsnrs[notificationType.ordinal()];
                if (lsrns != null)
                    lsnr = lsrns.get(resId);
                if (notificationType.keepNotificationsWithoutListener() && lsnr == null) {
                    pendingNotifications[notificationType.ordinal()].computeIfAbsent(resId, k -> new ConcurrentLinkedQueue<>()).add(new T2<>(res, err));
                }
            } finally {
                notificationLsnrsGuard.readLock().unlock();
            }
            if (lsnr != null)
                lsnr.acceptNotification(res, err);
        });
    }
}
Also used : BinaryCachingMetadataHandler(org.apache.ignite.internal.binary.BinaryCachingMetadataHandler) Arrays(java.util.Arrays) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) ClientFeatureNotSupportedByServerException(org.apache.ignite.client.ClientFeatureNotSupportedByServerException) BinaryOutputStream(org.apache.ignite.internal.binary.streams.BinaryOutputStream) ClientListenerNioListener(org.apache.ignite.internal.processors.odbc.ClientListenerNioListener) BITMAP_FEATURES(org.apache.ignite.internal.client.thin.ProtocolVersionFeature.BITMAP_FEATURES) ByteBuffer(java.nio.ByteBuffer) ClientException(org.apache.ignite.client.ClientException) Map(java.util.Map) ClientListenerRequest(org.apache.ignite.internal.processors.odbc.ClientListenerRequest) ClientReconnectedException(org.apache.ignite.client.ClientReconnectedException) ClientStatus(org.apache.ignite.internal.processors.platform.client.ClientStatus) EnumSet(java.util.EnumSet) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ClientAuthenticationException(org.apache.ignite.client.ClientAuthenticationException) V1_5_0(org.apache.ignite.internal.client.thin.ProtocolVersion.V1_5_0) V1_7_0(org.apache.ignite.internal.client.thin.ProtocolVersion.V1_7_0) V1_1_0(org.apache.ignite.internal.client.thin.ProtocolVersion.V1_1_0) BinaryContext(org.apache.ignite.internal.binary.BinaryContext) V1_3_0(org.apache.ignite.internal.client.thin.ProtocolVersion.V1_3_0) Collection(java.util.Collection) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) USER_ATTRIBUTES(org.apache.ignite.internal.client.thin.ProtocolBitmaskFeature.USER_ATTRIBUTES) UUID(java.util.UUID) PARTITION_AWARENESS(org.apache.ignite.internal.client.thin.ProtocolVersionFeature.PARTITION_AWARENESS) InetSocketAddress(java.net.InetSocketAddress) ClientConnectionMultiplexer(org.apache.ignite.internal.client.thin.io.ClientConnectionMultiplexer) Nullable(org.jetbrains.annotations.Nullable) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) Queue(java.util.Queue) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ClientConnection(org.apache.ignite.internal.client.thin.io.ClientConnection) AUTHORIZATION(org.apache.ignite.internal.client.thin.ProtocolVersionFeature.AUTHORIZATION) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) U(org.apache.ignite.internal.util.typedef.internal.U) CompletableFuture(java.util.concurrent.CompletableFuture) BinaryWriterExImpl(org.apache.ignite.internal.binary.BinaryWriterExImpl) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Function(java.util.function.Function) LATEST_VER(org.apache.ignite.internal.client.thin.ProtocolVersion.LATEST_VER) BinaryHeapOutputStream(org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream) ClientConnectionStateHandler(org.apache.ignite.internal.client.thin.io.ClientConnectionStateHandler) BinaryReaderExImpl(org.apache.ignite.internal.binary.BinaryReaderExImpl) ClientFlag(org.apache.ignite.internal.processors.platform.client.ClientFlag) ClientConnectionException(org.apache.ignite.client.ClientConnectionException) ClientAuthorizationException(org.apache.ignite.client.ClientAuthorizationException) BinaryInputStream(org.apache.ignite.internal.binary.streams.BinaryInputStream) F(org.apache.ignite.internal.util.typedef.F) V1_4_0(org.apache.ignite.internal.client.thin.ProtocolVersion.V1_4_0) V1_6_0(org.apache.ignite.internal.client.thin.ProtocolVersion.V1_6_0) Executor(java.util.concurrent.Executor) V1_0_0(org.apache.ignite.internal.client.thin.ProtocolVersion.V1_0_0) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) V1_2_0(org.apache.ignite.internal.client.thin.ProtocolVersion.V1_2_0) IOException(java.io.IOException) T2(org.apache.ignite.internal.util.typedef.T2) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) BinaryByteBufferInputStream(org.apache.ignite.internal.binary.streams.BinaryByteBufferInputStream) ClientMessageHandler(org.apache.ignite.internal.client.thin.io.ClientMessageHandler) ForkJoinPool(java.util.concurrent.ForkJoinPool) BinaryInputStream(org.apache.ignite.internal.binary.streams.BinaryInputStream) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) ByteBuffer(java.nio.ByteBuffer) ClientFeatureNotSupportedByServerException(org.apache.ignite.client.ClientFeatureNotSupportedByServerException) ClientException(org.apache.ignite.client.ClientException) ClientReconnectedException(org.apache.ignite.client.ClientReconnectedException) ClientAuthenticationException(org.apache.ignite.client.ClientAuthenticationException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ClientConnectionException(org.apache.ignite.client.ClientConnectionException) ClientAuthorizationException(org.apache.ignite.client.ClientAuthorizationException) IOException(java.io.IOException) ClientAuthorizationException(org.apache.ignite.client.ClientAuthorizationException) AtomicLong(java.util.concurrent.atomic.AtomicLong) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue)

Example 5 with ClientConnectionException

use of org.apache.ignite.client.ClientConnectionException in project ignite by apache.

the class TcpClientChannel method handshake.

/**
 * Client handshake.
 */
private void handshake(ProtocolVersion ver, String user, String pwd, Map<String, String> userAttrs) throws ClientConnectionException, ClientAuthenticationException, ClientProtocolError {
    ClientRequestFuture fut = new ClientRequestFuture();
    pendingReqs.put(-1L, fut);
    handshakeReq(ver, user, pwd, userAttrs);
    try {
        ByteBuffer res = timeout > 0 ? fut.get(timeout) : fut.get();
        handshakeRes(res, ver, user, pwd, userAttrs);
    } catch (IgniteCheckedException e) {
        throw new ClientConnectionException(e.getMessage(), e);
    }
}
Also used : ClientConnectionException(org.apache.ignite.client.ClientConnectionException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ClientConnectionException (org.apache.ignite.client.ClientConnectionException)8 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 InetSocketAddress (java.net.InetSocketAddress)5 ClientException (org.apache.ignite.client.ClientException)5 Arrays (java.util.Arrays)4 Collection (java.util.Collection)4 Map (java.util.Map)4 UUID (java.util.UUID)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 ForkJoinPool (java.util.concurrent.ForkJoinPool)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)4 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)4 Consumer (java.util.function.Consumer)4 Function (java.util.function.Function)4 ClientAuthenticationException (org.apache.ignite.client.ClientAuthenticationException)4 ClientAuthorizationException (org.apache.ignite.client.ClientAuthorizationException)4 ClientConfiguration (org.apache.ignite.configuration.ClientConfiguration)4 ClientConnectionMultiplexer (org.apache.ignite.internal.client.thin.io.ClientConnectionMultiplexer)4