In the last post related to Azure Batch we talk about the theoretical parts. We discover what kind of batch tasks we can have, what is a merge task and many more.
In this blog post we will focus on how we can create a batch and how we can push our own logic into a batch. We will focus on Azure Batch using a .NET library for now – running an .exe application as a task logic.
Base Terminology
Before going forward it would a good idea to review the batch terminology:
Account - A uniquely identified entity within the service. All processing is done through a Batch account.
Pool – A collection of task virtual machines on which task applications run.
Task Virtual Machine - A machine that is assigned to a pool and is used by tasks that are assigned to jobs that run in the pool.
Task Virtual Machine User – A user account on a task virtual machine.
Workitem - Specifies how computation is performed on task virtual machines in a pool.
Job - A running instance of a work item and consists of a collection of tasks.
Task - An application that is associated with a job and runs on a task virtual machine.
File – Contains the information that is processed by a task.
Source: http://azure.microsoft.com/en-us/documentation/articles/batch-dotnet-get-started/
Azure Batch SDK
Before starting, you should download from Visual Studio Gallery (Tools->Extensions and Updates), the SDK for Azure Batch. This will contain a template that can be used to create a new Batch.
Create an Azure Batch Project
To be able to define an Azure Batch you will need to create a new VS project of type “Azure Batch Embarrassingly Parallel Cloud Application”. First step is to specify the Application Name and Job Name.
Entry Point
This information can be changed later on from ‘ApplicationDefinition’ that represent the main entry point in the application. Using this definition we can control:
Job Splitter
As the name says, this class will split your job in subtask. In this way you can run in parallel multiple tasks. Things that you should remember:
Define a task (ParallelTaskProcessor.RunExternalTaskProcess)
At this level you define the task itselft. In our case, at this level you need to specify the executable that contains the logic of task, input and output files. Don’t forget that input and output of each task is based on files (from blob storage).
When you can execute an external executable you can specify the fallowing information:
Merge Task (ParallelTaskProcessor.RunExternalMergeProcess)
To implement the merge task you will need to override the above method. In theory this task should take all the output files from all tasks and merge it in only one task. The default merge task is merging all output files in one result.
In this post we saw how we can define a job that runs an Azure Batch Application. In the next post we will see how we can define an Azure Batch job using a library and not an .exe application
In this blog post we will focus on how we can create a batch and how we can push our own logic into a batch. We will focus on Azure Batch using a .NET library for now – running an .exe application as a task logic.
Base Terminology
Before going forward it would a good idea to review the batch terminology:
Account - A uniquely identified entity within the service. All processing is done through a Batch account.
Pool – A collection of task virtual machines on which task applications run.
Task Virtual Machine - A machine that is assigned to a pool and is used by tasks that are assigned to jobs that run in the pool.
Task Virtual Machine User – A user account on a task virtual machine.
Workitem - Specifies how computation is performed on task virtual machines in a pool.
Job - A running instance of a work item and consists of a collection of tasks.
Task - An application that is associated with a job and runs on a task virtual machine.
File – Contains the information that is processed by a task.
Source: http://azure.microsoft.com/en-us/documentation/articles/batch-dotnet-get-started/
Azure Batch SDK
Before starting, you should download from Visual Studio Gallery (Tools->Extensions and Updates), the SDK for Azure Batch. This will contain a template that can be used to create a new Batch.
Create an Azure Batch Project
To be able to define an Azure Batch you will need to create a new VS project of type “Azure Batch Embarrassingly Parallel Cloud Application”. First step is to specify the Application Name and Job Name.
Entry Point
This information can be changed later on from ‘ApplicationDefinition’ that represent the main entry point in the application. Using this definition we can control:
- Name of the Job
- Name of the Application
- Job Splitter – This step gives us the possibility to split a job in multiple tasks
- Task Processor – The task logic itself
public class ApplicationDefinition
{
public static readonly CloudApplication Application = new ParallelCloudApplication
{
ApplicationName = "FooApp",
JobType = "FooJob",
JobSplitterType = typeof(AzureBatchCloudApplication1JobSplitter),
TaskProcessorType = typeof(AzureBatchCloudApplication1TaskProcessor)
};
}
Remarks: This template can be very useful, because we can hook directly our own logic, simple and clean.Job Splitter
As the name says, this class will split your job in subtask. In this way you can run in parallel multiple tasks. Things that you should remember:
- Task index - Is optionally, is used only to specify an index to each task
- Task ID – Represent the Id of each task, you should specified this value
- Parameters – Is a list of parameters that can be used to transmit different parameters to a task
- RequiredFiles – Can be used to specify what files are needs to run the task
- DependsOn – It is used when you want to specify that a task depends on another task. The ID of the depending task needs to be specified. In this way the current task will not start until the depending task has not run.
- State – Is used to group multiple tasks in the same group. It is similar win ‘DependsOn’, but at collection level. In this way you can create a groups of tasks that will run in the order of State value. First, Azure Batch will run in parallel tasks with State value equal to 0, after that the one equal with 1 and so on. Using both solutions (DependsOn or State) you can create different execution model (similar with trees).
- IsMerge – There can be only one task of this kind and is used to merge the result of the tasks. This task needs to be executed as the last task (by default, you don’t need to define this task, there is a default merge tasks implemented).
protected override IEnumerable<TaskSpecifier> Split(IJob job, JobSplitSettings settings)
{
return new List<TaskSpecifier>
{
new TaskSpecifier
{
RequiredFiles = job.Files,
TaskId = 1,
TaskIndex = 1,
State = 0
Parameters = job.Parameters,
},
new TaskSpecifier
{
RequiredFiles = job.Files,
TaskId = 2,
TaskIndex = 2,
DependsOn = 1,
State = 0
Parameters = job.Parameters,
},
new TaskSpecifier
{
RequiredFiles = job.Files,
TaskId = 2,
TaskIndex = 2,
DependsOn = 1,
State = 1
Parameters = job.Parameters,
},
...
};
}
Define a task (ParallelTaskProcessor.RunExternalTaskProcess)
At this level you define the task itselft. In our case, at this level you need to specify the executable that contains the logic of task, input and output files. Don’t forget that input and output of each task is based on files (from blob storage).
When you can execute an external executable you can specify the fallowing information:
- CommandPath – The path to the executable file (the task logic)
- Arguments – The arguments that are send to executable file
- WorkingDirectory – The directory used as working directory
- CancellationToken – You obtain it from TaskExecutionSettings.CancellationToken and is used to transmit the cancelation token to you executable.
protected override TaskProcessResult RunExternalTaskProcess(ITask task, TaskExecutionSettings settings)
{
string inputFile = task.RequiredFiles[0].Name;
string outputFile = string.Format("{0}.scv", task.TaskId);
ExternalProcess process = new ExternalProcess
{
CommandPath = ExecutablePath("fooTask.exe"),
Arguments = string.Format("input:{0} output:{1}", inputFile, outputFile),
WorkingDirectory = LocalStoragePath
};
ExternalProcessResult processOutput = process.Run();
return TaskProcessResult.FromExternalProcessResult(processOutput);
}
At this level, based on how you define your job splitter, each task can use different input files and to generate different outputs.Merge Task (ParallelTaskProcessor.RunExternalMergeProcess)
To implement the merge task you will need to override the above method. In theory this task should take all the output files from all tasks and merge it in only one task. The default merge task is merging all output files in one result.
protected override JobResult RunExternalMergeProcess(ITask mergeTask, TaskExecutionSettings settings)
{
...
}
In this post we saw how we can define a job that runs an Azure Batch Application. In the next post we will see how we can define an Azure Batch job using a library and not an .exe application
Comments
Post a Comment