Skip to content

Commit

Permalink
fix: node discovery extended, empty host info bug fixed, logging enha…
Browse files Browse the repository at this point in the history
…nced
  • Loading branch information
Baha committed Dec 26, 2021
1 parent d78a50c commit 0e7f106
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 45 deletions.
29 changes: 27 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"devDependencies": {
"@openapitools/openapi-generator-cli": "^2.4.15",
"@types/humanize-duration": "^3.27.0",
"@types/mongoose": "^5.7.14",
"@types/node": "^14.14.10",
"@types/winston": "^2.4.4",
Expand Down Expand Up @@ -49,6 +50,7 @@
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"humanize-duration": "^3.27.1",
"module-alias": "^2.2.2",
"mongoose": "^5.9.16",
"symbol-sdk": "^1.0.3",
Expand Down
1 change: 1 addition & 0 deletions src/config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"PEER_NODE_PORT": 7900,
"REQUEST_TIMEOUT": 5000,
"NUMBER_OF_NODE_REQUEST_CHUNK": 10,
"NODE_PEERS_REQUEST_CHUNK_SIZE": 50,
"PREFERRED_NODES": ["*.symboldev.network"],
"MIN_PARTNER_NODE_VERSION": 16777728
}
5 changes: 5 additions & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface Symbol {
interface Monitor {
NODE_MONITOR_SCHEDULE_INTERVAL: number;
NUMBER_OF_NODE_REQUEST_CHUNK: number;
NODE_PEERS_REQUEST_CHUNK_SIZE: number;
CHAIN_HEIGHT_MONITOR_SCHEDULE_INTERVAL: number;
GEOLOCATION_MONITOR_SCHEDULE_INTERVAL: number;
API_NODE_PORT: number;
Expand Down Expand Up @@ -49,6 +50,7 @@ export const symbol: Symbol = {
export const monitor: Monitor = {
NODE_MONITOR_SCHEDULE_INTERVAL: Number(process.env.NODE_MONITOR_SCHEDULE_INTERVAL) || config.NODE_MONITOR_SCHEDULE_INTERVAL,
NUMBER_OF_NODE_REQUEST_CHUNK: Number(process.env.NUMBER_OF_NODE_REQUEST_CHUNK) || config.NUMBER_OF_NODE_REQUEST_CHUNK,
NODE_PEERS_REQUEST_CHUNK_SIZE: Number(process.env.NODE_PEERS_REQUEST_CHUNK_SIZE) || config.NODE_PEERS_REQUEST_CHUNK_SIZE,
CHAIN_HEIGHT_MONITOR_SCHEDULE_INTERVAL:
Number(process.env.CHAIN_HEIGHT_MONITOR_SCHEDULE_INTERVAL) || config.CHAIN_HEIGHT_MONITOR_SCHEDULE_INTERVAL,
GEOLOCATION_MONITOR_SCHEDULE_INTERVAL:
Expand Down Expand Up @@ -85,6 +87,9 @@ export const verifyConfig = (cfg: Config): boolean => {
if (isNaN(cfg.monitor.NUMBER_OF_NODE_REQUEST_CHUNK) || cfg.monitor.NUMBER_OF_NODE_REQUEST_CHUNK < 0)
error = 'Invalid "NUMBER_OF_NODE_REQUEST_CHUNK"';

if (isNaN(cfg.monitor.NODE_PEERS_REQUEST_CHUNK_SIZE) || cfg.monitor.NODE_PEERS_REQUEST_CHUNK_SIZE < 0)
error = 'Invalid "NODE_PEERS_REQUEST_CHUNK_SIZE"';

if (isNaN(cfg.monitor.API_NODE_PORT) || cfg.monitor.API_NODE_PORT <= 0 || cfg.monitor.API_NODE_PORT >= 10000)
error = 'Invalid "API_NODE_PORT"';

Expand Down
5 changes: 4 additions & 1 deletion src/models/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const NodeSchema: Schema = new Schema({
type: Number,
required: false,
},
required: false,
},
apiStatus: {
webSocket: {
Expand Down Expand Up @@ -236,7 +237,9 @@ NodeSchema.set('toObject', {
export const Node = mongoose.model<NodeDocument>('Node', NodeSchema);

export const validateNodeModel = (node: any): boolean => {
if (!node || typeof node !== 'object') return false;
if (!node || typeof node !== 'object') {
return false;
}

return !new Node(node).validateSync();
};
8 changes: 4 additions & 4 deletions src/services/ApiNodeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export class ApiNodeService {
try {
return (await HTTP.get(`${hostUrl}/node/info`)).data;
} catch (e) {
logger.error(`Fail to request /node/info: ${hostUrl}`, e);
logger.error(`[getNodeInfo] Fail to request /node/info: ${hostUrl}`, e);
return null;
}
};
Expand All @@ -172,7 +172,7 @@ export class ApiNodeService {
try {
return (await HTTP.get(`${hostUrl}/chain/info`)).data;
} catch (e) {
logger.error(`Fail to request /chain/info: ${hostUrl}`, e);
logger.error(`[getNodeChainInfo] Fail to request /chain/info: ${hostUrl}`, e);
return null;
}
};
Expand All @@ -183,7 +183,7 @@ export class ApiNodeService {

return nodeServerInfo.serverInfo;
} catch (e) {
logger.error(`Fail to request /node/server: ${hostUrl}`, e);
logger.error(`[getNodeServer] Fail to request /node/server: ${hostUrl}`, e);
return null;
}
};
Expand All @@ -194,7 +194,7 @@ export class ApiNodeService {

return health.status;
} catch (e) {
logger.error(`Fail to request /node/health: ${hostUrl}`, e);
logger.error(`[getNodeHealth] Fail to request /node/health: ${hostUrl}`, e);
return null;
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/services/ChainHeightMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class ChainHeightMonitor {
try {
this.nodeList = (await DataBase.getNodeList()).filter((node) => isAPIRole(node.roles));
} catch (e) {
logger.error('Failed to get node list. Use nodes from config');
logger.error('[getNodeList] Failed to get node list. Use nodes from config');
for (const node of symbol.NODES) {
const url = new URL(node);
const hostUrl = await ApiNodeService.buildHostUrl(url.hostname);
Expand Down
16 changes: 9 additions & 7 deletions src/services/DataBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,28 +109,30 @@ export class DataBase {
collectionName: string,
) => {
const prevState = await model.find().exec();
let error = Error();

try {
await model.deleteMany();
} catch (e) {
logger.error(`Update collection "${collectionName}" failed. Error during "model.deleteMany()". ${error.message}`);
throw error;
const msg = `Update collection "${collectionName}" failed. Error during "model.deleteMany()". ${e.message}`;

logger.error(msg);
throw new Error(msg);
}

try {
await model.insertMany(documents);
} catch (e) {
logger.error(`Update collection "${collectionName}" failed. Error during "model.insertMany()". ${error.message}`);
await model.insertMany(prevState);
throw error;
const msg = `Update collection "${collectionName}" failed. Error during "model.insertMany()". ${e.message}`;

logger.error(msg);
throw new Error(msg);
}

const currentState = await model.find().exec();

if (documents.length !== currentState.length) {
logger.error(
`Update collection "${collectionName}" failed. Collectin.length(${currentState.length}) !== documentsToInsert.length(${documents.length})`,
`Update collection "${collectionName}" failed. Collection.length(${currentState.length}) !== documentsToInsert.length(${documents.length})`,
);
await model.insertMany(prevState);
throw new Error(`Failed to update collection "${collectionName}. Length verification failed`);
Expand Down
2 changes: 1 addition & 1 deletion src/services/HostInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class HostInfo {
zip: data.zip,
};
} catch (e) {
logger.error(`Failed to get host ${host} info ${e.message}`);
logger.error(`[getHostDetail] Failed to get host ${host} info ${e.message}`);
return null;
}
};
Expand Down
Loading

0 comments on commit 0e7f106

Please sign in to comment.