/** <inheritdoc/> */
        override public IGridClientFuture <T> Execute <T>(String taskName, Object arg, Guid destNodeId)
        {
            GridClientTaskRequest msg = new GridClientTaskRequest(destNodeId);

            msg.TaskName = taskName;
            msg.Argument = arg;

            IGridClientFuture <GridClientTaskResultBean> fut = makeRequest <GridClientTaskResultBean>(msg);

            return(new GridClientFinishedFuture <T>(() => (T)fut.Result.Result));
        }
Example #2
0
        /** <inheritdoc /> */
        public virtual IGridClientFuture <TVal> CacheGet <TKey, TVal>(String cacheName, ISet <GridClientCacheFlag> cacheFlags, TKey key, Guid destNodeId)
        {
            IGridClientFuture <IDictionary <TKey, TVal> > res = CacheGetAll <TKey, TVal>(cacheName, cacheFlags, new TKey[] { key }, destNodeId);

            return(new GridClientFinishedFuture <TVal>(() => {
                TVal val;

                res.Result.TryGetValue(key, out val);

                return val;
            }));
        }
        public static void Main()
        {
            /* Enable debug messages. */
            //Debug.Listeners.Add(new TextWriterTraceListener(System.Console.Out));

            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;

                // Get client projection of grid partitioned cache.
                IGridClientData rmtCache = client.Data("partitioned");

                IList <String> keys = new List <String>(KeysCount);

                // Put some values to the cache.
                for (int i = 0; i < KeysCount; i++)
                {
                    String iKey = i + "";

                    // Put request will go exactly to the primary node for this key.
                    rmtCache.Put(iKey, "val-" + i);

                    Guid nodeId = rmtCache.Affinity(iKey);

                    X.WriteLine(">>> Storing key " + iKey + " on node " + nodeId);

                    keys.Add(iKey);
                }

                // Pin a remote node for communication. All further communication
                // on returned projection will happen through this pinned node.
                IGridClientData prj = rmtCache.PinNodes(client.Compute().Node(randNodeId));

                // Request batch from our local node in pinned mode.
                IDictionary <String, Object> vals = prj.GetAll <String, Object>(keys);

                foreach (KeyValuePair <String, Object> entry in vals)
                {
                    X.WriteLine(">>> Loaded cache entry [key=" + entry.Key + ", val=" + entry.Value + ']');
                }

                // After nodes are pinned the list of pinned nodes may be retrieved.
                X.WriteLine(">>> Pinned nodes: " + ToString(prj.PinnedNodes()));

                // Keys may be stored asynchronously.
                IGridClientFuture <Boolean> futPut = rmtCache.PutAsync("0", "new value for 0");

                X.WriteLine(">>> Result of asynchronous put: " + (futPut.Result ? "success" : "failure"));

                IDictionary <Guid, IDictionary <String, String> > keyVals = new Dictionary <Guid, IDictionary <String, String> >();

                // Batch puts are also supported.
                // Here we group key-value pairs by their affinity node ID to ensure
                // the least amount of network trips possible.
                for (int i = 0; i < KeysCount; i++)
                {
                    String iKey = i + "";

                    Guid nodeId = rmtCache.Affinity(iKey);

                    IDictionary <String, String> m;

                    if (!keyVals.TryGetValue(nodeId, out m))
                    {
                        keyVals.Add(nodeId, m = new Dictionary <String, String>());
                    }

                    m.Add(iKey, "val-" + i);
                }

                foreach (IDictionary <String, String> kvMap in keyVals.Values)
                {
                    // Affinity-aware bulk put operation - it will connect to the
                    // affinity node for provided keys.
                    rmtCache.PutAll(kvMap);
                }

                // Asynchronous batch put is available as well.
                ICollection <IGridClientFuture> futs = new LinkedList <IGridClientFuture>();

                foreach (IDictionary <String, String> kvMap in keyVals.Values)
                {
                    IGridClientFuture futPutAll = rmtCache.PutAllAsync(kvMap);

                    futs.Add(futPutAll);
                }

                // Wait for all futures to complete.
                foreach (IGridClientFuture fut in futs)
                {
                    fut.WaitDone();
                }

                // Of course there's getting value by key functionality.
                String key = 0 + "";

                X.WriteLine(">>> Value for key " + key + " is " + rmtCache.GetItem <String, Object>(key));

                // Asynchronous gets, too.
                IGridClientFuture <String> futVal = rmtCache.GetAsync <String, String>(key);

                X.WriteLine(">>> Asynchronous value for key " + key + " is " + futVal.Result);

                // Multiple values can be fetched at once. Here we batch our get
                // requests by affinity nodes to ensure least amount of network trips.
                foreach (KeyValuePair <Guid, IDictionary <String, String> > nodeEntry in keyVals)
                {
                    Guid nodeId = nodeEntry.Key;
                    ICollection <String> keyCol = nodeEntry.Value.Keys;

                    // Since all keys in our getAll(...) call are mapped to the same primary node,
                    // grid cache client will pick this node for the request, so we only have one
                    // network trip here.
                    X.WriteLine(">>> Values from node [nodeId=" + nodeId + ", values=" + ToString(rmtCache.GetAll <String, Object>(keyCol)) + ']');
                }

                // Multiple values may be retrieved asynchronously, too.
                // Here we retrieve all keys at ones. Since this request
                // will be sent to some grid node, this node may not be
                // the primary node for all keys and additional network
                // trips will have to be made within grid.
                IGridClientFuture <IDictionary <String, Object> > futVals = rmtCache.GetAllAsync <String, Object>(keys);

                X.WriteLine(">>> Asynchronous values for keys are " + ToString(futVals.Result));

                // Contents of cache may be removed one by one synchronously.
                // Again, this operation is affinity aware and only the primary
                // node for the key is contacted.
                bool res = rmtCache.Remove(0 + "");

                X.WriteLine(">>> Result of removal: " + (res ? "success" : "failure"));

                // ... and asynchronously.
                IGridClientFuture <Boolean> futRes = rmtCache.RemoveAsync(1 + "");

                X.WriteLine(">>> Result of asynchronous removal is: " + (futRes.Result ? "success" : "failure"));

                // Multiple entries may be removed at once synchronously...
                rmtCache.RemoveAll(new String[] { 2 + "", 3 + "" });

                // ... and asynchronously.
                IGridClientFuture futResAll = rmtCache.RemoveAllAsync(new String[] { 3 + "", 4 + "" });

                futResAll.WaitDone();

                // Values may also be replaced.
                res = rmtCache.Replace(0 + "", "new value for 0");

                X.WriteLine(">>> Result for replace for nonexistent key is " + (res ? "success" : "failure"));

                // Asynchronous replace is supported, too.
                futRes = rmtCache.ReplaceAsync("" + 0, "newest value for 0");

                X.WriteLine(">>> Result for asynchronous replace for nonexistent key is " +
                            (futRes.Result ? "success" : "failure"));

                // Compare and set are implemented, too.
                res = rmtCache.Cas("" + 0, "new value for 0", null);

                X.WriteLine(">>> Result for put using cas for key that didn't have value yet is " +
                            (res ? "success" : "failure"));

                // CAS can be asynchronous.
                futRes = rmtCache.CasAsync("" + 0, "newest value for 0", "new value for 0");

                X.WriteLine(">>> Result for put using asynchronous cas is " + (futRes.Result ? "success" : "failure"));

                // It's possible to obtain cache metrics using data client API.
                X.WriteLine(">>> Cache metrics : " + rmtCache.Metrics());

                // Global and per key metrics retrieval can be asynchronous, too.
                IGridClientFuture <IGridClientDataMetrics> futMetrics = rmtCache.MetricsAsync();

                X.WriteLine(">>> Cache asynchronous metrics : " + futMetrics.Result);
            }
            catch (GridClientException e) {
                Console.WriteLine("Unexpected grid client exception happens: {0}", e);
            }
            finally {
                GridClientFactory.StopAll();
            }
        }
Example #4
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();
            }
        }