Search in sources :

Example 1 with GridClientDataAffinity

use of org.apache.ignite.internal.client.GridClientDataAffinity in project ignite by apache.

the class GridClientDataImpl method affinity.

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override
public <K> UUID affinity(K key) throws GridClientException {
    A.notNull(key, "key");
    GridClientDataAffinity affinity = client.affinity(cacheName);
    if (affinity == null)
        return null;
    Collection<? extends GridClientNode> prj = projectionNodes();
    if (prj.isEmpty())
        throw new GridClientException("Failed to get affinity node (projection node set for cache is empty): " + cacheName());
    GridClientNode node = affinity.node(key, prj);
    assert node != null;
    return node.nodeId();
}
Also used : GridClientException(org.apache.ignite.internal.client.GridClientException) GridClientNode(org.apache.ignite.internal.client.GridClientNode) GridClientDataAffinity(org.apache.ignite.internal.client.GridClientDataAffinity)

Example 2 with GridClientDataAffinity

use of org.apache.ignite.internal.client.GridClientDataAffinity in project ignite by apache.

the class GridClientImpl method tryInitTopology.

/**
     * Tries to init client topology using configured set of servers or routers.
     *
     * @throws GridClientException If initialisation failed.
     * @throws InterruptedException If initialisation was interrupted.
     */
private void tryInitTopology() throws GridClientException, InterruptedException {
    boolean hasSrvs = routers.isEmpty();
    final Collection<InetSocketAddress> connSrvs = (hasSrvs) ? new LinkedHashSet<>(srvs) : routers;
    if (hasSrvs) {
        // Add REST endpoints for all nodes from previous topology snapshot.
        try {
            for (GridClientNodeImpl node : top.nodes()) {
                Collection<InetSocketAddress> endpoints = node.availableAddresses(cfg.getProtocol(), true);
                List<InetSocketAddress> resolvedEndpoints = new ArrayList<>(endpoints.size());
                for (InetSocketAddress endpoint : endpoints) if (!endpoint.isUnresolved())
                    resolvedEndpoints.add(endpoint);
                boolean sameHost = node.attributes().isEmpty() || F.containsAny(U.allLocalMACs(), node.attribute(ATTR_MACS).toString().split(", "));
                if (sameHost) {
                    Collections.sort(resolvedEndpoints, U.inetAddressesComparator(true));
                    connSrvs.addAll(resolvedEndpoints);
                } else {
                    for (InetSocketAddress endpoint : resolvedEndpoints) if (!endpoint.getAddress().isLoopbackAddress())
                        connSrvs.add(endpoint);
                }
            }
        } catch (GridClientDisconnectedException ignored) {
        // Ignore if latest topology update failed.
        }
    }
    connMgr.init(connSrvs);
    Map<String, GridClientCacheMode> overallCaches = new HashMap<>();
    for (GridClientNodeImpl node : top.nodes()) overallCaches.putAll(node.caches());
    for (Map.Entry<String, GridClientCacheMode> entry : overallCaches.entrySet()) {
        GridClientDataAffinity affinity = affinity(entry.getKey());
        if (affinity instanceof GridClientPartitionAffinity && entry.getValue() != GridClientCacheMode.PARTITIONED)
            log.warning(GridClientPartitionAffinity.class.getSimpleName() + " is used for a cache configured " + "for non-partitioned mode [cacheName=" + entry.getKey() + ", cacheMode=" + entry.getValue() + ']');
    }
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) GridClientPartitionAffinity(org.apache.ignite.internal.client.GridClientPartitionAffinity) InetSocketAddress(java.net.InetSocketAddress) GridClientDisconnectedException(org.apache.ignite.internal.client.GridClientDisconnectedException) ArrayList(java.util.ArrayList) GridClientDataAffinity(org.apache.ignite.internal.client.GridClientDataAffinity) GridClientCacheMode(org.apache.ignite.internal.client.GridClientCacheMode) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 3 with GridClientDataAffinity

use of org.apache.ignite.internal.client.GridClientDataAffinity in project ignite by apache.

the class GridClientAbstractProjection method withReconnectHandling.

/**
     * This method executes request to a communication layer and handles connection error, if it occurs. Server
     * is picked up according to the projection affinity and key given. Connection will be made with the node
     * on which key is cached. In case of communication exception client instance is notified and new instance
     * of client is created. If none of servers can be reached, an exception is thrown.
     *
     * @param c Closure to be executed.
     * @param cacheName Cache name for which mapped node will be calculated.
     * @param affKey Affinity key.
     * @param <R> Type of result in future.
     * @return Operation future.
     */
protected <R> GridClientFuture<R> withReconnectHandling(ClientProjectionClosure<R> c, String cacheName, @Nullable Object affKey) {
    GridClientDataAffinity affinity = client.affinity(cacheName);
    // If pinned (fixed-nodes) or no affinity provided use balancer.
    if (nodes != null || affinity == null || affKey == null)
        return withReconnectHandling(c);
    try {
        Collection<? extends GridClientNode> prjNodes = projectionNodes();
        if (prjNodes.isEmpty())
            throw new GridServerUnreachableException("Failed to get affinity node (no nodes in topology were " + "accepted by the filter): " + filter);
        GridClientNode node = affinity.node(affKey, prjNodes);
        for (int i = 0; i < RETRY_CNT; i++) {
            GridClientConnection conn = null;
            try {
                conn = client.connectionManager().connection(node);
                return c.apply(conn, node.nodeId());
            } catch (GridConnectionIdleClosedException e) {
                client.connectionManager().terminateConnection(conn, node, e);
            } catch (GridClientConnectionResetException e) {
                client.connectionManager().terminateConnection(conn, node, e);
                if (!checkNodeAlive(node.nodeId()))
                    throw new GridServerUnreachableException("Failed to communicate with mapped grid node for " + "given affinity key (node left the grid) [nodeId=" + node.nodeId() + ", affKey=" + affKey + ']', e);
            } catch (RuntimeException | Error e) {
                if (conn != null)
                    client.connectionManager().terminateConnection(conn, node, e);
                throw e;
            }
            U.sleep(RETRY_DELAY);
        }
        throw new GridServerUnreachableException("Failed to communicate with mapped grid node for given affinity " + "key (did node leave the grid?) [nodeId=" + node.nodeId() + ", affKey=" + affKey + ']');
    } catch (GridClientException e) {
        return new GridClientFutureAdapter<>(e);
    } catch (IgniteInterruptedCheckedException | InterruptedException e) {
        Thread.currentThread().interrupt();
        return new GridClientFutureAdapter<>(new GridClientException("Interrupted when (re)trying to perform " + "request.", e));
    }
}
Also used : GridClientNode(org.apache.ignite.internal.client.GridClientNode) GridClientException(org.apache.ignite.internal.client.GridClientException) GridConnectionIdleClosedException(org.apache.ignite.internal.client.impl.connection.GridConnectionIdleClosedException) GridClientConnectionResetException(org.apache.ignite.internal.client.impl.connection.GridClientConnectionResetException) GridClientDataAffinity(org.apache.ignite.internal.client.GridClientDataAffinity) GridClientConnection(org.apache.ignite.internal.client.impl.connection.GridClientConnection) GridServerUnreachableException(org.apache.ignite.internal.client.GridServerUnreachableException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException)

Aggregations

GridClientDataAffinity (org.apache.ignite.internal.client.GridClientDataAffinity)3 GridClientException (org.apache.ignite.internal.client.GridClientException)2 GridClientNode (org.apache.ignite.internal.client.GridClientNode)2 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)1 GridClientCacheMode (org.apache.ignite.internal.client.GridClientCacheMode)1 GridClientDisconnectedException (org.apache.ignite.internal.client.GridClientDisconnectedException)1 GridClientPartitionAffinity (org.apache.ignite.internal.client.GridClientPartitionAffinity)1 GridServerUnreachableException (org.apache.ignite.internal.client.GridServerUnreachableException)1 GridClientConnection (org.apache.ignite.internal.client.impl.connection.GridClientConnection)1 GridClientConnectionResetException (org.apache.ignite.internal.client.impl.connection.GridClientConnectionResetException)1 GridConnectionIdleClosedException (org.apache.ignite.internal.client.impl.connection.GridConnectionIdleClosedException)1