From cd16e8aa67fa2c0bb965ddd9e6a0118d3de1adf2 Mon Sep 17 00:00:00 2001 From: sairajzero Date: Sat, 17 Jul 2021 05:16:58 +0530 Subject: [PATCH] Fixes for 'Case: Less nodes available' --- src/backupProcess.js | 50 ++++++++++++++++++++++++-------------------- src/server.js | 4 ++-- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/backupProcess.js b/src/backupProcess.js index 0a59ad9..6a2f48b 100644 --- a/src/backupProcess.js +++ b/src/backupProcess.js @@ -249,10 +249,10 @@ function processTaskFromPrevNode(packet) { orderBackup(task.order) break; case STORE_BACKUP_DATA: - storeBackupData(task.data) + storeBackupData(task.data, from, packet) break; case TAG_BACKUP_DATA: - tagBackupData(task.data) + tagBackupData(task.data, from, packet) break; case DATA_REQUEST: sendStoredData(task.nodes, _prevNode) @@ -369,7 +369,15 @@ function reconnectNextNode() { _nextNode.close(); connectToNextNode() .then(result => console.log(result)) - .catch(error => console.error(error)) + .catch(error => { + //Case: No other node is online + console.error(error); + //Serve all nodes + for (let sn in supernodeList) + db.createTable(sn) + .then(result => _list[sn] = 0) + .catch(error => console.error(error)) + }) } //-----BACKUP TASKS----- @@ -377,11 +385,14 @@ function reconnectNextNode() { //Order the stored backup function orderBackup(order) { let new_order = []; + let cur_serve = kBucket.innerNodes(_prevNode.id, myFloID); for (let n in order) { if (order[n] + 1 !== _list[n]) { if (order[n] >= backupDepth) - REMOVE_STORING_if_exist(N) //TODO - else if (condition) { //TODO: condition to check roll over when less nodes online + db.dropTable(n).then(_ => null) + .catch(error => console.error(error)) + .finally(_ => _list.delete(n)) + else if (_list[n] !== 0 || !cur_serve.includes(n)) { _list[n] = order[n] + 1; new_order.push(n); } @@ -428,29 +439,22 @@ function dataSyncIndication(snID, status, from) { } //Store (backup) data -function storeBackupData(data) { +function storeBackupData(data, from, packet) { let closestNode = kBucket.closestNode(data.receiverID); if (_list.stored.includes(closestNode)) { db.storeData(closestNode, data); - if (_list[closestNode] < backupDepth) - _nextNode.send(packet_.constuct({ - type: STORE_BACKUP_DATA, - data: data - })); + if (_list[closestNode] < backupDepth && _nextNode.id !== from) + _nextNode.send(packet); } - } //Tag (backup) data -function tagBackupData(data) { +function tagBackupData(data, from, packet) { let closestNode = kBucket.closestNode(data.receiverID); if (_list.stored.includes(closestNode)) { db.storeTag(closestNode, data); - if (_list[closestNode] < backupDepth) - _nextNode.send(packet_.constuct({ - type: TAG_BACKUP_DATA, - data: data - })); + if (_list[closestNode] < backupDepth && _nextNode.id !== from) + _nextNode.send(packet); } } @@ -460,11 +464,11 @@ function forwardToNextNode(mode, data) { 'TAG': TAG_BACKUP_DATA, 'DATA': STORE_BACKUP_DATA } - if(mode in modeMap) - _nextNode.send(packet_.constuct({ - type: modeMap[mode], - data: data - })); + if (mode in modeMap && _nextNode.id) + _nextNode.send(packet_.constuct({ + type: modeMap[mode], + data: data + })); } //-----EXPORTS----- diff --git a/src/server.js b/src/server.js index 20707dd..b1070d2 100644 --- a/src/server.js +++ b/src/server.js @@ -48,7 +48,7 @@ wsServer.on('connection', function connection(ws) { backupProcess.processTaskFromSupernode(message, ws); else { var request = JSON.parse(message); - supernode.processRequestFromUser(JSON.parse(message)) //TODO: set live request + supernode.processRequestFromUser(JSON.parse(message)) .then(result => { ws.send(JSON.parse(result[0])) ws._liveReq = request; @@ -59,7 +59,7 @@ wsServer.on('connection', function connection(ws) { function sendToLiveRequests(data) { wsServer.clients.forEach(ws => { - if (supernode.checkIfRequestSatisfy(ws._liveReq, data)) //TODO + if (supernode.checkIfRequestSatisfy(ws._liveReq, data)) ws.send(data) }) } \ No newline at end of file