Skip to content

Commit

Permalink
feat(core): deprecated globals.databus
Browse files Browse the repository at this point in the history
I also updated packages, again.
  • Loading branch information
tabarra committed Jul 4, 2023
1 parent 8dcd0d6 commit cc1e511
Show file tree
Hide file tree
Showing 18 changed files with 660 additions and 614 deletions.
12 changes: 5 additions & 7 deletions core/components/HealthMonitor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,6 @@ export default class HealthMonitor {
const currTimestamp = now();
const elapsedRefreshStatus = currTimestamp - this.lastRefreshStatus;
if (this.lastRefreshStatus !== null && elapsedRefreshStatus > 10) {
globals.databus.txStatsData.monitorStats.freezeSeconds.push(elapsedRefreshStatus - 1);
if (globals.databus.txStatsData.monitorStats.freezeSeconds.length > 30) globals.databus.txStatsData.monitorStats.freezeSeconds.shift();
console.error(`FXServer was frozen for ${elapsedRefreshStatus - 1} seconds for unknown reason (random issue, VPS Lag, DDoS, etc).`);
console.error('Don\'t worry, txAdmin is preventing the server from being restarted.');
this.lastRefreshStatus = currTimestamp;
Expand Down Expand Up @@ -232,7 +230,7 @@ export default class HealthMonitor {
//Check if fxChild is closed, in this case no need to wait the failure count
const processStatus = globals.fxRunner.getStatus();
if (processStatus == 'closed') {
globals.databus.txStatsData.monitorStats.restartReasons.close++;
globals.statisticsManager.registerFxserverRestart('close');
this.restartFXServer(
'server close detected',
globals.translator.t('restarter.crash_detected'),
Expand Down Expand Up @@ -324,13 +322,13 @@ export default class HealthMonitor {
}
} else if (elapsedHealthCheck > this.hardConfigs.healthCheck.failLimit) {
//FIXME: se der hang tanto HB quanto HC, ele ainda sim cai nesse caso
globals.databus.txStatsData.monitorStats.restartReasons.healthCheck++;
globals.statisticsManager.registerFxserverRestart('healthCheck');
this.restartFXServer(
'server partial hang detected',
globals.translator.t('restarter.hang_detected'),
);
} else {
globals.databus.txStatsData.monitorStats.restartReasons.heartBeat++;
globals.statisticsManager.registerFxserverRestart('heartBeat');
this.restartFXServer(
'server hang detected',
globals.translator.t('restarter.hang_detected'),
Expand All @@ -349,7 +347,7 @@ export default class HealthMonitor {
&& tsNow - this.lastSuccessfulHTTPHeartBeat > 15
&& tsNow - this.lastSuccessfulFD3HeartBeat < 5
) {
globals.databus.txStatsData.monitorStats.heartBeatStats.httpFailed++;
globals.statisticsManager.registerFxserverRestart('http');
}
this.lastSuccessfulFD3HeartBeat = tsNow;
} else if (source === 'http') {
Expand All @@ -358,7 +356,7 @@ export default class HealthMonitor {
&& tsNow - this.lastSuccessfulFD3HeartBeat > 15
&& tsNow - this.lastSuccessfulHTTPHeartBeat < 5
) {
globals.databus.txStatsData.monitorStats.heartBeatStats.fd3Failed++;
globals.statisticsManager.registerFxserverRestart('fd3');
}
this.lastSuccessfulHTTPHeartBeat = tsNow;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,35 @@ import consoleFactory from '@extras/console';
const console = consoleFactory(modulename);


/*
NOTE Resource load scenarios knowledge base:
- resource lua error:
- `onResourceStarting` sourceRes
- print lua error
- `onResourceStart` sourceRes
- resource lua crash/hang:
- `onResourceStarting` sourceRes
- crash/hang
- dependency missing:
- `onResourceStarting` sourceRes
- does not get to `onResourceStart`
- dependency success:
- `onResourceStarting` sourceRes
- `onResourceStarting` dependency
- `onResourceStart` dependency
- `onResourceStart` sourceRes
- webpack/yarn fail:
- `onResourceStarting` sourceRes
- does not get to `onResourceStart`
- webpack/yarn success:
- `onResourceStarting` chat
- `onResourceStarting` yarn
- `onResourceStart` yarn
- `onResourceStarting` webpack
- `onResourceStart` webpack
- server first tick
- wait for build
- `onResourceStarting` chat
- `onResourceStart` chat
- ensure started resource:
- `onResourceStop` sourceRes
- `onResourceStarting` sourceRes
- `onResourceStart` sourceRes
- `onServerResourceStop` sourceRes
- `onServerResourceStart` sourceRes
*/
type ResourceEventType = {
type: 'txAdminResourceEvent';
resource: string;
event: 'onResourceStarting'
| 'onResourceStart'
| 'onServerResourceStart'
| 'onResourceListRefresh'
| 'onResourceStop'
| 'onServerResourceStop';
};

type ResourceReportType = {
ts: Date,
resources: any[]
}

export default class ResourcesManager {
constructor() {
this.activeStartingTime = null;
this.activeStartingResource = null;
this.lastResourceStartTime = null;
}
activeStartingTime: Date | null = null;
activeStartingResource: string | null = null;
lastResourceStartTime: Date | null = null;
resourceReport?: ResourceReportType;

constructor() {}


/**
* Handler for all txAdminResourceStatus structured trace events
* @param {object} payload
* @param {string} mutex
* Handler for all txAdminResourceEvent structured trace events
*/
handleServerEvents(payload, mutex) {
handleServerEvents(payload: ResourceEventType, mutex: string) {
// console.log(`${payload.event}: ${payload.resource}`);
if (payload.event === 'onResourceStarting') {
this.activeStartingResource = payload.resource;
Expand All @@ -77,16 +54,67 @@ export default class ResourcesManager {


/**
* Handler for server restart
* Handler for server restart.
* NOTE: replace this when we start tracking resource states internally
*/
tmpGetPendingStart() {
const getSecondsDiff = (date) => {
return date !== null ? Math.round((new Date() - date) / 1000) : null;
const getSecondsDiff = (date: Date | null) => {
return date !== null ? Math.round((Date.now() - date.getTime()) / 1000) : null;
}
return {
startingResName: this.activeStartingResource,
startingElapsedSecs: getSecondsDiff(this.activeStartingTime),
lastStartElapsedSecs: getSecondsDiff(this.lastResourceStartTime),
};
}


/**
* Handle resource report.
* NOTE: replace this when we start tracking resource states internally
*/
tmpUpdateResourceList(resources: any[]) {
this.resourceReport = {
ts: new Date(),
resources,
}
}
};

/*
NOTE Resource load scenarios knowledge base:
- resource lua error:
- `onResourceStarting` sourceRes
- print lua error
- `onResourceStart` sourceRes
- resource lua crash/hang:
- `onResourceStarting` sourceRes
- crash/hang
- dependency missing:
- `onResourceStarting` sourceRes
- does not get to `onResourceStart`
- dependency success:
- `onResourceStarting` sourceRes
- `onResourceStarting` dependency
- `onResourceStart` dependency
- `onResourceStart` sourceRes
- webpack/yarn fail:
- `onResourceStarting` sourceRes
- does not get to `onResourceStart`
- webpack/yarn success:
- `onResourceStarting` chat
- `onResourceStarting` yarn
- `onResourceStart` yarn
- `onResourceStarting` webpack
- `onResourceStart` webpack
- server first tick
- wait for build
- `onResourceStarting` chat
- `onResourceStart` chat
- ensure started resource:
- `onResourceStop` sourceRes
- `onResourceStarting` sourceRes
- `onResourceStart` sourceRes
- `onServerResourceStop` sourceRes
- `onServerResourceStart` sourceRes
*/
39 changes: 35 additions & 4 deletions core/components/StatisticsManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const jweHeader = {

/**
* Responsible for collecting runtime data for statistics
* NOTE: the register functions don't throw because we rather break stats than txAdmin itself
*/
export default class StatisticsManager {
readonly #txAdmin: TxAdmin;
Expand All @@ -39,18 +40,31 @@ export default class StatisticsManager {
public readonly whitelistCheckTime = new QuantileArray(5000, 50);
public currHbData: string = '{"error": "not yet initialized in StatisticsManager"}';

public monitorStats = {
healthIssues: {
fd3: 0,
http: 0,
},
restartReasons: {
close: 0,
heartBeat: 0,
healthCheck: 0,
},
};


constructor(txAdmin: TxAdmin) {
this.#txAdmin = txAdmin;
this.loadStatsPublicKey();

//Delaying this because host static data takes 10+ seconds to be set
setTimeout(() => {
this.refreshHbData().catch((e) => {});
this.refreshHbData().catch((e) => { });
}, 15_000);

//Cron function
setInterval(() => {
this.refreshHbData().catch((e) => {});
this.refreshHbData().catch((e) => { });
}, 60_000);
}

Expand All @@ -70,7 +84,6 @@ export default class StatisticsManager {

/**
* Called by HealthMonitor to keep track of the last boot time
* NOTE: we are not throwing an error because this is not a fundamental part of txAdmin
*/
registerFxserverBoot(seconds: number) {
if (!Number.isInteger(seconds) || seconds < 0) {
Expand All @@ -81,6 +94,24 @@ export default class StatisticsManager {
}


/**
* Called by HealthMonitor to keep track of the fxserver restart reasons
*/
registerFxserverRestart(reason: keyof typeof this.monitorStats.restartReasons) {
if (!(reason in this.monitorStats.restartReasons)) return;
this.monitorStats.restartReasons[reason]++;
}


/**
* Called by HealthMonitor to keep track of the fxserver HB/HC failures
*/
registerFxserverHealthIssue(type: keyof typeof this.monitorStats.healthIssues) {
if (!(type in this.monitorStats.healthIssues)) return;
this.monitorStats.healthIssues[type]++;
}


/**
* Processes general txadmin stuff to generate the HB data.
*
Expand Down Expand Up @@ -109,7 +140,7 @@ export default class StatisticsManager {

const tmpDurationDebugLog = (msg: string) => {
// @ts-expect-error
if(globals?.tmpSetHbDataTracking){
if (globals?.tmpSetHbDataTracking) {
console.verbose.debug(`refreshHbData: ${msg}`);
}
}
Expand Down
Loading

0 comments on commit cc1e511

Please sign in to comment.