@voicenter-team/mysql-dynamic-cluster
Version:
Galera cluster with implementation of dynamic choose mysql server for queries, caching, hashing it and metrics
113 lines • 4.83 kB
JavaScript
"use strict";
/**
* Created by Bohdan on Sep, 2021
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PoolStatus = void 0;
const Logger_1 = __importDefault(require("../utils/Logger"));
const Utils_1 = require("../utils/Utils");
const Timer_1 = require("../utils/Timer");
const Validator_1 = require("./Validator");
const LoadFactor_1 = require("./LoadFactor");
const QueryTimer_1 = require("../utils/QueryTimer");
const MetricNames_1 = __importDefault(require("../metrics/MetricNames"));
class PoolStatus {
/**
* @param pool pool of what status to check
* @param settings pool settings
* @param active default pool active status before status check
* @param availableConnectionCount max connection count in the pool
*/
constructor(pool, settings, active, availableConnectionCount) {
// Check if valid by passed validator
this._isValid = false;
// Score from load factor
this._loadScore = 0;
// Time to next check status. Time in ms
this._nextCheckTime = 10000;
this._pool = pool;
this.active = active;
this.availableConnectionCount = availableConnectionCount;
this._isValid = false;
this._loadScore = 100000;
this._validator = new Validator_1.Validator(this, settings.validators);
this._loadFactor = new LoadFactor_1.LoadFactor(settings.loadFactors);
this.timerCheckRange = settings.timerCheckRange;
this.timerCheckMultiplier = settings.timerCheckMultiplier;
this._timer = new Timer_1.Timer(this.checkStatus.bind(this));
Logger_1.default.debug("Pool status configured");
}
get isValid() {
return this._isValid;
}
get queryTime() {
return this._queryTime;
}
get loadScore() {
return this._loadScore;
}
/**
* Stop pool check status timer
*/
stopTimerCheck() {
this._timer.dispose();
Logger_1.default.info("Check status in pool stopped");
}
/**
* Check pool status
*/
checkStatus() {
return __awaiter(this, void 0, void 0, function* () {
const queryTimer = new QueryTimer_1.QueryTimer(MetricNames_1.default.pool.queryTime);
try {
if (!this.active)
return;
Logger_1.default.debug("checking pool status in host: " + this._pool.host);
queryTimer.start();
const result = yield this._pool.query(`SHOW GLOBAL STATUS;`, { redis: false });
queryTimer.end();
this._queryTime = queryTimer.get();
this._isValid = this._validator.check(result);
Logger_1.default.debug("Is status ok in host " + this._pool.host + "? -> " + this._isValid.toString());
this._loadScore = this._loadFactor.check(result);
Logger_1.default.debug("Load score by checking status in host " + this._pool.host + " is " + this._loadScore);
this.nextCheckStatus();
}
catch (err) {
Logger_1.default.error("Something wrong while checking status in host: " + this._pool.host + ".\n Message: " + err.message);
queryTimer.end();
this._queryTime = queryTimer.get();
this.nextCheckStatus(true);
}
});
}
/**
* Start timer for next pool check status
* @param downgrade downgrade time for next check pool status?
* @private
*/
nextCheckStatus(downgrade = false) {
if (downgrade) {
this._nextCheckTime /= this.timerCheckMultiplier;
}
else {
this._nextCheckTime *= this.timerCheckMultiplier;
}
this._nextCheckTime = Utils_1.Utils.clamp(this._nextCheckTime, this.timerCheckRange.start, this.timerCheckRange.end);
this._timer.start(this._nextCheckTime);
}
}
exports.PoolStatus = PoolStatus;
//# sourceMappingURL=PoolStatus.js.map