-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.m
232 lines (195 loc) · 7.07 KB
/
main.m
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
function main
% main function for the load balancer reasoner
%
% Parameters:
% file: location of the XML file to parse
%
% Copyright (c) 2012-2014, Imperial College London
% All rights reserved.
% the required jar files
javaaddpath(fullfile(pwd,'lib/commons-lang3-3.1.jar'));
javaaddpath(fullfile(pwd,'lib/object-store-api-0.1.jar'))
javaaddpath(fullfile(pwd,'lib/commons-logging-1.1.3.jar'));
javaaddpath(fullfile(pwd,'lib/httpclient-4.3.5.jar'))
javaaddpath(fullfile(pwd,'lib/httpcore-4.3.2.jar'))
javaaddpath(fullfile(pwd,'lib/haproxy-api.jar'))
javaaddpath(fullfile(pwd,'lib/json-simple-1.1.1.jar'))
% copy libcurl first
% add in /etc/enviornment
% MOSAIC_OBJECT_STORE_ENDPOINT_IP=194.102.62.209
% MOSAIC_OBJECT_STORE_ENDPOINT_PORT=20622
% MOSAIC_OBJECT_STORE_ENDPOINT_PATH=/v1/collections/c-1/objects/o-3/data
OS_IP = getenv('MOSAIC_OBJECT_STORE_ENDPOINT_IP');
OS_PORT = getenv('MOSAIC_OBJECT_STORE_ENDPOINT_PORT');
OS_PATH = getenv('MOSAIC_OBJECT_STORE_LB_REASONER_PATH');
command = strcat('curl -X GET http://',OS_IP,':',OS_PORT,OS_PATH,' | tee configuration_LB.xml')
status = system(command);
if status ~= 0
disp('Error getting the configuration file.')
exit
end
startTime = 0;
while 1
% parse the XML file
if java.lang.System.currentTimeMillis - startTime > 10000
%xDoc = xmlread('configuration_LB.xml');
xDoc = xmlread('configuration_LB.xml');
rootNode = xDoc.getDocumentElement.getChildNodes;
node = rootNode.getFirstChild;
while ~isempty(node)
if strcmp(node.getNodeName, 'path')
path = char(node.getTextContent);
end
if strcmp(node.getNodeName, 'haproxyIPGold')
haproxyIPGold = char(node.getTextContent);
end
if strcmp(node.getNodeName, 'haproxyIPSilver')
haproxyIPSilver = char(node.getTextContent);
end
if strcmp(node.getNodeName, 'frontendNameGold')
frontendNameGold = char(node.getTextContent);
end
if strcmp(node.getNodeName, 'frontendNameSilver')
frontendNameSilver = char(node.getTextContent);
end
if strcmp(node.getNodeName, 'algorithm')
algorithm = char(node.getTextContent);
end
if strcmp(node.getNodeName, 'period')
period = str2double(node.getTextContent);
end
if strcmp(node.getNodeName, 'revenue')
revenue = char(node.getTextContent);
end
node = node.getNextSibling;
end
end
try
load(path)
catch err
disp('No file found')
pause(5)
continue
end
revenue = str2double(strsplit(revenue,','));
if ~strcmp(frontendList.get(0),frontendNameGold)
revenue = revenue(end:-1:1);
end
value = -1;
% check vm availability
offline = [];
for s = 1:frontendList.size
if strcmp(frontendList.get(s-1),frontendNameGold)
IP = haproxyIPGold;
end
if strcmp(frontendList.get(s-1),frontendNameSilver)
IP = haproxyIPSilver;
end
classLoader = com.mathworks.jmi.ClassLoaderManager.getClassLoaderManager;
httpAPI = javaObject('imperial.modaclouds.HttpAPI');
success = 1;
for j = 1:serverIDListAll.size
content = httpAPI.sendGet(strcat(IP,'/v1/pools/',frontendList.get(s-1),'/targets/',serverIDListAll.get(j-1),'/check'));
if isempty(content)
disp('Not correct end server.')
break;
end
parser = javaObject('org.json.simple.parser.JSONParser');
obj = parser.parse(content);
temp = obj.get('Status');
if ~strcmp(temp,'Online')
offline = [offline,j];
end
end
end
for s = 1:frontendList.size
D_combined(:,s) = D{1,s};
end
D_combined(offline,:) = [];
D_combined
switch algorithm
case 'LI'
if iscolumn(N)
N = N';
end
if iscolumn(Z)
Z = Z';
end
[fbest, xopt] = bcmpoptim_poc_fmincon_li(D_combined, N, Z, revenue);
case 'Heuristic'
xopt = bcmpoptim_prog_heur1(D_combined, revenue);
case 'LD'
[fbest, xopt] = bcmpoptim_poc_fmincon_ld(D_combined, N, Z, f, F, revenue);
end
xopt_int = round(xopt*100)
% update the weights in the Haproxy
for s = 1:frontendList.size
if strcmp(frontendList.get(s-1),frontendNameGold)
IP = haproxyIPGold;
end
if strcmp(frontendList.get(s-1),frontendNameSilver)
IP = haproxyIPSilver;
end
classLoader = com.mathworks.jmi.ClassLoaderManager.getClassLoaderManager;
httpAPI = javaObject('imperial.modaclouds.HttpAPI');
success = 1;
count = 0;
for j = 1:serverIDListAll.size
if ismember(j,offline)
count = count + 1;
continue
end
content = httpAPI.sendGet(strcat(IP,'/v1/pools/',frontendList.get(s-1),'/targets/',serverIDListAll.get(j-1)));
if isempty(content)
disp('Not correct end server.')
break;
end
parser = javaObject('org.json.simple.parser.JSONParser');
obj = parser.parse(content);
if abs(xopt_int(j-count,s)) < 10^-3
obj.put('enabled',0);
else
obj.put('enabled',1);
end
%if abs(xopt_int(j,s) - 0) < 10^-3
% obj.put('enabled','false');
%else
% obj.put('enabled','true');
%end
obj.put('weight',num2str(xopt_int(j-count,s)));
temp = obj.get('Address');
if ~isempty(temp)
obj.put('address',temp);
end
obj.toString
response = httpAPI.sendPut(strcat(IP,'/v1/pools/',frontendList.get(s-1),'/targets/',serverIDListAll.get(j-1)),obj.toString);
if isempty(response)
disp('Unreachable server');
break;
end
if response.contains(java.lang.String(404))
disp(response);
break;
end
if ~strfind(char(response), num2str(200))
success = 0;
end
disp(response)
end
if success == 0
break;
end
response = httpAPI.sendPost(strcat(IP,'/v1/controller/commit'), '');
response
if isempty(response)
disp('Unreachable server');
break;
end
if response.contains(java.lang.String(404))
disp(response);
break;
end
end
% if file does not exist, then wait for 5 seconds to check again.
pause(period);
end