Fixes for 'Case: Less nodes available'

This commit is contained in:
sairajzero 2021-07-17 05:16:58 +05:30
parent b0080c5e42
commit cd16e8aa67
2 changed files with 29 additions and 25 deletions

View File

@ -249,10 +249,10 @@ function processTaskFromPrevNode(packet) {
orderBackup(task.order) orderBackup(task.order)
break; break;
case STORE_BACKUP_DATA: case STORE_BACKUP_DATA:
storeBackupData(task.data) storeBackupData(task.data, from, packet)
break; break;
case TAG_BACKUP_DATA: case TAG_BACKUP_DATA:
tagBackupData(task.data) tagBackupData(task.data, from, packet)
break; break;
case DATA_REQUEST: case DATA_REQUEST:
sendStoredData(task.nodes, _prevNode) sendStoredData(task.nodes, _prevNode)
@ -369,7 +369,15 @@ function reconnectNextNode() {
_nextNode.close(); _nextNode.close();
connectToNextNode() connectToNextNode()
.then(result => console.log(result)) .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----- //-----BACKUP TASKS-----
@ -377,11 +385,14 @@ function reconnectNextNode() {
//Order the stored backup //Order the stored backup
function orderBackup(order) { function orderBackup(order) {
let new_order = []; let new_order = [];
let cur_serve = kBucket.innerNodes(_prevNode.id, myFloID);
for (let n in order) { for (let n in order) {
if (order[n] + 1 !== _list[n]) { if (order[n] + 1 !== _list[n]) {
if (order[n] >= backupDepth) if (order[n] >= backupDepth)
REMOVE_STORING_if_exist(N) //TODO db.dropTable(n).then(_ => null)
else if (condition) { //TODO: condition to check roll over when less nodes online .catch(error => console.error(error))
.finally(_ => _list.delete(n))
else if (_list[n] !== 0 || !cur_serve.includes(n)) {
_list[n] = order[n] + 1; _list[n] = order[n] + 1;
new_order.push(n); new_order.push(n);
} }
@ -428,29 +439,22 @@ function dataSyncIndication(snID, status, from) {
} }
//Store (backup) data //Store (backup) data
function storeBackupData(data) { function storeBackupData(data, from, packet) {
let closestNode = kBucket.closestNode(data.receiverID); let closestNode = kBucket.closestNode(data.receiverID);
if (_list.stored.includes(closestNode)) { if (_list.stored.includes(closestNode)) {
db.storeData(closestNode, data); db.storeData(closestNode, data);
if (_list[closestNode] < backupDepth) if (_list[closestNode] < backupDepth && _nextNode.id !== from)
_nextNode.send(packet_.constuct({ _nextNode.send(packet);
type: STORE_BACKUP_DATA,
data: data
}));
} }
} }
//Tag (backup) data //Tag (backup) data
function tagBackupData(data) { function tagBackupData(data, from, packet) {
let closestNode = kBucket.closestNode(data.receiverID); let closestNode = kBucket.closestNode(data.receiverID);
if (_list.stored.includes(closestNode)) { if (_list.stored.includes(closestNode)) {
db.storeTag(closestNode, data); db.storeTag(closestNode, data);
if (_list[closestNode] < backupDepth) if (_list[closestNode] < backupDepth && _nextNode.id !== from)
_nextNode.send(packet_.constuct({ _nextNode.send(packet);
type: TAG_BACKUP_DATA,
data: data
}));
} }
} }
@ -460,11 +464,11 @@ function forwardToNextNode(mode, data) {
'TAG': TAG_BACKUP_DATA, 'TAG': TAG_BACKUP_DATA,
'DATA': STORE_BACKUP_DATA 'DATA': STORE_BACKUP_DATA
} }
if(mode in modeMap) if (mode in modeMap && _nextNode.id)
_nextNode.send(packet_.constuct({ _nextNode.send(packet_.constuct({
type: modeMap[mode], type: modeMap[mode],
data: data data: data
})); }));
} }
//-----EXPORTS----- //-----EXPORTS-----

View File

@ -48,7 +48,7 @@ wsServer.on('connection', function connection(ws) {
backupProcess.processTaskFromSupernode(message, ws); backupProcess.processTaskFromSupernode(message, ws);
else { else {
var request = JSON.parse(message); var request = JSON.parse(message);
supernode.processRequestFromUser(JSON.parse(message)) //TODO: set live request supernode.processRequestFromUser(JSON.parse(message))
.then(result => { .then(result => {
ws.send(JSON.parse(result[0])) ws.send(JSON.parse(result[0]))
ws._liveReq = request; ws._liveReq = request;
@ -59,7 +59,7 @@ wsServer.on('connection', function connection(ws) {
function sendToLiveRequests(data) { function sendToLiveRequests(data) {
wsServer.clients.forEach(ws => { wsServer.clients.forEach(ws => {
if (supernode.checkIfRequestSatisfy(ws._liveReq, data)) //TODO if (supernode.checkIfRequestSatisfy(ws._liveReq, data))
ws.send(data) ws.send(data)
}) })
} }