Exemplo n.º 1
0
        /**
         * <summary>
         * Convert response data into grid node bean.</summary>
         *
         * <param name="o">Response bean to convert into grid node.</param>
         * <returns>Converted grid node.</returns>
         */
        private IGridClientNode futureNodeConverter(Object o)
        {
            var bean = o as GridClientNodeBean;

            if (bean == null)
            {
                return(null);
            }

            IGridClientNode node = nodeBeanToNode(bean);

            // Update node in topology.
            node = Top.UpdateNode(node);

            return(node);
        }
        /**
         * <summary>
         * Resolve node info for specified node.
         * Add node to hash circle if this is the first node invocation.</summary>
         *
         * <param name="node">Node to get info for.</param>
         * <returns>Node info.</returns>
         */
        private NodeInfo ResolveNodeInfo(IGridClientNode node)
        {
            Guid     nodeId = node.Id;
            NodeInfo nodeInfo;

            if (addedNodes.TryGetValue(nodeId, out nodeInfo))
            {
                return(nodeInfo);
            }

            nodeInfo = new NodeInfo(nodeId, HashIdResolver == null ? nodeId : HashIdResolver(node));

            addedNodes[nodeId] = nodeInfo;
            nodeHash.AddNode(nodeInfo, node.ReplicaCount);

            return(nodeInfo);
        }
        /** <inheritdoc /> */
        public void OnNodeRemoved(IGridClientNode node)
        {
            // Cleans up node, removed from the topology.

            Guid     nodeId = node.Id;
            NodeInfo nodeInfo;

            if (!addedNodes.TryGetValue(nodeId, out nodeInfo))
            {
                return;
            }

            if (!addedNodes.Remove(nodeId))
            {
                return;
            }

            nodeHash.RemoveNode(nodeInfo);
        }
Exemplo n.º 4
0
        /**
         * <summary>
         * Gets active communication facade.</summary>
         *
         * <param name="node">Remote node to which connection should be established.</param>
         * <returns>Communication facade.</returns>
         * <exception cref="GridClientServerUnreachableException">If none of the servers can be reached after the exception.</exception>
         * <exception cref="GridClientClosedException">If client was closed manually.</exception>
         */
        public C connection(IGridClientNode node)
        {
            // Use router's connections if defined.
            if (routers.Count != 0)
            {
                return(connection(routers));
            }

            // Use node's connection, if node is available over rest.
            var srvs = node.AvailableAddresses(proto);

            if (srvs.Count == 0)
            {
                throw new GridClientServerUnreachableException("No available endpoints to connect " +
                                                               "(is rest enabled for this node?): " + node);
            }

            return(connection(srvs));
        }
Exemplo n.º 5
0
        /**
         * <summary>
         * Handles communication connection failure. Tries to reconnect to any of the servers specified and
         * throws an exception if none of the servers can be reached.</summary>
         *
         * <param name="node">Remote node for which this connection belongs to.</param>
         * <param name="conn">Facade that caused the exception.</param>
         * <param name="e">Exception.</param>
         */
        public void onFacadeFailed(IGridClientNode node, C conn, GridClientConnectionResetException e)
        {
            Dbg.WriteLine("Connection with remote node was terminated" +
                          "[node=" + node + ", srvAddr=" + conn.ServerAddress + ", errMsg=" + e.Message + ']');

            guard.AcquireWriterLock(Timeout.Infinite);

            try {
                IPEndPoint srv = conn.ServerAddress;
                C          cached;

                if (conns.TryGetValue(srv, out cached) && cached.Equals(conn))
                {
                    conns.Remove(srv);
                }
            }
            finally {
                guard.ReleaseWriterLock();
            }

            /* Close connection to failed node outside the writer lock. */
            closeSilent(conn, false);
        }
Exemplo n.º 6
0
        /** <inheritdoc /> */
        public IGridClientNode BalancedNode <TNode>(ICollection <TNode> nodes) where TNode : IGridClientNode
        {
            IDictionary <Guid, IGridClientNode> lookup = new Dictionary <Guid, IGridClientNode>(nodes.Count);

            foreach (IGridClientNode node in nodes)
            {
                lookup.Add(node.Id, node);
            }

            lock (nodeQueue) {
                IGridClientNode balanced = null;


                foreach (Guid nodeId in nodeQueue)
                {
                    balanced = lookup[nodeId];

                    if (balanced != null)
                    {
                        nodeQueue.Remove(nodeId);

                        break;
                    }
                }

                if (balanced != null)
                {
                    nodeQueue.AddLast(balanced.Id);

                    return(balanced);
                }

                throw new GridClientServerUnreachableException("Failed to get balanced node (topology does not have alive " +
                                                               "nodes): " + nodes);
            }
        }
 /** <inheritdoc /> */
 public void OnNodeAdded(IGridClientNode node)
 {
     // No-op.
 }
        /**
         * <summary>
         * Handles communication connection failure. Tries to reconnect to any of the servers specified and
         * throws an exception if none of the servers can be reached.</summary>
         *
         * <param name="node">Remote node for which this connection belongs to.</param>
         * <param name="conn">Facade that caused the exception.</param>
         * <param name="e">Exception.</param>
         */
        public void onFacadeFailed(IGridClientNode node, C conn, GridClientConnectionResetException e)
        {
            Dbg.WriteLine("Connection with remote node was terminated" +
                "[node=" + node + ", srvAddr=" + conn.ServerAddress + ", errMsg=" + e.Message + ']');

            guard.AcquireWriterLock(Timeout.Infinite);

            try {
                IPEndPoint srv = conn.ServerAddress;
                C cached;

                if (conns.TryGetValue(srv, out cached) && cached.Equals(conn))
                    conns.Remove(srv);
            }
            finally {
                guard.ReleaseWriterLock();
            }

            /* Close connection to failed node outside the writer lock. */
            closeSilent(conn, false);
        }
        /**
         * <summary>
         * Gets active communication facade.</summary>
         *
         * <param name="node">Remote node to which connection should be established.</param>
         * <returns>Communication facade.</returns>
         * <exception cref="GridClientServerUnreachableException">If none of the servers can be reached after the exception.</exception>
         * <exception cref="GridClientClosedException">If client was closed manually.</exception>
         */
        public C connection(IGridClientNode node)
        {
            // Use router's connections if defined.
            if (routers.Count != 0)
                return connection(routers);

            // Use node's connection, if node is available over rest.
            var srvs = node.AvailableAddresses(proto);

            if (srvs.Count == 0) {
                throw new GridClientServerUnreachableException("No available endpoints to connect " +
                    "(is rest enabled for this node?): " + node);
            }

            return connection(srvs);
        }
Exemplo n.º 10
0
        public static void Main()
        {
            /* Enable debug messages. */
            //Debug.Listeners.Add(new TextWriterTraceListener(System.Console.Out));

            String taskName = "org.gridgain.examples.misc.client.api.ClientExampleTask";
            String taskArg  = ".NET client - ";

            IGridClient client = CreateClient();

            try {
                // Show grid topology.
                X.WriteLine(">>> Client created, current grid topology: " + ToString(client.Compute().Nodes()));

                // Random node ID.
                Guid randNodeId = client.Compute().Nodes()[0].Id;

                // Note that in this example we get a fixed projection for task call because we cannot guarantee that
                // other nodes contain ClientExampleTask in classpath.
                IGridClientCompute prj = client.Compute().Projection(delegate(IGridClientNode node) {
                    return(node.Id.Equals(randNodeId));
                });

                // Execute test task that will count total number of nodes in grid.
                int entryCnt = prj.Execute <int>(taskName, taskArg + "predicate projection");

                X.WriteLine(">>> Predicate projection : there are totally " + entryCnt + " nodes in the grid");

                // Same as above, using different projection API.
                IGridClientNode clntNode = prj.Node(randNodeId);

                prj = prj.Projection(clntNode);

                entryCnt = prj.Execute <int>(taskName, taskArg + "node projection");

                X.WriteLine(">>> GridClientNode projection : there are totally " + entryCnt + " nodes in the grid");

                // Use of collections is also possible.
                prj = prj.Projection(new IGridClientNode[] { clntNode });

                entryCnt = prj.Execute <int>(taskName, taskArg + "nodes collection projection");

                X.WriteLine(">>> Collection projection : there are totally " + entryCnt + " nodes in the grid");

                // Balancing - may be random or round-robin. Users can create
                // custom load balancers as well.
                IGridClientLoadBalancer balancer = new GridClientRandomBalancer();

                // Balancer may be added to predicate or collection examples.
                prj = client.Compute().Projection(delegate(IGridClientNode node) {
                    return(node.Id.Equals(randNodeId));
                }, balancer);

                entryCnt = prj.Execute <int>(taskName, taskArg + "predicate projection with balancer");

                X.WriteLine(">>> Predicate projection with balancer : there are totally " + entryCnt +
                            " nodes in the grid");

                // Now let's try round-robin load balancer.
                balancer = new GridClientRoundRobinBalancer();

                prj = prj.Projection(new IGridClientNode[] { clntNode }, balancer);

                entryCnt = prj.Execute <int>(taskName, taskArg + "node projection with balancer");

                X.WriteLine(">>> GridClientNode projection : there are totally " + entryCnt + " nodes in the grid");

                // Execution may be asynchronous.
                IGridClientFuture <int> fut = prj.ExecuteAsync <int>(taskName, taskArg + "asynchronous execution");

                X.WriteLine(">>> Execute async : there are totally " + fut.Result + " nodes in the grid");

                // GridClientCompute can be queried for nodes participating in it.
                ICollection <IGridClientNode> c = prj.Nodes(new Guid[] { randNodeId });

                X.WriteLine(">>> Nodes with Guid " + randNodeId + " : " + ToString(c));

                // Nodes may also be filtered with predicate. Here
                // we create projection which only contains local node.
                c = prj.Nodes(delegate(IGridClientNode node) {
                    return(node.Id.Equals(randNodeId));
                });

                X.WriteLine(">>> Nodes filtered with predicate : " + ToString(c));

                // Information about nodes may be refreshed explicitly.
                clntNode = prj.RefreshNode(randNodeId, true, true);

                X.WriteLine(">>> Refreshed node : " + clntNode);

                // As usual, there's also an asynchronous version.
                IGridClientFuture <IGridClientNode> futClntNode = prj.RefreshNodeAsync(randNodeId, false, false);

                X.WriteLine(">>> Refreshed node asynchronously : " + futClntNode.Result);

                // Nodes may also be refreshed by IP address.
                String clntAddr = "127.0.0.1";

                foreach (var addr in clntNode.AvailableAddresses(GridClientProtocol.Tcp))
                {
                    if (addr != null)
                    {
                        clntAddr = addr.Address.ToString();
                    }
                }

                // Force node metrics refresh (by default it happens periodically in the background).
                clntNode = prj.RefreshNode(clntAddr, true, true);

                X.WriteLine(">>> Refreshed node by IP : " + clntNode);

                // Asynchronous version.
                futClntNode = prj.RefreshNodeAsync(clntAddr, false, false);

                X.WriteLine(">>> Refreshed node by IP asynchronously : " + futClntNode.Result);

                // Topology as a whole may be refreshed, too.
                ICollection <IGridClientNode> top = prj.RefreshTopology(true, true);

                X.WriteLine(">>> Refreshed topology : " + ToString(top));

                // Asynchronous version.
                IGridClientFuture <IList <IGridClientNode> > topFut = prj.RefreshTopologyAsync(false, false);

                X.WriteLine(">>> Refreshed topology asynchronously : " + ToString(topFut.Result));
            }
            catch (GridClientException e) {
                Console.WriteLine("Unexpected grid client exception happens: {0}", e);
            }
            finally {
                GridClientFactory.StopAll();
            }
        }
Exemplo n.º 11
0
 /**
  * <summary>
  * Gets active communication facade.</summary>
  *
  * <param name="node">Remote node to which connection should be established.</param>
  * <returns>Communication facade.</returns>
  * <exception cref="GridClientServerUnreachableException">If none of the servers can be reached after the exception.</exception>
  * <exception cref="GridClientClosedException">If client was closed manually.</exception>
  */
 public C connection(IGridClientNode node)
 {
     return connection(node.AvailableAddresses(proto));
 }
Exemplo n.º 12
0
 /** <inheritdoc /> */
 public void OnNodeRemoved(IGridClientNode node)
 {
     lock (nodeQueue) {
         nodeQueue.Remove(node.Id);
     }
 }
Exemplo n.º 13
0
 /** <inheritdoc /> */
 public void OnNodeAdded(IGridClientNode node)
 {
     lock (nodeQueue) {
         nodeQueue.AddFirst(node.Id);
     }
 }