/
ClusterController.cs
212 lines (177 loc) · 7.93 KB
/
ClusterController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using Azure;
using ClusterHead.Model;
using Microsoft.Azure.Batch;
using Microsoft.Azure.Batch.Common;
using Microsoft.Azure.Batch.Conventions.Files;
using Shared.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace ClusterHead
{
public class ClusterController
{
private string outputContainerSasUrl;
private readonly IClusterService clusterService;
public ClusterController(IClusterService clusterService) => this.clusterService = clusterService;
public async Task<ICloudJobWrapper> CreateJobAsync(string jobId, bool useStaticPool = true, string poolId = "staticpool-1")
{
ICloudJobWrapper job;
if (useStaticPool)
{
job = this.clusterService.CreateJob(jobId, Tools.UseStaticPool(poolId));
}
else
{
job = this.clusterService.CreateJob(jobId, Tools.UseAutoPool());
}
job.SetOnAllTasksComplete(OnAllTasksComplete.TerminateJob);
await job.CommitAsync();
return job;
}
public async Task<string> GetOuputContainerSasUrl(ICloudJobWrapper job)
{
var storageAccount = this.clusterService.GetStorageAccount();
await job.PrepareOutputStorageAsync(storageAccount);
return job.GetOutputStorageContainerUrl(storageAccount);
}
public async Task<IEnumerable<string>> CreateTasksAsync(string jobId, IEnumerable<Unit> units)
{
var job = this.clusterService.GetJob(jobId);
var taskIds = new List<string>();
this.outputContainerSasUrl = await GetOuputContainerSasUrl(job);
for (var i = 0; i < units.Count(); i++)
{
var id = $"task-{i}";
var commandLine = $"cmd /c %AZ_BATCH_APP_PACKAGE_CONSUMER#1.0.0%\\Consumer.exe %AZ_BATCH_TASK_WORKING_DIR%\\input-{i}.json";
var task = new CloudTask(id, commandLine)
{
ApplicationPackageReferences = new List<ApplicationPackageReference>()
{
new ApplicationPackageReference { ApplicationId = "consumer", Version = "1.0.0" }
},
EnvironmentSettings = new List<EnvironmentSetting>()
{
new EnvironmentSetting("JOB_OUTPUT_CONTAINER_URI", this.outputContainerSasUrl)
},
ResourceFiles = new List<ResourceFile> { ResourceFile.FromAutoStorageContainer("input-files", blobPrefix: $"input-{i}.json") },
Constraints = new TaskConstraints(retentionTime: TimeSpan.FromDays(1)),
};
taskIds.Add(id);
job.AddTask(task);
}
return taskIds;
}
public async Task UploadResourceFilesAsync(IEnumerable<Unit> units, string containerName)
{
var blobContainerClient = this.clusterService.GetBlobContainerClient(containerName);
try
{
var result = await blobContainerClient.CreateIfNotExistsAsync();
}
catch (RequestFailedException e)
{
Console.WriteLine(e.Message);
}
var unitsArray = units.ToArray();
for (var i = 0; i < units.Count(); i++)
{
var json = JsonSerializer.Serialize(unitsArray[i]);
using var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(json));
try
{
await blobContainerClient.DeleteBlobIfExistsAsync($"input-{i}.json");
await blobContainerClient.UploadBlobAsync($"input-{i}.json", memoryStream);
}
catch (RequestFailedException e)
{
Console.WriteLine(e.ErrorCode); ;
}
}
}
public async Task CreateStaticPoolIfNotExistsAsync(string poolId, int targetLowPriorityComputeNodes, int taskSlotsPerNode)
{
var imageReference = new ImageReference(
Offer.WINDOWSSERVER,
Publisher.MICROSOFTWINDOWSSERVER,
Sku.DATACENTER_SMALLDISK_2012_R2);
var applications = new List<ApplicationPackageReference>()
{
new ApplicationPackageReference { ApplicationId = "consumer", Version = "1.0.0" }
};
try
{
var vmConfiguration = new VirtualMachineConfiguration(imageReference, nodeAgentSkuId: "batch.node.windows amd64");
var pool = this.clusterService.CreatePool(
poolId,
VirtualMachineSize.STANDARD_A1,
vmConfiguration,
targetLowPriorityComputeNodes);
pool.SetApplicationPackageReferences(applications);
pool.SetTaskSlotsPerNode(taskSlotsPerNode);
await pool.CommitAsync();
}
catch (BatchException e)
{
if (e.RequestInformation?.BatchError?.Code == BatchErrorCodeStrings.PoolExists)
{
Console.WriteLine($"The pool {poolId} already exists!");
}
else
{
throw;
}
}
}
public async Task WaitForTasks(string jobId, TimeSpan timeout)
{
var taskStateMonitor = this.clusterService.CreateTaskStateMonitor();
var tasks = await this.clusterService.GetTasksAsync(jobId);
// Wait until all tasks are in complete state, check every 3 seconds
var controlParams = new ODATAMonitorControl() { DelayBetweenDataFetch = TimeSpan.FromSeconds(3) };
taskStateMonitor.WaitAll(tasks, TaskState.Completed, timeout, controlParams);
}
public async Task PrintTaskOutputAsync(string jobId)
{
var tasks = await this.clusterService.GetTasksAsync(jobId);
foreach (var task in tasks)
{
Console.WriteLine($"## Task: {task.GetId()} ###########################################################");
//Read the standard out of the task
var standardOutFile = await task.GetNodeFileAsync(Constants.StandardOutFileName);
var standardOutText = await standardOutFile.ReadAsStringAsync();
Console.WriteLine("Standard out:");
Console.WriteLine(standardOutText);
//Read the standard error of the task
var standardErrorFile = await task.GetNodeFileAsync(Constants.StandardErrorFileName);
var standardErrorText = await standardErrorFile.ReadAsStringAsync();
if (!string.IsNullOrEmpty(standardErrorText))
{
Console.WriteLine("Standard error:");
Console.WriteLine(standardErrorText);
}
Console.WriteLine();
}
}
public async Task<IEnumerable<Unit>> RetrieveOutputData(string jobId)
{
var tasks = await clusterService.GetTasksAsync(jobId);
var units = new List<Unit>();
foreach (var task in tasks)
{
var taskOutputStorage = new TaskOutputStorage(new Uri(this.outputContainerSasUrl), task.GetId());
var output = await taskOutputStorage.GetOutputAsync(TaskOutputKind.TaskOutput, "output.txt");
var stream = await output.OpenReadAsync();
var reader = new StreamReader(stream);
var unitAsJson = reader.ReadToEnd();
var unit = JsonSerializer.Deserialize<Unit>(unitAsJson);
units.Add(unit);
}
return units;
}
}
}