-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestComm.js
132 lines (122 loc) · 3.4 KB
/
testComm.js
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
/**
* IMPORT SECTION
*/
// group that runs the workflow
const groupsTemplate = require('./distribution/all/groups');
const distribution = require('./distribution');
const id = distribution.util.id;
//////////////////////////////////////////////////////////////////
/**
* CONFIG SECTION
*/
// nodes in the system = EC2 instances
const n1 = {ip: '127.0.0.1', port: 8081};
const n2 = {ip: '127.0.0.1', port: 8082}
const n3 = {ip: '127.0.0.1', port: 8083};
const n4 = {ip: '127.0.0.1', port: 8080}
const workerGroup = {};
workerGroup[id.getSID(n1)] = n1;
workerGroup[id.getSID(n2)] = n2;
workerGroup[id.getSID(n3)] = n3;
workerGroup[id.getSID(n4)] = n4;
let localServer = null;
const workerConfig = {gid: 'worker'};
//////////////////////////////////////////////////////////////////
/**
* HELPER SECTION
*/
// first thing the coordinator does is set up the group
function isEmptyObject(obj) {
return Object.keys(obj).length === 0 && obj.constructor === Object;
}
const setGroup = (server) => {
localServer = server;
groupsTemplate(workerConfig).put(workerConfig, workerGroup, async (e, v) => {
if ( !isEmptyObject(e)) {
cleanup(new Error('ERROR PUTTING THE GORUP'));
console.log("Error");
return;
} else {
console.log(JSON.stringify(e));
console.log(JSON.stringify(v));
await workflow();
cleanup();
return;
}
});
}
let cntr = 0;
async function workflow() {
const totalRepos = 100;
async function fetchAllRepos() {
let page = 1;
let dataset = [];
while (dataset.length < totalRepos) {
const newRepos = await distribution.util.crawl.fetchRepos(page, Math.min(100, totalRepos - dataset.length));
dataset = dataset.concat(newRepos);
if (newRepos.length === 0) {
break;
}
page++;
}
return dataset;
}
let dataset = await fetchAllRepos();
return await new Promise((resolve, reject) => {
dataset.forEach((o) => {
let key = Object.keys(o)[0];
let value = o[key];
distribution.worker.store.put(value, key, (e, v) => {
cntr++;
if (cntr === dataset.length) {
resolve(doMapReduce());
}
});
});
});
}
const doMapReduce = async () => {
return new Promise((resolve, reject) => {
distribution.worker.store.get(null, async (e, v) => {
console.log('key number: ', v.length);
distribution.worker.mr.exec({
keys: v,
map: async (key, value) =>
await distribution.util.searchPreprocessing['map'](key, value),
reduce: async (key, value) =>
await distribution.util.searchPreprocessing['reduce'](key, value),
}, (error, value) => {
if (error) {
reject(error);
return;
}
try {
resolve();
} catch (error) {
reject(error);
}
});
});
});
};
function cleanup(e) {
console.log(e);
let remote = {service: 'status', method: 'stop'};
remote.node = n1;
distribution.local.comm.send([], remote, (e, v) => {
remote.node = n2;
distribution.local.comm.send([], remote, (e, v) => {
remote.node = n3;
distribution.local.comm.send([], remote, (e, v) => {
localServer.close();
process.exit(1)
});
});
});
}
//////////////////////////////////////////////////////////////////
/**
* EXECUTION SECTION
*/
global.nodeConfig = {ip: '127.0.0.1', port: 8080};
distribution.node.start(setGroup);