Bug fixes
This commit is contained in:
parent
50c820310a
commit
5dd422afed
@ -343,7 +343,7 @@ function Database(user, password, dbname, host = 'localhost') {
|
||||
.then(result => resolve(result))
|
||||
.catch(error => reject(error));
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
db.clearAuthorisedAppData = function(snID, app, adminID, subAdmins, timestamp) {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
20
src/intra.js
20
src/intra.js
@ -454,12 +454,12 @@ function orderBackup(order) {
|
||||
req_sync = [];
|
||||
let cur_serve = kBucket.innerNodes(_prevNode.id, myFloID).concat(myFloID);
|
||||
for (let n in order) {
|
||||
if (!cur_serve.includes(n) && order[n] + 1 !== _list[n]) {
|
||||
if (!cur_serve.includes(n) && order[n] + 1 !== _list[n] && n in floGlobals.supernodes) {
|
||||
if (order[n] >= floGlobals.sn_config.backupDepth)
|
||||
DB.dropTable(n).then(_ => null)
|
||||
.catch(error => console.error(error))
|
||||
.finally(_ => _list.delete(n));
|
||||
else {
|
||||
else if (order[n] >= 0) {
|
||||
if (_list[n] === undefined)
|
||||
req_sync.push(n);
|
||||
_list[n] = order[n] + 1;
|
||||
@ -583,7 +583,7 @@ function deleteMigratedData(data, from, packet) {
|
||||
};
|
||||
|
||||
function initiateRefresh() {
|
||||
refresher.invoke(false).then(_ => null).catch(e => console.error(e));
|
||||
refresher.invoke(false).then(_ => null).catch(_ => null);
|
||||
};
|
||||
|
||||
//Forward incoming to next node
|
||||
@ -603,16 +603,15 @@ function forwardToNextNode(mode, data) {
|
||||
function dataMigration(node_change, flag) {
|
||||
if (!Object.keys(node_change).length)
|
||||
return;
|
||||
console.log("Node list changed! Data migration required");
|
||||
console.log("Node list changed! Data migration required", node_change);
|
||||
if (flag) dataMigration.intimateAllNodes(); //Initmate All nodes to call refresher
|
||||
let new_nodes = [],
|
||||
del_nodes = [];
|
||||
for (let n in node_change)
|
||||
(node_change[n] ? new_nodes : del_nodes).push(n);
|
||||
if (_prevNode.id && del_nodes.includes(_prevNode.id)) {
|
||||
_list[_prevNode.id] = 0; //Temporary serve for the deleted node
|
||||
if (_prevNode.id && del_nodes.includes(_prevNode.id))
|
||||
_prevNode.close();
|
||||
};
|
||||
const old_kb = kBucket;
|
||||
setTimeout(() => {
|
||||
//reconnect next node if current next node is deleted
|
||||
if (_nextNode.id) {
|
||||
@ -634,16 +633,17 @@ function dataMigration(node_change, flag) {
|
||||
|
||||
setTimeout(() => {
|
||||
dataMigration.process_new(new_nodes);
|
||||
dataMigration.process_del(del_nodes);
|
||||
dataMigration.process_del(del_nodes, old_kb);
|
||||
}, MIGRATE_WAIT_DELAY);
|
||||
}, MIGRATE_WAIT_DELAY);
|
||||
};
|
||||
|
||||
//data migration sub-process: Deleted nodes
|
||||
dataMigration.process_del = async function(del_nodes) {
|
||||
dataMigration.process_del = async function(del_nodes, old_kb) {
|
||||
if (!del_nodes.length)
|
||||
return;
|
||||
let process_nodes = del_nodes.filter(n => _list.serving.includes(n));
|
||||
let serve = _prevNode.id ? old_kb.innerNodes(_prevNode.id, myFloID) : _list.serving;
|
||||
let process_nodes = del_nodes.filter(n => serve.includes(n));
|
||||
if (process_nodes.length) {
|
||||
connectToAllActiveNodes().then(ws_connections => {
|
||||
let remaining = process_nodes.length;
|
||||
|
||||
116
src/kBucket.js
116
src/kBucket.js
@ -1,10 +1,9 @@
|
||||
'use strict';
|
||||
require('./lib/BuildKBucket');
|
||||
(function(GLOBAL) {
|
||||
var kBucket = GLOBAL.kBucket = {};
|
||||
var SNKB, SNCO;
|
||||
|
||||
function decodeID(floID) {
|
||||
module.exports = function K_Bucket(options = {}) {
|
||||
|
||||
const decodeID = function(floID) {
|
||||
let k = bitjs.Base58.decode(floID);
|
||||
k.shift();
|
||||
k.splice(-4, 4);
|
||||
@ -15,97 +14,84 @@ require('./lib/BuildKBucket');
|
||||
return nodeIdNewInt8Array;
|
||||
};
|
||||
|
||||
function distanceOf(floID) {
|
||||
let decodedId = decodeID(floID);
|
||||
return SNKB.distance(SNKB.localNodeId, decodedId);
|
||||
};
|
||||
const list = options.list || Object.keys(floGlobals.supernodes);
|
||||
const refID = options.masterID || floGlobals.SNStorageID;
|
||||
const _KB = new BuildKBucket({
|
||||
localNodeId: decodeID(refID)
|
||||
});
|
||||
list.forEach(id => _KB.add({
|
||||
id: decodeID(id),
|
||||
floID: id
|
||||
}));
|
||||
const _CO = list.map(sn => [_KB.distance(decodeID(refID), decodeID(sn)), sn])
|
||||
.sort((a, b) => a[0] - b[0])
|
||||
.map(a => a[1]);
|
||||
|
||||
function constructKB(list, refID) {
|
||||
let KB = new BuildKBucket({
|
||||
localNodeId: decodeID(refID)
|
||||
});
|
||||
list.forEach(id => KB.add({
|
||||
id: decodeID(id),
|
||||
floID: id
|
||||
}));
|
||||
return KB;
|
||||
};
|
||||
const self = this;
|
||||
|
||||
kBucket.launch = function() {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
let superNodeList = Object.keys(floGlobals.supernodes);
|
||||
let masterID = floGlobals.SNStorageID;
|
||||
SNKB = constructKB(superNodeList, masterID);
|
||||
SNCO = superNodeList.map(sn => [distanceOf(sn), sn])
|
||||
.sort((a, b) => a[0] - b[0])
|
||||
.map(a => a[1]);
|
||||
resolve('SuperNode KBucket formed');
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
};
|
||||
});
|
||||
};
|
||||
Object.defineProperty(self, 'order', {
|
||||
get: () => Array.from(_CO)
|
||||
});
|
||||
|
||||
kBucket.innerNodes = function(id1, id2) {
|
||||
if (!SNCO.includes(id1) || !SNCO.includes(id2))
|
||||
throw Error('Given nodes are not supernode');
|
||||
self.innerNodes = function(id1, id2) {
|
||||
if (!_CO.includes(id1) || !_CO.includes(id2))
|
||||
throw Error('Given nodes are not in KB');
|
||||
let iNodes = [];
|
||||
for (let i = SNCO.indexOf(id1) + 1; SNCO[i] != id2; i++) {
|
||||
if (i < SNCO.length)
|
||||
iNodes.push(SNCO[i]);
|
||||
for (let i = _CO.indexOf(id1) + 1; _CO[i] != id2; i++) {
|
||||
if (i < _CO.length)
|
||||
iNodes.push(_CO[i]);
|
||||
else i = -1;
|
||||
};
|
||||
return iNodes;
|
||||
};
|
||||
|
||||
kBucket.outterNodes = function(id1, id2) {
|
||||
if (!SNCO.includes(id1) || !SNCO.includes(id2))
|
||||
throw Error('Given nodes are not supernode');
|
||||
self.outterNodes = function(id1, id2) {
|
||||
if (!_CO.includes(id1) || !_CO.includes(id2))
|
||||
throw Error('Given nodes are not in KB');
|
||||
let oNodes = [];
|
||||
for (let i = SNCO.indexOf(id2) + 1; SNCO[i] != id1; i++) {
|
||||
if (i < SNCO.length)
|
||||
oNodes.push(SNCO[i]);
|
||||
for (let i = _CO.indexOf(id2) + 1; _CO[i] != id1; i++) {
|
||||
if (i < _CO.length)
|
||||
oNodes.push(_CO[i]);
|
||||
else i = -1;
|
||||
};
|
||||
return oNodes;
|
||||
};
|
||||
|
||||
kBucket.prevNode = function(id, N = 1) {
|
||||
let n = N || SNCO.length;
|
||||
if (!SNCO.includes(id))
|
||||
throw Error('Given node is not supernode');
|
||||
self.prevNode = function(id, N = 1) {
|
||||
let n = N || _CO.length;
|
||||
if (!_CO.includes(id))
|
||||
throw Error('Given node is not KB');
|
||||
let pNodes = [];
|
||||
for (let i = 0, j = SNCO.indexOf(id) - 1; i < n; j--) {
|
||||
if (j == SNCO.indexOf(id))
|
||||
for (let i = 0, j = _CO.indexOf(id) - 1; i < n; j--) {
|
||||
if (j == _CO.indexOf(id))
|
||||
break;
|
||||
else if (j > -1)
|
||||
pNodes[i++] = SNCO[j];
|
||||
else j = SNCO.length;
|
||||
pNodes[i++] = _CO[j];
|
||||
else j = _CO.length;
|
||||
};
|
||||
return (N == 1 ? pNodes[0] : pNodes);
|
||||
};
|
||||
|
||||
kBucket.nextNode = function(id, N = 1) {
|
||||
let n = N || SNCO.length;
|
||||
if (!SNCO.includes(id))
|
||||
throw Error('Given node is not supernode');
|
||||
self.nextNode = function(id, N = 1) {
|
||||
let n = N || _CO.length;
|
||||
if (!_CO.includes(id))
|
||||
throw Error('Given node is not KB');
|
||||
let nNodes = [];
|
||||
for (let i = 0, j = SNCO.indexOf(id) + 1; i < n; j++) {
|
||||
if (j == SNCO.indexOf(id))
|
||||
for (let i = 0, j = _CO.indexOf(id) + 1; i < n; j++) {
|
||||
if (j == _CO.indexOf(id))
|
||||
break;
|
||||
else if (j < SNCO.length)
|
||||
nNodes[i++] = SNCO[j];
|
||||
else if (j < _CO.length)
|
||||
nNodes[i++] = _CO[j];
|
||||
else j = -1;
|
||||
};
|
||||
return (N == 1 ? nNodes[0] : nNodes);
|
||||
};
|
||||
|
||||
kBucket.closestNode = function(id, N = 1) {
|
||||
self.closestNode = function(id, N = 1) {
|
||||
let decodedId = decodeID(id);
|
||||
let n = N || SNCO.length;
|
||||
let cNodes = SNKB.closest(decodedId, n)
|
||||
let n = N || _CO.length;
|
||||
let cNodes = _KB.closest(decodedId, n)
|
||||
.map(k => k.floID);
|
||||
return (N == 1 ? cNodes[0] : cNodes);
|
||||
};
|
||||
})(typeof global !== "undefined" ? global : window);
|
||||
}
|
||||
19
src/main.js
19
src/main.js
@ -2,7 +2,7 @@ const config = require('../args/config.json');
|
||||
global.floGlobals = require("./floGlobals");
|
||||
require('./set_globals');
|
||||
require('./lib');
|
||||
require('./kBucket');
|
||||
const K_Bucket = require('./kBucket');
|
||||
require('./floCrypto');
|
||||
require('./floBlockchainAPI');
|
||||
const Database = require("./database");
|
||||
@ -87,13 +87,12 @@ function refreshBlockchainData(base, flag) {
|
||||
return new Promise((resolve, reject) => {
|
||||
readSupernodeConfigFromAPI(base, flag).then(result => {
|
||||
console.log(result);
|
||||
kBucket.launch().then(result => {
|
||||
//console.log(result);
|
||||
readAppSubAdminListFromAPI(base)
|
||||
.then(result => console.log(result))
|
||||
.catch(warn => console.warn(warn))
|
||||
.finally(_ => resolve("Refreshed Data from blockchain"));
|
||||
}).catch(error => reject(error));
|
||||
global.kBucket = new K_Bucket();
|
||||
console.log("SNCO:", kBucket.order);
|
||||
readAppSubAdminListFromAPI(base)
|
||||
.then(result => console.log(result))
|
||||
.catch(warn => console.warn(warn))
|
||||
.finally(_ => resolve("Refreshed Data from blockchain"));
|
||||
}).catch(error => reject(error));
|
||||
});
|
||||
};
|
||||
@ -241,12 +240,12 @@ function selfDiskMigration(node_change) {
|
||||
disks.push(result[i][j].split("_")[1]);
|
||||
disks.forEach(n => {
|
||||
if (node_change[n] === false)
|
||||
DB.dropTable(n).then(_ => null).catch(_ => null);
|
||||
DB.dropTable(n).then(_ => null).catch(e => console.error(e));
|
||||
DB.getData(n, 0).then(result => {
|
||||
result.forEach(d => {
|
||||
let closest = kBucket.closestNode(d.receiverID);
|
||||
if (closest !== n)
|
||||
DB.deleteData(n, d.vectorClock).then(_ => null).catch(_ => null);
|
||||
DB.deleteData(n, d.vectorClock).then(_ => null).catch(e => console.error(e));
|
||||
});
|
||||
}).catch(error => console.error(error));
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user