-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_basic.js
More file actions
349 lines (309 loc) · 12.3 KB
/
client_basic.js
File metadata and controls
349 lines (309 loc) · 12.3 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
var Gearman = require("abraxas");
var batchMode = false;
var streamMode = true;
var studentNum = 31;
var problemNum = 25; // must be equal or less than 25
var nodeNum = 4;
var serverList = []; // List of Connected Servers
var portList = [];
var score = [];
var taskNum = 0;
var AVmode = 0; // 0 = FT Reconnection, 1 = FT Reassignment, 2 = Voting
var sumInput = [];
var countInput = [];
var countTemp = [];
var sumOutputNum = 0; // batch mode
var sumRRpointer = 0;
var countRRpointer = 0;
var finish;
var output = [];
// sleep
function timeout(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function setMode() {
var arg = Number(process.argv[2]);
if (arg == 0) batchMode = true;
else if (arg == 1) streamMode = true;
AVmode = Number(process.argv[3]);
if (AVmode == 2)
batchMode = true;
}
function connectServers() {
for (let i = 0; i < nodeNum; i++) {
let client = Gearman.Client.connect({
servers: ["127.0.0.1:" + (i + 4730)],
defaultEncoding: "utf8",
});
client.on('connect', function (client) {
serverList.push(client); // Add to ServerList
portList.push(i);
});
}
}
function setValues() {
var quotient;
var remainder;
var inputPerBatch = 7; // for stream mode
// initialize job
for (let i = 0; i < studentNum; i++) {
var temp = [];
for (let j = 0; j < problemNum; j++) {
temp.push([i, getRandomScore()]); // [student ID, score]
}
score.push(temp);
}
// divide job into task, and then push task into sum queue
if (AVmode == 2) {
taskNum = nodeNum;
for (let i = 0; i < nodeNum; i++)
sumInput.push(score);
}
else if (batchMode) {
// studentNum taskNum = nodeNum quotient (quotient+remainder)
// 29 3 9 9 (9+2)
// 30 3 10 10 (10+0)
// 31 3 10 10 (10+1)
quotient = Math.floor(studentNum / nodeNum); // 10
remainder = studentNum % nodeNum;
taskNum = nodeNum;
for (var i = 0; i < taskNum; i++) {
var input = [];
for (var j = 0; j < quotient; j++) {
input.push(score[i * quotient + j]);
}
// last loop
if (i == taskNum - 1) {
for (var j = 0; j < remainder; j++) {
input.push(score[quotient * nodeNum + j]);
}
}
sumInput.push(input);
}
} else if (streamMode) {
// studentNum IPB quotient (remainder) tasknum
// 27 7 7 7 7 (6) 4
// 28 7 7 7 7 7 4
// 29 7 7 7 7 7 (1) 5
quotient = Math.floor(studentNum / inputPerBatch);
remainder = studentNum % inputPerBatch;
taskNum = Math.floor((studentNum - 1) / inputPerBatch) + 1;
for (var i = 0; i <= quotient; i++) {
var input = [];
for (var j = 0; j < inputPerBatch; j++) {
if (i == quotient && j == remainder) break;
input.push(score[i * inputPerBatch + j]);
}
if (input.length > 0) sumInput.push(input);
}
}
console.log(score);
console.log("----------------------------------------");
for (var i = 0; i < sumInput.length; i++) console.log(sumInput[i]);
}
function getRandomScore() {
return Math.floor(Math.random() * 5);
}
function initializeOutput() {
for (var i = 0; i <= 100; i++) {
output.push([i, 0]);
}
}
function executeSum() {
if (sumInput.length == 0) return;
let client = serverList[sumRRpointer];
sumRRpointer++;
sumRRpointer %= serverList.length; // control index
// execute
let input = sumInput[0];
sumInput.splice(0, 1); // remove item on index 0
let startTime = Date.now();
let disconnectFunction = function (client) {
let portNum = portList[serverList.indexOf(client)];
if (AVmode == 1) { // Reassignment
if (serverList.indexOf(client) >= 0 && serverList.indexOf(client) < serverList.length) {
console.log('[ERROR] Fail From Server ' + portNum);
serverList.splice(serverList.indexOf(client), 1); // Remove From List
portList.splice(serverList.indexOf(client), 1);
client.disconnect();
console.log('Will Remove From Server List');
sumRRpointer %= serverList.length;
}
sumInput.push(input);
} else if (AVmode == 0) { // Reconnection
console.log('[ERROR] Fail From Server ' + portNum);
console.log('Will try to Reconnect');
console.log(input.length);
client.on('connect', function (client) {
console.log('Reconnected to Server ' + portNum);
sumRRpointer = serverList.indexOf(client);
sumInput.push(input);
});
} else if (AVmode == 2) { // Voting
if (serverList.indexOf(client) >= 0 && serverList.indexOf(client) < serverList.length) {
console.log('[ERROR] Fail From Server ' + portNum);
serverList.splice(serverList.indexOf(client), 1); // Remove From List
portList.splice(serverList.indexOf(client), 1);
client.disconnect();
console.log('Will Remove From Server List');
}
}
}
client
.submitJob("sum", JSON.stringify([input, startTime]))
.then(function (result) {
client.removeListener('disconnect', disconnectFunction);
let [sum, startTime] = JSON.parse(result);
let endTime = Date.now();
let time = endTime - startTime;
console.log(`------------SUM : ${time}ms elapsed------------`);
countInput.push(sum);
console.log(sum);
if (AVmode == 2 && (countInput.length == serverList.length)) {
let sumOut = [];
let tmp;
console.log('------Voting------');
for (let i = 0; i < countInput[0].length; i++) { // i번째 학생
let scoreCount = new Map();
let studentId = countInput[0][i][0];
for (let j = 0; j < countInput.length; j++) { // j 번째 input count
if (scoreCount.has(countInput[j][i][1])) {
tmp = scoreCount.get(countInput[j][i][1]);
scoreCount.delete(countInput[j][i][1]);
scoreCount.set(countInput[j][i][1], tmp + 1);
} else
scoreCount.set(countInput[j][i][1], 1);
}
scoreCount = [...scoreCount.entries()].sort();
console.log(i + 'th Student');
console.log(scoreCount);
sumOut.push([studentId, scoreCount[scoreCount.length - 1][0]]);
}
countInput = [];
for (let i = 0; i < serverList.length; i++)
countInput.push(sumOut);
console.log('success');
}
sumOutputNum++; // for batch mode
});
client.on('disconnect', disconnectFunction);
}
function executeCount() {
if (batchMode && sumOutputNum < taskNum) return;
if (countInput.length == 0) return;
client = serverList[countRRpointer];
countRRpointer++;
countRRpointer %= serverList.length;
// execute
let input = countInput[0];
countInput.splice(0, 1); // remove item on index 0
let startTime = Date.now();
let disconnectFunction = function (client) {
let portNum = portList[serverList.indexOf(client)];
if (AVmode == 1) { // Reassignment
if (serverList.indexOf(client) >= 0 && serverList.indexOf(client) < serverList.length) {
console.log('[ERROR] Fail From Server ' + portNum);
serverList.splice(serverList.indexOf(client), 1); // Remove From List
portList.splice(serverList.indexOf(client), 1);
client.disconnect();
console.log('Will Remove From Server List');
countRRpointer %= serverList.length;
}
countInput.push(input);
} else if (AVmode == 0) { // Reconnection
console.log('[ERROR] Fail From Server ' + portNum);
console.log('Will try to Reconnect');
console.log(input.length);
client.on('connect', function (client) {
console.log('Reconnected to Server ' + portNum);
countRRpointer = serverList.indexOf(client);
countInput.push(input);
});
} else if (AVmode == 2) { // Voting
if (serverList.indexOf(client) >= 0 && serverList.indexOf(client) < serverList.length) {
console.log('[ERROR] Fail From Server ' + portNum);
serverList.splice(serverList.indexOf(client), 1); // Remove From List
portList.splice(serverList.indexOf(client), 1);
client.disconnect();
console.log('Will Remove From Server List');
}
}
}
client
.submitJob("count", JSON.stringify([input, startTime]))
.then(function (result) {
client.removeListener('disconnect', disconnectFunction);
let [count, startTime] = JSON.parse(result);
let endTime = Date.now();
let time = endTime - startTime;
console.log(`------------COUNT : ${time}ms elapsed------------`);
/* update output */
if (AVmode !== 2) {
for (let k = 0; k <= 100; k++) {
output[k][1] += count[k][1];
}
} else {
countTemp.push(count);
}
console.log(count);
/* count finish */
finish--;
/* check final output */
if (finish == 0) {
if (AVmode == 2) {
let countOut = [];
let tmp;
//console.log(countTemp[0][0]);
for (let i = 0; i < countTemp[0].length; i++) { // i번째 점수
let scoreCount = new Map();
let scoreNum = countTemp[0][i][0];
for (let j = 0; j < countTemp.length; j++) { // j 번째 input count
if (scoreCount.has(countTemp[j][i][1])) {
tmp = scoreCount.get(countTemp[j][i][1]);
scoreCount.delete(countTemp[j][i][1]);
scoreCount.set(countTemp[j][i][1], tmp + 1);
} else
scoreCount.set(countTemp[j][i][1], 1);
}
scoreCount = [...scoreCount.entries()].sort();
countOut.push([scoreNum, scoreCount[scoreCount.length - 1][0]]);
}
output = countOut;
}
console.log("---------------FINAL OUTPUT---------------");
console.log(output);
}
});
client.on('disconnect', disconnectFunction);
}
async function execute() {
finish = taskNum;
let runningTime = Date.now();
while (finish != 0) {
executeSum();
executeCount();
await timeout(1); // make CPU idle (context switch for callback function)
}
verifyResult();
console.log("PROGRAM EXIT");
console.log('----Time Taken in Total : ' + (Date.now() - runningTime) / 1000 + 's----');
}
function verifyResult() {
var sum = 0;
for (var i = 0; i < output.length; i++) {
sum += output[i][1];
}
console.log("---------------VERIFY RESULT---------------");
console.log(`studentNum = ${studentNum} / sum of output = ${sum}`);
console.log(studentNum == sum ? "PASS" : "FAIL");
}
async function program() {
setMode();
connectServers();
while (serverList.length < nodeNum) await timeout(1);
console.log(serverList.length);
setValues();
initializeOutput();
execute();
}
program();