use of org.apache.ignite.internal.client.impl.GridClientNodeImpl in project ignite by apache.
the class GridClientTopology method prepareNode.
/**
* Updates node properties according to current topology settings.
* Particularly attributes and metrics caching policies.
*
* @param node Node to be processed.
* @return The same node if cache is enabled or node contains no attributes and metrics,
* otherwise will return new node without attributes and metrics.
*/
private GridClientNodeImpl prepareNode(final GridClientNodeImpl node) {
final boolean noAttrsAndMetrics = (metricsCache && attrCache) || (node.attributes().isEmpty() && node.metrics() == null);
// Try to bypass object copying.
if (noAttrsAndMetrics && routerAddrs.isEmpty() && node.connectable())
return node;
// Return a new node instance based on the original one.
GridClientNodeImpl.Builder nodeBuilder = GridClientNodeImpl.builder(node, !attrCache, !metricsCache);
for (InetSocketAddress addr : node.availableAddresses(prot, true)) {
boolean router = routerAddrs.isEmpty() || routerAddrs.contains(addr);
boolean reachable = noAttrsAndMetrics || !addr.getAddress().isLoopbackAddress() || F.containsAny(macsCache, node.<String>attribute(ATTR_MACS).split(", "));
if (router && reachable) {
nodeBuilder.connectable(true);
break;
}
}
return nodeBuilder.build();
}
use of org.apache.ignite.internal.client.impl.GridClientNodeImpl in project ignite by apache.
the class GridClientNioTcpConnection method node.
/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override
public GridClientFuture<GridClientNode> node(final UUID id, boolean inclAttrs, boolean inclMetrics, UUID destNodeId) throws GridClientConnectionResetException, GridClientClosedException {
assert id != null;
TcpClientFuture fut = refreshNodeReqs.get(id);
// Return request that is in progress.
if (fut != null)
return fut;
GridClientTopologyRequest msg = new GridClientTopologyRequest();
fut = new TcpClientFuture() {
@Override
public void onDone(Object res) {
//Clean up the node id requests map.
refreshNodeReqs.remove(id);
GridClientNodeImpl node = nodeBeanToNode((GridClientNodeBean) res);
if (node != null)
top.updateNode(node);
super.onDone(node);
}
};
GridClientFutureAdapter old = refreshNodeReqs.putIfAbsent(id, fut);
// If concurrent thread put request, do not send the message.
if (old != null)
return old;
msg.nodeId(id);
msg.includeAttributes(inclAttrs);
msg.includeMetrics(inclMetrics);
msg.destinationId(destNodeId);
return makeRequest(msg, fut);
}
use of org.apache.ignite.internal.client.impl.GridClientNodeImpl in project ignite by apache.
the class GridClientTopology method updateTopology.
/**
* Updates (if cache is enabled) the whole topology. If cache is disabled, original collection is returned.
*
* @param nodeList Converted rest server response.
* @return Topology nodes.
*/
public Collection<? extends GridClientNode> updateTopology(Collection<GridClientNodeImpl> nodeList) {
Collection<TopologyEvent> evts = new LinkedList<>();
lock.writeLock().lock();
try {
Map<UUID, GridClientNodeImpl> updated = new HashMap<>();
Collection<GridClientNodeImpl> preparedNodes = F.transform(nodeList, new C1<GridClientNodeImpl, GridClientNodeImpl>() {
@Override
public GridClientNodeImpl apply(GridClientNodeImpl e) {
return prepareNode(e);
}
});
for (GridClientNodeImpl node : preparedNodes) {
updated.put(node.nodeId(), node);
// Generate add events.
if (!nodes.containsKey(node.nodeId()))
evts.add(new TopologyEvent(true, node));
}
for (Map.Entry<UUID, GridClientNodeImpl> e : nodes.entrySet()) {
if (!updated.containsKey(e.getKey()))
evts.add(new TopologyEvent(false, e.getValue()));
}
nodes = updated;
lastError = null;
if (!evts.isEmpty())
notifyEvents(evts);
return preparedNodes;
} finally {
lock.writeLock().unlock();
}
}
use of org.apache.ignite.internal.client.impl.GridClientNodeImpl in project ignite by apache.
the class GridClientTopology method nodes.
/**
* Gets a collection of nodes from last saved topology snapshot by their ids.
*
* @param ids Collection of ids for which nodes should be retrieved..
* @return Collection of nodes that are in topology.
* @throws GridClientException If topology is failed and no nodes available.
*/
public Collection<GridClientNode> nodes(Iterable<UUID> ids) throws GridClientException {
assert ids != null;
Collection<GridClientNode> res = new LinkedList<>();
lock.readLock().lock();
try {
if (lastError != null)
throw new GridClientDisconnectedException("Latest topology update failed.", lastError);
for (UUID id : ids) {
GridClientNodeImpl node = nodes.get(id);
if (node != null)
res.add(node);
}
return res;
} finally {
lock.readLock().unlock();
}
}
use of org.apache.ignite.internal.client.impl.GridClientNodeImpl in project ignite by apache.
the class GridRouterClientImpl method forwardMessage.
/**
* Send a raw packet "as is" directly to the given node.
* The exact types of acceptable arguments and return values depends on underlying connections.
*
* @param msg Raw message to send.
* @param destId Id of node to send message to. If {@code null} than node will be chosen
* from the topology randomly.
* @return Future, representing forwarded message.
* @throws GridServerUnreachableException If destination node can't be reached.
* @throws GridClientClosedException If client is closed.
* @throws GridClientException If any other client-related error occurs.
* @throws InterruptedException If router was interrupted while trying.
* to establish connection with destination node.
*/
GridClientFutureAdapter<?> forwardMessage(Object msg, @Nullable UUID destId, byte marshId) throws GridClientException, InterruptedException {
GridClientTopology top = clientImpl.topology();
GridClientNode dest = destId != null ? top.node(destId) : cliCfg.getBalancer().balancedNode(applyFilter(top.nodes(), new GridClientPredicate<GridClientNodeImpl>() {
@Override
public boolean apply(GridClientNodeImpl e) {
return restAvailable(e, cliCfg.getProtocol());
}
}));
if (dest == null)
throw new GridServerUnreachableException("Failed to resolve node for specified destination ID: " + destId);
GridClientConnectionManager connMgr = connectionManager(marshId);
GridClientConnection conn = null;
// No reconnection handling there. Let client to do it if needed.
GridClientException cause;
try {
conn = connMgr.connection(dest);
return conn.forwardMessage(msg);
} catch (GridClientConnectionResetException e) {
if (destId != null)
connMgr.terminateConnection(conn, top.node(destId), e);
else
connMgr.terminateConnection(conn, null, e);
cause = e;
} catch (GridClientException e) {
cause = e;
}
GridClientFutureAdapter<Object> fail = new GridClientFutureAdapter<>();
fail.onDone(cause);
return fail;
}
Aggregations