-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_techDB.js
279 lines (234 loc) · 7.59 KB
/
test_techDB.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
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
/**
* Created by gags on 11/14/15.
*/
var inquirer = require('inquirer'),
io = require('socket.io-client'),
fs = require('fs'),
path = require('path'),
ip = require('ip'),
Server = require('socket.io'),
HashTable = require('hashtable'),
ConsistentHashing = require('consistent-hashing'),
constants = require('./constants'),
ioServer = new Server();
var log = false;
var argv = require('optimist')
.usage('Usage: $0 -c [CONFIG] -p [PORT] -k [KEY_RANGE]')
.demand(['c', 'p', 'k'])
.alias('c', 'config')
.describe('c', 'Config file with list of ip and port pair identifying peers')
.alias('p', 'port')
.describe('p', 'Port to run peer on')
.alias('k', 'keyRange')
.describe('k', 'Key Range')
.argv;
var peers = validateConfig(argv.config),
peersList = new ConsistentHashing(peers);
// NOTE: Validate config file
if (!peers) {
console.log("Please enter valid IP address and port separated by a space in config file ! : [IP_ADDRESS] [PORT] => ", argv.config);
process.exit();
}
// NOTE: Setup Store
var Store = require('./techDB');
Store.init().then(function (status) {
if (status)
listOperations();
else
console.log("ERROR INITIALIZING DB !");
}, function (err) {
console.log(err);
});
// NOTE: List the operations supported by DHT
function listOperations() {
var requestForOperation = [{
type: "list",
name: "operation",
message: "Please select the operation you would like to perform : ",
choices: ['Perform Tests']
}];
inquirer.prompt(requestForOperation, function( response ) {
// NOTE: Perform operations in sequence
doTest(constants.TEST_PUT);
});
}
var iteration = 0;
var totalLatency = 0;
var keyRange = argv.keyRange;
var maxIteration = 10;
function doTest(operation) {
switch (operation) {
case constants.TEST_PUT:
testPut();
break;
case constants.TEST_GET:
testGet();
break;
case constants.TEST_DELETE:
testDelete();
break;
default:
logServerMessage("ERROR PERFORMING TEST: SOMETHING WENT TERRIBLY WRONG !");
}
}
function testPut() {
if (iteration < maxIteration) {
delegateOperationToPeer(keyRange.toString(), constants.TEST_PUT, {
key: keyRange.toString(),
value: keyRange.toString() + '_value'
});
keyRange++; iteration++;
} else {
console.log("Total Put Latency / Lookup (ms) : ", totalLatency / maxIteration);
Store.getSize(function (size) {
console.log("Size : ", size);
});
iteration = 0;
totalLatency = 0;
keyRange = argv.keyRange;
doTest(constants.TEST_GET);
}
}
function testGet() {
if (iteration < maxIteration) {
delegateOperationToPeer(keyRange.toString(), constants.TEST_GET, {
key: keyRange.toString(),
value: keyRange.toString() + '_value'
});
keyRange++; iteration++;
} else {
console.log("Total Get Latency / Lookup (ms) : ", totalLatency / maxIteration);
Store.getSize(function (size) {
console.log("Size : ", size);
});
iteration = 0;
totalLatency = 0;
keyRange = argv.keyRange;
doTest(constants.TEST_DELETE);
}
}
function testDelete() {
if (iteration < maxIteration) {
delegateOperationToPeer(keyRange.toString(), constants.TEST_DELETE, {
key: keyRange.toString(),
value: keyRange.toString() + '_value'
});
keyRange++; iteration++;
} else {
console.log("Total Delete Latency / Lookup (ms) : ", totalLatency / maxIteration);
Store.getSize(function (size) {
console.log("Size : ", size);
});
iteration = 0;
totalLatency = 0;
keyRange = argv.keyRange;
}
}
// NOTE: perform specific operation based on 'operation' using the 'key' and 'value'
function performOperation(operation, key, value, callback) {
switch (operation) {
case constants.TEST_PUT:
Store.putValue(key, value).then(callback);
break;
case constants.TEST_GET:
Store.getValue(key).then(callback);
break;
case constants.TEST_DELETE:
Store.deleteKey(key).then(callback);
break;
default:
callback(null);
logServerMessage("ERROR: SOMETHING WENT TERRIBLY WRONG !");
}
}
// NOTE: Find target peer using ConsistentHashing
function findTargetPeer(key) {
return peersList.getNode(key);
}
var sockets = new HashTable();
function delegateOperationToPeer(key, operation, operation_params) {
var socket_address;
var peerID = findTargetPeer(key);
if (validateAddress(peerID)) {
socket_address = "http://" + peerID.split(" ").join(":");
} else {
logClientMessage("ERROR : SOMETHING TERRIBLY WENT WRONG WHILE CONNECTING TO PEER !");
process.exit();
}
var socket;
if (sockets.has(peerID)) {
socket = sockets.get(peerID);
socket.emit('operation', {
operation: operation,
params: operation_params,
timestamp: Date.now()
});
} else {
socket = io(socket_address);
console.log("Connecting to peer : ", socket_address);
socket.on('op_status', function (response) {
logClientMessage(response.operation + " : Status => " + response.status);
var latency = Date.now() - response.timestamp;
totalLatency += latency;
doTest(response.operation);
});
socket.on('connect', function () {
logClientMessage("Connected to Peer Server !");
socket.emit('operation', {
operation: operation,
params: operation_params,
timestamp: Date.now()
});
});
sockets.put(peerID, socket);
}
}
// NOTE: DHT Peer Server
ioServer.on('connect', function (socket) {
logServerMessage("Connected with Peer Client : " + socket.handshake.address);
console.log("Open Client Connections : ", socket.server.engine.clientsCount);
socket.on('operation', function (response) {
performOperation(response.operation, response.params.key, response.params.value,
function (status) {
// NOTE : Get STATUS in callback
socket.emit('op_status', {
operation: response.operation,
status: status,
timestamp: response.timestamp
});
});
});
});
// NOTE: validate the config file for correct peer addresses
function validateConfig(fileName) {
var peers = fs.readFileSync(fileName).toString().split('\n');
var invalidPeers = peers.filter(function (peer, i) {
return !validateAddress(peer);
});
return invalidPeers.length > 0 ? false : peers;
}
// NOTE: check if address is valid (ip:port)
function validateAddress(entry) {
var ip_port = entry.split(" ");
var blocks = ip_port[0].split(".");
if (ip_port.length < 2)
return false;
if(blocks.length === 4) {
return blocks.every(function(block) {
return parseInt(block,10) >=0 && parseInt(block,10) <= 255;
});
}
return false;
}
// NOTE: log client message
function logClientMessage(message) {
if (log)
console.log("[Client] : ", message);
}
// NOTE: log server message
function logServerMessage(message) {
if (log)
console.log("[Server] : ", message);
}
ioServer.listen(argv.port);
console.log("\n Server running at : " + ip.address() + ":" + argv.port);