// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements.  See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License.  You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
= Distributed Computing

Apache Ignite 3 provides an API for distributing computations across cluster nodes in a balanced and fault-tolerant manner. You can submit individual tasks for execution from Java and .NET clients.

You can use Java or .NET client to execute compute jobs. Make sure the required classes are deployed to the cluster before executing code.

Here is how you can execute a simple compute job:


[tabs]
--
tab:Java[]
[source, java]
----
private void example() {
    IgniteClient client = client();
    IgniteCompute compute = client.compute();
    Set<ClusterNode> nodes = new HashSet<>(client.clusterNodes());

    compute.execute(nodes, NodeNameJob.class, "Hello");
}

private static class NodeNameJob implements ComputeJob<String> {
    @Override
    public String execute(JobExecutionContext context, Object... args) {
        return context.ignite().name() + "_" + args[0];
    }
}
----


NOTE: Unlike Ignite 2, jobs are not serialized. Only the class name and arguments are sent to the node.

tab:.NET[]
[source, csharp]
----
IIgniteClient client = Client;
ICompute compute = client.Compute;
IList<IClusterNode> nodes = await Client.GetClusterNodesAsync();
string res = await compute.ExecuteAsync<string>(nodes, jobClassName: "org.foo.bar.NodeNameJob", "Hello!");
----
--


== Colocated Computations

In Apache Ignite 3 you can execute colocated computation with `executeColocated` method. When you do it, the compute task is guaranteed to be executed on the nodes that hold the specified key. This can significantly reduce execution time if your tasks require data.

In this example we will need a table to colocate:



[source, java]
----
executeSql("CREATE TABLE test (k int, v int, CONSTRAINT PK PRIMARY KEY (k))");
executeSql("INSERT INTO test(k, v) VALUES (1, 101)");
----

And we will execute a simple task:

----
class GetNodeNameJob implements ComputeJob<String> {
    @Override
    public String execute(JobExecutionContext context, Object... args) {
        return context.ignite().name();
    }
}
----


[tabs]
--
tab:Tuple[]
[source, java]
----
String actualNodeName = ignite.compute()
        .executeColocated("PUBLIC.test", Tuple.create(Map.of("k", 1)), GetNodeNameJob.class)
        .get(1, TimeUnit.SECONDS);

System.out.println(actualNodeName);
----

tab:Mapper[]
[source, java]
----
String actualNodeName = ignite.compute()
        .executeColocated("PUBLIC.test", 1, Mapper.of(Integer.class), GetNodeNameJob.class)
        .get(1, TimeUnit.SECONDS);

System.out.println(actualNodeName);
----
--