Merge branch 'master' of github.com:as2252258/kiri-databases

This commit is contained in:
2022-09-02 16:40:17 +08:00
2 changed files with 283 additions and 311 deletions
-57
View File
@@ -47,8 +47,6 @@ class DatabasesProviders extends Providers
if (empty($databases)) { if (empty($databases)) {
return; return;
} }
$this->provider->on(OnWorkerStart::class, [$this, 'check']);
$this->provider->on(OnTaskerStart::class, [$this, 'check']);
$this->provider->on(OnWorkerExit::class, [$this, 'exit'], 9999); $this->provider->on(OnWorkerExit::class, [$this, 'exit'], 9999);
foreach ($databases as $key => $database) { foreach ($databases as $key => $database) {
$application->set($key, $this->_settings($database)); $application->set($key, $this->_settings($database));
@@ -85,61 +83,6 @@ class DatabasesProviders extends Providers
} }
/**
* @param OnTaskerStart|OnWorkerStart $start
* @return void
*/
public function check(OnTaskerStart|OnWorkerStart $start): void
{
Timer::tick(10000, function ($timerId) {
$valid = $count = 0;
$logger = Kiri::getDi()->get(LoggerInterface::class);
$databases = Config::get('databases.connections', []);
if (!empty($databases)) {
[$valid, $count] = $this->each($databases, $logger);
}
$const = 'Worker %d db client has %d, valid %d';
$logger->alert(sprintf($const, env('environmental_workerId'), $count, $valid));
if ($this->container->get(WorkerStatus::class)->is(StatusEnum::EXIT)) {
Timer::clear($timerId);
}
});
}
/**
* @param $databases
* @param LoggerInterface $logger
* @return array
*/
public function each($databases, LoggerInterface $logger): array
{
$connection = Kiri::getDi()->get(PoolConnection::class);
$valid = $count = 0;
foreach ($databases as $database) {
try {
[$total, $success] = $connection->check($database['cds']);
$count += $total;
$valid += $success;
if (isset($database['slaveConfig']) && isset($database['slaveConfig']['cds'])) {
if ($database['slaveConfig']['cds'] != $database['cds']) {
[$total, $success] = $connection->check($database['slaveConfig']['cds']);
$count += $total;
$valid += $success;
}
}
} catch (\Throwable $throwable) {
$logger->error($throwable->getMessage());
}
}
return [$valid, $count];
}
/** /**
* @param $database * @param $database
* @return array * @return array
+283 -254
View File
@@ -8,6 +8,9 @@ use Kiri\Events\EventProvider;
use Kiri\Pool\StopHeartbeatCheck; use Kiri\Pool\StopHeartbeatCheck;
use Kiri\Server\Events\OnWorkerExit; use Kiri\Server\Events\OnWorkerExit;
use PDOStatement; use PDOStatement;
use Kiri\Server\WorkerStatus;
use Kiri\Server\Abstracts\StatusEnum;
use Swoole\Timer;
/** /**
* *
@@ -15,308 +18,334 @@ use PDOStatement;
class PDO implements StopHeartbeatCheck class PDO implements StopHeartbeatCheck
{ {
const DB_ERROR_MESSAGE = 'The system is busy, please try again later.'; const DB_ERROR_MESSAGE = 'The system is busy, please try again later.';
private ?\PDO $pdo = null; private ?\PDO $pdo = null;
private int $_transaction = 0; private int $_transaction = 0;
private int $_last = 0; private int $_last = 0;
public string $dbname; public string $dbname;
public string $cds; public string $cds;
public string $username; public string $username;
public string $password; public string $password;
public string $charset; public string $charset;
public int $connect_timeout; public int $connect_timeout;
public int $read_timeout; public int $read_timeout;
private int $_timerId = -1;
public array $attributes = []; public array $attributes = [];
/** /**
* @param array $config * @param array $config
*/ */
public function __construct(array $config) public function __construct(array $config)
{ {
$this->dbname = $config['dbname']; $this->dbname = $config['dbname'];
$this->cds = $config['cds']; $this->cds = $config['cds'];
$this->username = $config['username']; $this->username = $config['username'];
$this->password = $config['password']; $this->password = $config['password'];
$this->connect_timeout = $config['connect_timeout'] ?? 30; $this->connect_timeout = $config['connect_timeout'] ?? 30;
$this->read_timeout = $config['read_timeout'] ?? 10; $this->read_timeout = $config['read_timeout'] ?? 10;
$this->charset = $config['charset'] ?? 'utf8mb4'; $this->charset = $config['charset'] ?? 'utf8mb4';
$this->attributes = $config['attributes'] ?? []; $this->attributes = $config['attributes'] ?? [];
} }
/** /**
* @return void * @return void
* @throws Exception * @throws Exception
*/ */
public function init(): void public function init(): void
{ {
$eventProvider = Kiri::getDi()->get(EventProvider::class); $eventProvider = Kiri::getDi()->get(EventProvider::class);
$eventProvider->on(OnWorkerExit::class, [$this, 'onWorkerExit']); $eventProvider->on(OnWorkerExit::class, [$this, 'onWorkerExit']);
} $this->_timerId = Timer::tick(60000, [$this, 'check']);
}
/** /**
* @return bool * @return bool
*/ */
public function inTransaction(): bool public function inTransaction(): bool
{ {
return $this->_transaction > 0; return $this->_transaction > 0;
} }
/** /**
* @param Kiri\Server\Events\OnWorkerExit $exit * @param Kiri\Server\Events\OnWorkerExit $exit
* @return void * @return void
*/ */
public function onWorkerExit(OnWorkerExit $exit): void public function onWorkerExit(OnWorkerExit $exit): void
{ {
$this->stopHeartbeatCheck(); $this->stopHeartbeatCheck();
} }
/** /**
* *
*/ */
public function stopHeartbeatCheck(): void public function stopHeartbeatCheck(): void
{ {
$this->pdo = null; $this->pdo = null;
} if ($this->_timerId > -1) {
Timer::clear($this->_timerId);
$this->_timerId = -1;
}
}
/** /**
* *
*/ */
public function beginTransaction() public function beginTransaction()
{ {
if ($this->_transaction == 0) { if ($this->_transaction == 0) {
$this->_pdo()->beginTransaction(); $this->_pdo()->beginTransaction();
} }
$this->_transaction++; $this->_transaction++;
} }
/** /**
* *
*/ */
public function commit() public function commit()
{ {
$this->_transaction--; $this->_transaction--;
if ($this->_transaction == 0) { if ($this->_transaction == 0) {
$this->_pdo()->commit(); $this->_pdo()->commit();
} }
} }
/** /**
* *
*/ */
public function rollback() public function rollback()
{ {
$this->_transaction--; $this->_transaction--;
if ($this->_transaction == 0) { if ($this->_transaction == 0) {
$this->_pdo()->rollBack(); $this->_pdo()->rollBack();
} }
} }
/** /**
* @param string $sql * @param string $sql
* @param array $params * @param array $params
* @return bool|array|null * @return bool|array|null
* @throws Exception * @throws Exception
*/ */
public function fetchAll(string $sql, array $params = []): bool|null|array public function fetchAll(string $sql, array $params = []): bool|null|array
{ {
$pdo = $this->queryPrev($sql, $params); $pdo = $this->queryPrev($sql, $params);
$result = $pdo->fetchAll(\PDO::FETCH_ASSOC); $result = $pdo->fetchAll(\PDO::FETCH_ASSOC);
$pdo->closeCursor(); $pdo->closeCursor();
return $result; return $result;
} }
/** /**
* @param string $sql * @param string $sql
* @param array $params * @param array $params
* @return bool|array|null * @return bool|array|null
* @throws Exception * @throws Exception
*/ */
public function fetch(string $sql, array $params = []): bool|null|array public function fetch(string $sql, array $params = []): bool|null|array
{ {
$pdo = $this->queryPrev($sql, $params); $pdo = $this->queryPrev($sql, $params);
$result = $pdo->fetch(\PDO::FETCH_ASSOC); $result = $pdo->fetch(\PDO::FETCH_ASSOC);
$pdo->closeCursor(); $pdo->closeCursor();
return $result; return $result;
} }
/** /**
* @param string $sql * @param string $sql
* @param array $params * @param array $params
* @return bool|array|null * @return bool|array|null
* @throws \Exception * @throws \Exception
*/ */
public function fetchColumn(string $sql, array $params = []): bool|null|array public function fetchColumn(string $sql, array $params = []): bool|null|array
{ {
$pdo = $this->queryPrev($sql, $params); $pdo = $this->queryPrev($sql, $params);
$result = $pdo->fetchColumn(\PDO::FETCH_ASSOC); $result = $pdo->fetchColumn(\PDO::FETCH_ASSOC);
$pdo->closeCursor(); $pdo->closeCursor();
return $result; return $result;
} }
/** /**
* @param string $sql * @param string $sql
* @param array $params * @param array $params
* @return int * @return int
* @throws Exception * @throws Exception
*/ */
public function count(string $sql, array $params = []): int public function count(string $sql, array $params = []): int
{ {
$pdo = $this->queryPrev($sql, $params); $pdo = $this->queryPrev($sql, $params);
$result = $pdo->rowCount(); $result = $pdo->rowCount();
$pdo->closeCursor(); $pdo->closeCursor();
return $result; return $result;
} }
/** /**
* @param string $sql * @param string $sql
* @param array $params * @param array $params
* @return PDOStatement * @return PDOStatement
* @throws Exception * @throws Exception
*/ */
private function queryPrev(string $sql, array $params = []): PDOStatement private function queryPrev(string $sql, array $params = []): PDOStatement
{ {
$this->_last = time(); try {
try { if ($this->_timerId === -1) {
if (($statement = $this->_pdo()->query($sql)) === false) { $this->_timerId = Timer::tick(6000, [$this, 'check']);
throw new Exception($this->_pdo()->errorInfo()[1]); }
} $this->_last = time();
return $this->bindValue($statement, $params); if (($statement = $this->_pdo()->query($sql)) === false) {
} catch (\PDOException|\Throwable $throwable) { throw new Exception($this->_pdo()->errorInfo()[1]);
if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) { }
$this->pdo = null; return $this->bindValue($statement, $params);
} catch (\PDOException|\Throwable $throwable) {
if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
$this->pdo = null;
return $this->queryPrev($sql, $params); return $this->queryPrev($sql, $params);
} }
throw new Exception($throwable->getMessage()); throw new Exception($throwable->getMessage());
} }
} }
/** /**
* @return bool * @return bool
*/ */
public function check(): bool public function check(): bool
{ {
try { try {
if ($this->_last == 0) $this->_last = time(); if ($this->_last == 0) $this->_last = time();
if (time() - $this->_last >= 600) { if (time() - $this->_last >= 600) {
throw new Exception('Idle dis.'); return $result = false;
} } else if (!($this->pdo instanceof \PDO)) {
if (!($this->pdo instanceof \PDO)) { return $result = false;
return $result = false; }
} $this->pdo->getAttribute(\PDO::ATTR_SERVER_INFO);
$this->pdo->getAttribute(\PDO::ATTR_SERVER_INFO); $result = true;
$result = true; } catch (\Throwable $throwable) {
} catch (\Throwable $throwable) { if (!str_contains($throwable->getMessage(), 'Idle dis')) {
if (!str_contains($throwable->getMessage(), 'Idle dis')) { Kiri::getLogger()->error($throwable->getMessage());
Kiri::getLogger()->error($throwable->getMessage()); }
} $result = false;
$this->pdo = null; } finally {
$result = false; return $this->afterCheck($result);
} finally { }
return $result; }
}
}
/** /**
* @param PDOStatement $statement * @param bool $result
* @param array $params * @return bool
* @return PDOStatement */
*/ private function afterCheck(bool $result): bool
private function bindValue(PDOStatement $statement, array $params = []): PDOStatement {
{ $container = Kiri::getDi()->get(WorkerStatus::class);
if (empty($params)) return $statement; if (!$result || $container->is(StatusEnum::EXIT)) {
foreach ($params as $key => $param) { $this->pdo = null;
$statement->bindValue($key, $param); $result = Timer::clear($this->_timerId);
} $this->_timerId = -1;
return $statement; }
} return $result;
}
/** /**
* @param string $sql * @param PDOStatement $statement
* @param array $params * @param array $params
* @return int * @return PDOStatement
* @throws Exception */
*/ private function bindValue(PDOStatement $statement, array $params = []): PDOStatement
public function execute(string $sql, array $params = []): int {
{ if (empty($params)) return $statement;
$this->_last = time(); foreach ($params as $key => $param) {
$pdo = $this->_pdo(); $statement->bindValue($key, $param);
if (!(($prepare = $pdo->prepare($sql)) instanceof PDOStatement)) { }
throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE); return $statement;
} }
if ($prepare->execute($params) === false) {
throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE);
}
$result = (int)$pdo->lastInsertId();
$prepare->closeCursor();
if ($result == 0) {
return true;
}
return $result;
}
/** /**
* @return \PDO * @param string $sql
*/ * @param array $params
public function _pdo(): \PDO * @return int
{ * @throws Exception
if (!($this->pdo instanceof \PDO)) { */
$this->pdo = $this->newClient(); public function execute(string $sql, array $params = []): int
} {
return $this->pdo; $pdo = $this->_pdo();
} if ($this->_timerId === -1) {
$this->_timerId = Timer::tick(6000, [$this, 'check']);
}
$this->_last = time();
if (!(($prepare = $pdo->prepare($sql)) instanceof PDOStatement)) {
throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE);
}
if ($prepare->execute($params) === false) {
throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE);
}
$result = (int)$pdo->lastInsertId();
$prepare->closeCursor();
return $result == 0 ? true : $result;
}
/** /**
* @return \PDO * @return \PDO
*/ */
private function newClient(): \PDO public function _pdo(): \PDO
{ {
$link = new \PDO('mysql:dbname=' . $this->dbname . ';host=' . $this->cds, $this->username, $this->password, [ if (!($this->pdo instanceof \PDO)) {
\PDO::ATTR_EMULATE_PREPARES => false, $this->pdo = $this->newClient();
\PDO::ATTR_CASE => \PDO::CASE_NATURAL, }
\PDO::ATTR_TIMEOUT => $this->connect_timeout, return $this->pdo;
\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true, }
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $this->charset
]);
$link->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); /**
$link->setAttribute(\PDO::ATTR_STRINGIFY_FETCHES, false); * @return \PDO
$link->setAttribute(\PDO::ATTR_ORACLE_NULLS, \PDO::NULL_EMPTY_STRING); */
if (!empty($this->attributes)) { private function newClient(): \PDO
foreach ($this->attributes as $key => $attribute) { {
$link->setAttribute($key, $attribute); $link = new \PDO('mysql:dbname=' . $this->dbname . ';host=' . $this->cds, $this->username, $this->password, [
} \PDO::ATTR_EMULATE_PREPARES => false,
} \PDO::ATTR_CASE => \PDO::CASE_NATURAL,
return $link; \PDO::ATTR_TIMEOUT => $this->connect_timeout,
} \PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $this->charset
]);
$link->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$link->setAttribute(\PDO::ATTR_STRINGIFY_FETCHES, false);
$link->setAttribute(\PDO::ATTR_ORACLE_NULLS, \PDO::NULL_EMPTY_STRING);
if (!empty($this->attributes)) {
foreach ($this->attributes as $key => $attribute) {
$link->setAttribute($key, $attribute);
}
}
return $link;
}
} }