Search in sources :

Example 6 with GridClientNodeImpl

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();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) GridClientNodeImpl(org.apache.ignite.internal.client.impl.GridClientNodeImpl)

Example 7 with GridClientNodeImpl

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);
}
Also used : GridClientNodeBean(org.apache.ignite.internal.processors.rest.client.message.GridClientNodeBean) GridClientNodeImpl(org.apache.ignite.internal.client.impl.GridClientNodeImpl) GridClientTopologyRequest(org.apache.ignite.internal.processors.rest.client.message.GridClientTopologyRequest) GridClientFutureAdapter(org.apache.ignite.internal.client.impl.GridClientFutureAdapter)

Example 8 with GridClientNodeImpl

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();
    }
}
Also used : HashMap(java.util.HashMap) GridClientNodeImpl(org.apache.ignite.internal.client.impl.GridClientNodeImpl) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map) LinkedList(java.util.LinkedList)

Example 9 with GridClientNodeImpl

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();
    }
}
Also used : GridClientNode(org.apache.ignite.internal.client.GridClientNode) GridClientDisconnectedException(org.apache.ignite.internal.client.GridClientDisconnectedException) GridClientNodeImpl(org.apache.ignite.internal.client.impl.GridClientNodeImpl) UUID(java.util.UUID) LinkedList(java.util.LinkedList)

Example 10 with GridClientNodeImpl

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;
}
Also used : GridClientNode(org.apache.ignite.internal.client.GridClientNode) GridClientException(org.apache.ignite.internal.client.GridClientException) GridClientTopology(org.apache.ignite.internal.client.impl.connection.GridClientTopology) GridClientConnectionResetException(org.apache.ignite.internal.client.impl.connection.GridClientConnectionResetException) GridClientConnectionManager(org.apache.ignite.internal.client.impl.connection.GridClientConnectionManager) GridClientConnection(org.apache.ignite.internal.client.impl.connection.GridClientConnection) GridClientFutureAdapter(org.apache.ignite.internal.client.impl.GridClientFutureAdapter) GridServerUnreachableException(org.apache.ignite.internal.client.GridServerUnreachableException) GridClientNodeImpl(org.apache.ignite.internal.client.impl.GridClientNodeImpl)

Aggregations

GridClientNodeImpl (org.apache.ignite.internal.client.impl.GridClientNodeImpl)10 HashMap (java.util.HashMap)4 UUID (java.util.UUID)4 GridClientNode (org.apache.ignite.internal.client.GridClientNode)3 GridClientTopologyRequest (org.apache.ignite.internal.processors.rest.client.message.GridClientTopologyRequest)3 LinkedList (java.util.LinkedList)2 GridClientFutureAdapter (org.apache.ignite.internal.client.impl.GridClientFutureAdapter)2 GridClientNodeBean (org.apache.ignite.internal.processors.rest.client.message.GridClientNodeBean)2 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 GridClientCacheMode (org.apache.ignite.internal.client.GridClientCacheMode)1 GridClientDisconnectedException (org.apache.ignite.internal.client.GridClientDisconnectedException)1 GridClientException (org.apache.ignite.internal.client.GridClientException)1 GridServerUnreachableException (org.apache.ignite.internal.client.GridServerUnreachableException)1 GridClientNodeMetricsAdapter (org.apache.ignite.internal.client.impl.GridClientNodeMetricsAdapter)1 GridClientConnection (org.apache.ignite.internal.client.impl.connection.GridClientConnection)1 GridClientConnectionManager (org.apache.ignite.internal.client.impl.connection.GridClientConnectionManager)1