Search in sources :

Example 11 with ClientException

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

the class MultipleSSLContextsTest method testThinClients.

/**
 * Checks that thin clients with SSL enabled can join the cluster and perform some work on it.
 *
 * @throws Exception If failed.
 */
@Test
public void testThinClients() throws Exception {
    int clientsNum = 3;
    int keysNum = 1000;
    String cacheName = "thinClientCache";
    List<IgniteClient> clients = new ArrayList<>(clientsNum);
    try {
        for (int i = 0; i < clientsNum; i++) {
            IgniteClient client = Ignition.startClient(clientConfiguration("127.0.0.1:1080" + i));
            clients.add(client);
        }
        Map<Integer, Integer> expMap = new HashMap<>();
        for (int i = 0; i < keysNum; i++) {
            int clientId = keysNum % clientsNum;
            ClientCache<Integer, Integer> cache = clients.get(clientId).getOrCreateCache(cacheName);
            cache.put(i, i);
            expMap.put(i, i);
        }
        IgniteCache<Integer, Integer> cache = grid(0).cache(cacheName);
        assertCacheContent(expMap, cache);
    } catch (ClientException ex) {
        ex.printStackTrace();
        fail("Failed to start thin Java clients: " + ex.getMessage());
    } finally {
        for (IgniteClient client : clients) client.close();
        IgniteCache cache = grid(0).cache(cacheName);
        if (cache != null)
            cache.destroy();
    }
}
Also used : IgniteClient(org.apache.ignite.client.IgniteClient) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IgniteCache(org.apache.ignite.IgniteCache) ClientException(org.apache.ignite.client.ClientException) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 12 with ClientException

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

the class ReliableChannel method getRetryLimit.

/**
 * Get retry limit.
 */
private int getRetryLimit() {
    List<ClientChannelHolder> holders = channels;
    if (holders == null)
        throw new ClientException("Connections to nodes aren't initialized.");
    int size = holders.size();
    return clientCfg.getRetryLimit() > 0 ? Math.min(clientCfg.getRetryLimit(), size) : size;
}
Also used : ClientException(org.apache.ignite.client.ClientException)

Example 13 with ClientException

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

the class ReliableChannel method affinityServiceAsync.

/**
 * Send request to affinity node and handle response.
 */
public <T> IgniteClientFuture<T> affinityServiceAsync(int cacheId, Object key, ClientOperation op, Consumer<PayloadOutputChannel> payloadWriter, Function<PayloadInputChannel, T> payloadReader) throws ClientException, ClientError {
    if (partitionAwarenessEnabled && affinityInfoIsUpToDate(cacheId)) {
        UUID affNodeId = affinityCtx.affinityNode(cacheId, key);
        if (affNodeId != null) {
            CompletableFuture<T> fut = new CompletableFuture<>();
            Object result = applyOnNodeChannel(affNodeId, channel -> channel.serviceAsync(op, payloadWriter, payloadReader).handle((res, err) -> {
                if (err == null) {
                    fut.complete(res);
                    return null;
                }
                try {
                    // Will try to reinit channels if topology changed.
                    onChannelFailure(channel);
                } catch (Throwable ex) {
                    fut.completeExceptionally(ex);
                    return null;
                }
                if (err instanceof ClientConnectionException) {
                    ClientConnectionException failure = (ClientConnectionException) err;
                    int attemptsLimit = getRetryLimit() - 1;
                    if (attemptsLimit == 0 || !shouldRetry(op, 0, failure)) {
                        fut.completeExceptionally(err);
                        return null;
                    }
                    handleServiceAsync(fut, op, payloadWriter, payloadReader, attemptsLimit, failure);
                    return null;
                }
                fut.completeExceptionally(err);
                return null;
            }));
            if (result != null)
                return new IgniteClientFutureImpl<>(fut);
        }
    }
    return serviceAsync(op, payloadWriter, payloadReader);
}
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) CompletableFuture(java.util.concurrent.CompletableFuture) ClientConnectionException(org.apache.ignite.client.ClientConnectionException) UUID(java.util.UUID)

Example 14 with ClientException

use of org.apache.ignite.client.ClientException 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 15 with ClientException

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

the class TcpClientTransactions method txStart0.

/**
 * @param concurrency Concurrency.
 * @param isolation Isolation.
 * @param timeout Timeout.
 */
private ClientTransaction txStart0(TransactionConcurrency concurrency, TransactionIsolation isolation, Long timeout, String lb) {
    TcpClientTransaction tx0 = tx();
    if (tx0 != null)
        throw new ClientException("A transaction has already been started by the current thread.");
    tx0 = ch.service(ClientOperation.TX_START, req -> {
        ProtocolContext protocolCtx = req.clientChannel().protocolCtx();
        if (!protocolCtx.isFeatureSupported(TRANSACTIONS)) {
            throw new ClientProtocolError(String.format("Transactions are not supported by the server's " + "protocol version %s, required version %s", protocolCtx.version(), TRANSACTIONS.verIntroduced()));
        }
        try (BinaryRawWriterEx writer = new BinaryWriterExImpl(marsh.context(), req.out(), null, null)) {
            writer.writeByte((byte) (concurrency == null ? txCfg.getDefaultTxConcurrency() : concurrency).ordinal());
            writer.writeByte((byte) (isolation == null ? txCfg.getDefaultTxIsolation() : isolation).ordinal());
            writer.writeLong(timeout == null ? txCfg.getDefaultTxTimeout() : timeout);
            writer.writeString(lb);
        }
    }, res -> new TcpClientTransaction(res.in().readInt(), res.clientChannel()));
    threadLocTxUid.set(tx0.txUid);
    txMap.put(tx0.txUid, tx0);
    return tx0;
}
Also used : TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) BinaryRawWriterEx(org.apache.ignite.internal.binary.BinaryRawWriterEx) A(org.apache.ignite.internal.util.typedef.internal.A) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) ClientTransactions(org.apache.ignite.client.ClientTransactions) BinaryWriterExImpl(org.apache.ignite.internal.binary.BinaryWriterExImpl) AtomicLong(java.util.concurrent.atomic.AtomicLong) ClientException(org.apache.ignite.client.ClientException) ClientTransaction(org.apache.ignite.client.ClientTransaction) Map(java.util.Map) TRANSACTIONS(org.apache.ignite.internal.client.thin.ProtocolVersionFeature.TRANSACTIONS) ClientTransactionConfiguration(org.apache.ignite.configuration.ClientTransactionConfiguration) ClientException(org.apache.ignite.client.ClientException) BinaryWriterExImpl(org.apache.ignite.internal.binary.BinaryWriterExImpl) BinaryRawWriterEx(org.apache.ignite.internal.binary.BinaryRawWriterEx)

Aggregations

ClientException (org.apache.ignite.client.ClientException)20 ArrayList (java.util.ArrayList)9 UUID (java.util.UUID)6 ClientConfiguration (org.apache.ignite.configuration.ClientConfiguration)6 ClientConnectionException (org.apache.ignite.client.ClientConnectionException)5 IgniteClient (org.apache.ignite.client.IgniteClient)5 HashMap (java.util.HashMap)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 Consumer (java.util.function.Consumer)4 Function (java.util.function.Function)4 BinaryRawWriterEx (org.apache.ignite.internal.binary.BinaryRawWriterEx)4 InetSocketAddress (java.net.InetSocketAddress)3 Collection (java.util.Collection)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Map (java.util.Map)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 ClientAuthenticationException (org.apache.ignite.client.ClientAuthenticationException)3 ClientFeatureNotSupportedByServerException (org.apache.ignite.client.ClientFeatureNotSupportedByServerException)3