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();
}
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() + ']');
}
}
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));
}
}
Aggregations