-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
182 lines (164 loc) · 7.68 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tkl.Jumbo.Dfs;
using System.Runtime.Remoting;
using System.Net.Sockets;
using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Diagnostics;
using Tkl.Jumbo.Jet;
using System.Xml.Serialization;
using System.Threading;
using Tkl.Jumbo.IO;
using Tkl.Jumbo.Jet.Channels;
namespace ClientSample
{
class Program
{
static void Main(string[] args)
{
if( args.Length < 2 || args.Length > 4 )
{
Console.WriteLine("Usage: ClientSample.exe <task> <inputfile> [aggregate task count] [outputpath]");
return;
}
string task = args[0];
string input = args[1];
int aggregateTaskCount = args.Length == 3 ? Convert.ToInt32(args[2]) : 1;
string output = args.Length == 4 ? args[3] : "/output";
Type inputTaskType;
Type aggregateTaskType;
switch( task )
{
case "linecount":
inputTaskType = typeof(LineCounterTask);
aggregateTaskType = typeof(LineCounterAggregateTask);
break;
case "wordcount":
inputTaskType = typeof(WordCountTask);
aggregateTaskType = typeof(WordCountAggregateTask);
break;
default:
Console.WriteLine("Unknown task.");
return;
}
Console.WriteLine("Running task {0}, input file {1}, {2} aggregate tasks, output path {3}.", task, input, aggregateTaskCount, output);
DfsClient dfsClient = new DfsClient();
IJobServerClientProtocol jobServer = JetClient.CreateJobServerClient();
Console.WriteLine("Press any key to start");
Console.ReadKey();
//Stopwatch sw = new Stopwatch();
//sw.Start();
RunJob(dfsClient, jobServer, inputTaskType, aggregateTaskType, input, "/output", aggregateTaskCount);
//sw.Stop();
//Console.WriteLine(sw.Elapsed);
Console.WriteLine("Done, press any key to exit");
Console.ReadKey();
}
private static void RunJob(DfsClient dfsClient, IJobServerClientProtocol jobServer, Type inputTaskType, Type aggregateTaskType, string fileName, string outputPath, int aggregateTaskCount)
{
const int interval = 5000;
Guid jobId = StartJob(dfsClient, jobServer, inputTaskType, aggregateTaskType, fileName, outputPath, aggregateTaskCount);
if( jobId != Guid.Empty )
{
JobStatus status;
while( !jobServer.WaitForJobCompletion(jobId, interval) )
{
status = jobServer.GetJobStatus(jobId);
Console.WriteLine(status);
}
status = jobServer.GetJobStatus(jobId);
Console.WriteLine(status);
Console.WriteLine();
Console.WriteLine("Job completed.");
Console.WriteLine("Start time: {0:yyyy'-'MM'-'dd' 'HH':'mm':'ss'.'fff}", status.StartTime.ToLocalTime());
Console.WriteLine("End time: {0:yyyy'-'MM'-'dd' 'HH':'mm':'ss'.'fff}", status.EndTime.ToLocalTime());
TimeSpan duration = status.EndTime - status.StartTime;
Console.WriteLine("Duration: {0} ({1}s)", duration, duration.TotalSeconds);
}
}
private static Guid StartJob(DfsClient dfsClient, IJobServerClientProtocol jobServer, Type inputTaskType, Type aggregateTaskType, string fileName, string outputPath, int aggregateTaskCount)
{
Tkl.Jumbo.Dfs.File file = dfsClient.NameServer.GetFileInfo(fileName);
if( file == null )
{
Console.WriteLine("Input file not found.");
return Guid.Empty;
}
int blockSize = dfsClient.NameServer.BlockSize;
JobConfiguration config = new JobConfiguration()
{
AssemblyFileName = Path.GetFileName(inputTaskType.Assembly.Location),
Tasks = new List<TaskConfiguration>(),
Channels = new List<ChannelConfiguration>()
};
string[] tasks = new string[file.Blocks.Count];
for( int x = 0; x < file.Blocks.Count; ++x )
{
config.Tasks.Add(new TaskConfiguration()
{
TaskID = inputTaskType.Name + (x + 1).ToString(),
TypeName = inputTaskType.FullName,
DfsInput = new TaskDfsInput()
{
Path = fileName,
Block = x,
RecordReaderType = typeof(LineRecordReader).AssemblyQualifiedName
}
});
tasks[x] = inputTaskType.Name + (x + 1).ToString();
}
Type interfaceType = FindGenericInterfaceType(aggregateTaskType, typeof(ITask<,>));
Type outputType = interfaceType.GetGenericArguments()[1];
Type recordWriterType = typeof(TextRecordWriter<>).MakeGenericType(outputType);
string[] outputTasks = new string[aggregateTaskCount];
for( int x = 0; x < aggregateTaskCount; ++x )
{
config.Tasks.Add(new TaskConfiguration()
{
TaskID = aggregateTaskType.Name + (x + 1).ToString(),
TypeName = aggregateTaskType.FullName,
DfsOutput = new TaskDfsOutput()
{
Path = DfsPath.Combine(outputPath, string.Format("result{0}.txt", x + 1)),
RecordWriterType = recordWriterType.AssemblyQualifiedName
}
});
outputTasks[x] = aggregateTaskType.Name + (x + 1).ToString();
}
config.Channels.Add(new ChannelConfiguration()
{
ChannelType = ChannelType.File,
InputTasks = tasks,
OutputTasks = outputTasks,
PartitionerType = typeof(HashPartitioner<>).MakeGenericType(outputType).AssemblyQualifiedName
});
dfsClient.NameServer.Delete(outputPath, true);
dfsClient.NameServer.CreateDirectory(outputPath);
Job job = jobServer.CreateJob();
Console.WriteLine(job.JobID);
using( DfsOutputStream stream = dfsClient.CreateFile(job.JobConfigurationFilePath) )
{
config.SaveXml(stream);
}
dfsClient.UploadFile(inputTaskType.Assembly.Location, DfsPath.Combine(job.Path, config.AssemblyFileName));
jobServer.RunJob(job.JobID);
return job.JobID;
}
private static Type FindGenericInterfaceType(Type type, Type interfaceType)
{
// This is necessary because while in .Net you can use type.GetInterface with a generic interface type,
// in Mono that only works if you specify the type arguments which is precisely what we don't want.
Type[] interfaces = type.GetInterfaces();
foreach( Type i in interfaces )
{
if( i.IsGenericType && i.GetGenericTypeDefinition() == interfaceType )
return i;
}
throw new ArgumentException(string.Format("Type {0} does not implement interface {1}.", type, interfaceType));
}
}
}