bug fix: backup/sync
- Fixed minor bugs in backup/sync process - Fixed: Master doesn't sync when it comes back online - Decreased backup interval time to 1 min.
This commit is contained in:
parent
2c35c545e8
commit
bf302c3ebc
@ -276,9 +276,10 @@ function informLiveNodes(init) {
|
|||||||
} else
|
} else
|
||||||
console.warn(`Node(${nodes[i]}) is offline`);
|
console.warn(`Node(${nodes[i]}) is offline`);
|
||||||
if (init) {
|
if (init) {
|
||||||
if (flag === true)
|
if (flag === true) {
|
||||||
collectShares.active = true;
|
collectShares.active = true;
|
||||||
else {
|
syncRequest();
|
||||||
|
} else {
|
||||||
//No other node is active (possible 1st node to start exchange)
|
//No other node is active (possible 1st node to start exchange)
|
||||||
console.debug("Starting the exchange...")
|
console.debug("Starting the exchange...")
|
||||||
let newSink = generateNewSink();
|
let newSink = generateNewSink();
|
||||||
@ -294,8 +295,19 @@ function informLiveNodes(init) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function syncRequest(cur = global.myFloID) {
|
||||||
|
//Sync data from next available node
|
||||||
|
console.debug("syncRequest", cur);
|
||||||
|
let nextNode = nodeKBucket.nextNode(cur);
|
||||||
|
if (!nextNode)
|
||||||
|
return console.warn("No nodes available to Sync");
|
||||||
|
connectWS(nextNode)
|
||||||
|
.then(ws => slave.syncRequest(ws))
|
||||||
|
.catch(_ => syncRequest(nextNode));
|
||||||
|
}
|
||||||
|
|
||||||
function updateMaster(floID) {
|
function updateMaster(floID) {
|
||||||
let currentMaster = mod === MASTER_MODE ? global.floID : slave.masterWS.floID;
|
let currentMaster = mod === MASTER_MODE ? global.myFloID : slave.masterWS.floID;
|
||||||
if (nodeList.indexOf(floID) < nodeList.indexOf(currentMaster))
|
if (nodeList.indexOf(floID) < nodeList.indexOf(currentMaster))
|
||||||
connectToMaster();
|
connectToMaster();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const WAIT_TIME = 30 * 60 * 1000,
|
const WAIT_TIME = 10 * 60 * 1000,
|
||||||
BACKUP_INTERVAL = 10 * 60 * 1000;
|
BACKUP_INTERVAL = 1 * 60 * 1000;
|
||||||
|
|
||||||
var DB; //Container for Database connection
|
var DB; //Container for Database connection
|
||||||
var masterWS = null; //Container for Master websocket connection
|
var masterWS = null; //Container for Master websocket connection
|
||||||
@ -17,7 +17,7 @@ function startSlaveProcess(ws) {
|
|||||||
masterWS = ws;
|
masterWS = ws;
|
||||||
//inform master
|
//inform master
|
||||||
let message = {
|
let message = {
|
||||||
floID: global.floID,
|
floID: global.myFloID,
|
||||||
pubKey: global.pubKey,
|
pubKey: global.pubKey,
|
||||||
req_time: Date.now(),
|
req_time: Date.now(),
|
||||||
type: "SLAVE_CONNECT"
|
type: "SLAVE_CONNECT"
|
||||||
@ -46,11 +46,11 @@ function requestBackupSync(ws) {
|
|||||||
Users: "created",
|
Users: "created",
|
||||||
Request_Log: "request_time",
|
Request_Log: "request_time",
|
||||||
Transactions: "tx_time",
|
Transactions: "tx_time",
|
||||||
priceHistory: "rec_time",
|
//priceHistory: "rec_time",
|
||||||
_backup: "timestamp"
|
_backup: "timestamp"
|
||||||
};
|
};
|
||||||
let subs = [];
|
let subs = [];
|
||||||
for (t in tables)
|
for (let t in tables)
|
||||||
subs.push(`SELECT MAX(${tables[t]}) as ts FROM ${t}`);
|
subs.push(`SELECT MAX(${tables[t]}) as ts FROM ${t}`);
|
||||||
DB.query(`SELECT MAX(ts) as last_time FROM (${subs.join(' UNION ')}) AS Z`).then(result => {
|
DB.query(`SELECT MAX(ts) as last_time FROM (${subs.join(' UNION ')}) AS Z`).then(result => {
|
||||||
let request = {
|
let request = {
|
||||||
@ -61,7 +61,6 @@ function requestBackupSync(ws) {
|
|||||||
req_time: Date.now()
|
req_time: Date.now()
|
||||||
};
|
};
|
||||||
request.sign = floCrypto.signData(request.type + "|" + request.req_time, global.myPrivKey);
|
request.sign = floCrypto.signData(request.type + "|" + request.req_time, global.myPrivKey);
|
||||||
console.debug("REQUEST: ", request);
|
|
||||||
ws.send(JSON.stringify(request));
|
ws.send(JSON.stringify(request));
|
||||||
resolve(request);
|
resolve(request);
|
||||||
}).catch(error => reject(error))
|
}).catch(error => reject(error))
|
||||||
@ -77,10 +76,11 @@ const requestInstance = {
|
|||||||
add_data: null,
|
add_data: null,
|
||||||
total_add: null,
|
total_add: null,
|
||||||
request: null,
|
request: null,
|
||||||
|
onetime: null,
|
||||||
last_response_time: null
|
last_response_time: null
|
||||||
};
|
};
|
||||||
|
|
||||||
requestInstance.open = function() {
|
requestInstance.open = function(ws = null) {
|
||||||
const self = this;
|
const self = this;
|
||||||
//Check if there is an active request
|
//Check if there is an active request
|
||||||
if (self.request) {
|
if (self.request) {
|
||||||
@ -90,16 +90,25 @@ requestInstance.open = function() {
|
|||||||
else
|
else
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!masterWS)
|
//Use websocket connection if passed, else use masterWS if available
|
||||||
return console.warn("Not connected to master");
|
if (ws) {
|
||||||
requestBackupSync(masterWS).then(request => {
|
ws.on('message', processDataFromMaster);
|
||||||
|
self.onetime = true;
|
||||||
|
} else if (masterWS)
|
||||||
|
ws = masterWS;
|
||||||
|
else return console.warn("Not connected to master");
|
||||||
|
|
||||||
|
requestBackupSync(ws).then(request => {
|
||||||
self.request = request;
|
self.request = request;
|
||||||
self.ws = masterWS;
|
self.ws = ws;
|
||||||
}).catch(error => console.error(error))
|
}).catch(error => console.error(error))
|
||||||
}
|
}
|
||||||
|
|
||||||
requestInstance.close = function() {
|
requestInstance.close = function() {
|
||||||
const self = this;
|
const self = this;
|
||||||
|
if (self.onetime)
|
||||||
|
self.ws.close();
|
||||||
|
self.onetime = null;
|
||||||
self.ws = null;
|
self.ws = null;
|
||||||
self.delete_sync = null;
|
self.delete_sync = null;
|
||||||
self.add_sync = null;
|
self.add_sync = null;
|
||||||
@ -215,9 +224,11 @@ function deleteData(data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function addUpdateData(table, data) {
|
function addUpdateData(table, data) {
|
||||||
|
if (!data.length)
|
||||||
|
return;
|
||||||
let cols = Object.keys(data[0]),
|
let cols = Object.keys(data[0]),
|
||||||
_mark = "(" + Array(cols.length).fill('?') + ")";
|
_mark = "(" + Array(cols.length).fill('?') + ")";
|
||||||
values = data.map(r => cols.map(c => validateValue(r[c]))).flat();
|
let values = data.map(r => cols.map(c => validateValue(r[c]))).flat();
|
||||||
let statement = `INSERT INTO ${table} (${cols}) VALUES ${Array(data.length).fill(_mark)} AS new` +
|
let statement = `INSERT INTO ${table} (${cols}) VALUES ${Array(data.length).fill(_mark)} AS new` +
|
||||||
" ON DUPLICATE KEY UPDATE " + cols.filter(c => c != 'id').map(c => c + " = new." + c);
|
" ON DUPLICATE KEY UPDATE " + cols.filter(c => c != 'id').map(c => c + " = new." + c);
|
||||||
DB.query(statement, values).then(_ => null).catch(error => console.error(error));
|
DB.query(statement, values).then(_ => null).catch(error => console.error(error));
|
||||||
@ -231,7 +242,7 @@ function addImmutableData(table, data) {
|
|||||||
};
|
};
|
||||||
let cols = Object.keys(data[0]),
|
let cols = Object.keys(data[0]),
|
||||||
_mark = "(" + Array(cols.length).fill('?') + ")";
|
_mark = "(" + Array(cols.length).fill('?') + ")";
|
||||||
values = data.map(r => cols.map(c => validateValue(r[c]))).flat();
|
let values = data.map(r => cols.map(c => validateValue(r[c]))).flat();
|
||||||
let statement = `INSERT INTO ${table} (${cols}) VALUES ${Array(data.length).fill(_mark)}`;
|
let statement = `INSERT INTO ${table} (${cols}) VALUES ${Array(data.length).fill(_mark)}`;
|
||||||
if (table in primaryKeys)
|
if (table in primaryKeys)
|
||||||
statement += ` ON DUPLICATE KEY UPDATE ${primaryKeys[table]}=${primaryKeys[table]}`;
|
statement += ` ON DUPLICATE KEY UPDATE ${primaryKeys[table]}=${primaryKeys[table]}`;
|
||||||
@ -248,5 +259,6 @@ module.exports = {
|
|||||||
return masterWS;
|
return masterWS;
|
||||||
},
|
},
|
||||||
start: startSlaveProcess,
|
start: startSlaveProcess,
|
||||||
stop: stopSlaveProcess
|
stop: stopSlaveProcess,
|
||||||
|
syncRequest: ws => requestInstance.open(ws)
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user