1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Clustered MySQL |
4
|
|
|
* User: moyo |
5
|
|
|
* Date: 02/11/2017 |
6
|
|
|
* Time: 12:05 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Carno\Database\Clusters; |
10
|
|
|
|
11
|
|
|
use Carno\Cluster\Classify\Scenes; |
12
|
|
|
use Carno\Cluster\Contracts\Tags; |
13
|
|
|
use Carno\Cluster\Managed; |
14
|
|
|
use Carno\Cluster\Resources; |
15
|
|
|
use Carno\Database\Chips\SQLDetector; |
16
|
|
|
use Carno\Database\Chips\SQLExecutor; |
17
|
|
|
use Carno\Database\Chips\TransactionGuard; |
18
|
|
|
use Carno\Database\Connectors\MySQL as Connector; |
19
|
|
|
use Carno\Database\Contracts\Executable; |
20
|
|
|
use Carno\Database\Options\Timeouts; |
21
|
|
|
use Carno\DSN\DSN; |
22
|
|
|
use Carno\Net\Endpoint; |
23
|
|
|
use Carno\Pool\Options; |
24
|
|
|
use Carno\Pool\Pool; |
25
|
|
|
use Carno\Promise\Promised; |
26
|
|
|
|
27
|
|
|
abstract class MySQL extends Managed implements Executable |
28
|
|
|
{ |
29
|
|
|
use SQLDetector, SQLExecutor, TransactionGuard; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $tags = [Tags::MASTER, Tags::SLAVE]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $type = 'mysql'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var int |
43
|
|
|
*/ |
44
|
|
|
protected $port = 3306; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
protected $timeout = 8500; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* MySQL constructor. |
53
|
|
|
* @param Resources $resources |
54
|
|
|
*/ |
55
|
|
|
public function __construct(Resources $resources) |
56
|
|
|
{ |
57
|
|
|
$resources->initialize(Scenes::RESOURCE, $this->type, $this->server, $this); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param Endpoint $endpoint |
62
|
|
|
* @return Options |
63
|
|
|
*/ |
64
|
|
|
abstract protected function options(Endpoint $endpoint) : Options; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param Endpoint $endpoint |
68
|
|
|
* @return Pool |
69
|
|
|
*/ |
70
|
|
|
protected function connecting(Endpoint $endpoint) : Pool |
71
|
|
|
{ |
72
|
|
|
$vid = "{$this->type}:{$this->server}"; |
73
|
|
|
|
74
|
|
|
$dsn = new DSN($endpoint->address()->host()); |
75
|
|
|
|
76
|
|
|
$timeouts = new Timeouts( |
77
|
|
|
$dsn->option('connect', 1500), |
|
|
|
|
78
|
|
|
$dsn->option('execute', $this->timeout) |
|
|
|
|
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
return new Pool($this->options($endpoint), static function () use ($timeouts, $dsn, $vid) { |
82
|
|
|
return new Connector( |
83
|
|
|
$dsn->host(), |
84
|
|
|
$dsn->port() ?: 3306, |
85
|
|
|
$dsn->user(), |
86
|
|
|
$dsn->pass(), |
87
|
|
|
$dsn->path(), |
88
|
|
|
$dsn->option('charset', 'utf8mb4'), |
|
|
|
|
89
|
|
|
$timeouts, |
90
|
|
|
$vid |
91
|
|
|
); |
92
|
|
|
}, $vid); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param Pool $connected |
97
|
|
|
* @return Promised |
98
|
|
|
*/ |
99
|
|
|
protected function disconnecting($connected) : Promised |
100
|
|
|
{ |
101
|
|
|
return $connected->shutdown(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $sql |
106
|
|
|
* @return Pool |
107
|
|
|
*/ |
108
|
|
|
protected function assigned(string $sql = '') : Pool |
109
|
|
|
{ |
110
|
|
|
return $this->picking( |
111
|
|
|
$this->clustered() && $this->readonly($sql) |
112
|
|
|
? Tags::SLAVE |
113
|
|
|
: Tags::MASTER |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|