-
Notifications
You must be signed in to change notification settings - Fork 0
/
findEngineDeployment.pl
executable file
·316 lines (279 loc) · 12.3 KB
/
findEngineDeployment.pl
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/perl -ws
#
# ©2012–2015 Autodesk Development Sàrl
# Created by Ventsislav Zhechev on 18 Dec 2012
#
# Version history
# v0.7.1 Last modified on 06 Jan 2015 by Ventsislav Zhechev
# Fixed a bug that came up when a full deployment could be completed while leaving some servers empty.
#
# v0.7 Last modified on 03 Nov 2014 by Ventsislav Zhechev
# Disabled the code dealing with setting up deployment commands for an XX–EN engine, as that engine is no longer used.
#
# v0.6 Last modified on 30 Mar 2014 by Ventsislav Zhechev
# Added a command-line option to specify the XX–EN engine.
# Added a command-line option to specify the cap on maximum not-full deployments to collect.
#
# v0.5.2 Last modified on 19 Mar 2014 by Ventsislav Zhechev
# Updated the status output.
#
# v0.5.1 Last modified on 17 Mar 2014 by Ventsislav Zhechev
# Added an accounting of the number of deployed instances to allow shutting down operation as soon as a full deployment is found.
# Rearranged the deployment details output order.
#
# v0.5 Last modified on 14 Mar 2014 by Ventsislav Zhechev
# Modified to only store progressively better deployments (i.e. leaving less and less memory unused).
# Execution is stopped after a maximum of 500 maximal deployments are found.
# Updated the statistics output after execution.
# Significantly optimised the engine placement algorithm by cutting off bad branches early.
#
# v0.4.1 Last modified on 10 Mar 2014 by Ventsislav Zhechev
# Updated the usage string.
# Now we are sorting the list of engines by memory requirement for deployment purposes.
#
# v0.4 Last modified on 13 Feb 2014 by Ventsislav Zhechev
# Added two extra printout modes to simplify the use of the output with other systems.
#
# v0.3 Last modified on 22 Jan 2013 by Ventsislav Zhechev
# Added progress dots.
# Parametrised server deployment order.
#
# v0.2 Last modified on 24 Dec 2012 by Ventsislav Zhechev
# Now we are inspecting all possible deployments.
#
# v0.1 Last modified on 21 Dec 2012 by Ventsislav Zhechev
# Initial version.
#
########################
use strict;
use utf8;
use Encode qw/encode decode/;
use Storable qw/dclone/;
use List::Util qw/max/;
$| = 1;
our ($serverFile, $engineFile, $largeFirst, $maxDeploymentsCap);
die encode "utf-8", "Usage: $0 -serverFile=… -engineFile=… [-largeFirst] [-maxDeploymentsCap]\n"
unless defined $serverFile && defined $engineFile;
my $serverOrder = defined $largeFirst ? -1 : 1;
print encode "utf-8", "Deploying on ".(defined $largeFirst ? "large" : "small")." servers first.\n";
my %servers;
my %engines;
$maxDeploymentsCap ||= 500;
{
local $/ = undef;
open my $servers, "<$serverFile"
or die encode "utf-8", "Cannot read server file “$serverFile”!\n";
%servers = split ' ', <$servers>;
close $servers;
foreach (keys %servers) {
delete $servers{$_} if /^\#/;
}
open my $engines, "<$engineFile"
or die encode "utf-8", "Cannot read engine file “$engineFile”!\n";
%engines = split /[\t\n]/, <$engines>;
close $engines;
foreach (keys %engines) {
delete $engines{$_} if /^\#/;
}
%engines = map {$_ => [(split ' ', $engines{$_})]} keys %engines;
}
my $totalServerMemory;
#Sort the servers based on user preference and calculate the total server memory
my @serverList = grep {$totalServerMemory += $servers{$_}} sort {($servers{$a} <=> $servers{$b})*$serverOrder || $a cmp $b} keys %servers;
my $totalInstances;
#This engine order is necessary to help optimise the deployment process
my @engineList = grep {$totalInstances += $engines{$_}->[1]} sort {($engines{$a}->[0] <=> $engines{$b}->[0]) || $a cmp $b} keys %engines;
#print STDERR "We have $totalInstances total instances to deploy.\n";
my $maxMemoryRequirement = $engines{$engineList[0]}->[0];
map {$maxMemoryRequirement = $_->[0] if $_->[0] > $maxMemoryRequirement} values %engines;
my %deployment = map {$_ => {(map {$_ => 0} @engineList)}} @serverList;
my %deployedEngines;
my %deployedEngineIDs = map {$_ => []} @serverList;
my %testServers = %{dclone \%servers};
my %testEngines = %{dclone \%engines};
my $deployments = 0;
my @deployments;
my $leastMemoryRemaining = $totalServerMemory;
my $leastMemoryDeployments = 0;
my $totalDeployments = 0;
my $isFullDeployment = 0;
&deployServer();
my $remainingServerMemory = $totalServerMemory;
print encode "utf-8", "Total deployment attempts: $deployments\n";
print encode "utf-8", "Distinct deployments found: $totalDeployments\n";
print encode "utf-8", "Distinct deployments stored: ".scalar(@deployments)."\n";
print encode "utf-8", "Distinct maximal deployments: $leastMemoryDeployments\n";
foreach my $deployment (sort {$a->{remainingServerMemory} <=> $b->{remainingServerMemory}} @deployments) {
if ($deployment->{remainingServerMemory} > $remainingServerMemory) {
last;
} else {
$remainingServerMemory = $deployment->{remainingServerMemory};
}
%deployment = %{dclone $deployment};
foreach my $server (@serverList) {
foreach my $engine (@engineList) {
if ($deployment{$server}->{$engine}) {
++$deployedEngines{$engine};
$testServers{$server} -= $engines{$engine}->[0];
}
}
}
print encode "utf-8", "Total unused memory: $deployment{remainingServerMemory}\n";
print encode "utf-8", "Deployed instances per engine:\n";
foreach my $engine (sort {$a cmp $b} @engineList) {
print encode "utf-8", "$engine: ".(defined $deployedEngines{$engine} ? $deployedEngines{$engine} : 0)."\n";
$testEngines{$engine}->[1] -= defined $deployedEngines{$engine} ? $deployedEngines{$engine} : 0;
}
print encode "utf-8", "Undeployed instances:\n" unless @engineList ~~ sub { !$testEngines{$_[0]}->[1] };
foreach my $engine (grep {$testEngines{$_}->[1]} @engineList) {
print encode "utf-8", "$engine: $testEngines{$engine}->[1]\n";
}
&printDeployment();
%deployedEngines = ();
%testEngines = %{dclone \%engines};
%testServers = %{dclone \%servers};
}
sub deployServer {
#Try to deploy engines on a server
my $server = shift @serverList;
#Fill the server with engines
&addEngine($server, $testServers{$server});
#Consider in order all best engine distributions for the server
foreach my $deployment (@{$deployedEngineIDs{$server}}) {
#As we haven’t stored the actual deployment lattice, we have to make sure we don’t produce illegal deployments
#(that is, we might not have the engines we need to fulfill this deployment attempt)
next if @$deployment ~~ sub { $testEngines{$_[0]}->[1] <= 0 };
#Setup the environment to indicate that we are investigating a particular deployment
foreach my $engine (@$deployment) {
$deployment{$server}->{$engine} = 1;
$testServers{$server} -= $testEngines{$engine}->[0];
--$testEngines{$engine}->[1];
--$totalInstances;
}
$deployment{remainingServerMemory} += $testServers{$server};
#Check if there are servers and engines left
if (@serverList && grep {$testEngines{$_}->[1]} @engineList) {
#Continue deployment on the next server if it could produce a better result
&deployServer() unless $deployment{remainingServerMemory} > $leastMemoryRemaining;
} else {
#We've handled all servers. Now we need to check if the resulting deployment needs to be stored
if ($deployment{remainingServerMemory} < $leastMemoryRemaining) {
$leastMemoryRemaining = $deployment{remainingServerMemory};
$leastMemoryDeployments = 1;
push @deployments, dclone \%deployment;
$isFullDeployment = $totalInstances == 0;
print STDERR "Upgrading to ${leastMemoryRemaining}MB remaining and $totalInstances instance".($totalInstances == 1 ? "" : "s")." left to deploy.\n";
} elsif ($deployment{remainingServerMemory} == $leastMemoryRemaining) {
++$leastMemoryDeployments;
push @deployments, dclone \%deployment;
print STDERR "$leastMemoryDeployments deployments with ${leastMemoryRemaining}MB remaining and $totalInstances instances left to deploy.\n" if $leastMemoryDeployments > $maxDeploymentsCap*.8;
}
++$totalDeployments;
}
#Backtrack
$deployment{remainingServerMemory} -= $testServers{$server};
foreach my $engine (@$deployment) {
$deployment{$server}->{$engine} = 0;
$testServers{$server} += $testEngines{$engine}->[0];
++$testEngines{$engine}->[1];
++$totalInstances;
}
#Put an upper limit on the number of best deployments
#Stop processing after finding a full deployment
last if $leastMemoryDeployments >= $maxDeploymentsCap || $isFullDeployment;
}
#Backtrack
unshift @serverList, $server;
}
sub addEngine {
#Progress dots
++$deployments;
print STDERR "[$deployments]" unless $deployments % 500000;
print STDERR "." unless $deployments % 10000;
my $server = shift;
my $leastUnusedMemory = shift;
my $lastEngineID = shift;
$lastEngineID = -1 unless defined $lastEngineID;
my $availableMemory = $testServers{$server};
#Go through the available engines
foreach my $engineID (($lastEngineID+1)..$#engineList) {
my $engine = $engineList[$engineID];
#Engines are sorted by increasing memory, so no further engine could fit and we can cut the branch
last if $testEngines{$engine}->[0] > $testServers{$server};
next unless $testEngines{$engine}->[1];
#Indicate that we are using the engine for deployment
$deployment{$server}->{$engine} = 1;
$testServers{$server} -= $testEngines{$engine}->[0];
--$testEngines{$engine}->[1];
--$totalInstances;
#Store the current deployment if necessary
if ($testServers{$server} < $leastUnusedMemory) {
$leastUnusedMemory = $testServers{$server};
$deployedEngineIDs{$server} = [[(grep {$deployment{$server}->{$_}} @engineList)]];
} elsif ($testServers{$server} == $leastUnusedMemory) {
push @{$deployedEngineIDs{$server}}, [(grep {$deployment{$server}->{$_}} @engineList)];
}
#Try to add another engine—check if there are other engines and if any could fit in the remaining space
$leastUnusedMemory = &addEngine($server, $leastUnusedMemory, $engineID) if $engineID < $#engineList && $totalInstances > 0 && $testServers{$server} >= $testEngines{$engineList[$engineID + 1]}->[0];
#Backtrack
$deployment{$server}->{$engine} = 0;
$testServers{$server} += $testEngines{$engine}->[0];
++$testEngines{$engine}->[1];
++$totalInstances;
}
return $leastUnusedMemory;
}
sub isCompleteDeployment {
my $undeployedMemory;
my $minMemoryRequirement = $maxMemoryRequirement;
my $undeployedEngines;
foreach (grep {!$deployedEngines{$_}} @engineList) {
my $engineMemory = $testEngines{$_}->[0];
++$undeployedEngines;
$undeployedMemory += $engineMemory;
$minMemoryRequirement = $engineMemory if $engineMemory < $minMemoryRequirement;
}
return ($undeployedEngines || 0, $undeployedMemory || 0, $minMemoryRequirement);
}
sub printDeployment {
my ($undeployedEngines, $undeployedMemory, $minMemoryRequirement) = &isCompleteDeployment();
print encode "utf-8", "\n";
if ($undeployedEngines) {
print encode "utf-8", "Undeployed engines ($undeployedEngines): ".join(", ", sort {$a cmp $b} grep {!$deployedEngines{$_}} @engineList)."\n";
print encode "utf-8", "Undeployed memory: ${undeployedMemory}MB; Mininum memory requirement of undeployed engines: ${minMemoryRequirement}MB\n";
} else {
print encode "utf-8", "NO undeployed engines!\n";
}
foreach my $server (@serverList) {
print encode "utf-8", "$server ($testServers{$server}MB):";
foreach my $engine (sort {$a cmp $b} grep {$deployment{$server}->{$_}} keys %{$deployment{$server}}) {
print encode "utf-8", "\t$engine";
}
print encode "utf-8", "\n";
}
my %serverListByLanguage;
foreach my $server (@serverList) {
print encode "utf-8", "\t\"$server\" => [";
foreach my $engine (sort {$a cmp $b} grep {$deployment{$server}->{$_}} keys %{$deployment{$server}}) {
print encode "utf-8", "\"$engine\",";
my ($source, $target) = map {lc $_} $engine =~ /^.*?_(\w+)-(\w+)_.*?$/;
$serverListByLanguage{$source}->{$target} = [] unless defined $serverListByLanguage{$source}->{$target};
push @{$serverListByLanguage{$source}->{$target}}, $server;
}
print encode "utf-8", "],\n";
}
print encode "utf-8", "((\n";
foreach my $source (sort {$a cmp $b} keys %serverListByLanguage) {
print encode "utf-8", "$source => {\n";
foreach my $target (sort {$a cmp $b} keys %{$serverListByLanguage{$source}}) {
print encode "utf-8", "\t$target => [\n";
foreach my $server (sort {$a cmp $b} @{$serverListByLanguage{$source}->{$target}}) {
print encode "utf-8", "\t\t\"$server\",\n";
}
print encode "utf-8", "\t],\n";
}
print encode "utf-8", "},\n";
}
print encode "utf-8", "))\n";
}
1;