You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by ch...@apache.org on 2018/07/31 13:55:17 UTC

[1/2] ignite git commit: IGNITE-9034: [ML] Add Estimator API support to TensorFlow cluster on top of Apache Ignite.

Repository: ignite
Updated Branches:
  refs/heads/master c5e723c53 -> 9e884e5aa


http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/util/TensorFlowUserScriptRunner.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/util/TensorFlowUserScriptRunner.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/util/TensorFlowUserScriptRunner.java
new file mode 100644
index 0000000..3dcd5f8
--- /dev/null
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/util/TensorFlowUserScriptRunner.java
@@ -0,0 +1,236 @@
+/*
+ * 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.
+ */
+
+package org.apache.ignite.tensorflow.cluster.util;
+
+import java.io.BufferedOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.util.List;
+import java.util.Map;
+import java.util.StringJoiner;
+import java.util.concurrent.ExecutorService;
+import java.util.function.Consumer;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+import org.apache.commons.io.IOUtils;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.Ignition;
+import org.apache.ignite.tensorflow.cluster.TensorFlowJobArchive;
+import org.apache.ignite.tensorflow.cluster.spec.TensorFlowClusterSpec;
+import org.apache.ignite.tensorflow.cluster.spec.TensorFlowServerAddressSpec;
+import org.apache.ignite.tensorflow.core.util.AsyncNativeProcessRunner;
+import org.apache.ignite.tensorflow.core.util.NativeProcessRunner;
+
+/**
+ * Utils class that helps to start and stop user script process.
+ */
+public class TensorFlowUserScriptRunner extends AsyncNativeProcessRunner {
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** Job archive that will be extracted and used as working directory for the native process. */
+    private final TensorFlowJobArchive jobArchive;
+
+    /** TensorFlow cluster specification. */
+    private final TensorFlowClusterSpec clusterSpec;
+
+    /** Output stream data consumer. */
+    private final Consumer<String> out;
+
+    /** Error stream data consumer. */
+    private final Consumer<String> err;
+
+    /** Working directory of the user script process. */
+    private File workingDir;
+
+    /**
+     * Constructs a new instance of TensorFlow user script runner.
+     *
+     * @param ignite Ignite instance.
+     * @param executor Executor to be used in {@link AsyncNativeProcessRunner}.
+     * @param jobArchive  Job archive that will be extracted and used as working directory for the native process.
+     * @param clusterSpec TensorFlow cluster specification.
+     * @param out Output stream data consumer.
+     * @param err  Error stream data consumer.
+     */
+    public TensorFlowUserScriptRunner(Ignite ignite, ExecutorService executor, TensorFlowJobArchive jobArchive,
+        TensorFlowClusterSpec clusterSpec, Consumer<String> out, Consumer<String> err) {
+        super(ignite, executor);
+
+        this.log = ignite.log().getLogger(TensorFlowUserScriptRunner.class);
+
+        this.jobArchive = jobArchive;
+        this.clusterSpec = clusterSpec;
+        this.out = out;
+        this.err = err;
+    }
+
+    /** {@inheritDoc} */
+    @Override public NativeProcessRunner doBefore() {
+        try {
+            workingDir = Files.createTempDirectory("tf_us_").toFile();
+            log.debug("Directory has been created [path=" + workingDir.getAbsolutePath() + "]");
+
+            unzip(jobArchive.getData(), workingDir);
+            log.debug("Job archive has been extracted [path=" + workingDir.getAbsolutePath() + "]");
+
+            return prepareNativeProcessRunner();
+        }
+        catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void doAfter() {
+        if (workingDir != null) {
+            delete(workingDir);
+            log.debug("Directory has been deleted [path=" + workingDir.getAbsolutePath() + "]");
+        }
+    }
+
+    /**
+     * Prepares process builder and specifies working directory and command to be run.
+     *
+     * @return Prepared process builder.
+     */
+    private NativeProcessRunner prepareNativeProcessRunner() {
+        if (workingDir == null)
+            throw new IllegalStateException("Working directory is not created");
+
+        ProcessBuilder procBuilder = new ProcessBuilder();
+
+        procBuilder.directory(workingDir);
+        procBuilder.command(jobArchive.getCommands());
+
+        Map<String, String> env = procBuilder.environment();
+        env.put("PYTHONPATH", workingDir.getAbsolutePath());
+        env.put("TF_CONFIG", formatTfConfigVar());
+        env.put("TF_WORKERS", formatTfWorkersVar());
+        env.put("TF_CHIEF_SERVER", formatTfChiefServerVar());
+
+        return new NativeProcessRunner(procBuilder, null, out, err);
+    }
+
+    /**
+     * Formats "TF_CONFIG" variable to be passed into user script.
+     *
+     * @return Formatted "TF_CONFIG" variable to be passed into user script.
+     */
+    private String formatTfConfigVar() {
+        return new StringBuilder()
+            .append("{\"cluster\" : ")
+            .append(clusterSpec.format(Ignition.ignite()))
+            .append(", ")
+            .append("\"task\": {\"type\" : \"" + TensorFlowClusterResolver.CHIEF_JOB_NAME + "\", \"index\": 0}}")
+            .toString();
+    }
+
+    /**
+     * Formats "TF_WORKERS" variable to be passed into user script.
+     *
+     * @return Formatted "TF_WORKERS" variable to be passed into user script.
+     */
+    private String formatTfWorkersVar() {
+        StringJoiner joiner = new StringJoiner(", ");
+
+        int cnt = clusterSpec.getJobs().get(TensorFlowClusterResolver.WORKER_JOB_NAME).size();
+        for (int i = 0; i < cnt; i++)
+            joiner.add("\"/job:" + TensorFlowClusterResolver.WORKER_JOB_NAME + "/task:" + i + "\"");
+
+        return "[" + joiner + "]";
+    }
+
+    /**
+     * Formats "TF_CHIEF_SERVER" variable to be passed into user script.
+     *
+     * @return Formatted "TF_CHIEF_SERVER" variable to be passed into user script.
+     */
+    private String formatTfChiefServerVar() {
+        List<TensorFlowServerAddressSpec> tasks = clusterSpec.getJobs().get(TensorFlowClusterResolver.CHIEF_JOB_NAME);
+
+        if (tasks == null || tasks.size() != 1)
+            throw new IllegalStateException("TensorFlow cluster specification should contain exactly one chief task");
+
+        TensorFlowServerAddressSpec addrSpec = tasks.iterator().next();
+
+        return "grpc://" + addrSpec.format(Ignition.ignite());
+    }
+
+    /**
+     * Clears given file or directory recursively.
+     *
+     * @param file File or directory to be cleaned,
+     */
+    private void delete(File file) {
+        if (file.isDirectory()) {
+            String[] files = file.list();
+
+            if (files != null && files.length != 0)
+                for (String fileToBeDeleted : files)
+                    delete(new File(file, fileToBeDeleted));
+
+            if (!file.delete())
+                throw new IllegalStateException("Can't delete directory [path=" + file.getAbsolutePath() + "]");
+        }
+        else {
+            if (!file.delete())
+                throw new IllegalStateException("Can't delete file [path=" + file.getAbsolutePath() + "]");
+        }
+    }
+
+    /**
+     * Extracts specified zip archive into specified directory.
+     *
+     * @param data Zip archive to be extracted.
+     * @param extractTo Target directory.
+     */
+    private void unzip(byte[] data, File extractTo) {
+        try (ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(data))) {
+            ZipEntry entry;
+            while ((entry = zipStream.getNextEntry()) != null) {
+                File file = new File(extractTo, entry.getName());
+
+                if (entry.isDirectory() && !file.exists()) {
+                    boolean created = file.mkdirs();
+                    if (!created)
+                        throw new IllegalStateException("Can't create directory [path=" + file.getAbsolutePath() + "]");
+                }
+                else {
+                    if (!file.getParentFile().exists()) {
+                        boolean created = file.getParentFile().mkdirs();
+                        if (!created)
+                            throw new IllegalStateException("Can't create directory [path=" +
+                                file.getParentFile().getAbsolutePath() + "]");
+                    }
+
+                    try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file))) {
+                        IOUtils.copy(zipStream, out);
+                    }
+                }
+            }
+        }
+        catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/ProcessManager.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/ProcessManager.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/ProcessManager.java
index 0ef81bc..c825448 100644
--- a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/ProcessManager.java
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/ProcessManager.java
@@ -17,18 +17,17 @@
 
 package org.apache.ignite.tensorflow.core;
 
-import org.apache.ignite.tensorflow.core.longrunning.task.util.LongRunningProcessStatus;
-import java.io.Serializable;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
+import org.apache.ignite.tensorflow.core.longrunning.task.util.LongRunningProcessStatus;
 
 /**
  * Process manager that allows to run and maintain processes in the cluster.
  *
  * @param <R> Type of task to be run.
  */
-public interface ProcessManager<R> extends Serializable {
+public interface ProcessManager<R> {
     /**
      * Starts the processes by the given specifications.
      *

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/ProcessManagerWrapper.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/ProcessManagerWrapper.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/ProcessManagerWrapper.java
index b66b54f..4f10e83 100644
--- a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/ProcessManagerWrapper.java
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/ProcessManagerWrapper.java
@@ -17,11 +17,11 @@
 
 package org.apache.ignite.tensorflow.core;
 
-import org.apache.ignite.tensorflow.core.longrunning.task.util.LongRunningProcessStatus;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
+import org.apache.ignite.tensorflow.core.longrunning.task.util.LongRunningProcessStatus;
 
 /**
  * Process manager wrapper that allows to define how one type of process specification should be transformed into
@@ -31,9 +31,6 @@ import java.util.UUID;
  * @param <R> Type of accepted process specifications.
  */
 public abstract class ProcessManagerWrapper<T, R> implements ProcessManager<R> {
-    /** */
-    private static final long serialVersionUID = -6397225095261457524L;
-
     /** Delegate. */
     private final ProcessManager<T> delegate;
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/longrunning/LongRunningProcessManager.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/longrunning/LongRunningProcessManager.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/longrunning/LongRunningProcessManager.java
index 027ece3..a25ff97 100644
--- a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/longrunning/LongRunningProcessManager.java
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/longrunning/LongRunningProcessManager.java
@@ -17,7 +17,6 @@
 
 package org.apache.ignite.tensorflow.core.longrunning;
 
-import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -25,7 +24,6 @@ import java.util.Map;
 import java.util.UUID;
 import java.util.function.Consumer;
 import java.util.function.Function;
-import java.util.function.Supplier;
 import org.apache.ignite.Ignite;
 import org.apache.ignite.cluster.ClusterGroup;
 import org.apache.ignite.cluster.ClusterGroupEmptyException;
@@ -41,22 +39,18 @@ import org.apache.ignite.tensorflow.core.longrunning.task.util.LongRunningProces
  * Long running process manager that allows to start, stop and make other actions with long running processes.
  */
 public class LongRunningProcessManager implements ProcessManager<LongRunningProcess> {
-    /** */
-    private static final long serialVersionUID = 1151455641358063287L;
-
-    /** Ignite instance supplier. */
-    private final Supplier<Ignite> igniteSupplier;
+    /** Ignite instance. */
+    private final Ignite ignite;
 
     /**
      * Constructs a new instance of long running process manager.
      *
-     * @param igniteSupplier Ignite instance supplier.
-     * @param <T> Type of serializable supplier.
+     * @param ignite Ignite instance.
      */
-    public <T extends Supplier<Ignite> & Serializable> LongRunningProcessManager(T igniteSupplier) {
-        assert igniteSupplier != null : "Ignite supplier should not be null";
+    public LongRunningProcessManager(Ignite ignite) {
+        assert ignite != null : "Ignite instance should not be null";
 
-        this.igniteSupplier = igniteSupplier;
+        this.ignite = ignite;
     }
 
     /** {@inheritDoc} */
@@ -100,7 +94,6 @@ public class LongRunningProcessManager implements ProcessManager<LongRunningProc
                 List<T> nodeProcesses = params.get(nodeId);
                 LongRunningProcessTask<List<E>> task = taskSupplier.apply(nodeProcesses);
 
-                Ignite ignite = igniteSupplier.get();
                 ClusterGroup clusterGrp = ignite.cluster().forNodeId(nodeId);
 
                 try {

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/longrunning/task/LongRunningProcessStartTask.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/longrunning/task/LongRunningProcessStartTask.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/longrunning/task/LongRunningProcessStartTask.java
index 1d08519..04f90d3 100644
--- a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/longrunning/task/LongRunningProcessStartTask.java
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/longrunning/task/LongRunningProcessStartTask.java
@@ -17,13 +17,13 @@
 
 package org.apache.ignite.tensorflow.core.longrunning.task;
 
-import org.apache.ignite.tensorflow.core.longrunning.LongRunningProcess;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
+import org.apache.ignite.tensorflow.core.longrunning.LongRunningProcess;
 import org.apache.ignite.tensorflow.core.util.CustomizableThreadFactory;
 
 /**
@@ -78,7 +78,7 @@ public class LongRunningProcessStartTask extends LongRunningProcessTask<List<UUI
      */
     private Future<?> runTask(Runnable task) {
         return Executors
-            .newSingleThreadExecutor(new CustomizableThreadFactory("LONG_RUNNING_PROCESS_TASK", true))
+            .newSingleThreadExecutor(new CustomizableThreadFactory("tf-long-running", true))
             .submit(task);
     }
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/nativerunning/NativeProcess.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/nativerunning/NativeProcess.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/nativerunning/NativeProcess.java
index df36ba9..2ad3c9d 100644
--- a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/nativerunning/NativeProcess.java
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/nativerunning/NativeProcess.java
@@ -20,6 +20,7 @@ package org.apache.ignite.tensorflow.core.nativerunning;
 import java.io.Serializable;
 import java.util.UUID;
 import java.util.function.Supplier;
+import org.apache.ignite.tensorflow.util.SerializableSupplier;
 
 /**
  * Native process specification.
@@ -29,7 +30,7 @@ public class NativeProcess implements Serializable {
     private static final long serialVersionUID = -7056800139746134956L;
 
     /** Process builder supplier. */
-    private final Supplier<ProcessBuilder> procBuilderSupplier;
+    private final SerializableSupplier<ProcessBuilder> procBuilderSupplier;
 
     /** Stdin of the process. */
     private final String stdin;
@@ -44,8 +45,7 @@ public class NativeProcess implements Serializable {
      * @param stdin Stdin of the process.
      * @param nodeId Node identifier.
      */
-    public <T extends Supplier<ProcessBuilder> & Serializable> NativeProcess(T procBuilderSupplier, String stdin,
-        UUID nodeId) {
+    public NativeProcess(SerializableSupplier<ProcessBuilder> procBuilderSupplier, String stdin, UUID nodeId) {
         assert procBuilderSupplier != null : "Process builder supplier should not be null";
         assert nodeId != null : "Node identifier should not be null";
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/nativerunning/NativeProcessManager.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/nativerunning/NativeProcessManager.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/nativerunning/NativeProcessManager.java
index 60cd89b..5accf3d 100644
--- a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/nativerunning/NativeProcessManager.java
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/nativerunning/NativeProcessManager.java
@@ -17,8 +17,6 @@
 
 package org.apache.ignite.tensorflow.core.nativerunning;
 
-import java.io.Serializable;
-import java.util.function.Supplier;
 import org.apache.ignite.Ignite;
 import org.apache.ignite.tensorflow.core.ProcessManager;
 import org.apache.ignite.tensorflow.core.ProcessManagerWrapper;
@@ -30,17 +28,13 @@ import org.apache.ignite.tensorflow.core.nativerunning.task.NativeProcessStartTa
  * Native process manager that allows to start, stop and make other actions with native processes.
  */
 public class NativeProcessManager extends ProcessManagerWrapper<LongRunningProcess, NativeProcess> {
-    /** */
-    private static final long serialVersionUID = 718119807915504045L;
-
     /**
      * Constructs a new native process manager.
      *
-     * @param igniteSupplier Ignite instance supplier.
-     * @param <T> Type of serializable supplier.
+     * @param ignite Ignite instance.
      */
-    public <T extends Supplier<Ignite> & Serializable> NativeProcessManager(T igniteSupplier) {
-        super(new LongRunningProcessManager(igniteSupplier));
+    public NativeProcessManager(Ignite ignite) {
+        super(new LongRunningProcessManager(ignite));
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/nativerunning/task/NativeProcessStartTask.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/nativerunning/task/NativeProcessStartTask.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/nativerunning/task/NativeProcessStartTask.java
index 8fc28a5..ae9e2b9 100644
--- a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/nativerunning/task/NativeProcessStartTask.java
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/nativerunning/task/NativeProcessStartTask.java
@@ -18,16 +18,11 @@
 package org.apache.ignite.tensorflow.core.nativerunning.task;
 
 import java.util.function.Supplier;
-import org.apache.ignite.tensorflow.core.nativerunning.NativeProcess;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.PrintStream;
-import java.io.PrintWriter;
-import java.util.Scanner;
-import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.Ignition;
 import org.apache.ignite.lang.IgniteRunnable;
-import org.apache.ignite.tensorflow.core.util.CustomizableThreadFactory;
+import org.apache.ignite.tensorflow.core.nativerunning.NativeProcess;
+import org.apache.ignite.tensorflow.core.util.NativeProcessRunner;
 
 /**
  * Task that starts native process by its specification.
@@ -55,62 +50,26 @@ public class NativeProcessStartTask implements IgniteRunnable {
         Supplier<ProcessBuilder> procBuilderSupplier = procSpec.getProcBuilderSupplier();
         ProcessBuilder procBuilder = procBuilderSupplier.get();
 
-        Process proc;
-        try {
-            proc = procBuilder.start();
-        }
-        catch (IOException e) {
-            throw new RuntimeException(e);
-        }
-
-        Thread shutdownHook = new Thread(proc::destroy);
-        Runtime.getRuntime().addShutdownHook(shutdownHook);
+        NativeProcessRunner procRunner = new NativeProcessRunner(
+            procBuilder,
+            procSpec.getStdin(),
+            System.out::println,
+            System.err::println
+        );
 
-        Future<?> outForward = forwardStream(proc.getInputStream(), System.out);
-        Future<?> errForward = forwardStream(proc.getErrorStream(), System.err);
+        IgniteLogger log = Ignition.ignite().log().getLogger(NativeProcessStartTask.class);
 
         try {
-            if (procSpec.getStdin() != null) {
-                PrintWriter writer = new PrintWriter(proc.getOutputStream());
-                writer.println(procSpec.getStdin());
-                writer.flush();
-            }
-
-            int status;
-            try {
-                status = proc.waitFor();
-            }
-            catch (InterruptedException e) {
-                proc.destroy();
-                status = proc.exitValue();
-            }
-
-            Runtime.getRuntime().removeShutdownHook(shutdownHook);
-
-            if (status != 0)
-                throw new IllegalStateException("Native process exit status is " + status);
+            log.debug("Starting native process");
+            procRunner.startAndWait();
+            log.debug("Native process completed");
         }
-        finally {
-            outForward.cancel(true);
-            errForward.cancel(true);
+        catch (InterruptedException e) {
+            log.debug("Native process interrupted");
+        }
+        catch (Exception e) {
+            log.error("Native process failed", e);
+            throw e;
         }
-    }
-
-    /**
-     * Forwards stream.
-     *
-     * @param src Source stream.
-     * @param dst Destination stream.
-     * @return Future that allows to interrupt forwarding.
-     */
-    private Future<?> forwardStream(InputStream src, PrintStream dst) {
-        return Executors
-            .newSingleThreadExecutor(new CustomizableThreadFactory("NATIVE_PROCESS_FORWARD_STREAM", true))
-            .submit(() -> {
-                Scanner scanner = new Scanner(src);
-
-                while (!Thread.currentThread().isInterrupted() && scanner.hasNextLine())
-                    dst.println(scanner.nextLine());
-            });
     }
 }

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/pythonrunning/PythonProcessBuilderSupplier.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/pythonrunning/PythonProcessBuilderSupplier.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/pythonrunning/PythonProcessBuilderSupplier.java
new file mode 100644
index 0000000..e59ab00
--- /dev/null
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/pythonrunning/PythonProcessBuilderSupplier.java
@@ -0,0 +1,57 @@
+/*
+ * 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.
+ */
+
+package org.apache.ignite.tensorflow.core.pythonrunning;
+
+import org.apache.ignite.tensorflow.util.SerializableSupplier;
+
+/**
+ * Python process builder supplier that is used to create Python process builder.
+ */
+public class PythonProcessBuilderSupplier implements SerializableSupplier<ProcessBuilder> {
+    /** */
+    private static final long serialVersionUID = 7181937306294456125L;
+
+    /** Python environment variable name. */
+    private static final String PYTHON_ENV_NAME = "PYTHON";
+
+    /** Interactive flag (allows to used standard input to pass Python script). */
+    private final boolean interactive;
+
+    /**
+     * Constructs a new instance of Python process builder supplier.
+     *
+     * @param interactive Interactive flag (allows to used standard input to pass Python script).
+     */
+    public PythonProcessBuilderSupplier(boolean interactive) {
+        this.interactive = interactive;
+    }
+
+    /**
+     * Returns process builder to be used to start Python process.
+     *
+     * @return Process builder to be used to start Python process.
+     */
+    public ProcessBuilder get() {
+        String python = System.getenv(PYTHON_ENV_NAME);
+
+        if (python == null)
+            python = "python3";
+
+        return interactive ? new ProcessBuilder(python, "-i") : new ProcessBuilder(python);
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/pythonrunning/PythonProcessManager.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/pythonrunning/PythonProcessManager.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/pythonrunning/PythonProcessManager.java
index de35ff9..1f6c11e 100644
--- a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/pythonrunning/PythonProcessManager.java
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/pythonrunning/PythonProcessManager.java
@@ -17,8 +17,6 @@
 
 package org.apache.ignite.tensorflow.core.pythonrunning;
 
-import java.io.Serializable;
-import java.util.function.Supplier;
 import org.apache.ignite.Ignite;
 import org.apache.ignite.tensorflow.core.ProcessManager;
 import org.apache.ignite.tensorflow.core.ProcessManagerWrapper;
@@ -29,17 +27,13 @@ import org.apache.ignite.tensorflow.core.nativerunning.NativeProcessManager;
  * Python process manager that allows to  start, stop and make other actions with python processes.
  */
 public class PythonProcessManager extends ProcessManagerWrapper<NativeProcess, PythonProcess> {
-    /** */
-    private static final long serialVersionUID = -7095409565854538504L;
-
     /**
      * Constructs a new instance of python process manager.
      *
-     * @param igniteSupplier Ignite instance supplier.
-     * @param <T> Type of serializable supplier.
+     * @param ignite Ignite instance.
      */
-    public <T extends Supplier<Ignite> & Serializable> PythonProcessManager(T igniteSupplier) {
-        this(new NativeProcessManager(igniteSupplier));
+    public PythonProcessManager(Ignite ignite) {
+        this(new NativeProcessManager(ignite));
     }
 
     /**
@@ -54,30 +48,9 @@ public class PythonProcessManager extends ProcessManagerWrapper<NativeProcess, P
     /** {@inheritDoc} */
     @Override protected NativeProcess transformSpecification(PythonProcess spec) {
         return new NativeProcess(
-            new PythonProcessBuilderSupplier(),
+            new PythonProcessBuilderSupplier(true),
             spec.getStdin(),
             spec.getNodeId()
         );
     }
-
-    /**
-     * Python process builder supplier that is used to create Python process builder.
-     */
-    private static class PythonProcessBuilderSupplier implements Supplier<ProcessBuilder>, Serializable {
-        /** */
-        private static final long serialVersionUID = 8497087649461965914L;
-
-        /** Python environment variable name. */
-        private static final String PYTHON_ENV_NAME = "PYTHON";
-
-        /** {@inheritDoc} */
-        @Override public ProcessBuilder get() {
-            String python = System.getenv(PYTHON_ENV_NAME);
-
-            if (python == null)
-                python = "python3";
-
-            return new ProcessBuilder(python, "-i");
-        }
-    }
 }

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/util/AsyncNativeProcessRunner.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/util/AsyncNativeProcessRunner.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/util/AsyncNativeProcessRunner.java
new file mode 100644
index 0000000..b336b97
--- /dev/null
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/util/AsyncNativeProcessRunner.java
@@ -0,0 +1,107 @@
+/*
+ * 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.
+ */
+
+package org.apache.ignite.tensorflow.core.util;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteLogger;
+
+/**
+ * Asynchronous native process runner.
+ */
+public abstract class AsyncNativeProcessRunner {
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** Executors that is used to start async native process. */
+    private final ExecutorService executor;
+
+    /** Future of the async process process. */
+    private Future<?> fut;
+
+    /**
+     * Constructs a new asynchronous native process runner.
+     *
+     * @param ignite Ignite instance.
+     * @param executor Executor.
+     */
+    public AsyncNativeProcessRunner(Ignite ignite, ExecutorService executor) {
+        this.log = ignite.log().getLogger(AsyncNativeProcessRunner.class);
+        this.executor = executor;
+    }
+
+    /**
+     * Method that should be called before starting the process.
+     *
+     * @return Prepared native process runner.
+     */
+    public abstract NativeProcessRunner doBefore();
+
+    /**
+     * Method that should be called after starting the process.
+     */
+    public abstract void doAfter();
+
+    /**
+     * Starts the process in separate thread.
+     */
+    public synchronized void start() {
+        if (fut != null)
+            throw new IllegalStateException("Async native process has already been started");
+
+        NativeProcessRunner procRunner = doBefore();
+
+        fut = executor.submit(() -> {
+            while (!Thread.currentThread().isInterrupted()) {
+                try {
+                    log.debug("Starting native process");
+                    procRunner.startAndWait();
+                    log.debug("Native process completed");
+                    break;
+                }
+                catch (InterruptedException e) {
+                    log.debug("Native process interrupted");
+                    break;
+                }
+                catch (Exception e) {
+                    log.error("Native process failed", e);
+                }
+            }
+
+            doAfter();
+        });
+    }
+
+    /**
+     * Stops the process.
+     */
+    public synchronized void stop() {
+        if (fut != null && !fut.isDone())
+            fut.cancel(true);
+    }
+
+    /**
+     * Checks if process is already completed.
+     *
+     * @return {@code true} if process completed, otherwise {@code false}.
+     */
+    public boolean isCompleted() {
+        return fut != null && fut.isDone();
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/util/NativeProcessRunner.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/util/NativeProcessRunner.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/util/NativeProcessRunner.java
new file mode 100644
index 0000000..38af26d
--- /dev/null
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/core/util/NativeProcessRunner.java
@@ -0,0 +1,133 @@
+/*
+ * 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.
+ */
+
+package org.apache.ignite.tensorflow.core.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.util.Scanner;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Consumer;
+
+/**
+ * Utils class that helps to start native processes.
+ */
+public class NativeProcessRunner {
+    /** Thread name to be used by threads that forward streams. */
+    private static final String NATIVE_PROCESS_FORWARD_STREAM_THREAD_NAME = "tf-forward-native-output";
+
+    /** Process builder. */
+    private final ProcessBuilder procBuilder;
+
+    /** Standard input of the process. */
+    private final String stdin;
+
+    /** Output stream data consumer. */
+    private final Consumer<String> out;
+
+    /** Error stream data consumer. */
+    private final Consumer<String> err;
+
+    /**
+     * Constructs a new instance of native process runner.
+     *
+     * @param procBuilder Process builder.
+     * @param stdin Standard input of the process.
+     * @param out Output stream data consumer.
+     * @param err Error stream data consumer.
+     */
+    public NativeProcessRunner(ProcessBuilder procBuilder, String stdin, Consumer<String> out, Consumer<String> err) {
+        this.procBuilder = procBuilder;
+        this.stdin = stdin;
+        this.out = out;
+        this.err = err;
+    }
+
+    /**
+     * Starts the native process and waits it to be completed successfully or with exception.
+     */
+    public void startAndWait() throws InterruptedException {
+        Process proc;
+        try {
+            proc = procBuilder.start();
+        }
+        catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+
+        AtomicBoolean shutdown = new AtomicBoolean();
+
+        Thread shutdownHook = new Thread(() -> {
+            shutdown.set(true);
+            proc.destroy();
+        });
+
+        Runtime.getRuntime().addShutdownHook(shutdownHook);
+
+        Future<?> outForward = forwardStream(proc.getInputStream(), out);
+        Future<?> errForward = forwardStream(proc.getErrorStream(), err);
+
+        try {
+            if (stdin != null) {
+                PrintWriter writer = new PrintWriter(proc.getOutputStream());
+                writer.println(stdin);
+                writer.flush();
+            }
+
+            int status;
+            try {
+                status = proc.waitFor();
+            }
+            catch (InterruptedException e) {
+                proc.destroy();
+                throw e;
+            }
+
+            if (!shutdown.get()) {
+                Runtime.getRuntime().removeShutdownHook(shutdownHook);
+
+                if (status != 0)
+                    throw new IllegalStateException("Native process exit [status=" + status + "]");
+            }
+        }
+        finally {
+            outForward.cancel(true);
+            errForward.cancel(true);
+        }
+    }
+
+    /**
+     * Forwards stream.
+     *
+     * @param src Source stream.
+     * @param dst Destination stream.
+     * @return Future that allows to interrupt forwarding.
+     */
+    private Future<?> forwardStream(InputStream src, Consumer<String> dst) {
+        return Executors
+            .newSingleThreadExecutor(new CustomizableThreadFactory(NATIVE_PROCESS_FORWARD_STREAM_THREAD_NAME, true))
+            .submit(() -> {
+                Scanner scanner = new Scanner(src);
+
+                while (!Thread.currentThread().isInterrupted() && scanner.hasNextLine())
+                    dst.accept(scanner.nextLine());
+            });
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/JobSubmitter.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/JobSubmitter.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/JobSubmitter.java
new file mode 100644
index 0000000..0a7cae6
--- /dev/null
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/JobSubmitter.java
@@ -0,0 +1,35 @@
+/*
+ * 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.
+ */
+
+package org.apache.ignite.tensorflow.submitter;
+
+import org.apache.ignite.tensorflow.submitter.command.RootCommand;
+import picocli.CommandLine;
+
+/**
+ * Main class of the job submitter application that allows to submit TensorFlow jobs to be run within Ignite cluster.
+ */
+public class JobSubmitter {
+    /**
+     * Main method.
+     *
+     * @param args Arguments.
+     */
+    public static void main(String... args) {
+        CommandLine.run(new RootCommand(), System.out, args);
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/command/AbstractCommand.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/command/AbstractCommand.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/command/AbstractCommand.java
new file mode 100644
index 0000000..4d2fc18
--- /dev/null
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/command/AbstractCommand.java
@@ -0,0 +1,55 @@
+/*
+ * 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.
+ */
+
+package org.apache.ignite.tensorflow.submitter.command;
+
+import org.apache.ignite.Ignite;
+import org.apache.ignite.Ignition;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.logger.slf4j.Slf4jLogger;
+import picocli.CommandLine;
+
+/**
+ * Abstract command that contains options common for all commands.
+ */
+public abstract class AbstractCommand implements Runnable {
+    /** Ignite node configuration path. */
+    @CommandLine.Option(names = { "-c", "--config" }, description = "Apache Ignite client configuration.")
+    protected String cfg;
+
+    /**
+     * Returns Ignite instance based on configuration specified in {@link #cfg} field.
+     *
+     * @return Ignite instance.
+     */
+    protected Ignite getIgnite() {
+        if (cfg != null)
+            return Ignition.start(cfg);
+        else {
+            IgniteConfiguration igniteCfg = new IgniteConfiguration();
+            igniteCfg.setGridLogger(new Slf4jLogger());
+            igniteCfg.setClientMode(true);
+
+            return Ignition.start(igniteCfg);
+        }
+    }
+
+    /** */
+    public void setCfg(String cfg) {
+        this.cfg = cfg;
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/command/AttachCommand.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/command/AttachCommand.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/command/AttachCommand.java
new file mode 100644
index 0000000..946aa08
--- /dev/null
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/command/AttachCommand.java
@@ -0,0 +1,51 @@
+/*
+ * 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.
+ */
+
+package org.apache.ignite.tensorflow.submitter.command;
+
+import java.util.UUID;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.tensorflow.cluster.TensorFlowClusterGatewayManager;
+import picocli.CommandLine;
+
+/**
+ * Command "attach" that is used to attach to running TensorFlow cluster and receive output of the user script.
+ */
+@CommandLine.Command(
+    name = "attach",
+    description = "Attaches to running TensorFlow cluster (user script process).",
+    mixinStandardHelpOptions = true
+)
+public class AttachCommand extends AbstractCommand {
+    /** TensorFlow cluster identifier. */
+    @CommandLine.Parameters(paramLabel = "CLUSTER_ID", description = "Cluster identifier.")
+    private UUID clusterId;
+
+    /** {@inheritDoc} */
+    @Override public void run() {
+        try (Ignite ignite = getIgnite()) {
+            TensorFlowClusterGatewayManager mgr = new TensorFlowClusterGatewayManager(ignite);
+
+            mgr.listenToClusterUserScript(clusterId, System.out::println, System.err::println);
+        }
+    }
+
+    /** */
+    public void setClusterId(UUID clusterId) {
+        this.clusterId = clusterId;
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/command/PsCommand.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/command/PsCommand.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/command/PsCommand.java
new file mode 100644
index 0000000..0538496
--- /dev/null
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/command/PsCommand.java
@@ -0,0 +1,47 @@
+/*
+ * 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.
+ */
+
+package org.apache.ignite.tensorflow.submitter.command;
+
+import java.util.Map;
+import java.util.UUID;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.tensorflow.cluster.TensorFlowCluster;
+import org.apache.ignite.tensorflow.cluster.TensorFlowClusterManager;
+import picocli.CommandLine;
+
+/**
+ * Command "ps" that is used to print identifiers of all running TensorFlow clusters.
+ */
+@CommandLine.Command(
+    name = "ps",
+    description = "Prints identifiers of all running TensorFlow clusters.",
+    mixinStandardHelpOptions = true
+)
+public class PsCommand extends AbstractCommand {
+    /** {@inheritDoc} */
+    @Override public void run() {
+        try (Ignite ignite = getIgnite()) {
+            TensorFlowClusterManager mgr = new TensorFlowClusterManager(ignite);
+
+            Map<UUID, TensorFlowCluster> clusters = mgr.getAllClusters();
+
+            for (UUID clusterId : clusters.keySet())
+                System.out.println(clusterId);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/command/RootCommand.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/command/RootCommand.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/command/RootCommand.java
new file mode 100644
index 0000000..508ea7b
--- /dev/null
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/command/RootCommand.java
@@ -0,0 +1,42 @@
+/*
+ * 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.
+ */
+
+package org.apache.ignite.tensorflow.submitter.command;
+
+import picocli.CommandLine;
+
+/**
+ * Root command that aggregates all sub commands.
+ */
+@CommandLine.Command(
+    name = "ignite-tf",
+    description = "Apache Ignite and TensorFlow integration command line tool that allows to start, maintain and" +
+        " stop distributed deep learning utilizing Apache Ignite infrastructure and data.",
+    subcommands = {
+        StartCommand.class,
+        StopCommand.class,
+        AttachCommand.class,
+        PsCommand.class
+    },
+    mixinStandardHelpOptions = true
+)
+public class RootCommand extends AbstractCommand {
+    /** {@inheritDoc} */
+    @Override public void run() {
+        CommandLine.usage(this, System.out);
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/command/StartCommand.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/command/StartCommand.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/command/StartCommand.java
new file mode 100644
index 0000000..082b363
--- /dev/null
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/command/StartCommand.java
@@ -0,0 +1,205 @@
+/*
+ * 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.
+ */
+
+package org.apache.ignite.tensorflow.submitter.command;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.UUID;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+import org.apache.commons.io.IOUtils;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.tensorflow.cluster.TensorFlowClusterGatewayManager;
+import org.apache.ignite.tensorflow.cluster.TensorFlowJobArchive;
+import picocli.CommandLine;
+
+/**
+ * Command "start" that is used to start a new TensorFlow cluster on top of Apache Ignite.
+ */
+@CommandLine.Command(
+    name = "start",
+    description = "Starts a new TensorFlow cluster and attaches to user script process.",
+    mixinStandardHelpOptions = true
+)
+public class StartCommand extends AbstractCommand {
+    /** Upstream cache name. */
+    @CommandLine.Parameters(index = "0", paramLabel = "CACHE_NAME", description = "Upstream cache name.")
+    private String cacheName;
+
+    /** Job folder or archive. */
+    @CommandLine.Parameters(index = "1", paramLabel = "JOB_DIR", description = "Job folder (or zip archive).")
+    private String jobFolder;
+
+    /** Job command to be executed in cluster. */
+    @CommandLine.Parameters(index = "2", paramLabel = "JOB_CMD", description = "Job command.")
+    private String jobCmd;
+
+    /** Arguments of a job command to be executed in cluster. */
+    @CommandLine.Parameters(index = "3..*", paramLabel = "JOB_ARGS", description = "Job arguments.")
+    private String[] jobArguments;
+
+    /** {@inheritDoc} */
+    @Override public void run() {
+        try (Ignite ignite = getIgnite()) {
+            UUID clusterId = UUID.randomUUID();
+            String[] commands = new String[jobArguments.length + 1];
+            commands[0] = jobCmd;
+            System.arraycopy(jobArguments, 0, commands, 1, commands.length - 1);
+
+            TensorFlowJobArchive jobArchive = new TensorFlowJobArchive(
+                cacheName,
+                zip(jobFolder),
+                commands
+            );
+
+            TensorFlowClusterGatewayManager mgr = new TensorFlowClusterGatewayManager(ignite);
+            mgr.createCluster(clusterId, jobArchive);
+
+            mgr.listenToClusterUserScript(clusterId, System.out::println, System.err::println);
+        }
+        catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Archives specified folder or file into zip archive.
+     *
+     * @param jobArchivePath Path to folder to be archived.
+     * @return Byte array representing zip archive.
+     * @throws IOException In case of input/output exception.
+     */
+    private byte[] zip(String jobArchivePath) throws IOException {
+        Path path = Paths.get(jobArchivePath);
+        File file = path.toFile();
+
+        if (!file.exists())
+            throw new IllegalArgumentException("File doesn't exist [name=" + jobArchivePath + "]");
+
+        if (file.isDirectory())
+            return zipDirectory(file);
+        else if (jobArchivePath.endsWith(".zip"))
+            return zipArchive(file);
+        else
+            return zipFile(file);
+    }
+
+    /**
+     * Archives specified folder into zip archive.
+     *
+     * @param dir Directory to be archived.
+     * @return Byte array representing zip archive.
+     * @throws IOException In case of input/output exception.
+     */
+    private byte[] zipDirectory(File dir) throws IOException {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+        try (ZipOutputStream zipFile = new ZipOutputStream(baos)) {
+            compressDirectoryToZip(dir.getAbsolutePath(), dir.getAbsolutePath(), zipFile);
+        }
+
+        return baos.toByteArray();
+    }
+
+    /**
+     * Archives specified file into zip archive.
+     *
+     * @param file File to be archived.
+     * @return Byte array representing zip archive.
+     * @throws IOException In case of input/output exception.
+     */
+    private byte[] zipFile(File file) throws IOException {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+        try (ZipOutputStream zos = new ZipOutputStream(baos)) {
+            ZipEntry entry = new ZipEntry(file.getName());
+            zos.putNextEntry(entry);
+
+            try (FileInputStream in = new FileInputStream(file.getAbsolutePath())) {
+                IOUtils.copy(in, zos);
+            }
+        }
+
+        return baos.toByteArray();
+    }
+
+    /**
+     * Reads zip archive into byte array and returns this array.
+     *
+     * @param file Archive to be read.
+     * @return Byte array representing zip archive.
+     * @throws IOException In case of input/output exception.
+     */
+    private byte[] zipArchive(File file) throws IOException {
+        try (FileInputStream fis = new FileInputStream(file)) {
+            return IOUtils.toByteArray(fis);
+        }
+    }
+
+    /**
+     * Archives specified folder into zip output stream.
+     *
+     * @param rootDir Root directory.
+     * @param srcDir Source directory.
+     * @param out Zip output stream.
+     * @throws IOException In case of input/output exception.
+     */
+    private void compressDirectoryToZip(String rootDir, String srcDir, ZipOutputStream out) throws IOException {
+        File[] files = new File(srcDir).listFiles();
+
+        if (files != null) {
+            for (File file : files) {
+                if (file.isDirectory())
+                    compressDirectoryToZip(rootDir, srcDir + File.separator + file.getName(), out);
+                else {
+                    ZipEntry entry = new ZipEntry(srcDir.replace(rootDir, "")
+                        + File.separator + file.getName());
+                    out.putNextEntry(entry);
+
+                    try (FileInputStream in = new FileInputStream(srcDir + File.separator + file.getName())) {
+                        IOUtils.copy(in, out);
+                    }
+                }
+            }
+        }
+    }
+
+    /** */
+    public void setCacheName(String cacheName) {
+        this.cacheName = cacheName;
+    }
+
+    /** */
+    public void setJobFolder(String jobFolder) {
+        this.jobFolder = jobFolder;
+    }
+
+    /** */
+    public void setJobCmd(String jobCmd) {
+        this.jobCmd = jobCmd;
+    }
+
+    /** */
+    public void setJobArguments(String[] jobArguments) {
+        this.jobArguments = jobArguments;
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/command/StopCommand.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/command/StopCommand.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/command/StopCommand.java
new file mode 100644
index 0000000..8890370
--- /dev/null
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/command/StopCommand.java
@@ -0,0 +1,50 @@
+/*
+ * 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.
+ */
+
+package org.apache.ignite.tensorflow.submitter.command;
+
+import java.util.UUID;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.tensorflow.cluster.TensorFlowClusterGatewayManager;
+import picocli.CommandLine;
+
+/**
+ * Command "stop" that is used to stop TensorFlow cluster.
+ */
+@CommandLine.Command(
+    name = "stop",
+    description = "Stops a running TensorFlow cluster.",
+    mixinStandardHelpOptions = true
+)
+public class StopCommand extends AbstractCommand {
+    /** Cluster identifier. */
+    @CommandLine.Parameters(paramLabel = "CLUSTER_ID", description = "Cluster identifier.")
+    private UUID clusterId;
+
+    /** {@inheritDoc} */
+    @Override public void run() {
+        try (Ignite ignite = getIgnite()) {
+            TensorFlowClusterGatewayManager mgr = new TensorFlowClusterGatewayManager(ignite);
+            mgr.stopClusterIfExists(clusterId);
+        }
+    }
+
+    /** */
+    public void setClusterId(UUID clusterId) {
+        this.clusterId = clusterId;
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/command/package-info.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/command/package-info.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/command/package-info.java
new file mode 100644
index 0000000..7949feb
--- /dev/null
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/command/package-info.java
@@ -0,0 +1,22 @@
+/*
+ * 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.
+ */
+
+/**
+ * <!-- Package description. -->
+ * This package contains commands that command line tool provides. Pico CLI is used to make these commands maintainable.
+ */
+package org.apache.ignite.tensorflow.submitter.command;
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/package-info.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/package-info.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/package-info.java
new file mode 100644
index 0000000..8288b16
--- /dev/null
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/submitter/package-info.java
@@ -0,0 +1,23 @@
+/*
+ * 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.
+ */
+
+/**
+ * <!-- Package description. -->
+ * This package contains classes that allow to use command line interface to submit jobs into TensorFlow in Apache
+ * Ignite infrastructure.
+ */
+package org.apache.ignite.tensorflow.submitter;
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/util/SerializableConsumer.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/util/SerializableConsumer.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/util/SerializableConsumer.java
new file mode 100644
index 0000000..ece58aa
--- /dev/null
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/util/SerializableConsumer.java
@@ -0,0 +1,29 @@
+/*
+ * 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.
+ */
+
+package org.apache.ignite.tensorflow.util;
+
+import java.io.Serializable;
+import java.util.function.Consumer;
+
+/**
+ * Serializable consumer.
+ *
+ * @param <T> The type of the input to the operation.
+ */
+public interface SerializableConsumer<T> extends Consumer<T>, Serializable {
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/util/SerializableSupplier.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/util/SerializableSupplier.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/util/SerializableSupplier.java
new file mode 100644
index 0000000..768dbe1
--- /dev/null
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/util/SerializableSupplier.java
@@ -0,0 +1,29 @@
+/*
+ * 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.
+ */
+
+package org.apache.ignite.tensorflow.util;
+
+import java.io.Serializable;
+import java.util.function.Supplier;
+
+/**
+ * Serializable supplier.
+ *
+ * @param <T> The type of results supplied by this supplier.
+ */
+public interface SerializableSupplier<T> extends Supplier<T>, Serializable {
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/util/package-info.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/util/package-info.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/util/package-info.java
new file mode 100644
index 0000000..8ed43c3
--- /dev/null
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/util/package-info.java
@@ -0,0 +1,22 @@
+/*
+ * 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.
+ */
+
+/**
+ * <!-- Package description. -->
+ * Util classes used in {@link org.apache.ignite.tensorflow} package.
+ */
+package org.apache.ignite.tensorflow.util;
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/sh/ignite-tf.sh
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/sh/ignite-tf.sh b/modules/tensorflow/src/main/sh/ignite-tf.sh
new file mode 100755
index 0000000..fd3e02c
--- /dev/null
+++ b/modules/tensorflow/src/main/sh/ignite-tf.sh
@@ -0,0 +1,19 @@
+#!/usr/bin/env bash
+
+# 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.
+
+SCRIPT_PATH="$( cd "$(dirname "$0")" ; pwd -P )"
+java -Xmx4G -DIGNITE_QUIET=false -cp "$SCRIPT_PATH:$SCRIPT_PATH/lib/*" org.apache.ignite.tensorflow.submitter.JobSubmitter "$@"
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/sh/logback.xml
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/sh/logback.xml b/modules/tensorflow/src/main/sh/logback.xml
new file mode 100644
index 0000000..816b5e6
--- /dev/null
+++ b/modules/tensorflow/src/main/sh/logback.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+  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.
+-->
+<!--
+    Logback configuration file.
+-->
+<configuration>
+
+    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
+        <file>ignite-tf.log</file>
+        <append>false</append>
+        <encoder>
+            <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
+        </encoder>
+    </appender>
+
+    <root level="warn">
+        <appender-ref ref="FILE" />
+    </root>
+
+</configuration>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/test/java/org/apache/ignite/tensorflow/core/longrunning/LongRunningProcessManagerTest.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/test/java/org/apache/ignite/tensorflow/core/longrunning/LongRunningProcessManagerTest.java b/modules/tensorflow/src/test/java/org/apache/ignite/tensorflow/core/longrunning/LongRunningProcessManagerTest.java
index 7d917e7..faa2b6b 100644
--- a/modules/tensorflow/src/test/java/org/apache/ignite/tensorflow/core/longrunning/LongRunningProcessManagerTest.java
+++ b/modules/tensorflow/src/test/java/org/apache/ignite/tensorflow/core/longrunning/LongRunningProcessManagerTest.java
@@ -17,13 +17,11 @@
 
 package org.apache.ignite.tensorflow.core.longrunning;
 
-import java.io.Serializable;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
-import java.util.function.Supplier;
 import org.apache.ignite.Ignite;
 import org.apache.ignite.IgniteCluster;
 import org.apache.ignite.IgniteCompute;
@@ -67,7 +65,7 @@ public class LongRunningProcessManagerTest {
 
         List<LongRunningProcess> list = Collections.singletonList(new LongRunningProcess(nodeId, () -> {}));
 
-        LongRunningProcessManager mgr = new LongRunningProcessManager((Supplier<Ignite> & Serializable)() -> ignite);
+        LongRunningProcessManager mgr = new LongRunningProcessManager(ignite);
         Map<UUID, List<UUID>> res = mgr.start(list);
 
         assertEquals(1, res.size());
@@ -97,7 +95,7 @@ public class LongRunningProcessManagerTest {
         Map<UUID, List<UUID>> procIds = new HashMap<>();
         procIds.put(nodeId, Collections.singletonList(procId));
 
-        LongRunningProcessManager mgr = new LongRunningProcessManager((Supplier<Ignite> & Serializable)() -> ignite);
+        LongRunningProcessManager mgr = new LongRunningProcessManager(ignite);
         Map<UUID, List<LongRunningProcessStatus>> res = mgr.ping(procIds);
 
         assertEquals(1, res.size());
@@ -127,7 +125,7 @@ public class LongRunningProcessManagerTest {
         Map<UUID, List<UUID>> procIds = new HashMap<>();
         procIds.put(nodeId, Collections.singletonList(procId));
 
-        LongRunningProcessManager mgr = new LongRunningProcessManager((Supplier<Ignite> & Serializable)() -> ignite);
+        LongRunningProcessManager mgr = new LongRunningProcessManager(ignite);
         Map<UUID, List<LongRunningProcessStatus>> res = mgr.stop(procIds, true);
 
         assertEquals(1, res.size());
@@ -157,7 +155,7 @@ public class LongRunningProcessManagerTest {
         Map<UUID, List<UUID>> procIds = new HashMap<>();
         procIds.put(nodeId, Collections.singletonList(procId));
 
-        LongRunningProcessManager mgr = new LongRunningProcessManager((Supplier<Ignite> & Serializable)() -> ignite);
+        LongRunningProcessManager mgr = new LongRunningProcessManager(ignite);
         Map<UUID, List<LongRunningProcessStatus>> res = mgr.clear(procIds);
 
         assertEquals(1, res.size());


[2/2] ignite git commit: IGNITE-9034: [ML] Add Estimator API support to TensorFlow cluster on top of Apache Ignite.

Posted by ch...@apache.org.
IGNITE-9034: [ML] Add Estimator API support to TensorFlow
cluster on top of Apache Ignite.

this closes #4402


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/9e884e5a
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/9e884e5a
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/9e884e5a

Branch: refs/heads/master
Commit: 9e884e5aaa3fba3a68967c51280f3a8f41514f51
Parents: c5e723c
Author: Anton Dmitriev <dm...@gmail.com>
Authored: Tue Jul 31 16:54:26 2018 +0300
Committer: Yury Babak <yb...@gridgain.com>
Committed: Tue Jul 31 16:54:26 2018 +0300

----------------------------------------------------------------------
 examples/pom.xml                                |  21 --
 .../cluster/TensorFlowClusterExample.java       | 118 --------
 modules/tensorflow/README.txt                   |  55 +++-
 modules/tensorflow/pom.xml                      |  56 +++-
 modules/tensorflow/src/main/assembly/zip.xml    |  56 ++++
 .../cluster/TensorFlowClusterGateway.java       |  34 ++-
 .../TensorFlowClusterGatewayManager.java        |  95 +++++--
 .../cluster/TensorFlowClusterMaintainer.java    | 105 +++++---
 .../cluster/TensorFlowClusterManager.java       | 268 +++++++++++++------
 .../cluster/TensorFlowJobArchive.java           |  65 +++++
 .../cluster/spec/TensorFlowClusterSpec.java     |  39 +++
 .../spec/TensorFlowServerAddressSpec.java       |  14 +
 .../tfrunning/TensorFlowServerManager.java      | 112 +-------
 .../TensorFlowServerScriptFormatter.java        |  62 +++++
 .../cluster/util/ClusterPortManager.java        | 168 +++++++++---
 .../cluster/util/TensorFlowChiefRunner.java     |  80 ++++++
 .../cluster/util/TensorFlowClusterResolver.java |  92 ++++---
 .../util/TensorFlowUserScriptRunner.java        | 236 ++++++++++++++++
 .../ignite/tensorflow/core/ProcessManager.java  |   5 +-
 .../tensorflow/core/ProcessManagerWrapper.java  |   5 +-
 .../longrunning/LongRunningProcessManager.java  |  19 +-
 .../task/LongRunningProcessStartTask.java       |   4 +-
 .../core/nativerunning/NativeProcess.java       |   6 +-
 .../nativerunning/NativeProcessManager.java     |  12 +-
 .../task/NativeProcessStartTask.java            |  81 ++----
 .../PythonProcessBuilderSupplier.java           |  57 ++++
 .../pythonrunning/PythonProcessManager.java     |  35 +--
 .../core/util/AsyncNativeProcessRunner.java     | 107 ++++++++
 .../core/util/NativeProcessRunner.java          | 133 +++++++++
 .../tensorflow/submitter/JobSubmitter.java      |  35 +++
 .../submitter/command/AbstractCommand.java      |  55 ++++
 .../submitter/command/AttachCommand.java        |  51 ++++
 .../tensorflow/submitter/command/PsCommand.java |  47 ++++
 .../submitter/command/RootCommand.java          |  42 +++
 .../submitter/command/StartCommand.java         | 205 ++++++++++++++
 .../submitter/command/StopCommand.java          |  50 ++++
 .../submitter/command/package-info.java         |  22 ++
 .../tensorflow/submitter/package-info.java      |  23 ++
 .../tensorflow/util/SerializableConsumer.java   |  29 ++
 .../tensorflow/util/SerializableSupplier.java   |  29 ++
 .../ignite/tensorflow/util/package-info.java    |  22 ++
 modules/tensorflow/src/main/sh/ignite-tf.sh     |  19 ++
 modules/tensorflow/src/main/sh/logback.xml      |  36 +++
 .../LongRunningProcessManagerTest.java          |  10 +-
 44 files changed, 2222 insertions(+), 593 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/examples/pom.xml
----------------------------------------------------------------------
diff --git a/examples/pom.xml b/examples/pom.xml
index 3f6e6a8..e745beb 100644
--- a/examples/pom.xml
+++ b/examples/pom.xml
@@ -124,8 +124,6 @@
         <lgpl.test.folder>src/test/java</lgpl.test.folder>
         <spark.folder>src/main/java</spark.folder>
         <spark.test.folder>src/test/java</spark.test.folder>
-        <tensorflow.folder>src/main/java</tensorflow.folder>
-        <tensorflow.test.folder>src/test/java</tensorflow.test.folder>
     </properties>
 
     <profiles>
@@ -202,23 +200,6 @@
                 </dependency>
             </dependencies>
         </profile>
-
-        <profile>
-            <id>tensorflow</id>
-
-            <properties>
-                <tensorflow.folder>src/main/tensorflow</tensorflow.folder>
-                <tensorflow.test.folder>src/test/tensorflow</tensorflow.test.folder>
-            </properties>
-
-            <dependencies>
-                <dependency>
-                    <groupId>org.apache.ignite</groupId>
-                    <artifactId>ignite-tensorflow</artifactId>
-                    <version>${project.version}</version>
-                </dependency>
-            </dependencies>
-        </profile>
     </profiles>
 
     <build>
@@ -249,7 +230,6 @@
                             <sources>
                                 <source>${lgpl.folder}</source>
                                 <source>${spark.folder}</source>
-                                <source>${tensorflow.folder}</source>
                             </sources>
                         </configuration>
                     </execution>
@@ -264,7 +244,6 @@
                             <sources>
                                 <source>${lgpl.test.folder}</source>
                                 <source>${spark.test.folder}</source>
-                                <source>${tensorflow.test.folder}</source>
                             </sources>
                         </configuration>
                     </execution>

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/examples/src/main/tensorflow/org/apache/ignite/tensorflow/cluster/TensorFlowClusterExample.java
----------------------------------------------------------------------
diff --git a/examples/src/main/tensorflow/org/apache/ignite/tensorflow/cluster/TensorFlowClusterExample.java b/examples/src/main/tensorflow/org/apache/ignite/tensorflow/cluster/TensorFlowClusterExample.java
deleted file mode 100644
index 3a956c9..0000000
--- a/examples/src/main/tensorflow/org/apache/ignite/tensorflow/cluster/TensorFlowClusterExample.java
+++ /dev/null
@@ -1,118 +0,0 @@
-/*
- * 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.
- */
-
-package org.apache.ignite.tensorflow.cluster;
-
-import java.io.Serializable;
-import java.util.List;
-import java.util.Map;
-import java.util.UUID;
-import java.util.concurrent.CountDownLatch;
-import java.util.function.Supplier;
-import org.apache.ignite.Ignite;
-import org.apache.ignite.IgniteCache;
-import org.apache.ignite.Ignition;
-import org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction;
-import org.apache.ignite.configuration.CacheConfiguration;
-import org.apache.ignite.configuration.IgniteConfiguration;
-import org.apache.ignite.tensorflow.cluster.tfrunning.TensorFlowServerManager;
-import org.apache.ignite.tensorflow.core.longrunning.task.util.LongRunningProcessStatus;
-
-/**
- * Prerequisites: be aware that to successfully run this example you need to have Python and TensorFlow installed on
- * your machine. To find out how to install Python please take a look this page: https://www.python.org/downloads/. To
- * install TensorFlow see this web site: https://www.tensorflow.org/install/.
- *
- * Example that shows how to use {@link TensorFlowClusterGatewayManager} and start, maintain and stop TensorFlow
- * cluster.
- */
-public class TensorFlowClusterExample {
-    /** Run example. */
-    public static void main(String... args) throws InterruptedException {
-        IgniteConfiguration configuration = new IgniteConfiguration();
-        configuration.setClientMode(false);
-
-        try (Ignite ignite = Ignition.start(configuration)) {
-            System.out.println(">>> TensorFlow cluster example started.");
-
-            CacheConfiguration<Integer, Integer> cacheConfiguration = new CacheConfiguration<>();
-            cacheConfiguration.setAffinity(new RendezvousAffinityFunction(false, 10));
-            cacheConfiguration.setName("TEST_CACHE");
-
-            IgniteCache<Integer, Integer> cache = ignite.getOrCreateCache(cacheConfiguration);
-            for (int i = 0; i < 1000; i++)
-                cache.put(i, i);
-
-            System.out.println(">>> Cache created.");
-
-            TensorFlowClusterGatewayManager mgr = new TensorFlowClusterGatewayManager(ignite);
-            TensorFlowClusterGateway gateway = mgr.getOrCreateCluster("TEST_CACHE");
-
-            System.out.println(">>> TensorFlow cluster gateway started.");
-
-            CountDownLatch latch = new CountDownLatch(1);
-
-            gateway.subscribe(cluster -> {
-                StringBuilder builder = new StringBuilder();
-                builder.append("------------------- TensorFlow Cluster Service Info -------------------").append('\n');
-
-                builder.append("Specification : ").append('\n');
-
-                TensorFlowServerManager srvMgr = new TensorFlowServerManager(
-                    (Supplier<Ignite> & Serializable)() -> ignite
-                );
-
-                String clusterSpec = srvMgr.formatClusterSpec(cluster.getSpec());
-                builder.append(clusterSpec).append('\n');
-
-                Map<UUID, List<LongRunningProcessStatus>> statuses = srvMgr.ping(cluster.getProcesses());
-
-                builder.append("State : ").append('\n');
-
-                for (UUID nodeId : cluster.getProcesses().keySet()) {
-                    List<UUID> pr = cluster.getProcesses().get(nodeId);
-                    List<LongRunningProcessStatus> st = statuses.get(nodeId);
-
-                    builder.append("Node ").append(nodeId.toString().substring(0, 8)).append(" -> ").append('\n');
-                    for (int i = 0; i < pr.size(); i++) {
-                        builder.append("\tProcess ")
-                            .append(pr.get(i).toString().substring(0, 8))
-                            .append(" with status ")
-                            .append(st.get(i).getState());
-
-                        if (st.get(i).getException() != null)
-                            builder.append(" (").append(st.get(i).getException()).append(")");
-
-                        builder.append('\n');
-                    }
-                }
-
-                builder.append("-----------------------------------------------------------------------").append('\n');
-
-                System.out.println(builder);
-
-                latch.countDown();
-            });
-
-            latch.await();
-
-            mgr.stopClusterIfExists("TEST_CACHE");
-
-            System.out.println(">>> TensorFlow cluster example completed.");
-        }
-    }
-}

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/README.txt
----------------------------------------------------------------------
diff --git a/modules/tensorflow/README.txt b/modules/tensorflow/README.txt
index 45fc3cf..21ea88d 100644
--- a/modules/tensorflow/README.txt
+++ b/modules/tensorflow/README.txt
@@ -7,7 +7,7 @@ will be a datasource for any TensorFlow model training.
 Import Apache Ignite TensorFlow Integration Module In Maven Project
 -------------------------------------
 
-If you are using Maven to manage dependencies of your project, you can add Cassandra Store module
+If you are using Maven to manage dependencies of your project, you can add TensorFlow module
 dependency like this (replace '${ignite.version}' with actual Ignite version you are
 interested in):
 
@@ -26,4 +26,55 @@ interested in):
         ...
     </dependencies>
     ...
-</project>
\ No newline at end of file
+</project>
+-------------------------------------
+
+TensorFlow integration module provides command line tool that allows to start, maintain and stop distributed deep
+learning utilizing Apache Ignite infrastructure and data. This tool provides several commands that are shown here:
+
+Usage: ignite-tf [-hV] [-c=<cfg>] [COMMAND]
+Apache Ignite and TensorFlow integration command line utility that allows to
+start, maintain and stop distributed deep learning utilizing Apache Ignite
+infrastructure and data.
+  -c, --config=<cfg>   Apache Ignite client configuration.
+  -h, --help           Show this help message and exit.
+  -V, --version        Print version information and exit.
+Commands:
+  start   Starts a new TensorFlow cluster and attaches to user script process.
+  stop    Stops a running TensorFlow cluster.
+  attach  Attaches to running TensorFlow cluster (user script process).
+  ps      Prints identifiers of all running TensorFlow clusters.
+
+To start TensorFlow cluster you need to specify upstream cache that will be used as data source for training, folder
+that contains code that actually performs training and command that should be called on this code to start training
+correctly. Command "start" have the following help output:
+
+Usage: ignite-tf start [-hV] [-c=<cfg>] CACHE_NAME JOB_DIR JOB_CMD [JOB_ARGS...]
+Starts a new TensorFlow cluster and attaches to user script process.
+      CACHE_NAME       Upstream cache name.
+      JOB_DIR          Job folder (or zip archive).
+      JOB_CMD          Job command.
+      [JOB_ARGS...]    Job arguments.
+  -c, --config=<cfg>   Apache Ignite client configuration.
+  -h, --help           Show this help message and exit.
+  -V, --version        Print version information and exit.
+
+To attach to running TensorFlow cluster or stop it you can use commands "attach" and "stop" correspondingly. These
+commands accepts cluster identifier as a parameter:
+
+Usage: ignite-tf attach [-hV] [-c=<cfg>] CLUSTER_ID
+Attaches to running TensorFlow cluster (user script process).
+      CLUSTER_ID       Cluster identifier.
+  -c, --config=<cfg>   Apache Ignite client configuration.
+  -h, --help           Show this help message and exit.
+  -V, --version        Print version information and exit.
+
+Usage: ignite-tf stop [-hV] [-c=<cfg>] CLUSTER_ID
+Stops a running TensorFlow cluster.
+      CLUSTER_ID       Cluster identifier.
+  -c, --config=<cfg>   Apache Ignite client configuration.
+  -h, --help           Show this help message and exit.
+  -V, --version        Print version information and exit.
+
+To find out what TensorFlow clusters are currently running on top of Apache Ignite you can use "ps" command that doesn't
+require arguments.
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/pom.xml
----------------------------------------------------------------------
diff --git a/modules/tensorflow/pom.xml b/modules/tensorflow/pom.xml
index 72e7e69..b1cfe21 100644
--- a/modules/tensorflow/pom.xml
+++ b/modules/tensorflow/pom.xml
@@ -34,6 +34,30 @@
     <version>2.7.0-SNAPSHOT</version>
     <url>http://ignite.apache.org</url>
 
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-assembly-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <id>dist</id>
+                        <phase>package</phase>
+                        <goals>
+                            <goal>single</goal>
+                        </goals>
+                        <configuration>
+                            <appendAssemblyId>false</appendAssemblyId>
+                            <descriptors>
+                                <descriptor>src/main/assembly/zip.xml</descriptor>
+                            </descriptors>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+
     <dependencies>
         <dependency>
             <groupId>org.apache.ignite</groupId>
@@ -43,22 +67,39 @@
 
         <dependency>
             <groupId>org.apache.ignite</groupId>
-            <artifactId>ignite-core</artifactId>
+            <artifactId>ignite-spring</artifactId>
             <version>${project.version}</version>
-            <type>test-jar</type>
-            <scope>test</scope>
         </dependency>
 
         <dependency>
             <groupId>org.apache.ignite</groupId>
-            <artifactId>ignite-spring</artifactId>
+            <artifactId>ignite-slf4j</artifactId>
             <version>${project.version}</version>
-            <scope>test</scope>
         </dependency>
 
         <dependency>
-            <groupId>log4j</groupId>
-            <artifactId>log4j</artifactId>
+            <groupId>ch.qos.logback</groupId>
+            <artifactId>logback-classic</artifactId>
+            <version>1.2.3</version>
+        </dependency>
+
+        <dependency>
+            <groupId>commons-io</groupId>
+            <artifactId>commons-io</artifactId>
+            <version>2.6</version>
+        </dependency>
+
+        <dependency>
+            <groupId>info.picocli</groupId>
+            <artifactId>picocli</artifactId>
+            <version>3.3.0</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.ignite</groupId>
+            <artifactId>ignite-core</artifactId>
+            <version>${project.version}</version>
+            <type>test-jar</type>
             <scope>test</scope>
         </dependency>
 
@@ -86,5 +127,4 @@
             </build>
         </profile>
     </profiles>
-
 </project>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/assembly/zip.xml
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/assembly/zip.xml b/modules/tensorflow/src/main/assembly/zip.xml
new file mode 100644
index 0000000..0c90e98
--- /dev/null
+++ b/modules/tensorflow/src/main/assembly/zip.xml
@@ -0,0 +1,56 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+  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.
+-->
+<!--
+    Assembly file.
+-->
+<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
+          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2
+          http://maven.apache.org/xsd/assembly-1.1.2.xsd">
+    <id>zip</id>
+    <includeBaseDirectory>true</includeBaseDirectory>
+
+    <formats>
+        <format>zip</format>
+    </formats>
+
+    <files>
+        <file>
+            <source>${project.basedir}/src/main/sh/ignite-tf.sh</source>
+            <outputDirectory>/</outputDirectory>
+        </file>
+        <file>
+            <source>${project.basedir}/src/main/sh/logback.xml</source>
+            <outputDirectory>/</outputDirectory>
+        </file>
+        <file>
+            <source>${project.build.directory}/${project.artifactId}-${project.version}.jar</source>
+            <outputDirectory>/lib</outputDirectory>
+        </file>
+    </files>
+
+    <dependencySets>
+        <dependencySet>
+            <outputDirectory>lib</outputDirectory>
+            <useStrictFiltering>true</useStrictFiltering>
+            <useProjectArtifact>false</useProjectArtifact>
+            <scope>runtime</scope>
+        </dependencySet>
+    </dependencySets>
+</assembly>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/TensorFlowClusterGateway.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/TensorFlowClusterGateway.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/TensorFlowClusterGateway.java
index 5eee155..092dfcb 100644
--- a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/TensorFlowClusterGateway.java
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/TensorFlowClusterGateway.java
@@ -18,29 +18,43 @@
 package org.apache.ignite.tensorflow.cluster;
 
 import java.util.HashSet;
+import java.util.Optional;
 import java.util.UUID;
 import java.util.function.Consumer;
 import org.apache.ignite.lang.IgniteBiPredicate;
+import org.apache.ignite.tensorflow.util.SerializableConsumer;
 
 /**
  * TensorFlow cluster gateway that allows to subscribe on changes in cluster configuration.
  */
-public class TensorFlowClusterGateway implements IgniteBiPredicate<UUID, TensorFlowCluster> {
+public class TensorFlowClusterGateway implements IgniteBiPredicate<UUID, Optional<TensorFlowCluster>>, AutoCloseable {
     /** */
     private static final long serialVersionUID = -540323262800791340L;
 
+    /** Callback that will be called on unsubscribe. */
+    private final SerializableConsumer<TensorFlowClusterGateway> unsubscribeCb;
+
     /** Subscribers. */
-    private final HashSet<Consumer<TensorFlowCluster>> subscribers = new HashSet<>();
+    private final HashSet<Consumer<Optional<TensorFlowCluster>>> subscribers = new HashSet<>();
 
     /** Last value received from the upstream. */
-    private TensorFlowCluster last;
+    private Optional<TensorFlowCluster> last;
+
+    /**
+     * Constructs a new instance of TensorFlow cluster gateway.
+     *
+     * @param unsubscribeCb Callback that will be called on unsubscribe.
+     */
+    public TensorFlowClusterGateway(SerializableConsumer<TensorFlowClusterGateway> unsubscribeCb) {
+        this.unsubscribeCb = unsubscribeCb;
+    }
 
     /**
      * Subscribers the specified subscriber on the upstream events.
      *
      * @param subscriber Subscriber.
      */
-    public synchronized void subscribe(Consumer<TensorFlowCluster> subscriber) {
+    public synchronized void subscribe(Consumer<Optional<TensorFlowCluster>> subscriber) {
         subscribers.add(subscriber);
 
         if (last != null)
@@ -52,17 +66,23 @@ public class TensorFlowClusterGateway implements IgniteBiPredicate<UUID, TensorF
      *
      * @param subscriber Subscriber.
      */
-    public synchronized void unsubscribe(Consumer<TensorFlowCluster> subscriber) {
+    public synchronized void unsubscribe(Consumer<Optional<TensorFlowCluster>> subscriber) {
         subscribers.remove(subscriber);
     }
 
     /** {@inheritDoc} */
-    @Override public synchronized boolean apply(UUID uuid, TensorFlowCluster cluster) {
-        for (Consumer<TensorFlowCluster> subscriber : subscribers)
+    @Override public synchronized boolean apply(UUID uuid, Optional<TensorFlowCluster> cluster) {
+        for (Consumer<Optional<TensorFlowCluster>> subscriber : subscribers)
             subscriber.accept(cluster);
 
         last = cluster;
 
         return true;
     }
+
+    /** {@inheritDoc} */
+    @Override public void close() {
+        subscribers.clear();
+        unsubscribeCb.accept(this);
+    }
 }

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/TensorFlowClusterGatewayManager.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/TensorFlowClusterGatewayManager.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/TensorFlowClusterGatewayManager.java
index f4b8187..a315aaf 100644
--- a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/TensorFlowClusterGatewayManager.java
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/TensorFlowClusterGatewayManager.java
@@ -17,8 +17,12 @@
 
 package org.apache.ignite.tensorflow.cluster;
 
+import java.util.Optional;
+import java.util.UUID;
+import java.util.concurrent.CountDownLatch;
+import java.util.function.Consumer;
 import org.apache.ignite.Ignite;
-import org.apache.ignite.IgniteServices;
+import org.apache.ignite.IgniteLogger;
 
 /**
  * TensorFlow cluster manager that allows to start, maintain and stop TensorFlow cluster using
@@ -34,6 +38,9 @@ public class TensorFlowClusterGatewayManager {
     /** Ignite instance. */
     private final Ignite ignite;
 
+    /** Logger. */
+    private final IgniteLogger log;
+
     /**
      * Constructs a new instance of TensorFlow cluster manager with maintenance.
      *
@@ -43,37 +50,89 @@ public class TensorFlowClusterGatewayManager {
         assert ignite != null : "Ignite should not be null";
 
         this.ignite = ignite;
+        this.log = ignite.log().getLogger(TensorFlowClusterGatewayManager.class);
     }
 
     /**
-     * Creates and starts a new TensorFlow cluster for the specified cache if it doesn't exist, otherwise returns
-     * existing one.
+     * Subscribes on changes of the specified cluster.
      *
-     * @param upstreamCacheName Upstream cache name.
+     * @param clusterId Cluster identifier.
      * @return TensorFlow cluster gateway that allows to subscribe on cluster changes.
      */
-    public TensorFlowClusterGateway getOrCreateCluster(String upstreamCacheName) {
-        String svcName = String.format(SERVICE_NAME_TEMPLATE, upstreamCacheName);
-        String topicName = String.format(SERVICE_TOPIC_NAME_TEMPLATE, upstreamCacheName);
+    public TensorFlowClusterGateway getCluster(UUID clusterId) {
+        String topicName = String.format(SERVICE_TOPIC_NAME_TEMPLATE, clusterId);
 
-        TensorFlowClusterGateway gateway = createTensorFlowClusterGateway(topicName);
+        return createTensorFlowClusterGateway(topicName);
+    }
 
-        IgniteServices services = ignite.services();
+    /**
+     * Creates and starts a new TensorFlow cluster for the specified cache.
+     *
+     * @param clusterId Cluster identifier.
+     * @param jobArchive Job archive.
+     * @return TensorFlow cluster gateway that allows to subscribe on cluster changes.
+     */
+    public TensorFlowClusterGateway createCluster(UUID clusterId, TensorFlowJobArchive jobArchive) {
+        String svcName = String.format(SERVICE_NAME_TEMPLATE, clusterId);
+        String topicName = String.format(SERVICE_TOPIC_NAME_TEMPLATE, clusterId);
 
-        services.deployClusterSingleton(svcName, new TensorFlowClusterMaintainer(upstreamCacheName, topicName));
+        TensorFlowClusterGateway gateway = createTensorFlowClusterGateway(topicName);
+
+        ignite.services().deployClusterSingleton(
+            svcName,
+            new TensorFlowClusterMaintainer(clusterId, jobArchive, topicName)
+        );
+        log.info("Cluster maintainer deployed as a service [clusterId=" + clusterId + "]");
 
         return gateway;
     }
 
     /**
-     * Stops TensorFlow cluster.
+     * Listens to TensorFlow cluster user script.
      *
-     * @param upstreamCacheName Upstream cache name.
+     * @param clusterId Cluster identifier.
+     * @param out Output stream consumer.
+     * @param err Error stream consumer.
      */
-    public void stopClusterIfExists(String upstreamCacheName) {
-        IgniteServices services = ignite.services();
+    public void listenToClusterUserScript(UUID clusterId, Consumer<String> out, Consumer<String> err) {
+        TensorFlowClusterGateway gateway = getCluster(clusterId);
+
+        ignite.message().localListen("us_out_" + clusterId, (node, msg) -> {
+            out.accept(msg.toString());
+            return true;
+        });
+
+        ignite.message().localListen("us_err_" + clusterId, (node, msg) -> {
+            err.accept(msg.toString());
+            return true;
+        });
+
+        CountDownLatch latch = new CountDownLatch(1);
+
+        Consumer<Optional<TensorFlowCluster>> subscriber = cluster -> {
+            if (!cluster.isPresent())
+                latch.countDown();
+        };
+
+        gateway.subscribe(subscriber);
+
+        try {
+            latch.await();
+            gateway.unsubscribe(subscriber);
+        }
+        catch (InterruptedException e) {
+            throw new RuntimeException(e);
+        }
+    }
 
-        services.cancel(String.format(SERVICE_NAME_TEMPLATE, upstreamCacheName));
+    /**
+     * Stops TensorFlow cluster.
+     *
+     * @param clusterId Cluster identifier.
+     */
+    public void stopClusterIfExists(UUID clusterId) {
+        ignite.services().cancel(String.format(SERVICE_NAME_TEMPLATE, clusterId));
+        log.info("Cluster maintained cancelled as a service [clusterId=" + clusterId + "]");
     }
 
     /**
@@ -83,9 +142,13 @@ public class TensorFlowClusterGatewayManager {
      * @return TensorFlow cluster gateway.
      */
     private TensorFlowClusterGateway createTensorFlowClusterGateway(String topicName) {
-        TensorFlowClusterGateway gateway = new TensorFlowClusterGateway();
+        TensorFlowClusterGateway gateway = new TensorFlowClusterGateway(subscriber -> {
+            ignite.message().stopLocalListen(topicName, subscriber);
+            log.info("Stop listen to cluster gateway [topicName=" + topicName + "]");
+        });
 
         ignite.message().localListen(topicName, gateway);
+        log.info("Start listen to cluster gateway [topicName=" + topicName + "]");
 
         return gateway;
     }

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/TensorFlowClusterMaintainer.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/TensorFlowClusterMaintainer.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/TensorFlowClusterMaintainer.java
index 21b9c2b..e6ca33d 100644
--- a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/TensorFlowClusterMaintainer.java
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/TensorFlowClusterMaintainer.java
@@ -17,17 +17,18 @@
 
 package org.apache.ignite.tensorflow.cluster;
 
-import java.io.Serializable;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.UUID;
-import java.util.function.Supplier;
+import java.util.concurrent.locks.LockSupport;
 import org.apache.ignite.Ignite;
-import org.apache.ignite.IgniteMessaging;
-import org.apache.ignite.Ignition;
+import org.apache.ignite.IgniteLogger;
 import org.apache.ignite.cache.affinity.Affinity;
 import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.resources.IgniteInstanceResource;
+import org.apache.ignite.resources.LoggerResource;
 import org.apache.ignite.services.Service;
 import org.apache.ignite.services.ServiceContext;
 import org.apache.ignite.tensorflow.core.longrunning.task.util.LongRunningProcessState;
@@ -40,74 +41,116 @@ public class TensorFlowClusterMaintainer implements Service {
     /** */
     private static final long serialVersionUID = -3220563310643566419L;
 
-    /** Upstream cache name. */
-    private final String cacheName;
+    /** Ignite instance. */
+    @IgniteInstanceResource
+    private transient Ignite ignite;
+
+    /** Logger. */
+    @LoggerResource
+    private transient IgniteLogger log;
+
+    /** TensorFlow cluster identifier. */
+    private final UUID clusterId;
+
+    /** Job archive. */
+    private final TensorFlowJobArchive jobArchive;
 
     /** Topic name. */
     private final String topicName;
 
     /** TensorFlow cluster manager. */
-    private final TensorFlowClusterManager clusterMgr;
+    private transient TensorFlowClusterManager clusterMgr;
 
     /** Previous partition mapping. */
-    private UUID[] prev;
+    private transient UUID[] prev;
 
     /**
      * Constructs a new instance of TensorFlow cluster service.
      *
-     * @param cacheName Upstream cache name.
+     * @param clusterId Cluster identifier.
+     * @param jobArchive Job archive.
      * @param topicName Topic name.
      */
-    public TensorFlowClusterMaintainer(String cacheName, String topicName) {
-        assert cacheName != null : "Cache name should not be null";
+    public TensorFlowClusterMaintainer(UUID clusterId, TensorFlowJobArchive jobArchive, String topicName) {
+        assert clusterId != null : "Cluster identifier should not be null";
+        assert jobArchive != null : "Job archive should not be null";
         assert topicName != null : "Topic name should not be null";
 
-        this.clusterMgr = new TensorFlowClusterManager((Supplier<Ignite> & Serializable)Ignition::ignite);
-        this.cacheName = cacheName;
+        this.clusterId = clusterId;
+        this.jobArchive = jobArchive;
         this.topicName = topicName;
     }
 
     /** {@inheritDoc} */
     @Override public void cancel(ServiceContext ctx) {
-        clusterMgr.stopClusterIfExists(cacheName);
+        clusterMgr.stopClusterIfExists(clusterId);
+        log.debug("Cluster maintainer canceled [clusterId=" + clusterId + "]");
     }
 
     /** {@inheritDoc} */
     @Override public void init(ServiceContext ctx) {
-        clusterMgr.init();
+        clusterMgr = new TensorFlowClusterManager(ignite);
+        log.debug("Cluster maintainer initialized [clusterId=" + clusterId + "]");
     }
 
     /** {@inheritDoc} */
-    @Override public void execute(ServiceContext ctx) throws Exception {
+    @Override public void execute(ServiceContext ctx) {
         while (!ctx.isCancelled()) {
-            Thread.sleep(1000);
+            LockSupport.parkNanos(1_000_000);
+
+            boolean completed = clusterMgr.isUserScriptCompleted(clusterId);
+            if (completed)
+                break;
 
             boolean restartRequired = hasAffinityChanged();
 
+            if (restartRequired)
+                log.debug("Affinity mapping changed, cluster will be restarted [clusterId=" + clusterId + "]");
+
             if (!restartRequired) {
-                TensorFlowCluster cluster = clusterMgr.getCache().get(cacheName);
-                Map<UUID, List<LongRunningProcessStatus>> statuses = clusterMgr.getSrvProcMgr()
-                    .ping(cluster.getProcesses());
-
-                for (UUID nodeId : statuses.keySet()) {
-                    for (LongRunningProcessStatus status : statuses.get(nodeId)) {
-                        if (status.getState().equals(LongRunningProcessState.DONE)) {
-                            restartRequired = true;
-                            break;
+                try {
+                    TensorFlowCluster cluster = clusterMgr.getCluster(clusterId);
+                    Map<UUID, List<LongRunningProcessStatus>> statuses = clusterMgr.getSrvProcMgr()
+                        .ping(cluster.getProcesses());
+
+                    for (UUID nodeId : statuses.keySet()) {
+                        for (LongRunningProcessStatus status : statuses.get(nodeId)) {
+                            if (status.getState().equals(LongRunningProcessState.DONE)) {
+                                restartRequired = true;
+                                break;
+                            }
                         }
                     }
+
                 }
+                catch (Exception e) {
+                    log.error("Failed to check process statuses", e);
+                    restartRequired = true;
+                }
+
+                if (restartRequired)
+                    log.debug("Fail detected, cluster will be restarted [clusterId=" + clusterId + "]");
             }
 
             if (restartRequired) {
-                clusterMgr.stopClusterIfExists(cacheName);
+                clusterMgr.stopClusterIfExists(clusterId);
 
-                TensorFlowCluster cluster =  clusterMgr.getOrCreateCluster(cacheName);
+                TensorFlowCluster cluster = clusterMgr.createCluster(
+                    clusterId,
+                    jobArchive,
+                    str -> ignite.message().sendOrdered("us_out_" + clusterId, str, 60 * 1000),
+                    str -> ignite.message().sendOrdered("us_err_" + clusterId, str, 60 * 1000)
+                );
 
-                IgniteMessaging messaging = Ignition.ignite().message();
-                messaging.send(topicName, cluster);
+                ignite.message().send(topicName, Optional.of(cluster));
             }
         }
+
+        clusterMgr.stopClusterIfExists(clusterId);
+
+        ignite.message().send(topicName, Optional.empty());
+
+        log.debug("Cluster maintainer completed [clusterId=" + clusterId + "]");
     }
 
     /**
@@ -116,7 +159,7 @@ public class TensorFlowClusterMaintainer implements Service {
      * @return True if mapping has been changed, otherwise false.
      */
     private boolean hasAffinityChanged() {
-        Affinity<?> affinity = Ignition.ignite().affinity(cacheName);
+        Affinity<?> affinity = ignite.affinity(jobArchive.getUpstreamCacheName());
 
         int parts = affinity.partitions();
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/TensorFlowClusterManager.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/TensorFlowClusterManager.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/TensorFlowClusterManager.java
index 2d63195..cdbd774 100644
--- a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/TensorFlowClusterManager.java
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/TensorFlowClusterManager.java
@@ -17,36 +17,42 @@
 
 package org.apache.ignite.tensorflow.cluster;
 
-import java.io.Serializable;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.Executors;
 import java.util.concurrent.locks.Lock;
-import java.util.function.Supplier;
+import java.util.function.Consumer;
+import javax.cache.Cache;
 import org.apache.ignite.Ignite;
 import org.apache.ignite.IgniteCache;
 import org.apache.ignite.cache.CacheAtomicityMode;
 import org.apache.ignite.cache.CacheMode;
+import org.apache.ignite.cache.query.QueryCursor;
+import org.apache.ignite.cache.query.ScanQuery;
 import org.apache.ignite.configuration.CacheConfiguration;
 import org.apache.ignite.tensorflow.cluster.spec.TensorFlowClusterSpec;
 import org.apache.ignite.tensorflow.cluster.spec.TensorFlowServerAddressSpec;
 import org.apache.ignite.tensorflow.cluster.tfrunning.TensorFlowServer;
 import org.apache.ignite.tensorflow.cluster.tfrunning.TensorFlowServerManager;
+import org.apache.ignite.tensorflow.cluster.util.TensorFlowChiefRunner;
 import org.apache.ignite.tensorflow.cluster.util.TensorFlowClusterResolver;
+import org.apache.ignite.tensorflow.cluster.util.TensorFlowUserScriptRunner;
+import org.apache.ignite.tensorflow.core.util.CustomizableThreadFactory;
 
 /**
  * TensorFlow cluster manager that allows to start, maintain and stop TensorFlow cluster.
  */
-public class TensorFlowClusterManager implements Serializable {
-    /** */
-    private static final long serialVersionUID = -4847155592164802806L;
-
+public class TensorFlowClusterManager {
     /** TensorFlow cluster metadata cache name. */
     private static final String TF_CLUSTER_METADATA_CACHE_NAME = "TF_CLUSTER_METADATA_CACHE";
 
-    /** Ignite instance supplier. */
-    private final Supplier<Ignite> igniteSupplier;
+    /** Ignite instance. */
+    private final Ignite ignite;
 
     /** TensorFlow server manager. */
     private final TensorFlowServerManager srvProcMgr;
@@ -55,74 +61,61 @@ public class TensorFlowClusterManager implements Serializable {
     private final TensorFlowClusterResolver clusterRslvr;
 
     /** TensorFlow cluster metadata cache. */
-    private transient IgniteCache<String, TensorFlowCluster> cache;
+    private IgniteCache<UUID, TensorFlowCluster> cache;
+
+    /** TensorFlow chief runners. */
+    private ConcurrentMap<UUID, TensorFlowChiefRunner> chiefRunners;
+
+    /** TensorFlow user script runners. */
+    private ConcurrentMap<UUID, TensorFlowUserScriptRunner> userScriptRunners;
 
     /**
      * Constructs a new instance of TensorFlow cluster manager.
      *
-     * @param igniteSupplier Ignite instance supplier.
-     * @param <T> Type of serializable supplier.
+     * @param ignite Ignite instance.
      */
-    public <T extends Supplier<Ignite> & Serializable> TensorFlowClusterManager(T igniteSupplier) {
-        this(
-            igniteSupplier,
-            new TensorFlowServerManager(igniteSupplier),
-            new TensorFlowClusterResolver(igniteSupplier)
-        );
+    public TensorFlowClusterManager(Ignite ignite) {
+        assert ignite != null : "Ignite instance should not be null";
+
+        this.ignite = ignite;
+        this.srvProcMgr = new TensorFlowServerManager(ignite);
+        this.clusterRslvr = new TensorFlowClusterResolver(ignite, "TF", 10000, 1000);
+        this.cache = getOrCreateCache();
+        this.chiefRunners = new ConcurrentHashMap<>();
+        this.userScriptRunners = new ConcurrentHashMap<>();
     }
 
     /**
-     * Constructs a new instance of TensorFlow cluster manager.
+     * Returns cluster by identifier.
      *
-     * @param igniteSupplier Ignite instance supplier.
-     * @param srvProcMgr TensorFlow server manager.
-     * @param clusterRslvr TensorFlow cluster resolver.
+     * @param clusterId Cluster identifier.
+     * @return TensorFlow cluster.
      */
-    public <T extends Supplier<Ignite> & Serializable> TensorFlowClusterManager(T igniteSupplier,
-        TensorFlowServerManager srvProcMgr, TensorFlowClusterResolver clusterRslvr) {
-        assert igniteSupplier != null : "Ignite supplier should not be null";
-        assert srvProcMgr != null : "TensorFlow server manager should not be null";
-        assert clusterRslvr != null : "TensorFlow cluster resolver should not be null";
-
-        this.igniteSupplier = igniteSupplier;
-        this.srvProcMgr = srvProcMgr;
-        this.clusterRslvr = clusterRslvr;
-    }
-
-    /** Initializes TensorFlow cluster manager and gets or creates correspondent caches. */
-    public void init() {
-        clusterRslvr.init();
-
-        CacheConfiguration<String, TensorFlowCluster> cacheConfiguration = new CacheConfiguration<>();
-        cacheConfiguration.setName(TF_CLUSTER_METADATA_CACHE_NAME);
-        cacheConfiguration.setCacheMode(CacheMode.REPLICATED);
-        cacheConfiguration.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
-
-        Ignite ignite = igniteSupplier.get();
-        cache = ignite.getOrCreateCache(cacheConfiguration);
+    public TensorFlowCluster getCluster(UUID clusterId) {
+        return cache.get(clusterId);
     }
 
     /**
-     * Creates and starts a new TensorFlow cluster for the specified cache if it doesn't exist, otherwise returns
-     * existing one.
+     * Creates and starts a new TensorFlow cluster for the specified cache.
      *
-     * @param upstreamCacheName Upstream cache name.
+     * @param clusterId Cluster identifier.
+     * @param jobArchive Job archive.
      * @return TensorFlow cluster metadata.
      */
-    public TensorFlowCluster getOrCreateCluster(String upstreamCacheName) {
-        checkThatInitialized();
-
-        Lock clusterMgrCacheLock = cache.lock(upstreamCacheName);
+    public TensorFlowCluster createCluster(UUID clusterId, TensorFlowJobArchive jobArchive,
+        Consumer<String> userScriptOut, Consumer<String> userScriptErr) {
+        Lock clusterMgrCacheLock = cache.lock(clusterId);
         clusterMgrCacheLock.lock();
 
         try {
-            TensorFlowCluster cluster = cache.get(upstreamCacheName);
+            TensorFlowCluster cluster = cache.get(clusterId);
 
-            if (cluster == null) {
-                TensorFlowClusterSpec clusterSpec = clusterRslvr.resolveAndAcquirePorts(upstreamCacheName);
-                cluster = startCluster(clusterSpec);
-                cache.put(upstreamCacheName, cluster);
-            }
+            if (cluster != null)
+                throw new IllegalStateException("Cluster is already created [clusterId=" + clusterId + "]");
+
+            TensorFlowClusterSpec clusterSpec = clusterRslvr.resolveAndAcquirePorts(jobArchive.getUpstreamCacheName());
+            cluster = startCluster(clusterId, clusterSpec, jobArchive, userScriptOut, userScriptErr);
+            cache.put(clusterId, cluster);
 
             return cluster;
         }
@@ -134,21 +127,21 @@ public class TensorFlowClusterManager implements Serializable {
     /**
      * Stops TensorFlow cluster.
      *
-     * @param upstreamCacheName Upstream cache name.
+     * @param clusterId TensorFlow cluster identifier.
      */
-    public void stopClusterIfExists(String upstreamCacheName) {
-        checkThatInitialized();
-
-        Lock clusterMgrCacheLock = cache.lock(upstreamCacheName);
+    public void stopClusterIfExists(UUID clusterId) {
+        Lock clusterMgrCacheLock = cache.lock(clusterId);
         clusterMgrCacheLock.lock();
 
         try {
-            TensorFlowCluster cluster = cache.get(upstreamCacheName);
+            TensorFlowCluster cluster = cache.get(clusterId);
 
             if (cluster != null) {
+                stopChief(clusterId);
+                stopUserScript(clusterId);
                 srvProcMgr.stop(cluster.getProcesses(), true);
-                clusterRslvr.freePorts(cluster.getSpec());
-                cache.remove(upstreamCacheName);
+                clusterRslvr.releasePorts(cluster.getSpec());
+                cache.remove(clusterId);
             }
         }
         finally {
@@ -159,44 +152,146 @@ public class TensorFlowClusterManager implements Serializable {
     /** Destroys TensorFlow cluster manager and related caches. */
     public void destroy() {
         clusterRslvr.destroy();
-
-        Ignite ignite = igniteSupplier.get();
         ignite.destroyCache(TF_CLUSTER_METADATA_CACHE_NAME);
     }
 
     /**
      * Starts TensorFlow cluster using the specified specification and returns metadata of the started cluster.
      *
+     * @param clusterId Cluster identifier.
      * @param spec TensorFlow cluster specification.
      * @return TensorFlow cluster metadata.
      */
-    private TensorFlowCluster startCluster(TensorFlowClusterSpec spec) {
-        checkThatInitialized();
+    private TensorFlowCluster startCluster(UUID clusterId, TensorFlowClusterSpec spec, TensorFlowJobArchive jobArchive,
+        Consumer<String> userScriptOut, Consumer<String> userScriptErr) {
+        Map<String, List<TensorFlowServerAddressSpec>> jobs = spec.getJobs();
 
-        List<TensorFlowServer> srvs = new ArrayList<>();
+        Map<UUID, List<UUID>> workerProcesses = startWorkers(spec, jobs.get(TensorFlowClusterResolver.WORKER_JOB_NAME));
 
-        Map<String, List<TensorFlowServerAddressSpec>> jobs = spec.getJobs();
+        startChief(clusterId, spec);
+        startUserScript(clusterId, jobArchive, spec, userScriptOut, userScriptErr);
 
-        for (String jobName : jobs.keySet()) {
-            List<TensorFlowServerAddressSpec> tasks = jobs.get(jobName);
+        return new TensorFlowCluster(spec, workerProcesses);
+    }
+
+    /**
+     * Starts TensorFlow worker processes using the specified specification and returns identifiers of the started
+     * processes.
+     *
+     * @param spec TensorFlow cluster specification.
+     * @param tasks Worker tasks.
+     * @return Identifiers of the started processes.
+     */
+    private Map<UUID, List<UUID>> startWorkers(TensorFlowClusterSpec spec, List<TensorFlowServerAddressSpec> tasks) {
+        List<TensorFlowServer> srvs = new ArrayList<>();
 
+        if (tasks != null) {
             for (int i = 0; i < tasks.size(); i++) {
-                TensorFlowServer srvSpec = new TensorFlowServer(spec, jobName, i);
+                TensorFlowServer srvSpec = new TensorFlowServer(spec, TensorFlowClusterResolver.WORKER_JOB_NAME, i);
                 srvs.add(srvSpec);
             }
         }
 
-        Map<UUID, List<UUID>> processes = srvProcMgr.start(srvs);
+        return srvProcMgr.start(srvs);
+    }
 
-        return new TensorFlowCluster(spec, processes);
+    /**
+     * Starts chief process using the specified cluster specification.
+     *
+     * @param clusterId Cluster identifier.
+     * @param spec TensorFlow cluster specification.
+     */
+    private void startChief(UUID clusterId, TensorFlowClusterSpec spec) {
+        TensorFlowChiefRunner chiefRunner = new TensorFlowChiefRunner(
+            ignite,
+            Executors.newSingleThreadExecutor(
+                new CustomizableThreadFactory("tf-ch", true)
+            ),
+            spec,
+            System.out::println,
+            System.err::println
+        );
+
+        chiefRunner.start();
+
+        chiefRunners.put(clusterId, chiefRunner);
     }
 
     /**
-     * Checks that the component has been initialized.
+     * Stops chief process.
+     *
+     * @param clusterId Cluster identifier.
      */
-    private void checkThatInitialized() {
-        if (cache == null)
-            throw new IllegalStateException("TensorFlow Cluster Manager is not initialized");
+    private void stopChief(UUID clusterId) {
+        TensorFlowChiefRunner runner = chiefRunners.remove(clusterId);
+
+        if (runner != null)
+            runner.stop();
+    }
+
+    /**
+     * Starts user script processes using the specified job archive.
+     *
+     * @param clusterId Cluster identifier.
+     * @param jobArchive Job archive.
+     * @param clusterSpec Cluster specification.
+     */
+    private void startUserScript(UUID clusterId, TensorFlowJobArchive jobArchive, TensorFlowClusterSpec clusterSpec,
+        Consumer<String> out, Consumer<String> err) {
+        TensorFlowUserScriptRunner userScriptRunner = new TensorFlowUserScriptRunner(
+            ignite,
+            Executors.newSingleThreadExecutor(
+                new CustomizableThreadFactory("tf-us", true)
+            ),
+            jobArchive,
+            clusterSpec,
+            out,
+            err
+        );
+
+        userScriptRunner.start();
+
+        userScriptRunners.put(clusterId, userScriptRunner);
+    }
+
+    /**
+     * Stops user script process.
+     *
+     * @param clusterId Cluster identifier.
+     */
+    private void stopUserScript(UUID clusterId) {
+        TensorFlowUserScriptRunner runner = userScriptRunners.remove(clusterId);
+
+        if (runner != null)
+            runner.stop();
+    }
+
+    /**
+     * Checks if user script completed and returns result.
+     *
+     * @param clusterId Cluster identifier.
+     * @return {@code true} if user script completed, otherwise {@code false}.
+     */
+    public boolean isUserScriptCompleted(UUID clusterId) {
+        TensorFlowUserScriptRunner runner = userScriptRunners.get(clusterId);
+
+        return runner != null && runner.isCompleted();
+    }
+
+    /**
+     * Returns list of maintained TensorFlow clusters.
+     *
+     * @return List of maintained TensorFlow clusters.
+     */
+    public Map<UUID, TensorFlowCluster> getAllClusters() {
+        Map<UUID, TensorFlowCluster> res = new HashMap<>();
+
+        ScanQuery<UUID, TensorFlowCluster> qry = new ScanQuery<>();
+        QueryCursor<Cache.Entry<UUID, TensorFlowCluster>> cursor = cache.query(qry);
+        for (Cache.Entry<UUID, TensorFlowCluster> e : cursor)
+            res.put(e.getKey(), e.getValue());
+
+        return res;
     }
 
     /** */
@@ -204,8 +299,17 @@ public class TensorFlowClusterManager implements Serializable {
         return srvProcMgr;
     }
 
-    /** */
-    public IgniteCache<String, TensorFlowCluster> getCache() {
-        return cache;
+    /**
+     * Returns existing cluster manager cache or creates a new one.
+     *
+     * @return Cluster manager cache.
+     */
+    private IgniteCache<UUID, TensorFlowCluster> getOrCreateCache() {
+        CacheConfiguration<UUID, TensorFlowCluster> cacheConfiguration = new CacheConfiguration<>();
+        cacheConfiguration.setName(TF_CLUSTER_METADATA_CACHE_NAME);
+        cacheConfiguration.setCacheMode(CacheMode.REPLICATED);
+        cacheConfiguration.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
+
+        return ignite.getOrCreateCache(cacheConfiguration);
     }
 }

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/TensorFlowJobArchive.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/TensorFlowJobArchive.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/TensorFlowJobArchive.java
new file mode 100644
index 0000000..953cf76
--- /dev/null
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/TensorFlowJobArchive.java
@@ -0,0 +1,65 @@
+/*
+ * 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.
+ */
+
+package org.apache.ignite.tensorflow.cluster;
+
+import java.io.Serializable;
+
+/**
+ * TensorFlow job archive that keeps archived working directory and command to be executed.
+ */
+public class TensorFlowJobArchive implements Serializable {
+    /** */
+    private static final long serialVersionUID = -5977231383594482459L;
+
+    /** Upstream cache name. */
+    private final String upstreamCacheName;
+
+    /** Archived working directory. */
+    private final byte[] data;
+
+    /** Command to be executed with arguments. */
+    private final String[] commands;
+
+    /**
+     * Constructs a new instance of TensorFlow job archive.
+     *
+     * @param upstreamCacheName Upstream cache name.
+     * @param data Archived working directory.
+     * @param commands Command to be executed with arguments.
+     */
+    public TensorFlowJobArchive(String upstreamCacheName, byte[] data, String[] commands) {
+        this.upstreamCacheName = upstreamCacheName;
+        this.data = data;
+        this.commands = commands;
+    }
+
+    /** */
+    public String getUpstreamCacheName() {
+        return upstreamCacheName;
+    }
+
+    /** */
+    public byte[] getData() {
+        return data;
+    }
+
+    /** */
+    public String[] getCommands() {
+        return commands;
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/spec/TensorFlowClusterSpec.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/spec/TensorFlowClusterSpec.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/spec/TensorFlowClusterSpec.java
index a053b8e..ea813ea 100644
--- a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/spec/TensorFlowClusterSpec.java
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/spec/TensorFlowClusterSpec.java
@@ -23,6 +23,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
+import org.apache.ignite.Ignite;
 
 /**
  * TensorFlow cluster specification.
@@ -52,6 +53,44 @@ public class TensorFlowClusterSpec implements Serializable {
         return this;
     }
 
+    /**
+     * Formats cluster specification so that TensorFlow accepts it.
+     *
+     * @param ignite Ignite instance.
+     * @return Formatted cluster specification.
+     */
+    public String format(Ignite ignite) {
+        StringBuilder builder = new StringBuilder();
+
+        builder.append("{\n");
+
+        for (Map.Entry<String, List<TensorFlowServerAddressSpec>> entry : jobs.entrySet()) {
+            builder
+                .append("\t\"")
+                .append(entry.getKey())
+                .append("\" : [ ");
+
+            for (TensorFlowServerAddressSpec address : entry.getValue()) {
+                builder
+                    .append("\n\t\t\"")
+                    .append(address.format(ignite))
+                    .append("\", ");
+            }
+
+            if (!entry.getValue().isEmpty())
+                builder.delete(builder.length() - 2, builder.length());
+
+            builder.append("\n\t],\n");
+        }
+
+        if (!jobs.isEmpty())
+            builder.delete(builder.length() - 2, builder.length() - 1);
+
+        builder.append('}');
+
+        return builder.toString();
+    }
+
     /** */
     public Map<String, List<TensorFlowServerAddressSpec>> getJobs() {
         return jobs;

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/spec/TensorFlowServerAddressSpec.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/spec/TensorFlowServerAddressSpec.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/spec/TensorFlowServerAddressSpec.java
index 196b166..727a030 100644
--- a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/spec/TensorFlowServerAddressSpec.java
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/spec/TensorFlowServerAddressSpec.java
@@ -18,7 +18,9 @@
 package org.apache.ignite.tensorflow.cluster.spec;
 
 import java.io.Serializable;
+import java.util.Collection;
 import java.util.UUID;
+import org.apache.ignite.Ignite;
 
 /**
  * TensorFlow server address specification.
@@ -47,6 +49,18 @@ public class TensorFlowServerAddressSpec implements Serializable {
         this.port = port;
     }
 
+    /**
+     * Formats Server Address specification so that TensorFlow accepts it.
+     *
+     * @param ignite Ignite instance.
+     * @return Formatted server address specification.
+     */
+    public String format(Ignite ignite) {
+        Collection<String> names = ignite.cluster().forNodeId(nodeId).hostNames();
+
+        return names.iterator().next() + ":" + port;
+    }
+
     /** */
     public UUID getNodeId() {
         return nodeId;

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/tfrunning/TensorFlowServerManager.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/tfrunning/TensorFlowServerManager.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/tfrunning/TensorFlowServerManager.java
index 192f619..ee87089 100644
--- a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/tfrunning/TensorFlowServerManager.java
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/tfrunning/TensorFlowServerManager.java
@@ -17,36 +17,32 @@
 
 package org.apache.ignite.tensorflow.cluster.tfrunning;
 
-import java.io.Serializable;
-import java.util.function.Supplier;
-import org.apache.ignite.tensorflow.core.ProcessManager;
-import org.apache.ignite.tensorflow.core.ProcessManagerWrapper;
-import org.apache.ignite.tensorflow.core.pythonrunning.PythonProcess;
-import org.apache.ignite.tensorflow.core.pythonrunning.PythonProcessManager;
-import org.apache.ignite.tensorflow.cluster.spec.TensorFlowClusterSpec;
-import org.apache.ignite.tensorflow.cluster.spec.TensorFlowServerAddressSpec;
-import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
 import org.apache.ignite.Ignite;
 import org.apache.ignite.Ignition;
+import org.apache.ignite.tensorflow.cluster.spec.TensorFlowClusterSpec;
+import org.apache.ignite.tensorflow.cluster.spec.TensorFlowServerAddressSpec;
+import org.apache.ignite.tensorflow.core.ProcessManager;
+import org.apache.ignite.tensorflow.core.ProcessManagerWrapper;
+import org.apache.ignite.tensorflow.core.pythonrunning.PythonProcess;
+import org.apache.ignite.tensorflow.core.pythonrunning.PythonProcessManager;
 
 /**
  * TensorFlow server manager that allows to start, stop and make other actions with TensorFlow servers.
  */
 public class TensorFlowServerManager extends ProcessManagerWrapper<PythonProcess, TensorFlowServer> {
-    /** */
-    private static final long serialVersionUID = 8355019934723445973L;
+    /** TensorFlow server script formatter. */
+    private static final TensorFlowServerScriptFormatter scriptFormatter = new TensorFlowServerScriptFormatter();
 
     /**
      * Constructs a new instance of TensorFlow server manager.
      *
-     * @param igniteSupplier Ignite instance supplier.
-     * @param <T> Type of serializable supplier.
+     * @param ignite Ignite instance.
      */
-    public <T extends Supplier<Ignite> & Serializable> TensorFlowServerManager(T igniteSupplier) {
-        this(new PythonProcessManager(igniteSupplier));
+    public TensorFlowServerManager(Ignite ignite) {
+        this(new PythonProcessManager(ignite));
     }
 
     /**
@@ -61,7 +57,7 @@ public class TensorFlowServerManager extends ProcessManagerWrapper<PythonProcess
     /** {@inheritDoc} */
     @Override protected PythonProcess transformSpecification(TensorFlowServer spec) {
         return new PythonProcess(
-            formatPythonScript(spec),
+            scriptFormatter.format(spec, true, Ignition.ignite()),
             getNode(spec)
         );
     }
@@ -80,88 +76,4 @@ public class TensorFlowServerManager extends ProcessManagerWrapper<PythonProcess
 
         return addr.getNodeId();
     }
-
-    /**
-     * Formats TensorFlow server specification so that it's available to be passed into а python script.
-     *
-     * @param spec TensorFlow server specification.
-     * @return Formatted TensorFlow server specification.
-     */
-    private String formatPythonScript(TensorFlowServer spec) {
-        StringBuilder builder = new StringBuilder();
-
-        builder.append("import tensorflow as tf").append('\n');
-        builder.append("cluster = tf.train.ClusterSpec(")
-            .append(formatClusterSpec(spec.getClusterSpec()))
-            .append(')')
-            .append('\n');
-        builder.append("server = tf.train.Server(cluster");
-
-        if (spec.getJobName() != null)
-            builder.append(", job_name=\"").append(spec.getJobName()).append('"');
-
-        if (spec.getTaskIdx() != null)
-            builder.append(", task_index=").append(spec.getTaskIdx());
-
-        if (spec.getProto() != null)
-            builder.append(", protocol=\"").append(spec.getProto()).append('"');
-
-        builder.append(')').append('\n');
-        builder.append("server.join()").append('\n');
-
-        return builder.toString();
-    }
-
-    /**
-     * Formats TensorFlow cluster specification so that it's available to be passed into а python script.
-     *
-     * @param spec TensorFlow cluster specification.
-     * @return Formatted TensorFlow cluster specification.
-     */
-    public String formatClusterSpec(TensorFlowClusterSpec spec) {
-        StringBuilder builder = new StringBuilder();
-
-        builder.append("{\n");
-
-        for (Map.Entry<String, List<TensorFlowServerAddressSpec>> entry : spec.getJobs().entrySet()) {
-            builder
-                .append("\t\"")
-                .append(entry.getKey())
-                .append("\" : [ ");
-
-            for (TensorFlowServerAddressSpec address : entry.getValue()) {
-                builder
-                    .append("\n\t\t\"")
-                    .append(formatAddressSpec(address))
-                    .append("\", ");
-            }
-
-            if (!entry.getValue().isEmpty())
-                builder.delete(builder.length() - 2, builder.length());
-
-            builder.append("\n\t],\n");
-        }
-
-        if (!spec.getJobs().isEmpty())
-            builder.delete(builder.length() - 2, builder.length() - 1);
-
-        builder.append('}');
-
-        return builder.toString();
-    }
-
-    /**
-     * Formats TensorFlow server address specification so that it's available to be passed into а python script.
-     *
-     * @param spec TensorFlow server address specification.
-     * @return Formatted TensorFlow server address specification.
-     */
-    private String formatAddressSpec(TensorFlowServerAddressSpec spec) {
-        UUID nodeId = spec.getNodeId();
-
-        Ignite ignite = Ignition.localIgnite();
-        Collection<String> names = ignite.cluster().forNodeId(nodeId).hostNames();
-
-        return names.iterator().next() + ":" + spec.getPort();
-    }
 }

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/tfrunning/TensorFlowServerScriptFormatter.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/tfrunning/TensorFlowServerScriptFormatter.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/tfrunning/TensorFlowServerScriptFormatter.java
new file mode 100644
index 0000000..0645964
--- /dev/null
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/tfrunning/TensorFlowServerScriptFormatter.java
@@ -0,0 +1,62 @@
+/*
+ * 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.
+ */
+
+package org.apache.ignite.tensorflow.cluster.tfrunning;
+
+import org.apache.ignite.Ignite;
+
+/**
+ * Utils class that helps to format Python script that starts TensorFlow server.
+ */
+public class TensorFlowServerScriptFormatter {
+    /**
+     * Formats TensorFlow server specification so that it's available to be passed into а python script.
+     *
+     * @param srv Server specification.
+     * @param join Joins server by default or not.
+     * @param ignite Ignite instance.
+     * @return Formatted TensorFlow server script.
+     */
+    public String format(TensorFlowServer srv, boolean join, Ignite ignite) {
+        StringBuilder builder = new StringBuilder();
+
+        builder.append("import tensorflow as tf").append('\n');
+        builder.append("from tensorflow.contrib.ignite import IgniteDataset").append("\n");
+        builder.append("cluster = tf.train.ClusterSpec(")
+            .append(srv.getClusterSpec().format(ignite))
+            .append(')')
+            .append('\n');
+
+        builder.append("server = tf.train.Server(cluster");
+
+        if (srv.getJobName() != null)
+            builder.append(", job_name=\"").append(srv.getJobName()).append('"');
+
+        if (srv.getTaskIdx() != null)
+            builder.append(", task_index=").append(srv.getTaskIdx());
+
+        if (srv.getProto() != null)
+            builder.append(", protocol=\"").append(srv.getProto()).append('"');
+
+        builder.append(')').append('\n');
+
+        if (join)
+            builder.append("server.join()").append('\n');
+
+        return builder.toString();
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/util/ClusterPortManager.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/util/ClusterPortManager.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/util/ClusterPortManager.java
index 78087ab..462752c 100644
--- a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/util/ClusterPortManager.java
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/util/ClusterPortManager.java
@@ -18,23 +18,33 @@
 package org.apache.ignite.tensorflow.cluster.util;
 
 import java.io.Serializable;
+import java.net.NetworkInterface;
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.BitSet;
+import java.util.Enumeration;
+import java.util.List;
 import java.util.UUID;
 import java.util.concurrent.locks.Lock;
-import java.util.function.Supplier;
 import org.apache.ignite.Ignite;
 import org.apache.ignite.IgniteCache;
+import org.apache.ignite.IgniteLogger;
 import org.apache.ignite.cache.CacheAtomicityMode;
 import org.apache.ignite.cache.CacheMode;
+import org.apache.ignite.cluster.ClusterGroup;
+import org.apache.ignite.cluster.ClusterGroupEmptyException;
 import org.apache.ignite.configuration.CacheConfiguration;
 
 /**
- * Cluster port manager that allows to reliably {@link #acquirePort(UUID)} and {@link #freePort(UUID, int)} on the
+ * Cluster port manager that allows to reliably {@link #acquirePort(UUID)} and {@link #releasePort(UUID, int)} on the
  * cluster nodes.
  */
-public class ClusterPortManager implements Serializable {
-    /** */
-    private static final long serialVersionUID = -5116593574559007292L;
+public class ClusterPortManager {
+    /** Ignite instance. */
+    private final Ignite ignite;
+
+    /** Ignite logger. */
+    private final IgniteLogger log;
 
     /** Port manager cache name. */
     private final String portMgrCacheName;
@@ -45,11 +55,8 @@ public class ClusterPortManager implements Serializable {
     /** Port range size. */
     private final int cnt;
 
-    /** Ignite instance supplier. */
-    private final Supplier<Ignite> igniteSupplier;
-
     /** Port manager cache */
-    private transient IgniteCache<UUID, BitSet> cache;
+    private final IgniteCache<HostIdentifier, BitSet> cache;
 
     /**
      * Constructs a new instance of cluster port manager.
@@ -58,28 +65,19 @@ public class ClusterPortManager implements Serializable {
      * @param from Port range from point.
      * @param cnt Port range size.
      */
-    public <T extends Supplier<Ignite> & Serializable> ClusterPortManager(String poolName, int from, int cnt,
-        T igniteSupplier) {
+    public ClusterPortManager(Ignite ignite, String poolName, int from, int cnt) {
+        assert ignite != null : "Ignite instance should not be null";
         assert poolName != null : "Pool name should not be null";
         assert cnt >= 0 : "Count should not be negative";
         assert from >= 0 && cnt + from <= 0xFFFF : "Port range should be between 0 and 65535";
-        assert igniteSupplier != null : "Ignite supplier should not be null";
 
-        this.portMgrCacheName = String.format("PORT_MANAGER_CACHE_%s", poolName);
+        this.ignite = ignite;
+        this.log = ignite.log().getLogger(ClusterPortManager.class);
+
+        this.portMgrCacheName = String.format("PORT_MANAGER_%s_CACHE", poolName);
         this.from = from;
         this.cnt = cnt;
-        this.igniteSupplier = igniteSupplier;
-    }
-
-    /** Initializes port manager and creates or gets correspondent caches. */
-    public void init() {
-        CacheConfiguration<UUID, BitSet> cacheConfiguration = new CacheConfiguration<>();
-        cacheConfiguration.setName(portMgrCacheName);
-        cacheConfiguration.setCacheMode(CacheMode.REPLICATED);
-        cacheConfiguration.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
-
-        Ignite ignite = igniteSupplier.get();
-        cache = ignite.getOrCreateCache(cacheConfiguration);
+        this.cache = getOrCreateCache();
     }
 
     /**
@@ -89,13 +87,16 @@ public class ClusterPortManager implements Serializable {
      * @return Port to be acquired.
      */
     public int acquirePort(UUID nodeId) {
-        checkThatInitialized();
+        HostIdentifier hostId = getHostIdentifier(nodeId);
 
-        Lock lock = cache.lock(nodeId);
+        if (hostId == null)
+            throw new IllegalStateException("Can't find node [nodeId=" + nodeId + "]");
+
+        Lock lock = cache.lock(hostId);
         lock.lock();
 
         try {
-            BitSet ports = cache.get(nodeId);
+            BitSet ports = cache.get(hostId);
 
             if (ports == null)
                 ports = new BitSet(cnt);
@@ -106,8 +107,9 @@ public class ClusterPortManager implements Serializable {
                 throw new IllegalStateException("No free ports in range [from=" + from + ", cnt=" + cnt + "]");
 
             ports.set(free);
+            log.debug("Port acquired [nodeId=" + nodeId + ", port=" + (from + free) + "]");
 
-            cache.put(nodeId, ports);
+            cache.put(hostId, ports);
 
             return from + free;
         }
@@ -117,27 +119,31 @@ public class ClusterPortManager implements Serializable {
     }
 
     /**
-     * Frees acquired port on the specified node.
+     * Releases acquired port on the specified node.
      *
      * @param nodeId Node identifier.
      * @param port Acquired port to be free.
      */
-    public void freePort(UUID nodeId, int port) {
+    public void releasePort(UUID nodeId, int port) {
         assert port - from >= 0 && port - from < cnt : "Port not in the range";
 
-        checkThatInitialized();
+        HostIdentifier hostId = getHostIdentifier(nodeId);
+
+        if (hostId == null)
+            return;
 
-        Lock lock = cache.lock(nodeId);
+        Lock lock = cache.lock(hostId);
         lock.lock();
 
         try {
-            BitSet ports = cache.get(nodeId);
+            BitSet ports = cache.get(hostId);
 
             if (ports != null) {
                 ports.clear(port - from);
+                log.debug("Port released [nodeId=" + nodeId + ", port=" + port + "]");
 
                 if (ports.isEmpty())
-                    cache.remove(nodeId);
+                    cache.remove(hostId);
             }
         }
         finally {
@@ -147,15 +153,97 @@ public class ClusterPortManager implements Serializable {
 
     /** Destroys port manager and related caches. */
     public void destroy() {
-        Ignite ignite = igniteSupplier.get();
         ignite.destroyCache(portMgrCacheName);
     }
 
     /**
-     * Checks that the component has been initialized.
+     * Returns existed port pool cache or creates a new one.
+     *
+     * @return Port pool cache.
+     */
+    private IgniteCache<HostIdentifier, BitSet> getOrCreateCache() {
+        CacheConfiguration<HostIdentifier, BitSet> cacheConfiguration = new CacheConfiguration<>();
+        cacheConfiguration.setName(portMgrCacheName);
+        cacheConfiguration.setCacheMode(CacheMode.REPLICATED);
+        cacheConfiguration.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
+
+        return ignite.getOrCreateCache(cacheConfiguration);
+    }
+
+    /**
+     * Returns host identifier by node identifier.
+     *
+     * @param nodeId Node identifier.
+     * @return Host identifier.
      */
-    private void checkThatInitialized() {
-        if (cache == null)
-            throw new IllegalStateException("Cluster Port Manager is not initialized");
+    private HostIdentifier getHostIdentifier(UUID nodeId) {
+        try {
+            ClusterGroup grp = ignite.cluster().forNodeId(nodeId);
+
+            return ignite.compute(grp).call(() -> {
+                Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
+
+                List<byte[]> macAddrs = new ArrayList<>();
+
+                while (interfaces.hasMoreElements()) {
+                    NetworkInterface netItf = interfaces.nextElement();
+                    byte[] macAddr = netItf.getHardwareAddress();
+                    macAddrs.add(macAddr);
+                }
+
+                return new HostIdentifier(macAddrs.toArray(new byte[macAddrs.size()][]));
+            });
+        }
+        catch (ClusterGroupEmptyException e) {
+            return null;
+        }
+    }
+
+    /**
+     * Host identifier based on arrays of mac addresses of the host machine.
+     */
+    private static class HostIdentifier implements Serializable {
+        /** */
+        private static final long serialVersionUID = -7060231325908935162L;
+
+        /** Mac addresses. */
+        private final byte[][] macAddrs;
+
+        /**
+         * Constructs a new instance of host identifier.
+         *
+         * @param macAddrs Mac addresses.
+         */
+        public HostIdentifier(byte[][] macAddrs) {
+            this.macAddrs = macAddrs;
+        }
+
+        /** */
+        public byte[][] getMacAddrs() {
+            return macAddrs;
+        }
+
+        /** {@inheritDoc} */
+        @Override public boolean equals(Object o) {
+            if (this == o)
+                return true;
+            if (o == null || getClass() != o.getClass())
+                return false;
+
+            HostIdentifier that = (HostIdentifier)o;
+            if (macAddrs.length != that.macAddrs.length)
+                return false;
+
+            for (int i = 0; i < macAddrs.length; i++)
+                if (!Arrays.equals(macAddrs[i], that.macAddrs[i]))
+                    return false;
+
+            return true;
+        }
+
+        /** {@inheritDoc} */
+        @Override public int hashCode() {
+            return Arrays.hashCode(macAddrs);
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/util/TensorFlowChiefRunner.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/util/TensorFlowChiefRunner.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/util/TensorFlowChiefRunner.java
new file mode 100644
index 0000000..6998681
--- /dev/null
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/util/TensorFlowChiefRunner.java
@@ -0,0 +1,80 @@
+/*
+ * 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.
+ */
+
+package org.apache.ignite.tensorflow.cluster.util;
+
+import java.util.concurrent.ExecutorService;
+import java.util.function.Consumer;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.tensorflow.cluster.spec.TensorFlowClusterSpec;
+import org.apache.ignite.tensorflow.cluster.tfrunning.TensorFlowServer;
+import org.apache.ignite.tensorflow.cluster.tfrunning.TensorFlowServerScriptFormatter;
+import org.apache.ignite.tensorflow.core.pythonrunning.PythonProcessBuilderSupplier;
+import org.apache.ignite.tensorflow.core.util.AsyncNativeProcessRunner;
+import org.apache.ignite.tensorflow.core.util.NativeProcessRunner;
+
+/**
+ * Utils class that helps to start and stop chief process.
+ */
+public class TensorFlowChiefRunner extends AsyncNativeProcessRunner {
+    /** Ignite instance. */
+    private final Ignite ignite;
+
+    /** TensorFlow cluster specification. */
+    private final TensorFlowClusterSpec spec;
+
+    /** Output stream data consumer. */
+    private final Consumer<String> out;
+
+    /** Error stream data consumer. */
+    private final Consumer<String> err;
+
+    /**
+     * Constructs a new instance of TensorFlow chief runner.
+     *
+     * @param ignite Ignite instance.
+     * @param executor Executor to be used in {@link AsyncNativeProcessRunner}.
+     * @param spec TensorFlow cluster specification.
+     * @param out Output stream data consumer.
+     * @param err Error stream data consumer.
+     */
+    public TensorFlowChiefRunner(Ignite ignite, ExecutorService executor, TensorFlowClusterSpec spec,
+        Consumer<String> out, Consumer<String> err) {
+        super(ignite, executor);
+        this.ignite = ignite;
+        this.spec = spec;
+        this.out = out;
+        this.err = err;
+    }
+
+    /** {@inheritDoc} */
+    @Override public NativeProcessRunner doBefore() {
+        TensorFlowServer srv = new TensorFlowServer(spec, TensorFlowClusterResolver.CHIEF_JOB_NAME, 0);
+
+        return new NativeProcessRunner(
+            new PythonProcessBuilderSupplier(true).get(),
+            new TensorFlowServerScriptFormatter().format(srv, true, ignite),
+            out,
+            err
+        );
+    }
+
+    /** {@inheritDoc} */
+    @Override public void doAfter() {
+        // Do nothing.
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/9e884e5a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/util/TensorFlowClusterResolver.java
----------------------------------------------------------------------
diff --git a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/util/TensorFlowClusterResolver.java b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/util/TensorFlowClusterResolver.java
index e1d7d57..846af71 100644
--- a/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/util/TensorFlowClusterResolver.java
+++ b/modules/tensorflow/src/main/java/org/apache/ignite/tensorflow/cluster/util/TensorFlowClusterResolver.java
@@ -17,9 +17,7 @@
 
 package org.apache.ignite.tensorflow.cluster.util;
 
-import java.io.Serializable;
 import java.util.UUID;
-import java.util.function.Supplier;
 import org.apache.ignite.Ignite;
 import org.apache.ignite.cache.affinity.Affinity;
 import org.apache.ignite.cluster.ClusterNode;
@@ -29,32 +27,32 @@ import org.apache.ignite.tensorflow.cluster.spec.TensorFlowServerAddressSpec;
 /**
  * TensorFlow cluster resolver based on Ignite Cache affinity.
  */
-public class TensorFlowClusterResolver implements Serializable {
-    /** */
-    private static final long serialVersionUID = 631456775167710173L;
+public class TensorFlowClusterResolver {
+    /** TensorFlow worker job name. */
+    public static final String WORKER_JOB_NAME = "worker";
+
+    /** TensorFlow chief job name. */
+    public static final String CHIEF_JOB_NAME = "chief";
+
+    /** Ignite instance. */
+    private final Ignite ignite;
 
     /** Cluster port manager. */
     private final ClusterPortManager portMgr;
 
-    /** Ignite instance supplier. */
-    private final Supplier<Ignite> igniteSupplier;
-
     /**
      * Constructs a new instance of TensorFlow cluster resolver.
      *
-     * @param igniteSupplier Ignite instance supplier.
-     * @param <T> Type of serializable supplier.
+     * @param ignite Ignite instance.
      */
-    public <T extends Supplier<Ignite> & Serializable> TensorFlowClusterResolver(T igniteSupplier) {
-        assert igniteSupplier != null : "Ignite supplier should not be null";
-
-        this.igniteSupplier = igniteSupplier;
-        this.portMgr = new ClusterPortManager("TF_POOL", 10000, 100, igniteSupplier);
-    }
-
-    /** Initializes TensorFlow cluster resolver. */
-    public void init() {
-        portMgr.init();
+    public TensorFlowClusterResolver(Ignite ignite, String portPoolName, int portFrom, int portCnt) {
+        assert ignite != null : "Ignite instance should not be null";
+        assert portPoolName != null : "Port pool name should not be null";
+        assert portFrom >= 0 : "Port count should not be negative";
+        assert portCnt >= 0 && portCnt + portFrom <= 0xFFFF : "Port range should be between 0 and 65535";
+
+        this.ignite = ignite;
+        this.portMgr = new ClusterPortManager(ignite, portPoolName, portFrom, portCnt);
     }
 
     /**
@@ -64,38 +62,60 @@ public class TensorFlowClusterResolver implements Serializable {
      * @return TensorFlow cluster specification.
      */
     public TensorFlowClusterSpec resolveAndAcquirePorts(String upstreamCacheName) {
-        Ignite ignite = igniteSupplier.get();
-        Affinity<?> affinity = ignite.affinity(upstreamCacheName);
-
-        int parts = affinity.partitions();
-
         TensorFlowClusterSpec spec = new TensorFlowClusterSpec();
 
-        for (int part = 0; part < parts; part++) {
-            ClusterNode node = affinity.mapPartitionToNode(part);
-            UUID nodeId = node.id();
-
-            int port = portMgr.acquirePort(nodeId);
-
-            spec.addTask("WORKER", nodeId, port);
-        }
+        resolveAndAcquirePortsForWorkers(spec, upstreamCacheName);
+        resolveAndAcquirePortsForChief(spec);
 
         return spec;
     }
 
     /**
-     * Frees ports acquired for the given cluster specification.
+     * Releases ports acquired for the given cluster specification.
      *
      * @param spec TensorFlow cluster specification.
      */
-    public void freePorts(TensorFlowClusterSpec spec) {
+    public void releasePorts(TensorFlowClusterSpec spec) {
         for (String jobName : spec.getJobs().keySet())
             for (TensorFlowServerAddressSpec address : spec.getJobs().get(jobName))
-                portMgr.freePort(address.getNodeId(), address.getPort());
+                portMgr.releasePort(address.getNodeId(), address.getPort());
     }
 
     /** Destroys TensorFlow cluster resolver. */
     public void destroy() {
         portMgr.destroy();
     }
+
+    /**
+     * Resolves TensorFlow cluster worker jobs and acquires ports.
+     *
+     * @param spec TensorFlow cluster specification.
+     * @param upstreamCacheName Upstream cache name.
+     */
+    private void resolveAndAcquirePortsForWorkers(TensorFlowClusterSpec spec, String upstreamCacheName) {
+        Affinity<?> affinity = ignite.affinity(upstreamCacheName);
+        int parts = affinity.partitions();
+
+        for (int part = 0; part < parts; part++) {
+            ClusterNode node = affinity.mapPartitionToNode(part);
+            UUID nodeId = node.id();
+
+            int port = portMgr.acquirePort(nodeId);
+
+            spec.addTask(WORKER_JOB_NAME, nodeId, port);
+        }
+    }
+
+    /**
+     * Resolves TensorFlow cluster chief job and acquires ports.
+     *
+     * @param spec TensorFlow cluster specification.
+     */
+    private void resolveAndAcquirePortsForChief(TensorFlowClusterSpec spec) {
+        ClusterNode chiefNode = ignite.cluster().localNode();
+        UUID chiefNodeId = chiefNode.id();
+        int chiefPort = portMgr.acquirePort(chiefNodeId);
+
+        spec.addTask(CHIEF_JOB_NAME, chiefNodeId, chiefPort);
+    }
 }